martinzink commented on a change in pull request #1032:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1032#discussion_r621969441



##########
File path: libminifi/src/utils/OsUtils.cpp
##########
@@ -164,43 +168,140 @@ std::string OsUtils::userIdToUsername(const std::string 
&uid) {
   return name;
 }
 
-uint64_t OsUtils::getMemoryUsage() {
-#ifdef __linux__
-  static const std::string linePrefix = "VmRSS:";
-  std::ifstream statusFile("/proc/self/status");
+int64_t OsUtils::getCurrentProcessPhysicalMemoryUsage() {
+#if defined(__linux__)
+  static const std::string resident_set_size_prefix = "VmRSS:";
+  std::ifstream status_file("/proc/self/status");
   std::string line;
 
-  while (std::getline(statusFile, line)) {
-    // if line begins with "VmRSS:"
-    if (line.rfind(linePrefix, 0) == 0) {
-      std::istringstream valuableLine(line.substr(linePrefix.length()));
-      uint64_t kByteValue;
-      valuableLine >> kByteValue;
-      return kByteValue * 1024;
+  while (std::getline(status_file, line)) {
+    if (line.rfind(resident_set_size_prefix, 0) == 0) {
+      std::istringstream 
resident_set_size_value(line.substr(resident_set_size_prefix.length()));
+      uint64_t memory_usage_in_kBytes;
+      resident_set_size_value >> memory_usage_in_kBytes;
+      return memory_usage_in_kBytes * 1024;
     }
   }
 
-  throw std::runtime_error("Could not get memory info for current process");
-#endif
-
-#ifdef __APPLE__
+  return -1;
+#elif defined(__APPLE__)
   task_basic_info tInfo;
   mach_msg_type_number_t tInfoCount = TASK_BASIC_INFO_COUNT;
   if (KERN_SUCCESS != task_info(mach_task_self(), TASK_BASIC_INFO, 
(task_info_t)&tInfo, &tInfoCount))
-    throw std::runtime_error("Could not get memory info for current process");
+    return -1;
   return tInfo.resident_size;
-#endif
-
-#ifdef _WIN32
+#elif defined(WIN32)
   PROCESS_MEMORY_COUNTERS pmc;
   if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
-    throw std::runtime_error("Could not get memory info for current process");
+    return -1;
   return pmc.WorkingSetSize;
+#else
+  static_assert(false, "Unsupported platform");

Review comment:
       Thanks thats a valid point, changed it to only warn and give back -1 as 
result in https://github.com/apache/nifi-minifi-cpp/pull/1065

##########
File path: libminifi/src/utils/OsUtils.cpp
##########
@@ -164,43 +168,140 @@ std::string OsUtils::userIdToUsername(const std::string 
&uid) {
   return name;
 }
 
-uint64_t OsUtils::getMemoryUsage() {
-#ifdef __linux__
-  static const std::string linePrefix = "VmRSS:";
-  std::ifstream statusFile("/proc/self/status");
+int64_t OsUtils::getCurrentProcessPhysicalMemoryUsage() {
+#if defined(__linux__)
+  static const std::string resident_set_size_prefix = "VmRSS:";
+  std::ifstream status_file("/proc/self/status");
   std::string line;
 
-  while (std::getline(statusFile, line)) {
-    // if line begins with "VmRSS:"
-    if (line.rfind(linePrefix, 0) == 0) {
-      std::istringstream valuableLine(line.substr(linePrefix.length()));
-      uint64_t kByteValue;
-      valuableLine >> kByteValue;
-      return kByteValue * 1024;
+  while (std::getline(status_file, line)) {
+    if (line.rfind(resident_set_size_prefix, 0) == 0) {
+      std::istringstream 
resident_set_size_value(line.substr(resident_set_size_prefix.length()));
+      uint64_t memory_usage_in_kBytes;
+      resident_set_size_value >> memory_usage_in_kBytes;
+      return memory_usage_in_kBytes * 1024;
     }
   }
 
-  throw std::runtime_error("Could not get memory info for current process");
-#endif
-
-#ifdef __APPLE__
+  return -1;
+#elif defined(__APPLE__)
   task_basic_info tInfo;
   mach_msg_type_number_t tInfoCount = TASK_BASIC_INFO_COUNT;
   if (KERN_SUCCESS != task_info(mach_task_self(), TASK_BASIC_INFO, 
(task_info_t)&tInfo, &tInfoCount))
-    throw std::runtime_error("Could not get memory info for current process");
+    return -1;
   return tInfo.resident_size;
-#endif
-
-#ifdef _WIN32
+#elif defined(WIN32)
   PROCESS_MEMORY_COUNTERS pmc;
   if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
-    throw std::runtime_error("Could not get memory info for current process");
+    return -1;
   return pmc.WorkingSetSize;
+#else
+  static_assert(false, "Unsupported platform");
 #endif
+}
+
+int64_t OsUtils::getSystemPhysicalMemoryUsage() {
+#if defined(__linux__)
+  const std::string available_memory_prefix = "MemAvailable:";
+  const std::string total_memory_prefix = "MemTotal:";
+  std::ifstream meminfo_file("/proc/meminfo");
+  std::string line;
 
-  throw std::runtime_error("getMemoryUsage() is not implemented for this 
platform");
+  minifi::utils::optional<uint64_t> total_memory_kByte;
+  minifi::utils::optional<uint64_t> available_memory_kByte;
+  while ((!total_memory_kByte.has_value() || 
!available_memory_kByte.has_value()) && std::getline(meminfo_file, line)) {
+    if (line.rfind(total_memory_prefix, 0) == 0) {
+      std::istringstream 
total_memory_line(line.substr(total_memory_prefix.length()));
+      total_memory_kByte.emplace(0);
+      total_memory_line >> total_memory_kByte.value();
+    } else if (line.rfind(available_memory_prefix, 0) == 0) {
+      std::istringstream 
available_memory_line(line.substr(available_memory_prefix.length()));
+      available_memory_kByte.emplace(0);
+      available_memory_line >> available_memory_kByte.value();
+    }
+  }
+  if (total_memory_kByte.has_value() && available_memory_kByte.has_value())
+    return (total_memory_kByte.value() - available_memory_kByte.value()) * 
1024;
+
+  return -1;
+#elif defined(__APPLE__)
+  vm_size_t page_size;
+  mach_port_t mach_port = mach_host_self();
+  vm_statistics64_data_t vm_stats;
+  mach_msg_type_number_t count = sizeof(vm_stats) / sizeof(natural_t);
+  if (KERN_SUCCESS == host_page_size(mach_port, &page_size) &&
+      KERN_SUCCESS == host_statistics64(mach_port, HOST_VM_INFO,
+                                      (host_info64_t)&vm_stats, &count)) {
+      uint64_t physical_memory_used = ((int64_t)vm_stats.active_count +
+                               (int64_t)vm_stats.wire_count) *  
(int64_t)page_size;
+      return physical_memory_used;
+  }
+  return -1;
+#elif defined(WIN32)
+  MEMORYSTATUSEX memory_info;
+  memory_info.dwLength = sizeof(MEMORYSTATUSEX);
+  GlobalMemoryStatusEx(&memory_info);
+  DWORDLONG physical_memory_used = memory_info.ullTotalPhys - 
memory_info.ullAvailPhys;
+  return physical_memory_used;
+#else
+  static_assert(false, "Unsupported platform");
+#endif
+}
+
+int64_t OsUtils::getSystemTotalPhysicalMemory() {
+#if defined(__linux__)
+  struct sysinfo memory_info;
+  sysinfo(&memory_info);
+  uint64_t total_physical_memory = memory_info.totalram;
+  total_physical_memory *= memory_info.mem_unit;
+  return total_physical_memory;
+#elif defined(__APPLE__)
+  int mib[2];
+  int64_t total_physical_memory = 0;
+  mib[0] = CTL_HW;
+  mib[1] = HW_MEMSIZE;
+  size_t length = sizeof(int64_t);
+  sysctl(mib, 2, &total_physical_memory, &length, NULL, 0);
+  return total_physical_memory;
+#elif defined(WIN32)
+  MEMORYSTATUSEX memory_info;
+  memory_info.dwLength = sizeof(MEMORYSTATUSEX);
+  GlobalMemoryStatusEx(&memory_info);
+  DWORDLONG total_physical_memory = memory_info.ullTotalPhys;
+  return total_physical_memory;
+#else
+  static_assert(false, "Unsupported platform");
+#endif
 }
 
+std::string OsUtils::getMachineArchitecture() {
+#if defined(__linux__) || defined(__APPLE__)

Review comment:
       changed it in https://github.com/apache/nifi-minifi-cpp/pull/1065

##########
File path: libminifi/src/utils/ProcessCPUUsageTracker.cpp
##########
@@ -0,0 +1,140 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "utils/ProcessCPUUsageTracker.h"
+
+#if defined(__linux__) || defined(__APPLE__)
+#include <sys/times.h>
+#endif

Review comment:
       changed it in https://github.com/apache/nifi-minifi-cpp/pull/1065




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to