forked from activeloopai/deeplake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_tracker.cpp
More file actions
79 lines (69 loc) · 2.83 KB
/
Copy pathmemory_tracker.cpp
File metadata and controls
79 lines (69 loc) · 2.83 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "memory_tracker.hpp"
#ifdef __cplusplus
extern "C" {
#endif
#include <postgres.h>
#include <miscadmin.h>
#include <postmaster/bgworker.h>
#include <storage/proc.h>
#include <utils/elog.h>
#include <utils/guc.h>
#include <utils/memutils.h>
#ifdef __cplusplus
}
#endif
namespace pg {
int32_t memory_tracker::memory_limit_mb_{0};
void memory_tracker::initialize_guc_parameters()
{
//base::memory_info info;
//base::system_report::get_meminfo(info);
const auto default_memory_limit = 0;///(info.system_total / (1024 * 1024) * 0.90); /// consider parallel workers
DefineCustomIntVariable(
"pg_deeplake.memory_limit_mb",
"Memory limit for pg_deeplake operations in MB. Set to 0 to disable limit.",
"This parameter controls the maximum amount of PostgreSQL memory that pg_deeplake operations can use. "
"When the limit is exceeded, queries will error out to prevent memory exhaustion.",
&memory_tracker::memory_limit_mb_, // linked C variable
default_memory_limit, // default value (90% of system memory)
0, // min value (0 = unlimited)
INT_MAX / (1024 * 1024), // max value
PGC_USERSET, // context (USERSET, SUSET, etc.)
GUC_UNIT_MB, // flags - treat as MB
nullptr, // check_hook
nullptr, // assign_hook
nullptr // show_hook
);
elog(DEBUG1, "PostgreSQL memory tracker initialized with limit: %d MB", memory_tracker::memory_limit_mb_);
}
void memory_tracker::check_memory_limit()
{
if (is_memory_limit_exceeded()) {
const auto current = get_memory_usage_bytes();
const auto limit = get_memory_limit_bytes();
elog(ERROR, "PostgreSQL memory limit exceeded: current %ld MB, limit %ld MB",
current / (1024 * 1024), limit / (1024 * 1024));
}
}
void memory_tracker::ensure_memory_available(int64_t needed_bytes)
{
const auto limit = get_memory_limit_bytes();
if (limit <= 0) {
return;
}
const auto current_usage = get_memory_usage_bytes();
if ((current_usage + needed_bytes) > limit) {
elog(ERROR, "Insufficient memory: need %zu bytes, current %ld MB, limit %ld MB",
needed_bytes, current_usage / (1024 * 1024), limit / (1024 * 1024));
}
}
void memory_tracker::log_memory_stats()
{
const auto current_usage = get_memory_usage_bytes();
const auto limit = get_memory_limit_bytes();
elog(INFO, "PostgreSQL memory stats: usage %ld MB, limit %ld MB (%.1f%% used)",
current_usage / (1024 * 1024),
limit / (1024 * 1024),
limit > 0 ? (current_usage * 100.0 / limit) : 0.0);
}
} // namespace pg