Skip to content

Instantly share code, notes, and snippets.

@apage43
Last active April 1, 2023 02:59
Show Gist options
  • Save apage43/ef502602a511ccff73a8882677e54627 to your computer and use it in GitHub Desktop.
Save apage43/ef502602a511ccff73a8882677e54627 to your computer and use it in GitHub Desktop.
// make mcs; ./mcs file
// writes <sample size> as uint32 to mincore.bin followed by 1 mincore() per second for 60 seconds
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#define CHECK(x) if ((x) == -1) { perror(#x); err = 1; goto cleanup; }
#define USEC_PER_SEC 1000000
int main(int argc, char **argv) {
int err = 0;
if (argc < 2) {
printf("Usage: %s <filename>", argv[0]);
}
int fd = open(argv[1], O_RDONLY);
struct stat statbuf;
CHECK(fstat(fd, &statbuf));
void* mapping = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (mapping == NULL) {
perror("mmap");
err = 1;
goto cleanup;
}
int pagesize = 4096;
FILE* outf = fopen("mincore.bin", "w");
if (outf==NULL) {
perror("fopen");
err = 1;
goto cleanup;
}
uint32_t mcbuflen = (statbuf.st_size + pagesize -1 ) / pagesize;
fwrite(&mcbuflen, sizeof(mcbuflen), 1, outf);
int steps = 60;
void* mcorebuf = malloc(mcbuflen);
for (int i=0; i<steps; i++) {
mincore(mapping, statbuf.st_size, mcorebuf);
fwrite(mcorebuf, mcbuflen, 1, outf);
usleep(USEC_PER_SEC);
}
cleanup:
fclose(outf);
free(mcorebuf);
return err;
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment