You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by sz...@apache.org on 2020/08/04 21:48:38 UTC
[nifi-minifi-cpp] branch main updated: MINIFICPP-1277 - Create
cross-platform API for querying memory usage
This is an automated email from the ASF dual-hosted git repository.
szaszm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git
The following commit(s) were added to refs/heads/main by this push:
new 85990e6 MINIFICPP-1277 - Create cross-platform API for querying memory usage
85990e6 is described below
commit 85990e67cb76c5eb061be73052750cc37b91dbef
Author: Marton Szasz <sz...@gmail.com>
AuthorDate: Tue Aug 4 23:46:37 2020 +0200
MINIFICPP-1277 - Create cross-platform API for querying memory usage
This closes #845
Signed-off-by: Marton Szasz <sz...@gmail.com>
---
libminifi/include/utils/OsUtils.h | 13 +++--
libminifi/src/utils/OsUtils.cpp | 59 +++++++++++++++++++---
.../OsUtils.h => test/unit/MemoryUsageTest.cpp} | 37 ++++----------
3 files changed, 68 insertions(+), 41 deletions(-)
diff --git a/libminifi/include/utils/OsUtils.h b/libminifi/include/utils/OsUtils.h
index 7ae7994..28dcb0a 100644
--- a/libminifi/include/utils/OsUtils.h
+++ b/libminifi/include/utils/OsUtils.h
@@ -26,15 +26,14 @@ namespace minifi {
namespace utils {
namespace OsUtils {
-/**
- * Resolves a user ID to a username
- */
+/// Resolves a user ID to a username
extern std::string userIdToUsername(const std::string &uid);
+/// Returns memory usage in bytes, including shared memory
+uint64_t getMemoryUsage();
+
#ifdef WIN32
-/*
- Resolves common identifiers
-*/
+/// Resolves common identifiers
extern std::string resolve_common_identifiers(const std::string &id);
#endif
} /* namespace OsUtils */
@@ -44,4 +43,4 @@ extern std::string resolve_common_identifiers(const std::string &id);
} /* namespace apache */
} /* namespace org */
-#endif /* LIBMINIFI_INCLUDE_UTILS_OSUTILS_H_ */
+#endif // LIBMINIFI_INCLUDE_UTILS_OSUTILS_H_
diff --git a/libminifi/src/utils/OsUtils.cpp b/libminifi/src/utils/OsUtils.cpp
index a939324..9ac74c0 100644
--- a/libminifi/src/utils/OsUtils.cpp
+++ b/libminifi/src/utils/OsUtils.cpp
@@ -23,20 +23,30 @@
#include "utils/ScopeGuard.h"
-#ifdef WIN32
+#ifdef __linux__
+#include <sstream>
+#endif
+
+#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <Windows.h>
#include <sddl.h>
+#include <psapi.h>
+#include <vector>
+#include <algorithm>
#pragma comment(lib, "Ws2_32.lib")
#else
-#include <arpa/inet.h>
-#include <netdb.h>
#include <pwd.h>
#include <sys/types.h>
+#include <unistd.h>
+#include <fstream>
+
+#endif
-#include <mutex>
+#ifdef __APPLE__
+#include <mach/mach.h>
#endif
namespace org {
@@ -45,7 +55,7 @@ namespace nifi {
namespace minifi {
namespace utils {
-#ifdef WIN32
+#ifdef _WIN32
/*
These are common translations for SIDs in windows
*/
@@ -73,7 +83,7 @@ std::string OsUtils::userIdToUsername(const std::string &uid) {
std::string name;
name = uid;
if (!name.empty()) {
-#ifdef WIN32
+#ifdef _WIN32
const auto resolved_name = resolve_common_identifiers(name);
if (!resolved_name.empty()) {
return resolved_name;
@@ -154,6 +164,43 @@ 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");
+ 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;
+ }
+ }
+
+ throw std::runtime_error("Could not get memory info for current process");
+#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
+ PROCESS_MEMORY_COUNTERS pmc;
+ if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
+ throw std::runtime_error("Could not get memory info for current process");
+ return pmc.WorkingSetSize;
+#endif
+
+ throw std::runtime_error("getMemoryUsage() is not implemented for this platform");
+}
+
} // namespace utils
} // namespace minifi
} // namespace nifi
diff --git a/libminifi/include/utils/OsUtils.h b/libminifi/test/unit/MemoryUsageTest.cpp
similarity index 55%
copy from libminifi/include/utils/OsUtils.h
copy to libminifi/test/unit/MemoryUsageTest.cpp
index 7ae7994..fc2c2a8 100644
--- a/libminifi/include/utils/OsUtils.h
+++ b/libminifi/test/unit/MemoryUsageTest.cpp
@@ -1,4 +1,5 @@
/**
+ *
* 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.
@@ -14,34 +15,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-#ifndef LIBMINIFI_INCLUDE_UTILS_OSUTILS_H_
-#define LIBMINIFI_INCLUDE_UTILS_OSUTILS_H_
-
-#include <string>
-
-namespace org {
-namespace apache {
-namespace nifi {
-namespace minifi {
-namespace utils {
-namespace OsUtils {
-/**
- * Resolves a user ID to a username
- */
-extern std::string userIdToUsername(const std::string &uid);
-#ifdef WIN32
-/*
- Resolves common identifiers
-*/
-extern std::string resolve_common_identifiers(const std::string &id);
-#endif
-} /* namespace OsUtils */
-} /* namespace utils */
-} /* namespace minifi */
-} /* namespace nifi */
-} /* namespace apache */
-} /* namespace org */
+#include "utils/OsUtils.h"
+#include "../TestBase.h"
-#endif /* LIBMINIFI_INCLUDE_UTILS_OSUTILS_H_ */
+TEST_CASE("Test memory usage", "[testmemoryusage]") {
+ std::vector<char> v(30000000);
+ const auto memoryUsage = utils::OsUtils::getMemoryUsage();
+ REQUIRE(memoryUsage > v.size());
+ REQUIRE(memoryUsage < 2 * v.size());
+}