forked from microsoft/perfview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIProcess.cs
More file actions
294 lines (269 loc) · 9.8 KB
/
IProcess.cs
File metadata and controls
294 lines (269 loc) · 9.8 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
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
using System;
namespace PerfView
{
public interface IProcess : IComparable<IProcess>
{
string Name { get; }
string CommandLine { get; }
DateTime StartTime { get; }
DateTime EndTime { get; }
string Duration { get; }
int ProcessID { get; }
int ParentID { get; }
double CPUTimeMSec { get; }
}
internal class IProcessForProcessInfo : IProcess
{
public IProcessForProcessInfo(ProcessInfo process) { Process = process; }
public ProcessInfo Process { get; private set; }
public int ProcessID { get { return Process.ProcessID; } }
public int ParentID { get { if (Process.Parent == null) { return 0; } return Process.ParentProcessID; } }
public string Name { get { return new string(' ', ParentDepth(Process)) + Process.Name; } }
public string CommandLine { get { return Process.CommandLine; } }
public DateTime StartTime { get { return Process.CreationDate; } }
public DateTime EndTime { get { return DateTime.Now; } }
public string Duration
{
get
{
double duration = (DateTime.Now - Process.CreationDate).TotalSeconds;
if (duration < 60)
{
return duration.ToString("f2") + " sec";
}
duration /= 60;
if (duration < 60)
{
return duration.ToString("f2") + " min";
}
duration /= 60;
if (duration < 60)
{
return duration.ToString("f2") + " hr";
}
duration /= 24;
if (duration < 365)
{
return duration.ToString("f2") + " days";
}
duration /= 365;
return duration.ToString("f2") + " yr";
}
}
public double CPUTimeMSec { get { return Process.CpuTime100ns / 10000.0; } }
public int CompareTo(IProcess other)
{
var traceProcess1 = Process;
var traceProcess2 = ((IProcessForProcessInfo)other).Process;
return Compare(traceProcess1, ParentDepth(traceProcess1), traceProcess2, ParentDepth(traceProcess2));
}
public override string ToString() { return Process.ToString(); }
#region private
private static int Compare(ProcessInfo process1, int depth1, ProcessInfo process2, int depth2)
{
if (process1 == process2)
{
return 0;
}
int ret;
if (depth1 > depth2)
{
ret = Compare(process1.Parent, depth1 - 1, process2, depth2);
if (ret == 0)
{
ret = 1;
}
return ret;
}
if (depth2 > depth1)
{
ret = Compare(process1, depth1, process2.Parent, depth2 - 1);
if (ret == 0)
{
ret = -1;
}
return ret;
}
if (depth1 > 0)
{
ret = Compare(process1.Parent, depth1 - 1, process2.Parent, depth2 - 1);
if (ret != 0)
{
return ret;
}
}
// If parents are the same, we sort by time. youngest first
ret = -process1.CreationDate.CompareTo(process2.CreationDate);
if (ret != 0)
{
return ret;
}
// If times are the same, sort by process ID (decending)
return -process1.ProcessID.CompareTo(process2.ProcessID);
}
/// <summary>
/// Find the depth of a process (0 means I have no parent).
/// </summary>
private static int ParentDepth(ProcessInfo process)
{
int ret = 0;
while (process.Parent != null && process.ProcessID != 0)
{
process = process.Parent;
ret++;
if (ret > 1000) // Trivial loop prevention. TODO do better.
{
return 0;
}
}
return ret;
}
#endregion
}
internal class IProcessForStackSource : IProcess
{
internal IProcessForStackSource(string name) { Name = name; StartTime = DateTime.MaxValue; CommandLine = ""; }
public string Name { get; private set; }
public DateTime StartTime { get; internal set; }
public DateTime EndTime { get; internal set; }
public string CommandLine { get; internal set; }
public string Duration
{
get
{
double duration = (EndTime - StartTime).TotalSeconds;
if (duration < 60)
{
return duration.ToString("f2") + " sec";
}
duration /= 60;
if (duration < 60)
{
return duration.ToString("f2") + " min";
}
duration /= 60;
if (duration < 60)
{
return duration.ToString("f2") + " hr";
}
duration /= 24;
if (duration < 365)
{
return duration.ToString("f2") + " days";
}
duration /= 365;
return duration.ToString("f2") + " yr";
}
}
public int ProcessID { get; internal set; }
public int ParentID { get; internal set; }
public double CPUTimeMSec { get; internal set; }
public int CompareTo(IProcess other)
{
// Choose largest CPU time first.
var ret = -CPUTimeMSec.CompareTo(other.CPUTimeMSec);
if (ret != 0)
{
return ret;
}
// Otherwise go by date (reversed)
return -StartTime.CompareTo(other.StartTime);
}
public override string ToString()
{
return "<Process Name=\"" + Name +
"\" CPUTimeMSec=\"" + CPUTimeMSec +
"\" Duration=\"" + Duration +
"\">";
}
}
#if false
// TODO use or delete
class IProcessForTraceProcess : IProcess
{
public IProcessForTraceProcess(TraceProcess process) { Process = process; }
public TraceProcess Process { get; private set; }
public int ProcessID { get { return Process.ProcessID; } }
public int ParentID { get { if (Process.Parent == null) return 0; return Process.ParentID; } }
public string Name { get { return new string(' ', ParentDepth(Process)) + Process.Name; } }
public string CommandLine { get { return Process.CommandLine; } }
public DateTime StartTime { get { return Process.StartTime; } }
public DateTime EndTime { get { return Process.EndTime; } }
public string Duration
{
get
{
double duration = (Process.EndTime - Process.StartTime).TotalSeconds;
if (duration < 60)
return duration.ToString("f2") + " sec";
duration /= 60;
if (duration < 60)
return duration.ToString("f2") + " min";
duration /= 60;
if (duration < 60)
return duration.ToString("f2") + " hr";
duration /= 24;
if (duration < 365)
return duration.ToString("f2") + " days";
duration /= 365;
return duration.ToString("f2") + " yr";
}
}
public double CPUTimeMSec { get { return 0; } } // TODO FIX NOW do better
public int CompareTo(IProcess other)
{
var traceProcess1 = Process;
var traceProcess2 = ((IProcessForTraceProcess)other).Process;
return Compare(traceProcess1, ParentDepth(traceProcess1), traceProcess2, ParentDepth(traceProcess2));
}
public override string ToString() { return Process.ToString(); }
#region private
private static int Compare(TraceProcess process1, int depth1, TraceProcess process2, int depth2)
{
if (process1 == process2)
return 0;
int ret;
if (depth1 > depth2)
{
ret = Compare(process1.Parent, depth1 - 1, process2, depth2);
if (ret == 0)
ret = 1;
return ret;
}
if (depth2 > depth1)
{
ret = Compare(process1, depth1, process2.Parent, depth2 - 1);
if (ret == 0)
ret = -1;
return ret;
}
if (depth1 > 0)
{
ret = Compare(process1.Parent, depth1 - 1, process2.Parent, depth2 - 1);
if (ret != 0)
return ret;
}
// If parents are the same, we sort by time. youngest first
ret = -process1.StartTime100ns.CompareTo(process2.StartTime100ns);
if (ret != 0)
return ret;
// If times are the same, sort by process ID (decending)
return -process1.ProcessID.CompareTo(process2.ProcessID);
}
/// <summary>
/// Find the depth of a process (0 means I have no parent).
/// </summary>
private static int ParentDepth(TraceProcess process)
{
int ret = 0;
while (process.Parent != null && process.ProcessID != 0)
{
process = process.Parent;
ret++;
}
return ret;
}
#endregion
}
#endif
}