diff options
author | netblue30 <netblue30@yahoo.com> | 2020-12-01 08:51:27 -0500 |
---|---|---|
committer | netblue30 <netblue30@yahoo.com> | 2020-12-01 08:51:27 -0500 |
commit | 411279afc10f58c9ee8a7e5599bd96c5ff32cd6c (patch) | |
tree | ef1e83410f9a5d56837a885297ae75f6209c82a9 /src/lib/common.c | |
parent | Add a profile for dolphin-emu (diff) | |
download | firejail-411279afc10f58c9ee8a7e5599bd96c5ff32cd6c.tar.gz firejail-411279afc10f58c9ee8a7e5599bd96c5ff32cd6c.tar.zst firejail-411279afc10f58c9ee8a7e5599bd96c5ff32cd6c.zip |
a more portable implementation for time measurements
Diffstat (limited to 'src/lib/common.c')
-rw-r--r-- | src/lib/common.c | 55 |
1 files changed, 30 insertions, 25 deletions
diff --git a/src/lib/common.c b/src/lib/common.c index 872772ec7..823442835 100644 --- a/src/lib/common.c +++ b/src/lib/common.c | |||
@@ -30,6 +30,7 @@ | |||
30 | #include <signal.h> | 30 | #include <signal.h> |
31 | #include <dirent.h> | 31 | #include <dirent.h> |
32 | #include <string.h> | 32 | #include <string.h> |
33 | #include <time.h> | ||
33 | #include "../include/common.h" | 34 | #include "../include/common.h" |
34 | #define BUFLEN 4096 | 35 | #define BUFLEN 4096 |
35 | 36 | ||
@@ -290,38 +291,42 @@ int pid_hidepid(void) { | |||
290 | //************************** | 291 | //************************** |
291 | // time trace based on getticks function | 292 | // time trace based on getticks function |
292 | //************************** | 293 | //************************** |
293 | static int tt_not_implemented = 0; // not implemented for the current architecture | 294 | typedef struct list_entry_t { |
294 | static unsigned long long tt_1ms = 0; | 295 | struct list_entry_t *next; |
295 | static unsigned long long tt = 0; // start time | 296 | struct timespec ts; |
297 | } ListEntry; | ||
296 | 298 | ||
297 | void timetrace_start(void) { | 299 | static ListEntry *ts_list = NULL; |
298 | if (tt_not_implemented) | ||
299 | return; | ||
300 | unsigned long long t1 = getticks(); | ||
301 | if (t1 == 0) { | ||
302 | tt_not_implemented = 1; | ||
303 | return; | ||
304 | } | ||
305 | 300 | ||
306 | if (tt_1ms == 0) { | 301 | static inline float msdelta(struct timespec *start, struct timespec *end) { |
307 | usleep(1000); // sleep 1 ms | 302 | unsigned sec = end->tv_sec - start->tv_sec; |
308 | unsigned long long t2 = getticks(); | 303 | long nsec = end->tv_nsec - start->tv_nsec; |
309 | tt_1ms = t2 - t1; | 304 | return (float) sec * 1000 + (float) nsec / 1000000; |
310 | if (tt_1ms == 0) { | 305 | } |
311 | tt_not_implemented = 1; | ||
312 | return; | ||
313 | } | ||
314 | } | ||
315 | 306 | ||
316 | tt = getticks(); | 307 | void timetrace_start(void) { |
308 | ListEntry *t = malloc(sizeof(ListEntry)); | ||
309 | if (!t) | ||
310 | errExit("malloc"); | ||
311 | memset(t, 0, sizeof(ListEntry)); | ||
312 | clock_gettime(CLOCK_MONOTONIC, &t->ts); | ||
313 | |||
314 | // add it to the list | ||
315 | t->next = ts_list; | ||
316 | ts_list = t; | ||
317 | } | 317 | } |
318 | 318 | ||
319 | float timetrace_end(void) { | 319 | float timetrace_end(void) { |
320 | if (tt_not_implemented) | 320 | if (!ts_list) |
321 | return 0; | 321 | return 0; |
322 | 322 | ||
323 | unsigned long long delta = getticks() - tt; | 323 | // remove start time from the list |
324 | assert(tt_1ms); | 324 | ListEntry *t = ts_list; |
325 | ts_list = t->next; | ||
325 | 326 | ||
326 | return (float) delta / (float) tt_1ms; | 327 | struct timespec end; |
328 | clock_gettime(CLOCK_MONOTONIC, &end); | ||
329 | float rv = msdelta(&t->ts, &end); | ||
330 | free(t); | ||
331 | return rv; | ||
327 | } | 332 | } |