1 | // A simple fork results in two processes writing to the same file |
2 | // RUN: rm -fr %t.profdir |
3 | // RUN: %clang_pgogen=%t.profdir -o %t -O2 %s |
4 | // RUN: %run %t |
5 | // RUN: llvm-profdata show --all-functions --counts %t.profdir/default_*.profraw | FileCheck %s |
6 | // RUN: rm -fr %t.profdir |
7 | // RUN: env LLVM_PROFILE_NO_MMAP=1 %run %t |
8 | // RUN: llvm-profdata show --all-functions --counts %t.profdir/default_*.profraw | FileCheck %s |
9 | |
10 | // |
11 | // CHECK: func1: |
12 | // CHECK: Block counts: [21] |
13 | // CHECK: func2: |
14 | // CHECK: Block counts: [10] |
15 | |
16 | #include <sys/wait.h> |
17 | #include <unistd.h> |
18 | |
19 | __attribute__((noinline)) void func1() {} |
20 | __attribute__((noinline)) void func2() {} |
21 | |
22 | int main(void) { |
23 | // child | parent |
24 | // func1 func2 | func1 func2 |
25 | func1(); // +10 | +1 (*) |
26 | int i = 10; // | |
27 | while (i-- > 0) { // | |
28 | pid_t pid = fork(); // | |
29 | if (pid == -1) // | |
30 | return 1; // | |
31 | if (pid == 0) { // | |
32 | func2(); // +10 | |
33 | func1(); // +10 | |
34 | return 0; // | |
35 | } // | |
36 | } // ------------+------------ |
37 | int status; // 20 10 | 1 0 |
38 | i = 10; // (*) the child inherits counter values prior to fork |
39 | while (i-- > 0) // from the parent in non-continuous mode. |
40 | wait(stat_loc: &status); |
41 | return 0; |
42 | } |
43 | |