Skip to contentSkip to conversation

Trace code and documentation

PATCH In review David J. Wilder 2 participants 3 messages Feb 26, 2008 – Feb 29, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 2/3 Trace code and documentation

Trace - Provides tracing primitives

---
 Documentation/trace.txt |  166 ++++++++++++++
 include/linux/trace.h   |   99 +++++++++
 lib/Kconfig             |    9 +
 lib/Makefile            |    2 +
 lib/trace.c             |  563 +++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 839 insertions(+), 0 deletions(-)
Documentation/trace.txt new file +166 -0
new file mode 100644
index 0000000..5d966d3
@@ -0,0 +1,166 @@
+Trace Setup and Control
+=======================
+In the kernel, the trace interface provides a simple mechanism for
+starting and managing data channels (traces) to user space.  The
+trace interface builds on the relay interface.  For a complete
+description of the relay interface, please see:
+Documentation/filesystems/relay.txt.
+
+The trace interface provides a single layer in a complete tracing
+application.  Trace provides a kernel API that can be used for the setup
+and control of tracing channels.  User of trace must provide a data layer
+responsible for formatting and writing data into the trace channels.
+
+A layered approach to tracing
+=============================
+A complete kernel tracing application consists of a data provider and
+a data consumer.  Both provider and consumer contain three layers; each
+layer works in tandem with the corresponding layer in the opposite side.
+The layers are represented in the following diagram.
+
+Provider Data layer
+	Formats raw trace data and provides data-related service.
+	For example, adding timestamps used by consumer to sort data.
+
+Provider Control layer
+	Provided by the trace interface, this layer creates trace channels
+	and informs the data layer and consumer of the current state
+	of the trace channels.
+
+Provider Buffering layer
+	Provided by relay. This layer buffers data in the
+	kernel for consumption by the consumer's buffer
+	layer.
+
+Provider (in-kernel facility)
+-----------------------------------------------------------------------------
+Consumer (user application)
+
+
+Consumer Buffer layer
+	Reads/consumes data from the provider's data buffers.
+
+Consumer Control layer
+	Communicates to the provider's control layer to control the state
+	of the trace channels.
+
+Consumer Data layer
+	Sorts and formats data as provided by the provider's data layer.
+
+The provider is coded as a kernel facility.  The consumer is coded as
+a user application.
+
+
+Trace - Features
+================
+Trace exploits services and features provided by relay.  These features
+are:
+- The creation and destruction of relay channels.
+- Buffer management.  Overwrite or non-overwrite modes can be selected
+  as well as global or per-CPU buffering.
+
+Overwrite mode can be called "flight recorder mode".  Flight recorder
+mode is selected by setting the TRACE_FLIGHT_CHANNEL flag when
+creating trace channels.  In flight mode when a tracing buffer is
+full, the oldest records in the buffer will be discarded to make room
+as new records arrive. In the default non-overwrite mode, new records
+may be written only if the buffer has room.  In either case, to
+prevent data loss, a user space reader must keep the buffers
+drained. Trace provides a means to detect the number of records that
+have been dropped due to a buffer-full condition (non-overwrite mode
+only).
+
+When per-CPU buffers are used, relay creates one debugfs file for each
+running CPU.  The user-space consumer of the data is responsible for
+reading the per-CPU buffers and collating the records presumably using
+a time stamp or sequence number included in the trace records.  The
+use of global buffers eliminates this extra work of sequencing
+records; however the provider's data layer must hold a lock when
+writing records.  The lock prevents writers running on different CPUs
+from overwriting each other's data.  However, buffering may be slower
+because writes to the buffer are serialized. Global buffering is
+selected by setting the TRACE_GLOBAL_CHANNEL flag when creating trace
+channels.
+
+Trace User Interface
+===================
+When a trace channel is created and started, the following
+directories and files are created in the root of the mounted debugfs.
+
+/debug (root of the debugfs)
+	/<trace-root-dir>
+		/<trace-name>
+			trace[0...N-1]     Per-CPU trace data, one
+					   file per CPU.
+
+			state		   Start or stop tracing by
+					   by writing the strings
+					   "start" or "stop" to this
+					   file. Read the file to get the
+					   current state.
+
+			dropped		   The number of records dropped
+					   due to a full-buffer condition,
+					   for non-TRACE_FLIGHT_CHANNELs
+					   only.
+
+			rewind		   Trigger a rewind by writing
+					   to this file.  i.e. start
+					   next read at the beginning
+					   again. Only available for
+					   TRACE_FLIGHT_CHANNELS.
+
+
+			nr_sub		   Number of sub-buffers
+					   in the channel.
+
+			sub_size	   Size of sub-buffers in
+					   the channel.
+
+Trace data is gathered from the trace[0...N-1] files using one of the
+available interfaces provided by relay.
+
+When using the read(2) interface, as data is read it is marked as
+consumed by the relay subsystem.  Therefore, subsequent reads will
+only return unconsumed data.
+
+Trace Kernel API
+===============
+An overview of the trace Kernel API is now given. More details of the
+API can be found in linux/trace.h.
+
+The steps a kernel data provider takes to utilize the trace interface are:
+1) Set up a trace channel - trace_setup()
+2) Start the trace channel - trace_start()
+3) Write one or more trace records into the channel (using the relay API).
+
+   Important: When writing a trace record the provider must insure that
+   preemption is disabled and that trace state is  set to "running". A
+   typical function used to write records into a trace channel should
+   follow the following semantics:
+
+	rcu_read_lock();	    // disables preemption
+	if (trace_running(trace)){
+		relay_write(....); // use any available relay data
+				   // function
+	}
+	rcu_read_unlock();	   // enables preemption
+
+4) Stop and start tracing as desired - trace_start()/trace_stop()
+5) Destroy the trace channel and underlying relay channel -
+   trace_cleanup().
+
+Kernel Configuration
+--------------------
+To use trace, configure your kernel with CONFIG_TRACE=y. Trace depends on 
+both CONFIG_RELAY and CONFIG_DEBUG_FS, these will be automatically configured
+when CONFIG_TRACE is selected (if not already configured).
+
+Credits
+=======
+Trace is adapted from blktrace authored by Jens Axboe ([email protected]).
+
+Major contributions were made by:
+Tom Zanussi <[email protected]>
+Martin Hunt <[email protected]>
+David Wilder <[email protected]>
include/linux/trace.h new file +99 -0

99 lines not on this page · raw message

lib/Kconfig +9 -0
index ba3d104..ad19f87 100644
@@ -141,4 +141,13 @@ config HAS_DMA
 config CHECK_SIGNATURE
 	bool
 
+config TRACE
+	bool "Trace setup and control"
+	depends on RELAY && DEBUG_FS
+	help
+	  This option provides support for the setup, teardown and control
+	  of tracing channels from kernel code.  It also provides trace
+	  information and control to userspace via a set of debugfs control
+	  files.  If unsure, say N.
+
 endmenu
lib/Makefile +2 -0
index 92f8f97..a78ea9c 100644
@@ -72,6 +72,8 @@ lib-$(CONFIG_GENERIC_BUG) += bug.o
 
 obj-$(CONFIG_PROFILE_LIKELY) += likely_prof.o
 
+obj-$(CONFIG_TRACE) += trace.o
+
 hostprogs-y	:= gen_crc32table
 clean-files	:= crc32table.h
 
lib/trace.c new file +563 -0

563 lines not on this page · raw message

maintainer
#1 lore raw

lib/trace.c line 76

Quoted 6 lines · On Tue, 26 Feb 2008 14:38:48 -0800 "David J. Wilder" <[email protected]> wrote:
lib/trace.c new file +6 -0
+	if (strncmp(buf, "start", strlen("start")) == 0) {
+		ret = trace_start(trace);
+		if (ret)
+			return ret;
+	} else if (strncmp(buf, "stop", strlen("stop")) == 0)
+		trace_stop(trace);

Does gcc manage to avoid the pointless strlen() calls here?

It's a bit untidy that you can write "startfoo" in there and the kernel will happily accept it.

#2 lore raw
Quoted 11 lines · Andrew Morton

lib/trace.c line 76

Quoted 6 lines · On Tue, 26 Feb 2008 14:38:48 -0800 "David J. Wilder" <[email protected]> wrote:
lib/trace.c new file +6 -0
+	if (strncmp(buf, "start", strlen("start")) == 0) {
+		ret = trace_start(trace);
+		if (ret)
+			return ret;
+	} else if (strncmp(buf, "stop", strlen("stop")) == 0)
+		trace_stop(trace);

Does gcc manage to avoid the pointless strlen() calls here?

Quoted 2 lines

It's a bit untidy that you can write "startfoo" in there and the kernel will happily accept it.

I agree. I will change it to something like this in my next update.

buf[count-1] = '\0';

if (strcmp(buf, "start") == 0) {
	ret = trace_start(trace);
	if (ret)
		return ret;
} else if (strcmp(buf, "stop") == 0)
	trace_stop(trace);
else
	return -EINVAL;