sanitycheck should be looking for SCHED_FIFO
[ardour.git] / tools / sanity_check / systemtest.cpp
1 /**
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15  *
16  * Set of functions to gather system information for the jack setup wizard.
17  * 
18  * TODO: Test for rt prio availability
19  *
20  * @author Florian Faber, faber@faberman.de
21  *
22  **/
23
24 /** maximum number of groups a user can be a member of **/
25 #define MAX_GROUPS 100
26
27 #include <fcntl.h>
28
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32 #include <grp.h>
33
34 #include <sched.h>
35 #include <string.h>
36
37 #include <sys/time.h>
38 #include <sys/resource.h>
39
40 #include <stdio.h>
41 #include <errno.h>
42
43 #include "systemtest.h"
44
45 /**
46  * This function checks for the existence of known frequency scaling mechanisms 
47  * in this system by testing for the availability of scaling governors/
48  *
49  * @returns 0 if the system has no frequency scaling capabilities non-0 otherwise.
50  **/
51 int system_has_frequencyscaling() {
52   int fd;
53
54   fd = open("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors", O_RDONLY);
55
56   if (-1==fd) {
57     return 0;
58   }
59
60   (void) close(fd);
61
62   return 1;
63 }
64
65
66 static int read_string(char* filename, char* buf, size_t buflen) {
67   int fd;
68   ssize_t r=-1;
69
70   memset (buf, 0, buflen);
71
72   fd = open (filename, O_RDONLY);
73   if (-1<fd) {
74     r = read (fd, buf, buflen-1);
75     (void) close(fd);
76     
77     if (-1==r) {
78       fprintf(stderr, "Error while reading \"%s\": %s\n", filename, strerror(errno));
79       exit(EXIT_FAILURE);
80     }
81   }
82   
83   return (int) r;
84 }
85
86
87 static int read_int(char* filename, int* value) {
88   char buf[20];
89
90   if (0<read_string(filename, buf, 20)) {
91                 return (1==sscanf(buf, "%d", value));
92   }
93
94   return 0;
95 }
96
97
98 /**
99  * This function determines wether any CPU core uses a variable clock speed if frequency 
100  * scaling is available. If the governor for all cores is either "powersave" or
101  * "performance", the CPU frequency can be assumed to be static. This is also the case
102  * if scaling_min_freq and scaling_max_freq are set to the same value.
103  *
104  * @returns 0 if system doesn't use frequency scaling at the moment, non-0 otherwise
105  **/
106 int system_uses_frequencyscaling() {
107   int cpu=0, done=0, min, max;
108   char filename[256], buf[256];
109
110   while (!done) {
111           (void) snprintf(filename, 256, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", cpu);
112           if (0<read_string(filename, buf, 256)) {
113                   if ((0!=strncmp("performance", buf, 11)) && 
114                       (0!=strncmp("powersafe", buf, 9))) {
115                           // So it's neither the "performance" nor the "powersafe" governor
116                           (void) snprintf(filename, 256, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
117                           if (read_int(filename, &min)) {
118                                   (void) snprintf(filename, 256, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
119                                   if (read_int(filename, &max)) {
120                                           if (min!=max) {
121                                                   // wrong governor AND different frequency limits -> scaling
122                                                   return 1;
123                                           }
124                                   } 
125                           }
126                   }
127           } else {
128                   // couldn't open file -> no more cores
129                   done = 1;
130           }
131           cpu++;
132   }
133   
134   // couldn't find anything that points to scaling
135   return 0;
136 }
137
138
139 static gid_t get_group_by_name(const char* name) {
140   struct group* grp;
141         gid_t res = 0;
142
143   while ((0==res) && (NULL != (grp = getgrent()))) {
144     if (0==strcmp(name, grp->gr_name)) {
145       res = grp->gr_gid;
146     }
147   }
148
149         endgrent();
150
151   return res;
152 }
153
154 /**
155  * Tests wether the owner of this process is in the group 'name'.
156  *
157  * @returns 0 if the owner of this process is not in the group, non-0 otherwise
158  **/
159 int system_user_in_group(const char *name) {
160   gid_t* list = (gid_t*) malloc(MAX_GROUPS * sizeof(gid_t));
161   int num_groups, i=0, found=0;
162         unsigned int gid;
163
164   if (NULL==list) {
165     perror("Cannot allocate group list structure");
166     exit(EXIT_FAILURE);
167   }
168
169   gid = get_group_by_name(name);
170   if (0==gid) {
171     fprintf(stderr, "No %s group found\n", name);
172     return 0;
173   }
174   
175   num_groups = getgroups(MAX_GROUPS, list);
176   
177   while (i<num_groups) {
178     if (list[i]==gid) {
179       found = 1;
180       i = num_groups;
181     }
182     
183     i++;
184   }
185   
186   free(list);
187
188   return found;
189 }
190
191
192 /***
193  * Checks for a definition in /etc/security/limits.conf that looks
194  * as if it allows RT scheduling priority.
195  *
196  * @returns 1 if there appears to be such a line
197  **/
198 int system_has_rtprio_limits_conf ()
199 {
200         const char* limits = "/etc/security/limits.conf";
201         char cmd[100];
202
203         snprintf (cmd, sizeof (cmd), "grep -q 'rtprio *[0-9][0-9]*' %s", limits);
204         if (system (cmd) == 0) {
205                 return 1;
206         }
207         return 0;
208 }
209
210
211 /**
212  * Checks for the existence of the 'audio' group on this system
213  *
214  * @returns 0 if there is no 'audio' group, the group id otherwise
215  **/
216 int system_has_audiogroup() {
217         return get_group_by_name("audio") || get_group_by_name ("jackuser");
218 }
219
220
221 /**
222  * Checks for the existence of 'groupname' on this system
223  *
224  * @returns 0 if there is no group, the group id otherwise
225  **/
226 int system_has_group(const char * name) {
227         return get_group_by_name(name);
228 }
229
230
231 /**
232  * Tests wether the owner of this process is in the 'audio' group.
233  *
234  * @returns 0 if the owner of this process is not in the audio group, non-0 otherwise
235  **/
236 int system_user_in_audiogroup() {
237         return system_user_in_group("audio") || system_user_in_group("jackuser");
238 }
239
240
241 /**
242  * Determines wether the owner of this process can enable rt priority.
243  *
244  * @returns 0 if this process can not be switched to rt prio, non-0 otherwise
245  **/
246 int system_user_can_rtprio() {
247   int min_prio;
248   struct sched_param schparam;
249
250   memset(&schparam, 0, sizeof(struct sched_param));
251
252   if (-1 == (min_prio = sched_get_priority_min(SCHED_FIFO))) {
253     perror("sched_get_priority");
254     exit(EXIT_FAILURE);
255   }
256   schparam.sched_priority = min_prio;  
257
258   if (0 == sched_setscheduler(0, SCHED_FIFO, &schparam)) {
259     // TODO: restore previous state
260     schparam.sched_priority = 0;
261     if (0 != sched_setscheduler(0, SCHED_OTHER, &schparam)) {
262       perror("sched_setscheduler");
263       exit(EXIT_FAILURE);
264     }
265     return 1;
266   }  
267
268   return 0;
269 }
270
271
272 long long unsigned int system_memlock_amount() {
273         struct rlimit limits;
274
275         if (-1==getrlimit(RLIMIT_MEMLOCK, &limits)) {
276                 perror("getrlimit on RLIMIT_MEMLOCK");
277                 exit(EXIT_FAILURE);
278         }
279
280         return limits.rlim_max;
281 }
282
283
284 /**
285  * Checks wether the memlock limit is unlimited
286  *
287  * @returns - 0 if the memlock limit is limited, non-0 otherwise
288  **/
289 int system_memlock_is_unlimited() {
290         return ((RLIM_INFINITY==system_memlock_amount())?1:0);
291 }
292
293
294 long long unsigned int system_available_physical_mem() {
295         char buf[256];
296         long long unsigned int res = 0;
297
298         if (0<read_string((char*)"/proc/meminfo", buf, sizeof (buf))) {
299                 if (strncmp (buf, "MemTotal:", 9) == 0) {
300                         if (sscanf (buf, "%*s %llu", &res) != 1) {
301                                 perror ("parse error in /proc/meminfo");
302                         } 
303                 }
304         } else {
305                 perror("read from /proc/meminfo");
306         }
307
308         return res*1024;
309 }
310
311
312 /**
313  * Gets the version of the currently running kernel. The string
314  * returned has to be freed by the caller.
315  *
316  * @returns String with the full version of the kernel
317  **/
318 char* system_kernel_version() {
319   return NULL;
320 }
321
322
323
324 char* system_get_username() {
325   char* res = NULL;
326   char* name = NULL;
327
328   if ((name = getlogin())) {
329     res = strdup(name);
330   }
331
332   return res;
333 }