hc
2023-03-13 2ec15ae1cb4be1b4fcb56c6d621123d7ebdaad6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
From 9ff04d7acc700387e3837f8ab11a41efea5ee8b0 Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem@gmail.com>
Date: Thu, 21 Jun 2018 19:44:26 -0700
Subject: [PATCH] alloc.c: Avoid sysconf(_SC_LEVEL2_CACHE_LINESIZE) on linux
 
musl does not have it
 
Signed-off-by: Khem Raj <raj.khem@gmail.com>
 
---
 alloc.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
 
diff --git a/alloc.c b/alloc.c
index bce9464..cf7eb40 100644
--- a/alloc.c
+++ b/alloc.c
@@ -245,6 +245,19 @@ void free_huge_pages(void *ptr)
     __free_huge_pages(ptr, 1);
 }
 
+static size_t get_cacheline_size() {
+#if defined(__linux__)
+    FILE * fp = fopen("/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size", "r");
+    unsigned int line_size = 0;
+    if (fp) {
+        fscanf(fp, "%d", &line_size);
+        fclose(fp);
+    }
+    return line_size;
+#else
+    return sysconf(_SC_LEVEL2_CACHE_LINESIZE);
+#endif
+}
 /*
  * Offset the buffer using bytes wasted due to alignment to avoid using the
  * same cache lines for the start of every buffer returned by
@@ -261,7 +274,7 @@ void *cachecolor(void *buf, size_t len, size_t color_bytes)
 
     /* Lookup our cacheline size once */
     if (cacheline_size == 0) {
-        cacheline_size = sysconf(_SC_LEVEL2_CACHE_LINESIZE);
+        cacheline_size = get_cacheline_size();
         linemod = time(NULL);
     }