From 97e789c90b5ab45ffa3237dd2bd5da6158375f7a Mon Sep 17 00:00:00 2001 From: netblue30 Date: Tue, 9 May 2017 09:59:00 -0400 Subject: measure start time --- src/firejail/firejail.h | 1 + src/firejail/main.c | 15 ++++++--------- src/firejail/sandbox.c | 20 ++++++++++++++++++-- src/include/common.h | 11 +++++++++++ 4 files changed, 36 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/firejail/firejail.h b/src/firejail/firejail.h index 6b1786a93..fba4c4fe2 100644 --- a/src/firejail/firejail.h +++ b/src/firejail/firejail.h @@ -354,6 +354,7 @@ extern int login_shell; extern int parent_to_child_fds[2]; extern int child_to_parent_fds[2]; extern pid_t sandbox_pid; +extern unsigned long long start_timestamp; #define MAX_ARGS 128 // maximum number of command arguments (argc) extern char *fullargv[MAX_ARGS]; diff --git a/src/firejail/main.c b/src/firejail/main.c index 4357ddaa4..9e5c31c32 100644 --- a/src/firejail/main.c +++ b/src/firejail/main.c @@ -37,15 +37,6 @@ #include #include -#if 0 -#include -{ -struct tms tm; -clock_t systick = times(&tm); -printf("time %s:%d %u\n", __FILE__, __LINE__, (uint32_t) systick); -} -#endif - uid_t firejail_uid = 0; gid_t firejail_gid = 0; @@ -127,6 +118,7 @@ char *fullargv[MAX_ARGS]; // expanded argv for restricted shell int fullargc = 0; static pid_t child = 0; pid_t sandbox_pid; +unsigned long long start_timestamp; static void set_name_file(pid_t pid); static void delete_name_file(pid_t pid); @@ -826,6 +818,11 @@ int main(int argc, char **argv) { char *custom_profile_dir = NULL; // custom profile directory int arg_noprofile = 0; // use default.profile if none other found/specified + + // get starting timestamp + start_timestamp = getticks(); + + // build /run/firejail directory structure preproc_build_firejail_dir(); diff --git a/src/firejail/sandbox.c b/src/firejail/sandbox.c index e6deddac5..9ecb1c8cc 100644 --- a/src/firejail/sandbox.c +++ b/src/firejail/sandbox.c @@ -274,6 +274,22 @@ void start_audit(void) { exit(1); } +static void print_time(void) { + if (start_timestamp) { + unsigned long long end_timestamp = getticks(); + // measure 1 ms + usleep(1000); + unsigned long long onems = getticks() - end_timestamp; + if (onems) { + printf("Child process initialized in %.02f ms\n", + (float) (end_timestamp - start_timestamp) / (float) onems); + return; + } + } + + printf("Child process initialized\n"); +} + void start_application(void) { //if (setsid() == -1) //errExit("setsid"); @@ -315,7 +331,7 @@ void start_application(void) { } if (!arg_command && !arg_quiet) - printf("Child process initialized\n"); + print_time(); #ifdef HAVE_GCOV __gcov_dump(); @@ -366,7 +382,7 @@ void start_application(void) { } if (!arg_command && !arg_quiet) - printf("Child process initialized\n"); + print_time(); #ifdef HAVE_GCOV __gcov_dump(); #endif diff --git a/src/include/common.h b/src/include/common.h index fc4059334..59b7f6213 100644 --- a/src/include/common.h +++ b/src/include/common.h @@ -109,6 +109,17 @@ static inline int mac_not_zero(const unsigned char mac[6]) { return 0; } +// rtdsc timestamp on x86-64/amd64 processors +static inline unsigned long long getticks(void) { +#if defined(__x86_64__) + unsigned a, d; + asm volatile("rdtsc" : "=a" (a), "=d" (d)); + return ((unsigned long long)a) | (((unsigned long long)d) << 32); +#else + return 0; // not implemented +#endif +} + int join_namespace(pid_t pid, char *type); int name2pid(const char *name, pid_t *pid); char *pid_proc_comm(const pid_t pid); -- cgit v1.2.3-54-g00ecf