aboutsummaryrefslogtreecommitdiff
path: root/src/devices/ahci.cpp
blob: e65cff90cc91ec022b999c2b9c5ac4dbe2702ddb (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
/*
 * Copyright 2010, Intel Corporation
 *
 * This file is part of PowerTOP
 *
 * This program file is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program in a file named COPYING; if not, write to the
 * Free Software Foundation, Inc,
 * 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 * or just google for it.
 *
 * Authors:
 *	Arjan van de Ven <arjan@linux.intel.com>
 */
#include <iostream>
#include <fstream>

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <limits.h>


using namespace std;

#include "device.h"
#include "report/report.h"
#include "report/report-maker.h"
#include "ahci.h"
#include "../parameters/parameters.h"
#include "report/report-data-html.h"
#include <string.h>

vector <class ahci *> links;

static string disk_name(char *path, char *target, char *shortname)
{

	DIR *dir;
	struct dirent *dirent;
	char pathname[PATH_MAX];
	string diskname = "";

	snprintf(pathname, sizeof(pathname), "%s/%s", path, target);
	dir = opendir(pathname);
	if (!dir)
		return diskname;

	while ((dirent = readdir(dir))) {
		char line[4096], *c;
		FILE *file;
		if (dirent->d_name[0]=='.')
			continue;

		if (!strchr(dirent->d_name, ':'))
			continue;

		snprintf(line, sizeof(line), "%s/%s/model", pathname, dirent->d_name);
		file = fopen(line, "r");
		if (file) {
			if (fgets(line, sizeof(line), file) == NULL) {
				fclose(file);
				break;
			}
			fclose(file);
			c = strchr(line, '\n');
			if (c)
				*c = 0;
			diskname = line;
			break;
		}
	}
	closedir(dir);

	return diskname;
}

static string model_name(char *path, char *shortname)
{

	DIR *dir;
	struct dirent *dirent;
	char pathname[PATH_MAX];

	snprintf(pathname, sizeof(pathname), "%s/device", path);

	dir = opendir(pathname);
	if (!dir)
		return strdup(shortname);

	while ((dirent = readdir(dir))) {
		if (dirent->d_name[0]=='.')
			continue;

		if (!strchr(dirent->d_name, ':'))
			continue;
		if (!strstr(dirent->d_name, "target"))
			continue;
		return disk_name(pathname, dirent->d_name, shortname);
	}
	closedir(dir);

	return "";
}

ahci::ahci(char *_name, char *path): device()
{
	char buffer[4096];
	char devname[128];
	string diskname;

	end_active = 0;
	end_slumber = 0;
	end_devslp = 0;
	end_partial = 0;
	start_active = 0;
	start_slumber = 0;
	start_devslp = 0;
	start_partial = 0;
	strncpy(sysfs_path, path, sizeof(sysfs_path));

	register_sysfs_path(sysfs_path);

	snprintf(devname, sizeof(devname), "ahci:%s", _name);
	strncpy(name, devname, sizeof(name));
	active_index = get_param_index("ahci-link-power-active");
	partial_index = get_param_index("ahci-link-power-partial");

	snprintf(buffer, sizeof(buffer), "%s-active", name);
	active_rindex = get_result_index(buffer);

	snprintf(buffer, sizeof(buffer), "%s-partial", name);
	partial_rindex = get_result_index(buffer);

	snprintf(buffer, sizeof(buffer), "%s-slumber", name);
	slumber_rindex = get_result_index(buffer);

	snprintf(buffer, sizeof(buffer), "%s-devslp", name);
	devslp_rindex = get_result_index(buffer);

	diskname = model_name(path, _name);

	if (strlen(diskname.c_str()) == 0)
		snprintf(humanname, sizeof(humanname), _("SATA link: %s"), _name);
	else
		snprintf(humanname, sizeof(humanname), _("SATA disk: %s"), diskname.c_str());
}

void ahci::start_measurement(void)
{
	char filename[PATH_MAX];
	ifstream file;

	snprintf(filename, sizeof(filename), "%s/ahci_alpm_active", sysfs_path);
#ifndef DISABLE_TRYCATCH
	try {
#endif
		file.open(filename, ios::in);
		if (file) {
			file >> start_active;
		}
		file.close();
		snprintf(filename, sizeof(filename), "%s/ahci_alpm_partial", sysfs_path);
		file.open(filename, ios::in);

		if (file) {
			file >> start_partial;
		}
		file.close();
		snprintf(filename, sizeof(filename), "%s/ahci_alpm_slumber", sysfs_path);
		file.open(filename, ios::in);
		if (file) {
			file >> start_slumber;
		}
		file.close();
		snprintf(filename, sizeof(filename), "%s/ahci_alpm_devslp", sysfs_path);
		file.open(filename, ios::in);
		if (file) {
			file >> start_devslp;
		}
		file.close();
#ifndef DISABLE_TRYCATCH
	}
	catch (std::ios_base::failure &c) {
		fprintf(stderr, "Failed to start measurement for ahci device\n");
	}
#endif
}

void ahci::end_measurement(void)
{
	char filename[PATH_MAX];
	char powername[4096];
	ifstream file;
	double p;
	double total;

#ifndef DISABLE_TRYCATCH
	try {
#endif
		snprintf(filename, sizeof(filename), "%s/ahci_alpm_active", sysfs_path);
		file.open(filename, ios::in);
		if (file) {
			file >> end_active;
		}
		file.close();
		snprintf(filename, sizeof(filename), "%s/ahci_alpm_partial", sysfs_path);
		file.open(filename, ios::in);
		if (file) {
			file >> end_partial;
		}
		file.close();
		snprintf(filename, sizeof(filename), "%s/ahci_alpm_slumber", sysfs_path);
		file.open(filename, ios::in);
		if (file) {
			file >> end_slumber;
		}
		file.close();
		snprintf(filename, sizeof(filename), "%s/ahci_alpm_devslp", sysfs_path);
		file.open(filename, ios::in);
		if (file) {
			file >> end_devslp;
		}
		file.close();
#ifndef DISABLE_TRYCATCH
	}
	catch (std::ios_base::failure &c) {
		fprintf(stderr, "Failed to end measurement for ahci device\n");
	}
#endif
	if (end_active < start_active)
		end_active = start_active;
	if (end_partial < start_partial)
		end_partial = start_partial;
	if (end_slumber < start_slumber)
		end_slumber = start_slumber;

	total = 0.001 + end_active + end_partial + end_slumber + end_devslp -
		start_active - start_partial - start_slumber - start_devslp;

	/* percent in active */
	p = (end_active - start_active) / total * 100.0;
	if (p < 0)
		 p = 0;
	snprintf(powername, sizeof(powername), "%s-active", name);
	report_utilization(powername, p);

	/* percent in partial */
	p = (end_partial - start_partial) / total * 100.0;
	if (p < 0)
		 p = 0;
	snprintf(powername, sizeof(powername), "%s-partial", name);
	report_utilization(powername, p);

	/* percent in slumber */
	p = (end_slumber - start_slumber) / total * 100.0;
	if (p < 0)
		 p = 0;
	snprintf(powername, sizeof(powername), "%s-slumber", name);
	report_utilization(powername, p);

	/* percent in devslp */
	p = (end_devslp - start_devslp) / total * 100.0;
	if (p < 0)
		 p = 0;
	snprintf(powername, sizeof(powername), "%s-devslp", name);
	report_utilization(powername, p);
}


double ahci::utilization(void)
{
	double p;

	p = (end_partial - start_partial + end_active - start_active) / (0.001 + end_active + end_partial + end_slumber + end_devslp - start_active - start_partial - start_slumber - start_devslp) * 100.0;

	if (p < 0)
		p = 0;

	return p;
}

const char * ahci::device_name(void)
{
	return name;
}

void create_all_ahcis(void)
{
	struct dirent *entry;
	DIR *dir;
	char filename[PATH_MAX];

	dir = opendir("/sys/class/scsi_host/");
	if (!dir)
		return;
	while (1) {
		class ahci *bl;
		ofstream file;
		ifstream check_file;
		entry = readdir(dir);
		if (!entry)
			break;
		if (entry->d_name[0] == '.')
			continue;
		snprintf(filename, sizeof(filename), "/sys/class/scsi_host/%s/ahci_alpm_accounting", entry->d_name);

		check_file.open(filename, ios::in);
		check_file.get();
		check_file.close();
		if (check_file.bad())
			continue;

		file.open(filename, ios::in);
		if (!file)
			continue;
		file << 1 ;
		file.close();
		snprintf(filename, sizeof(filename), "/sys/class/scsi_host/%s", entry->d_name);

		bl = new class ahci(entry->d_name, filename);
		all_devices.push_back(bl);
		register_parameter("ahci-link-power-active", 0.6);  /* active sata link takes about 0.6 W */
		register_parameter("ahci-link-power-partial");
		links.push_back(bl);
	}
	closedir(dir);

}



double ahci::power_usage(struct result_bundle *result, struct parameter_bundle *bundle)
{
	double power;
	double factor;
	double util;

	power = 0;

	factor = get_parameter_value(active_index, bundle);
	util = get_result_value(active_rindex, result);
	power += util * factor / 100.0;


	factor = get_parameter_value(partial_index, bundle);
	util = get_result_value(partial_rindex, result);
	power += util * factor / 100.0;

	return power;
}

void ahci_create_device_stats_table(void)
{
	unsigned int i;
	int cols=0;
	int rows=0;

	/* div attr css_class and css_id */
	tag_attr div_attr;
	init_div(&div_attr, "clear_block", "ahci");

	/* Set Title attributes */
	tag_attr title_attr;
	init_title_attr(&title_attr);

	/* Add section */
	report.add_div(&div_attr);

	if (links.size() == 0) {
		report.add_title(&title_attr, __("AHCI ALPM Residency Statistics - Not supported on this macine"));
		report.end_div();
		return;
	}

	/* Set Table attributes, rows, and cols */
	table_attributes std_table_css;
	cols=5;
	rows=links.size()+1;
	init_std_side_table_attr(&std_table_css, rows, cols);



	/* Set array of data in row Major order */
	string *ahci_data = new string[cols * rows];
	ahci_data[0]=__("Link");
	ahci_data[1]=__("Active");
	ahci_data[2]=__("Partial");
	ahci_data[3]=__("Slumber");
	ahci_data[4]=__("Devslp");

	/* traverse list of all devices and put their residency in the table */
	for (i = 0; i < links.size(); i++){
		links[i]->report_device_stats(ahci_data, i);
	}
	report.add_title(&title_attr, __("AHCI ALPM Residency Statistics"));
	report.add_table(ahci_data, &std_table_css);
	report.end_div();
	delete [] ahci_data;
}

void ahci::report_device_stats(string *ahci_data, int idx)
{
	int offset=(idx*5+5);
	char util[128];
	double active_util = get_result_value(active_rindex, &all_results);
	double partial_util = get_result_value(partial_rindex, &all_results);
	double slumber_util = get_result_value(slumber_rindex, &all_results);
	double devslp_util = get_result_value(devslp_rindex, &all_results);

	ahci_data[offset]=humanname;
	printf("\nData from ahci %s\n",ahci_data[offset].c_str());
	offset +=1;

	snprintf(util, sizeof(util), "%5.1f",  active_util);
	ahci_data[offset]= util;
	offset +=1;

	snprintf(util, sizeof(util), "%5.1f",  partial_util);
	ahci_data[offset]= util;
	offset +=1;

	snprintf(util, sizeof(util), "%5.1f",  slumber_util);
	ahci_data[offset]= util;
	offset +=1;

	snprintf(util, sizeof(util), "%5.1f",  devslp_util);
	ahci_data[offset]= util;
}