1// REQUIRES: target={{.*windows-msvc.*}}
2// REQUIRES: lld-available
3
4// Test the online merging mode (%m) along with continuous mode (%c).
5//
6// Split files & cd into a temporary directory.
7// RUN: rm -rf %t.dir && split-file %s %t.dir && cd %t.dir
8//
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
13//
14// === Round 1 ===
15// Test merging+continuous mode without any file contention.
16//
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
20
21// ROUND1-LABEL: Counters:
22// ROUND1-DAG: foo:
23// ROUND1-DAG: Hash: 0x{{.*}}
24// ROUND1-DAG: Counters: 1
25// ROUND1-DAG: Block counts: [1]
26// ROUND1-DAG: bar:
27// ROUND1-DAG: Hash: 0x{{.*}}
28// ROUND1-DAG: Counters: 1
29// ROUND1-DAG: Block counts: [1]
30// ROUND1-DAG: main:
31// ROUND1-DAG: Hash: 0x{{.*}}
32// ROUND1-LABEL: Instrumentation level: IR
33//
34// === Round 2 ===
35// Test merging+continuous mode with some file contention.
36//
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
40
41// ROUND2-LABEL: Counters:
42// ROUND2-DAG: foo:
43// ROUND2-DAG: Hash: 0x{{.*}}
44// ROUND2-DAG: Counters: 1
45// ROUND2-DAG: Block counts: [97]
46// ROUND2-DAG: bar:
47// ROUND2-DAG: Hash: 0x{{.*}}
48// ROUND2-DAG: Counters: 1
49// ROUND2-DAG: Block counts: [97]
50// ROUND2-DAG: main:
51// ROUND2-DAG: Hash: 0x{{.*}}
52// ROUND2-LABEL: Instrumentation level: IR
53
54//--- foo.c
55__declspec(dllexport) void foo(void) {}
56
57//--- bar.c
58__declspec(dllexport) void bar(void) {}
59
60//--- main.c
61#include <stdio.h>
62#include <string.h>
63#include <windows.h>
64
65
66const int num_child_procs_to_spawn = 32;
67
68extern int __llvm_profile_is_continuous_mode_enabled(void);
69extern char *__llvm_profile_get_filename(void);
70
71__declspec(dllimport) void foo(void);
72__declspec(dllimport) void bar(void);
73
74// Change to "#define" for debug output.
75#undef DEBUG_TEST
76
77#ifdef DEBUG_TEST
78# define DEBUG(...) fprintf(stderr, __VA_ARGS__);
79#else
80# define DEBUG(...)
81#endif
82
83int main(int argc, char *const argv[]) {
84 if (argc < 2) {
85 DEBUG("Requires at least one argument.\n");
86 return 1;
87 }
88 if (strcmp(s1: argv[1], s2: "nospawn") == 0) {
89 DEBUG(
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());
93
94 foo();
95 bar();
96 return 0;
97 } else if (strcmp(s1: argv[1], s2: "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.
100 int I;
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
104 bar();
105
106 DEBUG("Spawning child with argv = {%s, %s, NULL} and envp = {%s, NULL}\n",
107 child_argv[0], child_argv[1], child_envp[0]);
108
109 // Start the child process.
110 STARTUPINFO si;
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
124 ) {
125 fprintf(stderr, format: "Child %d could not be spawned: %lu\n", I,
126 GetLastError());
127 return 1;
128 }
129 child_pids[I] = pi.hProcess;
130
131 DEBUG("Spawned child %d (pid = %zu).\n", I, pi.dwProcessId);
132 }
133 for (I = 0; I < num_child_procs_to_spawn; ++I) {
134 foo(); // Counts[dsoX] += num_child_procs_to_spawn
135 bar();
136
137 DWORD exit_code;
138 WaitForSingleObject(child_pids[I], INFINITE);
139 if (!GetExitCodeProcess(child_pids[I], &exit_code)) {
140 fprintf(stderr, format: "Failed to get exit code of child %d.\n", I);
141 return 1;
142 }
143 if (exit_code != 0) {
144 fprintf(stderr, format: "Child %d did not exit with code 0.\n", I);
145 return 1;
146 }
147 }
148
149 // At the end of Round 2, we have:
150 // Counts[dsoX] = 1 + (2 * num_child_procs_to_spawn) + num_child_procs_to_spawn
151 // = 97
152
153 return 0;
154 }
155
156 return 1;
157}

source code of compiler-rt/test/profile/ContinuousSyncMode/online-merging-windows.c