From ripley at stats.ox.ac.uk  Tue Feb 21 18:40:19 2006
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Tue, 21 Feb 2006 17:40:19 +0000 (GMT)
Subject: [Rd] profiling C code
In-Reply-To: <Pine.LNX.4.64.0602210720001.30643@gannet.stats.ox.ac.uk>
References: <39B6DDB9048D0F4DAD42CB26AAFF0AFAFED82C@usctmx1106.merck.com>
	<Pine.LNX.4.64.0602210720001.30643@gannet.stats.ox.ac.uk>
Message-ID: <Pine.LNX.4.64.0602211551440.11480@gannet.stats.ox.ac.uk>

Some more information.

1) Whether gprof works is system-specific.  Solaris says in man gprof

   64-bit profiling
      64-bit profiling may be used freely with dynamically  linked
      executables,  and profiling information is collected for the
      shared objects if the objects are  compiled  for  profiling.

   32-bit profiling
      32-bit profiling may be used with dynamically linked execut-
      ables, but care must be applied. In 32-bit profiling, shared
      objects cannot be profiled with gprof.

As usual, Linux seems not to give such details (or if it does, please 
point me at them).

2) Here is an example of using sprof.  I needed to be root to create 
some directories:

% mkdir -p /var/tmp//users/ripley/R/R-devel/library/stats/libs
% chown -R ripley:bdr /var/tmp//users/ripley
% setenv LD_PROFILE /users/ripley/R/R-devel/library/stats/libs/stats.so

% R
> example(smooth.spline)
> q()

% sprof /users/ripley/R/R-devel/library/stats/libs/stats.so \
/var/tmp/users/ripley/R/R-devel/library/stats/libs/stats.so.profile

Flat profile:

Each sample counts as 0.01 seconds.
   %   cumulative   self              self     total
  time   seconds   seconds    calls  us/call  us/call  name
100.00      0.01     0.01     2914     3.43           bsplvb_
   0.00      0.01     0.00     2261     0.00           bvalue_
   0.00      0.01     0.00     2186     0.00           bsplvd_
   0.00      0.01     0.00       91     0.00           sinerp_
   0.00      0.01     0.00       91     0.00           sslvrg_
   0.00      0.01     0.00        9     0.00           sbart_
   0.00      0.01     0.00        9     0.00           sgram_
   0.00      0.01     0.00        9     0.00           stxwx_
...

If you are running Linux, Torsten's suggestion of oprofile is a good one, 
provided you have root access (it seems to need it).  So I used in a root 
terminal (running the same example in another window in the middle)

% opcontrol --no-vmlinux
% opcontrol --start
.... run things ...
% opreport -l /users/ripley/R/R-devel/bin/exec/R
% opreport -l /users/ripley/R/R-devel/library/stats/libs/stats.so
...
(No unit mask) count 100000
samples  %        symbol name
6        31.5789  bvalue_
4        21.0526  bsplvb_
3        15.7895  bsplvd_
2        10.5263  sbart_
2        10.5263  sslvrg_
1         5.2632  anonymous symbol from section .plt
1         5.2632  stxwx_
% opannotate -s /users/ripley/R/R-devel/library/stats/libs/stats.so
[Problem, root cannot read my source files]
% opcontrol --shutdown

If there is a way to use this without root access I would like to know it, 
as only a few very privileged users get that here, and I am not going to 
open up root access to my private file server.

Since it does look worthwhile collecting this sort of information together 
for R-exts, please add further contributions (to the list or direct to 
me).


On Tue, 21 Feb 2006, Prof Brian Ripley wrote:

> On Mon, 20 Feb 2006, Liaw, Andy wrote:
>
>> The last time I tried I didn't have much luck.  The gprof manual I could
>> find seems to indicate that it can not profile code that are dynamically
>> loaded.  (I was trying on Linux.)  The R source seems to hint otherwise.
>
> grof is not mentioned in any of the R manuals, so what are your referring
> to?  (R-admin talks about `to compile a profiling version of R', not of
> shared libraries.)
>
>> I'd very much appreciate pointers as well.
>
> You need to use sprof rather than gprof.  Here's one relevant thread:
>
> http://tolstoy.newcastle.edu.au/~rking/R/devel/05/02/2351.html
>
> and I am pretty sure there have been others.
>
> Linux has very sketchy info on sprof.  However,
>
> http://people.redhat.com/drepper/dsohowto.pdf
>
> has some.  I've thought about putting something about this in the
> R-exts manual
>
>
>>
>> Andy
>>
>> From: Ross Boylan
>>>
>>> Does anyone have any advice about profiling C/C++ code in a package
>>> under R?  Does R need to be built specially for this to work?
>>>
>>> The FAQ has some entries about profiling but they cover R level
>>> profiling; I'm try to get at the C++ code I've written that is called
>>> from R.
>>>
>>> Primary target is Mac OS X.
>>>
>>> Thanks.
>>>
>>> Ross Boylan
>>>
>>> ______________________________________________
>>> R-devel at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>>
>>>
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] profiling C code
Prof Brian Ripley