Linux – How to check that perf events are enabled in Linux kernel, and how to install perf userland as non-root

gentookernellinuxperf-eventsoftware installation

From what I have checked it looks like kernel side of 'perf' subsystem is enabled on computer I work on.

Checking kernel configuration shows the following

$ zgrep "_PERF[_= ]" /proc/config.gz 
CONFIG_HAVE_PERF_EVENTS=y
CONFIG_PERF_EVENTS=y
# CONFIG_PERF_COUNTERS is not set
CONFIG_HAVE_PERF_EVENTS_NMI=y

I also did the check described in perf_events FAQ:

$ cat /proc/sys/kernel/perf_event_paranoid
1

But the perf tool is not installed:

$ perf
-bash: perf: command not found
$ /sbin/perf
-bash: /sbin/perf: No such file or directory
$ /usr/sbin/perf
-bash: /usr/sbin/perf: No such file or directory

Is it possible to install perf userland as an ordinary user, to one's own home directory (for '2.6.36-gentoo-r4' kernel)?

Or do I need to ask administrator of machine in question to install it? More unfortunately dev-util/perf package on Gentoo is masked (blocked) on amd64:

$ emerge --search perf
[...]
*  dev-util/perf [ Masked ]
      Latest version available: 2.6.35_rc4
      Latest version installed: [ Not Installed ]
      Size of files: 73,503 kB
      Homepage:      http://perf.wiki.kernel.org/
      Description:   Userland tools for Linux Performance Counters
      License:       GPL-2

Best Answer

How to install perf userland tool as non-root

  1. Get/find sources for kernel-2.6.36-gentoo-r4 (in Gentoo Linux). The first check from this answer

    Actually, first you should look at /usr/src/linux and see if the kernel sources are still installed. You could just copy them to a directory you can write to.)

    was enough, though instead of copying whole kernel sources I just linked them:

    $ mkdir -p build
    $ cd build
    $ ln -s /usr/src/linux-2.6.36-gentoo-r4
    
  2. Create directory where perf would be built, as I won't be able to write in ~/build/linux-2.6.36-gentoo-r4 directory.

    $ mkdir -p perf
    

    Actually it was not what I did at first... error messages from make were entirely unhelpful at first.

  3. Go to tools/perf directory in kernel sources

    $ cd linux-2.6.36-gentoo-r4/tools/perf
    
  4. Build perf, not forgetting about passing O=<destdir> option to makefile as the directory is not writable (there would be no such problem if I copied rather than symlinked kernel sources).

    $ make O=~/build/perf -k 
    Makefile:565: newt not found, disables TUI support. Please install newt-devel or libnewt-dev
        * new build flags or prefix
        CC ~/build/perf/perf.o
        CC ~/build/perf/builtin-annotate.o
        [...]
        CC ~/build/perf/util/scripting-engines/trace-event-python.o
        CC ~/build/perf/scripts/python/Perf-Trace-Util/Context.o
        AR ~/build/perf/libperf.a
        LINK ~/build/perf/perf
    ~/build/perf/libperf.a(trace-event-perl.o): In function `define_flag_value':
    ~/build/linux-2.6.36-gentoo-r4/tools/perf/util/scripting-engines/trace-event-perl.c:127: undefined reference to `PL_stack_sp'
    ~/build/linux-2.6.36-gentoo-r4/tools/perf/util/scripting-engines/trace-event-perl.c:131: undefined reference to `Perl_push_scope'
    [...]
    ~/build/perf/libperf.a(trace-event-python.o): In function `handler_call_die':
    ~/build/linux-2.6.36-gentoo-r4/tools/perf/util/scripting-engines/trace-event-python.c:53: undefined reference to `PyErr_Print'
    [...]
    collect2: ld returned 1 exit status
    make: *** [/home/narebski/build/perf/perf] Error 1
        GEN perf-archive
    make: Target `all' not remade because of errors.
    
  5. Google for "undefined reference to `Perl_push_scope'". Find Fail to install perf on slackware 13.1 on unix.stackexchange.com. Follow the advice in self answer, or to be more excat the diagnosis:

    $ make O=~/build/perf -k NO_LIBPERL=1 NO_LIBPYTHON=1
    Makefile:565: newt not found, disables TUI support. Please install newt-devel or libnewt-dev
        * new build flags or prefix
        CC ~/build/perf/perf.o
        CC ~/build/perf/builtin-annotate.o
        [...]
        CC ~/build/perf/util/probe-finder.o
        AR ~/build/perf/libperf.a
        LINK ~/build/perf/perf
        GEN perf-archive
    

    Note that it is workaround rather than a solution (I have libperl.so).

  6. Check Makefile for default install destination: its $(HOME). Install perf in one's own home directory:

    $ make O=~/build/perf -k NO_LIBPERL=1 NO_LIBPYTHON=1 install
    Makefile:565: newt not found, disables TUI support. Please install newt-devel or libnewt-dev
        GEN perf-archive
    install -d -m 755 '~/bin'
    install ~/build/perf/perf '~/bin'
    [...]
    install scripts/python/bin/* -t '~/libexec/perf-core/scripts/python/bin'
    
  7. Check that ~/bin is in PATH

  8. Check that perf works correctly (don't forget to cd in writable directory):

    $ cd
    $ perf record -f -- sleep 10
    [ perf record: Woken up 1 times to write data ]
    [ perf record: Captured and wrote 0.001 MB perf.data (~61 samples) ]
    

The output is a bit redacted, replacing my home directory with ~.

Related Question