PebbleOS v4.48.0-75-g7416f78a1
Loading...
Searching...
No Matches
Uptime, wall clock and cycles

Monotonic time since the scheduler started, the wall clock, and the CPU cycle counter. More...

Functions

uint64_t pbl_uptime_ticks (void)
 Get the time since the scheduler started, in ticks.
 
uint64_t pbl_uptime_ms (void)
 Get the time since the scheduler started, in milliseconds.
 
uint64_t pbl_uptime_us (void)
 Get the time since the scheduler started, in microseconds.
 
time_t pbl_realtime (void)
 Get the wall clock time.
 
uint64_t pbl_realtime_ms (void)
 Get the wall clock time, in milliseconds.
 
void pbl_realtime_set (time_t t)
 Set the wall clock time.
 
static uint32_t pbl_cycle_get_32 (void)
 Read the CPU cycle counter.
 
uint32_t pbl_cycles_per_sec (void)
 Get the rate of the CPU cycle counter.
 
void pbl_cycles_per_sec_set (uint32_t hz)
 Set the rate of the CPU cycle counter.
 

Detailed Description

Monotonic time since the scheduler started, the wall clock, and the CPU cycle counter.

Uptime counts kernel ticks, including those spent in tickless sleep, in 64 bits so it never wraps. The wall clock is UTC time, kept as an offset from the uptime: it follows the same clock source and jumps only when set. Persisting it across resets is up to the caller, through an RTC device. The cycle counter is for short, precise measurements: it is 32 bits wide, its rate follows the CPU clock and it may stop while the CPU sleeps.

uint64_t start = pbl_uptime_ms();
do_work();
PBL_LOG_DBG("took %" PRIu32 " ms", (uint32_t)(pbl_uptime_ms() - start));
uint32_t c0 = pbl_cycle_get_32();
do_short_work();
uint32_t cycles = pbl_cycle_get_32() - c0;
static uint32_t pbl_cycle_get_32(void)
Read the CPU cycle counter.
Definition time.h:90
uint64_t pbl_uptime_ms(void)
Get the time since the scheduler started, in milliseconds.
#define PBL_LOG_DBG(fmt,...)
Log a routine debug message, possibly queued.
Definition logging.h:562

Function Documentation

◆ pbl_cycle_get_32()

static uint32_t pbl_cycle_get_32 ( void  )
static

Read the CPU cycle counter.

Privileged only. Wraps around; compare differences, not absolute values.

Returns
Current cycle count.

◆ pbl_cycles_per_sec()

uint32_t pbl_cycles_per_sec ( void  )

Get the rate of the CPU cycle counter.

Returns
Cycles per second at the current CPU clock.

◆ pbl_cycles_per_sec_set()

void pbl_cycles_per_sec_set ( uint32_t  hz)

Set the rate of the CPU cycle counter.

For the code that knows the CPU clock, the SoC and its clock driver: at boot, before anything waits, then whenever the CPU clock changes.

Parameters
hzCycles per second.

◆ pbl_realtime()

time_t pbl_realtime ( void  )

Get the wall clock time.

Returns
UTC time in seconds since the epoch.

◆ pbl_realtime_ms()

uint64_t pbl_realtime_ms ( void  )

Get the wall clock time, in milliseconds.

Returns
UTC time in milliseconds since the epoch.

◆ pbl_realtime_set()

void pbl_realtime_set ( time_t  t)

Set the wall clock time.

Callable before the scheduler starts.

Parameters
tUTC time in seconds since the epoch.

◆ pbl_uptime_ms()

uint64_t pbl_uptime_ms ( void  )

Get the time since the scheduler started, in milliseconds.

Returns
Milliseconds elapsed, with tick resolution.

◆ pbl_uptime_ticks()

uint64_t pbl_uptime_ticks ( void  )

Get the time since the scheduler started, in ticks.

Returns
Ticks elapsed, at PBL_TICK_HZ.

◆ pbl_uptime_us()

uint64_t pbl_uptime_us ( void  )

Get the time since the scheduler started, in microseconds.

Callable from ISRs.

Returns
Microseconds elapsed, with the resolution of the clock source.