diff options
author | Maciej Żenczykowski <maze@google.com> | 2022-05-25 12:29:37 -0700 |
---|---|---|
committer | Maciej Żenczykowski <maze@google.com> | 2022-06-21 01:02:57 +0000 |
commit | 76286d5800efd3108c1062a8a2155f7bdb87c497 (patch) | |
tree | a3f13eb2fb6f649c51bd1aa127debde0afa18794 | |
parent | 1cd56433b641c91563c60378273ea9b3aefb3a20 (diff) | |
download | net-76286d5800efd3108c1062a8a2155f7bdb87c497.tar.gz |
cache kernelVersion(), make isAtLeastKernelVersion() cheaper
not perfect due to this being in a header file, so multiple copies
potentially exist, but it's really simple, and works nearly as well.
Bug: 218408035
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Id90c6933d57bc12f4dd640c8918fd0885c7474cf
(cherry picked from commit 9d481287aa69200a36c997200e71232b77234b0a)
Merged-In: Id90c6933d57bc12f4dd640c8918fd0885c7474cf
-rw-r--r-- | common/native/bpf_headers/include/bpf/BpfUtils.h | 7 | ||||
-rw-r--r-- | common/native/tcutils/kernelversion.h | 7 |
2 files changed, 12 insertions, 2 deletions
diff --git a/common/native/bpf_headers/include/bpf/BpfUtils.h b/common/native/bpf_headers/include/bpf/BpfUtils.h index 8f1b9a28..7801c3ef 100644 --- a/common/native/bpf_headers/include/bpf/BpfUtils.h +++ b/common/native/bpf_headers/include/bpf/BpfUtils.h @@ -92,7 +92,7 @@ static inline int setrlimitForTest() { #define KVER(a, b, c) (((a) << 24) + ((b) << 16) + (c)) -static inline unsigned kernelVersion() { +static inline unsigned uncachedKernelVersion() { struct utsname buf; int ret = uname(&buf); if (ret) return 0; @@ -108,6 +108,11 @@ static inline unsigned kernelVersion() { return KVER(kver_major, kver_minor, kver_sub); } +static inline unsigned kernelVersion() { + static unsigned kver = uncachedKernelVersion(); + return kver; +} + static inline bool isAtLeastKernelVersion(unsigned major, unsigned minor, unsigned sub) { return kernelVersion() >= KVER(major, minor, sub); } diff --git a/common/native/tcutils/kernelversion.h b/common/native/tcutils/kernelversion.h index 3be1ad29..492444ad 100644 --- a/common/native/tcutils/kernelversion.h +++ b/common/native/tcutils/kernelversion.h @@ -32,7 +32,7 @@ namespace android { -static inline unsigned kernelVersion() { +static inline unsigned uncachedKernelVersion() { struct utsname buf; int ret = uname(&buf); if (ret) @@ -51,6 +51,11 @@ static inline unsigned kernelVersion() { return KVER(kver_major, kver_minor, kver_sub); } +static unsigned kernelVersion() { + static unsigned kver = uncachedKernelVersion(); + return kver; +} + static inline bool isAtLeastKernelVersion(unsigned major, unsigned minor, unsigned sub) { return kernelVersion() >= KVER(major, minor, sub); |