From ed569ccbf7e5029e83c40521255e0e406f285bae Mon Sep 17 00:00:00 2001
From: jikai <jikai11@huawei.com>
Date: Fri, 12 Jan 2024 11:31:59 +0800
Subject: [PATCH 22/43] add swap usage in cri

Signed-off-by: jikai <jikai11@huawei.com>
---
 .../entry/cri/v1/v1_cri_container_manager_service.cc  | 11 +++++++++++
 src/daemon/executor/container_cb/execution_extend.c   |  2 ++
 src/daemon/modules/api/runtime_api.h                  |  3 +++
 src/daemon/modules/runtime/isula/isula_rt_ops.c       |  4 ++++
 4 files changed, 20 insertions(+)

diff --git a/src/daemon/entry/cri/v1/v1_cri_container_manager_service.cc b/src/daemon/entry/cri/v1/v1_cri_container_manager_service.cc
index 2dda1e16..47a33c2c 100644
--- a/src/daemon/entry/cri/v1/v1_cri_container_manager_service.cc
+++ b/src/daemon/entry/cri/v1/v1_cri_container_manager_service.cc
@@ -890,6 +890,17 @@ void ContainerManagerService::ContainerStatsToGRPC(
         if (response->container_stats[i]->major_page_faults != 0u) {
             container->mutable_memory()->mutable_major_page_faults()->set_value(response->container_stats[i]->major_page_faults);
         }
+
+        // Swap
+        container->mutable_swap()->set_timestamp(timestamp);
+        if (response->container_stats[i]->swap_used != 0u) {
+            container->mutable_swap()->mutable_swap_usage_bytes()->set_value(response->container_stats[i]->swap_used);
+        }
+        if (response->container_stats[i]->swap_limit >= response->container_stats[i]->swap_used) {
+            container->mutable_swap()->mutable_swap_available_bytes()->set_value(response->container_stats[i]->swap_limit
+                                                                                 - response->container_stats[i]->swap_used);
+        }
+
         containerstats.push_back(std::move(container));
     }
 }
diff --git a/src/daemon/executor/container_cb/execution_extend.c b/src/daemon/executor/container_cb/execution_extend.c
index 25ec5d3b..52401633 100644
--- a/src/daemon/executor/container_cb/execution_extend.c
+++ b/src/daemon/executor/container_cb/execution_extend.c
@@ -259,6 +259,8 @@ static container_info *get_container_stats(const container_t *cont,
     info->major_page_faults = einfo->major_page_faults;
     info->kmem_used = einfo->kmem_used;
     info->kmem_limit = einfo->kmem_limit;
+    info->swap_used = einfo->swap_used;
+    info->swap_limit = einfo->swap_limit;
     info->timestamp = util_get_now_time_nanos();
 
     // workingset is zero if memory used < total inactive file
diff --git a/src/daemon/modules/api/runtime_api.h b/src/daemon/modules/api/runtime_api.h
index d2679c2d..bd170c30 100644
--- a/src/daemon/modules/api/runtime_api.h
+++ b/src/daemon/modules/api/runtime_api.h
@@ -66,6 +66,9 @@ struct runtime_container_resources_stats_info {
     uint64_t cache;
     uint64_t cache_total;
     uint64_t inactive_file_total;
+    /* Swap usage*/
+    uint64_t swap_used;
+    uint64_t swap_limit;
 };
 
 typedef struct _rt_create_params_t {
diff --git a/src/daemon/modules/runtime/isula/isula_rt_ops.c b/src/daemon/modules/runtime/isula/isula_rt_ops.c
index 2c92cc59..745154bb 100644
--- a/src/daemon/modules/runtime/isula/isula_rt_ops.c
+++ b/src/daemon/modules/runtime/isula/isula_rt_ops.c
@@ -647,6 +647,10 @@ static void transform_stats_info_from_runtime(shim_client_runtime_stats *stats,
         info->page_faults = memory->raw->pgfault;
         info->major_page_faults = memory->raw->pgmajfault;
     }
+    if (memory != NULL && memory->swap != NULL) {
+        info->swap_used = memory->swap->usage;
+        info->swap_limit = memory->swap->limit;
+    }
     shim_client_runtime_stats_data_blkio *blkio = stats->data->blkio;
     if (blkio == NULL) {
         return;
-- 
2.34.1

