From: Benjamin Berg <benjamin@sipsolutions.net>
To: linux-trace-devel@vger.kernel.org
Cc: johannes@sipsolutions.net, Benjamin Berg <benjamin@sipsolutions.net>
Subject: [RFC PATCH 1/2] tracecmd library: Use libtraceevent record refcounting
Date: Wed, 12 Oct 2022 12:13:06 +0200 [thread overview]
Message-ID: <20221012101307.784747-1-benjamin@sipsolutions.net> (raw)
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
next reply other threads:[~2022-10-12 10:15 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-12 10:13 Benjamin Berg [this message]
2022-10-12 10:13 ` [RFC PATCH 2/2] trace-cmd: Add back python support without wrapping trace-cmd API Benjamin Berg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221012101307.784747-1-benjamin@sipsolutions.net \
--to=benjamin@sipsolutions.net \
--cc=johannes@sipsolutions.net \
--cc=linux-trace-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).