Blame src/common/vmfill.c
|
Szabolcs Nagy |
cfa23c |
#include <stdint.h>
|
|
Szabolcs Nagy |
cfa23c |
#include <sys/mman.h>
|
|
Szabolcs Nagy |
cfa23c |
#include <fcntl.h>
|
|
Szabolcs Nagy |
cfa23c |
#include <unistd.h>
|
|
Szabolcs Nagy |
daa94c |
#include <limits.h>
|
|
Szabolcs Nagy |
cfa23c |
#include "test.h"
|
|
Szabolcs Nagy |
cfa23c |
#ifndef PAGE_SIZE
|
|
Szabolcs Nagy |
cfa23c |
#define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
|
|
Szabolcs Nagy |
cfa23c |
#endif
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
/* max mmap size, *start is the largest power-of-2 size considered */
|
|
Szabolcs Nagy |
cfa23c |
static size_t mmax(int fd, size_t *start)
|
|
Szabolcs Nagy |
cfa23c |
{
|
|
Szabolcs Nagy |
cfa23c |
size_t i, n;
|
|
Szabolcs Nagy |
cfa23c |
void *p;
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
for (i=n=*start; i>=PAGE_SIZE; i/=2) {
|
|
Szabolcs Nagy |
cfa23c |
if ((p=mmap(0, n, PROT_NONE, MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
|
|
Szabolcs Nagy |
cfa23c |
n -= i/2;
|
|
Szabolcs Nagy |
cfa23c |
} else {
|
|
Szabolcs Nagy |
cfa23c |
munmap(p, n);
|
|
Szabolcs Nagy |
cfa23c |
if (n == i)
|
|
Szabolcs Nagy |
cfa23c |
*start = n;
|
|
Szabolcs Nagy |
cfa23c |
n += i/2;
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
return n & -PAGE_SIZE;
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
/*
|
|
Szabolcs Nagy |
cfa23c |
fills the virtual memory with anonymous PROT_NONE mmaps,
|
|
Szabolcs Nagy |
cfa23c |
returns the mappings in *p and *n in decreasing size order,
|
|
Szabolcs Nagy |
cfa23c |
the return value is the number of mappings or -1 on failure.
|
|
Szabolcs Nagy |
cfa23c |
*/
|
|
Szabolcs Nagy |
cfa23c |
int t_vmfill(void **p, size_t *n, int len)
|
|
Szabolcs Nagy |
cfa23c |
{
|
|
Szabolcs Nagy |
cfa23c |
int fd = open("/dev/zero", O_RDWR);
|
|
Szabolcs Nagy |
cfa23c |
size_t start = SIZE_MAX/2 + 1;
|
|
Szabolcs Nagy |
cfa23c |
size_t m;
|
|
Szabolcs Nagy |
cfa23c |
void *q;
|
|
Szabolcs Nagy |
cfa23c |
int i;
|
|
Szabolcs Nagy |
cfa23c |
|
|
Szabolcs Nagy |
cfa23c |
for (i=0;;i++) {
|
|
Szabolcs Nagy |
cfa23c |
m = mmax(fd, &start;;
|
|
Szabolcs Nagy |
cfa23c |
if (!m)
|
|
Szabolcs Nagy |
cfa23c |
break;
|
|
Szabolcs Nagy |
cfa23c |
q = mmap(0, m, PROT_NONE, MAP_PRIVATE, fd, 0);
|
|
Szabolcs Nagy |
cfa23c |
if (q == MAP_FAILED)
|
|
Szabolcs Nagy |
cfa23c |
return -1;
|
|
Szabolcs Nagy |
cfa23c |
if (i < len) {
|
|
Szabolcs Nagy |
cfa23c |
p[i] = q;
|
|
Szabolcs Nagy |
cfa23c |
n[i] = m;
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
}
|
|
Szabolcs Nagy |
cfa23c |
return i;
|
|
Szabolcs Nagy |
cfa23c |
}
|