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 | |
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')
-rw-r--r-- | src/firejail/main.c | 3 | ||||
-rw-r--r-- | src/firejail/sandbox.c | 15 | ||||
-rw-r--r-- | src/include/common.h | 15 | ||||
-rw-r--r-- | src/lib/common.c | 55 |
4 files changed, 33 insertions, 55 deletions
diff --git a/src/firejail/main.c b/src/firejail/main.c index 676d04895..53300fe2d 100644 --- a/src/firejail/main.c +++ b/src/firejail/main.c | |||
@@ -161,7 +161,6 @@ int fullargc = 0; | |||
161 | static pid_t child = 0; | 161 | static pid_t child = 0; |
162 | pid_t sandbox_pid; | 162 | pid_t sandbox_pid; |
163 | mode_t orig_umask = 022; | 163 | mode_t orig_umask = 022; |
164 | unsigned long long start_timestamp; | ||
165 | 164 | ||
166 | static void clear_atexit(void) { | 165 | static void clear_atexit(void) { |
167 | EUID_ROOT(); | 166 | EUID_ROOT(); |
@@ -1026,7 +1025,7 @@ int main(int argc, char **argv, char **envp) { | |||
1026 | init_cfg(argc, argv); | 1025 | init_cfg(argc, argv); |
1027 | 1026 | ||
1028 | // get starting timestamp, process --quiet | 1027 | // get starting timestamp, process --quiet |
1029 | start_timestamp = getticks(); | 1028 | timetrace_start(); |
1030 | char *env_quiet = getenv("FIREJAIL_QUIET"); | 1029 | char *env_quiet = getenv("FIREJAIL_QUIET"); |
1031 | if (check_arg(argc, argv, "--quiet", 1) || (env_quiet && strcmp(env_quiet, "yes") == 0)) | 1030 | if (check_arg(argc, argv, "--quiet", 1) || (env_quiet && strcmp(env_quiet, "yes") == 0)) |
1032 | arg_quiet = 1; | 1031 | arg_quiet = 1; |
diff --git a/src/firejail/sandbox.c b/src/firejail/sandbox.c index 41951f38f..5c7b5e556 100644 --- a/src/firejail/sandbox.c +++ b/src/firejail/sandbox.c | |||
@@ -400,19 +400,8 @@ static int monitor_application(pid_t app_pid) { | |||
400 | } | 400 | } |
401 | 401 | ||
402 | static void print_time(void) { | 402 | static void print_time(void) { |
403 | if (start_timestamp) { | 403 | float delta = timetrace_end(); |
404 | unsigned long long end_timestamp = getticks(); | 404 | fmessage("Child process initialized in %.02f ms\n", delta); |
405 | // measure 1 ms | ||
406 | usleep(1000); | ||
407 | unsigned long long onems = getticks() - end_timestamp; | ||
408 | if (onems) { | ||
409 | fmessage("Child process initialized in %.02f ms\n", | ||
410 | (float) (end_timestamp - start_timestamp) / (float) onems); | ||
411 | return; | ||
412 | } | ||
413 | } | ||
414 | |||
415 | fmessage("Child process initialized\n"); | ||
416 | } | 405 | } |
417 | 406 | ||
418 | 407 | ||
diff --git a/src/include/common.h b/src/include/common.h index 2fa61cc91..5df51c5a9 100644 --- a/src/include/common.h +++ b/src/include/common.h | |||
@@ -118,21 +118,6 @@ static inline int mac_not_zero(const unsigned char mac[6]) { | |||
118 | return 0; | 118 | return 0; |
119 | } | 119 | } |
120 | 120 | ||
121 | // rtdsc timestamp on x86-64/amd64 processors | ||
122 | static inline unsigned long long getticks(void) { | ||
123 | #if defined(__x86_64__) | ||
124 | unsigned a, d; | ||
125 | asm volatile("rdtsc" : "=a" (a), "=d" (d)); | ||
126 | return ((unsigned long long)a) | (((unsigned long long)d) << 32); | ||
127 | #elif defined(__i386__) | ||
128 | unsigned long long ret; | ||
129 | __asm__ __volatile__("rdtsc" : "=A" (ret)); | ||
130 | return ret; | ||
131 | #else | ||
132 | return 0; // not implemented | ||
133 | #endif | ||
134 | } | ||
135 | |||
136 | void timetrace_start(void); | 121 | void timetrace_start(void); |
137 | float timetrace_end(void); | 122 | float timetrace_end(void); |
138 | int join_namespace(pid_t pid, char *type); | 123 | int join_namespace(pid_t pid, char *type); |
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 | } |