Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 1 | From 9ff04d7acc700387e3837f8ab11a41efea5ee8b0 Mon Sep 17 00:00:00 2001 |
| 2 | From: Khem Raj <raj.khem@gmail.com> |
| 3 | Date: Thu, 21 Jun 2018 19:44:26 -0700 |
| 4 | Subject: [PATCH] alloc.c: Avoid sysconf(_SC_LEVEL2_CACHE_LINESIZE) on linux |
| 5 | |
| 6 | musl does not have it |
| 7 | |
| 8 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| 9 | |
| 10 | --- |
| 11 | alloc.c | 15 ++++++++++++++- |
| 12 | 1 file changed, 14 insertions(+), 1 deletion(-) |
| 13 | |
| 14 | diff --git a/alloc.c b/alloc.c |
| 15 | index 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 | |