aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/include/common.h2
-rw-r--r--src/lib/common.c39
2 files changed, 41 insertions, 0 deletions
diff --git a/src/include/common.h b/src/include/common.h
index 4c517e427..80f55803d 100644
--- a/src/include/common.h
+++ b/src/include/common.h
@@ -124,6 +124,8 @@ static inline unsigned long long getticks(void) {
124#endif 124#endif
125} 125}
126 126
127void timetrace_start(void);
128float timetrace_end(void);
127int join_namespace(pid_t pid, char *type); 129int join_namespace(pid_t pid, char *type);
128int name2pid(const char *name, pid_t *pid); 130int name2pid(const char *name, pid_t *pid);
129char *pid_proc_comm(const pid_t pid); 131char *pid_proc_comm(const pid_t pid);
diff --git a/src/lib/common.c b/src/lib/common.c
index b44563733..3d7fc5ffa 100644
--- a/src/lib/common.c
+++ b/src/lib/common.c
@@ -282,3 +282,42 @@ int pid_hidepid(void) {
282 fclose(fp); 282 fclose(fp);
283 return 0; 283 return 0;
284} 284}
285
286//**************************
287// time trace based on getticks function
288//**************************
289static int tt_not_implemented = 0; // not implemented for the current architecture
290static unsigned long long tt_1ms = 0;
291static unsigned long long tt = 0; // start time
292
293void timetrace_start(void) {
294 if (tt_not_implemented)
295 return;
296 unsigned long long t1 = getticks();
297 if (t1 == 0) {
298 tt_not_implemented = 1;
299 return;
300 }
301
302 if (tt_1ms == 0) {
303 usleep(1000); // sleep 1 ms
304 unsigned long long t2 = getticks();
305 tt_1ms = t2 - t1;
306 if (tt_1ms == 0) {
307 tt_not_implemented = 1;
308 return;
309 }
310 }
311
312 tt = getticks();
313}
314
315float timetrace_end(void) {
316 if (tt_not_implemented)
317 return 0;
318
319 unsigned long long delta = getticks() - tt;
320 assert(tt_1ms);
321
322 return (float) delta / (float) tt_1ms;
323}