xref: /OK3568_Linux_fs/kernel/tools/testing/selftests/powerpc/math/fpu_preempt.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright 2015, Cyril Bur, IBM Corp.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This test attempts to see if the FPU registers change across preemption.
6*4882a593Smuzhiyun  * Two things should be noted here a) The check_fpu function in asm only checks
7*4882a593Smuzhiyun  * the non volatile registers as it is reused from the syscall test b) There is
8*4882a593Smuzhiyun  * no way to be sure preemption happened so this test just uses many threads
9*4882a593Smuzhiyun  * and a long wait. As such, a successful test doesn't mean much but a failure
10*4882a593Smuzhiyun  * is bad.
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <stdio.h>
14*4882a593Smuzhiyun #include <unistd.h>
15*4882a593Smuzhiyun #include <sys/syscall.h>
16*4882a593Smuzhiyun #include <sys/time.h>
17*4882a593Smuzhiyun #include <sys/types.h>
18*4882a593Smuzhiyun #include <sys/wait.h>
19*4882a593Smuzhiyun #include <stdlib.h>
20*4882a593Smuzhiyun #include <pthread.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include "utils.h"
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /* Time to wait for workers to get preempted (seconds) */
25*4882a593Smuzhiyun #define PREEMPT_TIME 20
26*4882a593Smuzhiyun /*
27*4882a593Smuzhiyun  * Factor by which to multiply number of online CPUs for total number of
28*4882a593Smuzhiyun  * worker threads
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun #define THREAD_FACTOR 8
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun __thread double darray[] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
34*4882a593Smuzhiyun 		     1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0,
35*4882a593Smuzhiyun 		     2.1};
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun int threads_starting;
38*4882a593Smuzhiyun int running;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun extern void preempt_fpu(double *darray, int *threads_starting, int *running);
41*4882a593Smuzhiyun 
preempt_fpu_c(void * p)42*4882a593Smuzhiyun void *preempt_fpu_c(void *p)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	int i;
45*4882a593Smuzhiyun 	srand(pthread_self());
46*4882a593Smuzhiyun 	for (i = 0; i < 21; i++)
47*4882a593Smuzhiyun 		darray[i] = rand();
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	/* Test failed if it ever returns */
50*4882a593Smuzhiyun 	preempt_fpu(darray, &threads_starting, &running);
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	return p;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun 
test_preempt_fpu(void)55*4882a593Smuzhiyun int test_preempt_fpu(void)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun 	int i, rc, threads;
58*4882a593Smuzhiyun 	pthread_t *tids;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	threads = sysconf(_SC_NPROCESSORS_ONLN) * THREAD_FACTOR;
61*4882a593Smuzhiyun 	tids = malloc((threads) * sizeof(pthread_t));
62*4882a593Smuzhiyun 	FAIL_IF(!tids);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	running = true;
65*4882a593Smuzhiyun 	threads_starting = threads;
66*4882a593Smuzhiyun 	for (i = 0; i < threads; i++) {
67*4882a593Smuzhiyun 		rc = pthread_create(&tids[i], NULL, preempt_fpu_c, NULL);
68*4882a593Smuzhiyun 		FAIL_IF(rc);
69*4882a593Smuzhiyun 	}
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	setbuf(stdout, NULL);
72*4882a593Smuzhiyun 	/* Not really necessary but nice to wait for every thread to start */
73*4882a593Smuzhiyun 	printf("\tWaiting for all workers to start...");
74*4882a593Smuzhiyun 	while(threads_starting)
75*4882a593Smuzhiyun 		asm volatile("": : :"memory");
76*4882a593Smuzhiyun 	printf("done\n");
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	printf("\tWaiting for %d seconds to let some workers get preempted...", PREEMPT_TIME);
79*4882a593Smuzhiyun 	sleep(PREEMPT_TIME);
80*4882a593Smuzhiyun 	printf("done\n");
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	printf("\tStopping workers...");
83*4882a593Smuzhiyun 	/*
84*4882a593Smuzhiyun 	 * Working are checking this value every loop. In preempt_fpu 'cmpwi r5,0; bne 2b'.
85*4882a593Smuzhiyun 	 * r5 will have loaded the value of running.
86*4882a593Smuzhiyun 	 */
87*4882a593Smuzhiyun 	running = 0;
88*4882a593Smuzhiyun 	for (i = 0; i < threads; i++) {
89*4882a593Smuzhiyun 		void *rc_p;
90*4882a593Smuzhiyun 		pthread_join(tids[i], &rc_p);
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 		/*
93*4882a593Smuzhiyun 		 * Harness will say the fail was here, look at why preempt_fpu
94*4882a593Smuzhiyun 		 * returned
95*4882a593Smuzhiyun 		 */
96*4882a593Smuzhiyun 		if ((long) rc_p)
97*4882a593Smuzhiyun 			printf("oops\n");
98*4882a593Smuzhiyun 		FAIL_IF((long) rc_p);
99*4882a593Smuzhiyun 	}
100*4882a593Smuzhiyun 	printf("done\n");
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	free(tids);
103*4882a593Smuzhiyun 	return 0;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun 
main(int argc,char * argv[])106*4882a593Smuzhiyun int main(int argc, char *argv[])
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun 	return test_harness(test_preempt_fpu, "fpu_preempt");
109*4882a593Smuzhiyun }
110