142 lines · c
1// REQUIRES: continuous-mode2// 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.dir8//9// Create two DSOs and a driver program that uses them.10// RUN: echo "void dso1(void) {}" > dso1.c11// RUN: echo "void dso2(void) {}" > dso2.c12// RUN: %clang_pgogen=%t.dir/profdir -fprofile-continuous -fprofile-update=atomic %shared_lib_flag -o %t.dir/dso1.dylib dso1.c13// RUN: %clang_pgogen=%t.dir/profdir -fprofile-continuous -fprofile-update=atomic %shared_lib_flag -o %t.dir/dso2.dylib dso2.c14// RUN: %clang_pgogen=%t.dir/profdir -fprofile-continuous -fprofile-update=atomic -o main.exe %s %t.dir/dso1.dylib %t.dir/dso2.dylib15//16// === Round 1 ===17// Test merging+continuous mode without any file contention.18//19// RUN: %run %t.dir/main.exe nospawn20// RUN: llvm-profdata merge -o %t.profdata %t.dir/profdir21// RUN: llvm-profdata show --counts --all-functions %t.profdata | FileCheck %s -check-prefix=ROUND122 23// ROUND1-LABEL: Counters:24// ROUND1-DAG: dso1:25// ROUND1-DAG: Hash: 0x{{.*}}26// ROUND1-DAG: Counters: 127// ROUND1-DAG: Block counts: [1]28// ROUND1-DAG: dso2:29// ROUND1-DAG: Hash: 0x{{.*}}30// ROUND1-DAG: Counters: 131// ROUND1-DAG: Block counts: [1]32// ROUND1-DAG: main:33// ROUND1-DAG: Hash: 0x{{.*}}34// ROUND1-LABEL: Instrumentation level: IR35// ROUND1-NEXT: Functions shown: 336// ROUND1-NEXT: Total functions: 337// ROUND1-NEXT: Maximum function count: 138// ROUND1-NEXT: Maximum internal block count: 139//40// === Round 2 ===41// Test merging+continuous mode with some file contention.42//43// RUN: %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/profdir45// RUN: llvm-profdata show --counts --all-functions %t.profdata | FileCheck %s -check-prefix=ROUND246 47// ROUND2-LABEL: Counters:48// ROUND2-DAG: dso1:49// ROUND2-DAG: Hash: 0x{{.*}}50// ROUND2-DAG: Counters: 151// ROUND2-DAG: Block counts: [97]52// ROUND2-DAG: dso2:53// ROUND2-DAG: Hash: 0x{{.*}}54// ROUND2-DAG: Counters: 155// ROUND2-DAG: Block counts: [97]56// ROUND2-DAG: main:57// ROUND2-DAG: Hash: 0x{{.*}}58// ROUND2-LABEL: Instrumentation level: IR59// ROUND2-NEXT: Functions shown: 360// ROUND2-NEXT: Total functions: 361// ROUND2-NEXT: Maximum function count: 9762// ROUND2-NEXT: Maximum internal block count: 3363 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 71const int num_child_procs_to_spawn = 32;72 73extern int __llvm_profile_is_continuous_mode_enabled(void);74extern char *__llvm_profile_get_filename(void);75 76void dso1(void);77void dso2(void);78 79// Change to "#define" for debug output.80#undef DEBUG_TEST81 82#ifdef DEBUG_TEST83#define DEBUG(...) fprintf(stderr, __VA_ARGS__);84#else85#define DEBUG(...)86#endif87 88int 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_spawn106 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_spawn123 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_spawn135 // = 97136 137 return 0;138 }139 140 return 1;141}142