12d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines/*===- InstrProfilingBuffer.c - Write instrumentation to a memory buffer --===*\
22d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines|*
32d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines|*                     The LLVM Compiler Infrastructure
42d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines|*
52d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines|* This file is distributed under the University of Illinois Open Source
62d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines|* License. See LICENSE.TXT for details.
72d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines|*
82d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines\*===----------------------------------------------------------------------===*/
92d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines
102d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#include "InstrProfiling.h"
1186277eb844c4983c81de62d7c050e92fe7155788Stephen Hines#include "InstrProfilingInternal.h"
1286277eb844c4983c81de62d7c050e92fe7155788Stephen Hines
132d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#include <string.h>
142d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines
153d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga NainarCOMPILER_RT_VISIBILITY
162d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hinesuint64_t __llvm_profile_get_size_for_buffer(void) {
1786277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
1886277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
1986277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  const uint64_t *CountersBegin = __llvm_profile_begin_counters();
2086277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  const uint64_t *CountersEnd = __llvm_profile_end_counters();
2186277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  const char *NamesBegin = __llvm_profile_begin_names();
2286277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  const char *NamesEnd = __llvm_profile_end_names();
2386277eb844c4983c81de62d7c050e92fe7155788Stephen Hines
2486277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  return __llvm_profile_get_size_for_buffer_internal(
2586277eb844c4983c81de62d7c050e92fe7155788Stephen Hines      DataBegin, DataEnd, CountersBegin, CountersEnd, NamesBegin, NamesEnd);
2686277eb844c4983c81de62d7c050e92fe7155788Stephen Hines}
2786277eb844c4983c81de62d7c050e92fe7155788Stephen Hines
2886277eb844c4983c81de62d7c050e92fe7155788Stephen Hines#define PROFILE_RANGE_SIZE(Range) (Range##End - Range##Begin)
2986277eb844c4983c81de62d7c050e92fe7155788Stephen Hines
303d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga NainarCOMPILER_RT_VISIBILITY
3186277eb844c4983c81de62d7c050e92fe7155788Stephen Hinesuint64_t __llvm_profile_get_size_for_buffer_internal(
323d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar    const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,
333d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar    const uint64_t *CountersBegin, const uint64_t *CountersEnd,
343d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar    const char *NamesBegin, const char *NamesEnd) {
352d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines  /* Match logic in __llvm_profile_write_buffer(). */
3686277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  const uint64_t NamesSize = PROFILE_RANGE_SIZE(Names) * sizeof(char);
373d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar  const uint8_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize);
383d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar  return sizeof(__llvm_profile_header) +
393d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar         PROFILE_RANGE_SIZE(Data) * sizeof(__llvm_profile_data) +
403d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar         PROFILE_RANGE_SIZE(Counters) * sizeof(uint64_t) + NamesSize + Padding;
412d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines}
422d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines
433d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar/* The buffer writer is reponsponsible in keeping writer state
443d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar * across the call.
453d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar */
463d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainarstatic uint32_t bufferWriter(ProfDataIOVec *IOVecs, uint32_t NumIOVecs,
473d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar                             void **WriterCtx) {
483d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar  uint32_t I;
493d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar  char **Buffer = (char **)WriterCtx;
503d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar  for (I = 0; I < NumIOVecs; I++) {
513d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar    size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm;
523d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar    memcpy(*Buffer, IOVecs[I].Data, Length);
533d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar    *Buffer += Length;
543d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar  }
553d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar  return 0;
563d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar}
572d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines
583d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga NainarCOMPILER_RT_VISIBILITY int __llvm_profile_write_buffer(char *Buffer) {
593d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar  return llvmWriteProfData(bufferWriter, Buffer, 0, 0);
6086277eb844c4983c81de62d7c050e92fe7155788Stephen Hines}
6186277eb844c4983c81de62d7c050e92fe7155788Stephen Hines
623d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga NainarCOMPILER_RT_VISIBILITY int __llvm_profile_write_buffer_internal(
6386277eb844c4983c81de62d7c050e92fe7155788Stephen Hines    char *Buffer, const __llvm_profile_data *DataBegin,
6486277eb844c4983c81de62d7c050e92fe7155788Stephen Hines    const __llvm_profile_data *DataEnd, const uint64_t *CountersBegin,
6586277eb844c4983c81de62d7c050e92fe7155788Stephen Hines    const uint64_t *CountersEnd, const char *NamesBegin, const char *NamesEnd) {
663d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar  return llvmWriteProfDataImpl(bufferWriter, Buffer, DataBegin, DataEnd,
673d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar                               CountersBegin, CountersEnd, 0, 0, NamesBegin,
683d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar                               NamesEnd);
692d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines}
70