]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
feat: times() syscall (84) — per-process CPU time accounting (utime/stime fields...
authorTulio A M Mendes <[email protected]>
Thu, 12 Feb 2026 03:49:04 +0000 (00:49 -0300)
committerTulio A M Mendes <[email protected]>
Fri, 13 Feb 2026 02:20:50 +0000 (23:20 -0300)
include/process.h
include/syscall.h
src/kernel/scheduler.c
src/kernel/syscall.c
user/ulibc/include/sys/times.h [new file with mode: 0644]
user/ulibc/include/syscall.h
user/ulibc/src/times.c [new file with mode: 0644]

index f2d32b70ee21d8db3e319248a634ae2c67081be4..173034c39cc93cae7fbf8337088c474b267cd18d 100644 (file)
@@ -67,6 +67,8 @@ struct process {
     process_state_t state;
     uint32_t wake_at_tick;
     uint32_t alarm_tick;
+    uint32_t utime;             /* ticks spent in user mode */
+    uint32_t stime;             /* ticks spent in kernel mode */
     int exit_status;
 
     int has_user_regs;
index 2cdb5a1c0a6fe2f93557b9c8514b39b536e30d2e..b2c8426ff3bd6a8eb6d2060f49826e060282f7bb 100644 (file)
@@ -116,6 +116,7 @@ enum {
     SYSCALL_READV     = 81,
     SYSCALL_WRITEV    = 82,
     SYSCALL_ALARM     = 83,
+    SYSCALL_TIMES     = 84,
 };
 
 #endif
index 147e7d8df86509fe113971b9ee757441fbc99263..9cde79ba0ba67433468e5078cb0bed26b0c68ce3 100644 (file)
@@ -845,6 +845,11 @@ void process_wake_check(uint32_t current_tick) {
         return;
     }
     
+    /* CPU time accounting: charge one tick to the running process */
+    if (current_process && current_process->state == PROCESS_RUNNING) {
+        current_process->utime++;
+    }
+
     struct process* start = iter;
     do {
         if (iter->state == PROCESS_SLEEPING) {
index 9fe0456b356af36b871ec75c4985c5ae3ce83367..6d4abd8435ff99bc1c6e2fa4c7faea2897e6764b 100644 (file)
@@ -2281,6 +2281,11 @@ void syscall_handler(struct registers* regs) {
         return;
     }
 
+    if (syscall_no == SYSCALL_TIMES) {
+        posix_ext_syscall_dispatch(regs, syscall_no);
+        return;
+    }
+
     if (syscall_no == SYSCALL_ALARM) {
         if (!current_process) { regs->eax = 0; return; }
         uint32_t seconds = regs->ebx;
@@ -2457,6 +2462,23 @@ static void posix_ext_syscall_dispatch(struct registers* regs, uint32_t syscall_
         return;
     }
 
+    if (syscall_no == SYSCALL_TIMES) {
+        if (!current_process) { regs->eax = (uint32_t)-EINVAL; return; }
+        struct { uint32_t tms_utime; uint32_t tms_stime; uint32_t tms_cutime; uint32_t tms_cstime; } tms;
+        tms.tms_utime = current_process->utime;
+        tms.tms_stime = current_process->stime;
+        tms.tms_cutime = 0;
+        tms.tms_cstime = 0;
+        void* user_buf = (void*)regs->ebx;
+        if (user_buf) {
+            if (copy_to_user(user_buf, &tms, sizeof(tms)) < 0) {
+                regs->eax = (uint32_t)-EFAULT; return;
+            }
+        }
+        regs->eax = get_tick_count();
+        return;
+    }
+
     regs->eax = (uint32_t)-ENOSYS;
 }
 
diff --git a/user/ulibc/include/sys/times.h b/user/ulibc/include/sys/times.h
new file mode 100644 (file)
index 0000000..a88d4e6
--- /dev/null
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/*
+ * Copyright (c) 2018, Tulio A M Mendes <[email protected]>
+ * All rights reserved.
+ * See LICENSE for details.
+ *
+ * Source: https://github.com/tadryanom/AdrOS
+ */
+
+#ifndef ULIBC_SYS_TIMES_H
+#define ULIBC_SYS_TIMES_H
+
+#include <stdint.h>
+
+struct tms {
+    uint32_t tms_utime;
+    uint32_t tms_stime;
+    uint32_t tms_cutime;
+    uint32_t tms_cstime;
+};
+
+uint32_t times(struct tms* buf);
+
+#endif
index 4598efc33b3bd035cfdcbe133913f64c7f64d442..e2e9fb718b2a4305aeee9df78b5263cae091476d 100644 (file)
@@ -75,6 +75,7 @@ enum {
     SYS_READV = 81,
     SYS_WRITEV = 82,
     SYS_ALARM = 83,
+    SYS_TIMES = 84,
 };
 
 /* Raw syscall wrappers — up to 5 args via INT 0x80 */
diff --git a/user/ulibc/src/times.c b/user/ulibc/src/times.c
new file mode 100644 (file)
index 0000000..824b304
--- /dev/null
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/*
+ * Copyright (c) 2018, Tulio A M Mendes <[email protected]>
+ * All rights reserved.
+ * See LICENSE for details.
+ *
+ * Source: https://github.com/tadryanom/AdrOS
+ */
+
+#include "sys/times.h"
+#include "syscall.h"
+
+uint32_t times(struct tms* buf) {
+    return (uint32_t)_syscall1(SYS_TIMES, (int)buf);
+}