Skip to content

Commit

Permalink
a more portable implementation for time measurements
Browse files Browse the repository at this point in the history
  • Loading branch information
netblue30 committed Dec 1, 2020
1 parent fb56a26 commit 411279a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 55 deletions.
3 changes: 1 addition & 2 deletions src/firejail/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ int fullargc = 0;
static pid_t child = 0;
pid_t sandbox_pid;
mode_t orig_umask = 022;
unsigned long long start_timestamp;

static void clear_atexit(void) {
EUID_ROOT();
Expand Down Expand Up @@ -1026,7 +1025,7 @@ int main(int argc, char **argv, char **envp) {
init_cfg(argc, argv);

// get starting timestamp, process --quiet
start_timestamp = getticks();
timetrace_start();
char *env_quiet = getenv("FIREJAIL_QUIET");
if (check_arg(argc, argv, "--quiet", 1) || (env_quiet && strcmp(env_quiet, "yes") == 0))
arg_quiet = 1;
Expand Down
15 changes: 2 additions & 13 deletions src/firejail/sandbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,19 +400,8 @@ static int monitor_application(pid_t app_pid) {
}

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) {
fmessage("Child process initialized in %.02f ms\n",
(float) (end_timestamp - start_timestamp) / (float) onems);
return;
}
}

fmessage("Child process initialized\n");
float delta = timetrace_end();
fmessage("Child process initialized in %.02f ms\n", delta);
}


Expand Down
15 changes: 0 additions & 15 deletions src/include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,6 @@ 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);
#elif defined(__i386__)
unsigned long long ret;
__asm__ __volatile__("rdtsc" : "=A" (ret));
return ret;
#else
return 0; // not implemented
#endif
}

void timetrace_start(void);
float timetrace_end(void);
int join_namespace(pid_t pid, char *type);
Expand Down
55 changes: 30 additions & 25 deletions src/lib/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <signal.h>
#include <dirent.h>
#include <string.h>
#include <time.h>
#include "../include/common.h"
#define BUFLEN 4096

Expand Down Expand Up @@ -290,38 +291,42 @@ int pid_hidepid(void) {
//**************************
// time trace based on getticks function
//**************************
static int tt_not_implemented = 0; // not implemented for the current architecture
static unsigned long long tt_1ms = 0;
static unsigned long long tt = 0; // start time
typedef struct list_entry_t {
struct list_entry_t *next;
struct timespec ts;
} ListEntry;

void timetrace_start(void) {
if (tt_not_implemented)
return;
unsigned long long t1 = getticks();
if (t1 == 0) {
tt_not_implemented = 1;
return;
}
static ListEntry *ts_list = NULL;

if (tt_1ms == 0) {
usleep(1000); // sleep 1 ms
unsigned long long t2 = getticks();
tt_1ms = t2 - t1;
if (tt_1ms == 0) {
tt_not_implemented = 1;
return;
}
}
static inline float msdelta(struct timespec *start, struct timespec *end) {
unsigned sec = end->tv_sec - start->tv_sec;
long nsec = end->tv_nsec - start->tv_nsec;
return (float) sec * 1000 + (float) nsec / 1000000;
}

tt = getticks();
void timetrace_start(void) {
ListEntry *t = malloc(sizeof(ListEntry));
if (!t)
errExit("malloc");
memset(t, 0, sizeof(ListEntry));
clock_gettime(CLOCK_MONOTONIC, &t->ts);

// add it to the list
t->next = ts_list;
ts_list = t;
}

float timetrace_end(void) {
if (tt_not_implemented)
if (!ts_list)
return 0;

unsigned long long delta = getticks() - tt;
assert(tt_1ms);
// remove start time from the list
ListEntry *t = ts_list;
ts_list = t->next;

return (float) delta / (float) tt_1ms;
struct timespec end;
clock_gettime(CLOCK_MONOTONIC, &end);
float rv = msdelta(&t->ts, &end);
free(t);
return rv;
}

0 comments on commit 411279a

Please sign in to comment.