-
Notifications
You must be signed in to change notification settings - Fork 6
/
win32_blandwidth.c
261 lines (216 loc) · 7.18 KB
/
win32_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
/* ========================================================================
$File: work/tools/blandwidth/win32_blandwidth.c $
$Date: 2020/06/18 00:32:25 UTC $
$Revision: 2 $
$Creator: Casey Muratori $
======================================================================== */
#include <windows.h>
#include <immintrin.h>
#include <smmintrin.h>
#include <wmmintrin.h>
#ifdef __clang__
#include <intrin.h>
#include <avxintrin.h>
#include <avx2intrin.h>
#include <avx512fintrin.h>
#endif
#include "blandwidth.h"
#define TIME_OPEN(Stamp) QueryPerformanceCounter((LARGE_INTEGER *)&(Stamp).Counter); (Stamp).Clock = __rdtsc()
#define TIME_CLOSE(Stamp) (Stamp).Clock = __rdtsc(); QueryPerformanceCounter((LARGE_INTEGER *)&(Stamp).Counter)
#include "x64_blandwidth.c"
#include "blandwidth.c"
typedef struct win32_queues
{
HANDLE Result;
HANDLE Dispatch;
} win32_queues;
typedef struct win32_context
{
context Context;
win32_queues Queues;
} win32_context;
function void
DispatchWork(context *Context, u32 OpCount, memory_operation *Ops)
{
win32_context *Win32Context = (win32_context *)Context;
for(u32 OpIndex = 0;
OpIndex < OpCount;
++OpIndex)
{
PostQueuedCompletionStatus(Win32Context->Queues.Dispatch, 0, 0, (OVERLAPPED *)(Ops + OpIndex));
}
}
function memory_operation *
ReceiveWorkResult(context *Context)
{
win32_context *Win32Context = (win32_context *)Context;
DWORD IgnoredBytes;
ULONG_PTR IgnoredKey;
OVERLAPPED *Overlapped;
GetQueuedCompletionStatus(Win32Context->Queues.Result, &IgnoredBytes, &IgnoredKey, &Overlapped, INFINITE);
memory_operation *Result = (memory_operation *)Overlapped;
return(Result);
}
function void
Statusf(char const *Format, ...)
{
char Buffer[1024];
va_list Args;
va_start(Args, Format);
u32 Length = wvsprintf(Buffer, Format, Args);
va_end(Args);
DWORD Ignored;
WriteFile(GetStdHandle(STD_ERROR_HANDLE), Buffer, Length, &Ignored, 0);
}
function void
Dataf(char const *Format, ...)
{
char Buffer[1024];
va_list Args;
va_start(Args, Format);
u32 Length = wvsprintf(Buffer, Format, Args);
va_end(Args);
DWORD Ignored;
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), Buffer, Length, &Ignored, 0);
}
function void *
AllocateAndClear(u64 Size)
{
void *Result = VirtualAlloc(0, Size, MEM_COMMIT, PAGE_READWRITE);
if(!Result)
{
Statusf("ERROR: Unable to allocate required memory.\n");
ExitProcess(1);
}
return(Result);
}
function DWORD WINAPI
ThreadEntryPoint(void *Passthrough)
{
win32_queues *Queues = (win32_queues *)Passthrough;
/* NOTE(casey): The semantic analysis in modern compilers is actively awful. I had to do while() here
because if I did for(;;), it errored out because the return(0) was unreachable, and if I got rid of
the return(0), it errored out because the function didn't return a value. I spend so much of my
time dealing with stupid stuff like this, it makes me loathe C/C++ specs and compilers, because they
are clearly not designed with effective programming in mind :( */
while(Queues)
{
DWORD IgnoredBytes;
ULONG_PTR IgnoredKey;
OVERLAPPED *Overlapped;
GetQueuedCompletionStatus(Queues->Dispatch, &IgnoredBytes, &IgnoredKey, &Overlapped, INFINITE);
memory_operation *Op = (memory_operation *)Overlapped;
do
{
TIME_OPEN(Op->StartStamp);
} while(Op->StartStamp.Counter < Op->StartGateCounter);
Op->Handler(Op);
TIME_CLOSE(Op->EndStamp);
PostQueuedCompletionStatus(Queues->Result, 0, 0, Overlapped);
}
return(0);
}
void __cdecl
mainCRTStartup(void)
{
//
// NOTE(casey): Determine base timer and CPU frequencies
//
time BaseHz;
QueryPerformanceFrequency((LARGE_INTEGER *)&BaseHz.Counter);
BaseHz.Clock = 0;
time SleepBegin;
TIME_OPEN(SleepBegin);
Sleep(1000);
time SleepEnd;
TIME_CLOSE(SleepEnd);
time Delta = Subtract(SleepEnd, SleepBegin);
BaseHz.Clock = (Delta.Clock * BaseHz.Counter) / Delta.Counter;
//
// NOTE(casey): Configure testing setup based on CPU parameters
//
SYSTEM_INFO SysInfo = {0};
GetSystemInfo(&SysInfo);
u32 MaxThreadCount = SysInfo.dwNumberOfProcessors;
int CID[4] = {0};
char CPUBrand[64] = {0};
__cpuidex((int *)(CPUBrand + 0), 0x80000002, 0);
__cpuidex((int *)(CPUBrand + 16), 0x80000003, 0);
__cpuidex((int *)(CPUBrand + 32), 0x80000004, 0);
handler_table_entry MemoryHandlers[] =
{
HANDLER_ENTRY(X64Read128), HANDLER_ENTRY(X64Write128), HANDLER_ENTRY(X64ReadWrite128),
HANDLER_ENTRY(X64Read256), HANDLER_ENTRY(X64Write256), HANDLER_ENTRY(X64ReadWrite256),
HANDLER_ENTRY(X64Read512), HANDLER_ENTRY(X64Write512), HANDLER_ENTRY(X64ReadWrite512),
};
u32 HandlerCount = 3; // NOTE(casey): We assume SSE for all x64 chips
__cpuidex(CID, 1, 0);
if((CID[2] & (1 << 28)))
{
HandlerCount = 6; // NOTE(casey): AVX is supported
__cpuidex(CID, 7, 0);
if((CID[1] & (1 << 16)))
{
HandlerCount = 9; // NOTE(casey): AVX-512 is supported
}
}
//
// NOTE(casey): Prepare threads
//
win32_queues Queues;
Queues.Dispatch = CreateIoCompletionPort(INVALID_HANDLE_VALUE, 0, 0, 0);
Queues.Result = CreateIoCompletionPort(INVALID_HANDLE_VALUE, 0, 0, 0);
DWORD *ThreadIDs = (DWORD *)AllocateAndClear(MaxThreadCount*SizeOf(DWORD));
for(u32 ThreadIndex = 0;
ThreadIndex < MaxThreadCount;
++ThreadIndex)
{
u32 StackSize = 1024*1024; // NOTE(casey): We need extremely little stack space, but we don't know how much the OS might need, so we err high
HANDLE Thread = CreateThread(0, StackSize, ThreadEntryPoint, &Queues, 0, ThreadIDs + ThreadIndex);
if(Thread == INVALID_HANDLE_VALUE)
{
Statusf("Unable to create thread");
ExitProcess(2);
}
CloseHandle(Thread);
}
//
// NOTE(casey): Run tests
//
win32_context Win32Context = {0};
Win32Context.Context.BaseHz = BaseHz;
Win32Context.Context.MaxThreadCount = MaxThreadCount;
Win32Context.Context.HandlerCount = HandlerCount;
Win32Context.Context.Handlers = MemoryHandlers;
Win32Context.Context.CPUBrand = CPUBrand;
Win32Context.Context.LogicalCoreCount = MaxThreadCount;
Win32Context.Queues = Queues;
Main(&Win32Context.Context);
//
// NOTE(casey): Exit
//
ExitProcess(0);
}
#undef function
#pragma function(memset)
void *memset(void *DestInit, int SourceInit, size_t Size)
{
unsigned char Source = *(unsigned char *)&SourceInit;
unsigned char *Dest = (unsigned char *)DestInit;
while(Size--)
{
*Dest++ = Source;
}
return(DestInit);
}
#pragma function(memcpy)
void *memcpy(void *DestInit, void const *SourceInit, size_t Size)
{
unsigned char *Source = (unsigned char *)SourceInit;
unsigned char *Dest = (unsigned char *)DestInit;
while(Size--)
{
*Dest++ = *Source++;
}
return(DestInit);
}