Skip to content

Commit 5b59cd7

Browse files
committed
Docs: add memory manager example
1 parent ff773f8 commit 5b59cd7

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

docs/user_guide.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,38 @@ a report on the number of allocations, bytes used, etc.
13701370
This data will then be reported alongside other performance data, currently
13711371
only when using JSON output.
13721372
1373+
For example, a custom manager can report allocation data collected from
1374+
allocator hooks or another profiler:
1375+
1376+
```c++
1377+
class MyMemoryManager : public benchmark::MemoryManager {
1378+
public:
1379+
void Start() override {}
1380+
1381+
void Stop(Result& result) override {
1382+
result.num_allocs = 42;
1383+
result.max_bytes_used = 42000;
1384+
}
1385+
};
1386+
1387+
static void BM_work(benchmark::State& state) {
1388+
for (auto _ : state) {
1389+
/* ... */
1390+
}
1391+
}
1392+
BENCHMARK(BM_work);
1393+
1394+
int main(int argc, char** argv) {
1395+
benchmark::MaybeReenterWithoutASLR(argc, argv);
1396+
MyMemoryManager mm;
1397+
benchmark::RegisterMemoryManager(&mm);
1398+
benchmark::Initialize(&argc, argv);
1399+
benchmark::RunSpecifiedBenchmarks();
1400+
benchmark::RegisterMemoryManager(nullptr);
1401+
benchmark::Shutdown();
1402+
}
1403+
```
1404+
13731405
<a name="profiling" />
13741406

13751407
## Profiling

0 commit comments

Comments
 (0)