aboutsummaryrefslogtreecommitdiff
path: root/devices/ahci.cpp
blob: dc657952064d235f9651ec1e0b565726afd4bbd6 (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
#include <iostream>
#include <fstream>

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


using namespace std;

#include "device.h"
#include "ahci.h"
#include "../parameters/parameters.h"

#include <string.h>


ahci::ahci(char *_name, char *path)
{
	char devname[128];
	end_active = 0;
	end_slumber = 0;
	end_partial = 0;
	start_active = 0;
	start_slumber = 0;
	start_partial = 0;
	strncpy(sysfs_path, path, sizeof(sysfs_path));
	sprintf(devname, "ahci:%s", _name);
	strncpy(name, devname, sizeof(name));
}

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

	sprintf(filename, "%s/ahci_alpm_active", sysfs_path);
	try {
		file.open(filename, ios::in);
		if (file) {
			file >> start_active;
		}
		file.close();
		sprintf(filename, "%s/ahci_alpm_partial", sysfs_path);
		file.open(filename, ios::in);
		
		if (file) {
			file >> start_partial;
		}
		file.close();
		sprintf(filename, "%s/ahci_alpm_slumber", sysfs_path);
		file.open(filename, ios::in);
		if (file) {
				file >> start_slumber;
		}	
		file.close();
	} catch (std::ios_base::failure c) {
	}

}

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

	try {
		sprintf(filename, "%s/ahci_alpm_active", sysfs_path);
		file.open(filename, ios::in);
		if (file) {
			file >> end_active;
		}
		file.close();
		sprintf(filename, "%s/ahci_alpm_partial", sysfs_path);
		file.open(filename, ios::in);
		if (file) {
			file >> end_partial;
		}
		file.close();
		sprintf(filename, "%s/ahci_alpm_slumber", sysfs_path);
		file.open(filename, ios::in);
		if (file) {
			file >> end_slumber;
		}
		file.close();
	} catch (std::ios_base::failure c) {
	}

	p = (end_active - start_active) / (0.001 + end_active + end_partial + end_slumber - start_active - start_partial - start_slumber) * 100.0;
	if (p < 0)
		 p = 0;
	sprintf(powername, "%s-active", name);
	report_utilization(powername, p);

	p = (end_partial - start_partial) / (0.001 + end_active + end_partial + end_slumber - start_active - start_partial - start_slumber) * 100.0;
	if (p < 0)
		 p = 0;
	sprintf(powername, "%s-partial", 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 - start_active - start_partial - start_slumber) * 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[4096];
	
	dir = opendir("/sys/class/scsi_host/");
	if (!dir)
		return;
	while (1) {
		class ahci *bl;
		ofstream file;
		entry = readdir(dir);
		if (!entry)
			break;
		if (entry->d_name[0] == '.')
			continue;
		sprintf(filename, "/sys/class/scsi_host/%s/ahci_alpm_accounting", entry->d_name);
		file.open(filename, ios::in);
		if (!file)
			continue;
		file << 1 ;
		file.close();
		sprintf(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");
		register_parameter("ahci-link-power-partial");
	}
	closedir(dir);

}



double ahci::power_usage(struct result_bundle *result, struct parameter_bundle *bundle)
{
	double power;
	double factor;
	double utilization;
	char buffer[4096];

	static int active_index = 0;
	static int partial_index = 0;

	if (!active_index)
		active_index = get_param_index("ahci-link-power-active");

	if (!partial_index)
		partial_index = get_param_index("ahci-link-power-partial");


	power = 0;
	factor = get_parameter_value(active_index, bundle);

	if (!active_rindex) {
		sprintf(buffer, "%s-active", name);
		active_rindex = get_result_index(buffer);
	}


	utilization = get_result_value(active_rindex, result);

	power += utilization * factor / 100.0;


	if (!partial_rindex) {
		sprintf(buffer, "%s-partial", name);
		partial_rindex = get_result_index(buffer);
	}

	factor = get_parameter_value(partial_index, bundle);
	utilization = get_result_value(partial_rindex, result);

	power += utilization * factor / 100.0;

	return power;
}