The next couple of days I'll work on the automation of producing a graphing of this "magic number".<br><br>Also I'm doing repeat testing now to get a feeling of the kind of spread in the numbers that cachegrind is producing.<br>
<br>Stephan<br><div class="gmail_extra"><br><br><div class="gmail_quote">2012/12/5 Michael Meeks <span dir="ltr"><<a href="mailto:michael.meeks@suse.com" target="_blank">michael.meeks@suse.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi guys,<br>
<br>
        Forwarding Josef's rather interesting mail (with permission) to the dev<br>
list - since it's generally useful :-) Stephan is working on plugging a<br>
nice callgrinding script into tinderbox building so we can build an<br>
automated performance tracker that will catch even small - 0.5% type<br>
performance regressions which can accumulate over time - and do it in a<br>
reproducible way, independent of the underlying hardware.<br>
<br>
        Thanks so much for working on this Stephan; it seems the punch-line<br>
formula for the magic number should be:<br>
<br>
> Actually, what I have in recent versions of {Q,K}Cachegrind is this:<br>
>  CEst = Ir + 10 Bm + 10 L1m + 20 Ge + 100 L2m + 100 LLm<br>
<br>
        ATB !<br>
<br>
                Michael.<br>
<br>
-------- Forwarded Message --------<br>
<br>
Hi,<br>
<br>
Am 04.12.2012 15:07, schrieb Julian Seward:<br>
><br>
> Josef,<br>
> Gruesse.  See below ..<br>
><br>
> Michael:<br>
> Josef is the expert on this, so forwarding it to him.<br>
><br>
> It used to be "1 * #insns + 10 * #L1 misses + 100 * #L2 misses",<br>
> but I wouldn't put too much reliance on it.  It was just something<br>
> that (IIRC!) NickN and I kludged up during a coffee break probably<br>
> about a decade ago at <a href="http://cl.cam.ac.uk" target="_blank">cl.cam.ac.uk</a>.  Like so many other bits of<br>
> Valgrind :-) (*)<br>
<br>
AFAIK, Cachegrind always just printed out the raw event numbers.<br>
Also, nowhere in Callgrind you will find that magic formula ;-)<br>
I think I actually was the one who came up with that formula as<br>
a rough "Cycle Estimation" in KCachegrind ;-)<br>
<br>
Actually, doing a somehow accurate time estimation is quite tricky.<br>
The rule of thumb should be: If you come up with an optimization<br>
reducing miss numbers (and also the time estimation formula above),<br>
always also check against real time.<br>
<br>
In general, any estimation using cache metrics gets more accurate<br>
the worse the cache behavior of the application, as time needed<br>
for memory accesses is more predictable and hides any smaller latencies<br>
within a core, especially with out-of-order execution, and even<br>
ARM application cores are out-of-order nowadays (Cortex A9/A15).<br>
<br>
The most accurate thing next to real measurement would be cycle-accurate<br>
simulation, which of course is out of scope. There is some nice work<br>
on a simpler, yet quite accurate processor timing model using so-called<br>
"interval simulation", as implemented in the Sniper simulator<br>
(see <a href="http://snipersim.org" target="_blank">http://snipersim.org</a>). However, it's roughly an order of magnitude<br>
slower than Cachegrind/Callgrind.<br>
<br>
The biggest issues with the simple formulas above are:<br>
(1) Hardware prefetching is not taken into account, which often allows<br>
streaming of data at maximum bandwidth from memory,<br>
(2) Latency hiding effects are not included, e.g. it easily can happen<br>
that with lots of memory accesses, it does not matter if the code<br>
actually was compiled with -O3 or -O0. Further, it can make<br>
a huge difference whether two consecutive memory accesses depend on each<br>
other or not (ie. one hiding the latency of the other or not).<br>
(3) Saturation effects are not taken into account. E.g. if all cores<br>
on a multicore chip do memory accesses at the same time, the limited<br>
bandwidth going off-chip will slow down each of them.<br>
<br>
Thus, the accuracy of the simple formula very much depends on an<br>
"average" application behavior, in the hope that the difficult<br>
effects mentioned above do not play too much of a role each.<br>
<br>
BTW, one can argue whether L1/LL misses on write should appear in<br>
the formula at all. Writes usually are done by the hardware in the<br>
background. However, if there is a memcpy operation doing a lot<br>
of consecutive writes, write buffers will fill up, and will<br>
throttle execution in the end.<br>
<br>
 > That said, I am sure it got checked by Josef :-)<br>
<br>
Despite above issues, I still think that it is useful to combine<br>
the cache simulation results into one value. It makes it easier to<br>
get an overview regarding the cache behavior.<br>
<br>
The thing is: too make time estimation more accurate, you need to<br>
extend the model, which results in slower simulation.<br>
I have some ideas to make the cache model more accurate for time<br>
estimation, but I am not sure it is worth it (real measurements<br>
are faster anyway).<br>
<br>
E.g. there is an optional hardware prefetch simulation in Callgrind.<br>
We can introduce a new event "LL miss detected by prefetcher", and<br>
give this e.g. a much lower penalty, depending on the bandwidth your<br>
hardware can do: e.g. if you can do 1GB/s on a 1GHz processor, this<br>
means 1 bytes on average per clock, thus with a 64byte line size<br>
one line can be fetched every 64 cycles. As every LL miss will also<br>
be a L1 miss (with penalty of 10), you would go for around 50 for<br>
the penalty for the new prefetchable memory access event.<br>
<br>
For latency effects, it gets more complicated as you want to check<br>
if there is a dependency between memory accesses, and do something<br>
similar to above mentioned interval simulation.<br>
<br>
For the saturation effects, one needs to take simultaneous<br>
execution of multiple threads into account.<br>
<br>
> Josef: is there a more recent version of the pseudo-cycles formula?<br>
> Also, maybe we can incorporate the number of global bus locks into<br>
> the formula?  Or does the cost of a bus lock depend also on which<br>
> cache the selected line is in?<br>
<br>
Actually, what I have in recent versions of {Q,K}Cachegrind is this:<br>
  CEst = Ir + 10 Bm + 10 L1m + 20 Ge + 100 L2m + 100 LLm<br>
<br>
L2m is there for backward compatiblity with old VG versions.<br>
If a given event type is not measured, the formula gets truncated<br>
accordingly.<br>
<br>
> I mention this because I remember at some time the LO crew were<br>
> complaining that the cycle count from Callgrind was completely misleading<br>
> due to not taking into account global bus locks.<br>
<br>
Hm, ok. The "20 Ge" estimates that locking often can be resolved at the<br>
L2/L3 level. However, I never did any measurements myself. I think you<br>
mentioned something ...<br>
<br>
Josef<br>
<br>
<br>
><br>
> J<br>
><br>
> (*) eg.  the first version of the memcheck leak checker was written<br>
> during a 4 hour plane-changing-stop at Chicago O'Hare.<br>
><br>
> ----------  Forwarded Message  ----------<br>
><br>
> Subject: Re: [Fwd: Re: callgrind / profiling bits ? ...]<br>
> Date: Tuesday, December 04, 2012, 12:35:15 am<br>
> From: Michael Meeks <<a href="mailto:michael.meeks@suse.com">michael.meeks@suse.com</a>><br>
...<br>
>       Julian - incidentally - where does is the latest/greatest set of<br>
> fudge-factors for predicting pseudo-cycles live from L1 / L2 misses<br>
> etc. :-) We'd like to use that a spreadsheet we want to use to track<br>
> incremental libreoffice performance (using callgrind of course).<br>
...<br>
>       At least from my perspective, I'd -love- to see what impact simple<br>
> day-to-day work has on performance particularly when repeatable: what do<br>
> dozens of changes per day do to the various documents we have ? - I<br>
> suspect slow bit-rot, with steps of improved perf. but ... ;-) perhaps<br>
> publishing the graph will change that.<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
<a href="mailto:michael.meeks@suse.com">michael.meeks@suse.com</a>  <><, Pseudo Engineer, itinerant idiot<br>
<br>
</font></span></blockquote></div><br></div>