• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_profgen -O2 -o %t %s
2 // RUN: %run %t %t.profraw 1 1
3 // RUN: llvm-profdata show --all-functions --counts %t.profraw  | FileCheck %s
4 
5 // FIXME: llvm-profdata exits with "Malformed instrumentation profile data"
6 // XFAIL: msvc
7 
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include "profile_test.h"
12 
13 int __llvm_profile_runtime = 0;
14 uint64_t __llvm_profile_get_size_for_buffer(void);
15 int __llvm_profile_write_buffer(char *);
16 void __llvm_profile_reset_counters(void);
17 void __llvm_profile_merge_from_buffer(const char *, uint64_t);
18 
dumpBuffer(const char * FileN,const char * Buffer,uint64_t Size)19 int dumpBuffer(const char *FileN, const char *Buffer, uint64_t Size) {
20   FILE *File = fopen(FileN, "w");
21   if (!File)
22     return 1;
23   if (fwrite(Buffer, 1, Size, File) != Size)
24     return 1;
25   return fclose(File);
26 }
27 
28 int g = 0;
foo(char c)29 void foo(char c) {
30   if (c == '1')
31     g++;
32   else
33     g--;
34 }
35 
36 /* This function is not profiled */
bar(int M)37 void bar(int M) { g += M; }
38 
main(int argc,const char * argv[])39 int main(int argc, const char *argv[]) {
40   int i;
41   if (argc < 4)
42     return 1;
43 
44   const uint64_t MaxSize = 10000;
45   static ALIGNED(sizeof(uint64_t)) char Buffer[MaxSize];
46 
47   uint64_t Size = __llvm_profile_get_size_for_buffer();
48   if (Size > MaxSize)
49     return 1;
50 
51   /* Start profiling. */
52   __llvm_profile_reset_counters();
53   foo(argv[2][0]);
54   /* End profiling by freezing counters. */
55   if (__llvm_profile_write_buffer(Buffer))
56     return 1;
57 
58   /* Its profile will be discarded. */
59   for (i = 0; i < 10; i++)
60     bar(1);
61 
62   /* Start profiling again and merge in previously
63      saved counters in buffer. */
64   __llvm_profile_reset_counters();
65   __llvm_profile_merge_from_buffer(Buffer, Size);
66   foo(argv[3][0]);
67   /* End profiling */
68   if (__llvm_profile_write_buffer(Buffer))
69     return 1;
70 
71   /* Its profile will be discarded. */
72   bar(2);
73 
74   /* Now it is time to dump the profile to file.  */
75   return dumpBuffer(argv[1], Buffer, Size);
76 }
77 
78 // Not profiled
79 // CHECK-LABEL: dumpBuffer:
80 // CHECK:        Counters: 3
81 // CHECK-NEXT:   Function count: 0
82 // CHECK-NEXT:   Block counts: [0, 0]
83 
84 // Profiled with entry count == 2
85 // CHECK-LABEL:  foo:
86 // CHECK:         Counters: 2
87 // CHECK-NEXT:    Function count: 2
88 // CHECK-NEXT:    Block counts: [2]
89 
90 // Not profiled
91 // CHECK-LABEL:  bar:
92 // CHECK:         Counters: 1
93 // CHECK-NEXT:    Function count: 0
94 // CHECK-NEXT:    Block counts: []
95 
96 // Not profiled
97 // CHECK-LABEL:  main:
98 // CHECK:         Counters: 6
99 // CHECK-NEXT:    Function count: 0
100 // CHECK-NEXT:    Block counts: [0, 0, 0, 0, 0]
101