-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataStatistics.cs
350 lines (301 loc) · 11.7 KB
/
DataStatistics.cs
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
/*
* Copyright (C) 2011, Mobile and Internet Systems Laboratory.
* Department of Computer Science, Wayne State University.
* All rights reserved.
*
* Authors: Hui Chen (huichen@wayne.edu), Youhuizi Li (huizi@wayne.edu)
*
* This program 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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.
*/
using System;
using System.Management;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ptopw
{
class DataStatistics
{
bool hasWNIC; //wheter the system exists wireless NIC
int wnicIndex; //the index of the wireless network interface
int SaveResult; //0 # 1:save the collected data into files , 0 : do not save the result info files
string FileName; //ResultLog.txt #file name that save the result, only useful when SaveResult = 1
int StatisticLevel; //# 1. only stat and show component level energy result; 2. stat and show process level energy result.
SystemInfo sysInfo; //used to save temporal system info
StatData tempData;
StreamWriter log;
FileStream fileStream;
Hashtable counterTable = new Hashtable();
ManagementObject win32ProcessorObj;
Processor processor = new Processor();
bool debug = false;
[DllImport("mydll.dll", EntryPoint = "GetNetInfo", CallingConvention = CallingConvention.Cdecl)]
public static extern bool GetNetInfo(ref WNICInfo info);
public DataStatistics(Hashtable param)
{
SaveResult = PublicFuns.str2Int32((String)param["SaveResult"], 1);
FileName = (String)param["FileName"];
StatisticLevel = PublicFuns.str2Int32((String)param["StatisticLevel"], 0);
wnicIndex = PublicFuns.str2Int32((String)param["WNICIndex"], 0);
tempData = new StatData(param);
hasWNIC = false;
sysInfo = new SystemInfo();
win32ProcessorObj = RetrieveManagementObject("Win32_Processor");
processor.update(win32ProcessorObj);
sysInfo.pro = processor;
try
{
getSystemInfo();
createSystemCounters();
initLog();
}
catch (MyException ex)
{
throw ex;
}
}
private void initLog()
{
try
{
fileStream = new FileStream(FileName, FileMode.Create);
log = new StreamWriter(fileStream);
}
catch (System.Exception ex)
{
throw new MyException("Log file is in use.", MyException.MSG_INIT_LOG_INUSE_ERROR);
}
}
private ManagementObject RetrieveManagementObject(String className)
{
ManagementClass proClass = new ManagementClass(className);
ManagementObjectCollection classObjects = proClass.GetInstances();
//TODO consider multiple devices such as processor, disk
foreach (ManagementObject item in classObjects)
{
return item;
}
return null;
}
public void stat()
{
ManagementObject diskObj = RetrieveManagementObject("Win32_PerfFormattedData_PerfDisk_PhysicalDisk");
//read system info
sysInfo.percentDiskIdleTime = (UInt64)diskObj["PercentIdleTime"];
sysInfo.percentDiskReadTime = (UInt64)diskObj["PercentDiskReadTime"];
sysInfo.percentDiskWriteTime = (UInt64)diskObj["PercentDiskWriteTime"];
//network
sysInfo.nrecv = 0;
sysInfo.nsend = 0;
if (hasWNIC)
{
WNICInfo info = new WNICInfo();
info.index = wnicIndex;
bool res = GetNetInfo(ref info);
if(res)
{
sysInfo.nrecv = info.In - sysInfo.winfo.In;
sysInfo.nsend = info.Out - sysInfo.winfo.Out;
sysInfo.winfo = info;
}
}
//memory
ManagementObject memObj = RetrieveManagementObject("Win32_PerfFormattedData_PerfOS_Memory");
ManagementObject cacheObj = RetrieveManagementObject("Win32_PerfFormattedData_PerfOS_Cache");
sysInfo.memRead = (UInt32)memObj["PagesOutputPerSec"];//pages/sec
sysInfo.memWrite = (UInt32)memObj["PagesInputPerSec"];
sysInfo.memCopy = (UInt32)cacheObj["CopyReadsPerSec"];//pages/sec
//processor
ManagementObject cpuObj = RetrieveManagementObject("Win32_Processor");
sysInfo.cpuFrequency = (UInt32)cpuObj["CurrentClockSpeed"];
sysInfo.cpuTime = 0;
sysInfo.voltage = (UInt16)cpuObj["CurrentVoltage"];
//disk
sysInfo.diskRead = 0;
sysInfo.diskWrite = 0;
//save
tempData.beginRecord();
tempData.addSystemInfo(sysInfo);//before add process info, important
//read process info
Process[] processes = Process.GetProcesses();
HashSet<UInt32> processNetStatus = NetInfoWrapper.netActiveProcesses();
sysInfo.NetActiveProcessNum = processNetStatus.Count;
foreach (Process p in processes)
{
int id = 0;
try
{
String name = p.ProcessName;
id = p.Id;
ProcessInfo pinfo = statProcessInfo(p);
if (pinfo != null)
{
if (processNetStatus.Contains((UInt32)p.Id))
pinfo.networkActive = true;
tempData.addProcessInfo(pinfo);
sysInfo.cpuTime += pinfo.cpuTime;
sysInfo.diskRead += pinfo.dread;
sysInfo.diskWrite += pinfo.dwrite;
}
}
catch (Exception ex)//process exited
{
if (counterTable.ContainsKey(id))
{
counterTable.Remove(id);
}
Console.WriteLine(ex.StackTrace);
}
}
//update time
double time = new TimeSpan(DateTime.Now.Ticks).TotalMilliseconds;
sysInfo.timeInterval = time - sysInfo.time;
sysInfo.time = time;
tempData.endRecord();
//log if needed
if (SaveResult == 1) LogResult();
}
private ProcessInfo statProcessInfo(Process p)
{
ProcessCounters counters;
if (counterTable.ContainsKey(p.Id))//old process
{
counters = (ProcessCounters)counterTable[p.Id];
if(p.HasExited)
{
return null;
}
}
else//new process
{
counters = new ProcessCounters(p.ProcessName);
counterTable.Add(p.Id, counters);
}
ProcessInfo info = counters.sample(p);
return info;
}
void createSystemCounters()
{
try
{
//network
PerformanceCounterCategory niccat = new PerformanceCounterCategory("Network Interface");
String[] instanceNames = niccat.GetInstanceNames();
for (int i = 0; i < instanceNames.Length; i++)
{
if (instanceNames[i].IndexOf("Wireless") > 0)
{
hasWNIC = true;
break;
}
}
}
catch (System.Exception ex)
{
throw new MyException(ex.Message, MyException.MSG_INIT_STAT_COUNTER_ERROR);
}
}
public DataNode GetDisplayData()
{
DataNode dn = tempData.getLastPowerInfo();
return dn;
}
public ArrayList GetDataList()
{
ArrayList dl = tempData.getDataInfo();
return dl;
}
public ArrayList GetRequired(long time)
{
ArrayList dl = tempData.GetDataRequired(time);
return dl;
}
void closeSystemCounters()
{
}
void getSystemInfo()
{
try
{
SYSTEM_INFO sys = new SYSTEM_INFO();
WinApi.GetSystemInfo(ref sys);
sysInfo.pageSize = ((int)sys.dwPageSize) / 1024; //kb
}
catch (System.Exception ex)
{
throw new MyException(ex.Message, MyException.MSG_INIT_SYSTEMINFO_ERROR);
}
}
void LogResult()
{
if (SaveResult != 1)
return;
switch (tempData.getDataRecordMode())
{
case 1://save energy
tempData.logEnergy(log);
break;
case 2://save raw data and energy
tempData.logEnergy(log);
tempData.logStatData(log);
break;
default:
break;
}
}
public void Log(String str)
{
if (debug)
{
log.WriteLine("log: " + str);
log.Flush();
}
}
public void SetWnicIndex(int index)
{
wnicIndex = index;
}
public int statCount()
{
return tempData.statCount();
}
}
class ProcessCounters{
public TimeSpan lastProcessorTime;
IO_COUNTERS lastio;
public ProcessCounters(String name){
lastio.init();
}
public ProcessInfo sample(Process p)
{
TimeSpan timespan = p.TotalProcessorTime;
// if (timespan.TotalMilliseconds == lastProcessorTime.TotalMilliseconds)
// return null;
ProcessInfo info = new ProcessInfo();
info.no = p.Id;
info.name = p.ProcessName;
//stat cpu info
info.cpuTime = timespan.TotalMilliseconds - lastProcessorTime.TotalMilliseconds;
lastProcessorTime = timespan;
//stat the io information
IO_COUNTERS ioc;
WinApi.GetProcessIoCounters(p.Handle, out ioc);
info.dread = ioc.ReadTransferCount - lastio.ReadTransferCount;
info.dwrite = ioc.WriteTransferCount - lastio.WriteTransferCount;
lastio = ioc;
return info;
}
}
}