Commit cab4d7c6 authored by 4ast's avatar 4ast Committed by GitHub

Merge pull request #1595 from iovisor/yhs_dev

fix hang with "trace.py --max-events #"
parents 6044643c a7554fc4
...@@ -20,8 +20,10 @@ ...@@ -20,8 +20,10 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <syscall.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <linux/perf_event.h> #include <linux/perf_event.h>
...@@ -43,6 +45,7 @@ struct perf_reader { ...@@ -43,6 +45,7 @@ struct perf_reader {
size_t buf_size; size_t buf_size;
void *base; void *base;
int rb_use_state; int rb_use_state;
pid_t rb_read_tid;
int page_size; int page_size;
int page_cnt; int page_cnt;
int fd; int fd;
...@@ -70,7 +73,12 @@ struct perf_reader * perf_reader_new(perf_reader_cb cb, ...@@ -70,7 +73,12 @@ struct perf_reader * perf_reader_new(perf_reader_cb cb,
void perf_reader_free(void *ptr) { void perf_reader_free(void *ptr) {
if (ptr) { if (ptr) {
struct perf_reader *reader = ptr; struct perf_reader *reader = ptr;
while (!__sync_bool_compare_and_swap(&reader->rb_use_state, RB_NOT_USED, RB_USED_IN_MUNMAP)); pid_t tid = syscall(__NR_gettid);
while (!__sync_bool_compare_and_swap(&reader->rb_use_state, RB_NOT_USED, RB_USED_IN_MUNMAP)) {
// If the same thread, it is called from call back handler, no locking needed
if (tid == reader->rb_read_tid)
break;
}
munmap(reader->base, reader->page_size * (reader->page_cnt + 1)); munmap(reader->base, reader->page_size * (reader->page_cnt + 1));
if (reader->fd >= 0) { if (reader->fd >= 0) {
ioctl(reader->fd, PERF_EVENT_IOC_DISABLE, 0); ioctl(reader->fd, PERF_EVENT_IOC_DISABLE, 0);
...@@ -228,6 +236,7 @@ void perf_reader_event_read(struct perf_reader *reader) { ...@@ -228,6 +236,7 @@ void perf_reader_event_read(struct perf_reader *reader) {
uint8_t *sentinel = (uint8_t *)reader->base + buffer_size + reader->page_size; uint8_t *sentinel = (uint8_t *)reader->base + buffer_size + reader->page_size;
uint8_t *begin, *end; uint8_t *begin, *end;
reader->rb_read_tid = syscall(__NR_gettid);
if (!__sync_bool_compare_and_swap(&reader->rb_use_state, RB_NOT_USED, RB_USED_IN_READ)) if (!__sync_bool_compare_and_swap(&reader->rb_use_state, RB_NOT_USED, RB_USED_IN_READ))
return; return;
...@@ -280,6 +289,8 @@ void perf_reader_event_read(struct perf_reader *reader) { ...@@ -280,6 +289,8 @@ void perf_reader_event_read(struct perf_reader *reader) {
write_data_tail(perf_header, perf_header->data_tail + e->size); write_data_tail(perf_header, perf_header->data_tail + e->size);
} }
reader->rb_use_state = RB_NOT_USED; reader->rb_use_state = RB_NOT_USED;
__sync_synchronize();
reader->rb_read_tid = 0;
} }
int perf_reader_poll(int num_readers, struct perf_reader **readers, int timeout) { int perf_reader_poll(int num_readers, struct perf_reader **readers, int timeout) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment