TheRiver | blog

You have reached the world's edge, none but devils play past here

0%

gperftool分析leveldb内存申请

总结下gperftool的使用,写了个leveldb大量写数据的demo用来测试。

gperftool安装使用

install libunwind

git clone git@github.com:libunwind/libunwind.git

cd libunwind/

autoreconf -i

./configure

make

make install

install gperftools

git clone https://github.com/gperftools/gperftools

cd gperftools/

./autogen.sh

./configure

make

make install prefix=/usr

图形化工具

yum install graphviz

yum install ghostscript

测试leveldb的demo

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
#include <cassert>
#include <iostream>
#include <string>
#include <leveldb/db.h>
#include <gperftools/profiler.h>
using namespace std;
// g++ -o run run.cpp -pthread -lleveldb -std=c++11
#define ASSERT_STATUS(status, db) \
do{ \
if (!status.ok()) \
{ \
delete db; \
return -1; \
} \
} while (0) \


int main() {
// ProfilerStart("/root/code/leveldb/heap.prof");
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
assert(status.ok());

for (auto k{0}; k < 10000000; ++k)
{
std::string key = to_string(k+600000);
std::string value = "river_fdsssssdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" + to_string(k);
status = db->Put(leveldb::WriteOptions(), key, value);
ASSERT_STATUS(status, db);
}


// cout << "get key = " << key << ", value = " << str << endl;
// cin.get();
delete db;
// ProfilerStop();
cout << "ok!!!" << endl;
return 0;
}

测试结果

1
2
3
4
5
g++ run.cpp -std=c++11 -lleveldb -pthread -ltcmalloc -lprofiler -g -o run

HEAPPROFILE="/root/code/leveldb/run" ./run

pprof --pdf run run.0001.heap >run.pdf

参考

https://github.com/gperftools/gperftools/blob/master/README

https://github.com/libunwind/libunwind

https://gperftools.github.io/gperftools/heapprofile.html

https://www.cnblogs.com/GODYCA/archive/2013/05/28/3104281.html

----------- ending -----------