xref: /titanic_51/usr/src/man/man3c/gethrtime.3c (revision a65cd518c5d0f30c53594a7022eb0f7d04c98cef)
te
Copyright (c) 2004, Sun Microsystems, Inc. All Rights Reserved.
The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
GETHRTIME 3C "Sep 7, 2004"
NAME
gethrtime, gethrvtime - get high resolution time
SYNOPSIS

#include <sys/time.h>

hrtime_t gethrtime(void);

hrtime_t gethrvtime(void);
DESCRIPTION

The gethrtime() function returns the current high-resolution real time. Time is expressed as nanoseconds since some arbitrary time in the past; it is not correlated in any way to the time of day, and thus is not subject to resetting or drifting by way of adjtime(2) or settimeofday(3C). The hi-res timer is ideally suited to performance measurement tasks, where cheap, accurate interval timing is required.

The gethrvtime() function returns the current high-resolution LWP virtual time, expressed as total nanoseconds of execution time.

The gethrtime() and gethrvtime() functions both return an hrtime_t, which is a 64-bit (long long) signed integer.

EXAMPLES

The following code fragment measures the average cost of getpid(2):

hrtime_t start, end;
int i, iters = 100;

start = gethrtime();
for (i = 0; i < iters; i++)
 getpid();
end = gethrtime();

printf("Avg getpid() time = %lld nsec\en", (end - start) / iters);
ATTRIBUTES

See attributes(5) for descriptions of the following attributes:

ATTRIBUTE TYPE ATTRIBUTE VALUE
MT-Level MT-Safe
SEE ALSO

proc(1), adjtime(2), gettimeofday(3C), settimeofday(3C), attributes(5)

NOTES

Although the units of hi-res time are always the same (nanoseconds), the actual resolution is hardware dependent. Hi-res time is guaranteed to be monotonic (it won't go backward, it won't periodically wrap) and linear (it won't occasionally speed up or slow down for adjustment, like the time of day can), but not necessarily unique: two sufficiently proximate calls may return the same value.