Skip to content

KFunc scx_bpf_now

v6.14

This function returns a high-performance monotonically non-decreasing clock for the current CPU. The clock returned is in nanoseconds.

Definition

It provides the following properties:

1) High performance: Many BPF schedulers call bpf_ktime_get_ns frequently to account for execution time and track tasks' runtime properties. Unfortunately, in some hardware platforms, bpf_ktime_get_ns -- which eventually reads a hardware timestamp counter -- is neither performant nor scalable. scx_bpf_now aims to provide a high-performance clock by using the rq clock in the scheduler core whenever possible.

2) High enough resolution for the BPF scheduler use cases: In most BPF scheduler use cases, the required clock resolution is lower than the most accurate hardware clock (e.g., rdtsc in x86). scx_bpf_now basically uses the rq clock in the scheduler core whenever it is valid. It considers that the rq clock is valid from the time the rq clock is updated (update_rq_clock) until the rq is unlocked (rq_unpin_lock).

3) Monotonically non-decreasing clock for the same CPU: scx_bpf_now guarantees the clock never goes backward when comparing them in the same CPU. On the other hand, when comparing clocks in different CPUs, there is no such guarantee -- the clock can go backward. It provides a monotonically non-decreasing clock so that it would provide the same clock values in two different scx_bpf_now calls in the same CPU during the same period of when the rq clock is valid.

Returns

A high-performance monotonically non-decreasing clock for the current CPU. The clock returned is in nanoseconds.

Signature

u64 scx_bpf_now()

Usage

Docs could be improved

This part of the docs is incomplete, contributions are very welcome

Program types

The following program types can make use of this kfunc:

Example

Docs could be improved

This part of the docs is incomplete, contributions are very welcome