Skip to contentSkip to conversation

linux-user: Implement starttime field in self stat emulation

PATCH Superseded Cameron Esfahani 2 participants 2 messages Jan 27, 2022

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 linux-user: Implement starttime field in self stat emulation

Instead of always returning 0, return actual starttime.

v2: Use clock_gettime() instead of scanning /proc/self/stat

---
 linux-user/main.c    | 11 +++++++++++
 linux-user/qemu.h    |  3 +++
 linux-user/syscall.c |  3 +++
 3 files changed, 17 insertions(+)
linux-user/main.c +11 -0
index 16def5215d..2e5e0e886b 100644
@@ -196,6 +196,17 @@ void init_task_state(TaskState *ts)
         .ss_size = 0,
         .ss_flags = TARGET_SS_DISABLE,
     };
+
+    /* Capture task start time relative to system boot */
+
+    long long ticksPerSec = sysconf(_SC_CLK_TCK);
+    struct timespec bt;
+
+    if ((ticksPerSec > 0) && !clock_gettime(CLOCK_BOOTTIME, &bt)) {
+        /* start_boottime is expressed in clock ticks */
+        ts->start_boottime = bt.tv_sec * ticksPerSec;
+        ts->start_boottime += bt.tv_nsec * ticksPerSec / 1000000000L;
+    }
 }
 
 CPUArchState *cpu_copy(CPUArchState *env)
linux-user/qemu.h +3 -0
index 7910ce59cc..86bc169e72 100644
@@ -158,6 +158,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 */
+    unsigned long long start_boottime;
 } __attribute__((aligned(16))) TaskState;
 
 abi_long do_brk(abi_ulong new_brk);
linux-user/syscall.c +3 -0
index 5950222a77..2f77dbdda7 100644
@@ -8107,6 +8107,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, "%llu ", ts->start_boottime);
         } else if (i == 27) {
             /* stack bottom */
             g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack);
Signature · 1 line
2.32.0 (Apple Git-131)
#1 lore raw

Le 27/01/2022 à 21:58, Cameron Esfahani a écrit :

Quoted 16 lines

Instead of always returning 0, return actual starttime.

v2: Use clock_gettime() instead of scanning /proc/self/stat

---
  linux-user/main.c    | 11 +++++++++++
  linux-user/qemu.h    |  3 +++
  linux-user/syscall.c |  3 +++
  3 files changed, 17 insertions(+)
linux-user/main.c +0 -0
index 16def5215d..2e5e0e886b 100644

linux-user/main.c void init_task_state(TaskState *ts) · line 202

Quoted 7 lines
linux-user/main.c +4 -0
@@ -196,6 +196,17 @@ void init_task_state(TaskState *ts)
          .ss_size = 0,
          .ss_flags = TARGET_SS_DISABLE,
      };
+
+    /* Capture task start time relative to system boot */
+
+    long long ticksPerSec = sysconf(_SC_CLK_TCK);

I don't think we need a "long long" here. sysconf() returns "long".

Use lower case with underscore

https://qemu-project.gitlab.io/qemu/devel/style.html#variable-naming-conventions

Put the declaration at the beginning of the function

https://qemu-project.gitlab.io/qemu/devel/style.html#declarations

linux-user/main.c void init_task_state(TaskState *ts) · line 207

Quoted 5 lines
linux-user/main.c +5 -0
+    struct timespec bt;
+
+    if ((ticksPerSec > 0) && !clock_gettime(CLOCK_BOOTTIME, &bt)) {
+        /* start_boottime is expressed in clock ticks */
+        ts->start_boottime = bt.tv_sec * ticksPerSec;

You can cast ticksPerSec with uint64_t to have a 64bit result.

linux-user/main.c void init_task_state(TaskState *ts) · line 208

Quoted 1 line
linux-user/main.c +1 -0
+        ts->start_boottime += bt.tv_nsec * ticksPerSec / 1000000000L;

Use NANOSECONDS_PER_SECOND

Quoted 9 lines
linux-user/main.c +1 -0
+    }
  }
 
  CPUArchState *cpu_copy(CPUArchState *env)
linux-user/qemu.h +0 -0
index 7910ce59cc..86bc169e72 100644

linux-user/qemu.h typedef struct TaskState { · line 163

Quoted 6 lines
linux-user/qemu.h +3 -0
@@ -158,6 +158,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 */
+    unsigned long long start_boottime;

Use uint64_t, like the kernel does for start_boottime.

Quoted 8 lines
linux-user/qemu.h +0 -0
  } __attribute__((aligned(16))) TaskState;
 
  abi_long do_brk(abi_ulong new_brk);
linux-user/syscall.c +0 -0
index 5950222a77..2f77dbdda7 100644

linux-user/syscall.c static int open_self_stat(void *cpu_env, int fd) · line 8112

Quoted 6 lines
linux-user/syscall.c +3 -0
@@ -8107,6 +8107,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, "%llu ", ts->start_boottime);

with uint64_t, use "%PRIu64 ".

linux-user/syscall.c static int open_self_stat(void *cpu_env, int fd) · line 8115

Quoted 3 lines
linux-user/syscall.c +0 -0
          } else if (i == 27) {
              /* stack bottom */
              g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack);

Except these minor tweaks, it looks good.

Thanks,
Laurent