From 6f38a6a711ea0eec2914d8119a676bb8d8a185ba Mon Sep 17 00:00:00 2001 From: Dmitriy Musatkin Date: Fri, 20 Oct 2023 16:21:58 -0700 Subject: [PATCH] doc --- include/aws/common/system_resource_util.h | 12 ++++++++---- source/posix/system_resource_utils.c | 13 +++++++++---- source/windows/system_resource_utils.c | 10 +++++----- tests/CMakeLists.txt | 2 +- tests/system_resource_util_test.c | 12 ++++++------ 5 files changed, 29 insertions(+), 20 deletions(-) diff --git a/include/aws/common/system_resource_util.h b/include/aws/common/system_resource_util.h index df1c456bc..a4a8e4860 100644 --- a/include/aws/common/system_resource_util.h +++ b/include/aws/common/system_resource_util.h @@ -11,14 +11,18 @@ AWS_PUSH_SANE_WARNING_LEVEL AWS_EXTERN_C_BEGIN -struct aws_resource_usage { - size_t maxrss; - size_t page_faults; +struct aws_memory_usage { + size_t maxrss; /* max resident set size in kilobytes since program start */ + size_t page_faults; /* num of page faults since program start */ size_t _reserved[8]; }; -AWS_COMMON_API int aws_resource_usage_for_current_process(struct aws_resource_usage *resource_usage); +/* + * Get memory usage for current process. + * Raises AWS_ERROR_SYS_CALL_FAILURE on failure. +*/ +AWS_COMMON_API int aws_memory_usage_for_current_process(struct aws_memory_usage *memory_usage); AWS_EXTERN_C_END AWS_POP_SANE_WARNING_LEVEL diff --git a/source/posix/system_resource_utils.c b/source/posix/system_resource_utils.c index cc2774643..7863013bb 100644 --- a/source/posix/system_resource_utils.c +++ b/source/posix/system_resource_utils.c @@ -7,8 +7,8 @@ #include -int aws_resource_usage_for_current_process(struct aws_resource_usage *ru) { - AWS_PRECONDITION(ru); +int aws_memory_usage_for_current_process(struct aws_memory_usage *mu) { + AWS_PRECONDITION(mu); struct rusage usage; @@ -17,9 +17,14 @@ int aws_resource_usage_for_current_process(struct aws_resource_usage *ru) { } #if defined(AWS_OS_APPLE) - ru->maxrss = usage.ru_maxrss / 1024; + /* + * For some reason Apple switched to reporting this in bytes instead of KB + * around MacOS 10.6. + * Make it back to KB. Result might be slightly off due to rounding. + */ + mu->maxrss = usage.ru_maxrss / 1024; #else - ru->maxrss = usage.ru_maxrss; + mu->maxrss = usage.ru_maxrss; #endif ru->page_faults = usage.ru_majflt; return AWS_OP_SUCCESS; diff --git a/source/windows/system_resource_utils.c b/source/windows/system_resource_utils.c index 1d63fe701..847ccef94 100644 --- a/source/windows/system_resource_utils.c +++ b/source/windows/system_resource_utils.c @@ -10,11 +10,11 @@ #include -int aws_resource_usage_for_current_process(struct aws_resource_usage *ru) { - AWS_PRECONDITION(resource_usage); +int aws_memory_usage_for_current_process(struct aws_memory_usage *mu) { + AWS_PRECONDITION(mu); HANDLE hProcess = GetCurrentProcess(); - ; + PROCESS_MEMORY_COUNTERS pmc; BOOL ret = GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc)); @@ -24,8 +24,8 @@ int aws_resource_usage_for_current_process(struct aws_resource_usage *ru) { return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); } - ru->maxrss = pmc.PeakWorkingSetSize; - ru->page_faults = pmc.PageFaultCount; + mu->maxrss = pmc.PeakWorkingSetSize; + mu->page_faults = pmc.PageFaultCount; return AWS_OP_SUCCESS; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 907432b58..de072868a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -402,7 +402,7 @@ add_test_case(ring_buffer_acquire_up_to_multi_threaded_test) add_test_case(string_to_log_level_success_test) add_test_case(string_to_log_level_failure_test) -add_test_case(test_resource_usage_maxrss) +add_test_case(test_memory_usage_maxrss) if(NOT ANDROID) add_test_case(test_logging_filter_at_AWS_LL_NONE_s_logf_all_levels) diff --git a/tests/system_resource_util_test.c b/tests/system_resource_util_test.c index f89c79a8f..0d58882d8 100644 --- a/tests/system_resource_util_test.c +++ b/tests/system_resource_util_test.c @@ -7,17 +7,17 @@ #include -static int s_test_resource_usage_maxrss(struct aws_allocator *allocator, void *ctx) { +static int s_test_memory_usage_maxrss(struct aws_allocator *allocator, void *ctx) { (void)allocator; (void)ctx; - struct aws_resource_usage ru; - AWS_ZERO_STRUCT(ru); - ASSERT_SUCCESS(aws_resource_usage_for_current_process(&ru)); + struct aws_resource_usage mu; + AWS_ZERO_STRUCT(mu); + ASSERT_SUCCESS(aws_memory_usage_for_current_process(&mu)); - ASSERT_TRUE(ru.maxrss > 0); + ASSERT_TRUE(mu.maxrss > 0); return 0; } -AWS_TEST_CASE(test_resource_usage_maxrss, s_test_resource_usage_maxrss) \ No newline at end of file +AWS_TEST_CASE(test_memory_usage_maxrss, s_test_memory_usage_maxrss) \ No newline at end of file