resolution of CLOCK_PROCESS_CPUTIME_ID

Jan Stary <[email protected]> Wed, 24 Jun 2026 15:08:24 +0200
Newsgroups gmane.os.openbsd.misc
Message-ID <[email protected]>
Dear all,

the resolution of CLOCK_PROCESS_CPUTIME_ID,
as reported by clock_getres(), is 10000000 ns;
this seems to be the case on all archs I have.

This resolution seems to be coarser than
the actual cpu time reported (in a process that does nothing else),
such as 7233653 ns on this machine here.

FWIW, each of the OSes below (+Debian) returns 1 ns.

	Jan


#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <err.h>

void
show(clockid_t id, char* name)
{
	struct timespec ts;
	struct timespec cr;
	if (clock_gettime(id, &ts) == -1)
		err(1, NULL);
	if (clock_getres(id, &cr) == -1)
		err(1, NULL);
	printf("%s %10lld.%09ld s, %8ld ns resolution\n",
		name, ts.tv_sec, ts.tv_nsec, cr.tv_nsec);
}

int
main(void)
{
#ifdef  __APPLE__
	show(CLOCK_REALTIME,		"real time");
	show(CLOCK_MONOTONIC,		"monotonic");
	show(CLOCK_UPTIME_RAW,		"up time  ");
	show(CLOCK_PROCESS_CPUTIME_ID,	"proc time");
#elif __OpenBSD__
	show(CLOCK_REALTIME,		"real time");
	show(CLOCK_MONOTONIC,		"monotonic");
	show(CLOCK_BOOTTIME,		"boot time");
	show(CLOCK_UPTIME,		"up time  ");
	show(CLOCK_PROCESS_CPUTIME_ID,	"proc time");
#elif __FreeBSD__
	show(CLOCK_REALTIME,		"real time");
	show(CLOCK_MONOTONIC,		"monotonic");
	show(CLOCK_BOOTTIME,		"boot time");
	show(CLOCK_UPTIME,		"up time  ");
	show(CLOCK_PROCESS_CPUTIME_ID,  "proc time");
#elif __NetBSD__
	show(CLOCK_REALTIME,		"real time");
	show(CLOCK_MONOTONIC,		"monotonic");
	show(CLOCK_PROCESS_CPUTIME_ID,  "proc time");
#else
	show(CLOCK_REALTIME,		"real time");
	show(CLOCK_MONOTONIC,		"monotonic");
	show(CLOCK_BOOTTIME,		"boot time");
	show(CLOCK_PROCESS_CPUTIME_ID,	"proc time");
#endif
	return 0;
}