[LLVMdev] multithreaded performance disaster with -fprofile-instr-generate (contention on profile counters)
Xinliang David Li
xinliangli at gmail.com
Wed Apr 23 09:13:01 PDT 2014
This is very neat. The concern is that for long running process, the
precision loss can be huge. Run to run counter variance can also be very
large (without atomic update).
>
> Moreover, this approach allows to implement the counter increment using a
> callback:
> if ((fast_thread_local_rand() & counter) == 0)
> __cov_increment(&counter);
> which in turn will let us use the same hack as in AsanCoverage: use the PC
> to map the counter to the source code.
> (= no need to create separate section in the objects).
>
Similar callback mechanism (GCC based) is used in Google for testing
coverage.
David
>
> Thoughts?
>
> --kcc
>
>
> % clang++ -O2 -lpthread coverage_mt_vec.cc && time ./a.out
> TIME: real: 0.219; user: 0.430; system: 0.000
> % clang++ -O2 -lpthread -fprofile-instr-generate coverage_mt_vec.cc &&
> time ./a.out
> TIME: real: 3.743; user: 7.280; system: 0.000
>
> % cat coverage_mt_vec.cc
> #include <pthread.h>
> #include <vector>
>
> std::vector<int> v(1000);
>
> __attribute__((noinline)) void foo() { v[0] = 42; }
>
> void *Thread1(void *) {
> for (int i = 0; i < 100000000; i++)
> foo();
> return 0;
> }
>
> __attribute__((noinline)) void bar() { v[999] = 66; }
>
> void *Thread2(void *) {
> for (int i = 0; i < 100000000; i++)
> bar();
> return 0;
> }
>
> int main() {
> static const int kNumThreads = 16;
> pthread_t t[kNumThreads];
> pthread_create(&t[0], 0, Thread1, 0);
> pthread_create(&t[1], 0, Thread2, 0);
> pthread_join(t[0], 0);
> pthread_join(t[1], 0);
> return 0;
> }
>
>
>
>
> On Fri, Apr 18, 2014 at 11:45 PM, Xinliang David Li <xinliangli at gmail.com>wrote:
>
>>
>>
>>
>> On Fri, Apr 18, 2014 at 12:13 AM, Dmitry Vyukov <dvyukov at google.com>wrote:
>>
>>> Hi,
>>>
>>> This is long thread, so I will combine several comments into single
>>> email.
>>>
>>>
>>> >> - 8-bit per-thread counters, dumping into central counters on
>>> overflow.
>>> >The overflow will happen very quickly with 8bit counter.
>>>
>>> Yes, but it reduces contention by 256x (a thread must execute at least
>>> 256 loop iterations between increments). In practice, if you reduce
>>> contention below some threshold, it does not represent a problem anymore.
>>>
>>>
>>>
>>> >> - per-thread counters. Solves the problem at huge cost in RAM
>>> per-thread
>>> >It is not practical. Especially for TLS counters -- it creates huge
>>> pressure on stack memory.
>>>
>>> Do we have any numbers about number of counters? If there are 100K
>>> 1-byte counters, I would consider it as practical.
>>>
>>>
>> A medium sized app I looked at has about 10M counters (arcs only). It is
>> also not uncommon to see such apps running with hundreds of threads.
>>
>>
>>
>>>
>>>
>>>
>>>
>>> > In Google GCC, we implemented another technique which proves to be
>>> very effective -- it is called FDO sampling.
>>> > Basically counters will be updated every N samples.
>>>
>>> How does it work?
>>>
>>
>> Similar to how occurrences based PMU sampling work. Setting sampling
>> period to 100 can reduce the instrumentation overhead by close to 100x
>> without introducing much precision loss.
>>
>>
>>>
>>>
>>>
>>> >> It seems to me like we’re going to have a hard time getting good
>>> multithreaded performance without significant impact on the single-threaded
>>> behavior.
>>> > I don't really agree.
>>>
>>>
>>> >We are talking about developers here. Nobody would know the exact
>>> thread counts, but developers know the ballpark number
>>>
>>> I strongly believe that we must relief developers from this choice
>>> during build time, and do our best to auto-tune (if the final scheme
>>> requires tuning).
>>>
>>
>>
>> That really depends. If the tuning space is small, it won't be a
>> problem for the developer/builder.
>>
>>
>>
>>> First, such questions puts unnecessary burden on developers. We don't
>>> ask what register allocation algorithm to use for each function, right?
>>>
>>
>> Crazy developers can actually do that via internal options, but this is
>> totally different case. People just needs one flag to turn on/off
>> sharding. When sharding is on, compiler can pick the best 'N' according to
>> some heuristics at compile time.
>>
>>
>>
>>> Second, there are significant chances we will get a wrong answer,
>>> because e.g. developer's view of how threaded is the app can differ from
>>> reality or from our classification.
>>> Third, the app can be build by a build engineer; or the procedure can be
>>> applied to a base with 10'000 apps; or threaded-ness can change; or the app
>>> can work in different modes; or the app can have different phases.
>>>
>>>
>> We have forgotten to mention the benefit of implementation simplicity.
>> If the static/simple solution solves the problem for most of the use
>> cases, designing fancy dynamic solution sounds like over-engineering to me.
>> It (overhead reduction technique) may also get in the way of easier
>> functional enhancement in the future.
>>
>> David
>>
>>
>> _______________________________________________
>> LLVM Developers mailing list
>> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140423/5f2d7493/attachment.html>
More information about the llvm-dev
mailing list