From f17e68a10933d3f1f7a3de7f977f639bcde7456c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ciro=20Santilli=20=E5=85=AD=E5=9B=9B=E4=BA=8B=E4=BB=B6=20?= =?UTF-8?q?=E6=B3=95=E8=BD=AE=E5=8A=9F?= Date: Wed, 13 May 2020 01:00:00 +0000 Subject: [PATCH] pure getcpu() example --- README.adoc | 1 + path_properties.py | 4 +++ userland/linux/getcpu.c | 53 ++++++++++++++++++++++++++++++ userland/linux/sched_getaffinity.c | 2 +- userland/linux/sched_getcpu.c | 1 + 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 userland/linux/getcpu.c diff --git a/README.adoc b/README.adoc index e1184d0..da879bd 100644 --- a/README.adoc +++ b/README.adoc @@ -19006,6 +19006,7 @@ The example in `man futex` is also a must. Examples: * link:userland/linux/sched_getcpu.c[] +* link:userland/linux/getcpu.c[]: a wrapper close the the syscall that also returns the current NUMA node * link:userland/linux/sched_getcpu_barrier.c[]: this uses a barrier to ensure that gem5 will run each thread on one separate CPU Returns the CPU that the process/thread is currently running on: diff --git a/path_properties.py b/path_properties.py index 3af79bd..7316296 100644 --- a/path_properties.py +++ b/path_properties.py @@ -730,6 +730,10 @@ path_properties_tuples = ( 'more_than_1s': True, 'test_run_args': {'cpus': 2}, }, + 'getcpu.c': { + 'requires_syscall_getcpu': True, + 'test_run_args': {'cpus': 2}, + }, 'init_env_poweroff.c': {'requires_sudo': True}, 'mmap_anonymous_touch.c': { # https://github.com/cirosantilli/linux-kernel-module-cheat/issues/103 diff --git a/userland/linux/getcpu.c b/userland/linux/getcpu.c new file mode 100644 index 0000000..3decd13 --- /dev/null +++ b/userland/linux/getcpu.c @@ -0,0 +1,53 @@ +/* https://cirosantilli.com/linux-kernel-module-cheat#getcpu */ + +#define _GNU_SOURCE +#include +#include /* getcpu */ +#include +#include +#include +#include +#include +#include +#include + +/* man getcpu says this exists since glibc 2.29, but I can't find it in Ubuntu 20.04 glibc 2.31 not even with locate + * (there is a hit under /usr/src/linux-headers-5.4.0-29/include/linux/getcpu.h but it only defines `struct getcpu_cache` + * and nothing else: + * https://stackoverflow.com/questions/23224607/how-do-i-include-linux-header-files-like-linux-getcpu-h + * Furthermore, there is already a prototype in sched.h, so we can't define our own either. */ +#if 0 +#include +#endif + +void* main_thread(void *arg) { + (void)arg; + unsigned cpu, numa; + getcpu(&cpu, &numa); + printf("%u %u\n", cpu, numa); + return NULL; +} + +int main(int argc, char **argv) { + pthread_t *threads; + unsigned int nthreads, i; + if (argc > 1) { + nthreads = strtoll(argv[1], NULL, 0); + } else { + nthreads = 1; + } + threads = malloc(nthreads * sizeof(*threads)); + for (i = 0; i < nthreads; ++i) { + assert(pthread_create( + &threads[i], + NULL, + main_thread, + NULL + ) == 0); + } + for (i = 0; i < nthreads; ++i) { + pthread_join(threads[i], NULL); + } + free(threads); + return EXIT_SUCCESS; +} diff --git a/userland/linux/sched_getaffinity.c b/userland/linux/sched_getaffinity.c index 2b6cf00..ec23a7d 100644 --- a/userland/linux/sched_getaffinity.c +++ b/userland/linux/sched_getaffinity.c @@ -2,7 +2,7 @@ #define _GNU_SOURCE #include -#include +#include /* sched_getaffinity */ #include #include #include diff --git a/userland/linux/sched_getcpu.c b/userland/linux/sched_getcpu.c index 0d5cd1b..9cc3cd1 100644 --- a/userland/linux/sched_getcpu.c +++ b/userland/linux/sched_getcpu.c @@ -5,6 +5,7 @@ #include #include #include +#include /* sched_getcpu */ #include #include #include