Skip to content

Commit

Permalink
get rss as well
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyMusatkin committed Oct 30, 2023
1 parent a0689cf commit c104236
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
4 changes: 3 additions & 1 deletion include/aws/common/system_resource_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ 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];
size_t rss;

size_t _reserved[7];
};

/*
Expand Down
39 changes: 33 additions & 6 deletions source/posix/system_resource_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,23 @@
*/

#include <aws/common/system_resource_util.h>
#include <aws/common/logging.h>

#include <sys/resource.h>

#if defined(AWS_OS_APPLE)
#include <mach/mach.h>
#else
#include <aws/common/file.h>
#include <unistd.h>
#endif

/*
* TODO:
* use mallinfo2 to get malloc view into how much is allocated? seems missing on
* anything non-glibc
*/

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

Expand All @@ -18,14 +32,27 @@ int aws_init_memory_usage_for_current_process(struct aws_memory_usage_stats *mem
}

#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;
struct mach_task_basic_info info;
mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &infoCount) != KERN_SUCCESS) {
return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE);
}

memory_usage->maxrss = (size_t)info.resident_size_max;
memory_usage->rss = (size_t)info.resident_size;
#else
FILE* statm = aws_fopen_safe("/proc/self/statm", "r");
if (statm == NULL) {
return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE);
}

long rss = 0; /* Note: procfs specifies it as long*/
if (fscanf(statm, "%*s %ld", &rss) != 1) {
return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE);
}

memory_usage->maxrss = usage.ru_maxrss;
memory_usage->rss = rss;
#endif
memory_usage->page_faults = usage.ru_majflt;
return AWS_OP_SUCCESS;
Expand Down
1 change: 1 addition & 0 deletions source/windows/system_resource_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ int aws_init_memory_usage_for_current_process(struct aws_memory_usage_stats *mem
}

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

return AWS_OP_SUCCESS;
Expand Down

0 comments on commit c104236

Please sign in to comment.