-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from ffengc/dev
dev to main
- Loading branch information
Showing
12 changed files
with
581 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
|
||
|
||
#include "./include/tcmalloc.hpp" | ||
#include <atomic> | ||
#include <thread> | ||
|
||
// ntimes 一轮申请和释放内存的次数 | ||
// rounds 轮次 | ||
void BenchmarkMalloc(size_t ntimes, size_t nworks, size_t rounds) { | ||
std::vector<std::thread> vthread(nworks); | ||
std::atomic<size_t> malloc_costtime(0); | ||
std::atomic<size_t> free_costtime(0); | ||
for (size_t k = 0; k < nworks; ++k) { | ||
vthread[k] = std::thread([&, k]() { | ||
std::vector<void*> v; | ||
v.reserve(ntimes); | ||
for (size_t j = 0; j < rounds; ++j) { | ||
size_t begin1 = clock(); | ||
for (size_t i = 0; i < ntimes; i++) { | ||
v.push_back(malloc(16)); | ||
// v.push_back(malloc((16 + i) % 8192 + 1)); | ||
} | ||
size_t end1 = clock(); | ||
size_t begin2 = clock(); | ||
for (size_t i = 0; i < ntimes; i++) { | ||
free(v[i]); | ||
} | ||
size_t end2 = clock(); | ||
v.clear(); | ||
malloc_costtime += (end1 - begin1); | ||
free_costtime += (end2 - begin2); | ||
} | ||
}); | ||
} | ||
for (auto& t : vthread) { | ||
t.join(); | ||
} | ||
std::cout << nworks << "threads run" << rounds << " times, each round malloc " << ntimes << " times, cost: " << malloc_costtime.load() << "ms\n"; | ||
std::cout << nworks << "threads run" << rounds << " times, each round free " << ntimes << " times, cost: " << free_costtime.load() << " ms\n"; | ||
std::cout << nworks << "threads run malloc and free " << nworks * rounds * ntimes << " time, total cost: " << malloc_costtime.load() + free_costtime.load() << " ms\n"; | ||
} | ||
|
||
// 单轮次申请释放次数 线程数 轮次 | ||
void BenchmarkConcurrentMalloc(size_t ntimes, size_t nworks, size_t rounds) { | ||
std::vector<std::thread> vthread(nworks); | ||
std::atomic<size_t> malloc_costtime(0); | ||
std::atomic<size_t> free_costtime(0); | ||
for (size_t k = 0; k < nworks; ++k) { | ||
vthread[k] = std::thread([&]() { | ||
std::vector<void*> v; | ||
v.reserve(ntimes); | ||
for (size_t j = 0; j < rounds; ++j) { | ||
size_t begin1 = clock(); | ||
for (size_t i = 0; i < ntimes; i++) { | ||
v.push_back(tcmalloc(16)); | ||
// v.push_back(ConcurrentAlloc((16 + i) % 8192 + 1)); | ||
} | ||
size_t end1 = clock(); | ||
size_t begin2 = clock(); | ||
for (size_t i = 0; i < ntimes; i++) { | ||
tcfree(v[i]); | ||
} | ||
size_t end2 = clock(); | ||
v.clear(); | ||
malloc_costtime += (end1 - begin1); | ||
free_costtime += (end2 - begin2); | ||
} | ||
}); | ||
} | ||
for (auto& t : vthread) { | ||
t.join(); | ||
} | ||
std::cout << nworks << "threads run" << rounds << " times, each round malloc " << ntimes << " times, cost: " << malloc_costtime.load() << "ms\n"; | ||
std::cout << nworks << "threads run" << rounds << " times, each round free " << ntimes << " times, cost: " << free_costtime.load() << " ms\n"; | ||
std::cout << nworks << "threads run tcmalloc and tcfree " << nworks * rounds * ntimes << " time, total cost: " << malloc_costtime.load() + free_costtime.load() << " ms\n"; | ||
} | ||
|
||
int main() { | ||
size_t n = 1000; | ||
BenchmarkConcurrentMalloc(n, 4, 10); | ||
std::cout << std::endl | ||
<< std::endl; | ||
BenchmarkMalloc(n, 4, 10); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
|
||
#ifndef __YUFC_OBJECT_POOL_HPP__ | ||
#define __YUFC_OBJECT_POOL_HPP__ | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include "./common.hpp" | ||
|
||
#define __DEFAULT_KB__ 128 | ||
|
||
|
||
|
||
template <class T> | ||
class object_pool { | ||
private: | ||
char* __memory = nullptr; // char 方便切 | ||
size_t __remain_bytes = 0; // 大块内存在切的过程中剩余的字节数 | ||
void* __free_list = nullptr; // 还回来的时候形成的自由链表 | ||
public: | ||
T* new_() { | ||
T* obj = nullptr; | ||
// 不够空间 首选是把还回来的内存块对象进行再次利用 | ||
if (__free_list) { | ||
// 头删 | ||
void* next = *((void**)__free_list); | ||
obj = (T*)__free_list; | ||
__free_list = next; | ||
return obj; | ||
} | ||
if (__remain_bytes < sizeof(T)) { | ||
// 空间不够了,要重新开一个空间 | ||
__remain_bytes = __DEFAULT_KB__ * 1024; | ||
__memory = (char*)malloc(__remain_bytes); | ||
if (__memory == nullptr) { | ||
throw std::bad_alloc(); | ||
} | ||
} | ||
obj = (T*)__memory; | ||
size_t obj_size = sizeof(T) < sizeof(void*) ? sizeof(void*) : sizeof(T); | ||
__memory += obj_size; | ||
__remain_bytes -= obj_size; | ||
new (obj) T; | ||
return obj; | ||
} | ||
void delete_(T* obj) { | ||
obj->~T(); | ||
*(void**)obj = __free_list; | ||
__free_list = obj; | ||
} | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.