linux-user: Implement starttime field in self stat emulation
authorCameron Esfahani <dirty@apple.com>
Fri, 28 Jan 2022 00:12:51 +0000 (16:12 -0800)
committerLaurent Vivier <laurent@vivier.eu>
Fri, 28 Jan 2022 10:08:54 +0000 (11:08 +0100)
Instead of always returning 0, return actual starttime.

Signed-off-by: Cameron Esfahani <dirty@apple.com>
Reviewed-by: Laurent Vivier <laurent@vivier.eu>
Message-Id: <20220128001251.45165-1-dirty@apple.com>
Signed-off-by: Laurent Vivier <laurent@vivier.eu>
linux-user/main.c
linux-user/qemu.h
linux-user/syscall.c

index 16def5215d917316cc62affbfaeb7ff4aabb22ed..fbc9bcfd5f5f4d4e0f869db8eaff0ecef3daed6e 100644 (file)
@@ -190,12 +190,26 @@ void stop_all_tasks(void)
 /* Assumes contents are already zeroed.  */
 void init_task_state(TaskState *ts)
 {
+    long ticks_per_sec;
+    struct timespec bt;
+
     ts->used = 1;
     ts->sigaltstack_used = (struct target_sigaltstack) {
         .ss_sp = 0,
         .ss_size = 0,
         .ss_flags = TARGET_SS_DISABLE,
     };
+
+    /* Capture task start time relative to system boot */
+
+    ticks_per_sec = sysconf(_SC_CLK_TCK);
+
+    if ((ticks_per_sec > 0) && !clock_gettime(CLOCK_BOOTTIME, &bt)) {
+        /* start_boottime is expressed in clock ticks */
+        ts->start_boottime = bt.tv_sec * (uint64_t) ticks_per_sec;
+        ts->start_boottime += bt.tv_nsec * (uint64_t) ticks_per_sec /
+                              NANOSECONDS_PER_SECOND;
+    }
 }
 
 CPUArchState *cpu_copy(CPUArchState *env)
index 9d2b3119d1fe2fd273a8e8d670f4fdd46677ca5a..98dfbf20962b6ca443a78865c811d05badf0a554 100644 (file)
@@ -154,6 +154,9 @@ typedef struct TaskState {
 
     /* This thread's sigaltstack, if it has one */
     struct target_sigaltstack sigaltstack_used;
+
+    /* Start time of task after system boot in clock ticks */
+    uint64_t start_boottime;
 } TaskState;
 
 abi_long do_brk(abi_ulong new_brk);
index 84cfa223df4e3b72184018794afe099ea8f1ac24..b3948d13a9dd963bbcdc484b7c6b072bcc7cca0b 100644 (file)
@@ -8077,6 +8077,9 @@ static int open_self_stat(void *cpu_env, int fd)
         } else if (i == 3) {
             /* ppid */
             g_string_printf(buf, FMT_pid " ", getppid());
+        } else if (i == 21) {
+            /* starttime */
+            g_string_printf(buf, "%" PRIu64 " ", ts->start_boottime);
         } else if (i == 27) {
             /* stack bottom */
             g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack);