How to do it...

A kernel profiling session workflow is as follows:

  1. Create a profiling session with the following:
# lttng create test-session
Spawning a session daemon
Session test-session created.
Traces will be written in /home/root/lttng-traces/test-session-20171213-201520  
  1. Enable the events you want to trace with the following:
# lttng enable-event --kernel sched_switch,sched_process_fork
Kernel event sched_switch created in channel channel0
Kernel event sched_process_fork created in channel channel0  
  1. You can get a list of the available kernel events with the following:
# lttng list --kernel  

This corresponds to the static tracepoint events available in the Linux kernel.

  1. Now, you are ready to start sampling profiling data:
# lttng start
Tracing started for session test-session  
  1. Run the workload you want to profile:
# ping -c 1 192.168.1.1  
  1. When the command finishes or is interrupted, stop the gathering of profiling data:
# lttng stop
Waiting for data availability. Tracing stopped for session test-session
  1. Finally, destroy the profiling session using the following command. Note that this keeps the tracing data and only destroys the session:
# lttng destroy
Session test-session destroyed  
  1. To view the profiling data so that it is readable by humans, start babeltrace with:
# babeltrace /home/root/lttng-traces/test-session-20171213-201520  

The profiling data can also be copied to the host to be analyzed.

User space applications and libraries need to be instrumented so that they can be profiled. This is done by linking them with the liblttng-ust and the libdl libraries.

Applications can then make use of the tracef() function call, which has the same format as printf(), to output traces. For example, to instrument the example helloworld.c application we saw in previous chapters, modify the source in meta-custom/recipes-example/helloworld/helloworld-1.0/helloworld.c as follows:

#include <stdio.h>                                                               
#include <lttng/tracef.h>                                                        
                                                                                 
int main(void)                                                                   
{                                                                                
        printf("Hello World");                                                   
        tracef("I said: %s", "Hello World");                                     
        return 0;                                                                
}   

Modify its Yocto recipe in meta-custom/recipes-example/helloworld/helloworld_1.0.bb as follows:

DESCRIPTION = "Simple helloworld application" 
SECTION = "examples" 
LICENSE = "MIT" 
LIC_FILES_CHKSUM = 
"file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302" SRC_URI = "file://helloworld.c" DEPENDS = "lttng-ust" S = "${WORKDIR}" do_compile() { ${CC} ${LDFLAGS} helloworld.c -o helloworld -llttng-ust -ldl } do_install() { install -d ${D}${bindir} install -m 0755 helloworld ${D}${bindir} }

Then build the package, copy it to the target, and start a profiling session as follows:

  1. Create a profiling session by executing the following command:
# lttng create test-user-session
Spawning a session daemon
Session test-user-session created.
Traces will be written in /home/root/lttng-traces/test-user-session-20171214-213957  
  1. Enable the events you want to profile—in this case, all the user space events:
# lttng enable-event -u -a
All UST events are enabled in channel channel0  
  1. Start to gather profiling data:
# lttng start
Tracing started for session test-user-session  
  1. Run the workload—in this case, the instrumented hello world example program:
# helloworld
Hello World  
  1. Once it finishes, stop gathering data:
# lttng stop
Waiting for data availability.
Tracing stopped for session test-user-session  
  1. Without destroying the session, you can inspect the recorded events by executing:
# lttng view
[21:40:38.250606174] (+0.004467000) wandboard lttng_ust_tracef:event: { cpu_id = 1 }, { _msg_length = 19, msg = "I said: Hello World" }  
  1. Finally, you can destroy the profiling session:
# lttng destroy test-user-session
Session test-user-session destroyed
  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset