github.com/cilium/cilium@v1.16.2/bpf/lib/utime.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 "common.h"
     7  #include "maps.h"
     8  
     9  /*
    10   * Number of bits to shift a monotonic 64-bit nanosecond clock for utime unit.
    11   * Dividing nanoseconds by 2^9 yields roughly half microsecond accuracy without
    12   * any loss when converting from seconds (as 1e9/2^9 is an integer), but avoids
    13   * expensive 64-bit divisions.  With this shift the range of an u64 is ~300000
    14   * years instead of ~600 years if left at nanoseconds.
    15   */
    16  #define UTIME_SHIFT 9
    17  
    18  static __always_inline __u64
    19  _utime_get_offset()
    20  {
    21  	__u32 index = RUNTIME_CONFIG_UTIME_OFFSET;
    22  	__u64 *offset;
    23  
    24  	offset = map_lookup_elem(&CONFIG_MAP, &index);
    25  	if (likely(offset))
    26  		return *offset;
    27  
    28  	return 0;
    29  }
    30  
    31  /**
    32   * Return the current time in "utime" unit (512 ns per unit) that is directly
    33   * comparable to expirations times in bpf maps.
    34   */
    35  static __always_inline __u64
    36  utime_get_time()
    37  {
    38  	return (ktime_get_ns() >> UTIME_SHIFT) + _utime_get_offset();
    39  }