1 /*
2  * Copyright 2007, Intel Corporation
3  *
4  * This file is part of PowerTOP
5  *
6  * This program file is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program in a file named COPYING; if not, write to the
17  * Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301 USA
20  *
21  * Authors:
22  * 	Arjan van de Ven <arjan@linux.intel.com>
23  */
24 
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <sys/types.h>
31 #include <dirent.h>
32 
33 #include "powertop.h"
34 
activate_ondemand(void)35 static void activate_ondemand(void)
36 {
37 	DIR *dir;
38 	struct dirent *dirent;
39 	FILE *file;
40 	char filename[PATH_MAX];
41 
42 	system("/sbin/modprobe cpufreq_ondemand &> /dev/null");
43 
44 
45 	dir = opendir("/sys/devices/system/cpu");
46 	if (!dir)
47 		return;
48 
49 	while ((dirent = readdir(dir))) {
50 		if (dirent->d_name[0]=='.')
51 			continue;
52 		sprintf(filename, "/sys/devices/system/cpu/%s/cpufreq/scaling_governor", dirent->d_name);
53 		file = fopen(filename, "w");
54 		if (!file)
55 			continue;
56 		fprintf(file, "ondemand\n");
57 		fclose(file);
58 	}
59 
60 	closedir(dir);
61 }
62 
suggest_ondemand_governor(void)63 void suggest_ondemand_governor(void)
64 {
65 	DIR *dir;
66 	struct dirent *dirent;
67 	FILE *file;
68 	char filename[PATH_MAX];
69 	char line[1024];
70 
71 	char gov[1024];
72 	int ret = 0;
73 
74 
75 	gov[0] = 0;
76 
77 
78 	dir = opendir("/sys/devices/system/cpu");
79 	if (!dir)
80 		return;
81 
82 	while ((dirent = readdir(dir))) {
83 		if (dirent->d_name[0]=='.')
84 			continue;
85 		sprintf(filename, "/sys/devices/system/cpu/%s/cpufreq/scaling_governor", dirent->d_name);
86 		file = fopen(filename, "r");
87 		if (!file)
88 			continue;
89 		memset(line, 0, 1024);
90 		if (fgets(line, 1023,file)==NULL) {
91 			fclose(file);
92 			continue;
93 		}
94 		if (strlen(gov)==0)
95 			strcpy(gov, line);
96 		else
97 			/* if the governors are inconsistent, warn */
98 			if (strcmp(gov, line))
99 				ret = 1;
100 		fclose(file);
101 	}
102 
103 	closedir(dir);
104 
105 	/* if the governor is set to userspace, also warn */
106 	if (strstr(gov, "userspace"))
107 		ret = 1;
108 
109 	/* if the governor is set to performance, also warn */
110 	/* FIXME: check if this is fair on all cpus */
111 	if (strstr(gov, "performance"))
112 		ret = 1;
113 
114 
115 	if (ret) {
116 		add_suggestion(_("Suggestion: Enable the ondemand cpu speed governor for all processors via: \n"
117 				 " echo ondemand > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor \n"),
118 				15, 'O', _(" O - enable Ondemand governor "), activate_ondemand);
119 	}
120 }
121