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 for (i = 0; i < ARRAY_SIZE(cdrv); ++i) {
94 fd = open(cdrv[i].file, O_RDWR);
95 if (fd == -1)
96 continue;
97
98 id = i;
99 close(fd);
100 break;
101 }
102
103 if (id == -1)
104 tst_brkm(TCONF, NULL, "overclock not supported");
105
106 tst_resm(TINFO, "found '%s' driver, sysfs knob '%s'",
107 cdrv[id].name, cdrv[id].file);
108
109 tst_sig(FORK, DEF_HANDLER, cleanup);
110
111 SAFE_FILE_SCANF(NULL, cdrv[i].file, "%d", &boost_value);
112
113 /* For intel_pstate, we cannot write data into intel_pstate/no_turbo
114 * and return EPERM if turbo is disabled by BIOS or unavailable on
115 * processor. We should check this case by writing original data.
116 */
117 if (!strcmp(cdrv[i].name, "intel_pstate") && boost_value == cdrv[i].off)
118 check_set_turbo(cdrv[i].file, cdrv[i].off_str);
119
120 /* change cpu0 scaling governor */
121 SAFE_FILE_SCANF(NULL, governor, "%s", governor_name);
122 SAFE_FILE_PRINTF(cleanup, governor, "%s", "performance");
123
124 /* use only cpu0 */
125 cpu_set_t set;
126 CPU_ZERO(&set);
127 CPU_SET(0, &set);
128 if (sched_setaffinity(0, sizeof(cpu_set_t), &set) < 0)
129 tst_brkm(TBROK | TERRNO, cleanup, "failed to set CPU0");
130
131 struct sched_param params;
132 params.sched_priority = sched_get_priority_max(SCHED_FIFO);
133 if (sched_setscheduler(getpid(), SCHED_FIFO, ¶ms)) {
134 tst_resm(TWARN | TERRNO,
135 "failed to set FIFO sched with max priority");
136 }
137 }
138
load_cpu(int max_freq_khz)139 static int load_cpu(int max_freq_khz)
140 {
141 int sum = 0, i = 0, total_time_ms;
142 struct timespec tv_start, tv_end;
143
144 const int max_sum = max_freq_khz / 1000;
145 const int units = 1000000; /* Mhz */
146
147 clock_gettime(CLOCK_MONOTONIC_RAW, &tv_start);
148
149 do {
150 for (i = 0; i < units; ++i)
151 asm ("" : : : "memory");
152 } while (++sum < max_sum);
153
154 clock_gettime(CLOCK_MONOTONIC_RAW, &tv_end);
155
156 total_time_ms = (tv_end.tv_sec - tv_start.tv_sec) * 1000 +
157 (tv_end.tv_nsec - tv_start.tv_nsec) / 1000000;
158
159 if (!total_time_ms)
160 tst_brkm(TBROK, cleanup, "time period is 0");
161
162 tst_resm(TINFO, "elapsed time is %d ms", total_time_ms);
163
164 return total_time_ms;
165 }
166
test_run(void)167 static void test_run(void)
168 {
169 int boost_time, boost_off_time, max_freq_khz;
170 SAFE_FILE_SCANF(cleanup, maxspeed, "%d", &max_freq_khz);
171 tst_resm(TINFO, "maximum speed is %d KHz", max_freq_khz);
172
173 /* Enable boost */
174 if (boost_value == cdrv[id].off)
175 SAFE_FILE_PRINTF(cleanup, cdrv[id].file, "%s", cdrv[id].on_str);
176 tst_resm(TINFO, "load CPU0 with boost enabled");
177 boost_time = load_cpu(max_freq_khz);
178
179 /* Disable boost */
180 SAFE_FILE_PRINTF(cleanup, cdrv[id].file, "%s", cdrv[id].off_str);
181 tst_resm(TINFO, "load CPU0 with boost disabled");
182 boost_off_time = load_cpu(max_freq_khz);
183
184 boost_off_time *= .98;
185
186 tst_resm((boost_time < boost_off_time) ? TPASS : TFAIL,
187 "compare time spent with and without boost (-2%%)");
188 }
189
main(int argc,char * argv[])190 int main(int argc, char *argv[])
191 {
192 tst_parse_opts(argc, argv, NULL, NULL);
193
194 setup();
195
196 test_run();
197
198 cleanup();
199
200 tst_exit();
201 }
202