/[projects]/wxCpuThrottle/src/acpiparser.cpp
ViewVC logotype

Contents of /wxCpuThrottle/src/acpiparser.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 326 - (show annotations) (download)
Wed Sep 16 18:47:18 2009 UTC (14 years, 8 months ago) by torben
File size: 3340 byte(s)
Use CPU0 instead of CPU1

1 /***************************************************************************
2 * Copyright (C) 2007 by Torben Nielsen *
3 * torben@t-hoerup.dk *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #include "acpiparser.h"
21
22 #include <iostream>
23 #include <fstream>
24 #include <sstream>
25 #include <stdexcept>
26 #include <stdlib.h>
27
28 AcpiParser::AcpiParser()
29 {
30 mNumstates = 0;
31 mCurrentState = 0;
32 }
33
34
35 AcpiParser::~AcpiParser()
36 {
37 }
38
39 void AcpiParser::ReadAcpiFile()
40 {
41 char buffer[1024];
42 std::ifstream in;
43
44 in.open("/proc/acpi/processor/CPU0/throttling");
45 if (!in)
46 throw std::runtime_error("Could not open file");
47
48 int linecounter = 0;
49 mStateDescriptions.clear();
50
51 while (! in.eof() )
52 {
53 in.getline(buffer,1024);
54 std::string line(buffer);
55 linecounter++;
56
57 unsigned int pos = line.rfind(' ');
58 if (pos == std::string::npos)
59 continue;
60 pos++;
61
62 std::string value = line.substr(pos, line.size() - pos);
63 //std::cout << "Value: (" << linecounter << ") = " << value << std::endl;
64
65 switch (linecounter)
66 {
67 case 1:
68 mNumstates = atoi(value.c_str());
69 break;
70 case 2:
71 {
72 std::string tmp = value.substr(1, value.size() -1);
73 mCurrentState = atoi(tmp.c_str() );
74 //std::cout << "tmp: >" << tmp << "< = " << std::endl;
75 break;
76 }
77 case 3:
78 break;
79 default:
80 mStateDescriptions.push_back( value );
81 }
82 }
83
84 in.close();
85 //std::cout << "Current state:" << mCurrentState <<std::endl;
86 //std::cout << "Numstates:" << mNumstates << std::endl;
87 }
88
89
90 int AcpiParser::GetCurrentState()
91 {
92 return mCurrentState;
93 }
94
95
96
97 int AcpiParser::GetStateCount()
98 {
99 return mNumstates;
100 }
101
102
103 std::string AcpiParser::GetStateDescription(unsigned int stateNumber)
104 {
105 if (stateNumber >= mStateDescriptions.size() )
106 return "";
107
108 return mStateDescriptions[stateNumber];
109 }
110
111
112
113 void AcpiParser::SetState(int newState)
114 {
115 if (newState > mNumstates)
116 throw std::invalid_argument("newState too small");
117
118 std::ofstream out;
119 out.open("/proc/acpi/processor/CPU0/throttling");
120
121 if (!out)
122 throw std::runtime_error("Could not open file");
123
124
125 std::stringstream buf;
126 buf << newState << "\n";
127 out << buf.str();
128 out.close();
129 }

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.20