1From 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--- 11Upstream-Status: Pending 12 13 alloc.c | 15 ++++++++++++++- 14 1 file changed, 14 insertions(+), 1 deletion(-) 15 16diff --git a/alloc.c b/alloc.c 17index bce9464..cf7eb40 100644 18--- a/alloc.c 19+++ b/alloc.c 20@@ -245,6 +245,19 @@ void free_huge_pages(void *ptr) 21 __free_huge_pages(ptr, 1); 22 } 23 24+static size_t get_cacheline_size() { 25+#if defined(__linux__) 26+ FILE * fp = fopen("/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size", "r"); 27+ unsigned int line_size = 0; 28+ if (fp) { 29+ fscanf(fp, "%d", &line_size); 30+ fclose(fp); 31+ } 32+ return line_size; 33+#else 34+ return sysconf(_SC_LEVEL2_CACHE_LINESIZE); 35+#endif 36+} 37 /* 38 * Offset the buffer using bytes wasted due to alignment to avoid using the 39 * same cache lines for the start of every buffer returned by 40@@ -261,7 +274,7 @@ void *cachecolor(void *buf, size_t len, size_t color_bytes) 41 42 /* Lookup our cacheline size once */ 43 if (cacheline_size == 0) { 44- cacheline_size = sysconf(_SC_LEVEL2_CACHE_LINESIZE); 45+ cacheline_size = get_cacheline_size(); 46 linemod = time(NULL); 47 } 48 49