[compiler-rt] r204498 - InstrProf: If libc is available, use it; no functionality change

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


Author: dexonsmith
Date: Fri Mar 21 13:29:19 2014
New Revision: 204498

URL: http://llvm.org/viewvc/llvm-project?rev=204498&view=rev
Log:
InstrProf: If libc is available, use it; no functionality change

It was misguided to plan to rely on __llvm_profile_write_buffer() in
__llvm_profile_write_file().  It's less complex to duplicate the writing
logic than to mmap the file.

Since it's here to stay, move `FILE*`-based writing logic into
InstrProfilingFile.c.

<rdar://problem/15943240>

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

Modified: compiler-rt/trunk/lib/profile/InstrProfiling.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfiling.h?rev=204498&r1=204497&r2=204498&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfiling.h (original)
+++ compiler-rt/trunk/lib/profile/InstrProfiling.h Fri Mar 21 13:29:19 2014
@@ -10,8 +10,6 @@
 #ifndef PROFILE_INSTRPROFILING_H__
 #define PROFILE_INSTRPROFILING_H__
 
-#include <stdio.h>
-
 #define I386_FREEBSD (defined(__FreeBSD__) && defined(__i386__))
 
 #if !I386_FREEBSD
@@ -42,16 +40,18 @@ typedef struct __llvm_profile_data {
   uint64_t *const Counters;
 } __llvm_profile_data;
 
-/* TODO: void __llvm_profile_get_size_for_buffer(void); */
+/*!
+ * \brief Get required size for profile buffer.
+ */
+uint64_t __llvm_profile_get_size_for_buffer(void);
 
 /*!
  * \brief Write instrumentation data to the given buffer.
  *
- * This function is currently broken:  it shouldn't rely on libc, but it does.
- * It should be changed to take a char* buffer, and write binary data directly
- * to it.
+ * \pre \c Buffer is the start of a buffer at least as big as \a
+ * __llvm_profile_get_size_for_buffer().
  */
-int __llvm_profile_write_buffer(FILE *OutputFile);
+int __llvm_profile_write_buffer(char *Buffer);
 
 const __llvm_profile_data *__llvm_profile_data_begin(void);
 const __llvm_profile_data *__llvm_profile_data_end(void);

Modified: compiler-rt/trunk/lib/profile/InstrProfilingBuffer.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingBuffer.c?rev=204498&r1=204497&r2=204498&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingBuffer.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingBuffer.c Fri Mar 21 13:29:19 2014
@@ -11,44 +11,4 @@
 #include <string.h>
 
 /* TODO: uint64_t __llvm_profile_get_size_for_buffer(void) */
-
-int __llvm_profile_write_buffer(FILE *OutputFile) {
-  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 = __llvm_profile_get_magic();
-  const uint64_t Version = __llvm_profile_get_version();
-  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;
-}
+/* TODO: int __llvm_profile_write_buffer(char *Buffer) */

Modified: compiler-rt/trunk/lib/profile/InstrProfilingFile.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingFile.c?rev=204498&r1=204497&r2=204498&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingFile.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingFile.c Fri Mar 21 13:29:19 2014
@@ -12,6 +12,47 @@
 #include <stdlib.h>
 #include <string.h>
 
+static int writeFile(FILE *File) {
+  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 = __llvm_profile_get_magic();
+  const uint64_t Version = __llvm_profile_get_version();
+  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, File);
+  CHECK_fwrite(&Version,       sizeof(uint64_t), 1, File);
+  CHECK_fwrite(&DataSize,      sizeof(uint64_t), 1, File);
+  CHECK_fwrite(&CountersSize,  sizeof(uint64_t), 1, File);
+  CHECK_fwrite(&NamesSize,     sizeof(uint64_t), 1, File);
+  CHECK_fwrite(&CountersDelta, sizeof(uint64_t), 1, File);
+  CHECK_fwrite(&NamesDelta,    sizeof(uint64_t), 1, File);
+
+  /* Write the data. */
+  CHECK_fwrite(DataBegin, sizeof(__llvm_profile_data), DataSize, File);
+  CHECK_fwrite(CountersBegin, sizeof(uint64_t), CountersSize, File);
+  CHECK_fwrite(NamesBegin, sizeof(char), NamesSize, File);
+
+#undef CHECK_fwrite
+
+   return 0;
+}
+
 static int writeFileWithName(const char *OutputName) {
   int RetVal;
   FILE *OutputFile;
@@ -21,7 +62,7 @@ static int writeFileWithName(const char
   if (!OutputFile)
     return -1;
 
-  RetVal = __llvm_profile_write_buffer(OutputFile);
+  RetVal = writeFile(OutputFile);
 
   fclose(OutputFile);
   return RetVal;





More information about the llvm-commits mailing list