Skip to content

Instantly share code, notes, and snippets.

@cycyyy
Last active September 26, 2019 01:15
Show Gist options
  • Save cycyyy/abf909cceaf07f76288f6bab02fd87ab to your computer and use it in GitHub Desktop.
Save cycyyy/abf909cceaf07f76288f6bab02fd87ab to your computer and use it in GitHub Desktop.
void PerfSampleWrap::signal_handler(int sig_num, siginfo_t *sig_info, void *context)
{
perf_event_mmap_page *mmap_buffer = (perf_event_mmap_page *) data->buffers[index];
while (__mmap_get_remain_size(mmap_buffer) > 0)
{
uint64_t remain = __mmap_get_remain_size(mmap_buffer);
if (remain < sizeof(perf_event_header))
{
__mmap_move_index(mmap_buffer, remain);
break;
}
PerfMySample *sample = (PerfMySample *) __mmap_get_index(mmap_buffer);
if (sample->header.type == PERF_RECORD_SAMPLE)
{
__config.handler->callback(index, sample, context);
}
else
{
if (sample->header.size == 0)
{
__mmap_move_index(mmap_buffer, remain);
break;
}
}
__mmap_move_index(mmap_buffer, sample->header.size);
}
}
void PerfSampleWrap::__mmap_move_index(perf_event_mmap_page *buffer, size_t n)
{
assert(n <= __mmap_get_remain_size(buffer));
buffer->data_tail += n;
}
void *PerfSampleWrap::__mmap_get_index(perf_event_mmap_page *buffer)
{
char *data = (char *) buffer + PAGE_SIZE;
uint64_t tail = buffer->data_tail & (PAGE_SIZE * RING_BUFFER_PAGES - 1);
return data + tail;
}
uint64_t PerfSampleWrap::__mmap_get_remain_size(perf_event_mmap_page *buffer)
{
uint64_t data_head = buffer->data_head;
__sync_synchronize();
return data_head - buffer->data_tail;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment