This repository was archived by the owner on May 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmultiThreadRandomTest.cpp
More file actions
132 lines (112 loc) · 3.92 KB
/
multiThreadRandomTest.cpp
File metadata and controls
132 lines (112 loc) · 3.92 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <thread>
#include <vector>
#include <chrono>
#include <cstdlib>
#include <ctime>
#include <mutex>
#include <fstream>
#include <jemalloc/jemalloc.h>
std::mutex mtx;
void dumpMallocStats() {
const char* opts = "g"; // General information
malloc_stats_print(NULL, NULL, opts);
}
void probeMemoryStats(size_t time) {
uint64_t epoch = 1;
size_t sz = sizeof(epoch);
mallctl("epoch", &epoch, &sz, &epoch, sz);
size_t allocated, active, resident, pdirty, purged, metadata;
sz = sizeof(size_t);
mallctl("stats.allocated", &allocated, &sz, NULL, 0);
mallctl("stats.active", &active, &sz, NULL, 0);
mallctl("stats.resident", &resident, &sz, NULL, 0);
mallctl("stats.arenas.4096.pdirty", &pdirty, &sz, NULL, 0);
mallctl("stats.arenas.4096.dirty_purged", &purged, &sz, NULL, 0);
mallctl("stats.metadata", &metadata, &sz, NULL, 0);
std::cout << allocated << ";" << active << ";" << resident << ";"<< pdirty << ";"<<purged<<";"<<metadata<<";" << time << std::endl;
}
static inline void *no_opt_ptr(void *ptr) {
asm volatile("" : "+r"(ptr));
return ptr;
}
void performOperations(int id, int runTimeSeconds, std::string file_name) {
auto startTime = std::chrono::steady_clock::now();
std::vector<void*> allocations;
std::ifstream inputFile;
long long numOps = 0;
inputFile.open(file_name);
if (!inputFile.is_open()) {
std::cerr << "Failed to open file: " << file_name << std::endl;
return;
}
while (true) {
auto currentTime = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(currentTime - startTime).count();
if (elapsed >= runTimeSeconds) {
break;
}
std::string operation;
size_t size_or_index;
if (inputFile >> operation >> size_or_index) {
if (operation == "alloc") {
void* ptr = malloc(size_or_index);
if (ptr) {
no_opt_ptr(ptr);
allocations.push_back(ptr);
}
} else if (operation == "dalloc") {
if (size_or_index < allocations.size()) {
free(allocations[size_or_index]);
allocations.erase(allocations.begin() + size_or_index);
}
}
numOps ++;
} else {
// End of file or invalid input
break;
}
}
for (void* ptr : allocations) {
free(ptr);
}
allocations.clear();
std::cout << "Total numOps: " << numOps <<std::endl;
}
int main(int argc, char* argv[]) {
if (argc < 5) {
std::cerr << "Usage: " << argv[0] << " <num_threads> <runTimeSeconds> <probeInterval> <file>" << std::endl;
return 1;
}
int num_threads = std::stoi(argv[1]);
int runTimeSeconds = std::stoi(argv[2]);
int probeInterval = std::stoi(argv[3]);
std::string file_name = argv[4];
std::vector<std::thread> threads;
// Launch threads
for (int i = 0; i < num_threads; ++i) {
threads.emplace_back(performOperations, i, runTimeSeconds, file_name);
}
// Main thread probes memory stats
auto startTime = std::chrono::steady_clock::now();
while (true) {
auto currentTime = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(currentTime - startTime).count();
if (elapsed >= runTimeSeconds) {
break;
}
probeMemoryStats(elapsed);
std::this_thread::sleep_for(std::chrono::seconds(probeInterval));
}
// Join all threads
for (auto& th : threads) {
th.join();
}
// Final 30 seconds wait with probing every 10 seconds
for (int i = 0; i < 3; ++i) {
std::this_thread::sleep_for(std::chrono::seconds(10));
probeMemoryStats(1000000);
}
std::cout << "Program ended." << std::endl;
return 0;
}