| 1 |
#' Read an Rprofmem log file |
| 2 |
#' |
| 3 |
#' Reads and parses an Rprofmem log file that was created by |
| 4 |
#' [utils::Rprofmem()]. |
| 5 |
#' |
| 6 |
#' @param pathname The Rprofmem log file to be read. |
| 7 |
#' |
| 8 |
#' @param as Specifies in what format data should be returned. |
| 9 |
#' If `"raw"`, the line content of the file is returned as is |
| 10 |
#' (as a character vector). |
| 11 |
#' If `"fixed"`, as `"raw"` but with missing newlines added to lines with empty |
| 12 |
#' stack calls that may be introduced in R (< 3.5.0) (see Ref. 1). |
| 13 |
#' If `"Rprofmem"`, the collected Rprofmem data is fully |
| 14 |
#' parsed into bytes and call stack information. |
| 15 |
#' |
| 16 |
#' @param drop Number of levels to drop from the top of the call stack. |
| 17 |
#' |
| 18 |
#' @param ... Not used |
| 19 |
#' |
| 20 |
#' @return An `Rprofmem` data.frame or a character vector (if `as` is `"raw"` |
| 21 |
#' or `"fixed"`). |
| 22 |
#' An `Rprofmem` data.frame has columns `what`, `bytes`, and `trace`, with: |
| 23 |
#' |
| 24 |
#' * `what`: (character) type of memory event; |
| 25 |
#' either `"alloc"` or `"new page"` |
| 26 |
#' * `bytes`: (numeric) number of bytes allocated or `NA_real_` |
| 27 |
#' (when `what` is `"new page"`) |
| 28 |
#' * `trace`: (list of character vectors) zero or more function names |
| 29 |
#' |
| 30 |
#' @example incl/readRprofmem.R |
| 31 |
#' |
| 32 |
#' @references |
| 33 |
#' Ref. 1: \url{https://github.com/HenrikBengtsson/Wishlist-for-R/issues/25} |
| 34 |
#' |
| 35 |
#' @export |
| 36 |
#' @importFrom utils file_test |
| 37 |
readRprofmem <- function(pathname, as = c("Rprofmem", "fixed", "raw"), drop = 0L, ...) { |
| 38 |
stop_if_not(file_test("-f", pathname)) |
| 39 |
as <- match.arg(as) |
| 40 |
drop <- as.integer(drop) |
| 41 |
stop_if_not(length(drop) == 1, drop >= 0) |
| 42 |
|
| 43 |
## Read raw |
| 44 |
bfr <- readLines(pathname, warn=FALSE) |
| 45 |
if (as == "raw") return(bfr) |
| 46 |
|
| 47 |
|
| 48 |
## WORKAROUND: Add newlines for entries with empty call stacks |
| 49 |
## https://github.com/HenrikBengtsson/Wishlist-for-R/issues/25 |
| 50 |
pattern <- "^(new page|[0-9]+)[ ]?:(new page|[0-9]+)[ ]?:" |
| 51 |
while(any(grepl(pattern, bfr))) { |
| 52 |
bfr <- gsub(pattern, "\\1 :\n\\2 :", bfr) |
| 53 |
bfr <- unlist(strsplit(bfr, split="\n", fixed=TRUE)) |
| 54 |
} |
| 55 |
|
| 56 |
if (as == "fixed") return(bfr) |
| 57 |
|
| 58 |
if (getOption("profmem.debug", FALSE)) print(bfr) |
| 59 |
|
| 60 |
## Parse Rprofmem results |
| 61 |
pattern <- "^([0-9]+|new page)[ ]?:(.*)" |
| 62 |
res <- lapply(bfr, FUN=function(x) { |
| 63 |
bytes <- gsub(pattern, "\\1", x) |
| 64 |
what <- rep("alloc", times = length(x)) |
| 65 |
idxs <- which(bytes == "new page") |
| 66 |
if (length(idxs) > 0) { |
| 67 |
what[idxs] <- "new page" |
| 68 |
bytes[idxs] <- "" # Will become NA below w/out warning |
| 69 |
} |
| 70 |
bytes <- as.numeric(bytes) |
| 71 |
|
| 72 |
trace <- gsub(pattern, "\\2", x) |
| 73 |
trace <- gsub('" "', '", "', trace, fixed=TRUE) |
| 74 |
trace <- sprintf("c(%s)", trace) |
| 75 |
|
| 76 |
trace <- eval(parse(text=trace), enclos = baseenv()) |
| 77 |
trace <- trace[seq_len(max(0L, length(trace)-drop))] |
| 78 |
|
| 79 |
list(what = what, bytes = bytes, trace = trace) |
| 80 |
}) |
| 81 |
|
| 82 |
if (length(res) == 0) { |
| 83 |
what <- character(0L) |
| 84 |
bytes <- numeric(0L) |
| 85 |
traces <- list() |
| 86 |
} else { |
| 87 |
what <- unlist(lapply(res, FUN=function(x) x$what), use.names=FALSE) |
| 88 |
bytes <- unlist(lapply(res, FUN=function(x) x$bytes), use.names=FALSE) |
| 89 |
traces <- lapply(res, FUN=function(x) x$trace) |
| 90 |
} |
| 91 |
res <- data.frame(what = what, bytes = bytes, stringsAsFactors = FALSE) |
| 92 |
res$trace <- traces |
| 93 |
bfr <- bytes <- traces <- NULL |
| 94 |
|
| 95 |
class(res) <- c("Rprofmem", class(res)) |
| 96 |
|
| 97 |
## Sanity check |
| 98 |
stop_if_not(all(c("what", "bytes", "trace") %in% names(res))) |
| 99 |
|
| 100 |
res |
| 101 |
} ## readRprofmem() |