[compiler-rt] r204495 - InstrProf: Write raw binary profile from runtime

Duncan P. N. Exon Smith dexonsmith at apple.com
Fri Mar 21 11:25:56 PDT 2014


Author: dexonsmith
Date: Fri Mar 21 13:25:56 2014
New Revision: 204495

URL: http://llvm.org/viewvc/llvm-project?rev=204495&view=rev
Log:
InstrProf: Write raw binary profile from runtime

Write a raw binary profile from the runtime.

<rdar://problem/15950346>

Modified:
    compiler-rt/trunk/lib/profile/InstrProfiling.c

Modified: compiler-rt/trunk/lib/profile/InstrProfiling.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfiling.c?rev=204495&r1=204494&r2=204495&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfiling.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfiling.c Fri Mar 21 13:25:56 2014
@@ -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;
-
-  for (I = __llvm_profile_data_begin(), E = __llvm_profile_data_end();
-       I != E; ++I)
-    if (writeFunction(OutputFile, I))
-      return -1;
+  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);
+
+  /* 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;
 }





More information about the llvm-commits mailing list