mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
pure getcpu() example
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
53
userland/linux/getcpu.c
Normal file
53
userland/linux/getcpu.c
Normal file
@@ -0,0 +1,53 @@
|
||||
/* https://cirosantilli.com/linux-kernel-module-cheat#getcpu */
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <assert.h>
|
||||
#include <sched.h> /* getcpu */
|
||||
#include <pthread.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* 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 <linux/getcpu.h>
|
||||
#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;
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <assert.h>
|
||||
#include <sched.h>
|
||||
#include <sched.h> /* sched_getaffinity */
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <pthread.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <sched.h> /* sched_getcpu */
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
Reference in New Issue
Block a user