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