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