xref: /llvm-project/compiler-rt/test/profile/ContinuousSyncMode/online-merging.c (revision 6ffc4451037bbae26cce51fb80418f8e9ed2ac84)
1 // REQUIRES: continuous-mode
2 // UNSUPPORTED: powerpc-{{.*}}
3 
4 // Test the online merging mode (%m) along with continuous mode (%c).
5 //
6 // Create & cd into a temporary directory.
7 // RUN: rm -rf %t.dir && mkdir -p %t.dir && cd %t.dir
8 //
9 // Create two DSOs and a driver program that uses them.
10 // RUN: echo "void dso1(void) {}" > dso1.c
11 // RUN: echo "void dso2(void) {}" > dso2.c
12 // RUN: %clang_pgogen_cont %shared_lib_flag -o %t.dir/dso1.dylib dso1.c -fprofile-update=atomic
13 // RUN: %clang_pgogen_cont %shared_lib_flag -o %t.dir/dso2.dylib dso2.c -fprofile-update=atomic
14 // RUN: %clang_pgogen_cont -o main.exe %s %t.dir/dso1.dylib %t.dir/dso2.dylib -fprofile-update=atomic
15 //
16 // === Round 1 ===
17 // Test merging+continuous mode without any file contention.
18 //
19 // RUN: env LLVM_PROFILE_FILE="%t.dir/profdir/%m%c.profraw" %run %t.dir/main.exe nospawn
20 // RUN: llvm-profdata merge -o %t.profdata %t.dir/profdir
21 // RUN: llvm-profdata show --counts --all-functions %t.profdata | FileCheck %s -check-prefix=ROUND1
22 
23 // ROUND1-LABEL: Counters:
24 // ROUND1-DAG:   dso1:
25 // ROUND1-DAG:     Hash: 0x{{.*}}
26 // ROUND1-DAG:     Counters: 1
27 // ROUND1-DAG:     Block counts: [1]
28 // ROUND1-DAG:   dso2:
29 // ROUND1-DAG:     Hash: 0x{{.*}}
30 // ROUND1-DAG:     Counters: 1
31 // ROUND1-DAG:     Block counts: [1]
32 // ROUND1-DAG:   main:
33 // ROUND1-DAG:     Hash: 0x{{.*}}
34 // ROUND1-LABEL: Instrumentation level: IR
35 // ROUND1-NEXT: Functions shown: 3
36 // ROUND1-NEXT: Total functions: 3
37 // ROUND1-NEXT: Maximum function count: 1
38 // ROUND1-NEXT: Maximum internal block count: 1
39 //
40 // === Round 2 ===
41 // Test merging+continuous mode with some file contention.
42 //
43 // RUN: env LLVM_PROFILE_FILE="%t.dir/profdir/%m%c.profraw" %run %t.dir/main.exe spawn 'LLVM_PROFILE_FILE=%t.dir/profdir/%m%c.profraw'
44 // RUN: llvm-profdata merge -o %t.profdata %t.dir/profdir
45 // RUN: llvm-profdata show --counts --all-functions %t.profdata | FileCheck %s -check-prefix=ROUND2
46 
47 // ROUND2-LABEL: Counters:
48 // ROUND2-DAG:   dso1:
49 // ROUND2-DAG:     Hash: 0x{{.*}}
50 // ROUND2-DAG:     Counters: 1
51 // ROUND2-DAG:     Block counts: [97]
52 // ROUND2-DAG:   dso2:
53 // ROUND2-DAG:     Hash: 0x{{.*}}
54 // ROUND2-DAG:     Counters: 1
55 // ROUND2-DAG:     Block counts: [97]
56 // ROUND2-DAG:   main:
57 // ROUND2-DAG:     Hash: 0x{{.*}}
58 // ROUND2-LABEL: Instrumentation level: IR
59 // ROUND2-NEXT: Functions shown: 3
60 // ROUND2-NEXT: Total functions: 3
61 // ROUND2-NEXT: Maximum function count: 97
62 // ROUND2-NEXT: Maximum internal block count: 33
63 
64 #include <spawn.h>
65 #include <sys/wait.h>
66 #include <sys/errno.h>
67 #include <unistd.h>
68 #include <string.h>
69 #include <stdio.h>
70 
71 const int num_child_procs_to_spawn = 32;
72 
73 extern int __llvm_profile_is_continuous_mode_enabled(void);
74 extern char *__llvm_profile_get_filename(void);
75 
76 void dso1(void);
77 void dso2(void);
78 
79 // Change to "#define" for debug output.
80 #undef DEBUG_TEST
81 
82 #ifdef DEBUG_TEST
83 #define DEBUG(...) fprintf(stderr, __VA_ARGS__);
84 #else
85 #define DEBUG(...)
86 #endif
87 
88 int main(int argc, char *const argv[]) {
89   if (strcmp(argv[1], "nospawn") == 0) {
90     DEBUG("Hello from child (pid = %d, cont-mode-enabled = %d, profile = %s).\n",
91         getpid(), __llvm_profile_is_continuous_mode_enabled(), __llvm_profile_get_filename());
92 
93     dso1();
94     dso2();
95     return 0;
96   } else if (strcmp(argv[1], "spawn") == 0) {
97     // This is the start of Round 2.
98     // Expect Counts[dsoX] = 1, as this was the state at the end of Round 1.
99 
100     int I;
101     pid_t child_pids[num_child_procs_to_spawn];
102     char *const child_argv[] = {argv[0], "nospawn", NULL};
103     char *const child_envp[] = {argv[2], NULL};
104     for (I = 0; I < num_child_procs_to_spawn; ++I) {
105       dso1(); // Counts[dsoX] += 2 * num_child_procs_to_spawn
106       dso2();
107 
108       DEBUG("Spawning child with argv = {%s, %s, NULL} and envp = {%s, NULL}\n",
109           child_argv[0], child_argv[1], child_envp[0]);
110 
111       int ret = posix_spawn(&child_pids[I], argv[0], NULL, NULL, child_argv,
112           child_envp);
113       if (ret != 0) {
114         fprintf(stderr, "Child %d could not be spawned: ret = %d, msg = %s\n",
115                 I, ret, strerror(ret));
116         return 1;
117       }
118 
119       DEBUG("Spawned child %d (pid = %d).\n", I, child_pids[I]);
120     }
121     for (I = 0; I < num_child_procs_to_spawn; ++I) {
122       dso1(); // Counts[dsoX] += num_child_procs_to_spawn
123       dso2();
124 
125       int status;
126       waitpid(child_pids[I], &status, 0);
127       if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
128         fprintf(stderr, "Child %d did not exit with code 0.\n", I);
129         return 1;
130       }
131     }
132 
133     // At the end of Round 2, we have:
134     // Counts[dsoX] = 1 + (2 * num_child_procs_to_spawn) + num_child_procs_to_spawn
135     //              = 97
136 
137     return 0;
138   }
139 
140   return 1;
141 }
142