系统:增加系统空闲内存显示 1205/1 1205/head
authorCaisking <3449750381@qq.com>
Thu, 17 Oct 2024 03:18:03 +0000 (11:18 +0800)
committerCaisking <3449750381@qq.com>
Thu, 17 Oct 2024 03:18:03 +0000 (11:18 +0800)
code/sdk/application/wearable/fd230app/nativeui/system/include/SystemLogModel.h
code/sdk/application/wearable/fd230app/nativeui/system/src/SystemLogModel.cpp
code/sdk/application/wearable/service/msg_center/include/notify/debug_output_notification/debug_output_notification.h
code/sdk/application/wearable/service/msg_center/src/notify/debug_output_notification/debug_output_notification.cpp

index 63022c9..73fd26b 100644 (file)
@@ -2,7 +2,7 @@
 #define SystemLog_MODEL_H
 
 #include <cstdint>
-#include <deque> 
+#include <deque>
 namespace OHOS {
 enum class SystemLogType {
   POWER_ON = 0,    // 开机
@@ -34,6 +34,8 @@ public:
 
   uint32_t GetCurrentTime();
 
+  std::string get_system_mem_info();
+
 private:
   SystemLogModel() = default;
   ~SystemLogModel() {}
index 3336cdd..a78b27c 100644 (file)
@@ -9,14 +9,17 @@
 #include <algorithm>
 #include <chrono>
 #include <cstdlib>
+#include <cstring>
 #include <ctime>
-#include <deque> 
+#include <deque>
 #include <fstream>
 #include <iostream>
 #include <sstream>
 #include <string>
+#include <sys/statfs.h>
 #include <sys/time.h>
 #include <thread>
+#include <vector>
 
 #define SYSTEM_LOG_FILE_PATH "/system/system_log.txt"
 
@@ -107,7 +110,7 @@ bool SystemLogModel::loadLog() {
   // while (logData.size() > 50) {
   //   logData.pop_front(); // 从前端删除最早的日志
   // }
-  
+
   WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "Loaded %d logs from file",
                 logData.size());
 
@@ -116,10 +119,51 @@ bool SystemLogModel::loadLog() {
 
 // 获取日志记录,返回deque
 const std::deque<SystemLogData> &SystemLogModel::getLogData() const {
-  return logData; 
+  return logData;
 }
 
 // 获取日志大小
 size_t SystemLogModel::getLogSize() const { return logData.size(); }
 
+//获取系统内存
+/*
+[FS INFO]total size=12416KB used size=8114KB free size=4302KB
+[FS INFO]/update/:
+[FS INFO]total size=12416KB used size=552KB free size=11864KB
+[FS INFO]/music/:
+[FS INFO]total size=12416KB used size=1270KB free size=11146KB
+[FS INFO]/user/:
+[FS INFO]total size=91264KB used size=64596KB free size=26668KB
+*/
+std::string SystemLogModel::get_system_mem_info() {
+  struct statfs file_buf = {0};
+  std::stringstream result; // 使用 stringstream 来构建返回的字符串
+  const std::vector<std::string> volume_names = {"/system/", "/update/",
+                                                 "/music/", "/user/"};
+
+  for (const auto &volume : volume_names) {
+    // 获取文件系统状态
+    if (statfs(volume.c_str(), &file_buf) != 0) {
+      result << "Error retrieving info for " << volume << std::endl;
+      continue;
+    }
+
+    // 计算总大小、已用大小和自由空间
+    uint64_t total_size =
+        static_cast<uint64_t>(file_buf.f_blocks) * file_buf.f_bsize;
+    uint64_t free_size =
+        static_cast<uint64_t>(file_buf.f_bfree) * file_buf.f_bsize;
+    uint64_t used_size = total_size - free_size;
+
+    // 按照所需格式输出
+    result << "[" << volume << "]" << std::endl;
+    result << "Total size=" << total_size / 1024 << "KB " << std::endl;
+    result << "Used size=" << used_size / 1024 << "KB " << std::endl;
+    result << "Free size=" << free_size / 1024 << "KB" << std::endl;
+  }
+
+  // 返回所有分区的自由空间信息,如果没有信息,则返回说明信息
+  return result.str().empty() ? "No volumes found or accessible."
+                              : result.str();
+}
 } // namespace OHOS
\ No newline at end of file
index 71d0de3..7995ef5 100644 (file)
@@ -40,6 +40,7 @@ namespace OHOS
         std::string get_battery_info(void);
         std::string get_shutdown_info(void);
         std::string get_sleep_time(void);
+        std::string get_free_memory(void);
 
       private:
         DebugOutputViewEventListener();
index 62a3ae9..12a9d49 100644 (file)
@@ -88,6 +88,8 @@ namespace OHOS
         debugOutputItemInfo[index++] = {6, "shutdown_file_list"};
         //睡眠数据
         debugOutputItemInfo[index++] = {7, "sleep_time"};
+        //系统空闲内存
+        debugOutputItemInfo[index++] = {8, "system_free_memory"};
         usedLen = index;
         /* -------------------------------- Add data -------------------------------- */
         for (uint8_t i = 0; i < usedLen; i++)
@@ -313,8 +315,8 @@ result += "advdata:" + (getCurrentWatchadvdata()) + " ";
       return oss.str();
     }
 
+    //获取死机列表
     static const char *SHUTDOWN_PATH = "/user/tar";  
-
     std::string DebugOutputViewEventListener::get_shutdown_info() {  
         std::ostringstream oss;  
 
@@ -353,6 +355,14 @@ result += "advdata:" + (getCurrentWatchadvdata()) + " ";
         return result;
     }
 
+    //获取系统free内存
+    std::string DebugOutputViewEventListener::get_free_memory()
+    {
+        auto logModel = SystemLogModel::GetInstance();
+        std::string result = logModel->get_system_mem_info();
+        return result;
+    }
+
     bool DebugOutputViewEventListener::OnClick(OHOS::UIView &view, const OHOS::ClickEvent &event)
     {
         std::string viewId = view.GetViewId();
@@ -411,6 +421,9 @@ result += "advdata:" + (getCurrentWatchadvdata()) + " ";
         case 7:
             str = get_sleep_time();
             break;
+        case 8:
+            str = get_free_memory();
+            break;
         default:
             str = "--";
             break;