-
-
Save mikeblas/7f75f16c94652ba6c4317559ad315f86 to your computer and use it in GitHub Desktop.
allocate pages of memory and dirty them up
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <vector> | |
typedef struct { | |
int visitcount; | |
} pagestruct; | |
int main(int argc, char **argv) | |
{ | |
if (argc < 2) | |
{ | |
printf("Usage: swapper <pages>\n\n"); | |
return 1; | |
} | |
// how many pages? | |
int numPages = atoi(argv[1]); | |
printf("%d pages is %d bytes\n", numPages, 4096L * numPages); | |
// build table | |
std::vector<int> indexes(numPages); | |
for (int n = 0; n < numPages; n++) | |
indexes[n] = n; | |
for (int n = 0; n < numPages; n++) | |
{ | |
int target = (rand() % (numPages - n)) + n; | |
// printf("swap %d, %d\n", n, target); | |
int t = indexes[n]; | |
std::swap(indexes[n], indexes[target]); | |
} | |
/* | |
for (int n = 0; n < numPages; n++) | |
printf("%d\n", indexes[n]); | |
*/ | |
// get the actual memory | |
printf("Allocating %d pages ...\n", numPages); | |
std::vector<pagestruct*> pages(numPages); | |
for (int n = 0; n < numPages; n++) | |
{ | |
pages[n] = (pagestruct*) calloc(1, 4096); | |
if (pages[n] == nullptr) | |
{ | |
printf("Allocation failed!\n"); | |
return 1; | |
} | |
} | |
printf("Allocation successful!\n"); | |
// do something with it | |
int visits = 0; | |
while (true) | |
{ | |
for (int n = 0; n < numPages; n++) | |
{ | |
pagestruct* p = pages[indexes[n]]; | |
p->visitcount += 1; | |
} | |
visits += 1; | |
if (visits % 100 == 0) | |
{ | |
for (int n = 0; n < numPages; n++) | |
{ | |
if (pages[indexes[n]]->visitcount != visits) | |
{ | |
printf("got %d, expected %d\n", pages[indexes[n]], visits); | |
return 1; | |
} | |
} | |
printf("done with %d visits\n", visits); | |
} | |
} | |
// done | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment