-
Notifications
You must be signed in to change notification settings - Fork 3
/
SimCPULog.pas
217 lines (197 loc) · 5.82 KB
/
SimCPULog.pas
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
unit SimCPULog;
{$MODE Delphi}
interface
uses
LCLIntf, LCLType, LMessages, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ExtCtrls, Globals;
type
TCPUdata = record
Phases, // number of phases played
Calls, // number of calls
Time: integer; // total time (milliseconds)
end;
TPlayerStats = record
Name: string; // name
CPUdata: array [TRoutine] of TCPUdata;
end;
TfSimCPULog = class(TForm)
Panel1: TPanel;
lstDetail: TListView;
lstSummary: TListView;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure lstSummarySelectItem(Sender: TObject; Item: TListItem;
Selected: Boolean);
private
aPStats: array of TPlayerStats;
procedure LoadLogFile;
function DecodeRoutine(uRoutine: TRoutine): string;
public
sLogFileName: string;
end;
var
fSimCPULog: TfSimCPULog;
implementation
{$R *.lfm}
uses IniFiles, StrUtils{, StdPas};
procedure TfSimCPULog.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
procedure TfSimCPULog.FormDestroy(Sender: TObject);
begin
fSimCPULog := nil;
end;
procedure TfSimCPULog.FormShow(Sender: TObject);
begin
Caption := 'CPU usage analysis: ' + sLogFileName;
LoadLogFile;
end;
procedure TfSimCPULog.LoadLogFile;
var
LogFile: TIniFile;
TRPs: TStringList;
i: integer;
uRoutine: TRoutine;
sRoutine: string;
begin
if not FileExists(sG_AppPath + sLogFileName) then begin
ShowMessage('Log file "' + sLogFileName + '" not found.');
exit;
end;
// open log file
LogFile := TIniFile.Create(sG_AppPath + sLogFileName);
TRPs := TStringList.Create;
try
with LogFile do begin
ReadSections(TRPs); // read TRP names in log file
TRPs.Sorted := true; // sort them by name
lstSummary.Clear; // clear analysis
SetLength(aPStats, TRPs.Count);
// analysis
for i := 0 to TRPs.Count - 1 do begin
with lstSummary.Items.Add do begin
aPStats[i].Name := TRPs[i];
Caption := TRPs[i];
for uRoutine := rtAssignment to rtFortification do begin
sRoutine := IntToStr(ord(uRoutine));
aPStats[i].CPUdata[uRoutine].Phases := ReadInteger(TRPs[i],
'Phases' + sRoutine, 0);
aPStats[i].CPUdata[uRoutine].Calls := ReadInteger(TRPs[i],
'Calls' + sRoutine, 0);
aPStats[i].CPUdata[uRoutine].Time := ReadInteger(TRPs[i],
'Time' + sRoutine, 0);
if aPStats[i].CPUdata[uRoutine].Phases > 0 then
SubItems.Add(FormatFloat('0.0',
aPStats[i].CPUdata[uRoutine].Time / aPStats[i].CPUdata
[uRoutine].Phases))
else
SubItems.Add('-');
end;
end;
end;
// legenda
(* txtCPULog.Lines.Add('');
txtCPULog.Lines.Add(
'Phases = number of times the TRP has entered a phase (attack, occupation...)');
txtCPULog.Lines.Add(
'Calls = number of times the TRP code has been called during the phase');
txtCPULog.Lines.Add('T/Ph = average time per phase (milliseconds)');
txtCPULog.Lines.Add('C/Ph = average number of calls per phase');
txtCPULog.Lines.Add('T/C = average time per call (milliseconds)');
*)
end;
finally
LogFile.Free;
TRPs.Free;
end;
end;
procedure TfSimCPULog.lstSummarySelectItem(Sender: TObject; Item: TListItem;
Selected: Boolean);
var
iP: integer;
uRoutine: TRoutine;
begin
lstDetail.Clear;
if lstSummary.ItemIndex < 0 then begin
lstDetail.Columns[0].Caption := '';
exit;
end;
// populate player's list
iP := lstSummary.ItemIndex;
lstDetail.Columns[0].Caption := aPStats[iP].Name;
with lstDetail.Items.Add do begin
Caption := 'Phases';
for uRoutine := rtAssignment to rtFortification do begin
with aPStats[iP].CPUdata[uRoutine] do begin
SubItems.Add(FormatFloat(',0', Phases));
end;
end;
end;
with lstDetail.Items.Add do begin
Caption := 'Calls';
for uRoutine := rtAssignment to rtFortification do begin
with aPStats[iP].CPUdata[uRoutine] do begin
SubItems.Add(FormatFloat(',0', Calls));
end;
end;
end;
with lstDetail.Items.Add do begin
Caption := 'Time (ms)';
for uRoutine := rtAssignment to rtFortification do begin
with aPStats[iP].CPUdata[uRoutine] do begin
SubItems.Add(FormatFloat(',0', Time));
end;
end;
end;
with lstDetail.Items.Add do begin
Caption := 'Time/Phase (ms)';
for uRoutine := rtAssignment to rtFortification do begin
with aPStats[iP].CPUdata[uRoutine] do begin
if Phases > 0 then
SubItems.Add(FormatFloat('0.0', Time / Phases))
else
SubItems.Add('-');
end;
end;
end;
with lstDetail.Items.Add do begin
Caption := 'Time/Call (ms)';
for uRoutine := rtAssignment to rtFortification do begin
with aPStats[iP].CPUdata[uRoutine] do begin
if Calls > 0 then
SubItems.Add(FormatFloat('0.0', Time / Calls))
else
SubItems.Add('-');
end;
end;
end;
with lstDetail.Items.Add do begin
Caption := 'Calls/Phase';
for uRoutine := rtAssignment to rtFortification do begin
with aPStats[iP].CPUdata[uRoutine] do begin
if Phases > 0 then
SubItems.Add(FormatFloat('0.0', Calls / Phases))
else
SubItems.Add('-');
end;
end;
end;
end;
function TfSimCPULog.DecodeRoutine(uRoutine: TRoutine): string;
begin
case uRoutine of
rtAssignment:
result := 'Assignment';
rtPlacement:
result := 'Placement';
rtAttack:
result := 'Attack';
rtOccupation:
result := 'Occupation';
rtFortification:
result := 'Fortification';
end;
end;
end.