xref: /haiku/src/tests/libs/gnu/sched_affinity_test.cpp (revision c5a740cdb23a6dc9973a8142303b67f830c67b54)
1*c5a740cdSJérôme Duval /*
2*c5a740cdSJérôme Duval  * Copyright 2023, Jérôme Duval, jerome.duval@gmail.com. All rights reserved.
3*c5a740cdSJérôme Duval  * Distributed under the terms of the MIT License.
4*c5a740cdSJérôme Duval  */
5*c5a740cdSJérôme Duval 
6*c5a740cdSJérôme Duval 
7*c5a740cdSJérôme Duval #include <pthread.h>
8*c5a740cdSJérôme Duval #include <sched.h>
9*c5a740cdSJérôme Duval #include <stdio.h>
10*c5a740cdSJérôme Duval #include <stdlib.h>
11*c5a740cdSJérôme Duval 
12*c5a740cdSJérôme Duval 
13*c5a740cdSJérôme Duval int
main(int argc,char ** argv)14*c5a740cdSJérôme Duval main(int argc, char** argv)
15*c5a740cdSJérôme Duval {
16*c5a740cdSJérôme Duval 	if (argc != 2)
17*c5a740cdSJérôme Duval 		exit(1);
18*c5a740cdSJérôme Duval 	if (strcmp(argv[1], "off") != 0) {
19*c5a740cdSJérôme Duval 		printf("with affinity on CPU 1\n");
20*c5a740cdSJérôme Duval 		pthread_t thread = pthread_self();
21*c5a740cdSJérôme Duval 		cpuset_t cpuset;
22*c5a740cdSJérôme Duval 		CPUSET_ZERO(&cpuset);
23*c5a740cdSJérôme Duval 		CPUSET_SET(1, &cpuset);
24*c5a740cdSJérôme Duval 		if (pthread_setaffinity_np(thread, sizeof(cpuset_t), &cpuset) != 0) {
25*c5a740cdSJérôme Duval 			fprintf(stderr, "pthread_setaffinity_np failed\n");
26*c5a740cdSJérôme Duval 			exit(1);
27*c5a740cdSJérôme Duval 		}
28*c5a740cdSJérôme Duval 		if (pthread_getaffinity_np(thread, sizeof(cpuset_t), &cpuset) != 0) {
29*c5a740cdSJérôme Duval 			fprintf(stderr, "pthread_getaffinity_np failed\n");
30*c5a740cdSJérôme Duval 			exit(1);
31*c5a740cdSJérôme Duval 		}
32*c5a740cdSJérôme Duval 		if (!CPUSET_ISSET(1, &cpuset))
33*c5a740cdSJérôme Duval 			fprintf(stderr, "affinity not on CPU 1\n");
34*c5a740cdSJérôme Duval 		if (sched_getcpu() != 1)
35*c5a740cdSJérôme Duval 			fprintf(stderr, "not running on CPU 1\n");
36*c5a740cdSJérôme Duval 	} else {
37*c5a740cdSJérôme Duval 		printf("without affinity\n");
38*c5a740cdSJérôme Duval 	}
39*c5a740cdSJérôme Duval 	int* p;
40*c5a740cdSJérôme Duval 	while (true)
41*c5a740cdSJérôme Duval 		(*p)++;
42*c5a740cdSJérôme Duval 	return *p;
43*c5a740cdSJérôme Duval }
44