<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">Hi All,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">This is great. Thank you very much for the information.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">-Brian<o:p></o:p></span></p>
<p class="MsoNormal"><a name="_MailEndCompose"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></a></p>
<p class="MsoNormal"><b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> Francis Giraldeau [mailto:francis.giraldeau@gmail.com]
<br>
<b>Sent:</b> Thursday, March 12, 2015 11:34 AM<br>
<b>To:</b> Mathieu Desnoyers<br>
<b>Cc:</b> Brian Robbins; lttng-dev@lists.lttng.org<br>
<b>Subject:</b> Re: [lttng-dev] Userspace Tracing and Backtraces<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<div>
<p class="MsoNormal">2015-03-10 21:47 GMT-04:00 Mathieu Desnoyers <<a href="mailto:mathieu.desnoyers@efficios.com" target="_blank">mathieu.desnoyers@efficios.com</a>>:<o:p></o:p></p>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal"><span style="color:black">Francis: Did you define UNW_LOCAL_ONLY before including<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">the libunwind header in your benchmarks ? (see<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black"><a href="http://www.nongnu.org/libunwind/man/libunwind%283%29.html" target="_blank">http://www.nongnu.org/libunwind/man/libunwind%283%29.html</a>)</span><span style="font-family:"Arial",sans-serif;color:#222222"> </span><span style="color:black"><o:p></o:p></span></p>
</div>
</div>
</blockquote>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal"><span style="color:black"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">The seems to change performance dramatically according to the documentation.<o:p></o:p></span></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Yes, this is the case. Time to unwind is higher at the beginning (probably related to internal cache build), and also vary according to call-stack depth.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal"><span style="color:black">Agreed on having the backtrace as a context. The main question left is<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">to figure out if we want to call libunwind from within the traced application<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">execution context.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">Unfortunately, libunwind is not reentrant wrt signals. This is already<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">a good argument for not calling it from within a tracepoint. I wonder<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">if the authors of libunwind would be open to make it signal-reentrant<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">in the future (not by disabling signals, but rather by keeping a TLS<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">nesting counter, and returning an error if nested, for performance<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">considerations).<o:p></o:p></span></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">The functions unw_init_local() and unw_step() are signal safe [1]. The critical sections are protected using lock_acquire() that blocks all signals before taking the mutex, which prevent the recursion.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<div>
<p class="MsoNormal">#define lock_acquire(l,m) \<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">do { \<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> SIGPROCMASK (SIG_SETMASK, &unwi_full_mask, &(m)); \<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> mutex_lock (l); \<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">} while (0)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">#define lock_release(l,m) \<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">do { \<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> mutex_unlock (l); \<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> SIGPROCMASK (SIG_SETMASK, &(m), NULL); \<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">} while (0)<o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">To understand the implications, I did a small program to study nested signals [2], where a signal is sent from within a signal, or when segmentation fault occurs in a signal handler. Blocking a signal differs it when it is unblocked, while
ignored signals are discarded. Blocked signals that can't be ignored have their default behaviour. It prevents a possible deadlock, let's say if lock_acquire() was nesting with a custom SIGSEGV handler trying to get the same lock.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">So, let's say that instead of blocking signals, we have a per-thread mutex, that returns if try_lock() fails. It would be faster, but from the user's point of view, the backtrace will be dropped randomly. I would prefer it a bit slower,
but reliable.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">In addition, could it be possible that TLS is not signal safe [3]? <o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<blockquote style="border:none;border-left:solid #1010FF 1.5pt;padding:0in 0in 0in 4.0pt;margin-left:3.75pt;margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<div>
<div>
<div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt">
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">or using the perf capture mechanism that you describe below?</span><span style="color:black"><o:p></o:p></span></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="color:black">Perf is peeking at the userspace from kernel space, it's another story. I guess that libunwind was not ported to the kernel because it is a large chunk
of complicated code that performs a lot of I/O and computation, while copying a portion of the stack is really about KISS and low runtime overhead.<o:p></o:p></span></p>
</div>
</div>
</div>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><span style="color:black">If using libunwind does not work out, another alternative I would consider<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">would be to copy the stack like perf is doing from the kernel. However,<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">in the spirit of compacting trace data, I would be tempted to do the following<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">if we go down that route: check each pointer-aligned address for its content.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">If it looks like a pointer to an executable memory area (library, executable, or<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">JIT'd code), we keep it. Else, we zero this information (not needed). We can<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">then do a RLE-alike compression on the zeroes, so we can keep the layout<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">of the stack after uncompression.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black"><o:p> </o:p></span></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Interesting! For comparison, here is a perf event [4] that shows there is a lot of room for reducing the event size. We should check if discarding other saved register values on the stack impacts restoring the instruction pointer register.
Doing the unwind offline also solves signal safety, should be fast and scalable.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Francis<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">[1] <a href="http://www.nongnu.org/libunwind/man/unw_init_local(3).html">http://www.nongnu.org/libunwind/man/unw_init_local(3).html</a><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">[2] <a href="https://gist.github.com/giraldeau/98f08161e83a7ab800ea">https://gist.github.com/giraldeau/98f08161e83a7ab800ea</a><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">[3] <a href="https://sourceware.org/glibc/wiki/TLSandSignals">https://sourceware.org/glibc/wiki/TLSandSignals</a><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">[4] <a href="http://pastebin.com/sByfXXAQ">http://pastebin.com/sByfXXAQ</a><o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>