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