Ocean
Loading...
Searching...
No Matches
system/Memory.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_SYSTEM_MEMORY_H
9#define META_OCEAN_SYSTEM_MEMORY_H
10
11#include "ocean/system/System.h"
12
13#include "ocean/base/Thread.h"
14
15namespace Ocean
16{
17
18namespace System
19{
20
21/**
22 * This class implements system memory management functions.
23 * @ingroup system
24 */
25class OCEAN_SYSTEM_EXPORT Memory
26{
27 public:
28
29 /**
30 * This class implements a simple engine measuring memory (RAM) usage over time for the entire process.
31 * The object simply starts an own thread and measures the used virtual memory as often as possible.<br>
32 * Beware: The object can create several thousand measurements in a few seconds.<br>
33 * Thus, this function is mainly intended to measure the RAM impact of individual functions instead of measuring the RAM impact during a longer time period.<br>
34 * This function is not thread-safe.
35 *
36 * Here is a tutorial how to use this class:
37 * @code
38 * {
39 * Memory::MemoryMeasurement measurement;
40 *
41 * measurement.start();
42 *
43 * // now we can call a function needing a huge amount of memory
44 * // beware: other threads may also have a memory impact that will be part of the measurements
45 * expensiveMemoryFunction();
46 *
47 * measurement.stop();
48 * }
49 * @endcode
50 * @see processVirtualMemory().
51 */
52 class MemoryMeasurement : protected Thread
53 {
54 public:
55
56 /**
57 * Starts the measurements.
58 * Previous measurements will be cleared.<br>
59 * Beware: Stop measuring as quick as possible - as we will get a large amount of measurements.
60 * @see stop().
61 */
62 void start();
63
64 /**
65 * Stops the measurements.
66 * Do not call this function before calling start().
67 * @see start().
68 */
69 void stop();
70
71 /**
72 * Returns all memory measurements that have been done between calling start() and stop().
73 * The resulting measurements provide the virtual memory used by the calling process in bytes (over time).<br>
74 * Beware: Do not call this function before stopping measurements.
75 * @return The entire set of measurements
76 * @see stop(), processVirtualMemory().
77 */
78 const std::vector<uint64_t>& measurements();
79
80 /**
81 * Returns the number of bytes this memory profiler object will be responsible for.
82 * The impact is based on the assumption e.g., starting a thread or storing measurements will need memory.<br>
83 * We can use the result of this function to have a rough guess how big this impact is.
84 * @return The impact this profile tool has on the memory usage in bytes, with range (-infinity, infinity)
85 */
87
88 /**
89 * Returns the minimal measurement that has been done between calling start() and stop().
90 * @return The minimal virtual memory usage in bytes, with range [0, maximal()]
91 * Beware: Do not call this function before stopping measurements.
92 * @see stop(), maximal().
93 */
94 uint64_t minimum();
95
96 /**
97 * Returns the maximal measurement that has been done between calling start() and stop().
98 * @return The maximal virtual memory usage in bytes, with range [minimum(), infinity)
99 * Beware: Do not call this function before stopping measurements.
100 * @see stop(), minimum().
101 */
102 uint64_t maximum();
103
104 /**
105 * Returns the minimal memory peak in relation to the identity.
106 * The identity is the very first measurement immediately determined when starting the profiler via start().<br>
107 * This Function actually returns minimum() - measurements.front()<br>
108 * Beware: Do not call this function before stopping measurements.
109 * @return The difference between minimum memory usage and the very first memory measurement
110 * @see measurementImpact(), maxPeakToIdentity(), stop().
111 */
113
114 /**
115 * Returns the maximal memory peak in relation to the identity.
116 * The identity is the very first measurement immediately determined when starting the profiler via start().<br>
117 * This Function actually returns maximum() - measurements.front()<br>
118 * Beware: Do not call this function before stopping measurements.
119 * @return The difference between maximal memory usage and the very first memory measurement
120 * @see measurementImpact(), minPeakToIdentity(), stop().
121 */
123
124 protected:
125
126 /**
127 * This function has to be overloaded in derived class.
128 */
129 void threadRun() override;
130
131 protected:
132
133 /// All measurements that have been gathered between calling start() and stop().
134 std::vector<uint64_t> measurements_;
135
136 /// The first measurement within the thread function.
138 };
139
140 public:
141
142 /**
143 * Returns the current system memory load in percent, with range [0, 100], -1 if not available
144 * @return Current memory load
145 */
146 static int memoryLoad();
147
148 /**
149 * Returns the amount of virtual memory used by the calling process.
150 * @return The virtual memory use by the process, in bytes, 0 if not available
151 */
152 static uint64_t processVirtualMemory();
153
154 /**
155 * Returns the system-wide total amount of actual physical memory in bytes.
156 * @return Physical memory in bytes, 0 if not available
157 */
158 static uint64_t totalPhysicalMemory();
159
160 /**
161 * Returns the system-wide total amount of virtual memory in bytes.
162 * @return Virtual memory in bytes, 0 if not available
163 */
164 static uint64_t totalVirtualMemory();
165
166 /**
167 * Returns the system-wide available amount of physical memory in bytes.
168 * @return Available physical memory in bytes, 0 if not available
169 */
170 static uint64_t availablePhysicalMemory();
171
172 /**
173 * Returns the system-wide available amount of virtual memory in bytes.
174 * @return Available virtual memory in bytes, 0 if not available
175 */
176 static uint64_t availableVirtualMemory();
177};
178
179}
180
181}
182
183#endif // META_OCEAN_SYSTEM_MEMORY_H
This class implements a simple engine measuring memory (RAM) usage over time for the entire process.
Definition system/Memory.h:53
void start()
Starts the measurements.
uint64_t maximum()
Returns the maximal measurement that has been done between calling start() and stop().
void stop()
Stops the measurements.
int64_t maxPeakToIdentity()
Returns the maximal memory peak in relation to the identity.
uint64_t measurementFirstInThread_
The first measurement within the thread function.
Definition system/Memory.h:137
std::vector< uint64_t > measurements_
All measurements that have been gathered between calling start() and stop().
Definition system/Memory.h:134
void threadRun() override
This function has to be overloaded in derived class.
int64_t minPeakToIdentity()
Returns the minimal memory peak in relation to the identity.
const std::vector< uint64_t > & measurements()
Returns all memory measurements that have been done between calling start() and stop().
uint64_t minimum()
Returns the minimal measurement that has been done between calling start() and stop().
int64_t measurementImpact()
Returns the number of bytes this memory profiler object will be responsible for.
This class implements system memory management functions.
Definition system/Memory.h:26
static uint64_t availableVirtualMemory()
Returns the system-wide available amount of virtual memory in bytes.
static uint64_t processVirtualMemory()
Returns the amount of virtual memory used by the calling process.
static uint64_t totalVirtualMemory()
Returns the system-wide total amount of virtual memory in bytes.
static uint64_t availablePhysicalMemory()
Returns the system-wide available amount of physical memory in bytes.
static uint64_t totalPhysicalMemory()
Returns the system-wide total amount of actual physical memory in bytes.
static int memoryLoad()
Returns the current system memory load in percent, with range [0, 100], -1 if not available.
This class implements a thread.
Definition Thread.h:115
The namespace covering the entire Ocean framework.
Definition Accessor.h:15