-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When referring to memory mapping, one is usually referring to mmap(2). There are 2 categories for using mmap. One category is SHARED vs PRIVATE mappings. The other category is FILE vs ANONYMOUS mappings. Mixed together you get the 4 following combinations: 1. PRIVATE FILE MAPPING 2. SHARED FILE MAPPING 3. PRIVATE ANONYMOUS MAPPING 4. SHARED ANONYMOUS MAPPING Add a memory mapping tool to support the above four scenarios. Updates numa_stress with mmap tool. Signed-off-by: zhenyzha <[email protected]>
- Loading branch information
Showing
4 changed files
with
204 additions
and
83 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <string.h> | ||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
#include <fcntl.h> | ||
#include <sys/mman.h> | ||
#include <errno.h> | ||
#include <pthread.h> | ||
|
||
void *thread_body(void *arg) { | ||
printf("thread: pid = %d, thread_id = %lu\n", getpid(), pthread_self()); | ||
return NULL; | ||
} | ||
struct mmap_struct { | ||
int page_size; | ||
|
||
int fd; | ||
int flags; | ||
void *buf; | ||
unsigned long len; | ||
|
||
int key_break; | ||
}; | ||
|
||
#define MMAP_DEFAULT_SIZE 0x200000 /* 2MB */ | ||
|
||
static int util_misc_key_press(int enable, const char *prefix, const char *desc) | ||
{ | ||
int c; | ||
|
||
if (!enable) | ||
return 0; | ||
|
||
fprintf(stdout, "%s%s\n", prefix, desc); | ||
scanf("%c", &c); | ||
|
||
return c; | ||
} | ||
|
||
static unsigned long util_memory_parse_size(char *args) | ||
{ | ||
char *nptr, *p = NULL; | ||
unsigned long factor = 1, size = 0; | ||
|
||
if (!strlen(args)) | ||
return 0; | ||
|
||
nptr = (char *)malloc(strlen(args) + 1); | ||
if (!nptr) | ||
return 0; | ||
|
||
memset(nptr, 0, strlen(args) + 1); | ||
strcpy(nptr, args); | ||
|
||
if ((p = strstr(nptr, "G")) || (p = strstr(nptr, "g"))) | ||
factor = 0x40000000; | ||
else if ((p = strstr(nptr, "M")) || (p = strstr(nptr, "m"))) | ||
factor = 0x100000; | ||
else if ((p = strstr(nptr, "K")) || (p = strstr(nptr, "k"))) | ||
factor = 0x400; | ||
|
||
if (p) *p = '\0'; | ||
size = factor * strtoul(nptr, NULL, 0); | ||
|
||
free(nptr); | ||
return size; | ||
} | ||
|
||
static void usage(void) | ||
{ | ||
fprintf(stdout, "testsuite mmap [-a] [-s] [-f <filename>] -l <size>] -k\n"); | ||
fprintf(stdout, "\n"); | ||
fprintf(stdout, "-a: Anonymous mapping\n"); | ||
fprintf(stdout, "-f: File based mapping\n"); | ||
fprintf(stdout, "-s Shared anonymous mapping\n"); | ||
fprintf(stdout, "-p: Private mapping\n"); | ||
fprintf(stdout, "-l: Length of memory to be mapped\n"); | ||
fprintf(stdout, "-k: Stop at various stages\n"); | ||
fprintf(stdout, "\n"); | ||
} | ||
|
||
static void mmap_init_data(struct mmap_struct *m) | ||
{ | ||
m->page_size = getpagesize(); | ||
|
||
m->fd = -1; | ||
m->flags = 0; | ||
m->buf = (void *)-1; | ||
m->len = MMAP_DEFAULT_SIZE; | ||
m->key_break = 0; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
struct mmap_struct m; | ||
int opt, c, i; | ||
void * thread_result; | ||
|
||
|
||
mmap_init_data(&m); | ||
|
||
while ((opt = getopt(argc, argv, "af:spl:kh")) != -1) { | ||
switch (opt) { | ||
case 'a': | ||
m.flags |= MAP_ANONYMOUS; | ||
break; | ||
case 'f': | ||
m.fd = open(optarg, O_RDWR); | ||
if (m.fd < 0) { | ||
fprintf(stderr, "Unable to open <%s>\n", optarg); | ||
} | ||
break; | ||
case 's': | ||
m.flags |= MAP_SHARED; | ||
break; | ||
case 'p': | ||
m.flags |= MAP_PRIVATE; | ||
break; | ||
case 'l': | ||
m.len = util_memory_parse_size(optarg); | ||
break; | ||
case 'k': | ||
m.key_break = 1; | ||
break; | ||
case 'h': | ||
default: | ||
usage(); | ||
} | ||
} | ||
|
||
/* mmap */ | ||
m.buf = mmap(NULL, m.len, PROT_READ | PROT_WRITE, m.flags, m.fd, 0); | ||
if (m.buf == (void *)-1) { | ||
fprintf(stdout, "Unable do mmap()\n"); | ||
} | ||
|
||
/* Write */ | ||
memset(m.buf, 0, m.len); | ||
|
||
/* The program will hold memory for 3600s */ | ||
sleep(3600); | ||
|
||
return 0; | ||
} | ||
|
Binary file not shown.
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
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