OSC: removed letter automation modes to not conflict with feedback
[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     free(list);
173     return 0;
174   }
175
176   num_groups = getgroups(MAX_GROUPS, list);
177
178   while (i<num_groups) {
179     if (list[i]==gid) {
180       found = 1;
181       i = num_groups;
182     }
183
184     i++;
185   }
186
187   free(list);
188
189   return found;
190 }
191
192
193 /***
194  * Checks for a definition in /etc/security/limits.conf that looks
195  * as if it allows RT scheduling priority.
196  *
197  * @returns 1 if there appears to be such a line
198  **/
199 int system_has_rtprio_limits_conf ()
200 {
201         const char* limits = "/etc/security/limits.conf";
202         char cmd[100];
203
204         snprintf (cmd, sizeof (cmd), "grep -q 'rtprio *[0-9][0-9]*' %s", limits);
205         if (system (cmd) == 0) {
206                 return 1;
207         }
208         return 0;
209 }
210
211
212 /**
213  * Checks for the existence of the 'audio' group on this system
214  *
215  * @returns 0 if there is no 'audio' group, the group id otherwise
216  **/
217 int system_has_audiogroup() {
218         return get_group_by_name("audio") || get_group_by_name ("jackuser");
219 }
220
221
222 /**
223  * Checks for the existence of 'groupname' on this system
224  *
225  * @returns 0 if there is no group, the group id otherwise
226  **/
227 int system_has_group(const char * name) {
228         return get_group_by_name(name);
229 }
230
231
232 /**
233  * Tests wether the owner of this process is in the 'audio' group.
234  *
235  * @returns 0 if the owner of this process is not in the audio group, non-0 otherwise
236  **/
237 int system_user_in_audiogroup() {
238         return system_user_in_group("audio") || system_user_in_group("jackuser");
239 }
240
241
242 /**
243  * Determines wether the owner of this process can enable rt priority.
244  *
245  * @returns 0 if this process can not be switched to rt prio, non-0 otherwise
246  **/
247 int system_user_can_rtprio() {
248   int min_prio;
249   struct sched_param schparam;
250
251   memset(&schparam, 0, sizeof(struct sched_param));
252
253   if (-1 == (min_prio = sched_get_priority_min(SCHED_FIFO))) {
254     perror("sched_get_priority");
255     exit(EXIT_FAILURE);
256   }
257   schparam.sched_priority = min_prio;
258
259   if (0 == sched_setscheduler(0, SCHED_FIFO, &schparam)) {
260     // TODO: restore previous state
261     schparam.sched_priority = 0;
262     if (0 != sched_setscheduler(0, SCHED_OTHER, &schparam)) {
263       perror("sched_setscheduler");
264       exit(EXIT_FAILURE);
265     }
266     return 1;
267   }
268
269   return 0;
270 }
271
272
273 long long unsigned int system_memlock_amount() {
274         struct rlimit limits;
275
276         if (-1==getrlimit(RLIMIT_MEMLOCK, &limits)) {
277                 perror("getrlimit on RLIMIT_MEMLOCK");
278                 exit(EXIT_FAILURE);
279         }
280
281         return limits.rlim_max;
282 }
283
284
285 /**
286  * Checks wether the memlock limit is unlimited
287  *
288  * @returns - 0 if the memlock limit is limited, non-0 otherwise
289  **/
290 int system_memlock_is_unlimited() {
291         return ((RLIM_INFINITY==system_memlock_amount())?1:0);
292 }
293
294
295 long long unsigned int system_available_physical_mem() {
296         char buf[256];
297         long long unsigned int res = 0;
298
299         if (0<read_string(const_cast<char*>("/proc/meminfo"), buf, sizeof (buf))) {
300                 if (strncmp (buf, "MemTotal:", 9) == 0) {
301                         if (sscanf (buf, "%*s %llu", &res) != 1) {
302                                 perror ("parse error in /proc/meminfo");
303                         }
304                 }
305         } else {
306                 perror("read from /proc/meminfo");
307         }
308
309         return res*1024;
310 }
311
312
313 /**
314  * Gets the version of the currently running kernel. The string
315  * returned has to be freed by the caller.
316  *
317  * @returns String with the full version of the kernel
318  **/
319 char* system_kernel_version() {
320   return NULL;
321 }
322
323
324
325 char* system_get_username() {
326   char* res = NULL;
327   char* name = NULL;
328
329   if ((name = getlogin())) {
330     res = strdup(name);
331   }
332
333   return res;
334 }