Skip to content

Commit

Permalink
doc
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyMusatkin committed Oct 20, 2023
1 parent d66cf9d commit 6f38a6a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
12 changes: 8 additions & 4 deletions include/aws/common/system_resource_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions source/posix/system_resource_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

#include <sys/resource.h>

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;

Expand All @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions source/windows/system_resource_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
#include <psapi.h>


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));
Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions tests/system_resource_util_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@

#include <aws/testing/aws_test_harness.h>

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)
AWS_TEST_CASE(test_memory_usage_maxrss, s_test_memory_usage_maxrss)

0 comments on commit 6f38a6a

Please sign in to comment.