ulib/clock.c

filter: Statementstmt 83.0% 88 / 106 Branchbran 67.1% 47 / 70 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 76.7% 135 / 176 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« ulib/util.c ↑ Summary ulib/chacha.c »
1 #ifdef __cplusplus
2 extern "C" {
3 #endif
4  
5 #include "ulib/clock.h"
6 #include "ulib/chacha.h"
7 #include "ulib/gettime.h"
8  
9 #ifdef __cplusplus
10 }
11 #endif
12  
13 #ifdef USE_WIN32_NATIVE
14 #define ftruncate(a,b) _chsize(a,b)
15 #endif
16 #undef open
17  
18 #define state_fd UCXT.clock_state_fd
19 #define state_f UCXT.clock_state_f
20 #define thread_persist UCXT.clock_persist
21 #define adjustment SMEM->clock_adj
22 #define last SMEM->clock_last
23 #define clock_seq SMEM->clock_seq
24 #define prev_reg SMEM->clock_prev_reg
25 #define defer_100ns SMEM->clock_defer_100ns
26 #define global_persist SMEM->clock_persist
27  
28 /* state_fd:
29 * -4 cannot create
30 * -3 untried
31 * -2 symlink
32 * -1 can create
33 * >=0 open fd
34 */
35 #define STATEFD_NOCREATE -4
36 #define STATEFD_UNTRIED -3
37 #define STATEFD_SYMLINK -2
38 #define STATEFD_CREATEOK -1
39  
40  
41 /* compare paths, return 0 if same or longer length if not */
42 54171 static IV persistNE(persist_t *a, persist_t *b) {
43 54171 STRLEN longer = a->len >= b->len ? a->len : b->len;
44 UV i;
45 54171 ▶ if (a->len != b->len)
Branch: 1 branch
BranchTrueFalseTotal
if (a->len != b->len) 0 54171 54171
46 0 return longer;
47 54625 ▶ for (i=0 ; i<longer ; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i=0 ; i<longer ; ++i) 454 54171 54625
48 454 ▶ if(a->path[i] != b->path[i])
Branch: 1 branch
BranchTrueFalseTotal
if(a->path[i] != b->path[i]) 0 454 454
49 0 return longer;
50 54171 return 0;
51 }
52  
53 /* called at boot */
54 204 void uu_clock_init(pUCXT) {
55 UV ptod[2];
56  
57 /* gettimeofday(&tv, 0); */
58 204 (*uu_gettime_U2time)(aTHX_ ptod);
59 204 last.tv_sec = (long)ptod[0];
60 204 last.tv_usec = (long)ptod[1];
61  
62 204 uu_chacha_rand16(aUCXT, &clock_seq);
63 204 clock_seq &= 0x3fff;
64  
65 204 state_fd = STATEFD_UNTRIED;
66 204 state_f = NULL;
67 204 adjustment = 0;
68 204 prev_reg = 0;
69 204 defer_100ns = 0;
70 204 Zero(&global_persist, 1, persist_t);
71 204 Zero(&thread_persist, 1, persist_t);
72 204 }
73  
74 10 void uu_clock_getpath(pUCXT, persist_t *persist) {
75 10 Copy(&global_persist, &thread_persist, 1, persist_t);
76 10 Copy(&thread_persist, persist, 1, persist_t);
77 10 }
78  
79 30 void uu_clock_setpath(pUCXT, persist_t *persist) {
80 30 Copy(persist, &thread_persist, 1, persist_t);
81 30 Copy(persist, &global_persist, 1, persist_t);
82 30 ▶ if (state_fd >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (state_fd >= 0) 7 23 30
83 7 fclose(state_f);
84 30 state_fd = STATEFD_UNTRIED;
85 /* doing this would break inter-process deferrals */
86 /* prev_reg = 0; */
87 30 }
88  
89 /* returns 100ns intervals since unix epoch.
90 * since gettimeofday() is in 1usec intervals,
91 * last digit is simulated via adjustment.
92 */
93 54171 IV uu_clock(pUCXT, U64 *ret_clock_reg, U16 *ret_clock_seq) {
94 struct timeval tv;
95 mode_t save_umask;
96 U64 clock_reg;
97 STRLEN longer;
98 UV ptod[2];
99 #ifdef HAVE_LSTAT
100 struct stat statbuf;
101 #endif
102  
103 54171 ▶ if ((longer = persistNE(&global_persist, &thread_persist))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((longer = persistNE(&global_persist, &thread_persist))) { 0 54171 54171
104 0 Copy(&global_persist, &thread_persist, longer, UCHAR);
105 0 ▶ if (state_fd >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (state_fd >= 0) 0 0 0
106 0 fclose(state_f);
107 0 state_fd = STATEFD_UNTRIED;
108 }
109  
110 54171 ▶ if (state_fd == STATEFD_UNTRIED) {
Branch: 1 branch
BranchTrueFalseTotal
if (state_fd == STATEFD_UNTRIED) { 106 54065 54171
111 #ifdef HAVE_LSTAT
112 106 ▶ if (lstat((char*)thread_persist.path, &statbuf) < 0) { /* this covers EINTR too.. ugh */
Branch: 1 branch
BranchTrueFalseTotal
if (lstat((char*)thread_persist.path, &statbuf) < 0) { /* this covers EINTR too.. ugh */ 103 3 106
113 103 ▶ if (errno == ENOENT)
Branch: 1 branch
BranchTrueFalseTotal
if (errno == ENOENT) 103 0 103
114 103 state_fd = STATEFD_CREATEOK;
115 else
116 0 state_fd = STATEFD_NOCREATE;
117 }
118 3 ▶ else if ((statbuf.st_mode & S_IFMT) == S_IFLNK)
Branch: 1 branch
BranchTrueFalseTotal
else if ((statbuf.st_mode & S_IFMT) == S_IFLNK) 1 2 3
119 1 state_fd = STATEFD_SYMLINK;
120 else {
121 #endif
122 2 state_fd = open((char*)thread_persist.path, O_RDWR);
123 2 ▶ if (state_fd < 0 && errno == ENOENT)
Branch: 2 branches
BranchTrueFalseTotal
if (state_fd < 0 && errno == ENOENT) 0 2 2
if (state_fd < 0 && errno == ENOENT) 0 0 0
124 0 state_fd = STATEFD_CREATEOK;
125 2 ▶ else if (state_fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (state_fd >= 0) { 2 0 2
126 #ifdef HAVE_LSTAT
127 2 state_f = NULL;
128 2 ▶ if ((lstat((char*)thread_persist.path, &statbuf) == 0) && ((statbuf.st_mode & S_IFMT) != S_IFLNK))
Branch: 2 branches
BranchTrueFalseTotal
if ((lstat((char*)thread_persist.path, &statbuf) == 0) && ((statbuf.st_mode & S_IFMT) != S_IFLNK)) 2 0 2
if ((lstat((char*)thread_persist.path, &statbuf) == 0) && ((statbuf.st_mode & S_IFMT) != S_IFLNK)) 2 0 2
129 #endif
130 2 state_f = fdopen(state_fd, "r+");
131 2 ▶ if (!state_f) {
Branch: 1 branch
BranchTrueFalseTotal
if (!state_f) { 0 2 2
132 0 close(state_fd);
133 0 state_fd = STATEFD_SYMLINK;
134 }
135 }
136 #ifdef HAVE_LSTAT
137 }
138 #endif
139 }
140  
141 54171 ▶ if (state_fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (state_fd >= 0) { 2 54169 54171
142 unsigned int cl;
143 unsigned long tv1, tv2;
144 int a;
145  
146 2 rewind(state_f);
147  
148 2 ▶ if (fscanf(state_f, "clock: %04x tv: %lu %lu adj: %d\n", &cl, &tv1, &tv2, &a) == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (fscanf(state_f, "clock: %04x tv: %lu %lu adj: %d\n", &cl, &tv1, &tv2, &a) == 4) { 1 1 2
149 1 clock_seq = cl & 0x3fff;
150 1 last.tv_sec = tv1;
151 1 last.tv_usec = tv2;
152 1 adjustment = a;
153 }
154 }
155  
156 /* gettimeofday(&tv, 0); */
157 54171 (*uu_gettime_U2time)(aTHX_ ptod);
158 54171 tv.tv_sec = (long)ptod[0];
159 54171 tv.tv_usec = (long)ptod[1];
160  
161 54171 ▶ if ((tv.tv_sec == last.tv_sec) && (tv.tv_usec == last.tv_usec)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((tv.tv_sec == last.tv_sec) && (tv.tv_usec == last.tv_usec)) { 54143 28 54171
if ((tv.tv_sec == last.tv_sec) && (tv.tv_usec == last.tv_usec)) { 3182 50961 54143
162 /* same time as last */
163 3182 ▶ if (++adjustment >= MAX_ADJUSTMENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (++adjustment >= MAX_ADJUSTMENT) { 0 3182 3182
164 0 clock_seq = (clock_seq+1) & 0x3fff;
165 0 adjustment = 0;
166 }
167 }
168 else {
169 /* time changed */
170 50989 ▶ if (((tv.tv_sec == last.tv_sec) && (tv.tv_usec < last.tv_usec)) || (tv.tv_sec < last.tv_sec))
Branch: 3 branches
BranchTrueFalseTotal
if (((tv.tv_sec == last.tv_sec) && (tv.tv_usec < last.tv_usec)) || (tv.tv_sec < last.tv_sec)) 50961 28 50989
if (((tv.tv_sec == last.tv_sec) && (tv.tv_usec < last.tv_usec)) || (tv.tv_sec < last.tv_sec)) 50961 0 50961
if (((tv.tv_sec == last.tv_sec) && (tv.tv_usec < last.tv_usec)) || (tv.tv_sec < last.tv_sec)) 0 50989 50989
171 0 clock_seq = (clock_seq+1) & 0x3fff; /* time moved backward */
172 50989 adjustment = 0;
173 50989 last = tv;
174 }
175  
176 54171 clock_reg = tv.tv_usec*10 + adjustment;
177 54171 clock_reg += ((U64)tv.tv_sec)*10000000;
178 /* *clock_reg += (((U64)0x01b21dd2) << 32) + 0x13814000; */
179  
180 54171 ▶ if ((clock_reg - prev_reg) >= defer_100ns) {
Branch: 1 branch
BranchTrueFalseTotal
if ((clock_reg - prev_reg) >= defer_100ns) { 54169 2 54171
181 54169 ▶ if (state_fd == STATEFD_CREATEOK) {
Branch: 1 branch
BranchTrueFalseTotal
if (state_fd == STATEFD_CREATEOK) { 101 54068 54169
182 #ifdef HAVE_LSTAT
183 101 ▶ if ((lstat((char*)thread_persist.path, &statbuf) == 0) && ((statbuf.st_mode & S_IFMT) == S_IFLNK))
Branch: 2 branches
BranchTrueFalseTotal
if ((lstat((char*)thread_persist.path, &statbuf) == 0) && ((statbuf.st_mode & S_IFMT) == S_IFLNK)) 0 101 101
if ((lstat((char*)thread_persist.path, &statbuf) == 0) && ((statbuf.st_mode & S_IFMT) == S_IFLNK)) 0 0 0
184 0 state_fd = STATEFD_SYMLINK;
185 else {
186 #endif
187 101 save_umask = umask(0);
188 101 state_fd = open((char*)thread_persist.path, O_RDWR|O_CREAT, 0660);
189 101 ▶ if (state_fd < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (state_fd < 0) 95 6 101
190 95 state_fd = STATEFD_NOCREATE;
191 101 (void) umask(save_umask);
192 101 ▶ if (state_fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (state_fd >= 0) { 6 95 101
193 #ifdef HAVE_LSTAT
194 6 state_f = NULL;
195 6 ▶ if ((lstat((char*)thread_persist.path, &statbuf) == 0) && ((statbuf.st_mode & S_IFMT) != S_IFLNK))
Branch: 2 branches
BranchTrueFalseTotal
if ((lstat((char*)thread_persist.path, &statbuf) == 0) && ((statbuf.st_mode & S_IFMT) != S_IFLNK)) 6 0 6
if ((lstat((char*)thread_persist.path, &statbuf) == 0) && ((statbuf.st_mode & S_IFMT) != S_IFLNK)) 6 0 6
196 #endif
197 6 state_f = fdopen(state_fd, "r+");
198 6 ▶ if (!state_f) {
Branch: 1 branch
BranchTrueFalseTotal
if (!state_f) { 0 6 6
199 0 close(state_fd);
200 0 state_fd = STATEFD_SYMLINK;
201 }
202 }
203 #ifdef HAVE_LSTAT
204 }
205 #endif
206 }
207  
208 54169 ▶ if (state_fd > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (state_fd > 0) { 8 54161 54169
209 8 rewind(state_f);
210 16 long len = fprintf(state_f,
211 "clock: %04x tv: %016lu %08lu adj: %08d\n",
212 8 clock_seq, (unsigned long)last.tv_sec,
213 8 (unsigned long)last.tv_usec, adjustment);
214 8 fflush(state_f);
215 8 ▶ if (ftruncate(state_fd, len) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ftruncate(state_fd, len) < 0) { 0 8 8
216 0 fprintf(state_f, " \n");
217 0 fflush(state_f);
218 }
219 8 rewind(state_f);
220 }
221 }
222  
223 54171 prev_reg = clock_reg;
224  
225 /* *clock_high = clock_reg >> 32; */
226 /* *clock_low = (U32)clock_reg; */
227 54171 *ret_clock_reg = clock_reg;
228 54171 *ret_clock_seq = clock_seq;
229 54171 return 0;
230 }
231  
232 /* ex:set ts=2 sw=2 itab=spaces: */
« ulib/util.c ↑ Summary ulib/chacha.c »