adamdebreceni commented on a change in pull request #1032:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1032#discussion_r600387294
##########
File path: libminifi/src/utils/OsUtils.cpp
##########
@@ -197,10 +200,117 @@ uint64_t OsUtils::getMemoryUsage() {
throw std::runtime_error("Could not get memory info for current process");
return pmc.WorkingSetSize;
#endif
+ throw std::runtime_error("getCurrentProcessPhysicalMemoryUsage() is not
implemented for this platform");
+}
+
+uint64_t OsUtils::getSystemPhysicalMemoryUsage() {
+#ifdef __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;
+
+ throw std::runtime_error("Could not get memory info");
+#endif
+
+#ifdef __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;
+ }
+ throw std::runtime_error("Could not get memory info");
Review comment:
should we throw from these functions? as I understand we are calling
these while serializing the heartbeat, in which case an unhandled exception
results in no heartbeat (which might not be what we want)
should we put a `static_assert(false, "Unsupported platform")` in the
"else"-branch, we would get compile-time notification if we compile for
unsupported platform and whenever we still could get runtime invalid results
(like here) we should log the error and not send that (nonexistent) information
in the heartbeat (or send some sensible "error-value", -1 or something)
--
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:
[email protected]