bug-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[RFC PATCH v2 1/1 Mach] Add a mach host operation which returns elapsed


From: Zhaoming Luo
Subject: [RFC PATCH v2 1/1 Mach] Add a mach host operation which returns elapsed time since bootup
Date: Thu, 19 Dec 2024 10:01:26 +0800

Signed-off-by: Zhaoming Luo <zhmingluo@163.com>
---
 include/mach/mach_host.defs |  7 +++++++
 kern/mach_clock.c           | 26 ++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/include/mach/mach_host.defs b/include/mach/mach_host.defs
index 8fd9d6b3..7ea99bbe 100644
--- a/include/mach/mach_host.defs
+++ b/include/mach/mach_host.defs
@@ -386,3 +386,10 @@ routine    host_adjust_time64(
 routine        host_get_kernel_version(
                host            : host_t;
        out     kernel_version  : new_kernel_version_t);
+
+/*
+ *     Get the uptime on this host.
+ */
+routine        host_get_uptime(
+               host            : host_t;
+       out     up_time         : time_value_t);
diff --git a/kern/mach_clock.c b/kern/mach_clock.c
index 4b953650..850e25cc 100644
--- a/kern/mach_clock.c
+++ b/kern/mach_clock.c
@@ -70,6 +70,7 @@ int           hz = HZ;                /* number of ticks per 
second */
 int            tick = (MICROSECONDS_IN_ONE_SECOND / HZ);       /* number of 
usec per tick */
 time_value64_t time = { 0, 0 };        /* time since bootup (uncorrected) */
 unsigned long  elapsed_ticks = 0;      /* ticks elapsed since bootup */
+unsigned long  check_elapsed_ticks = 0;
 
 int            timedelta = 0;
 int            tickdelta = 0;
@@ -219,6 +220,7 @@ void clock_interrupt(
            s = simple_lock_irq(&timer_lock);
 
            elapsed_ticks++;
+           check_elapsed_ticks++;
 
            telt = (timer_elt_t)queue_first(&timer_head.chain);
            if (telt->ticks <= elapsed_ticks)
@@ -379,6 +381,7 @@ void init_timeout(void)
        timer_head.ticks = ~0;  /* MAXUINT - sentinel */
 
        elapsed_ticks = 0;
+       check_elapsed_ticks = 0;
 }
 
 /*
@@ -571,6 +574,29 @@ host_adjust_time64(
        return (KERN_SUCCESS);
 }
 
+/*
+ * Read the uptime (the elapsed time since boot up).
+ */
+kern_return_t
+host_get_uptime(const host_t host, time_value_t *uptime)
+{
+       unsigned long read_elapsed_ticks;
+       int64_t elapsed_usec;
+
+       if (host == HOST_NULL)
+               return (KERN_INVALID_HOST);
+
+       do {
+           read_elapsed_ticks = elapsed_ticks;
+       } while (read_elapsed_ticks != check_elapsed_ticks);
+
+       elapsed_usec = read_elapsed_ticks * tick;
+       uptime->seconds = elapsed_usec / MICROSECONDS_IN_ONE_SECOND;
+       uptime->microseconds = elapsed_usec % MICROSECONDS_IN_ONE_SECOND;
+
+       return (KERN_SUCCESS);
+}
+
 void mapable_time_init(void)
 {
        if (kmem_alloc_wired(kernel_map, (vm_offset_t *) &mtime, PAGE_SIZE)
-- 
2.45.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]