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

Add maxrss to ps_memory_full_info() on macOS #189

Merged
merged 3 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ps (development version)

* `ps_memory_full_info()` now contains `maxrss`, the maximum resident set
size for the calling process.

# ps 1.8.1

* ps can now be installed again on unsupported platforms.
Expand Down
18 changes: 11 additions & 7 deletions R/low-level.R
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,8 @@
#' might not have access to some processes that `ps_memory_info()` can
#' query:
#'
#' * `maxrss` maximum resident set size over the process's lifetime. This
#' only works for the calling process, otherwise it is `NA_real_`.
#' * `uss`: Unique Set Size, this is the memory which is unique to a
#' process and which would be freed if the process was terminated right
#' now.
Expand Down Expand Up @@ -608,6 +610,13 @@

ps_memory_full_info <- function(p = ps_handle()) {
assert_ps_handle(p)
info <- ps_memory_info(p)
info[["maxrss"]] <- if (Sys.getpid() == ps_pid(p)) {
.Call(psll_memory_maxrss, p)
} else {
NA_real_
}

Check warning on line 618 in R/low-level.R

View check run for this annotation

Codecov / codecov/patch

R/low-level.R#L613-L618

Added lines #L613 - L618 were not covered by tests

type <- ps_os_type()
if (type[["LINUX"]]) {
match <- function(re) {
Expand All @@ -620,23 +629,18 @@
sum(as.integer(st), na.rm = TRUE) * 1024
}

info <- ps_memory_info(p)
smaps <- .Call(ps__memory_maps, p)
info[["uss"]] <- match("\nPrivate.*:\\s+(\\d+)")
info[["pss"]] <- match("\nPss:\\s+(\\d+)")
info[["swap"]] <- match("\nSwap:\\s+(\\d+)")
info

} else if (type[["MACOS"]]) {
info <- ps_memory_info(p)
info[["uss"]] <- .Call(psll_memory_uss, p)
info

} else if (type[["WINDOWS"]]) {
info <- ps_memory_info(p)
} else if (type[["WINDOWS"]]) {

Check warning on line 640 in R/low-level.R

View check run for this annotation

Codecov / codecov/patch

R/low-level.R#L640

Added line #L640 was not covered by tests
info[["uss"]] <- .Call(psll_memory_uss, p)
info
}
info

Check warning on line 643 in R/low-level.R

View check run for this annotation

Codecov / codecov/patch

R/low-level.R#L643

Added line #L643 was not covered by tests
}

process_signal_result <- function(p, res, err_msg) {
Expand Down
2 changes: 2 additions & 0 deletions man/ps_memory_info.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/api-posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,13 @@
UNPROTECT(1);
return res;
}

SEXP psll_memory_maxrss(SEXP p) {

Check warning on line 482 in src/api-posix.c

View check run for this annotation

Codecov / codecov/patch

src/api-posix.c#L482

Added line #L482 was not covered by tests
struct rusage rusage;
getrusage(RUSAGE_SELF, &rusage);

Check warning on line 484 in src/api-posix.c

View check run for this annotation

Codecov / codecov/patch

src/api-posix.c#L484

Added line #L484 was not covered by tests
#if defined(__APPLE__)
return Rf_ScalarReal(rusage.ru_maxrss);
#else
return Rf_ScalarReal(rusage.ru_maxrss * 1024.0);

Check warning on line 488 in src/api-posix.c

View check run for this annotation

Codecov / codecov/patch

src/api-posix.c#L488

Added line #L488 was not covered by tests
#endif
}
6 changes: 6 additions & 0 deletions src/api-windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,12 @@ SEXP psll_memory_uss(SEXP p) {
return Rf_ScalarInteger(wsCounters.NumberOfPrivatePages * sysinfo.dwPageSize);
}

SEXP psll_memory_maxrss(SEXP p) {
PROCESS_MEMORY_COUNTERS info;
GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
return Rf_ScalarReal(info.PeakWorkingSetSize);
}

SEXP ps__users() {
HANDLE hServer = WTS_CURRENT_SERVER_HANDLE;
WCHAR *buffer_user = NULL;
Expand Down
1 change: 1 addition & 0 deletions src/dummy.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ SEXP psll_connections(SEXP x) { return ps__dummy("ps_connections"); }
SEXP psll_get_nice(SEXP x) { return ps__dummy("ps_get_nice"); }
SEXP psll_set_nice(SEXP x, SEXP y) { return ps__dummy("ps_set_nice"); }
SEXP psll_memory_uss(SEXP p) { return ps__dummy("psll_memory_uss"); }
SEXP psll_memory_maxrss(SEXP p) { return ps__dummy("psll_memory_maxrss"); }
SEXP psll_get_cpu_aff(SEXP p) { return ps__dummy("psll_get_cpu_aff"); }
SEXP psll_set_cpu_aff(SEXP p) { return ps__dummy("psll_set_cpu_aff"); }
SEXP psll_wait(SEXP pps, SEXP timeout) { return ps__dummy("psll_wait"); }
Expand Down
1 change: 1 addition & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ static const R_CallMethodDef callMethods[] = {
{ "psll_cpu_times", (DL_FUNC) psll_cpu_times, 1 },
{ "psll_memory_info", (DL_FUNC) psll_memory_info , 1 },
{ "psll_memory_uss", (DL_FUNC) psll_memory_uss, 1 },
{ "psll_memory_maxrss",(DL_FUNC) psll_memory_maxrss,1 },
{ "psll_send_signal", (DL_FUNC) psll_send_signal , 2 },
{ "psll_suspend", (DL_FUNC) psll_suspend, 1 },
{ "psll_resume", (DL_FUNC) psll_resume, 1 },
Expand Down
1 change: 1 addition & 0 deletions src/ps.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ SEXP psll_num_threads(SEXP p);
SEXP psll_cpu_times(SEXP p);
SEXP psll_memory_info(SEXP p);
SEXP psll_memory_uss(SEXP p);
SEXP psll_memory_maxrss(SEXP p);
SEXP psll_send_signal(SEXP p, SEXP sig);
SEXP psll_suspend(SEXP p);
SEXP psll_resume(SEXP p);
Expand Down
Loading