perfect, a single-header wrapper around kernel perf trace events written in C.

repo

Any developer that is serious about performance is probably also serious about profiling their code. While linux provides the perf tool, it is often desirable to write custom tooling for specific systems.

The current system for doing this is poorly documented and difficult to work with. Perfect aims to provide a lightweight wrapper around these interfaces that maintains customizability and performance.

Perfect still requires knowledge of the underlying perf_event_open interfaces, but handles much of the redundant implementation and error handling. Perfect is also designed to be thread-safe.

Example of using the perfect counter interfaces to sample event counts:

#define PERFECT_INCLUDE_IMPLEMENTATION
#include "perfect.h"

struct perf_event_attr pe;
memset(&pe, 0, sizeof(struct perf_event_attr));
pe.size = sizeof(struct perf_event_attr);
pe.disabled = 1;
pe.inherit = 1;
pe.exclude_kernel = 1;
pe.exclude_guest = 1;
pe.exclude_hv = 1;
pe.type = PERF_TYPE_HARDWARE;
pe.config = PERF_COUNT_HW_BRANCH_MISSES;

Perfect_Counter branch_miss_counter;
if (perfect_counter_init(&branch_miss_counter, &pe, -1, 0, -1, 0))
{
  perfect_counter_start(&branch_miss_counter);
  {
    // ... do something ...
  }
  perfect_counter_stop(&branch_miss_counter);
  
  printf("branch misses: %lld\n", branch_miss_counter.value);

  perfect_counter_destroy(&branch_miss_counter);
}

Example of using the perfect sampling interfaces to sample real-time events:

#define PERFECT_INCLUDE_IMPLEMENTATION
#include "perfect.h"

struct perf_event_attr pe;
//.. configure perf event ...

Perfect_Sampler sampler;
if (perfect_sampler_init(&sampler, 64, &pe, -1, 0, -1, PERF_FLAG_FD_CLOEXEC))
{
  perfect_sampler_start(&sampler);

  Perfect_Sample_Iterator iter = perfect_sample_iterator(&sampler);
  while (perfect_sample_iterator_is_valid(&iter))
  {
    perfect_sample_iterator_skip(&iter, sizeof(uint64_t)); // skip time

    uint64_t count;
    perfect_sample_iterator_read(&iter, &count, sizeof(uint64_t));
    // ... read more from sample ...

    // ... persist sample ...

    perfect_sample_iterator_next(&iter);
  }

  perfect_sampler_stop(&sampler);
  perfect_sampler_destroy(&sampler);
}

incoming(1): software tools

Last update on 7E711D, edited 1 times. 3/3thh