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

BUG: Rprofmem() clutters up output when the call stack is empty #42

Closed
HenrikBengtsson opened this issue May 29, 2017 · 3 comments
Closed

Comments

@HenrikBengtsson
Copy link
Owner

HenrikBengtsson commented May 29, 2017

(Extracted from Issue #25)

When R does memory allocations internally, these are also logged with Rprofmem(). These entries have an empty call stack. Due to a bug in the code, the log of such entries lack newlines causing several entries to appear on the same line in the log file. For example,

> Rprofmem()
> x <- integer(1000)
> y <- double(1000)
> Rprofmem(NULL)
> cat(readLines("Rprofmem.out", warn=FALSE), sep="\n")
4040 :"integer"
200 :360 :360 :1064 :new page:8040 :"double"

The lack of newlines for some of the lines makes it unnecessarily hard/tricky to parse the Rprofmem log file.

Solution

Fixing this is very simple; it is just a matter of making sure there are no side effects, which it appears not to be (see below R-devel thread 'RProfmem output format').

$ svn diff src/main/memory.c 
Index: src/main/memory.c
===================================================================
--- src/main/memory.c	(revision 72746)
+++ src/main/memory.c	(working copy)
@@ -3803,7 +3803,6 @@
 
 static void R_OutputStackTrace(FILE *file)
 {
-    int newline = 0;
     RCNTXT *cptr;
 
     for (cptr = R_GlobalContext; cptr; cptr = cptr->nextcontext) {
@@ -3810,13 +3809,12 @@
 	if ((cptr->callflag & (CTXT_FUNCTION | CTXT_BUILTIN))
 	    && TYPEOF(cptr->call) == LANGSXP) {
 	    SEXP fun = CAR(cptr->call);
-	    if (!newline) newline = 1;
 	    fprintf(file, "\"%s\" ",
 		    TYPEOF(fun) == SYMSXP ? CHAR(PRINTNAME(fun)) :
 		    "<Anonymous>");
 	}
     }
-    if (newline) fprintf(file, "\n");
+    fprintf(file, "\n");
 }
 
 static void R_ReportAllocation(R_size_t size)

With the above patch, we get:

> Rprofmem()
> x <- integer(1000)
> y <- double(1000)
> Rprofmem(NULL)
> cat(readLines("Rprofmem.out", warn=FALSE), sep="\n")
4040 :"integer" 
240 :
480 :
472 :
1064 :
new page:
8040 :"double" 

See also

  • This issue has been discussed previously:
  • The profmem package is backward and forward compatible with this bug / fix.

/cc @kalibera, if you still have a few brain cycles to spare after tweaking summaryRprof() (r72743), I'm cc:ing you in the hope that you can fix also this very old bug that seems to be forgotten about over and over.

@HenrikBengtsson HenrikBengtsson changed the title BUG: Rprofmem() clutters up outputs when the call stack is empty BUG: Rprofmem() clutters up output when the call stack is empty May 29, 2017
@HenrikBengtsson
Copy link
Owner Author

UPDATE: This has now been patched in R-devel r72747, with the difference that the newline is no longer outputted by R_OutputStackTrace() itself but the calling functions. We now get:

$ R --vanilla
R Under development (unstable) (2017-05-30 r72747) -- "Unsuffered Consequences"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
[...]
> Rprofmem()
> x <- integer(1000)
> y <- double(1000)
> Rprofmem(NULL)
> cat(readLines("Rprofmem.out", warn=FALSE), sep="\n")
4040 :"integer" 
240 :
480 :
472 :
1064 :
8040 :"double" 
new page:

Thanks @kalibera

@jangorecki
Copy link

@HenrikBengtsson any idea what happened to?

Radford Neal, R-devel thread 'Improved version of Rprofmem', 2011-08-14 (https://stat.ethz.ch/pipermail/r-devel/2011-August/061783.html).

@HenrikBengtsson
Copy link
Owner Author

Yeah, I don't much happened. It's about timing - getting the attention of R core members at the right time and their spare time. I might be worth bringing up again. Please note that I have a separate issue 'WISH: Rprofmem() overhaul' on this; #25.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants