<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Apr 17, 2014 at 1:27 PM, Justin Bogner <span dir="ltr"><<a href="mailto:mail@justinbogner.com" target="_blank">mail@justinbogner.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="">Chandler Carruth <<a href="mailto:chandlerc@google.com">chandlerc@google.com</a>> writes: <br>
</div></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="">
> if (thread-ID != main's thread-ID && shard_count < std::min(MAX, NUMBER_OF_CORES)) {<br>
> shard_count = std::min(MAX, std::max(NUMBER_OF_THREADS, NUMBER_OF_CORES));<br>
> // if shard_count changed with this, we can also call a library routine here<br>
> that does the work of allocating the actual extra shards.<br>
> }<br></div></blockquote><div><br></div><div>Note, I had a bug here. The first time I had this only grow the shard count once, and later thought it might be nice to grow this incrementally so that applications that only need 2 or 3 threads, don't pay for more. I fixed the guard here, but naturally this is still hand-wavy. =D</div>
<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="">
<br>
</div>Is it possible to hook on something more clever than function entry?<br>
Choosing to do this based on thread creation or something like that<br>
could make this extra check very cheap.<br></blockquote><div><br></div><div>Yea, *if* you can do it on thread creation, that would be awesome. But we don't really have a good way to model that right now. on the flip side.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class=""><br>
> MAX is a fixed cap so even on systems with 100s of cores we don't do something<br>
> silly. NUBER_OF_THREADS, if supported on the OS, can limit the shards when we<br>
> only have a small number of threads in the program. NUMBER_OF_CORES, if<br>
> supported on the OS, can limit the shards. If we don't have the number of<br>
> threads, we just use the number of cores. If we don't have the number of<br>
> cores, we can just guess 8 (or something).<br>
<br>
</div>I like the general idea of this approach. The obvious TLS approach<br>
worried me in that it explodes memory usage by much more than you'll<br>
generally need.<br>
<div class=""><br>
> Then, we can gracefully fall back on the following strategies to pick an index<br>
> into the shards:<br>
><br>
> - Per-core non-atomic counter updates (if we support them) using restartable<br>
> sequences<br>
> - Use a core-ID as the index into the shards to statistically minimize the<br>
> contention, and do the increments atomically so it remains correct even if the<br>
> core-ID load happens before a core migration and the counter increment occurs<br>
> afterward<br>
> - Use (thread-ID % number of cores) if we don't have support for getting a<br>
> core-ID from the target OS. This will still have a reasonable distribution I<br>
> suspect, even if not perfect.<br>
><br>
> Finally, I wouldn't merge on shutdown if possible. I would just write larger<br>
> raw profile data for multithreaded runs, and let the profdata tool merge them.<br>
<br>
</div>Agreed. Doing the merging offline is a clear win. The space overhead is<br>
short lived enough that it shouldn't be a big issue.<br>
<div class=""><br>
> If this is still too much memory, then I would suggest doing the above, but<br>
> doing it independently for each function so that only those functions actually<br>
> called via multithreaded code end up sharding their counters.<br>
<br>
</div>I'd worry that doing this per-function might be too fine of granularity,<br>
but there are certainly use cases where large chunks of the code are<br>
only exercised by one (of many) threads, so something along these lines<br>
could be worthwhile.<br></blockquote><div><br></div><div>Yea. If we need to do it in a fine grained way (IE, not whole-program sharding), I think my favorite granularity would be per-post-inliner-function. The other thing to do is to set up function-local variables at that phase that are used to locate the counters cheaply within the function body.</div>
<div><br></div><div>One way that might work would be to use essentially synthetic intrinsics to introduce the *markers* for instrumentation (and the function names, frontend hashes etc, all the stuff that needs language-level semantics) into the code, but have LLVM transform these into the actual instrumentation code after inlining and optimization. Then it can also inject the necessary code into the more coarse grained boundaries.</div>
</div><br></div></div>