1 /* Copyright (C) 2006-2022 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #include <errno.h>
19 #include <pthread.h>
20 #include <sched.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/syscall.h>
26 
27 /* This test is Linux specific.  */
28 #define CHECK_TPP_PRIORITY(normal, boosted) \
29   do								\
30     {								\
31       pid_t tid = syscall (__NR_gettid);			\
32 								\
33       struct sched_param cep_sp;				\
34       int cep_policy;						\
35       if (pthread_getschedparam (pthread_self (), &cep_policy,	\
36 				 &cep_sp) != 0)			\
37 	{							\
38 	  puts ("getschedparam failed");			\
39 	  ret = 1;						\
40 	}							\
41       else if (cep_sp.sched_priority != (normal))		\
42 	{							\
43 	  printf ("unexpected priority %d != %d\n",		\
44 		  cep_sp.sched_priority, (normal));		\
45 	}							\
46       if (syscall (__NR_sched_getparam, tid, &cep_sp) == 0	\
47 	  && cep_sp.sched_priority != (boosted))		\
48 	{							\
49 	  printf ("unexpected boosted priority %d != %d\n",	\
50 		  cep_sp.sched_priority, (boosted));		\
51 	  ret = 1;						\
52 	}							\
53     }								\
54   while (0)
55 
56 int fifo_min, fifo_max;
57 
58 void
init_tpp_test(void)59 init_tpp_test (void)
60 {
61   fifo_min = sched_get_priority_min (SCHED_FIFO);
62   if (fifo_min < 0)
63     {
64       printf ("couldn't get min priority for SCHED_FIFO: %m\n");
65       exit (1);
66     }
67 
68   fifo_max = sched_get_priority_max (SCHED_FIFO);
69   if (fifo_max < 0)
70     {
71       printf ("couldn't get max priority for SCHED_FIFO: %m\n");
72       exit (1);
73     }
74 
75   if (fifo_min > 4 || fifo_max < 10)
76     {
77       printf ("%d..%d SCHED_FIFO priority interval not suitable for this test\n",
78 	      fifo_min, fifo_max);
79       exit (0);
80     }
81 
82   struct sched_param sp;
83   memset (&sp, 0, sizeof (sp));
84   sp.sched_priority = 4;
85   int e = pthread_setschedparam (pthread_self (), SCHED_FIFO, &sp);
86   if (e != 0)
87     {
88       errno = e;
89       printf ("cannot set scheduling params: %m\n");
90       exit (0);
91     }
92 }
93