Skip to content

Commit 624caf9

Browse files
committed
update page example
1 parent 41d1109 commit 624caf9

5 files changed

Lines changed: 101 additions & 17 deletions

File tree

lib3rd/utils/rss.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
#include <malloc.h>
2121
#include <stdint.h>
2222
#include <stdlib.h>
23-
#include <string>
2423
#include <string.h>
24+
#include <string>
2525
#include <sys/resource.h>
2626
#include <sys/time.h>
2727
#include <vector>
@@ -59,12 +59,8 @@ inline void FillRSS(rusage &rUsage)
5959
{
6060
newRss = getThreadRss(rUsage);
6161
p = (char *)calloc(++sz, 4096);
62-
auto s = (char *)((uintptr_t)p & (-4096));
63-
while (s < (p + sz * 4096))
64-
{
65-
*s = '0';
66-
s += 4096;
67-
}
62+
63+
memset(p, 0, sz * 4096);
6864
DoNotOptimize(p);
6965
}
7066

src/container/set/reserve.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ BENCHMARK_TEMPLATE(BM_insert, phmap::btree_set<int>);
6363
BENCHMARK_TEMPLATE(BM_insert, std::unordered_set<int>);
6464
BENCHMARK_TEMPLATE(BM_insert, phmap::node_hash_set<int>);
6565
BENCHMARK_TEMPLATE(BM_insert, phmap::flat_hash_set<int>);
66+
BENCHMARK_TEMPLATE(BM_insert, spp::sparse_hash_set<int>);
6667
BENCHMARK_TEMPLATE(BM_insert, tsl::sparse_set<int>);
6768
BENCHMARK_TEMPLATE(BM_insert, tsl::ordered_set<int>);
6869
BENCHMARK_TEMPLATE(BM_insert, tsl::vector_set<int>);
@@ -71,6 +72,7 @@ BENCHMARK_TEMPLATE(BM_insert, tsl::vector_set<int>);
7172
BENCHMARK_TEMPLATE(BM_reserve, std::unordered_set<int>);
7273
BENCHMARK_TEMPLATE(BM_reserve, phmap::node_hash_set<int>);
7374
BENCHMARK_TEMPLATE(BM_reserve, phmap::flat_hash_set<int>);
75+
BENCHMARK_TEMPLATE(BM_reserve, spp::sparse_hash_set<int>);
7476
BENCHMARK_TEMPLATE(BM_reserve, tsl::sparse_set<int>);
7577
BENCHMARK_TEMPLATE(BM_reserve, tsl::ordered_set<int>);
7678
BENCHMARK_TEMPLATE(BM_reserve, tsl::vector_set<int>);

src/memory/page.cpp

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,103 @@
1414
1515

1616
#include "utils/rss.h"
17+
#include <fstream>
18+
#include <sstream>
19+
#include <sys/mman.h>
1720
#include <unistd.h>
21+
22+
const int PAGE_SIZE = sysconf(_SC_PAGESIZE);
23+
24+
inline std::string &trim_(std::string &str)
25+
{
26+
const char *spaces = " \n\r\t";
27+
str.erase(str.find_last_not_of(spaces) + 1);
28+
str.erase(0, str.find_first_not_of(spaces));
29+
return str;
30+
}
31+
32+
static inline void StringSplit(const std::string &str, const std::string &delimiters, std::vector<std::string> &elems)
33+
{
34+
std::string::size_type pos, prev = 0;
35+
while ((pos = str.find_first_of(delimiters, prev)) != std::string::npos)
36+
{
37+
if (pos > prev)
38+
{
39+
elems.emplace_back(str, prev, pos - prev);
40+
}
41+
prev = pos + 1;
42+
}
43+
if (prev < str.size())
44+
elems.emplace_back(str, prev, str.size() - prev);
45+
}
46+
47+
struct stat_m
48+
{
49+
size_t vmSize;
50+
size_t vmRss;
51+
size_t vmShare;
52+
size_t textSize;
53+
size_t libSize;
54+
size_t dataSize;
55+
size_t dirtyPages;
56+
};
57+
58+
stat_m GetStatM()
59+
{
60+
std::ostringstream oss;
61+
std::fstream fs("/proc/self/statm", std::ios_base::in | std::ios_base::binary);
62+
oss << fs.rdbuf();
63+
std::vector<std::string> vec;
64+
StringSplit(oss.str(), " ", vec);
65+
stat_m ret;
66+
if (vec.size() >= 7)
67+
{
68+
size_t *p = &ret.vmSize;
69+
for (const auto &it : vec)
70+
{
71+
if (p <= &ret.dirtyPages)
72+
{
73+
*p++ = std::stoul(it) * PAGE_SIZE;
74+
}
75+
}
76+
}
77+
return ret;
78+
}
79+
80+
void PrintStatM(const stat_m &m)
81+
{
82+
std::cout << "vmSize : " << m.vmSize << " B / " << m.vmSize / 1024 << " KB /" << m.vmSize / 1024.0 / 1024.0 << " MB" << '\n';
83+
std::cout << "vmRss : " << m.vmRss << " B / " << m.vmRss / 1024 << " KB /" << m.vmRss / 1024.0 / 1024.0 << " MB" << '\n';
84+
std::cout << "vmShare : " << m.vmShare << " B / " << m.vmShare / 1024 << " KB /" << m.vmShare / 1024.0 / 1024.0 << " MB" << '\n';
85+
std::cout << "textSize : " << m.textSize << " B / " << m.textSize / 1024 << " KB /" << m.textSize / 1024.0 / 1024.0 << " MB" << '\n';
86+
std::cout << "libSize : " << m.libSize << " B / " << m.libSize / 1024 << " KB /" << m.libSize / 1024.0 / 1024.0 << " MB" << '\n';
87+
std::cout << "dataSize : " << m.dataSize << " B / " << m.dataSize / 1024 << " KB /" << m.dataSize / 1024.0 / 1024.0 << " MB" << '\n';
88+
std::cout << "dirtySize : " << m.dirtyPages << " B / " << m.dirtyPages / 1024 << " KB /" << m.dirtyPages / 1024.0 / 1024.0 << " MB" << '\n';
89+
std::cout << '\n';
90+
std::cout.flush();
91+
}
92+
1893
int main(int argc, char **argv)
1994
{
95+
auto ret = GetStatM();
96+
PrintStatM(ret);
2097
rusage usage{};
2198
FillRSS(usage);
22-
auto sz = size_t(1) << 32;
23-
auto p = (char *)sbrk(sz);
99+
auto sz = size_t(1) << 32;
100+
auto p = (char *)sbrk(sz);
101+
auto old_p = p;
24102
for (size_t i = 0; i < sz / 4096; i++)
25103
{
26104
p[0] = 'c';
27105
p += 4096;
28106
}
29107
PrintUsage(usage);
108+
ret = GetStatM();
109+
PrintStatM(ret);
110+
auto r = madvise(old_p, sz, MADV_DONTNEED);
111+
std::cout << r << strerror(errno) << '\n';
112+
PrintUsage(usage);
113+
ret = GetStatM();
114+
PrintStatM(ret);
30115
return 0;
31116
}

src/memory/rss_tc.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
const int SIZE = 1024 * 1024;
3737
int main(int argc, char *argv[])
3838
{
39+
std::string out;
40+
MallocExtension::instance()->GetHeapSample(&out);
41+
std::cout<<out<<'\n';
3942
{
4043
rusage usage{};
4144
FillRSS(usage);

src/string/string_split.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ static void BenchStringSplit(benchmark::State &state)
5454
{
5555
std::string tag = "1,2,3,4,5,6,7,8,9,0,1,2,3,4,6,1,2,3,4,56,7,8,9,09,12";
5656
int r = 0;
57-
std::vector<std::string> vec;
57+
5858
for (auto _ : state)
5959
{
60+
std::vector<std::string> vec;
6061
StringSplit(tag, ",", vec);
6162
for (auto &i : vec)
6263
{
6364
r += i.size();
6465
}
65-
vec.clear();
6666
}
6767
benchmark::DoNotOptimize(r);
6868
}
@@ -73,15 +73,15 @@ static void BenchStringSplitStream(benchmark::State &state)
7373
{
7474
std::string tag = "1,2,3,4,5,6,7,8,9,0,1,2,3,4,6,1,2,3,4,56,7,8,9,09,12";
7575
int r = 0;
76-
std::vector<std::string> vec;
76+
7777
for (auto _ : state)
7878
{
79+
std::vector<std::string> vec;
7980
StringSplitStream(tag, ',', vec);
8081
for (auto &i : vec)
8182
{
8283
r += i.size();
8384
}
84-
vec.clear();
8585
}
8686
benchmark::DoNotOptimize(r);
8787
}
@@ -92,15 +92,14 @@ static void BenchBoostSplit(benchmark::State &state)
9292
{
9393
std::string tag = "1,2,3,4,5,6,7,8,9,0,1,2,3,4,6,1,2,3,4,56,7,8,9,09,12";
9494
int r = 0;
95-
std::vector<std::string> vec;
9695
for (auto _ : state)
9796
{
97+
std::vector<std::string> vec;
9898
boost::split(vec, tag, [](char c) { return c = ','; });
9999
for (auto &i : vec)
100100
{
101101
r += i.size();
102102
}
103-
vec.clear();
104103
}
105104
benchmark::DoNotOptimize(r);
106105
}
@@ -128,15 +127,14 @@ static void BenchPBSplit(benchmark::State &state)
128127
{
129128
std::string tag = "1,2,3,4,5,6,7,8,9,0,1,2,3,4,6,1,2,3,4,56,7,8,9,09,12";
130129
int r = 0;
131-
std::vector<std::string> vec;
132130
for (auto _ : state)
133131
{
132+
std::vector<std::string> vec;
134133
google::protobuf::SplitStringAllowEmpty(tag, ",", &vec);
135134
for (auto &i : vec)
136135
{
137136
r += i.size();
138137
}
139-
vec.clear();
140138
}
141139
benchmark::DoNotOptimize(r);
142140
}

0 commit comments

Comments
 (0)