Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic process mem usage functions #1065

Merged
merged 15 commits into from
Oct 23, 2023
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ if (WIN32)
endif ()

list(APPEND PLATFORM_DEFINES WINDOWS_KERNEL_LIB=${WINDOWS_KERNEL_LIB})
list(APPEND PLATFORM_LIBS BCrypt ${WINDOWS_KERNEL_LIB} Ws2_32 Shlwapi)
# PSAPI_VERSION=1 is needed to support GetProcessMemoryInfo on both pre and
# post Win7 OS's.
list(APPEND PLATFORM_DEFINES PSAPI_VERSION=1)
DmitriyMusatkin marked this conversation as resolved.
Show resolved Hide resolved
list(APPEND PLATFORM_LIBS BCrypt ${WINDOWS_KERNEL_LIB} Ws2_32 Shlwapi Psapi)
else ()
file(GLOB AWS_COMMON_OS_HEADERS
"include/aws/common/posix/*"
Expand Down
30 changes: 30 additions & 0 deletions include/aws/common/system_resource_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef AWS_COMMON_SYSTEM_RESOURCE_UTIL_H
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debatable: We already have a system_info header which might be a good place for these functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im on the fence about it. I was originally planning to put it into system_info, but functions here have a slightly different use case - how many resource is my process consuming vs which system im running on. I was also planning to add functions to modify resource limits to this file, which will diverge things even more in future. So im leaning towards a separate header.

#define AWS_COMMON_SYSTEM_RESOURCE_UTIL_H

/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/common/common.h>

AWS_PUSH_SANE_WARNING_LEVEL

AWS_EXTERN_C_BEGIN

struct aws_memory_usage_stats {
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];
};

/*
* Get memory usage for current process.
* Raises AWS_ERROR_SYS_CALL_FAILURE on failure.
*/
AWS_COMMON_API int aws_init_memory_usage_for_current_process(struct aws_memory_usage_stats *memory_usage);

AWS_EXTERN_C_END
AWS_POP_SANE_WARNING_LEVEL

#endif /* AWS_COMMON_SYSTEM_RESOURCE_UTIL_H */
32 changes: 32 additions & 0 deletions source/posix/system_resource_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

#include <aws/common/system_resource_util.h>

#include <sys/resource.h>

int aws_init_memory_usage_for_current_process(struct aws_memory_usage_stats *memory_usage) {
AWS_PRECONDITION(memory_usage);

AWS_ZERO_STRUCT(*memory_usage);
struct rusage usage;

if (getrusage(RUSAGE_SELF, &usage)) {
return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE);
}

#if defined(AWS_OS_APPLE)
/*
* 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.
*/
memory_usage->maxrss = usage.ru_maxrss / 1024;
#else
memory_usage->maxrss = usage.ru_maxrss;
#endif
memory_usage->page_faults = usage.ru_majflt;
return AWS_OP_SUCCESS;
}
31 changes: 31 additions & 0 deletions source/windows/system_resource_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

#include <aws/common/system_resource_util.h>

#include <windows.h>

#include <psapi.h>

int aws_init_memory_usage_for_current_process(struct aws_memory_usage_stats *memory_usage) {
AWS_PRECONDITION(memory_usage);

AWS_ZERO_STRUCT(*memory_usage);
HANDLE hProcess = GetCurrentProcess();

PROCESS_MEMORY_COUNTERS pmc;

BOOL ret = GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc));
CloseHandle(hProcess);

if (!ret) {
return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE);
}

memory_usage->maxrss = pmc.PeakWorkingSetSize;
memory_usage->page_faults = pmc.PageFaultCount;

return AWS_OP_SUCCESS;
}
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ 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_memory_usage_maxrss)

if(NOT ANDROID)
add_test_case(test_logging_filter_at_AWS_LL_NONE_s_logf_all_levels)
add_test_case(test_logging_filter_at_AWS_LL_FATAL_s_logf_all_levels)
Expand Down
22 changes: 22 additions & 0 deletions tests/system_resource_util_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

#include <aws/common/system_resource_util.h>

#include <aws/testing/aws_test_harness.h>

static int s_test_memory_usage_maxrss(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
(void)ctx;

struct aws_memory_usage_stats mu;
ASSERT_SUCCESS(aws_init_memory_usage_for_current_process(&mu));

ASSERT_TRUE(mu.maxrss > 0);

return 0;
}

AWS_TEST_CASE(test_memory_usage_maxrss, s_test_memory_usage_maxrss)
Loading