[RFC PATCH 1/2] tracecmd library: Use libtraceevent record refcounting

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Moving this into libtraceevent has the advantage that a python plugin
loader can be implemented that does not need access to trace-cmd API.
---
 .../include/private/trace-cmd-private.h       |  2 +-
 lib/trace-cmd/trace-input.c                   | 94 +++++++++----------
 2 files changed, 48 insertions(+), 48 deletions(-)

diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h
index d73a5191..b08ebcee 100644
--- a/lib/trace-cmd/include/private/trace-cmd-private.h
+++ b/lib/trace-cmd/include/private/trace-cmd-private.h
@@ -235,7 +235,7 @@ tracecmd_peek_data_ref(struct tracecmd_input *handle, int cpu)
 {
 	struct tep_record *rec = tracecmd_peek_data(handle, cpu);
 	if (rec)
-		rec->ref_count++;
+		tep_record_ref(rec);
 	return rec;
 }
 
diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c
index cdaa17bd..157e2d97 100644
--- a/lib/trace-cmd/trace-input.c
+++ b/lib/trace-cmd/trace-input.c
@@ -35,6 +35,16 @@
 /* for debugging read instead of mmap */
 static int force_read = 0;
 
+#define REC_PRIV(rec) ((struct record_priv*) (rec)->priv)
+struct record_priv {
+	struct page		*page;
+	int			locked;
+#if DEBUG_RECORD
+	struct tep_record	*prev;
+	struct tep_record	*next;
+#endif
+};
+
 struct page_map {
 	struct list_head	list;
 	off64_t			offset;
@@ -261,19 +271,23 @@ void *tracecmd_get_private(struct tracecmd_input *handle)
 #if DEBUG_RECORD
 static void remove_record(struct page *page, struct tep_record *record)
 {
-	if (record->prev)
-		record->prev->next = record->next;
+	struct record_priv *priv = REC_PRIV(record);
+
+	if (priv->prev)
+		REC_PRIV(priv->prev)->next = priv->next;
 	else
-		page->records = record->next;
-	if (record->next)
-		record->next->prev = record->prev;
+		page->records = priv->next;
+	if (priv->next)
+		REC_PRIV(priv->next)->prev = priv->prev;
 }
 static void add_record(struct page *page, struct tep_record *record)
 {
+	struct record_priv *priv = REC_PRIV(record);
+
 	if (page->records)
-		page->records->prev = record;
-	record->next = page->records;
-	record->prev = NULL;
+		REC_PRIV(page->records)->prev = record;
+	priv->next = page->records;
+	priv->prev = NULL;
 	page->records = record;
 }
 static const char *show_records(struct page **pages, int nr_pages)
@@ -290,7 +304,7 @@ static const char *show_records(struct page **pages, int nr_pages)
 		page = pages[i];
 		if (!page)
 			continue;
-		for (record = page->records; record; record = record->next) {
+		for (record = page->records; record; record = REC_PRIV(record)->next) {
 			int n;
 			n = snprintf(buf+len, BUFSIZ - len, " 0x%lx", record->alloc_addr);
 			len += n;
@@ -1601,13 +1615,20 @@ static void free_page(struct tracecmd_input *handle, int cpu)
 
 static void __free_record(struct tep_record *record)
 {
-	if (record->priv) {
-		struct page *page = record->priv;
+	struct record_priv *priv = record->priv;
+
+	if (priv->locked) {
+		tracecmd_critical("freeing record when it is locked!");
+		return;
+	}
+
+	record->data = NULL;
+
+	if (priv->page) {
+		struct page *page = priv->page;
 		remove_record(page, record);
 		__free_page(page->handle, page);
 	}
-
-	free(record);
 }
 
 void tracecmd_free_record(struct tep_record *record)
@@ -1615,29 +1636,12 @@ void tracecmd_free_record(struct tep_record *record)
 	if (!record)
 		return;
 
-	if (!record->ref_count) {
-		tracecmd_critical("record ref count is zero!");
-		return;
-	}
-
-	record->ref_count--;
-
-	if (record->ref_count)
-		return;
-
-	if (record->locked) {
-		tracecmd_critical("freeing record when it is locked!");
-		return;
-	}
-
-	record->data = NULL;
-
-	__free_record(record);
+	tep_record_unref(record);
 }
 
 void tracecmd_record_ref(struct tep_record *record)
 {
-	record->ref_count++;
+	tep_record_ref(record);
 #if DEBUG_RECORD
 	/* Update locating of last reference */
 	record->alloc_addr = (unsigned long)__builtin_return_address(0);
@@ -1657,7 +1661,7 @@ static void free_next(struct tracecmd_input *handle, int cpu)
 
 	handle->cpu_data[cpu].next = NULL;
 
-	record->locked = 0;
+	REC_PRIV(record)->locked = 0;
 	tracecmd_free_record(record);
 }
 
@@ -2353,12 +2357,10 @@ tracecmd_translate_data(struct tracecmd_input *handle,
 	if (size < 8)
 		return NULL;
 
-	record = malloc(sizeof(*record));
+	record = tep_record_alloc(sizeof(struct record_priv), __free_record);
 	if (!record)
 		return NULL;
-	memset(record, 0, sizeof(*record));
 
-	record->ref_count = 1;
 	if (tep_is_local_bigendian(pevent) == tep_is_file_bigendian(pevent))
 		swap = 0;
 	record->data = kbuffer_translate_data(swap, ptr, &length);
@@ -2437,10 +2439,9 @@ read_again:
 
 	index = kbuffer_curr_offset(kbuf);
 
-	record = malloc(sizeof(*record));
+	record = tep_record_alloc(sizeof(struct record_priv), __free_record);
 	if (!record)
 		return NULL;
-	memset(record, 0, sizeof(*record));
 
 	record->ts = handle->cpu_data[cpu].timestamp;
 	record->size = kbuffer_event_size(kbuf);
@@ -2448,13 +2449,12 @@ read_again:
 	record->data = data;
 	record->offset = handle->cpu_data[cpu].offset + index;
 	record->missed_events = kbuffer_missed_events(kbuf);
-	record->ref_count = 1;
-	record->locked = 1;
+	REC_PRIV(record)->locked = 1;
 
 	handle->cpu_data[cpu].next = record;
 
 	record->record_size = kbuffer_curr_size(kbuf);
-	record->priv = page;
+	REC_PRIV(record)->page = page;
 	add_record(page, record);
 	page->ref_count++;
 
@@ -2484,7 +2484,7 @@ tracecmd_read_data(struct tracecmd_input *handle, int cpu)
 	record = tracecmd_peek_data(handle, cpu);
 	handle->cpu_data[cpu].next = NULL;
 	if (record) {
-		record->locked = 0;
+		REC_PRIV(record)->locked = 0;
 #if DEBUG_RECORD
 		record->alloc_addr = (unsigned long)__builtin_return_address(0);
 #endif
@@ -5619,7 +5619,7 @@ __hidden int tracecmd_copy_trace_data(struct tracecmd_input *in_handle,
 int tracecmd_record_at_buffer_start(struct tracecmd_input *handle,
 				    struct tep_record *record)
 {
-	struct page *page = record->priv;
+	struct page *page = REC_PRIV(record)->page;
 	struct kbuffer *kbuf = handle->cpu_data[record->cpu].kbuf;
 	int offset;
 
@@ -5633,7 +5633,7 @@ int tracecmd_record_at_buffer_start(struct tracecmd_input *handle,
 unsigned long long tracecmd_page_ts(struct tracecmd_input *handle,
 				    struct tep_record *record)
 {
-	struct page *page = record->priv;
+	struct page *page = REC_PRIV(record)->page;
 	struct kbuffer *kbuf = handle->cpu_data[record->cpu].kbuf;
 
 	if (!page || !kbuf)
@@ -5646,7 +5646,7 @@ unsigned int tracecmd_record_ts_delta(struct tracecmd_input *handle,
 				      struct tep_record *record)
 {
 	struct kbuffer *kbuf = handle->cpu_data[record->cpu].kbuf;
-	struct page *page = record->priv;
+	struct page *page = REC_PRIV(record)->page;
 	int offset;
 
 	if (!page || !kbuf)
@@ -5666,7 +5666,7 @@ struct kbuffer *tracecmd_record_kbuf(struct tracecmd_input *handle,
 void *tracecmd_record_page(struct tracecmd_input *handle,
 			   struct tep_record *record)
 {
-	struct page *page = record->priv;
+	struct page *page = REC_PRIV(record)->page;
 
 	return page ? page->map : NULL;
 }
@@ -5674,7 +5674,7 @@ void *tracecmd_record_page(struct tracecmd_input *handle,
 void *tracecmd_record_offset(struct tracecmd_input *handle,
 			     struct tep_record *record)
 {
-	struct page *page = record->priv;
+	struct page *page = REC_PRIV(record)->page;
 	int offset;
 
 	if (!page)
-- 
2.37.3




[Index of Archives]     [Linux USB Development]     [Linux USB Development]     [Linux Audio Users]     [Yosemite Hiking]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux