minix/minix/lib/libsys/clock_time.c
David van Moolenbroek d91f738bd8 Kernel: export clock information on kernel page
Please note that this information is for use by system services only!
The clock facility is not ready to be used directly by userland, and
thus, this kernel page extension is NOT part of the userland ABI.

For service programmers' convenience, change the prototype of the
getticks(3) to return the uptime clock value directly, since the call
can no longer fail.

Correct the sys_times(2) reply message to use the right field type
for the boot time.

Restructure the kernel internals a bit so as to have all the clock
stuff closer together.

Change-Id: Ifc050b7bd253aecbe46e3bd7d7cc75bd86e45555
2015-09-23 12:00:46 +00:00

45 lines
1.1 KiB
C

#include "sysutil.h"
#include <sys/time.h>
/*
* This routine returns the time in seconds since 1.1.1970. MINIX is an
* astrophysically naive system that assumes the earth rotates at a constant
* rate and that such things as leap seconds do not exist. If a non-NULL
* pointer to a timespec structure is given, that structure is filled with
* the current time in subsecond precision.
*/
time_t
clock_time(struct timespec *tv)
{
struct minix_kerninfo *minix_kerninfo;
uint32_t system_hz;
clock_t realtime;
time_t boottime, sec;
minix_kerninfo = get_minix_kerninfo();
/* We assume atomic 32-bit field retrieval. TODO: 64-bit support. */
boottime = minix_kerninfo->kclockinfo->boottime;
realtime = minix_kerninfo->kclockinfo->realtime;
system_hz = minix_kerninfo->kclockinfo->hz;
sec = boottime + realtime / system_hz;
if (tv != NULL) {
tv->tv_sec = sec;
/*
* We do not want to overflow, and system_hz can be as high as
* 50kHz.
*/
if (system_hz < LONG_MAX / 40000)
tv->tv_nsec = (realtime % system_hz) * 40000 /
system_hz * 25000;
else
tv->tv_nsec = 0; /* bad, but what's better? */
}
return sec;
}