Skip to content

Instantly share code, notes, and snippets.

@chatman
Created July 3, 2023 13:37
Show Gist options
  • Save chatman/31ba9053a229a34b0fd85a454c3f1b34 to your computer and use it in GitHub Desktop.
Save chatman/31ba9053a229a34b0fd85a454c3f1b34 to your computer and use it in GitHub Desktop.
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <time.h>
#include <stdlib.h>
#define handle_error(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0);
#define OneM (1024 * 1024)
int main(int argc, char *argv[]) {
size_t map_len = (long)25 * 1073741824; // 1024 * 1024 * 1024;
char text_1[OneM];
{ for (int i = 0; i < OneM - 1; i ++) text_1[i] = 'w'; text_1[OneM - 1] = '\0'; }
// Get PID
{ int pid_t = getpid(); printf("Process PID: %d\n", pid_t); }
// dd if=/dev/random of=./f1.data bs=OneM count=65536
int fd_1 = open("/home/ishan/work/mmap/f1.data", O_RDONLY);
if (fd_1 < 0) { handle_error("open"); }
char *map_region = (char *) mmap(NULL, map_len, PROT_READ, MAP_NORESERVE | MAP_PRIVATE | MAP_POPULATE, fd_1, 0);
if(map_region == MAP_FAILED) { handle_error("Mapping Error"); }
printf("MMap File successful: %ld bytes\n", map_len);
srand(time(NULL));
for (int j = 0; j < 10; j++) {
/*
printf("Writing file: Pass - %d\n", j);
int wr_len = strlen(text_1);
for (long i = 0; i < 64 * 1024; i++) {
lseek(fd_1, i * wr_len, SEEK_SET );
long bytesWritten = write(fd_1, text_1, wr_len);
if (bytesWritten != strlen(text_1)) { perror("write() error"); close(fd_1); return -1; }
else { if (i % 1024 == 0) printf("."); }
}
printf("\n");
*/
printf("Reading file: Pass - %d\n", j);
int wr_len = strlen(text_1);
for (long i = 0; i < map_len; i++) {
//char c = map_region[i];
long r = rand() % map_len;
char c = map_region[r];
if (i % 10000000 == 0) printf("r: %llu, c: 0x%x\n", r, c & 0xff);
}
printf("\n");
printf("Sleep for 5s ...\n");
sleep(5);
}
printf("Sleep for 1h ...\n");
sleep(3600);
munmap(map_region, map_len);
if (fd_1 > 0) { close(fd_1); }
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment