From 48631d1e379ab50ed4981c1490f242b67cb38e41 Mon Sep 17 00:00:00 2001
From: Antonio Filipovic <61245998+antoniofilipovic@users.noreply.github.com>
Date: Fri, 3 Nov 2023 14:40:45 +0100
Subject: [PATCH] Rename memory usage and memory allocated  (#1426)

---
 src/http_handlers/metrics.hpp           |  2 +-
 src/license/license_sender.cpp          |  4 ++--
 src/query/interpreter.cpp               |  4 ++--
 src/storage/v2/disk/storage.cpp         |  2 +-
 src/storage/v2/inmemory/storage.cpp     |  2 +-
 src/storage/v2/storage.hpp              |  4 ++--
 src/telemetry/collectors.cpp            |  2 +-
 src/utils/stat.hpp                      |  2 +-
 tests/e2e/configuration/storage_info.py |  6 +++---
 tests/stress/memory_limit.py            |  2 +-
 tests/stress/memory_tracker.py          | 10 +++++-----
 tests/unit/database_get_info.cpp        |  4 ++--
 tests/unit/storage_v2_get_info.cpp      |  4 ++--
 13 files changed, 24 insertions(+), 24 deletions(-)

diff --git a/src/http_handlers/metrics.hpp b/src/http_handlers/metrics.hpp
index 06876167f..d4620b774 100644
--- a/src/http_handlers/metrics.hpp
+++ b/src/http_handlers/metrics.hpp
@@ -65,7 +65,7 @@ class MetricsService {
     return MetricsResponse{.vertex_count = info.vertex_count,
                            .edge_count = info.edge_count,
                            .average_degree = info.average_degree,
-                           .memory_usage = info.memory_usage,
+                           .memory_usage = info.memory_res,
                            .disk_usage = info.disk_usage,
                            .event_counters = GetEventCounters(),
                            .event_gauges = GetEventGauges(),
diff --git a/src/license/license_sender.cpp b/src/license/license_sender.cpp
index ac93565e8..1feabbb48 100644
--- a/src/license/license_sender.cpp
+++ b/src/license/license_sender.cpp
@@ -42,7 +42,7 @@ void LicenseInfoSender::SendData() {
   license_info_.WithLock([&data, this](const auto &license_info) mutable {
     if (license_info && !license_info->organization_name.empty()) {
       const auto memory_info = utils::GetMemoryInfo();
-      const auto memory_usage = utils::GetMemoryUsage();
+      const auto memory_res = utils::GetMemoryRES();
       data = {{"run_id", uuid_},
               {"machine_id", machine_id_},
               {"type", "license-check"},
@@ -52,7 +52,7 @@ void LicenseInfoSender::SendData() {
               {"valid", license_info->is_valid},
               {"physical_memory_size", memory_info.memory},
               {"swap_memory_size", memory_info.swap},
-              {"memory_used", memory_usage},
+              {"memory_usage", memory_res},
               {"runtime_memory_limit", memory_limit_},
               {"license_memory_limit", license_info->license.memory_limit},
               {"timestamp", utils::Timestamp::Now().SecWithNsecSinceTheEpoch()}};
diff --git a/src/query/interpreter.cpp b/src/query/interpreter.cpp
index c92f3faa7..074c90176 100644
--- a/src/query/interpreter.cpp
+++ b/src/query/interpreter.cpp
@@ -3119,9 +3119,9 @@ PreparedQuery PrepareSystemInfoQuery(ParsedQuery parsed_query, bool in_explicit_
             {TypedValue("vertex_count"), TypedValue(static_cast<int64_t>(info.vertex_count))},
             {TypedValue("edge_count"), TypedValue(static_cast<int64_t>(info.edge_count))},
             {TypedValue("average_degree"), TypedValue(info.average_degree)},
-            {TypedValue("memory_usage"), TypedValue(utils::GetReadableSize(static_cast<double>(info.memory_usage)))},
+            {TypedValue("memory_res"), TypedValue(utils::GetReadableSize(static_cast<double>(info.memory_res)))},
             {TypedValue("disk_usage"), TypedValue(utils::GetReadableSize(static_cast<double>(info.disk_usage)))},
-            {TypedValue("memory_allocated"),
+            {TypedValue("memory_tracked"),
              TypedValue(utils::GetReadableSize(static_cast<double>(utils::total_memory_tracker.Amount())))},
             {TypedValue("allocation_limit"),
              TypedValue(utils::GetReadableSize(static_cast<double>(utils::total_memory_tracker.HardLimit())))},
diff --git a/src/storage/v2/disk/storage.cpp b/src/storage/v2/disk/storage.cpp
index 3b1b740f9..809d744ed 100644
--- a/src/storage/v2/disk/storage.cpp
+++ b/src/storage/v2/disk/storage.cpp
@@ -801,7 +801,7 @@ StorageInfo DiskStorage::GetBaseInfo(bool /* unused */) {
     // NOLINTNEXTLINE(bugprone-narrowing-conversions, cppcoreguidelines-narrowing-conversions)
     info.average_degree = 2.0 * static_cast<double>(info.edge_count) / info.vertex_count;
   }
-  info.memory_usage = utils::GetMemoryUsage();
+  info.memory_res = utils::GetMemoryRES();
   info.disk_usage = GetDiskSpaceUsage();
   return info;
 }
diff --git a/src/storage/v2/inmemory/storage.cpp b/src/storage/v2/inmemory/storage.cpp
index 2bbb1888a..d2ce80417 100644
--- a/src/storage/v2/inmemory/storage.cpp
+++ b/src/storage/v2/inmemory/storage.cpp
@@ -1557,7 +1557,7 @@ StorageInfo InMemoryStorage::GetBaseInfo(bool force_directory) {
     // NOLINTNEXTLINE(bugprone-narrowing-conversions, cppcoreguidelines-narrowing-conversions)
     info.average_degree = 2.0 * static_cast<double>(info.edge_count) / info.vertex_count;
   }
-  info.memory_usage = utils::GetMemoryUsage();
+  info.memory_res = utils::GetMemoryRES();
   // Special case for the default database
   auto update_path = [&](const std::filesystem::path &dir) {
     if (!force_directory && std::filesystem::is_directory(dir) && dir.has_filename()) {
diff --git a/src/storage/v2/storage.hpp b/src/storage/v2/storage.hpp
index 0f4195aee..2d36d202a 100644
--- a/src/storage/v2/storage.hpp
+++ b/src/storage/v2/storage.hpp
@@ -69,7 +69,7 @@ struct StorageInfo {
   uint64_t vertex_count;
   uint64_t edge_count;
   double average_degree;
-  uint64_t memory_usage;
+  uint64_t memory_res;
   uint64_t disk_usage;
   uint64_t label_indices;
   uint64_t label_property_indices;
@@ -86,7 +86,7 @@ static inline nlohmann::json ToJson(const StorageInfo &info) {
 
   res["edges"] = info.edge_count;
   res["vertices"] = info.vertex_count;
-  res["memory"] = info.memory_usage;
+  res["memory"] = info.memory_res;
   res["disk"] = info.disk_usage;
   res["label_indices"] = info.label_indices;
   res["label_prop_indices"] = info.label_property_indices;
diff --git a/src/telemetry/collectors.cpp b/src/telemetry/collectors.cpp
index dbfe8eeb4..db5517e3e 100644
--- a/src/telemetry/collectors.cpp
+++ b/src/telemetry/collectors.cpp
@@ -79,7 +79,7 @@ const nlohmann::json GetResourceUsage(std::filesystem::path root_directory) {
   auto cpu_total = GetCpuUsage(pid);
   cpu["usage"] = cpu_total.second;
 
-  return {{"cpu", cpu}, {"memory", utils::GetMemoryUsage()}, {"disk", utils::GetDirDiskUsage(root_directory)}};
+  return {{"cpu", cpu}, {"memory", utils::GetMemoryRES()}, {"disk", utils::GetDirDiskUsage(root_directory)}};
 }
 
 }  // namespace memgraph::telemetry
diff --git a/src/utils/stat.hpp b/src/utils/stat.hpp
index 611c9b4d7..a14973fd7 100644
--- a/src/utils/stat.hpp
+++ b/src/utils/stat.hpp
@@ -40,7 +40,7 @@ inline uint64_t GetDirDiskUsage(const std::filesystem::path &path) {
 }
 
 /// Returns the number of bytes the process is using in the memory.
-inline uint64_t GetMemoryUsage() {
+inline uint64_t GetMemoryRES() {
   // Get PID of entire process.
   pid_t pid = getpid();
   uint64_t memory = 0;
diff --git a/tests/e2e/configuration/storage_info.py b/tests/e2e/configuration/storage_info.py
index 614d644cd..c73656ddb 100644
--- a/tests/e2e/configuration/storage_info.py
+++ b/tests/e2e/configuration/storage_info.py
@@ -20,9 +20,9 @@ default_storage_info_dict = {
     "vertex_count": 0,
     "edge_count": 0,
     "average_degree": 0,
-    "memory_usage": "",  # machine dependent
+    "memory_res": "",  # machine dependent
     "disk_usage": "",  # machine dependent
-    "memory_allocated": "",  # machine dependent
+    "memory_tracked": "",  # machine dependent
     "allocation_limit": "",  # machine dependent
     "global_isolation_level": "SNAPSHOT_ISOLATION",
     "session_isolation_level": "",
@@ -53,7 +53,7 @@ def test_does_default_config_match():
     config = cursor.fetchall()
 
     # The default value of these is dependent on the given machine.
-    machine_dependent_configurations = ["memory_usage", "disk_usage", "memory_allocated", "allocation_limit"]
+    machine_dependent_configurations = ["memory_res", "disk_usage", "memory_tracked", "allocation_limit"]
 
     # Number of different data-points returned by SHOW STORAGE INFO
     assert len(config) == 12
diff --git a/tests/stress/memory_limit.py b/tests/stress/memory_limit.py
index 8c6b699a9..2b2084484 100644
--- a/tests/stress/memory_limit.py
+++ b/tests/stress/memory_limit.py
@@ -160,7 +160,7 @@ def get_tracker_data(session) -> Optional[float]:
 
     try:
         data, _ = try_execute(session, f"SHOW STORAGE INFO")
-        memory_tracker_data = isolate_value(data, "memory_allocated")
+        memory_tracker_data = isolate_value(data, "memory_tracked")
 
         return parse_data(memory_tracker_data)
 
diff --git a/tests/stress/memory_tracker.py b/tests/stress/memory_tracker.py
index a9ca26b06..ec67fe97b 100644
--- a/tests/stress/memory_tracker.py
+++ b/tests/stress/memory_tracker.py
@@ -144,7 +144,7 @@ def run_writer(repetition_count: int, sleep_sec: float, worker_id: int) -> int:
     def create() -> bool:
         exception_occured = False
 
-        memory_allocated_before, _ = get_storage_data(session)
+        memory_tracked_before, _ = get_storage_data(session)
         count_before = execute_till_success(session, f"MATCH (n) RETURN COUNT(n) AS cnt")[0][0]["cnt"]
         try:
             try_execute(
@@ -155,12 +155,12 @@ def run_writer(repetition_count: int, sleep_sec: float, worker_id: int) -> int:
             log.info(f"Exception occured during create: {ex}")
             exception_occured = True
 
-        memory_allocated_after, _ = get_storage_data(session)
+        memory_tracked_after, _ = get_storage_data(session)
         count_after = execute_till_success(session, f"MATCH (n) RETURN COUNT(n) AS cnt")[0][0]["cnt"]
         if exception_occured:
             log.info(
                 f"Exception occured, stopping exection of run {Constants.CREATE_FUNCTION} worker."
-                f"Memory stats: before query: {memory_allocated_before}, after query: {memory_allocated_after}."
+                f"Memory stats: before query: {memory_tracked_before}, after query: {memory_tracked_after}."
                 f"Node stats: before query {count_before}, after query {count_after}"
             )
             return False
@@ -228,8 +228,8 @@ def get_storage_data(session) -> Tuple[float, float]:
 
     try:
         data = execute_till_success(session, f"SHOW STORAGE INFO")[0]
-        res_data = isolate_value(data, "memory_usage")
-        memory_tracker_data = isolate_value(data, "memory_allocated")
+        res_data = isolate_value(data, "memory_res")
+        memory_tracker_data = isolate_value(data, "memory_tracked")
         log.info(
             f"Worker {Constants.MONITOR_CLEANUP_FUNCTION} logged memory: memory tracker {memory_tracker_data} vs res data {res_data}"
         )
diff --git a/tests/unit/database_get_info.cpp b/tests/unit/database_get_info.cpp
index 627028d95..043042100 100644
--- a/tests/unit/database_get_info.cpp
+++ b/tests/unit/database_get_info.cpp
@@ -161,8 +161,8 @@ TYPED_TEST(InfoTest, InfoCheck) {
   ASSERT_EQ(info.storage_info.vertex_count, 5);
   ASSERT_EQ(info.storage_info.edge_count, 2);
   ASSERT_EQ(info.storage_info.average_degree, 0.8);
-  ASSERT_GT(info.storage_info.memory_usage, 10'000'000);  // 200MB < > 10MB
-  ASSERT_LT(info.storage_info.memory_usage, 200'000'000);
+  ASSERT_GT(info.storage_info.memory_res, 10'000'000);  // 200MB < > 10MB
+  ASSERT_LT(info.storage_info.memory_res, 200'000'000);
   ASSERT_GT(info.storage_info.disk_usage, 100);  // 1MB < > 100B
   ASSERT_LT(info.storage_info.disk_usage, 1000'000);
   ASSERT_EQ(info.storage_info.label_indices, 1);
diff --git a/tests/unit/storage_v2_get_info.cpp b/tests/unit/storage_v2_get_info.cpp
index 3e50e67c8..bb7d5c0a1 100644
--- a/tests/unit/storage_v2_get_info.cpp
+++ b/tests/unit/storage_v2_get_info.cpp
@@ -140,8 +140,8 @@ TYPED_TEST(InfoTest, InfoCheck) {
   ASSERT_EQ(info.vertex_count, 5);
   ASSERT_EQ(info.edge_count, 2);
   ASSERT_EQ(info.average_degree, 0.8);
-  ASSERT_GT(info.memory_usage, 10'000'000);  // 200MB < > 10MB
-  ASSERT_LT(info.memory_usage, 200'000'000);
+  ASSERT_GT(info.memory_res, 10'000'000);  // 200MB < > 10MB
+  ASSERT_LT(info.memory_res, 200'000'000);
   ASSERT_GT(info.disk_usage, 100);  // 1MB < > 100B
   ASSERT_LT(info.disk_usage, 1000'000);
   ASSERT_EQ(info.label_indices, 1);