aboutsummaryrefslogtreecommitdiff
path: root/calibrate/calibrate.cpp
blob: c83093f3362850777251ba3ec6e8c5eb768784ed (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include <iostream>
#include <fstream>
#include <algorithm>

#include "calibrate.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <math.h>
#include <sys/types.h>
#include <dirent.h>

#include "../parameters/parameters.h"

#include <map>
#include <vector>
#include <string>

using namespace std;


static vector<string> usb_devices;
static vector<string> rfkill_devices;
static vector<string> backlight_devices;
static vector<string> scsi_link_devices;
static int blmax;

static map<string, string> saved_sysfs;


static volatile int stop_measurement;


static void save_sysfs(char *filename)
{
	char line[4096];
	ifstream file;

	file.open(filename, ios::in);
	if (!file)
		return;
	file.getline(line, 4096);
	file.close();

	saved_sysfs[filename] = line;
}

static void restore_all_sysfs(void)
{
	map<string, string>::iterator it;

	for (it = saved_sysfs.begin(); it != saved_sysfs.end(); it++)
		write_sysfs(it->first, it->second);
}

static void find_all_usb(void)
{
	struct dirent *entry;
	DIR *dir;
	char filename[4096];
	
	dir = opendir("/sys/bus/usb/devices/");
	if (!dir)
		return;
	while (1) {
		ifstream file;

		entry = readdir(dir);

		if (!entry)
			break;
		if (entry->d_name[0] == '.')
			continue;

		sprintf(filename, "/sys/bus/usb/devices/%s/power/active_duration", entry->d_name);
		if (access(filename, R_OK)!=0)
			continue;

		sprintf(filename, "/sys/bus/usb/devices/%s/power/idVendor", entry->d_name);
		file.open(filename, ios::in);
		if (file) {
			file.getline(filename, 4096);
			file.close();
			if (strcmp(filename, "1d6b")==0)
				continue;
		}

		sprintf(filename, "/sys/bus/usb/devices/%s/power/control", entry->d_name);

		save_sysfs(filename);

		usb_devices.push_back(filename);
	}
	closedir(dir);
}

static void suspend_all_usb_devices(void)
{
	unsigned int i;

	for (i = 0; i < usb_devices.size(); i++)
		write_sysfs(usb_devices[i], "auto\n");
}


static void find_all_rfkill(void)
{
	struct dirent *entry;
	DIR *dir;
	char filename[4096];
	
	dir = opendir("/sys/class/rfkill/");
	if (!dir)
		return;
	while (1) {
		ifstream file;

		entry = readdir(dir);

		if (!entry)
			break;
		if (entry->d_name[0] == '.')
			continue;

		sprintf(filename, "/sys/class/rfkill/%s/soft", entry->d_name);
		if (access(filename, R_OK)!=0)
			continue;

		save_sysfs(filename);

		rfkill_devices.push_back(filename);
	}
	closedir(dir);
}

static void rfkill_all_radios(void)
{
	unsigned int i;

	for (i = 0; i < rfkill_devices.size(); i++)
		write_sysfs(rfkill_devices[i], "1\n");
}
static void unrfkill_all_radios(void)
{
	unsigned int i;

	for (i = 0; i < rfkill_devices.size(); i++)
		write_sysfs(rfkill_devices[i], "0\n");
}

static void find_backlight(void)
{
	struct dirent *entry;
	DIR *dir;
	char filename[4096];
	
	dir = opendir("/sys/class/backlight/");
	if (!dir)
		return;
	while (1) {
		ifstream file;

		entry = readdir(dir);

		if (!entry)
			break;
		if (entry->d_name[0] == '.')
			continue;

		sprintf(filename, "/sys/class/backlight/%s/brightness", entry->d_name);
		if (access(filename, R_OK)!=0)
			continue;

		save_sysfs(filename);

		backlight_devices.push_back(filename);

		sprintf(filename, "/sys/class/backlight/%s/max_brightness", entry->d_name);
		blmax = read_sysfs(filename);
	}
	closedir(dir);
}

static void lower_backlight(void)
{
	unsigned int i;

	for (i = 0; i < backlight_devices.size(); i++)
		write_sysfs(backlight_devices[i], "0\n");
}


static void find_scsi_link(void)
{
	struct dirent *entry;
	DIR *dir;
	char filename[4096];
	
	dir = opendir("/sys/class/scsi_host/");
	if (!dir)
		return;
	while (1) {
		ifstream file;

		entry = readdir(dir);

		if (!entry)
			break;
		if (entry->d_name[0] == '.')
			continue;

		sprintf(filename, "/sys/class/scsi_host/%s/link_power_management_policy", entry->d_name);
		if (access(filename, R_OK)!=0)
			continue;

		save_sysfs(filename);

		scsi_link_devices.push_back(filename);

	}
	closedir(dir);
}

static void set_scsi_link(const char *state)
{
	unsigned int i;

	for (i = 0; i < scsi_link_devices.size(); i++)
		write_sysfs(scsi_link_devices[i], state);
}


static void *burn_cpu(void *dummy)
{
	volatile double d = 1.1;

	while (!stop_measurement) {
		d = pow(d, 1.0001);
	}
	return NULL;
}

static void *burn_cpu_wakeups(void *dummy)
{
	struct timespec tm;

	while (!stop_measurement) {
		tm.tv_sec = 0;
		tm.tv_nsec = (unsigned long)dummy;
		nanosleep(&tm, NULL);
	}
	return NULL;
}

static void *burn_disk(void *dummy)
{
	int fd;
	char buffer[64*1024];
	char filename[256];

	strcpy(filename ,"/tmp/powertop.XXXXXX");
	fd = mkstemp(filename);

	if (fd < 0) {
		printf("Cannot create temp file\n");
		return NULL;
	}

	while (!stop_measurement) {
		lseek(fd, 0, SEEK_SET);
		write(fd, buffer, 64*1024);
		fdatasync(fd);
	}
	close(fd);
	return NULL;
}


static void cpu_calibration(int threads)
{	
	int i;
	pthread_t thr;

	printf("Calibrating: CPU usage on %i threads\n", threads);

	stop_measurement = 0;
	for (i = 0; i < threads; i++)
		pthread_create(&thr, NULL, burn_cpu, NULL);

	one_measurement(15);
	stop_measurement = 1;
	sleep(1);
	learn_parameters(50, "cpu-consumption");
}

static void wakeup_calibration(unsigned long interval)
{	
	pthread_t thr;

	printf("Calibrating: CPU wakeup power consumption\n");

	stop_measurement = 0;
	
	pthread_create(&thr, NULL, burn_cpu_wakeups, (void *)interval);

	one_measurement(15);
	stop_measurement = 1;
	sleep(1);
	learn_parameters(50, "cpu-wakeups");
}

static void usb_calibration(void)
{
	unsigned int i;

	/* chances are one of the USB devices is bluetooth; unrfkill first */
	unrfkill_all_radios();
	printf("Calibrating USB devices\n");
	for (i = 0; i < usb_devices.size(); i++) {
		printf(".... device %s \n", usb_devices[i].c_str());
		suspend_all_usb_devices();
		write_sysfs(usb_devices[i], "on\n");
		one_measurement(15);
		suspend_all_usb_devices();
		sleep(3);		
	}
	rfkill_all_radios();
	learn_parameters(50, "usb-device");
	sleep(4);
}

static void rfkill_calibration(void)
{
	unsigned int i;

	printf("Calibrating radio devices\n");
	for (i = 0; i < rfkill_devices.size(); i++) {
		printf(".... device %s \n", rfkill_devices[i].c_str());
		rfkill_all_radios();
		write_sysfs(rfkill_devices[i], "0\n");
		one_measurement(15);
		rfkill_all_radios();
		sleep(3);		
	}
	for (i = 0; i < rfkill_devices.size(); i++) {
		printf(".... device %s \n", rfkill_devices[i].c_str());
		unrfkill_all_radios();
		write_sysfs(rfkill_devices[i], "1\n");
		one_measurement(15);
		unrfkill_all_radios();
		sleep(3);		
	}
	rfkill_all_radios();
	learn_parameters(50, "radio");
}

static void backlight_calibration(void)
{
	unsigned int i;

	printf("Calibrating backlight\n");
	for (i = 0; i < backlight_devices.size(); i++) {
		char str[4096];
		printf(".... device %s \n", backlight_devices[i].c_str());
		lower_backlight();
		one_measurement(15);
		sprintf(str, "%i\n", blmax / 4);
		write_sysfs(backlight_devices[i], str);
		one_measurement(15);

		sprintf(str, "%i\n", blmax / 2);
		write_sysfs(backlight_devices[i], str);
		one_measurement(15);

		sprintf(str, "%i\n", 3 * blmax / 4 );
		write_sysfs(backlight_devices[i], str);
		one_measurement(15);

		sprintf(str, "%i\n", blmax);
		write_sysfs(backlight_devices[i], str);
		one_measurement(15);
		lower_backlight();
		sleep(1);		
	}
	printf("Calibrating idle\n");
	system("DISPLAY=:0 /usr/bin/xset dpms force off");	
	one_measurement(15);
	system("DISPLAY=:0 /usr/bin/xset dpms force on");	
	learn_parameters(50, "backlight");
}

static void idle_calibration(void)
{
	printf("Calibrating idle\n");
	system("DISPLAY=:0 /usr/bin/xset dpms force off");	
	one_measurement(15);
	system("DISPLAY=:0 /usr/bin/xset dpms force on");	
}


static void disk_calibration(void)
{	
	pthread_t thr;

	printf("Calibrating: disk usage \n");

	set_scsi_link("min_power");

	stop_measurement = 0;
	pthread_create(&thr, NULL, burn_disk, NULL);

	one_measurement(15);
	stop_measurement = 1;
	sleep(1);

	set_scsi_link("medium_power");

	stop_measurement = 0;
	pthread_create(&thr, NULL, burn_disk, NULL);

	one_measurement(15);
	stop_measurement = 1;
	sleep(1);

	set_scsi_link("medium_power");

	stop_measurement = 0;
	one_measurement(15);
	stop_measurement = 1;
	sleep(1);

	learn_parameters(50, "ahci");

	/* work around a bug in the ahci driver where medium->min transitions don't work */
	set_scsi_link("max_performance");
	sleep(1);
	set_scsi_link("min_power");
}


void calibrate(void)
{
	find_all_usb();
	find_all_rfkill();
	find_backlight();
	find_scsi_link();

	cout << "Starting PowerTOP power estimate calibration \n";
	suspend_all_usb_devices();
	rfkill_all_radios();
	lower_backlight();

	sleep(4);
	

	idle_calibration();
	disk_calibration();
	backlight_calibration();
	cpu_calibration(1);
	cpu_calibration(4);
	wakeup_calibration(10000);
	wakeup_calibration(100000);
	wakeup_calibration(1000000);
	usb_calibration();
	rfkill_calibration();

	cout << "Finishing PowerTOP power estimate calibration \n";

	restore_all_sysfs();
        learn_parameters();
        learn_parameters();
        learn_parameters();
	printf("Parameters after calibration:\n");
	dump_parameter_bundle();
	save_parameters("saved_parameters.powertop");
        save_all_results("saved_results.powertop");

}