Skip to content

Commit 09ebaf9

Browse files
committed
Add hooks to MaxMemoryPreload to allow pausing the monitoring for a piece of code
1 parent b96fd02 commit 09ebaf9

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

PerfTools/MaxMemoryPreload/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,33 @@ LD_PRELOAD="libPerfToolsAllocMonitorPreload.so libPerfToolsMaxMemoryPreload.so"
1616

1717
the order is important.
1818

19+
### Pausing the monitoring
20+
21+
It is possible to temporarily pause the monitoring by instrumenting the target application by definining the following functions
22+
```cpp
23+
// in some header,
24+
void pauseMaxMemoryPreload();
25+
void unpauseMaxMemoryPreload();
26+
27+
// in an implementation source file
28+
void pauseMaxMemoryPreload() {
29+
}
30+
void unpauseMaxMemoryPreload() {
31+
}
32+
```
33+
and then using these in code
34+
```cpp
35+
...
36+
pauseMaxMemoryPreload();
37+
// code that should be excluded from the monitoring
38+
unpauseMaxMemoryPreload();
39+
...
40+
```
41+
42+
The trick is that by default these functions are defined in the application, and the functions do nothing. The `libPerfToolsMaxMemoryPreload.so` provides also the same functions that actually pause the data collection, and the LD_PRELOADing makes the application to call the functions within `libPerfToolsMaxMemoryPreload.so`.
43+
44+
It is recommended to not pause the monitoring within a multithreaded section, because that could result in unexpected results, because the pausing setting is global.
45+
1946
## Reporting
2047
When the application ends, the monitor will report the following to standard error:
2148

PerfTools/MaxMemoryPreload/src/preload.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@
2626
// static data member definitions
2727
//
2828

29+
// Hooks the target application can be instrumented with to pause and
30+
// unpause the MaxMemoryPreload. Pausing the monitoring during a
31+
// multithreaded execution can result in unexpected results, because
32+
// the setting is global.
33+
namespace {
34+
std::atomic<bool> paused = false;
35+
}
36+
void pauseMaxMemoryPreload() { paused = true; }
37+
void unpauseMaxMemoryPreload() { paused = false; }
38+
2939
namespace {
3040
class MonitorAdaptor : public cms::perftools::AllocMonitorBase {
3141
public:
@@ -34,6 +44,9 @@ namespace {
3444

3545
private:
3646
void allocCalled(size_t iRequested, size_t iActual, void const*) final {
47+
if (paused)
48+
return;
49+
3750
nAllocations_.fetch_add(1, std::memory_order_acq_rel);
3851
requested_.fetch_add(iRequested, std::memory_order_acq_rel);
3952

@@ -48,6 +61,9 @@ namespace {
4861
}
4962
}
5063
void deallocCalled(size_t iActual, void const*) final {
64+
if (paused)
65+
return;
66+
5167
nDeallocations_.fetch_add(1, std::memory_order_acq_rel);
5268
auto present = presentActual_.load(std::memory_order_acquire);
5369
if (present >= iActual) {

0 commit comments

Comments
 (0)