github.com/cilium/cilium@v1.16.2/bpf/lib/time_cache.h (about)

     1  /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
     2  /* Copyright Authors of Cilium */
     3  
     4  #pragma once
     5  
     6  #include <bpf/ctx/ctx.h>
     7  #include <bpf/api.h>
     8  
     9  #include "time.h"
    10  
    11  /* Per-CPU ktime cache for faster clock access. */
    12  struct {
    13  	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
    14  	__type(key, __u32);
    15  	__type(value, __u64);
    16  	__uint(pinning, LIBBPF_PIN_BY_NAME);
    17  	__uint(max_entries, 1);
    18  } cilium_ktime_cache __section_maps_btf;
    19  
    20  /* Currently supported clock types:
    21   *
    22   * - bpf_ktime_cache_set(ns)      -> CLOCK_MONOTONIC
    23   * - bpf_ktime_cache_set(boot_ns) -> CLOCK_BOOTTIME
    24   */
    25  #define bpf_ktime_cache_set(clock)					     \
    26  	({								     \
    27  		__u32 __z = 0;						     \
    28  		__u64 *__cache = map_lookup_elem(&cilium_ktime_cache, &__z); \
    29  		__u64 __ktime = ktime_get_##clock();			     \
    30  		if (always_succeeds(__cache))				     \
    31  			*__cache = __ktime;				     \
    32  		__ktime;						     \
    33  	})
    34  
    35  #define bpf_ktime_cache_get()						     \
    36  	({								     \
    37  		__u32 __z = 0;						     \
    38  		__u64 *__cache = map_lookup_elem(&cilium_ktime_cache, &__z); \
    39  		__u64 __ktime = 0;					     \
    40  		if (always_succeeds(__cache))				     \
    41  			__ktime = *__cache;				     \
    42  		__ktime;						     \
    43  	})