Go to main content

man pages section 3: Basic Library Functions

Exit Print View

Updated: Thursday, June 13, 2019
 
 

get_nsec_fromepoch (3C)

Name

get_hrusec, get_sec_fromepoch, get_nsec_fromepoch - timestamp for interval measurements

Synopsis

#include <sys/types.h>
#include <sys/system_stats.h>

void get_hrusec(hrtime_t *usec);

void get_sec_fromepoch(memtime_sec_t *sec);

void get_nsec_fromepoch(hrtime_t *nsec);

Description

These functions are provided for performance reasons. The overhead of reading time using these functions is minimum as they return time read from memory. No system calls are involved in get_* functions.

The get_hrusec() function returns high-resolution real time which is expressed as microseconds 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). This high-resolution time is suited for performance measurement. Although the best time accuracy it gives is 1 millisecond unlike gethrtime(3C).

The get_sec_fromepoch() and get_nsec_fromepoch() functions return time expressed in elapsed seconds from epoch(January 1, 1970) and elapsed nanoseconds from epoch respectively. Time returned by both these functions are subject to setting or drifting by adjtime(2) or settimeofday(3C) calls. Note that the accuracy of nsecs returned by get_nsec_fromepoch() is 1 millisecond.

The in memory time that is read by these functions, is updated by an interrupt routine and so there will be slight variation compared to the time reported by the gettimeofday() function. Therefore the time returned by the get_sec_fromepoch() and get_nsec_fromepoch() functions should not be compared against time returned by the gettimeofday() function. For more information, see the gettimeofday(3C), get_sec_fromepoch(3C) and get_nsec_fromepoch(3C) man pages.

Examples

Example 1 Timestamping using get_hrusec(), get_sec_fromepoch(), and get_nsec_fromepoch():
#include <stdio.h>
#include <unistd.h>
#include <sys/system_stats.h>
#include <time.h>

void main() {
        hrtime_t hres1, hres2;
        memtime_sec_t   sec;

        get_hrusec(&hres1);
        sleep(1);
        get_hrusec(&hres2);

        printf("Slept for %lld microseconds\n",
            (hres2 - hres1));

        get_sec_fromepoch(&sec);
        printf("Current time in seconds : %lld\n",
            sec);

        get_nsec_fromepoch(&hres1);
        printf("Current time in nanoseconds: %lld\n",
            hres1);
}

Attributes

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

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Architecture
SPARC and x86
Interface Stability
Committed
MT-Level
MT-Safe

See Also

gettimeofday(3C), gethrtime(3C)