1 // REQUIRES: target={{.*windows-msvc.*}}
2 // REQUIRES: lld-available
4 // Test the online merging mode (%m) along with continuous mode (%c).
6 // Split files & cd into a temporary directory.
7 // RUN: rm -rf %t.dir && split-file %s %t.dir && cd %t.dir
9 // Create two DLLs and a driver program that uses them.
10 // RUN: %clang_pgogen foo.c -mllvm -instrprof-atomic-counter-update-all=1 -mllvm -runtime-counter-relocation=true -fuse-ld=lld -Wl,-dll -o %t.dir/foo.dll
11 // RUN: %clang_pgogen bar.c -mllvm -instrprof-atomic-counter-update-all=1 -mllvm -runtime-counter-relocation=true -fuse-ld=lld -Wl,-dll -o %t.dir/bar.dll
12 // RUN: %clang_pgogen main.c -o main.exe %t.dir/foo.lib %t.dir/bar.lib -mllvm -instrprof-atomic-counter-update-all=1 -mllvm -runtime-counter-relocation=true -fuse-ld=lld
15 // Test merging+continuous mode without any file contention.
17 // RUN: env LLVM_PROFILE_FILE="%t.dir/profdir/%m%c.profraw" %run %t.dir/main.exe nospawn
18 // RUN: llvm-profdata merge -o %t.profdata %t.dir/profdir
19 // RUN: llvm-profdata show --counts --all-functions %t.profdata | FileCheck %s -check-prefix=ROUND1
21 // ROUND1-LABEL: Counters:
23 // ROUND1-DAG: Hash: 0x{{.*}}
24 // ROUND1-DAG: Counters: 1
25 // ROUND1-DAG: Block counts: [1]
27 // ROUND1-DAG: Hash: 0x{{.*}}
28 // ROUND1-DAG: Counters: 1
29 // ROUND1-DAG: Block counts: [1]
31 // ROUND1-DAG: Hash: 0x{{.*}}
32 // ROUND1-LABEL: Instrumentation level: IR
35 // Test merging+continuous mode with some file contention.
37 // RUN: env LLVM_PROFILE_FILE="%t.dir/profdir/%m%c.profraw" %run %t.dir/main.exe spawn
38 // RUN: llvm-profdata merge -o %t.profdata %t.dir/profdir
39 // RUN: llvm-profdata show --counts --all-functions %t.profdata | FileCheck %s -check-prefix=ROUND2
41 // ROUND2-LABEL: Counters:
43 // ROUND2-DAG: Hash: 0x{{.*}}
44 // ROUND2-DAG: Counters: 1
45 // ROUND2-DAG: Block counts: [97]
47 // ROUND2-DAG: Hash: 0x{{.*}}
48 // ROUND2-DAG: Counters: 1
49 // ROUND2-DAG: Block counts: [97]
51 // ROUND2-DAG: Hash: 0x{{.*}}
52 // ROUND2-LABEL: Instrumentation level: IR
55 __declspec(dllexport
) void foo(void) {}
58 __declspec(dllexport
) void bar(void) {}
66 const int num_child_procs_to_spawn
= 32;
68 extern int __llvm_profile_is_continuous_mode_enabled(void);
69 extern char *__llvm_profile_get_filename(void);
71 __declspec(dllimport
) void foo(void);
72 __declspec(dllimport
) void bar(void);
74 // Change to "#define" for debug output.
78 # define DEBUG(...) fprintf(stderr, __VA_ARGS__);
83 int main(int argc
, char *const argv
[]) {
85 DEBUG("Requires at least one argument.\n");
88 if (strcmp(argv
[1], "nospawn") == 0) {
90 "Hello from child (pid = %lu, cont-mode-enabled = %d, profile = %s).\n",
91 GetCurrentProcessId(), __llvm_profile_is_continuous_mode_enabled(),
92 __llvm_profile_get_filename());
97 } else if (strcmp(argv
[1], "spawn") == 0) {
98 // This is the start of Round 2.
99 // Expect Counts[dsoX] = 1, as this was the state at the end of Round 1.
101 HANDLE child_pids
[num_child_procs_to_spawn
];
102 for (I
= 0; I
< num_child_procs_to_spawn
; ++I
) {
103 foo(); // Counts[dsoX] += 2 * num_child_procs_to_spawn
106 DEBUG("Spawning child with argv = {%s, %s, NULL} and envp = {%s, NULL}\n",
107 child_argv
[0], child_argv
[1], child_envp
[0]);
109 // Start the child process.
111 ZeroMemory(&si
, sizeof(si
));
112 PROCESS_INFORMATION pi
;
113 ZeroMemory(&pi
, sizeof(pi
));
114 if (!CreateProcess(NULL
, // No module name (use command line)
115 "main.exe nospawn", // Command line
116 NULL
, // Process handle not inheritable
117 NULL
, // Thread handle not inheritable
118 FALSE
, // Set handle inheritance to FALSE
119 0, // No creation flags
120 NULL
, // Use parent's environment block
121 NULL
, // Use parent's starting directory
122 &si
, // Pointer to STARTUPINFO structure
123 &pi
) // Pointer to PROCESS_INFORMATION structure
125 fprintf(stderr
, "Child %d could not be spawned: %lu\n", I
,
129 child_pids
[I
] = pi
.hProcess
;
131 DEBUG("Spawned child %d (pid = %zu).\n", I
, pi
.dwProcessId
);
133 for (I
= 0; I
< num_child_procs_to_spawn
; ++I
) {
134 foo(); // Counts[dsoX] += num_child_procs_to_spawn
138 WaitForSingleObject(child_pids
[I
], INFINITE
);
139 if (!GetExitCodeProcess(child_pids
[I
], &exit_code
)) {
140 fprintf(stderr
, "Failed to get exit code of child %d.\n", I
);
143 if (exit_code
!= 0) {
144 fprintf(stderr
, "Child %d did not exit with code 0.\n", I
);
149 // At the end of Round 2, we have:
150 // Counts[dsoX] = 1 + (2 * num_child_procs_to_spawn) + num_child_procs_to_spawn