summaryrefslogtreecommitdiffstats
path: root/tools/power/cpupower/utils/cpuidle-set.c
blob: d6b6ae44b8c247ffdb6378d8dcd3cb36f0265142 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <ctype.h>

#include <getopt.h>

#include "cpufreq.h"
#include "helpers/helpers.h"
#include "helpers/sysfs.h"

static struct option info_opts[] = {
     {"disable",	required_argument,		NULL, 'd'},
     {"enable",		required_argument,		NULL, 'e'},
     {"disable-by-latency", required_argument,		NULL, 'D'},
     {"enable-all",	no_argument,			NULL, 'E'},
     { },
};


int cmd_idle_set(int argc, char **argv)
{
	extern char *optarg;
	extern int optind, opterr, optopt;
	int ret = 0, cont = 1, param = 0, disabled;
	unsigned long long latency = 0, state_latency;
	unsigned int cpu = 0, idlestate = 0, idlestates = 0;
	char *endptr;

	do {
		ret = getopt_long(argc, argv, "d:e:ED:", info_opts, NULL);
		if (ret == -1)
			break;
		switch (ret) {
		case '?':
			param = '?';
			cont = 0;
			break;
		case 'd':
			if (param) {
				param = -1;
				cont = 0;
				break;
			}
			param = ret;
			idlestate = atoi(optarg);
			break;
		case 'e':
			if (param) {
				param = -1;
				cont = 0;
				break;
			}
			param = ret;
			idlestate = atoi(optarg);
			break;
		case 'D':
			if (param) {
				param = -1;
				cont = 0;
				break;
			}
			param = ret;
			latency = strtoull(optarg, &endptr, 10);
			if (*endptr != '\0') {
				printf(_("Bad latency value: %s\n"), optarg);
				exit(EXIT_FAILURE);
			}
			break;
		case 'E':
			if (param) {
				param = -1;
				cont = 0;
				break;
			}
			param = ret;
			break;
		case -1:
			cont = 0;
			break;
		}
	} while (cont);

	switch (param) {
	case -1:
		printf(_("You can't specify more than one "
			 "output-specific argument\n"));
		exit(EXIT_FAILURE);
	case '?':
		printf(_("invalid or unknown argument\n"));
		exit(EXIT_FAILURE);
	}

	/* Default is: set all CPUs */
	if (bitmask_isallclear(cpus_chosen))
		bitmask_setall(cpus_chosen);

	for (cpu = bitmask_first(cpus_chosen);
	     cpu <= bitmask_last(cpus_chosen); cpu++) {

		if (!bitmask_isbitset(cpus_chosen, cpu))
			continue;

		if (sysfs_is_cpu_online(cpu) != 1)
			continue;

		idlestates = sysfs_get_idlestate_count(cpu);
		if (idlestates <= 0)
			continue;

		switch (param) {
		case 'd':
			ret = sysfs_idlestate_disable(cpu, idlestate, 1);
			if (ret == 0)
		printf(_("Idlestate %u disabled on CPU %u\n"),  idlestate, cpu);
			else if (ret == -1)
		printf(_("Idlestate %u not available on CPU %u\n"),
		       idlestate, cpu);
			else if (ret == -2)
		printf(_("Idlestate disabling not supported by kernel\n"));
			else
		printf(_("Idlestate %u not disabled on CPU %u\n"),
		       idlestate, cpu);
			break;
		case 'e':
			ret = sysfs_idlestate_disable(cpu, idlestate, 0);
			if (ret == 0)
		printf(_("Idlestate %u enabled on CPU %u\n"),  idlestate, cpu);
			else if (ret == -1)
		printf(_("Idlestate %u not available on CPU %u\n"),
		       idlestate, cpu);
			else if (ret == -2)
		printf(_("Idlestate enabling not supported by kernel\n"));
			else
		printf(_("Idlestate %u not enabled on CPU %u\n"),
		       idlestate, cpu);
			break;
		case 'D':
			for (idlestate = 0; idlestate < idlestates; idlestate++) {
				disabled = sysfs_is_idlestate_disabled
					(cpu, idlestate);
				state_latency = sysfs_get_idlestate_latency
					(cpu, idlestate);
				if (disabled == 1) {
					if (latency > state_latency){
						ret = sysfs_idlestate_disable
							(cpu, idlestate, 0);
						if (ret == 0)
		printf(_("Idlestate %u enabled on CPU %u\n"),  idlestate, cpu);
					}
					continue;
				}
				if (latency <= state_latency){
					ret = sysfs_idlestate_disable
						(cpu, idlestate, 1);
					if (ret == 0)
		printf(_("Idlestate %u disabled on CPU %u\n"), idlestate, cpu);
				}
			}
			break;
		case 'E':
			for (idlestate = 0; idlestate < idlestates; idlestate++) {
				disabled = sysfs_is_idlestate_disabled
					(cpu, idlestate);
				if (disabled == 1) {
					ret = sysfs_idlestate_disable
						(cpu, idlestate, 0);
					if (ret == 0)
		printf(_("Idlestate %u enabled on CPU %u\n"), idlestate, cpu);
				}
			}
			break;
		default:
			/* Not reachable with proper args checking */
			printf(_("Invalid or unknown argument\n"));
			exit(EXIT_FAILURE);
			break;
		}
	}
	return EXIT_SUCCESS;
}
OpenPOWER on IntegriCloud