From 4f69eb130dc2c67a90f2460cedb78f6db11dd6d2 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

Upstream-Status: Pending

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 b8ed663..a58c43d 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);
 	}
 
-- 
2.25.1

