Skip to content

Commit

Permalink
add --rio flag to use winsock registered i/o extension
Browse files Browse the repository at this point in the history
  • Loading branch information
mmozeiko committed Sep 26, 2021
1 parent aeed2b9 commit 06a6eba
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ un-REM the clang++ line to build with clang if you have it installed.
Option | Description
------------ | -------------
--large | Use 2mb pages (default: 4k pages)
--rio | register allocated memory with [RIORegisterBuffer][] function
[number] | Set total allocation size in mb (default: 1024)

### Usage examples:
Expand All @@ -42,3 +43,5 @@ In this simple benchmark, 2mb pages outperform 4k pages dramatically - by number
Alas, no such flag exists, so this is purely hypothetical at the moment.

\- [Casey](https://caseymuratori.com)

[RIORegisterBuffer]: https://docs.microsoft.com/en-us/windows/win32/api/mswsock/nc-mswsock-lpfn_rioregisterbuffer
4 changes: 2 additions & 2 deletions build.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@echo off
call cl -nologo -O2 largepages.cpp advapi32.lib
REM call clang++ -O2 -fuse-ld=lld -Wl,-subsystem:console,advapi32.lib largepages.cpp -o largepages.exe
call cl -nologo -O2 largepages.cpp advapi32.lib ws2_32.lib
REM call clang++ -O2 -fuse-ld=lld -Wl,-subsystem:console,advapi32.lib,ws2_32.lib largepages.cpp -o largepages.exe
25 changes: 25 additions & 0 deletions largepages.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <winsock2.h>
#include <mswsock.h>
#include <windows.h>
#include <stdio.h>
#include <immintrin.h>
Expand Down Expand Up @@ -52,6 +54,7 @@ int main(int ArgCount, char **Args)
int Result = 0;

int TryLargePages = false;
int TryRio = false;

SIZE_T Kilobyte = 1024;
SIZE_T Megabyte = 1024*1024;
Expand All @@ -70,6 +73,10 @@ int main(int ArgCount, char **Args)
{
TryLargePages = true;
}
else if(strcmp(Arg, "--rio") == 0)
{
TryRio = true;
}
else
{
int IsNum = true;
Expand Down Expand Up @@ -106,13 +113,31 @@ int main(int ArgCount, char **Args)
//

SIZE_T AllocSize = PageSize * ((TotalSize + PageSize - 1) / PageSize);

RIO_EXTENSION_FUNCTION_TABLE Rio = {};
if (TryRio)
{
// NOTE(mmozeiko): need to get function pointers to RIO functions, and that requires dummy socket
WSADATA WinSockData;
WSAStartup(MAKEWORD(2, 2), &WinSockData);

GUID Guid = WSAID_MULTIPLE_RIO;
DWORD RioBytes = 0;
SOCKET Sock = socket(AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
WSAIoctl(Sock, SIO_GET_MULTIPLE_EXTENSION_FUNCTION_POINTER, &Guid, sizeof(Guid), (void**)&Rio, sizeof(Rio), &RioBytes, 0, 0);
closesocket(Sock);
}

// NOTE(casey): Timed portion begins here
// {

// NOTE(casey): "Allocate" memory
LARGE_INTEGER Start; QueryPerformanceCounter(&Start);
char *Memory = (char *)VirtualAlloc(0, AllocSize, VirtualAllocFlags, PAGE_READWRITE);
if (TryRio)
{
Rio.RIODeregisterBuffer(Rio.RIORegisterBuffer(Memory, AllocSize));
};
LARGE_INTEGER Mid; QueryPerformanceCounter(&Mid);

if(Memory)
Expand Down

0 comments on commit 06a6eba

Please sign in to comment.