Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Bug 228669] CLOCK_PROCESS_CPUTIME_ID is unnecessarily inaccurate

1 view
Skip to first unread message

bugzilla...@freebsd.org

unread,
Jun 2, 2018, 2:22:58 AM6/2/18
to
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=228669

Bug ID: 228669
Summary: CLOCK_PROCESS_CPUTIME_ID is unnecessarily inaccurate
Product: Base System
Version: CURRENT
Hardware: Any
OS: Any
Status: New
Severity: Affects Only Me
Priority: ---
Component: kern
Assignee: bu...@FreeBSD.org
Reporter: cper...@FreeBSD.org

clock_gettime(CLOCK_PROCESS_CPUTIME_ID) is far less accurate than
clock_gettime(CLOCK_PROF), even though it seems that they should theoretically
measure the same thing:

CLOCK_PROF
Increments when the CPU is running in user or kernel mode.
CLOCK_PROCESS_CPUTIME_ID
Returns the execution time of the calling process.

It looks like this is due to CLOCK_PROF (aka. calcru) adding time from the
current time slice, while CLOCK_PROCESS_CPUTIME_ID (aka. get_process_cputime
aka rufetch) does not.

Since CLOCK_PROCESS_CPUTIME_ID is the version mandated by POSIX, it would be
nice if it were at least as accurate as the historical BSD profiling clock ID.

--
You are receiving this mail because:
You are the assignee for the bug.
_______________________________________________
freebs...@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-bugs
To unsubscribe, send any mail to "freebsd-bugs...@freebsd.org"

bugzilla...@freebsd.org

unread,
Jun 22, 2018, 5:34:37 AM6/22/18
to
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=228669

--- Comment #1 from Colin Percival <cper...@FreeBSD.org> ---
Created attachment 194476
--> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=194476&action=edit
Proposed fix

This patch adds the used portion of the current thread's time slice, if the
current thread belongs to the process for which the cputime clock is being
measured.

bugzilla...@freebsd.org

unread,
Jun 22, 2018, 6:01:34 AM6/22/18
to
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=228669

--- Comment #2 from Colin Percival <cper...@FreeBSD.org> ---
Created attachment 194477
--> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=194477&action=edit
Revised based on feedback from kib

bugzilla...@freebsd.org

unread,
Jun 22, 2018, 6:03:37 AM6/22/18
to
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=228669

--- Comment #3 from Colin Percival <cper...@FreeBSD.org> ---
That was weird, let's try uploading the revised patch again...

bugzilla...@freebsd.org

unread,
Jun 22, 2018, 6:03:41 AM6/22/18
to
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=228669

--- Comment #3 from Colin Percival <cper...@FreeBSD.org> ---
That was weird, let's try uploading the revised patch again...

--- Comment #4 from Colin Percival <cper...@FreeBSD.org> ---
That was weird, let's try uploading the revised patch again...

bugzilla...@freebsd.org

unread,
Jun 22, 2018, 6:04:51 AM6/22/18
to
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=228669

Colin Percival <cper...@FreeBSD.org> changed:

What |Removed |Added
----------------------------------------------------------------------------
Attachment #194476|0 |1
is obsolete| |
Attachment #194477|0 |1
is obsolete| |

--- Comment #5 from Colin Percival <cper...@FreeBSD.org> ---
Created attachment 194478
--> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=194478&action=edit
Revised based on feedback from kib (2nd try to attach this...)

bugzilla...@freebsd.org

unread,
Jun 22, 2018, 6:24:52 AM6/22/18
to
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=228669

--- Comment #6 from commi...@freebsd.org ---
A commit references this bug:

Author: cperciva
Date: Fri Jun 22 10:23:32 UTC 2018
New revision: 335553
URL: https://svnweb.freebsd.org/changeset/base/335553

Log:
Improve the accuracy of the POSIX "process CPU-time" clocks by adding the
used portion of the current thread's time slice if the current thread
belongs to the process being queried (i.e., if clock_gettime is invoked
with a clock ID of CLOCK_PROCESS_CPUTIME_ID or the value provided by
passing getpid(2) to clock_getcpuclockid(3)).

The CLOCK_VIRTUAL and CLOCK_PROF timers already make this adjustment via
long-standing code in calcru(), but since those timers are not specified
by POSIX it seems useful to add it here so that the higher accuracy is
available to code which aims to be portable.

PR: 228669
Reported by: Graham Percival
Reviewed by: kib
MFC after: 1 week

Changes:
head/sys/kern/kern_time.c
0 new messages