adam-markovics commented on a change in pull request #845:
URL: https://github.com/apache/nifi-minifi-cpp/pull/845#discussion_r458812667



##########
File path: libminifi/src/utils/OsUtils.cpp
##########
@@ -154,6 +160,80 @@ std::string OsUtils::userIdToUsername(const std::string 
&uid) {
   return name;
 }
 
+unsigned long long OsUtils::getMemoryUsage() {
+#ifdef __linux__
+  long resPages;
+  long sharedPages;
+  {
+    std::string ignore;
+    std::ifstream ifs("/proc/self/statm");
+    ifs >> ignore >> resPages >> sharedPages;
+  }
+
+  if (sharedPages > resPages) {
+    throw std::range_error("Shared memory page count ("
+      + std::to_string(sharedPages)
+      + ") should not be larger than resident set size ("
+      + std::to_string(resPages)
+      + "), that includes it"
+    );
+  }
+
+  const long ownPages = resPages - sharedPages;
+  const long pageSize = sysconf(_SC_PAGE_SIZE);
+  return ownPages * pageSize;
+#endif
+
+#ifdef __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 tInfo.resident_size;
+#endif
+
+#ifdef _WIN32
+  const auto hCurrentProcess = GetCurrentProcess();
+
+  PSAPI_WORKING_SET_INFORMATION workingSetSizeInfo;
+  QueryWorkingSet(hCurrentProcess, &workingSetSizeInfo, 
sizeof(workingSetSizeInfo));
+  auto pageCountLimit = workingSetSizeInfo.NumberOfEntries * 2; // twice the 
size for sure fit next time
+  BOOL success = 0;
+
+  // allocate storage
+  size_t storageSize = sizeof(ULONG_PTR) + pageCountLimit * 
sizeof(PSAPI_WORKING_SET_BLOCK);
+  std::vector<char> storage(storageSize);
+  ULONG_PTR* totalPages = nullptr;
+  PSAPI_WORKING_SET_BLOCK* workingSetBlock = nullptr;
+
+  for (int tries = 0; tries < 10 && !success; ++tries) {
+    if (storage.size() != storageSize)
+      storage.resize(storageSize);
+
+    // allocate structured data continuously in storage, Windows only likes it 
this way
+    totalPages = new(storage.data()) ULONG_PTR;
+    workingSetBlock = new(storage.data() + sizeof(ULONG_PTR)) 
PSAPI_WORKING_SET_BLOCK[pageCountLimit];
+
+    // get page information or set number of entries correctly for next try
+    success = QueryWorkingSet(hCurrentProcess, storage.data(), storageSize);

Review comment:
       Done.




----------------------------------------------------------------
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]


Reply via email to