forked from rmetzger/dynamic-ballooner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
104 lines (91 loc) · 2.44 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <stdlib.h>
#include <proc/sysinfo.h>
#include <string.h>
#include <deque>
#include <queue>
#include <unistd.h>
/**
* Author: Robert Metzger ([email protected])
*
* The purpose of this tool is to constantly allocate n MB of main memory
* on a linux machine.
* I've used this tool to do I/O hard disk benchmarks and we wanted to have
* a fixed amount of page cache pages around.
*
*/
using namespace std;
struct mem {
int mb_cached;
int mb_buffers;
int mb_free;
};
mem m_info;
// a chunk of allocated memory
struct chunk{
void* ptr;
int size;
};
void update_m_info() {
meminfo();
m_info.mb_cached = kb_main_cached/1024;
m_info.mb_buffers = kb_main_buffers/1024;
m_info.mb_free = kb_main_free/1024;
//cout << "Cached: " << m_info.mb_cached << " Buffers: " << m_info.mb_buffers
// << " Free: " << m_info.mb_free << "\r";
}
int main(int argc, char* argv[])
{
// info from /proc/meminfo
m_info.mb_cached = 0;
m_info.mb_buffers = 0;
m_info.mb_free = 0;
// the amount of mem the user wants for the cache.
int desiredCacheSize = -1;
deque<chunk>* allocated_memory = new deque<chunk>;
if(argc <= 1) {
cout << "Usage: ballooner <desiredCacheSizeMegaBytes>" << endl;
} else {
desiredCacheSize = strtol(argv[1], NULL, 10);
if(desiredCacheSize <= 0) {
cout << "Values equal or lower to zero are not supported";
return 1;
}
update_m_info();
while(true) {
if(m_info.mb_free+m_info.mb_cached > desiredCacheSize) {
// we need to allocate memory
int alloc_mb = ( (m_info.mb_cached+m_info.mb_free)-desiredCacheSize) /2; // edit this param for finer alloc granularity
if(alloc_mb <= 2) {
usleep(5000); // sleep for 0.005 seconds
goto endofLoop;
}
long amount = (long)alloc_mb*1024*1024;
cout << "Allocating " << alloc_mb << " MB " << endl;
void * buffer = malloc ( amount );
memset(buffer, 1, amount);
chunk newAll;
newAll.ptr = buffer;
newAll.size = alloc_mb;
allocated_memory->push_front(newAll);
} else {
// free up again.
if(allocated_memory->size() == 0) {
cout << "No memory allocated!" << endl;
goto endofLoop;
}
chunk somemem = allocated_memory->front();
allocated_memory->pop_front();
if(somemem.ptr != NULL) {
cout << "Freeing " << somemem.size << "MB" << endl;
free(somemem.ptr);
} else {
cout << "Can not free more memory! Exiting...";
return 0;
}
}
endofLoop:
update_m_info();
}
}
}