• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2014 Oracle and/or its affiliates. All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Author: Alexey Kodanev <alexey.kodanev@oracle.com>
19  *
20  * Check support for disabling dynamic overclocking in acpi_cpufreq driver.
21  * Required Linux 3.7+.
22  *
23  * The test compares time spent on sum calculation with/without
24  * boost-disable bit. If boost is enabled we can get a slightly shorter
25  * time period. Measure elapsed time instead of sysfs cpuinfo_cur_freq value,
26  * because after the upstream commit 8673b83bf2f013379453b4779047bf3c6ae387e4,
27  * current cpu frequency became target cpu frequency.
28  */
29 
30 #define _GNU_SOURCE
31 #include <errno.h>
32 #include <sched.h>
33 #include <time.h>
34 
35 #include "test.h"
36 #include "lapi/posix_clocks.h"
37 #include "safe_macros.h"
38 
39 char *TCID = "cpufreq_boost";
40 
41 #define SYSFS_CPU_DIR "/sys/devices/system/cpu/"
42 
43 struct cpufreq_driver_info {
44 	char *name;
45 	int off;
46 	char *on_str;
47 	char *off_str;
48 	char *file;
49 };
50 static const struct cpufreq_driver_info cdrv[] = {
51 	{ "acpi_cpufreq", 0, "1", "0", SYSFS_CPU_DIR "cpufreq/boost" },
52 	{ "intel_pstate", 1, "0", "1", SYSFS_CPU_DIR "intel_pstate/no_turbo" },
53 };
54 static int id = -1;
55 
56 static int boost_value;
57 
58 const char governor[]	= SYSFS_CPU_DIR "cpu0/cpufreq/scaling_governor";
59 static char governor_name[16];
60 
61 const char maxspeed[]	= SYSFS_CPU_DIR "cpu0/cpufreq/scaling_max_freq";
62 
check_set_turbo(char * file,char * off)63 static void check_set_turbo(char *file, char *off)
64 {
65 	int fd;
66 
67 	fd = SAFE_OPEN(NULL, file, O_WRONLY);
68 
69 	/* We try to skip test when getting EPERM. */
70 	if (write(fd, off, 1) == -1 && errno == EPERM) {
71 		SAFE_CLOSE(NULL, fd);
72 		tst_brkm(TCONF, NULL,
73 			 "Turbo is disabled by BIOS or unavailable on processor");
74 	}
75 
76 	SAFE_CLOSE(NULL, fd);
77 }
78 
cleanup(void)79 static void cleanup(void)
80 {
81 	FILE_PRINTF(cdrv[id].file, "%d", boost_value);
82 
83 	if (governor[0] != '\0')
84 		FILE_PRINTF(governor, "%s", governor_name);
85 }
86 
setup(void)87 static void setup(void)
88 {
89 	int fd;
90 	unsigned int i;
91 	tst_require_root();
92 
93 	if (tst_is_virt(VIRT_ANY))
94 		tst_brkm(TCONF, NULL, "running in a virtual machine, overclock not reliably measureable");
95 
96 	for (i = 0; i < ARRAY_SIZE(cdrv); ++i) {
97 		fd = open(cdrv[i].file, O_RDWR);
98 		if (fd == -1)
99 			continue;
100 
101 		id = i;
102 		close(fd);
103 		break;
104 	}
105 
106 	if (id == -1)
107 		tst_brkm(TCONF, NULL, "overclock not supported");
108 
109 	tst_resm(TINFO, "found '%s' driver, sysfs knob '%s'",
110 		cdrv[id].name, cdrv[id].file);
111 
112 	tst_sig(FORK, DEF_HANDLER, cleanup);
113 
114 	SAFE_FILE_SCANF(NULL, cdrv[i].file, "%d", &boost_value);
115 
116 	/* For intel_pstate, we cannot write data into intel_pstate/no_turbo
117 	 * and return EPERM if turbo is disabled by BIOS or unavailable on
118 	 * processor.  We should check this case by writing original data.
119 	 */
120 	if (!strcmp(cdrv[i].name, "intel_pstate") && boost_value == cdrv[i].off)
121 		check_set_turbo(cdrv[i].file, cdrv[i].off_str);
122 
123 	/* change cpu0 scaling governor */
124 	SAFE_FILE_SCANF(NULL, governor, "%s", governor_name);
125 	SAFE_FILE_PRINTF(cleanup, governor, "%s", "performance");
126 
127 	/* use only cpu0 */
128 	cpu_set_t set;
129 	CPU_ZERO(&set);
130 	CPU_SET(0, &set);
131 	if (sched_setaffinity(0, sizeof(cpu_set_t), &set) < 0)
132 		tst_brkm(TBROK | TERRNO, cleanup, "failed to set CPU0");
133 
134 	struct sched_param params;
135 	params.sched_priority = sched_get_priority_max(SCHED_FIFO);
136 	if (sched_setscheduler(getpid(), SCHED_FIFO, &params)) {
137 		tst_resm(TWARN | TERRNO,
138 			"failed to set FIFO sched with max priority");
139 	}
140 }
141 
load_cpu(int max_freq_khz)142 static int load_cpu(int max_freq_khz)
143 {
144 	int sum = 0, i = 0, total_time_ms;
145 	struct timespec tv_start, tv_end;
146 
147 	const int max_sum = max_freq_khz / 1000;
148 	const int units = 1000000; /* Mhz */
149 
150 	clock_gettime(CLOCK_MONOTONIC_RAW, &tv_start);
151 
152 	do {
153 		for (i = 0; i < units; ++i)
154 			asm ("" : : : "memory");
155 	} while (++sum < max_sum);
156 
157 	clock_gettime(CLOCK_MONOTONIC_RAW, &tv_end);
158 
159 	total_time_ms = (tv_end.tv_sec - tv_start.tv_sec) * 1000 +
160 		(tv_end.tv_nsec - tv_start.tv_nsec) / 1000000;
161 
162 	if (!total_time_ms)
163 		tst_brkm(TBROK, cleanup, "time period is 0");
164 
165 	tst_resm(TINFO, "elapsed time is %d ms", total_time_ms);
166 
167 	return total_time_ms;
168 }
169 
test_run(void)170 static void test_run(void)
171 {
172 	int boost_time, boost_off_time, max_freq_khz;
173 	SAFE_FILE_SCANF(cleanup, maxspeed, "%d", &max_freq_khz);
174 	tst_resm(TINFO, "maximum speed is %d KHz", max_freq_khz);
175 
176 	/* Enable boost */
177 	if (boost_value == cdrv[id].off)
178 		SAFE_FILE_PRINTF(cleanup, cdrv[id].file, "%s", cdrv[id].on_str);
179 	tst_resm(TINFO, "load CPU0 with boost enabled");
180 	boost_time = load_cpu(max_freq_khz);
181 
182 	/* Disable boost */
183 	SAFE_FILE_PRINTF(cleanup, cdrv[id].file, "%s", cdrv[id].off_str);
184 	tst_resm(TINFO, "load CPU0 with boost disabled");
185 	boost_off_time = load_cpu(max_freq_khz);
186 
187 	boost_off_time *= .98;
188 
189 	tst_resm((boost_time < boost_off_time) ? TPASS : TFAIL,
190 		"compare time spent with and without boost (-2%%)");
191 }
192 
main(int argc,char * argv[])193 int main(int argc, char *argv[])
194 {
195 	tst_parse_opts(argc, argv, NULL, NULL);
196 
197 	setup();
198 
199 	test_run();
200 
201 	cleanup();
202 
203 	tst_exit();
204 }
205