forked from fenrus75/powertop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.cpp
More file actions
212 lines (182 loc) · 5.92 KB
/
report.cpp
File metadata and controls
212 lines (182 loc) · 5.92 KB
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
/*
* Copyright 2011, 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>
* Chris Ferron <chris.e.ferron@linux.intel.com>
*/
#include "css.h"
#include "lib.h"
#include "report.h"
#include <errno.h>
#include <string.h>
#include <utility>
#include <iostream>
#include <fstream>
#include <string.h>
#include <malloc.h>
#include <unistd.h>
using namespace std;
struct reportstream reportout;
bool reporttype;
static void css_header(void)
{
if (!reportout.http_report)
return;
#ifdef EXTERNAL_CSS_FILE
if (reporttype)
fprintf(reportout.http_report, "<link rel=\"stylesheet\" href=\"powertop.css\">\n");
#else
if (reporttype) {
fprintf(reportout.http_report, "<style type=\"text/css\">\n");
fprintf(reportout.http_report, "%s\n", css);
fprintf(reportout.http_report, "</style>\n");
}
#endif
}
string cpu_model(void)
{
ifstream file;
file.open("/proc/cpuinfo", ios::in);
if (!file)
return "";
while (file) {
char line[4096];
file.getline(line, 4096);
if (strstr(line, "model name")) {
char *c;
c = strchr(line, ':');
if (c) {
file.close();
c++;
return c;
}
}
}
file.close();
return "";
}
static void system_info(void)
{
string str, str2, str3;
if ((!reportout.csv_report)&&(!reportout.http_report))
return;
if (reporttype) {
fprintf(reportout.http_report, "<h1>PowerTOP Report</h1>\n");
fprintf(reportout.http_report, "<h2>System Information</h2>\n");
fprintf(reportout.http_report, "<table width=100%%>\n");
fprintf(reportout.http_report, "<tr class=\"system_even\"><td width=20%%>PowerTOP Version</td><td>%s</td></tr>\n", POWERTOP_VERSION);
} else {
fprintf(reportout.csv_report, "***PowerTOP Report***, \n");
fprintf(reportout.csv_report, "**System Information**, \n");
fprintf(reportout.csv_report, "\n");
fprintf(reportout.csv_report, "PowerTOP Version:, \"%s\", \n", POWERTOP_VERSION);
}
str = read_sysfs_string("/proc/version");
if (reporttype)
fprintf(reportout.http_report,"<tr class=\"system_odd\"><td>Kernel Version</td><td>%s</td></tr>\n",
str.c_str());
else
fprintf(reportout.csv_report, "Kernel Version:, \"%s\", \n",
str.c_str());
str = read_sysfs_string("/sys/devices/virtual/dmi/id/board_vendor");
str2 = read_sysfs_string("/sys/devices/virtual/dmi/id/board_name");
str3 = read_sysfs_string("/sys/devices/virtual/dmi/id/product_version");
if (reporttype)
fprintf(reportout.http_report, "<tr class=\"system_even\"><td>System Name</td><td>%s %s %s</td></tr>\n",
str.c_str(), str2.c_str(), str3.c_str());
else
fprintf(reportout.csv_report,"System Name:,\"%s %s %s,\" \n",
str.c_str(), str2.c_str(), str3.c_str());
str = cpu_model();
if (reporttype)
fprintf(reportout.http_report, "<tr class=\"system_odd\"><td>CPU Information</td><td>%lix %s</td></tr>\n",
sysconf(_SC_NPROCESSORS_ONLN), str.c_str());
else
fprintf(reportout.csv_report,"CPU Information:, %lix \"%s\", \n",
sysconf(_SC_NPROCESSORS_ONLN), str.c_str());
str = read_sysfs_string("/etc/system-release");
if (str.length() < 1)
str = read_sysfs_string("/etc/redhat-release");
if (reporttype) {
fprintf(reportout.http_report, "<tr class=\"system_even\"><td>OS Information</td><td>%s</td></tr>\n",
str.c_str());
fprintf(reportout.http_report,"</table>\n");
} else {
fprintf(reportout.csv_report,"OS Information:,\"%s\", \n",
str.c_str());
fprintf(reportout.csv_report,"\n");
}
}
void init_report_output(char *filename_str)
{
size_t period;
char file_prefix[256];
char file_postfix[8];
string mystring = string(filename_str);
sprintf(file_postfix, "%s", reporttype ? "html":"csv");
period=mystring.find_last_of(".");
sprintf(file_prefix, "%s",mystring.substr(0,period).c_str());
if (reporttype) {
sprintf(reportout.filename, "%s.%s", file_prefix, file_postfix);
reportout.http_report = fopen(reportout.filename, "wm");
if (!reportout.http_report) {
fprintf(stderr, "Cannot open output file %s (%s)\n", reportout.filename, strerror(errno));
}
}else {
time_t stamp;
char datestr[200];
memset(&datestr, 0, 200);
memset(&stamp, 0, sizeof(time_t));
stamp=time(NULL);
strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M%S", localtime(&stamp));
sprintf(reportout.filename, "%s-%s.%s", file_prefix, datestr,file_postfix);
reportout.csv_report = fopen(reportout.filename, "wm");
if (!reportout.csv_report) {
fprintf(stderr, "Cannot open output file %s (%s)\n", reportout.filename, strerror(errno));
}
}
http_header_output();
system_info();
}
void http_header_output(void) {
if (!reportout.http_report)
return;
fprintf(reportout.http_report, "<!DOCTYPE html PUBLIC \"-//W3C/DTD HTML 4.01//EN\">\n");
fprintf(reportout.http_report, "<html>\n\n <head>\n");
css_header();
fprintf(reportout.http_report, "</head>\n\n <body>\n");
}
void finish_report_output(void)
{
printf("PowerTOP outputing using base filename %s\n", reportout.filename);
if (reportout.http_report){
fprintf(reportout.http_report, "</body>\n\n </html>\n");
fflush(reportout.http_report);
fdatasync(fileno(reportout.http_report));
fclose(reportout.http_report);
}
if (reportout.csv_report) {
fflush(reportout.csv_report);
fdatasync(fileno(reportout.csv_report));
fclose(reportout.csv_report);
}
}