summaryrefslogtreecommitdiff
path: root/lib/profile/InstrProfiling.c
diff options
context:
space:
mode:
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>2014-03-21 18:25:56 +0000
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>2014-03-21 18:25:56 +0000
commit10692cccd92c84573854be22f01544324da328cf (patch)
tree247cb0d2d328afcf65285c392af33cd089c05c3d /lib/profile/InstrProfiling.c
parent7ab97504264b63fa7d99255042d1cc55ce6c9d10 (diff)
InstrProf: Write raw binary profile from runtime
Write a raw binary profile from the runtime. <rdar://problem/15950346> git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@204495 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/profile/InstrProfiling.c')
-rw-r--r--lib/profile/InstrProfiling.c69
1 files changed, 47 insertions, 22 deletions
diff --git a/lib/profile/InstrProfiling.c b/lib/profile/InstrProfiling.c
index 61981b773..a62c214c1 100644
--- a/lib/profile/InstrProfiling.c
+++ b/lib/profile/InstrProfiling.c
@@ -12,36 +12,61 @@
/* TODO: void __llvm_profile_get_size_for_buffer(void); */
-static int writeFunction(FILE *OutputFile, const __llvm_profile_data *Data) {
- /* TODO: Requires libc: break requirement by writing directly to a buffer
- * instead of a FILE stream.
- */
- uint32_t I;
- for (I = 0; I < Data->NameSize; ++I)
- if (fputc(Data->Name[I], OutputFile) != Data->Name[I])
- return -1;
- if (fprintf(OutputFile, "\n%" PRIu64 "\n%u\n", Data->FuncHash,
- Data->NumCounters) < 0)
- return -1;
- for (I = 0; I < Data->NumCounters; ++I)
- if (fprintf(OutputFile, "%" PRIu64 "\n", Data->Counters[I]) < 0)
- return -1;
- if (fprintf(OutputFile, "\n") < 0)
- return -1;
+static uint64_t getMagic(void) {
+ return
+ (uint64_t)'l' << 56 |
+ (uint64_t)'p' << 48 |
+ (uint64_t)'r' << 40 |
+ (uint64_t)'o' << 32 |
+ (uint64_t)'f' << 24 |
+ (uint64_t)'r' << 16 |
+ (uint64_t)'a' << 8 |
+ (uint64_t)'w';
+}
- return 0;
+static uint64_t getVersion(void) {
+ return 1;
}
int __llvm_profile_write_buffer(FILE *OutputFile) {
/* TODO: Requires libc: break requirement by taking a char* buffer instead of
* a FILE stream.
*/
- const __llvm_profile_data *I, *E;
+ const __llvm_profile_data *DataBegin = __llvm_profile_data_begin();
+ const __llvm_profile_data *DataEnd = __llvm_profile_data_end();
+ const uint64_t *CountersBegin = __llvm_profile_counters_begin();
+ const uint64_t *CountersEnd = __llvm_profile_counters_end();
+ const char *NamesBegin = __llvm_profile_names_begin();
+ const char *NamesEnd = __llvm_profile_names_end();
+
+ /* Calculate size of sections. */
+ const uint64_t DataSize = DataEnd - DataBegin;
+ const uint64_t CountersSize = CountersEnd - CountersBegin;
+ const uint64_t NamesSize = NamesEnd - NamesBegin;
+
+ /* Get rest of header data. */
+ const uint64_t Magic = getMagic();
+ const uint64_t Version = getVersion();
+ const uint64_t CountersDelta = (uint64_t)CountersBegin;
+ const uint64_t NamesDelta = (uint64_t)NamesBegin;
+
+#define CHECK_fwrite(Data, Size, Length, File) \
+ do { if (fwrite(Data, Size, Length, File) != Length) return -1; } while (0)
+
+ /* Write the header. */
+ CHECK_fwrite(&Magic, sizeof(uint64_t), 1, OutputFile);
+ CHECK_fwrite(&Version, sizeof(uint64_t), 1, OutputFile);
+ CHECK_fwrite(&DataSize, sizeof(uint64_t), 1, OutputFile);
+ CHECK_fwrite(&CountersSize, sizeof(uint64_t), 1, OutputFile);
+ CHECK_fwrite(&NamesSize, sizeof(uint64_t), 1, OutputFile);
+ CHECK_fwrite(&CountersDelta, sizeof(uint64_t), 1, OutputFile);
+ CHECK_fwrite(&NamesDelta, sizeof(uint64_t), 1, OutputFile);
- for (I = __llvm_profile_data_begin(), E = __llvm_profile_data_end();
- I != E; ++I)
- if (writeFunction(OutputFile, I))
- return -1;
+ /* Write the data. */
+ CHECK_fwrite(DataBegin, sizeof(__llvm_profile_data), DataSize, OutputFile);
+ CHECK_fwrite(CountersBegin, sizeof(uint64_t), CountersSize, OutputFile);
+ CHECK_fwrite(NamesBegin, sizeof(char), NamesSize, OutputFile);
+#undef CHECK_fwrite
return 0;
}