Re: Unified tracing buffer

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Mathieu Desnoyers
Date: Monday, September 22, 2008 - 11:45 am

* Steven Rostedt (rostedt@goodmis.org) wrote:

Hi Steven,

As I expressed above, this is merely one way I propose data could be
exported to user-space. If you have other simpler design ideas in mind,
I look forward to hear them so we can discuss the technical difficulties
associated with that kind of exercice : sending binary data across the
kernel-userspace boundary.

See below for comments.


The scheme you propose here is based on a few inherent assumptions :

- You assume ring_buffer_reserve() and ring_buffer_commit() are static
  inline and thus does not turn into function calls.
- You assume these are small enough so they can be inlined without
  causing L1 insn cache trashing when tracing is activated.
- You therefore assume they use a locking scheme that lets them be
  really really compact (e.g. interrupt disable and spin lock).
- You assume that the performance impact of doing a function call is
  bigger than the impact of locking, which is false by at least a factor
  10.

Interrupt disable and spin locks are _really_ slow. So I think putting
the function call concern up front here is really a matter of premature
optimization gone wrong.

I've got burned in the past history of LTTng. The first versions has a
code generator which created specialized code to serialize the
information into the buffers, exactly like you propose to do. But the
overall impact on kernel code size ended up being too big because we
have to repeat all the code to deal with the buffers for every different
type.

However, I think there might be a way to satisfy us both. An information
source like dynamic function trace happen to fit in a particular
use-case where one single execution site is used to format the data
received as parameter for a _lot_ of instrumented sites, and the type
and event names happen to be the same everywhere. This would therefore
benefit widely of having the capability to write directly into the
buffers.

The thing is that I would like ftrace to expose the types it expects to
write into the trace buffers so a generic trace buffer userspace
consumer could read it.

One way to do it, which would let you write data directly into the
buffers, would be something like : (before anyone says "that's many
lines of code", please compile it and look at the assembly result. A lot
of this translates in precomputed values, especially for the event size
computation). (And no, the following code has not been compile-tested)

include/linux/someheader.h :

/* Calculate the offset needed to align the type */
static inline unsigned int var_align(size_t align_drift,
                 size_t size_of_type)
{
        size_t alignment = min(sizeof(void *), size_of_type);
        return ((alignment - align_drift) & (alignment-1));
}

kernel/trace/ftrace.c :

/*
 * the following macro would only do the "declaration" part of the
 * markers, without doing all the function call stuff.
 */
DECLARE_MARKER(function_entry,
  "pid %d pc %d flags %lu func 0x%lX parent 0x%lX");

void ftrace_mcount(unsigned long ip, unsigned long parent_ip)
{
  size_t ev_size = 0;
  char *buffer;

  /*
   * We assume event payload aligned on sizeof(void *).
   * Event size calculated statically.
   */
  ev_size += sizeof(int);
  ev_size += var_align(ev_size, sizeof(int));
  ev_size += sizeof(int);
  ev_size += var_align(ev_size, sizeof(unsigned long));
  ev_size += sizeof(unsigned long);
  ev_size += var_align(ev_size, sizeof(unsigned long));
  ev_size += sizeof(unsigned long);
  ev_size += var_align(ev_size, sizeof(unsigned long));
  ev_size += sizeof(unsigned long);

  /*
   * Now reserve space and copy data.
   */
  buffer = ring_buffer_reserve(func_event_id, ev_size);
  /* Write pid */
  *(int *)buffer = current->pid;
  buffer += sizeof(int);

  /* Write pc */
  buffer += var_align(buffer, sizeof(int));
  *(int *)buffer = preempt_count();
  buffer += sizeof(int);

  /* Write flags */
  buffer += var_align(buffer, sizeof(unsigned long));
  *(unsigned long *)buffer = local_irq_flags();
  buffer += sizeof(unsigned long);

  /* Write func */
  buffer += var_align(buffer, sizeof(unsigned long));
  *(unsigned long *)buffer = func;
  buffer += sizeof(unsigned long);

  /* Write parent */
  buffer += var_align(buffer, sizeof(unsigned long));
  *(unsigned long *)buffer = parent;
  buffer += sizeof(unsigned long);

  ring_buffer_commit(buffer, ev_size);
}


Would that be suitable for you ?

We could also think of passing the function pointer of the bin to ascii
converter to DECLARE_MARKER(), such as :

void function_entry_show(struct seq_file *m, char *buffer);

DECLARE_MARKER(function_entry, function_entry_show,
  "pid %d pc %d flags %lu func 0x%lX parent 0x%lX");

void function_entry_show(struct seq_file *m, char *buffer)
{
  /* Read pid */
  seq_printf(m, "pid = %d ", *(int *)buffer);
  buffer += sizeof(int);

  /* Read pc */
  buffer += var_align(buffer, sizeof(int));
  seq_printf(m, "pc = %d ", *(int *)buffer);
  buffer += sizeof(int);

  /* Read flags */
  buffer += var_align(buffer, sizeof(unsigned long));
  seq_printf(m, "flags = %lu ", *(unsigned long *)buffer);
  buffer += sizeof(unsigned long);

  /* Read func */
  buffer += var_align(buffer, sizeof(unsigned long));
  seq_printf(m, "func = 0x%lX ", *(unsigned long *)buffer);
  buffer += sizeof(unsigned long);

  /* Read parent */
  buffer += var_align(buffer, sizeof(unsigned long));
  seq_printf(m, "parent = 0x%lX ", *(unsigned long *)buffer);
  buffer += sizeof(unsigned long);
}

Note that in this particular case, given we would not need any special
"dump everything as if it was an unorganized array of bytes", the
function_entry_show() would be totally useless if we provide a sane
vsnprintf-like decoder based on the format string.

I did this example to show you how we could deal with the special cases
where people would be interested to write a whole network packet (or any
similar structure) directly to the trace (given it has field structures
which are not too tied to the compiler internals and has field sizes
portable across architectures). We could do this without much problem by
adding a format string type which specified such a binary blob, and we
could even leave room for people to provide their ascii formatting
function pointer, as shows my second example here.

Mathieu


-- 
Mathieu Desnoyers
OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F  BA06 3F25 A8FE 3BAE 9A68
--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Unified tracing buffer, Martin Bligh, (Fri Sep 19, 2:33 pm)
Re: Unified tracing buffer, Randy Dunlap, (Fri Sep 19, 2:42 pm)
Re: Unified tracing buffer, Martin Bligh, (Fri Sep 19, 2:57 pm)
Re: Unified tracing buffer, Martin Bligh, (Fri Sep 19, 3:09 pm)
Re: Unified tracing buffer, Martin Bligh, (Fri Sep 19, 3:19 pm)
Re: Unified tracing buffer, Olaf Dabrunz, (Fri Sep 19, 3:28 pm)
Re: Unified tracing buffer, Olaf Dabrunz, (Fri Sep 19, 3:41 pm)
Re: Unified tracing buffer, Frank Ch. Eigler, (Fri Sep 19, 4:18 pm)
Re: Unified tracing buffer, Peter Zijlstra, (Fri Sep 19, 5:07 pm)
Re: Unified tracing buffer, Marcel Holtmann, (Fri Sep 19, 5:26 pm)
Re: Unified tracing buffer, Olaf Dabrunz, (Sat Sep 20, 1:10 am)
Re: Unified tracing buffer, Steven Rostedt, (Sat Sep 20, 1:26 am)
Re: Unified tracing buffer, Steven Rostedt, (Sat Sep 20, 1:29 am)
Re: Unified tracing buffer, Steven Rostedt, (Sat Sep 20, 1:50 am)
Re: Unified tracing buffer, Steven Rostedt, (Sat Sep 20, 2:03 am)
Re: Unified tracing buffer, Mathieu Desnoyers, (Sat Sep 20, 4:40 am)
Re: Unified tracing buffer, Mathieu Desnoyers, (Sat Sep 20, 4:44 am)
Re: Unified tracing buffer, Mathieu Desnoyers, (Sat Sep 20, 6:37 am)
Re: Unified tracing buffer, Steven Rostedt, (Sat Sep 20, 6:51 am)
Re: Unified tracing buffer, Mathieu Desnoyers, (Sat Sep 20, 6:55 am)
Re: Unified tracing buffer, Arjan van de Ven, (Sat Sep 20, 7:12 am)
Re: Unified tracing buffer, Steven Rostedt, (Sat Sep 20, 7:54 am)
Re: Unified tracing buffer, KOSAKI Motohiro, (Sun Sep 21, 8:09 pm)
Re: Unified tracing buffer, Peter Zijlstra, (Mon Sep 22, 2:57 am)
Re: Unified tracing buffer, K.Prasad, (Mon Sep 22, 6:57 am)
Re: Unified tracing buffer, K.Prasad, (Mon Sep 22, 7:07 am)
Re: Unified tracing buffer, Peter Zijlstra, (Mon Sep 22, 7:45 am)
Re: Unified tracing buffer, Martin Bligh, (Mon Sep 22, 9:29 am)
Re: Unified tracing buffer, Peter Zijlstra, (Mon Sep 22, 9:36 am)
Re: Unified tracing buffer, Mathieu Desnoyers, (Mon Sep 22, 11:45 am)
Re: Unified tracing buffer, Mathieu Desnoyers, (Mon Sep 22, 11:52 am)
Re: Unified tracing buffer, Masami Hiramatsu, (Mon Sep 22, 12:45 pm)
Re: Unified tracing buffer, Martin Bligh, (Mon Sep 22, 1:13 pm)
Re: Unified tracing buffer, Masami Hiramatsu, (Mon Sep 22, 1:50 pm)
Re: Unified tracing buffer, Steven Rostedt, (Mon Sep 22, 2:39 pm)
Re: Unified tracing buffer, Masami Hiramatsu, (Mon Sep 22, 3:25 pm)
Re: Unified tracing buffer, Darren Hart, (Mon Sep 22, 4:11 pm)
Re: Unified tracing buffer, Martin Bligh, (Mon Sep 22, 4:16 pm)
Re: Unified tracing buffer, Masami Hiramatsu, (Mon Sep 22, 5:04 pm)
Re: Unified tracing buffer, Masami Hiramatsu, (Mon Sep 22, 5:05 pm)
Re: Unified tracing buffer, Martin Bligh, (Mon Sep 22, 5:12 pm)
Re: Unified tracing buffer, Linus Torvalds, (Mon Sep 22, 5:39 pm)
Re: Unified tracing buffer, Roland Dreier, (Mon Sep 22, 6:26 pm)
Re: Unified tracing buffer, Steven Rostedt, (Mon Sep 22, 6:39 pm)
Re: Unified tracing buffer, Mathieu Desnoyers, (Mon Sep 22, 7:02 pm)
Re: Unified tracing buffer, Darren Hart, (Mon Sep 22, 7:26 pm)
Re: Unified tracing buffer, Mathieu Desnoyers, (Mon Sep 22, 7:30 pm)
Re: Unified tracing buffer, Mathieu Desnoyers, (Mon Sep 22, 7:31 pm)
Re: Unified tracing buffer, Mathieu Desnoyers, (Mon Sep 22, 7:36 pm)
Re: Unified tracing buffer, Mathieu Desnoyers, (Mon Sep 22, 7:49 pm)
Re: Unified tracing buffer, Mathieu Desnoyers, (Mon Sep 22, 8:05 pm)
Re: Unified tracing buffer, Masami Hiramatsu, (Mon Sep 22, 8:06 pm)
Re: Unified tracing buffer, Linus Torvalds, (Mon Sep 22, 8:26 pm)
Re: Unified tracing buffer, Mathieu Desnoyers, (Mon Sep 22, 8:27 pm)
Re: Unified tracing buffer, Andi Kleen, (Mon Sep 22, 8:33 pm)
Re: Unified tracing buffer, Mathieu Desnoyers, (Mon Sep 22, 8:36 pm)
Re: Unified tracing buffer, Steven Rostedt, (Mon Sep 22, 8:43 pm)
Re: Unified tracing buffer, Martin Bligh, (Mon Sep 22, 8:47 pm)
Re: Unified tracing buffer, Linus Torvalds, (Mon Sep 22, 9:05 pm)
Re: Unified tracing buffer, Masami Hiramatsu, (Mon Sep 22, 9:10 pm)
Re: Unified tracing buffer, Martin Bligh, (Mon Sep 22, 9:17 pm)
Re: Unified tracing buffer, Linus Torvalds, (Mon Sep 22, 9:19 pm)
Re: Unified tracing buffer, Andi Kleen, (Mon Sep 22, 10:04 pm)
Re: Unified tracing buffer, Tom Zanussi, (Mon Sep 22, 10:25 pm)
[PATCH 1/3] relay - clean up subbuf switch, Tom Zanussi, (Mon Sep 22, 10:27 pm)
[PATCH 2/3] relay - make subbuf switch replaceable, Tom Zanussi, (Mon Sep 22, 10:27 pm)
[PATCH 3/3] relay - add channel flags, Tom Zanussi, (Mon Sep 22, 10:27 pm)
Re: Unified tracing buffer, Peter Zijlstra, (Tue Sep 23, 2:31 am)
Re: Unified tracing buffer, Steven Rostedt, (Tue Sep 23, 3:53 am)
Re: Unified tracing buffer, Mathieu Desnoyers, (Tue Sep 23, 6:50 am)
Re: Unified tracing buffer, Martin Bligh, (Tue Sep 23, 7:00 am)
Re: Unified tracing buffer, Mathieu Desnoyers, (Tue Sep 23, 7:12 am)
Re: Unified tracing buffer, KOSAKI Motohiro, (Tue Sep 23, 7:36 am)
Re: Unified tracing buffer, Masami Hiramatsu, (Tue Sep 23, 7:49 am)
Re: Unified tracing buffer, Frank Ch. Eigler, (Tue Sep 23, 8:02 am)
Re: Unified tracing buffer, Mathieu Desnoyers, (Tue Sep 23, 8:04 am)
Re: Unified tracing buffer, Masami Hiramatsu, (Tue Sep 23, 8:21 am)
Re: Unified tracing buffer, Masami Hiramatsu, (Tue Sep 23, 8:23 am)
Re: Unified tracing buffer, Masami Hiramatsu, (Tue Sep 23, 8:30 am)
Re: Unified tracing buffer, Linus Torvalds, (Tue Sep 23, 8:46 am)
Re: Unified tracing buffer, Linus Torvalds, (Tue Sep 23, 9:01 am)
Re: Unified tracing buffer, Masami Hiramatsu, (Tue Sep 23, 10:04 am)
Re: Unified tracing buffer, Thomas Gleixner, (Tue Sep 23, 10:30 am)
Re: Unified tracing buffer, K.Prasad, (Tue Sep 23, 10:55 am)
Re: Unified tracing buffer, KOSAKI Motohiro, (Tue Sep 23, 10:59 am)
Re: Unified tracing buffer, Mathieu Desnoyers, (Tue Sep 23, 11:13 am)
Re: Unified tracing buffer, Martin Bligh, (Tue Sep 23, 11:27 am)
Re: Unified tracing buffer, Martin Bligh, (Tue Sep 23, 11:28 am)
Re: Unified tracing buffer, Christoph Lameter, (Tue Sep 23, 11:33 am)
Re: Unified tracing buffer, Linus Torvalds, (Tue Sep 23, 11:56 am)
Re: Unified tracing buffer, Masami Hiramatsu, (Tue Sep 23, 11:59 am)
Re: Unified tracing buffer, Thomas Gleixner, (Tue Sep 23, 12:36 pm)
Re: Unified tracing buffer, Martin Bligh, (Tue Sep 23, 12:38 pm)
Re: Unified tracing buffer, Thomas Gleixner, (Tue Sep 23, 12:41 pm)
Re: Unified tracing buffer, Martin Bligh, (Tue Sep 23, 12:50 pm)
Re: Unified tracing buffer, Thomas Gleixner, (Tue Sep 23, 1:03 pm)
Re: Unified tracing buffer, Masami Hiramatsu, (Tue Sep 23, 1:03 pm)
Re: Unified tracing buffer, Thomas Gleixner, (Tue Sep 23, 1:08 pm)
Re: [PATCH 1/3] relay - clean up subbuf switch, Andrew Morton, (Tue Sep 23, 1:15 pm)
Re: [PATCH 2/3] relay - make subbuf switch replaceable, Andrew Morton, (Tue Sep 23, 1:17 pm)
Re: [PATCH 3/3] relay - add channel flags, Andrew Morton, (Tue Sep 23, 1:20 pm)
Re: Unified tracing buffer, Martin Bligh, (Tue Sep 23, 2:02 pm)
Re: Unified tracing buffer, Tom Zanussi, (Tue Sep 23, 8:50 pm)
Re: [PATCH 3/3] relay - add channel flags, Tom Zanussi, (Tue Sep 23, 8:57 pm)
Re: Unified tracing buffer, K.Prasad, (Tue Sep 23, 10:42 pm)
[RFC PATCH 0/8] current relay cleanup patchset, Tom Zanussi, (Wed Sep 24, 11:07 pm)
Re: Unified tracing buffer, Jason Baron, (Thu Oct 2, 8:28 am)
Re: Unified tracing buffer, Mathieu Desnoyers, (Fri Oct 3, 9:11 am)
Re: Unified tracing buffer, Jason Baron, (Fri Oct 3, 11:37 am)
Re: Unified tracing buffer, Mathieu Desnoyers, (Fri Oct 3, 12:10 pm)
Re: Unified tracing buffer, Jason Baron, (Fri Oct 3, 12:25 pm)
Re: Unified tracing buffer, Mathieu Desnoyers, (Fri Oct 3, 12:56 pm)
Re: Unified tracing buffer, Jason Baron, (Fri Oct 3, 1:25 pm)
Re: Unified tracing buffer, Frank Ch. Eigler, (Fri Oct 3, 2:52 pm)