Commit 2ed93414 authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼
Browse files

kernel: inprove portability

parent 36903b4f
......@@ -39,8 +39,23 @@ int kernel_get_cacheline_size()
{
#if defined(__linux__) && defined(__x86_64__)
return sysconf(_SC_LEVEL1_ICACHE_LINESIZE);
#else
#elif defined(__MACH__)
/* Open the command for reading. */
FILE *fp = popen("sysctl -n machdep.cpu.cache.linesize", "r");
if (fp == NULL)
return -1;
int ret, size;
ret = fscanf(fp, "%d", &size);
pclose(fp);
return ret == 1 ? size : -1;
#elif defined(__x86_64__) || defined(__i386__)
return 64; /** @todo fixme */
#else
#error "Unsupported architecture"
#endif
}
......@@ -49,8 +64,47 @@ int kernel_get_page_size()
{
return sysconf(_SC_PAGESIZE);
}
#else
#error "Unsupported platform"
#endif
/* There is no sysconf interface to get the hugepage size */
int kernel_get_hugepage_size()
{
#ifdef __linux__
char *key, *value, *unit, *line = NULL;
int sz = -1;
size_t len = 0;
FILE *f;
f = fopen(PROCFS_PATH "/meminfo", "r");
if (!f)
return -1;
while (getline(&line, &len, f) != -1) {
key = strtok(line, ": ");
value = strtok(NULL, " ");
unit = strtok(NULL, "\n");
if (!strcmp(key, "Hugepagesize") && !strcmp(unit, "kB")) {
sz = strtoul(value, NULL, 10) * 1024;
break;
}
}
free(line);
fclose(f);
return sz;
#elif defined(__x86_64__)
return 1 << 21;
#elif defined(__i386__)
return 1 << 22;
#else
#error "Unsupported architecture"
#endif
}
#ifdef __linux__
int kernel_module_set_param(const char *module, const char *param, const char *value)
......@@ -158,35 +212,6 @@ out:
return -1; /* not found or error */
}
/* There is no sysconf interface to get the hugepage size */
int kernel_get_hugepage_size()
{
char *key, *value, *unit, *line = NULL;
int sz = -1;
size_t len = 0;
FILE *f;
f = fopen(PROCFS_PATH "/meminfo", "r");
if (!f)
return -1;
while (getline(&line, &len, f) != -1) {
key = strtok(line, ": ");
value = strtok(NULL, " ");
unit = strtok(NULL, "\n");
if (!strcmp(key, "Hugepagesize") && !strcmp(unit, "kB")) {
sz = strtoul(value, NULL, 10) * 1024;
break;
}
}
free(line);
fclose(f);
return sz;
}
int kernel_get_nr_hugepages()
{
FILE *f;
......
......@@ -27,8 +27,6 @@
TestSuite(kernel, .description = "Kernel features");
#ifdef __linux__
#if defined(__x86_64__) || defined(__i386__)
#define PAGESIZE (1 << 12)
#define CACHELINESIZE 64
......@@ -57,6 +55,7 @@ Test(kernel, sizes)
cr_assert_eq(sz, CACHELINESIZE);
}
#ifdef __linux__
Test(kernel, hugepages)
{
int ret;
......@@ -108,5 +107,4 @@ Test(kernel, frequency)
/* Check for plausability only */
cr_assert(freq > 1e9 && freq < 5e9);
}
#endif /* __linux__ */
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment