-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstats.cpp
51 lines (37 loc) · 1.31 KB
/
stats.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
#include <iostream>
#include "stats.hpp"
void AllocStats::recordAlloc(size_t size) {
std::lock_guard<std::mutex> guard(statsMutex);
totalAllocatedBuffers++;
totalAllocatedMemory += size;
liveBuffers++;
liveMemory += size;
if (liveMemory > peakMemory)
peakMemory = liveMemory;
if (liveBuffers > peakBuffers)
peakBuffers = liveBuffers;
}
void AllocStats::recordFree(size_t size) {
std::lock_guard<std::mutex> guard(statsMutex);
totalFreedBuffers++;
totalFreedMemory += size;
liveBuffers--;
liveMemory -= size;
}
void AllocStats::print() {
std::lock_guard<std::mutex> guard(statsMutex);
std::cout << "Total buffers allocated: " << totalAllocatedBuffers << std::endl;
std::cout << "Total buffers freed: " << totalFreedBuffers << std::endl;
if (totalAllocatedBuffers > totalFreedBuffers)
std::cout << "WARNING: Possible memory leak!" << std::endl;
std::cout << "Total memory allocated: " << totalAllocatedMemory << std::endl;
std::cout << "Total memory freed: " << totalFreedMemory << std::endl;
std::cout << "Peak memory: " << peakMemory << std::endl;
std::cout << "Peak # of buffers: " << peakBuffers << std::endl;
}
size_t AllocStats::getCurrentMemory() {
return liveMemory;
}
size_t AllocStats::getCurrentBuffers() {
return liveBuffers;
}