summaryrefslogtreecommitdiffstats
path: root/src/hardware_cpu.cpp
blob: 5bf25ac3ee4816e5d771903b34d9702bb19d7127 (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
 /**************************************************************************
 *   Copyright (C) 2006 by Daniel Gollub                                   *
 *                            <dgollub@suse.de>                            *
 *                         Danny Kukawka                                   *
 *                            <dkukawka@suse.de>, <danny.kukawka@web.de>   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License     *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 *   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; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

/*! 
 * \file 	hardware_cpu.cpp
 * \brief 	In this file can be found the CPU information related code. 
 * \author 	Danny Kukawka, <dkukawka@suse.de>, <danny.kukawka@web.de>
 *  \author 	Daniel Gollub, <dgollub@suse.de>
 * \author 	
 * \date    	2006
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

// include own header
#include "hardware_cpu.h"
#include "hardware_cpu.moc"

// QT Header
#include <tqdir.h>
#include <tqtimer.h>

// system header
#include <fcntl.h>
#include <unistd.h>

/*! The default constructor of the class CPUInfo */
CPUInfo::CPUInfo() {
	kdDebugFuncIn(trace);

	update_info_cpufreq_speed_changed = true;
	numOfCPUs = -1;

	m_hwdevices = TDEGlobal::hardwareDevices();
	m_hwdevices->setBatteryUpdatesEnabled(true);

	kdDebugFuncOut(trace);
}

/*! The default desctuctor of the class CPUInfo */
CPUInfo::~CPUInfo() {
	kdDebugFuncIn(trace);
}

/*!
 * This function counts all online/offline CPUS.
 * Returns the total count of CPUs - _not_ the last CPU ID!
 */
int CPUInfo::getCPUNum() {
	TDEGenericHardwareList hwlist = m_hwdevices->listByDeviceClass(TDEGenericDeviceType::CPU);
	return hwlist.count();
}

/*!
 * The function checks the current CPU Speed. The current cpu speed needs to be read out from 
 * sysfs and currently not be obtained through the daemon. If the CPUFreg changed the new value
 * is set to \ref cpufreq_speed .
 * \return Information if something changed or if there are errors as an interger value
 * \retval 	-1 if there are error by reading from /sys/..
 * \retval	 0 if nothing changed
 * \retval 	 1 if something changed
 */
int CPUInfo::checkCPUSpeed(){
	kdDebugFuncOut(trace);

	TDEGenericHardwareList hwlist = m_hwdevices->listByDeviceClass(TDEGenericDeviceType::CPU);

	bool speed_changed = false;
	int new_value = -1;

	// first check path for the kernel on-demand-govenour then 
	// for the use userspace case
	update_info_cpufreq_speed_changed = false;
	cpufreq_speed.clear();

	if (numOfCPUs == -1) {
		numOfCPUs = hwlist.count();
	}

	for (int cpu_id=0; cpu_id < numOfCPUs; cpu_id++) {
		TDECPUDevice* cdevice = NULL;
		TDEGenericDevice *hwdevice;
		for (hwdevice = hwlist.first(); hwdevice; hwdevice = hwlist.next()) {
			TDECPUDevice* possiblecpu = static_cast<TDECPUDevice*>(hwdevice);
			if (possiblecpu) {
				if (possiblecpu->coreNumber() == cpu_id) {
					cdevice = possiblecpu;
				}
			}
		}

		new_value = -1;

		if (cdevice) {
			new_value = cdevice->frequency();
		}
		else {
			speed_changed = true;
			// CPU disabled -> set Freq to -1
			cpufreq_speed.append(-1);
		}

		if (new_value != cpufreq_speed[cpu_id]) {
			speed_changed = true;
			cpufreq_speed.append(new_value);
		}
	}
	
	if (speed_changed) {
		update_info_cpufreq_speed_changed = true;
		kdDebugFuncOut(trace);
		return 1;
	}

	kdDebugFuncOut(trace);
	return 0;
}

/*!
 * The function checks the Speed of throttling CPU(s). The cpu speed needs to be read out from 
 * /proc/cpuinfo.
 * \return Success or error while reading /proc/cpuinfo 
 * \retval	  0 successful
 * \retval 	 -1 reading problem
 */
int CPUInfo::checkCPUSpeedThrottling() {
	kdDebugFuncOut(trace);

	TQString cpu_file = "/proc/cpuinfo";
#ifdef FAKE_CPU
	cpu_file.prepend("/tmp/foo");
#endif	
	TQFile cpu_info(cpu_file);

	// clear cpufreq list
	cpufreq_speed.clear();

	if ( !cpu_info.open(IO_ReadOnly) ) {
		cpu_info.close();
		kdDebugFuncOut(trace);
		return -1;
	}

	TQTextStream stream( &cpu_info );
	TQString line;

	while ( !stream.atEnd() ) {
		line = stream.readLine();
		
		if (line.startsWith("cpu MHz		: ")) {
			line.remove("cpu MHz		: ");
			line = line.remove(line.length() - 4, 4);
			cpufreq_speed.append(line.toInt());
		}
	}
	
	while ((int) cpufreq_speed.count() < numOfCPUs) {
		cpufreq_speed.append(-1);
	}

	cpu_info.close();
	kdDebugFuncOut(trace);
	return 0;
}

/*!
 * The function gets the current throttling state of the CPU(s). The throttling state needs to be 
 * read out from /proc/acpi/processor/CPUX/throttling.
 * \return boolean with info if throttling is supported
 * \retval true if throttling is supported
 * \retval false if not supported or on any other error
 */
bool CPUInfo::getCPUThrottlingState() {
	kdDebugFuncIn(trace);

	int id = 0;
	TQFileInfo *fi;
	TQString cpu_dirname;
	TQString dir_acpi_processor = "/proc/acpi/processor/";
#ifdef FAKE_CPU
	dir_acpi_processor.prepend("/tmp/foo");
#endif

	TQDir d_throttling(dir_acpi_processor);
	if (!d_throttling.exists()) {
		kdDebugFuncOut(trace);
		return false;
	}

	d_throttling.setFilter( TQDir::Dirs );
	d_throttling.setNameFilter("CPU*");

	const TQFileInfoList *list = d_throttling.entryInfoList();
	TQFileInfoListIterator it( *list );

	// clear throttling value list
	cpu_throttling.clear();

	while ((fi = it.current()) != 0 ) {
		cpu_dirname = fi->fileName();

		TQString throttling_device = d_throttling.absPath();
		throttling_device.append("/").append(cpu_dirname).append("/throttling");

		kdDebug() << "Throttling state file for CPU" << id << " will be: " << throttling_device << endl;

		TQFile throttling(throttling_device);

		// open throttling state file
		if ( throttling.open(IO_ReadOnly) ) {
			TQTextStream stream( &throttling );
			TQString line;

			do {
				line = stream.readLine();
			} while (!line.startsWith("   *T") && !stream.atEnd() );

			if (line.startsWith("   *T")) {
				line = line.right(3);
				line.remove("%");
				cpu_throttling.append(line.toInt());
				kdDebug () << "CPU " << id 
					   << ": cpu_throttling is set to: " << cpu_throttling[id] << endl;
			} else {
				cpu_throttling.append(0);
			}
		}

		throttling.close();
		++it; // next entry
		id++; // count cpu id
	}

	kdDebugFuncOut(trace);
	return true;
}

/*!
 * The function gets the Max CPU Speed. The max cpu speed needs to be read out from 
 * sysfs and currently not be obtained through the daemon. 
 */
void CPUInfo::getCPUMaxSpeed() {
	kdDebugFuncIn(trace);

	TDEGenericHardwareList hwlist = m_hwdevices->listByDeviceClass(TDEGenericDeviceType::CPU);

	cpufreq_max_speed.clear();

	if (numOfCPUs == -1) {
		numOfCPUs = getCPUNum();
	}

	for (int cpu_id=0; cpu_id < numOfCPUs; cpu_id++) {
		TDECPUDevice* cdevice = NULL;
		TDEGenericDevice *hwdevice;
		for (hwdevice = hwlist.first(); hwdevice; hwdevice = hwlist.next()) {
			TDECPUDevice* possiblecpu = static_cast<TDECPUDevice*>(hwdevice);
			if (possiblecpu) {
				if (possiblecpu->coreNumber() == cpu_id) {
					cdevice = possiblecpu;
				}
			}
		}

		if (cdevice) {
			cpufreq_max_speed.append(cdevice->maxFrequency());
		}
		else {
			cpufreq_max_speed.append(-1);
		}
	}

	kdDebugFuncOut(trace);
}