-
Notifications
You must be signed in to change notification settings - Fork 6
/
blandwidth.c
369 lines (307 loc) · 11.9 KB
/
blandwidth.c
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* ========================================================================
$File: work/tools/blandwidth/blandwidth.c $
$Date: 2020/06/17 05:05:33 UTC $
$Revision: 5 $
$Creator: Casey Muratori $
======================================================================== */
function u64
Minimum(u64 A, u64 B)
{
u64 Result = (A < B) ? A : B;
return(Result);
}
function u64
Maximum(u64 A, u64 B)
{
u64 Result = (A > B) ? A : B;
return(Result);
}
function u64
RoundedDiv(u64 A, u64 B)
{
u64 Result = A;
if(B)
{
Result = (A + B/2) / B;
}
return(Result);
}
function void
NoOp(memory_operation *Op)
{
(void)(Op);
}
function void *
AllocateAndFill(u64 Size)
{
u8 *Result = (u8 *)AllocateAndClear(Size);
for(u64 ValueIndex = 0;
ValueIndex < Size;
++ValueIndex)
{
Result[ValueIndex] = (255 - (u8)ValueIndex);
}
return(Result);
}
function time
Subtract(time A, time B)
{
time Result;
Result.Clock = A.Clock - B.Clock;
Result.Counter = A.Counter - B.Counter;
return(Result);
}
function time
Average(time_stat A)
{
time Result = A.Sum;
if(A.Count)
{
Result.Clock /= A.Count;
Result.Counter /= A.Count;
}
return(Result);
}
function void
Include(time_stat *Stat, time T)
{
if(Stat->Count)
{
Stat->Min.Clock = Minimum(Stat->Min.Clock, T.Clock);
Stat->Min.Counter = Minimum(Stat->Min.Counter, T.Counter);
Stat->Max.Clock = Maximum(Stat->Max.Clock, T.Clock);
Stat->Max.Counter = Maximum(Stat->Max.Counter, T.Counter);
}
else
{
Stat->Min = T;
Stat->Max = T;
}
Stat->Sum.Clock += T.Clock;
Stat->Sum.Counter += T.Counter;
++Stat->Count;
}
function u64
GetNanoseconds(time BaseHz, u64 A)
{
u64 NSPerS = 1000ULL * 1000 * 1000;
u64 Result = (NSPerS*A)/BaseHz.Counter;
return(Result);
}
function u64
GetBandwidthAs(time BaseHz, memory_test_results *Res, u64 Unit)
{
u64 Hz = BaseHz.Counter;
u64 Measure = Res->Total.Min.Counter;
u64 BytesPerSecond = 0;
if(Measure)
{
BytesPerSecond = (Res->TotalSize * Hz) / Measure;
}
u64 Result = RoundedDiv(BytesPerSecond, Unit);
return(Result);
}
function u64
GetBandwidth(time BaseHz, memory_test_results *Res)
{
u64 Result = GetBandwidthAs(BaseHz, Res, 1);
return(Result);
}
function void
TimeOperation(context *Context, u32 OpCount, memory_operation *Operations, time_stat *ThreadStat, time_stat *TotalStat)
{
u64 MaxCyclesToSpend = (1ULL*1000*1000*1000);
u64 CyclesSpentOnNewMin = 0;
while(CyclesSpentOnNewMin < MaxCyclesToSpend)
{
time Now;
TIME_OPEN(Now);
u64 StartGate = (Now.Counter + Context->BaseHz.Counter)/1000;
for(u32 OpIndex = 0;
OpIndex < OpCount;
++OpIndex)
{
Operations[OpIndex].StartGateCounter = StartGate;
}
DispatchWork(Context, OpCount, Operations);
time_stat ThisRun = {0};
for(u32 ThreadIndex = 0;
ThreadIndex < OpCount;
++ThreadIndex)
{
memory_operation *ResultOp = ReceiveWorkResult(Context);
time ThreadTime = Subtract(ResultOp->EndStamp, ResultOp->StartStamp);
Include(ThreadStat, ThreadTime);
Include(&ThisRun, ResultOp->EndStamp);
Include(&ThisRun, ResultOp->StartStamp);
}
time TotalTime = Subtract(ThisRun.Max, ThisRun.Min);
CyclesSpentOnNewMin += TotalTime.Clock;
time PrevMin = TotalStat->Min;
Include(TotalStat, TotalTime);
if((TotalStat->Min.Clock != PrevMin.Clock) ||
(TotalStat->Min.Counter != PrevMin.Counter))
{
// NOTE(casey): Every time we see a new minimum clock or counter, restart the testing
CyclesSpentOnNewMin = 0;
}
}
}
function void
Main(context *Context)
{
u64 Megabyte = 1024*1024;
u64 Gigabyte = 1024*Megabyte;
u64 Million = 1000*1000;
//
// NOTE(casey): Print the informational header
//
Statusf("\n");
Statusf("========================================================================\n");
Statusf("BlandWidth " VERSION_STRING " - A compact bandwidth tester for x64 CPUs\n");
Statusf("by Casey Muratori circa 2020\n");
Statusf("========================================================================\n");
Statusf("\n");
Statusf("WARNING: USE AT YOUR OWN RISK. Numbers reported by this utility reflect\n");
Statusf("the interplay of cores, clocking, hyperthreading, buffers, caches, and\n");
Statusf("memory. Considerable expertise is required to interpret them properly.\n");
Statusf("\n");
Statusf("CPU: %s\n", Context->CPUBrand);
Statusf("Logical Cores: %u\n", Context->LogicalCoreCount);
Statusf("Expected frequency: %Iumhz\n", RoundedDiv(Context->BaseHz.Clock, Million));
Statusf("Support: ");
for(u32 HandlerIndex = 0;
HandlerIndex < Context->HandlerCount;
++HandlerIndex)
{
Statusf(" %s", Context->Handlers[HandlerIndex].Name);
}
Statusf("\n\n");
//
// NOTE(casey): Prepare test buffers
//
memory_operation *Operations = (memory_operation *)AllocateAndClear(Context->MaxThreadCount*SizeOf(memory_operation));
u64 BankSize = 4ULL*Gigabyte;
u8 *Bank[2];
Bank[0] = (u8 *)AllocateAndFill(BankSize);
Bank[1] = (u8 *)AllocateAndFill(BankSize);
u64 BankPerThread = BankSize / Context->MaxThreadCount;
u32 ValuesSize = Context->MaxThreadCount*BLOCK_SIZE;
u8 *ValuesBuffer = (u8 *)AllocateAndFill(ValuesSize);
//
// NOTE(casey): Run tests
//
u32 SizeOffset = 14;
u32 SizeCount = 14;
u32 MinThreadCount = 1;
u32 ResultsPerTest = Context->HandlerCount*(Context->MaxThreadCount - MinThreadCount + 1);
u32 ResultCount = SizeCount*ResultsPerTest;
memory_test_results *TestResults = (memory_test_results *)AllocateAndClear(ResultCount*SizeOf(memory_test_results));
u32 SourceBankIndex = 0;
u32 DestBankIndex = 0;
u32 ResultIndex = 0;
for(u32 HandlerIndex = 0;
HandlerIndex < Context->HandlerCount;
++HandlerIndex)
{
for(u32 SizeIndex = 0;
SizeIndex < SizeCount;
++SizeIndex)
{
u64 TotalRegionSize = (1ULL << (SizeOffset + SizeIndex));
u64 TotalRegionMask = (TotalRegionSize - 1);
u32 FirstResultIndex = ResultIndex;
memory_test_results *Fastest = TestResults + FirstResultIndex;
memory_test_results *Slowest = TestResults + FirstResultIndex;
for(u32 ThreadCount = MinThreadCount;
ThreadCount <= Context->MaxThreadCount;
++ThreadCount)
{
memory_test_results *Results = TestResults + ResultIndex++;
Results->ThreadCount = ThreadCount;
Results->HandlerIndex = HandlerIndex;
char *PrintSizeTable[] = {"b", "kb", "mb", "gb", 0};
u32 PrintSizePower = 0;
u64 PrintSize = TotalRegionSize;
while(PrintSizeTable[PrintSizePower + 1] && (PrintSize > 1024))
{
PrintSize /= 1024;
++PrintSizePower;
}
wsprintf(Results->Name, "%s %Iu%s/%ut", Context->Handlers[HandlerIndex].Name, PrintSize, PrintSizeTable[PrintSizePower], ThreadCount);
Results->TotalSize = 0;
for(u32 ThreadIndex = 0;
ThreadIndex < ThreadCount;
++ThreadIndex)
{
memory_operation *ThreadOp = Operations + ThreadIndex;
ThreadOp->Pattern.Source = Bank[SourceBankIndex] + ThreadIndex*BankPerThread;
ThreadOp->Pattern.Dest = Bank[DestBankIndex] + ThreadIndex*BankPerThread;
ThreadOp->Pattern.SourceStride = BLOCK_SIZE;
ThreadOp->Pattern.SourceMask = TotalRegionMask;
ThreadOp->Pattern.DestStride = BLOCK_SIZE;
ThreadOp->Pattern.DestMask = TotalRegionMask;
ThreadOp->Count = (4*TotalRegionSize) / BLOCK_SIZE;
if(ThreadOp->Count < Million)
{
ThreadOp->Count = Million;
}
ThreadOp->Values = ValuesBuffer + ThreadIndex*BLOCK_SIZE;
Results->TotalSize += ThreadOp->Count*BLOCK_SIZE;
ThreadOp->Handler = Context->Handlers[HandlerIndex].Function;
}
TimeOperation(Context, ThreadCount, Operations, &Results->Thread, &Results->Total);
if(GetBandwidth(Context->BaseHz, Fastest) < GetBandwidth(Context->BaseHz, Results))
{
Fastest = Results;
}
if(GetBandwidth(Context->BaseHz, Slowest) > GetBandwidth(Context->BaseHz, Results))
{
Slowest = Results;
}
Statusf("\r%s %IuGB/s (best: %IuGB/s) ",
Results->Name,
GetBandwidthAs(Context->BaseHz, Results, Gigabyte),
GetBandwidthAs(Context->BaseHz, Fastest, Gigabyte));
}
Statusf("\rBest: %s %IuGB/s (slowest: %ut - %IuGB/s)\n",
Fastest->Name, GetBandwidthAs(Context->BaseHz, Fastest, Gigabyte),
Slowest->ThreadCount, GetBandwidthAs(Context->BaseHz, Slowest, Gigabyte));
}
}
Statusf("\n\n");
Dataf("Test,Estimated MB/s,Threads,Handler Index,Size,Min Total ns,Max Total ns,Avg Total ns,Min Total Clocks,Max Total Clocks,Avg Total Clocks,Min Thread ns,Max Thread ns,Avg Thread ns,Min Thread Clocks,Max Thread Clocks,Avg Thread Clocks\n");
for(ResultIndex = 0;
ResultIndex < ResultCount;
++ResultIndex)
{
memory_test_results *Result = TestResults + ResultIndex;
if(Result->Total.Count)
{
time TotalAvg = Average(Result->Total);
time ThreadAvg = Average(Result->Thread);
u64 MinTotalNS = GetNanoseconds(Context->BaseHz, Result->Total.Min.Counter);
u64 MaxTotalNS = GetNanoseconds(Context->BaseHz, Result->Total.Max.Counter);
u64 AvgTotalNS = GetNanoseconds(Context->BaseHz, TotalAvg.Counter);
u64 MinTotalClocks = Result->Total.Min.Clock;
u64 MaxTotalClocks = Result->Total.Max.Clock;
u64 AvgTotalClocks = TotalAvg.Clock;
u64 MinThreadNS = GetNanoseconds(Context->BaseHz, Result->Thread.Min.Counter);
u64 MaxThreadNS = GetNanoseconds(Context->BaseHz, Result->Thread.Max.Counter);
u64 AvgThreadNS = GetNanoseconds(Context->BaseHz, ThreadAvg.Counter);
u64 MinThreadClocks = Result->Thread.Min.Clock;
u64 MaxThreadClocks = Result->Thread.Max.Clock;
u64 AvgThreadClocks = ThreadAvg.Clock;
Dataf("%s,%Iu,%u,%u,%Iu,%Iu,%Iu,%Iu,%Iu,%Iu,%Iu,%Iu,%Iu,%Iu,%Iu,%Iu,%Iu\n",
Result->Name,
GetBandwidthAs(Context->BaseHz, Result, Megabyte),
Result->ThreadCount,
Result->HandlerIndex,
Result->TotalSize,
MinTotalNS, MaxTotalNS, AvgTotalNS,
MinTotalClocks, MaxTotalClocks, AvgTotalClocks,
MinThreadNS, MaxThreadNS, AvgThreadNS,
MinThreadClocks, MaxThreadClocks, AvgThreadClocks);
}
}
}