diff options
Diffstat (limited to 'compiler-rt/test')
7 files changed, 319 insertions, 0 deletions
diff --git a/compiler-rt/test/profile/ContinuousSyncMode/basic.c b/compiler-rt/test/profile/ContinuousSyncMode/basic.c new file mode 100644 index 00000000000..9e29a0b0477 --- /dev/null +++ b/compiler-rt/test/profile/ContinuousSyncMode/basic.c @@ -0,0 +1,32 @@ +// RUN: %clang -fprofile-instr-generate -fcoverage-mapping -o %t.exe %s +// RUN: echo "garbage" > %t.profraw +// RUN: env LLVM_PROFILE_FILE="%c%t.profraw" %run %t.exe +// RUN: llvm-profdata show --counts --all-functions %t.profraw | FileCheck %s -check-prefix=CHECK-COUNTS +// RUN: llvm-profdata merge -o %t.profdata %t.profraw +// RUN: llvm-cov report %t.exe -instr-profile %t.profdata | FileCheck %s -check-prefix=CHECK-COVERAGE + +// CHECK-COUNTS: Counters: +// CHECK-COUNTS-NEXT: main: +// CHECK-COUNTS-NEXT: Hash: 0x{{.*}} +// CHECK-COUNTS-NEXT: Counters: 2 +// CHECK-COUNTS-NEXT: Function count: 1 +// CHECK-COUNTS-NEXT: Block counts: [1] +// CHECK-COUNTS-NEXT: Instrumentation level: Front-end +// CHECK-COUNTS-NEXT: Functions shown: 1 +// CHECK-COUNTS-NEXT: Total functions: 1 +// CHECK-COUNTS-NEXT: Maximum function count: 1 +// CHECK-COUNTS-NEXT: Maximum internal block count: 1 + +// CHECK-COVERAGE: Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover +// CHECK-COVERAGE-NEXT: --- +// CHECK-COVERAGE-NEXT: basic.c 4 1 75.00% 1 0 100.00% 5 2 60.00% +// CHECK-COVERAGE-NEXT: --- +// CHECK-COVERAGE-NEXT: TOTAL 4 1 75.00% 1 0 100.00% 5 2 60.00% + +extern int __llvm_profile_is_continuous_mode_enabled(void); + +int main() { + if (__llvm_profile_is_continuous_mode_enabled()) + return 0; + return 1; +} diff --git a/compiler-rt/test/profile/ContinuousSyncMode/darwin-proof-of-concept.c b/compiler-rt/test/profile/ContinuousSyncMode/darwin-proof-of-concept.c new file mode 100644 index 00000000000..85caca9a56b --- /dev/null +++ b/compiler-rt/test/profile/ContinuousSyncMode/darwin-proof-of-concept.c @@ -0,0 +1,151 @@ +// Test whether mmap'ing profile counters onto an open file is feasible. As +// this involves some platform-specific logic, this test is designed to be a +// minimum viable proof-of-concept: it may be useful when porting the mmap() +// mode to a new platform, but is not in and of itself a test of the profiling +// runtime. + +// REQUIRES: darwin + +// Align counters and data to the maximum expected page size (16K). +// RUN: %clang -g -o %t %s \ +// RUN: -Wl,-sectalign,__DATA,__pcnts,0x4000 \ +// RUN: -Wl,-sectalign,__DATA,__pdata,0x4000 + +// Create a 'profile' using mmap() and validate it. +// RUN: %run %t create %t.tmpfile +// RUN: %run %t validate %t.tmpfile + +#include <fcntl.h> +#include <unistd.h> +#include <sys/mman.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +__attribute__((section("__DATA,__pcnts"))) int counters[] = {0xbad}; +extern int cnts_start __asm("section$start$__DATA$__pcnts"); +const size_t cnts_len = 0x4000; + +__attribute__((section("__DATA,__pdata"))) int data[] = {1, 2, 3}; +extern int data_start __asm("section$start$__DATA$__pdata"); +const size_t data_len = sizeof(int) * 3; + +int create_tmpfile(char *path) { + // Create a temp file. + int fd = open(path, O_RDWR | O_TRUNC | O_CREAT, 0666); + if (fd == -1) { + perror("open"); + return EXIT_FAILURE; + } + + // Grow the file to hold data and counters. + if (0 != ftruncate(fd, cnts_len + data_len)) { + perror("ftruncate"); + return EXIT_FAILURE; + } + + // Write the data first (at offset 0x4000, after the counters). + if (data_len != pwrite(fd, &data, data_len, 0x4000)) { + perror("write"); + return EXIT_FAILURE; + } + + // Map the counters into the file, before the data. + // + // Requirements (on Darwin): + // - &cnts_start must be page-aligned. + // - The length and offset-into-fd must be page-aligned. + int *counter_map = (int *)mmap(&cnts_start, 0x4000, PROT_READ | PROT_WRITE, + MAP_FIXED | MAP_SHARED, fd, 0); + if (counter_map != &cnts_start) { + perror("mmap"); + return EXIT_FAILURE; + } + + // Update counters 1..9. These updates should be visible in the file. + // Expect counter 0 (0xbad), which is not updated, to be zero in the file. + for (int i = 1; i < 10; ++i) + counter_map[i] = i; + + // Intentionally do not msync(), munmap(), or close(). + return EXIT_SUCCESS; +} + +int validate_tmpfile(char *path) { + int fd = open(path, O_RDONLY); + if (fd == -1) { + perror("open"); + return EXIT_FAILURE; + } + + // Verify that the file length is: sizeof(counters) + sizeof(data). + const size_t num_bytes = cnts_len + data_len; + int buf[num_bytes]; + if (num_bytes != read(fd, &buf, num_bytes)) { + perror("read"); + return EXIT_FAILURE; + } + + // Verify the values of counters 1..9 (i.e. that the mmap() worked). + for (int i = 0; i < 10; ++i) { + if (buf[i] != i) { + fprintf(stderr, + "validate_tmpfile: Expected '%d' at pos=%d, but got '%d' instead.\n", + i, i, buf[i]); + return EXIT_FAILURE; + } + } + + // Verify that the rest of the counters (after counter 9) are 0. + const int num_cnts = 0x4000 / sizeof(int); + for (int i = 10; i < num_cnts; ++i) { + if (buf[i] != 0) { + fprintf(stderr, + "validate_tmpfile: Expected '%d' at pos=%d, but got '%d' instead.\n", + 0, i, buf[i]); + return EXIT_FAILURE; + } + } + + // Verify that the data written after the counters is equal to the "data[]" + // array (i.e. {1, 2, 3}). + for (int i = num_cnts; i < num_cnts + 3; ++i) { + if (buf[i] != (i - num_cnts + 1)) { + fprintf(stderr, + "validate_tmpfile: Expected '%d' at pos=%d, but got '%d' instead.\n", + i - num_cnts + 1, i, buf[i]); + return EXIT_FAILURE; + } + } + + // Intentionally do not close(). + return EXIT_SUCCESS; +} + +int main(int argc, char **argv) { + intptr_t cnts_start_int = (intptr_t)&cnts_start; + intptr_t data_start_int = (intptr_t)&data_start; + int pagesz = getpagesize(); + + if (cnts_start_int % pagesz != 0) { + fprintf(stderr, "__pcnts is not page-aligned: 0x%lx.\n", cnts_start_int); + return EXIT_FAILURE; + } + if (data_start_int % pagesz != 0) { + fprintf(stderr, "__pdata is not page-aligned: 0x%lx.\n", data_start_int); + return EXIT_FAILURE; + } + if (cnts_start_int + 0x4000 != data_start_int) { + fprintf(stderr, "__pdata not ordered after __pcnts.\n"); + return EXIT_FAILURE; + } + + char *action = argv[1]; + char *path = argv[2]; + if (0 == strcmp(action, "create")) + return create_tmpfile(path); + else if (0 == strcmp(action, "validate")) + return validate_tmpfile(path); + else + return EXIT_FAILURE; +} diff --git a/compiler-rt/test/profile/ContinuousSyncMode/lit.local.cfg.py b/compiler-rt/test/profile/ContinuousSyncMode/lit.local.cfg.py new file mode 100644 index 00000000000..0918f09cdaa --- /dev/null +++ b/compiler-rt/test/profile/ContinuousSyncMode/lit.local.cfg.py @@ -0,0 +1,18 @@ +import subprocess + +def getRoot(config): + if not config.parent: + return config + return getRoot(config.parent) + +root = getRoot(config) + +# As this has not been tested extensively on non-Darwin platforms, +# only Darwin support is enabled for the moment. However, continuous mode +# may "just work" without modification on Linux and other UNIX-likes (AIUI +# the default value for the GNU linker's `--section-alignment` flag is +# 0x1000, which is the size of a page on many systems). +# +# Please add supported configs to this list. +if root.host_os not in ['Darwin']: + config.unsupported = True diff --git a/compiler-rt/test/profile/ContinuousSyncMode/multiple-DSOs.c b/compiler-rt/test/profile/ContinuousSyncMode/multiple-DSOs.c new file mode 100644 index 00000000000..a54c9af6828 --- /dev/null +++ b/compiler-rt/test/profile/ContinuousSyncMode/multiple-DSOs.c @@ -0,0 +1,35 @@ +// RUN: echo "void dso1(void) {}" > %t.dso1.c +// RUN: echo "void dso2(void) {}" > %t.dso2.c +// RUN: %clang_pgogen -dynamiclib -o %t.dso1.dylib %t.dso1.c +// RUN: %clang_pgogen -dynamiclib -o %t.dso2.dylib %t.dso2.c +// RUN: %clang_pgogen -o %t.exe %s %t.dso1.dylib %t.dso2.dylib +// RUN: env LLVM_PROFILE_FILE="%c%t.profraw" %run %t.exe +// RUN: llvm-profdata show --counts --all-functions %t.profraw | FileCheck %s + +// CHECK-LABEL: Counters: +// CHECK-NEXT: dso1: +// CHECK-NEXT: Hash: 0x{{.*}} +// CHECK-NEXT: Counters: 1 +// CHECK-NEXT: Block counts: [1] +// CHECK-NEXT: dso2: +// CHECK-NEXT: Hash: 0x{{.*}} +// CHECK-NEXT: Counters: 1 +// CHECK-NEXT: Block counts: [1] +// CHECK-NEXT: main: +// CHECK-NEXT: Hash: 0x{{.*}} +// CHECK-NEXT: Counters: 1 +// CHECK-NEXT: Block counts: [1] +// CHECK-NEXT: Instrumentation level: IR +// CHECK-NEXT: Functions shown: 3 +// CHECK-NEXT: Total functions: 3 +// CHECK-NEXT: Maximum function count: 1 +// CHECK-NEXT: Maximum internal block count: 0 + +void dso1(void); +void dso2(void); + +int main() { + dso1(); + dso2(); + return 0; +} diff --git a/compiler-rt/test/profile/ContinuousSyncMode/pid-substitution.c b/compiler-rt/test/profile/ContinuousSyncMode/pid-substitution.c new file mode 100644 index 00000000000..64ba0fbdb7a --- /dev/null +++ b/compiler-rt/test/profile/ContinuousSyncMode/pid-substitution.c @@ -0,0 +1,34 @@ +// RUN: rm -rf %t.dir && mkdir -p %t.dir +// RUN: %clang_pgogen -o %t.exe %s +// +// Note: %%p is needed here, not %p, because of lit's path substitution. +// RUN: env LLVM_PROFILE_FILE="%t.dir/%c-%%p" %run %t.exe + +#include <stdlib.h> +#include <string.h> + +extern int __llvm_profile_is_continuous_mode_enabled(void); +extern const char *__llvm_profile_get_filename(void); +extern int getpid(void); + +int main() { + // Check that continuous mode is enabled. + if (!__llvm_profile_is_continuous_mode_enabled()) + return 1; + + // Check that the PID is actually in the filename. + const char *Filename = __llvm_profile_get_filename(); + + int Len = strlen(Filename); + --Len; + while (Filename[Len] != '-') + --Len; + + const char *PidStr = Filename + Len + 1; + int Pid = atoi(PidStr); + + if (Pid != getpid()) + return 1; + + return 0; +} diff --git a/compiler-rt/test/profile/ContinuousSyncMode/set-file-object.c b/compiler-rt/test/profile/ContinuousSyncMode/set-file-object.c new file mode 100644 index 00000000000..ac3be3b5237 --- /dev/null +++ b/compiler-rt/test/profile/ContinuousSyncMode/set-file-object.c @@ -0,0 +1,32 @@ +// RUN: %clang_pgogen -o %t.exe %s +// RUN: env LLVM_PROFILE_FILE="%c%t.profraw" %run %t.exe %t.bad 2>&1 | FileCheck %s + +// CHECK: __llvm_profile_set_file_object(fd={{[0-9]+}}) not supported +// CHECK: Profile data not written to file: already written. + +#include <stdio.h> + +extern int __llvm_profile_is_continuous_mode_enabled(void); +extern void __llvm_profile_set_file_object(FILE *, int); +extern int __llvm_profile_write_file(void); + +int main(int argc, char **argv) { + if (!__llvm_profile_is_continuous_mode_enabled()) + return 1; + + FILE *f = fopen(argv[1], "a+b"); + if (!f) + return 1; + + __llvm_profile_set_file_object(f, 0); // Try to set the file to "%t.bad". + + if (__llvm_profile_write_file() != 0) + return 1; + + f = fopen(argv[1], "r"); + if (!f) + return 1; + + fseek(f, 0, SEEK_END); + return ftell(f); // Check that the "%t.bad" is empty. +} diff --git a/compiler-rt/test/profile/ContinuousSyncMode/set-filename.c b/compiler-rt/test/profile/ContinuousSyncMode/set-filename.c new file mode 100644 index 00000000000..2e6a78950a0 --- /dev/null +++ b/compiler-rt/test/profile/ContinuousSyncMode/set-filename.c @@ -0,0 +1,17 @@ +// RUN: %clang_pgogen -o %t.exe %s +// RUN: env LLVM_PROFILE_FILE="%c%t.profraw" %run %t.exe %t.profraw %t.bad + +#include <string.h> + +extern int __llvm_profile_is_continuous_mode_enabled(void); +extern void __llvm_profile_set_filename(const char *); +extern const char *__llvm_profile_get_filename(); + +int main(int argc, char **argv) { + if (!__llvm_profile_is_continuous_mode_enabled()) + return 1; + + __llvm_profile_set_filename(argv[2]); // Try to set the filename to "%t.bad". + const char *Filename = __llvm_profile_get_filename(); + return strcmp(Filename, argv[1]); // Check that the filename is "%t.profraw". +} |