/perf/perftools

To get this branch, use:
bzr branch http://darksoft.org/webbzr/perf/perftools

« back to all changes in this revision

Viewing changes to memeater.c

  • Committer: Suren A. Chilingaryan
  • Date: 2012-11-27 01:10:56 UTC
  • Revision ID: csa@dside.dyndns.org-20121127011056-n8apsuky9x9rae7b
initial

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#define _XOPEN_SOURCE 500
 
2
#include <stdio.h>
 
3
#include <stdlib.h>
 
4
#include <unistd.h>
 
5
#include <errno.h>
 
6
#include <sys/mman.h>
 
7
#include <sys/sysinfo.h>
 
8
 
 
9
    // 8 bytes page tables + 64 bytes mem map
 
10
    // However may be mem_map (struct page) is already excluded?
 
11
#define BYTES_PER_PAGE 8
 
12
 
 
13
int main(int argc, char *argv[]) {
 
14
    int err;
 
15
    size_t size;
 
16
    void *buf;
 
17
    FILE *f;
 
18
    struct sysinfo info;
 
19
    
 
20
    if (argc < 2) {
 
21
        printf("Usage: %s [+]<GB>\n", argv[0]);
 
22
        printf("  + : lock this amount of memory\n");
 
23
        printf("  = : total memory after lock\n");
 
24
        printf("  - : free memory after lock [default]\n");
 
25
        exit(0);
 
26
    }
 
27
 
 
28
    f = fopen("/proc/sys/vm/drop_caches", "w");
 
29
    if (!f) {
 
30
        printf("Failed to drop caches...\n");
 
31
        exit(-1);
 
32
    }
 
33
    fprintf(f, "3\n");
 
34
    fclose(f);
 
35
 
 
36
    err = sysinfo(&info);
 
37
    if (err) {
 
38
        printf("sysinfo failed with errno: %i\n", errno);
 
39
        exit(-1);
 
40
    }
 
41
 
 
42
    
 
43
    if ((argv[1][0] < '0')||(argv[1][0] > '9'))
 
44
        size = atol(argv[1]+1) * 1024 * 1024 * 1024;
 
45
    else
 
46
        size = atol(argv[1]) * 1024 * 1024 * 1024;
 
47
    
 
48
    if  (argv[1][0]=='+') {
 
49
    } else if  (argv[1][0]=='=') {
 
50
        size_t page_size = getpagesize();
 
51
        size_t pages;
 
52
        size_t page_tables;
 
53
        
 
54
        pages = (info.totalram * info.mem_unit) / page_size;
 
55
        page_tables = pages * BYTES_PER_PAGE;
 
56
        
 
57
        printf("Detected %zu units of memory, unit size %u\n", info.totalram, info.mem_unit);
 
58
        printf("Expected size of page tables: %zu MB, \n", page_tables / 1024 / 1024);
 
59
        printf("Leaving aside %zu bytes\n", size);
 
60
 
 
61
        if (info.totalram * info.mem_unit < size) {
 
62
            printf("Requested more memory (%zu GB) when available (%zu GB)\n", size / 1024 / 1024 / 1024, info.totalram * info.mem_unit / 1024 / 1024 / 1024);
 
63
            exit(-1);
 
64
        }
 
65
        size = info.totalram * info.mem_unit - size - page_tables;
 
66
    } else {
 
67
        if (info.freeram*info.mem_unit < size) {
 
68
            printf("Requested more memory (%zu GB) when is currently free (%zu GB)\n", size / 1024 / 1024 / 1024, info.freeram * info.mem_unit / 1024 / 1024 / 1024);
 
69
            exit(-1);
 
70
        }
 
71
 
 
72
        size = info.freeram * info.mem_unit - size;
 
73
    }
 
74
    
 
75
    if (!size) exit(0);
 
76
 
 
77
    printf("Trying to get %zu bytes\n", size);
 
78
 
 
79
    buf = malloc(size);
 
80
    if (!buf) {
 
81
        printf("Allocation failed\n");
 
82
        exit(-1);
 
83
    }
 
84
    
 
85
    
 
86
    err = mlock(buf, size);
 
87
    if (err) {
 
88
        printf("Locking memory failed, errno: %i\n", errno);
 
89
        exit(-1);
 
90
    }
 
91
    
 
92
    printf("Locked %zu GB memory\n", size/1024/1024/1024);
 
93
    while (1) sleep(60);
 
94
}
 
 
b'\\ No newline at end of file'