implement user/sys time + max rss
Stefan Schuermans

Stefan Schuermans commited on 2020-05-21 12:27:46
Showing 4 changed files, with 57 additions and 11 deletions.

... ...
@@ -6,6 +6,8 @@
6 6
 #include <uproctrace.pb-c.h>
7 7
 
8 8
 #include <stdlib.h>
9
+#include <sys/resource.h>
10
+#include <sys/time.h>
9 11
 #include <sys/types.h>
10 12
 #include <unistd.h>
11 13
 
... ...
@@ -21,12 +23,24 @@ int uptev_proc_end(void **data, size_t *size) {
21 23
   struct _Uproctrace__Timespec timestamp = UPROCTRACE__TIMESPEC__INIT;
22 24
   uptev_timing_get_timestamp(&timestamp);
23 25
 
24
-  struct _Uproctrace__Timespec proc_cpu_time = UPROCTRACE__TIMESPEC__INIT;
25
-  uptev_timing_get_proc_cpu_time(&proc_cpu_time);
26
+  struct _Uproctrace__Timespec cpu_time = UPROCTRACE__TIMESPEC__INIT;
27
+  uptev_timing_get_proc_cpu_time(&cpu_time);
26 28
 
27 29
   struct _Uproctrace__ProcEnd proc_end = UPROCTRACE__PROC_END__INIT;
28 30
   proc_end.pid = getpid();
29
-  proc_end.proc_cpu_time = &proc_cpu_time;
31
+  proc_end.cpu_time = &cpu_time;
32
+
33
+  struct rusage usage;
34
+  struct _Uproctrace__Timespec user_time = UPROCTRACE__TIMESPEC__INIT;
35
+  struct _Uproctrace__Timespec sys_time = UPROCTRACE__TIMESPEC__INIT;
36
+  if (getrusage(RUSAGE_SELF, &usage) == 0) {
37
+    uptev_timing_timeval_to_pb(&usage.ru_utime, &user_time);
38
+    proc_end.user_time = &user_time;
39
+    uptev_timing_timeval_to_pb(&usage.ru_stime, &sys_time);
40
+    proc_end.sys_time = &sys_time;
41
+    proc_end.has_max_rss_kb = 1;
42
+    proc_end.max_rss_kb = usage.ru_maxrss;
43
+  }
30 44
 
31 45
   struct _Uproctrace__Event event = UPROCTRACE__EVENT__INIT;
32 46
   event.timestamp = &timestamp;
... ...
@@ -2,22 +2,32 @@
2 2
 
3 3
 #include <uproctrace.pb-c.h>
4 4
 
5
+#include <sys/time.h>
5 6
 #include <time.h>
6 7
 
7
-static void uptev_timing_clock_gettime(clockid_t clockid,
8
+void uptev_timing_timeval_to_pb(struct timeval const *tv,
8 9
                                 struct _Uproctrace__Timespec *tsp) {
9
-  struct timespec ts;
10
-  clock_gettime(clockid, &ts);
11
-  tsp->sec = ts.tv_sec;
10
+  tsp->sec = tv->tv_sec;
12 11
   tsp->has_nsec = 1;
13
-  tsp->nsec = ts.tv_nsec;
12
+  tsp->nsec = tv->tv_usec * 1000;
13
+}
14
+
15
+void uptev_timing_timespec_to_pb(struct timespec const *ts,
16
+                                 struct _Uproctrace__Timespec *tsp) {
17
+  tsp->sec = ts->tv_sec;
18
+  tsp->has_nsec = 1;
19
+  tsp->nsec = ts->tv_nsec;
14 20
 }
15 21
 
16 22
 void uptev_timing_get_timestamp(struct _Uproctrace__Timespec *timestamp) {
17
-  uptev_timing_clock_gettime(CLOCK_REALTIME, timestamp);
23
+  struct timespec ts;
24
+  clock_gettime(CLOCK_REALTIME, &ts);
25
+  uptev_timing_timespec_to_pb(&ts, timestamp);
18 26
 }
19 27
 
20 28
 void uptev_timing_get_proc_cpu_time(
21 29
     struct _Uproctrace__Timespec *proc_cpu_time) {
22
-  uptev_timing_clock_gettime(CLOCK_PROCESS_CPUTIME_ID, proc_cpu_time);
30
+  struct timespec ts;
31
+  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
32
+  uptev_timing_timespec_to_pb(&ts, proc_cpu_time);
23 33
 }
... ...
@@ -3,6 +3,25 @@
3 3
 
4 4
 #include <uproctrace.pb-c.h>
5 5
 
6
+#include <sys/time.h>
7
+#include <time.h>
8
+
9
+/**
10
+ * @brief convert timeval to protobuf
11
+ * @param[in] ts timeval to convert
12
+ * @param[in,out] timestamp initialized structure to set to current time
13
+ */
14
+void uptev_timing_timeval_to_pb(struct timeval const *tv,
15
+                                struct _Uproctrace__Timespec *tsp);
16
+
17
+/**
18
+ * @brief convert timespec to protobuf
19
+ * @param[in] ts timespec to convert
20
+ * @param[in,out] timestamp initialized structure to set to current time
21
+ */
22
+void uptev_timing_timespec_to_pb(struct timespec const *ts,
23
+                                 struct _Uproctrace__Timespec *tsp);
24
+
6 25
 /**
7 26
  * @brief fill timestamp with current time
8 27
  * @param[in,out] timestamp initialized structure to set to current time
... ...
@@ -22,7 +22,10 @@ message proc_begin {
22 22
 
23 23
 message proc_end {
24 24
   required int32 pid = 1;
25
-  optional timespec proc_cpu_time = 2;
25
+  optional timespec cpu_time = 2;
26
+  optional timespec user_time = 3;
27
+  optional timespec sys_time = 4;
28
+  optional int64 max_rss_kb = 5;
26 29
 }
27 30
 
28 31
 message event {
29 32