Skip to content

Instantly share code, notes, and snippets.

@kostikbel
Created June 19, 2020 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kostikbel/45ff1d526aef78e8697daeca9560f0f1 to your computer and use it in GitHub Desktop.
Save kostikbel/45ff1d526aef78e8697daeca9560f0f1 to your computer and use it in GitHub Desktop.
/* $Id: promotion_lat.c,v 1.8 2020/06/19 14:57:10 kostik Exp kostik $ */
#include <sys/param.h>
#include <sys/mman.h>
#include <machine/cpufunc.h>
#include <err.h>
#include <fcntl.h>
#include <pthread.h>
#include <pthread_np.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
char *m, *mincore_status;
uint64_t *lats;
cpuset_t orig, pin;
size_t idx, page_sizes[10], shmsz, step;
int error, n_sps, opt, shmfd, sp_count;
bool all;
shmsz = 1024 * 1024 * 1024;
all = false;
n_sps = getpagesizes(page_sizes, nitems(page_sizes));
if (n_sps < 2) {
fprintf(stderr, "superpages are not supported\n");
exit(1);
}
while ((opt = getopt(argc, argv, "as:")) != -1) {
switch (opt) {
case 'a':
all = true;
break;
case 's':
shmsz = atoll(optarg);
break;
default:
fprintf(stderr, "Unknown option '%c'\n", opt);
exit(3);
}
}
if (argc != optind) {
fprintf(stderr, "unknown args\n");
exit(3);
}
if (shmsz % page_sizes[1] != 0) {
fprintf(stderr, "shmsz must be multiple of %zd\n", page_sizes[1]);
exit(3);
}
shmfd = shm_open(SHM_ANON, O_RDWR | O_CREAT, 0666);
if (shmfd == -1)
err(1, "shm_open");
error = ftruncate(shmfd, shmsz);
if (error == -1)
err(1, "ftruncate");
mincore_status = malloc(shmsz / page_sizes[0]);
if (mincore_status == NULL)
errx(1, "malloc mincore_status");
lats = malloc((shmsz / page_sizes[0]) * sizeof(*lats));
if (lats == NULL)
errx(1, "malloc lats");
m = mmap(NULL, shmsz, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ALIGNED_SUPER, shmfd, 0);
if (m == MAP_FAILED)
err(1, "mmap");
error = pthread_getaffinity_np(pthread_self(), sizeof(orig), &orig);
if (error != 0)
errc(1, error, "getaffinity");
CPU_ZERO(&pin);
CPU_SET(0, &pin);
error = pthread_setaffinity_np(pthread_self(), sizeof(pin), &pin);
if (error != 0)
errc(1, error, "setaffinity pin");
for (idx = 0; idx < shmsz; idx += page_sizes[0]) {
lats[idx / page_sizes[0]] = -rdtscp();
m[idx] = 0;
lats[idx / page_sizes[0]] += rdtscp();
}
error = pthread_setaffinity_np(pthread_self(), sizeof(orig), &orig);
if (error != 0)
errc(1, error, "setaffinity orig");
error = mincore(m, shmsz, mincore_status);
if (error == -1)
err(1, "mincore");
for (idx = 0, sp_count = 0; idx < shmsz; idx += page_sizes[1]) {
if ((mincore_status[idx / page_sizes[0]] & MINCORE_SUPER) != 0)
sp_count++;
}
error = munmap(m, shmsz);
if (error == -1)
err(1, "munmap");
fprintf(stderr, "promoted %d superpages\n", sp_count);
if (all) {
for (idx = 0; idx < shmsz / page_sizes[0]; idx++) {
printf("%jx %jx %c\n", idx, (uintmax_t)lats[idx],
(mincore_status[idx] & MINCORE_SUPER) != 0 ?
's' : 'n');
}
} else {
step = page_sizes[1] / page_sizes[0];
for (idx = step - 1; idx < shmsz / page_sizes[0]; idx += step) {
if ((mincore_status[idx] & MINCORE_SUPER) != 0)
printf("%jx %jx\n", idx,
(uintmax_t)lats[idx]);
}
}
free(mincore_status);
free(lats);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment