forked from jemalloc/jemalloc-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadObject.cpp
68 lines (54 loc) · 1.54 KB
/
ThreadObject.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
#include "ThreadObject.h"
#include <chrono>
#include <exception>
#include <iostream>
#include <mutex>
#include <thread>
#include <assert.h>
#include <gflags/gflags.h>
#include "SizeConstants.h"
DEFINE_int64(alloc_per_thread, k1GB,
"stop each thread after allocating this amount of memory");
DEFINE_int64(bytes_per_phase, k1MB, "bytes allocated per clock 'tick'");
void ThreadObject::free() {
std::lock_guard<std::mutex> guard(this->lock_);
while (!this->q_.empty() &&
this->q_.top().freeAfterAbsolute <= this->currentPhase()) {
this->q_.top().clear();
this->q_.pop();
}
}
void ThreadObject::freeIgnoreLifetime() {
std::lock_guard<std::mutex> guard(this->lock_);
while (!this->q_.empty()) {
this->q_.top().clear();
this->q_.pop();
}
}
void ThreadObject::addToFree(Allocation a) {
int absolutePhase = this->currentPhase() + a.freeAfterRelative;
a.freeAfterAbsolute = absolutePhase;
std::lock_guard<std::mutex> guard(this->lock_);
this->q_.push(a);
}
void *ThreadObject::allocate(size_t sz) {
if (FLAGS_alloc_per_thread <= this->allocSoFar_) {
return nullptr;
} else {
this->allocSoFar_ += sz;
assert(sz > 0);
void *r = malloc(sz);
if (r == nullptr) {
std::cout << "malloc failed." << std::endl;
exit(1);
}
return r;
}
}
int ThreadObject::currentPhase() const {
return this->allocSoFar_ / FLAGS_bytes_per_phase;
}
int ThreadObject::maxPhase() const {
return FLAGS_alloc_per_thread / FLAGS_bytes_per_phase;
}
ThreadObject::ThreadObject() : allocSoFar_(0) {}