InstrProf: Reorganize files; no functionality change

Move functions around to prepare for some other changes.

  - Merge InstrProfilingExtras.h with InstrProfiling.h.  There's no
    benefit to having these split.

  - Rename InstrProfilingExtras.c to InstrProfilingFile.c.

  - Split actual buffer writing code out of InstrProfiling.c into
    InstrProfilingBuffer.c.

  - Drive-by corrections of a couple of header comments.

<rdar://problem/15943240>

llvm-svn: 204497
This commit is contained in:
Duncan P. N. Exon Smith 2014-03-21 18:29:15 +00:00
parent 24b4b65339
commit be0a5e176b
10 changed files with 129 additions and 109 deletions

View File

@ -6,9 +6,10 @@ if(APPLE)
set(PROFILE_SOURCES
GCDAProfiling.c
InstrProfiling.c
InstrProfilingBuffer.c
InstrProfilingFile.c
InstrProfilingPlatformDarwin.c
InstrProfilingRuntime.cc
InstrProfilingExtras.c)
InstrProfilingRuntime.cc)
add_compiler_rt_osx_static_runtime(clang_rt.profile_osx
ARCH ${PROFILE_SUPPORTED_ARCH}
@ -18,9 +19,10 @@ else()
set(PROFILE_SOURCES
GCDAProfiling.c
InstrProfiling.c
InstrProfilingBuffer.c
InstrProfilingFile.c
InstrProfilingPlatformOther.c
InstrProfilingRuntime.cc
InstrProfilingExtras.c)
InstrProfilingRuntime.cc)
foreach(arch ${PROFILE_SUPPORTED_ARCH})
add_compiler_rt_static_runtime(clang_rt.profile-${arch}

View File

@ -10,9 +10,8 @@
#include "InstrProfiling.h"
#include <string.h>
/* TODO: void __llvm_profile_get_size_for_buffer(void); */
static uint64_t getMagic(void) {
uint64_t __llvm_profile_get_magic(void) {
/* Magic number to detect file format and endianness. */
return
(uint64_t)'l' << 56 |
(uint64_t)'p' << 48 |
@ -24,53 +23,11 @@ static uint64_t getMagic(void) {
(uint64_t)'w';
}
static uint64_t getVersion(void) {
uint64_t __llvm_profile_get_version(void) {
/* This should be bumped any time the output format changes. */
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 *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;
}
void __llvm_profile_reset_counters(void) {
uint64_t *I = __llvm_profile_counters_begin();
uint64_t *E = __llvm_profile_counters_end();

View File

@ -7,8 +7,10 @@
|*
\*===----------------------------------------------------------------------===*/
#ifndef PROFILE_INSTRPROFILING_H__
#define PROFILE_INSTRPROFILING_H__
#include <stdio.h>
#include <stdlib.h>
#define I386_FREEBSD (defined(__FreeBSD__) && defined(__i386__))
@ -40,7 +42,7 @@ typedef struct __llvm_profile_data {
uint64_t *const Counters;
} __llvm_profile_data;
/* TODO: void __llvm_profile_get_size_for_buffer(void); */
/* TODO: void __llvm_profile_get_size_for_buffer(void); */
/*!
* \brief Write instrumentation data to the given buffer.
@ -57,3 +59,37 @@ const char *__llvm_profile_names_begin(void);
const char *__llvm_profile_names_end(void);
uint64_t *__llvm_profile_counters_begin(void);
uint64_t *__llvm_profile_counters_end(void);
#define PROFILE_RANGE_SIZE(Range) \
(__llvm_profile_ ## Range ## _end() - __llvm_profile_ ## Range ## _begin())
/*!
* \brief Write instrumentation data to the current file.
*
* Writes to the file with the last name given to \a __llvm_profile_set_filename(),
* or if it hasn't been called, the \c LLVM_PROFILE_FILE environment variable,
* or if that's not set, \c "default.profdata".
*/
int __llvm_profile_write_file(void);
/*!
* \brief Set the filename for writing instrumentation data.
*
* Sets the filename to be used for subsequent calls to
* \a __llvm_profile_write_file().
*
* \c Name is not copied, so it must remain valid. Passing NULL resets the
* filename logic to the default behaviour.
*/
void __llvm_profile_set_filename(const char *Name);
/*! \brief Register to write instrumentation data to file at exit. */
int __llvm_profile_register_write_file_atexit(void);
/*! \brief Get the magic token for the file format. */
uint64_t __llvm_profile_get_magic(void);
/*! \brief Get the version of the file format. */
uint64_t __llvm_profile_get_version(void);
#endif /* PROFILE_INSTRPROFILING_H__ */

View File

@ -0,0 +1,54 @@
/*===- InstrProfilingBuffer.c - Write instrumentation to a memory buffer --===*\
|*
|* The LLVM Compiler Infrastructure
|*
|* This file is distributed under the University of Illinois Open Source
|* License. See LICENSE.TXT for details.
|*
\*===----------------------------------------------------------------------===*/
#include "InstrProfiling.h"
#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;
}

View File

@ -1,31 +0,0 @@
/*===- InstrProfilingExtras.h - Support library for PGO instrumentation ---===*\
|*
|* The LLVM Compiler Infrastructure
|*
|* This file is distributed under the University of Illinois Open Source
|* License. See LICENSE.TXT for details.
|*
\*===----------------------------------------------------------------------===*/
/*!
* \brief Write instrumentation data to the current file.
*
* Writes to the file with the last name given to \a __llvm_profile_set_filename(),
* or if it hasn't been called, the \c LLVM_PROFILE_FILE environment variable,
* or if that's not set, \c "default.profdata".
*/
int __llvm_profile_write_file(void);
/*!
* \brief Set the filename for writing instrumentation data.
*
* Sets the filename to be used for subsequent calls to
* \a __llvm_profile_write_file().
*
* \c Name is not copied, so it must remain valid. Passing NULL resets the
* filename logic to the default behaviour.
*/
void __llvm_profile_set_filename(const char *Name);
/*! \brief Register to write instrumentation data to file at exit. */
int __llvm_profile_register_write_file_atexit(void);

View File

@ -1,4 +1,4 @@
/*===- InstrProfilingExtras.c - Support library for PGO instrumentation ---===*\
/*===- InstrProfilingFile.c - Write instrumentation to a file -------------===*\
|*
|* The LLVM Compiler Infrastructure
|*
@ -8,9 +8,11 @@
\*===----------------------------------------------------------------------===*/
#include "InstrProfiling.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int __llvm_profile_write_file_with_name(const char *OutputName) {
static int writeFileWithName(const char *OutputName) {
int RetVal;
FILE *OutputFile;
if (!OutputName || !OutputName[0])
@ -19,9 +21,6 @@ static int __llvm_profile_write_file_with_name(const char *OutputName) {
if (!OutputFile)
return -1;
/* TODO: mmap file to buffer of size __llvm_profile_get_size_for_buffer() and
* pass the buffer in, instead of the file.
*/
RetVal = __llvm_profile_write_buffer(OutputFile);
fclose(OutputFile);
@ -44,7 +43,7 @@ int __llvm_profile_write_file(void) {
int PidLength = 0;
int NumPids = 0;
// Get the filename.
/* Get the filename. */
const char *Filename = CurrentFilename;
#define UPDATE_FILENAME(NextFilename) \
if (!Filename || !Filename[0]) Filename = NextFilename
@ -52,7 +51,7 @@ int __llvm_profile_write_file(void) {
UPDATE_FILENAME("default.profdata");
#undef UPDATE_FILENAME
// Check the filename for "%p", which indicates a pid-substitution.
/* Check the filename for "%p", which indicates a pid-substitution. */
for (I = 0; Filename[I]; ++I)
if (Filename[I] == '%' && Filename[++I] == 'p')
if (!NumPids++) {
@ -61,31 +60,31 @@ int __llvm_profile_write_file(void) {
return -1;
}
if (NumPids) {
// Allocate enough space for the substituted filename.
/* Allocate enough space for the substituted filename. */
AllocatedFilename = (char*)malloc(I + NumPids*(PidLength - 2) + 1);
if (!AllocatedFilename)
return -1;
// Construct the new filename.
/* Construct the new filename. */
for (I = 0, J = 0; Filename[I]; ++I)
if (Filename[I] == '%') {
if (Filename[++I] == 'p') {
memcpy(AllocatedFilename + J, PidChars, PidLength);
J += PidLength;
}
// Drop any unknown substitutions.
/* Drop any unknown substitutions. */
} else
AllocatedFilename[J++] = Filename[I];
AllocatedFilename[J] = 0;
// Actually use the computed name.
/* Actually use the computed name. */
Filename = AllocatedFilename;
}
// Write the file.
RetVal = __llvm_profile_write_file_with_name(Filename);
/* Write the file. */
RetVal = writeFileWithName(Filename);
// Free the filename.
/* Free the filename. */
if (AllocatedFilename)
free(AllocatedFilename);
@ -95,11 +94,13 @@ int __llvm_profile_write_file(void) {
static void writeFileWithoutReturn(void) {
__llvm_profile_write_file();
}
void __llvm_profile_register_write_file_atexit(void) {
int __llvm_profile_register_write_file_atexit(void) {
static int HasBeenRegistered = 0;
if (!HasBeenRegistered) {
HasBeenRegistered = 1;
atexit(writeFileWithoutReturn);
}
if (HasBeenRegistered)
return 0;
HasBeenRegistered = 1;
return atexit(writeFileWithoutReturn);
}

View File

@ -1,4 +1,4 @@
/*===- InstrProfilingDarwin.c - Profile data on Darwin --------------------===*\
/*===- InstrProfilingPlatformDarwin.c - Profile data on Darwin ------------===*\
|*
|* The LLVM Compiler Infrastructure
|*

View File

@ -1,4 +1,4 @@
/*===- InstrProfilingDefault.c - Profile data default platfrom ------------===*\
/*===- InstrProfilingPlatformOther.c - Profile data default platfrom ------===*\
|*
|* The LLVM Compiler Infrastructure
|*
@ -8,6 +8,7 @@
\*===----------------------------------------------------------------------===*/
#include "InstrProfiling.h"
#include <stdlib.h>
static const __llvm_profile_data *DataFirst = NULL;
static const __llvm_profile_data *DataLast = NULL;

View File

@ -9,7 +9,7 @@
extern "C" {
#include "InstrProfilingExtras.h"
#include "InstrProfiling.h"
extern int __llvm_profile_runtime;
int __llvm_profile_runtime;

View File

@ -222,9 +222,9 @@ FUNCTIONS.ios.x86_64h := $(FUNCTIONS.ios.x86_64)
FUNCTIONS.osx := mulosi4 mulodi4 muloti4
FUNCTIONS.profile_osx := GCDAProfiling InstrProfiling \
InstrProfilingPlatformDarwin InstrProfilingRuntime \
InstrProfilingExtras
FUNCTIONS.profile_osx := GCDAProfiling InstrProfiling InstrProfilingBuffer \
InstrProfilingFile InstrProfilingPlatformDarwin \
InstrProfilingRuntime
FUNCTIONS.profile_ios := $(FUNCTIONS.profile_osx)
FUNCTIONS.asan_osx_dynamic := $(AsanFunctions) $(InterceptionFunctions) \