2014-03-22 02:29:15 +08:00
|
|
|
/*===- InstrProfilingBuffer.c - Write instrumentation to a memory buffer --===*\
|
|
|
|
|*
|
2019-01-19 16:50:56 +08:00
|
|
|
|* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
|* See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-03-22 02:29:15 +08:00
|
|
|
|*
|
|
|
|
\*===----------------------------------------------------------------------===*/
|
|
|
|
|
|
|
|
#include "InstrProfiling.h"
|
2014-12-10 06:07:25 +08:00
|
|
|
#include "InstrProfilingInternal.h"
|
[profile] Add a mode to continuously sync counter updates to a file
Add support for continuously syncing profile counter updates to a file.
The motivation for this is that programs do not always exit cleanly. On
iOS, for example, programs are usually killed via a signal from the OS.
Running atexit() handlers after catching a signal is unreliable, so some
method for progressively writing out profile data is necessary.
The approach taken here is to mmap() the `__llvm_prf_cnts` section onto
a raw profile. To do this, the linker must page-align the counter and
data sections, and the runtime must ensure that counters are mapped to a
page-aligned offset within a raw profile.
Continuous mode is (for the moment) incompatible with the online merging
mode. This limitation is lifted in https://reviews.llvm.org/D69586.
Continuous mode is also (for the moment) incompatible with value
profiling, as I'm not sure whether there is interest in this and the
implementation may be tricky.
As I have not been able to test extensively on non-Darwin platforms,
only Darwin support is included for the moment. However, continuous mode
may "just work" without modification on Linux and some UNIX-likes. AIUI
the default value for the GNU linker's `--section-alignment` flag is set
to the page size on many systems. This appears to be true for LLD as
well, as its `no_nmagic` option is on by default. Continuous mode will
not "just work" on Fuchsia or Windows, as it's not possible to mmap() a
section on these platforms. There is a proposal to add a layer of
indirection to the profile instrumentation to support these platforms.
rdar://54210980
Differential Revision: https://reviews.llvm.org/D68351
2019-09-20 02:56:43 +08:00
|
|
|
#include "InstrProfilingPort.h"
|
|
|
|
|
2019-10-05 04:29:56 +08:00
|
|
|
/* When counters are being relocated at runtime, this parameter is set to 1. */
|
|
|
|
COMPILER_RT_VISIBILITY int RuntimeCounterRelocation = 0;
|
|
|
|
|
2019-11-13 02:24:23 +08:00
|
|
|
/* When continuous mode is enabled (%c), this parameter is set to 1.
|
[profile] Add a mode to continuously sync counter updates to a file
Add support for continuously syncing profile counter updates to a file.
The motivation for this is that programs do not always exit cleanly. On
iOS, for example, programs are usually killed via a signal from the OS.
Running atexit() handlers after catching a signal is unreliable, so some
method for progressively writing out profile data is necessary.
The approach taken here is to mmap() the `__llvm_prf_cnts` section onto
a raw profile. To do this, the linker must page-align the counter and
data sections, and the runtime must ensure that counters are mapped to a
page-aligned offset within a raw profile.
Continuous mode is (for the moment) incompatible with the online merging
mode. This limitation is lifted in https://reviews.llvm.org/D69586.
Continuous mode is also (for the moment) incompatible with value
profiling, as I'm not sure whether there is interest in this and the
implementation may be tricky.
As I have not been able to test extensively on non-Darwin platforms,
only Darwin support is included for the moment. However, continuous mode
may "just work" without modification on Linux and some UNIX-likes. AIUI
the default value for the GNU linker's `--section-alignment` flag is set
to the page size on many systems. This appears to be true for LLD as
well, as its `no_nmagic` option is on by default. Continuous mode will
not "just work" on Fuchsia or Windows, as it's not possible to mmap() a
section on these platforms. There is a proposal to add a layer of
indirection to the profile instrumentation to support these platforms.
rdar://54210980
Differential Revision: https://reviews.llvm.org/D68351
2019-09-20 02:56:43 +08:00
|
|
|
*
|
|
|
|
* This parameter is defined here in InstrProfilingBuffer.o, instead of in
|
|
|
|
* InstrProfilingFile.o, to sequester all libc-dependent code in
|
|
|
|
* InstrProfilingFile.o. The test `instrprof-without-libc` will break if this
|
|
|
|
* layering is violated. */
|
|
|
|
static int ContinuouslySyncProfile = 0;
|
|
|
|
|
|
|
|
COMPILER_RT_VISIBILITY int __llvm_profile_is_continuous_mode_enabled(void) {
|
|
|
|
return ContinuouslySyncProfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
COMPILER_RT_VISIBILITY void __llvm_profile_enable_continuous_mode(void) {
|
|
|
|
ContinuouslySyncProfile = 1;
|
|
|
|
}
|
2014-12-10 06:07:25 +08:00
|
|
|
|
2015-12-16 11:29:15 +08:00
|
|
|
COMPILER_RT_VISIBILITY
|
2014-03-22 02:29:22 +08:00
|
|
|
uint64_t __llvm_profile_get_size_for_buffer(void) {
|
2014-12-10 06:07:25 +08:00
|
|
|
const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
|
|
|
|
const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
|
|
|
|
const uint64_t *CountersBegin = __llvm_profile_begin_counters();
|
|
|
|
const uint64_t *CountersEnd = __llvm_profile_end_counters();
|
|
|
|
const char *NamesBegin = __llvm_profile_begin_names();
|
|
|
|
const char *NamesEnd = __llvm_profile_end_names();
|
|
|
|
|
|
|
|
return __llvm_profile_get_size_for_buffer_internal(
|
|
|
|
DataBegin, DataEnd, CountersBegin, CountersEnd, NamesBegin, NamesEnd);
|
|
|
|
}
|
|
|
|
|
2016-02-26 10:49:41 +08:00
|
|
|
COMPILER_RT_VISIBILITY
|
|
|
|
uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
|
|
|
|
const __llvm_profile_data *End) {
|
|
|
|
intptr_t BeginI = (intptr_t)Begin, EndI = (intptr_t)End;
|
|
|
|
return ((EndI + sizeof(__llvm_profile_data) - 1) - BeginI) /
|
|
|
|
sizeof(__llvm_profile_data);
|
|
|
|
}
|
2014-12-10 06:07:25 +08:00
|
|
|
|
[profile] Add a mode to continuously sync counter updates to a file
Add support for continuously syncing profile counter updates to a file.
The motivation for this is that programs do not always exit cleanly. On
iOS, for example, programs are usually killed via a signal from the OS.
Running atexit() handlers after catching a signal is unreliable, so some
method for progressively writing out profile data is necessary.
The approach taken here is to mmap() the `__llvm_prf_cnts` section onto
a raw profile. To do this, the linker must page-align the counter and
data sections, and the runtime must ensure that counters are mapped to a
page-aligned offset within a raw profile.
Continuous mode is (for the moment) incompatible with the online merging
mode. This limitation is lifted in https://reviews.llvm.org/D69586.
Continuous mode is also (for the moment) incompatible with value
profiling, as I'm not sure whether there is interest in this and the
implementation may be tricky.
As I have not been able to test extensively on non-Darwin platforms,
only Darwin support is included for the moment. However, continuous mode
may "just work" without modification on Linux and some UNIX-likes. AIUI
the default value for the GNU linker's `--section-alignment` flag is set
to the page size on many systems. This appears to be true for LLD as
well, as its `no_nmagic` option is on by default. Continuous mode will
not "just work" on Fuchsia or Windows, as it's not possible to mmap() a
section on these platforms. There is a proposal to add a layer of
indirection to the profile instrumentation to support these platforms.
rdar://54210980
Differential Revision: https://reviews.llvm.org/D68351
2019-09-20 02:56:43 +08:00
|
|
|
/// Calculate the number of padding bytes needed to add to \p Offset in order
|
|
|
|
/// for (\p Offset + Padding) to be page-aligned.
|
|
|
|
static uint64_t calculateBytesNeededToPageAlign(uint64_t Offset,
|
|
|
|
unsigned PageSize) {
|
|
|
|
uint64_t OffsetModPage = Offset % PageSize;
|
|
|
|
if (OffsetModPage > 0)
|
|
|
|
return PageSize - OffsetModPage;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
COMPILER_RT_VISIBILITY
|
|
|
|
void __llvm_profile_get_padding_sizes_for_counters(
|
|
|
|
uint64_t DataSize, uint64_t CountersSize, uint64_t NamesSize,
|
|
|
|
uint64_t *PaddingBytesBeforeCounters, uint64_t *PaddingBytesAfterCounters,
|
|
|
|
uint64_t *PaddingBytesAfterNames) {
|
2019-10-05 04:29:56 +08:00
|
|
|
if (!__llvm_profile_is_continuous_mode_enabled() ||
|
|
|
|
RuntimeCounterRelocation) {
|
[profile] Add a mode to continuously sync counter updates to a file
Add support for continuously syncing profile counter updates to a file.
The motivation for this is that programs do not always exit cleanly. On
iOS, for example, programs are usually killed via a signal from the OS.
Running atexit() handlers after catching a signal is unreliable, so some
method for progressively writing out profile data is necessary.
The approach taken here is to mmap() the `__llvm_prf_cnts` section onto
a raw profile. To do this, the linker must page-align the counter and
data sections, and the runtime must ensure that counters are mapped to a
page-aligned offset within a raw profile.
Continuous mode is (for the moment) incompatible with the online merging
mode. This limitation is lifted in https://reviews.llvm.org/D69586.
Continuous mode is also (for the moment) incompatible with value
profiling, as I'm not sure whether there is interest in this and the
implementation may be tricky.
As I have not been able to test extensively on non-Darwin platforms,
only Darwin support is included for the moment. However, continuous mode
may "just work" without modification on Linux and some UNIX-likes. AIUI
the default value for the GNU linker's `--section-alignment` flag is set
to the page size on many systems. This appears to be true for LLD as
well, as its `no_nmagic` option is on by default. Continuous mode will
not "just work" on Fuchsia or Windows, as it's not possible to mmap() a
section on these platforms. There is a proposal to add a layer of
indirection to the profile instrumentation to support these platforms.
rdar://54210980
Differential Revision: https://reviews.llvm.org/D68351
2019-09-20 02:56:43 +08:00
|
|
|
*PaddingBytesBeforeCounters = 0;
|
|
|
|
*PaddingBytesAfterCounters = 0;
|
|
|
|
*PaddingBytesAfterNames = __llvm_profile_get_num_padding_bytes(NamesSize);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In continuous mode, the file offsets for headers and for the start of
|
|
|
|
// counter sections need to be page-aligned.
|
|
|
|
unsigned PageSize = getpagesize();
|
|
|
|
uint64_t DataSizeInBytes = DataSize * sizeof(__llvm_profile_data);
|
|
|
|
uint64_t CountersSizeInBytes = CountersSize * sizeof(uint64_t);
|
|
|
|
*PaddingBytesBeforeCounters = calculateBytesNeededToPageAlign(
|
|
|
|
sizeof(__llvm_profile_header) + DataSizeInBytes, PageSize);
|
|
|
|
*PaddingBytesAfterCounters =
|
|
|
|
calculateBytesNeededToPageAlign(CountersSizeInBytes, PageSize);
|
|
|
|
*PaddingBytesAfterNames =
|
|
|
|
calculateBytesNeededToPageAlign(NamesSize, PageSize);
|
|
|
|
}
|
|
|
|
|
2015-12-16 11:29:15 +08:00
|
|
|
COMPILER_RT_VISIBILITY
|
2014-12-10 06:07:25 +08:00
|
|
|
uint64_t __llvm_profile_get_size_for_buffer_internal(
|
2015-11-23 12:38:17 +08:00
|
|
|
const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,
|
|
|
|
const uint64_t *CountersBegin, const uint64_t *CountersEnd,
|
|
|
|
const char *NamesBegin, const char *NamesEnd) {
|
2014-03-22 02:29:24 +08:00
|
|
|
/* Match logic in __llvm_profile_write_buffer(). */
|
2016-02-26 10:49:41 +08:00
|
|
|
const uint64_t NamesSize = (NamesEnd - NamesBegin) * sizeof(char);
|
[profile] Add a mode to continuously sync counter updates to a file
Add support for continuously syncing profile counter updates to a file.
The motivation for this is that programs do not always exit cleanly. On
iOS, for example, programs are usually killed via a signal from the OS.
Running atexit() handlers after catching a signal is unreliable, so some
method for progressively writing out profile data is necessary.
The approach taken here is to mmap() the `__llvm_prf_cnts` section onto
a raw profile. To do this, the linker must page-align the counter and
data sections, and the runtime must ensure that counters are mapped to a
page-aligned offset within a raw profile.
Continuous mode is (for the moment) incompatible with the online merging
mode. This limitation is lifted in https://reviews.llvm.org/D69586.
Continuous mode is also (for the moment) incompatible with value
profiling, as I'm not sure whether there is interest in this and the
implementation may be tricky.
As I have not been able to test extensively on non-Darwin platforms,
only Darwin support is included for the moment. However, continuous mode
may "just work" without modification on Linux and some UNIX-likes. AIUI
the default value for the GNU linker's `--section-alignment` flag is set
to the page size on many systems. This appears to be true for LLD as
well, as its `no_nmagic` option is on by default. Continuous mode will
not "just work" on Fuchsia or Windows, as it's not possible to mmap() a
section on these platforms. There is a proposal to add a layer of
indirection to the profile instrumentation to support these platforms.
rdar://54210980
Differential Revision: https://reviews.llvm.org/D68351
2019-09-20 02:56:43 +08:00
|
|
|
uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
|
|
|
|
uint64_t CountersSize = CountersEnd - CountersBegin;
|
|
|
|
|
|
|
|
/* Determine how much padding is needed before/after the counters and after
|
|
|
|
* the names. */
|
|
|
|
uint64_t PaddingBytesBeforeCounters, PaddingBytesAfterCounters,
|
|
|
|
PaddingBytesAfterNames;
|
|
|
|
__llvm_profile_get_padding_sizes_for_counters(
|
|
|
|
DataSize, CountersSize, NamesSize, &PaddingBytesBeforeCounters,
|
|
|
|
&PaddingBytesAfterCounters, &PaddingBytesAfterNames);
|
|
|
|
|
2015-10-17 06:21:56 +08:00
|
|
|
return sizeof(__llvm_profile_header) +
|
[profile] Add a mode to continuously sync counter updates to a file
Add support for continuously syncing profile counter updates to a file.
The motivation for this is that programs do not always exit cleanly. On
iOS, for example, programs are usually killed via a signal from the OS.
Running atexit() handlers after catching a signal is unreliable, so some
method for progressively writing out profile data is necessary.
The approach taken here is to mmap() the `__llvm_prf_cnts` section onto
a raw profile. To do this, the linker must page-align the counter and
data sections, and the runtime must ensure that counters are mapped to a
page-aligned offset within a raw profile.
Continuous mode is (for the moment) incompatible with the online merging
mode. This limitation is lifted in https://reviews.llvm.org/D69586.
Continuous mode is also (for the moment) incompatible with value
profiling, as I'm not sure whether there is interest in this and the
implementation may be tricky.
As I have not been able to test extensively on non-Darwin platforms,
only Darwin support is included for the moment. However, continuous mode
may "just work" without modification on Linux and some UNIX-likes. AIUI
the default value for the GNU linker's `--section-alignment` flag is set
to the page size on many systems. This appears to be true for LLD as
well, as its `no_nmagic` option is on by default. Continuous mode will
not "just work" on Fuchsia or Windows, as it's not possible to mmap() a
section on these platforms. There is a proposal to add a layer of
indirection to the profile instrumentation to support these platforms.
rdar://54210980
Differential Revision: https://reviews.llvm.org/D68351
2019-09-20 02:56:43 +08:00
|
|
|
(DataSize * sizeof(__llvm_profile_data)) + PaddingBytesBeforeCounters +
|
|
|
|
(CountersSize * sizeof(uint64_t)) + PaddingBytesAfterCounters +
|
|
|
|
NamesSize + PaddingBytesAfterNames;
|
2014-03-22 02:29:22 +08:00
|
|
|
}
|
|
|
|
|
2017-06-28 01:28:01 +08:00
|
|
|
COMPILER_RT_VISIBILITY
|
|
|
|
void initBufferWriter(ProfDataWriter *BufferWriter, char *Buffer) {
|
|
|
|
BufferWriter->Write = lprofBufferWriter;
|
|
|
|
BufferWriter->WriterCtx = Buffer;
|
|
|
|
}
|
|
|
|
|
2015-12-16 11:29:15 +08:00
|
|
|
COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer(char *Buffer) {
|
2017-06-28 01:28:01 +08:00
|
|
|
ProfDataWriter BufferWriter;
|
|
|
|
initBufferWriter(&BufferWriter, Buffer);
|
2017-06-29 00:46:06 +08:00
|
|
|
return lprofWriteData(&BufferWriter, 0, 0);
|
2014-12-10 06:07:25 +08:00
|
|
|
}
|
|
|
|
|
2015-12-16 11:29:15 +08:00
|
|
|
COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer_internal(
|
2014-12-10 06:07:25 +08:00
|
|
|
char *Buffer, const __llvm_profile_data *DataBegin,
|
|
|
|
const __llvm_profile_data *DataEnd, const uint64_t *CountersBegin,
|
|
|
|
const uint64_t *CountersEnd, const char *NamesBegin, const char *NamesEnd) {
|
2017-06-28 01:28:01 +08:00
|
|
|
ProfDataWriter BufferWriter;
|
|
|
|
initBufferWriter(&BufferWriter, Buffer);
|
|
|
|
return lprofWriteDataImpl(&BufferWriter, DataBegin, DataEnd, CountersBegin,
|
2017-06-29 00:46:06 +08:00
|
|
|
CountersEnd, 0, NamesBegin, NamesEnd, 0);
|
2014-03-22 02:29:22 +08:00
|
|
|
}
|