From 1f18504e513d549093432516f983105ad5142808 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Mon, 5 Dec 2011 18:45:04 -0500 Subject: [PATCH] Add loglevel enable/disable support Signed-off-by: Mathieu Desnoyers --- include/lttng/ust-events.h | 33 +++++++ liblttng-ust/ltt-events.c | 37 ++++++-- liblttng-ust/ltt-probes.c | 165 +++++++++++++++++++++++++++++++++++ liblttng-ust/lttng-ust-abi.c | 103 +++++++++++++++++++++- 4 files changed, 332 insertions(+), 6 deletions(-) diff --git a/include/lttng/ust-events.h b/include/lttng/ust-events.h index 0a031939..5867b124 100644 --- a/include/lttng/ust-events.h +++ b/include/lttng/ust-events.h @@ -19,6 +19,7 @@ */ #include +#include #include #include #include @@ -188,6 +189,22 @@ struct tracepoint_loglevel_entry { long value; }; +/* + * Entry describing an active loglevel, along with the event attribute + * and channel information configuring the events that need to be + * enabled. + */ +struct loglevel_entry { + struct cds_hlist_node hlist; + struct ltt_channel *chan; + struct lttng_ctx *ctx; /* TODO */ + struct lttng_ust_event event_param; + struct cds_list_head events; /* list of events enabled */ + struct cds_list_head list; /* per-session list of loglevels */ + unsigned int enabled:1; + char name[0]; +}; + struct lttng_event_desc { const char *name; void *probe_callback; @@ -221,6 +238,9 @@ struct ust_pending_probe; /* * ltt_event structure is referred to by the tracing fast path. It must be * kept small. + * Note about loglevel_list: this list is only used to enable/disable + * events on a per-loglevel basis. The events created internally by the + * loglevel are only freed when the session is destroyed. */ struct ltt_event { unsigned int id; @@ -233,6 +253,7 @@ struct ltt_event { union { } u; struct cds_list_head list; /* Event list */ + struct cds_list_head loglevel_list; /* Event list for loglevel */ struct ust_pending_probe *pending_probe; unsigned int metadata_dumped:1; }; @@ -309,6 +330,7 @@ struct ltt_session { struct ltt_channel *metadata; /* Metadata channel */ struct cds_list_head chan; /* Channel list head */ struct cds_list_head events; /* Event list head */ + struct cds_list_head loglevels; /* Loglevel list head */ struct cds_list_head list; /* Session list */ unsigned int free_chan_id; /* Next chan ID to allocate */ uuid_t uuid; /* Trace session unique ID */ @@ -388,4 +410,15 @@ void ltt_probes_prune_event_list(struct lttng_ust_tracepoint_list *list); struct lttng_ust_tracepoint_iter * lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list); +struct loglevel_entry *get_loglevel(const char *name); +struct loglevel_entry *add_loglevel(const char *name, + struct ltt_channel *chan, + struct lttng_ust_event *event_param); +void _remove_loglevel(struct loglevel_entry *e); +int ltt_loglevel_enable(struct loglevel_entry *loglevel); +int ltt_loglevel_disable(struct loglevel_entry *loglevel); +int ltt_loglevel_create(struct ltt_channel *chan, + struct lttng_ust_event *event_param, + struct loglevel_entry **_entry); + #endif /* _LTTNG_UST_EVENTS_H */ diff --git a/liblttng-ust/ltt-events.c b/liblttng-ust/ltt-events.c index 06d2f897..c754d6ff 100644 --- a/liblttng-ust/ltt-events.c +++ b/liblttng-ust/ltt-events.c @@ -31,6 +31,7 @@ #include #include +#include "error.h" #include "ltt-tracer.h" #include "ltt-tracer-core.h" @@ -73,6 +74,7 @@ struct ust_pending_probe { }; static void _ltt_event_destroy(struct ltt_event *event); +static void _ltt_loglevel_destroy(struct loglevel_entry *entry); static void _ltt_channel_destroy(struct ltt_channel *chan); static int _ltt_event_unregister(struct ltt_event *event); static @@ -134,6 +136,11 @@ int pending_probe_fix_events(const struct lttng_event_desc *desc) uint32_t hash = jhash(name, name_len - 1, 0); int ret = 0; + /* TODO: + * For this event, we need to lookup the loglevel. If active (in + * the active loglevels hash table), we must create the event. + */ + head = &pending_probe_table[hash & (PENDING_PROBE_HASH_SIZE - 1)]; cds_hlist_for_each_entry_safe(e, node, p, head, node) { struct ltt_event *event; @@ -173,6 +180,7 @@ struct ltt_session *ltt_session_create(void) return NULL; CDS_INIT_LIST_HEAD(&session->chan); CDS_INIT_LIST_HEAD(&session->events); + CDS_INIT_LIST_HEAD(&session->loglevels); uuid_generate(session->uuid); cds_list_add(&session->list, &sessions); return session; @@ -182,6 +190,7 @@ void ltt_session_destroy(struct ltt_session *session) { struct ltt_channel *chan, *tmpchan; struct ltt_event *event, *tmpevent; + struct loglevel_entry *loglevel, *tmploglevel; int ret; CMM_ACCESS_ONCE(session->active) = 0; @@ -190,6 +199,8 @@ void ltt_session_destroy(struct ltt_session *session) WARN_ON(ret); } synchronize_trace(); /* Wait for in-flight events to complete */ + cds_list_for_each_entry_safe(loglevel, tmploglevel, &session->loglevels, list) + _ltt_loglevel_destroy(loglevel); cds_list_for_each_entry_safe(event, tmpevent, &session->events, list) _ltt_event_destroy(event); cds_list_for_each_entry_safe(chan, tmpchan, &session->chan, list) @@ -347,6 +358,26 @@ void _ltt_channel_destroy(struct ltt_channel *chan) chan->ops->channel_destroy(chan); } +int ltt_loglevel_create(struct ltt_channel *chan, + struct lttng_ust_event *event_param, + struct loglevel_entry **_entry) +{ + struct loglevel_entry *entry; + + entry = add_loglevel(event_param->name, chan, event_param); + if (!entry || IS_ERR(entry)) { + return PTR_ERR(entry); + } + *_entry = entry; + return 0; +} + +static +void _ltt_loglevel_destroy(struct loglevel_entry *entry) +{ + _remove_loglevel(entry); +} + /* * Supports event creation while tracing session is active. */ @@ -410,11 +441,7 @@ int ltt_event_create(struct ltt_channel *chan, } break; case LTTNG_UST_TRACEPOINT_LOGLEVEL: - /* - * TODO: add tracepoint loglevel to hash table, with - * event info. Enable all events corresponding to - * loglevel. - */ + assert(0); break; default: WARN_ON_ONCE(1); diff --git a/liblttng-ust/ltt-probes.c b/liblttng-ust/ltt-probes.c index 957a9b59..d2623947 100644 --- a/liblttng-ust/ltt-probes.c +++ b/liblttng-ust/ltt-probes.c @@ -11,17 +11,28 @@ #include #include #include +#include #include #include #include #include "ltt-tracer-core.h" +#include "jhash.h" +#include "error.h" /* * probe list is protected by ust_lock()/ust_unlock(). */ static CDS_LIST_HEAD(probe_list); +/* + * Loglevel hash table, containing the active loglevels. + * Protected by ust lock. + */ +#define LOGLEVEL_HASH_BITS 6 +#define LOGLEVEL_TABLE_SIZE (1 << LOGLEVEL_HASH_BITS) +static struct cds_hlist_head loglevel_table[LOGLEVEL_TABLE_SIZE]; + static const struct lttng_probe_desc *find_provider(const char *provider) { @@ -198,3 +209,157 @@ struct lttng_ust_tracepoint_iter * struct tp_list_entry, head); return &entry->tp; } + +/* + * Get loglevel if the loglevel is present in the loglevel hash table. + * Must be called with ust lock held. + * Returns NULL if not present. + */ +struct loglevel_entry *get_loglevel(const char *name) +{ + struct cds_hlist_head *head; + struct cds_hlist_node *node; + struct loglevel_entry *e; + uint32_t hash = jhash(name, strlen(name), 0); + + head = &loglevel_table[hash & (LOGLEVEL_TABLE_SIZE - 1)]; + cds_hlist_for_each_entry(e, node, head, hlist) { + if (!strcmp(name, e->name)) + return e; + } + return NULL; +} + +/* + * marshall all probes/all events and create those that fit the + * loglevel. Add them to the events list as created. + */ +static +void _probes_create_loglevel_events(struct loglevel_entry *loglevel) +{ + struct lttng_probe_desc *probe_desc; + int i; + + cds_list_for_each_entry(probe_desc, &probe_list, head) { + for (i = 0; i < probe_desc->nr_events; i++) { + const struct tracepoint_loglevel_entry *ev_ll; + + if (!(probe_desc->event_desc[i]->loglevel)) + continue; + ev_ll = *probe_desc->event_desc[i]->loglevel; + if (!strcmp(ev_ll->identifier, loglevel->name)) { + struct ltt_event *ev; + int ret; + + /* create event */ + ret = ltt_event_create(loglevel->chan, + &loglevel->event_param, NULL, + &ev); + /* + * TODO: report error. + */ + if (ret) + continue; + cds_list_add(&ev->loglevel_list, + &loglevel->events); + } + + } + } +} + +/* + * Add the loglevel to the loglevel hash table. Must be called with + * ust lock held. + */ +struct loglevel_entry *add_loglevel(const char *name, + struct ltt_channel *chan, + struct lttng_ust_event *event_param) +{ + struct cds_hlist_head *head; + struct cds_hlist_node *node; + struct loglevel_entry *e; + size_t name_len = strlen(name) + 1; + uint32_t hash = jhash(name, name_len-1, 0); + + head = &loglevel_table[hash & (LOGLEVEL_TABLE_SIZE - 1)]; + cds_hlist_for_each_entry(e, node, head, hlist) { + if (!strcmp(name, e->name)) { + DBG("loglevel %s busy", name); + return ERR_PTR(-EEXIST); /* Already there */ + } + } + /* + * Using zmalloc here to allocate a variable length element. Could + * cause some memory fragmentation if overused. + */ + e = zmalloc(sizeof(struct loglevel_entry) + name_len); + if (!e) + return ERR_PTR(-ENOMEM); + memcpy(&e->name[0], name, name_len); + e->chan = chan; + e->enabled = 1; + memcpy(&e->event_param, event_param, sizeof(e->event_param)); + cds_hlist_add_head(&e->hlist, head); + CDS_INIT_LIST_HEAD(&e->events); + _probes_create_loglevel_events(e); + cds_list_add(&e->list, &chan->session->loglevels); + return e; +} + +/* + * Remove the loglevel from the loglevel hash table. Must be called with + * ust_lock held. Only called at session teardown. + */ +void _remove_loglevel(struct loglevel_entry *loglevel) +{ + struct ltt_event *ev, *tmp; + + /* + * Just remove the events owned (for enable/disable) by this + * loglevel from the list. The session teardown will take care + * of freeing the event memory. + */ + cds_list_for_each_entry_safe(ev, tmp, &loglevel->events, list) { + cds_list_del(&ev->list); + } + cds_hlist_del(&loglevel->hlist); + cds_list_del(&loglevel->list); + free(loglevel); +} + +int ltt_loglevel_enable(struct loglevel_entry *loglevel) +{ + struct ltt_event *ev; + int ret; + + if (loglevel->enabled) + return -EEXIST; + cds_list_for_each_entry(ev, &loglevel->events, list) { + ret = ltt_event_enable(ev); + if (ret) { + DBG("Error: enable error.\n"); + return ret; + } + } + loglevel->enabled = 1; + return 0; +} + +int ltt_loglevel_disable(struct loglevel_entry *loglevel) +{ + struct ltt_event *ev; + int ret; + + if (!loglevel->enabled) + return -EEXIST; + cds_list_for_each_entry(ev, &loglevel->events, list) { + ret = ltt_event_disable(ev); + if (ret) { + DBG("Error: disable error.\n"); + return ret; + } + } + loglevel->enabled = 0; + return 0; +} diff --git a/liblttng-ust/lttng-ust-abi.c b/liblttng-ust/lttng-ust-abi.c index 900f7834..548b3a52 100644 --- a/liblttng-ust/lttng-ust-abi.c +++ b/liblttng-ust/lttng-ust-abi.c @@ -203,6 +203,7 @@ static const struct lttng_ust_objd_ops lttng_session_ops; static const struct lttng_ust_objd_ops lttng_channel_ops; static const struct lttng_ust_objd_ops lttng_metadata_ops; static const struct lttng_ust_objd_ops lttng_event_ops; +static const struct lttng_ust_objd_ops lttng_loglevel_ops; static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops; static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops; @@ -658,6 +659,44 @@ objd_error: return ret; } +static +int lttng_abi_create_loglevel(int channel_objd, + struct lttng_ust_event *event_param) +{ + struct ltt_channel *channel = objd_private(channel_objd); + struct loglevel_entry *loglevel; + int loglevel_objd, ret; + + event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; + loglevel_objd = objd_alloc(NULL, <tng_loglevel_ops); + if (loglevel_objd < 0) { + ret = loglevel_objd; + goto objd_error; + } + /* + * We tolerate no failure path after loglevel creation. It will + * stay invariant for the rest of the session. + */ + ret = ltt_loglevel_create(channel, event_param, &loglevel); + if (ret < 0) { + goto loglevel_error; + } + objd_set_private(loglevel_objd, loglevel); + /* The loglevel holds a reference on the channel */ + objd_ref(channel_objd); + return loglevel_objd; + +loglevel_error: + { + int err; + + err = lttng_ust_objd_unref(loglevel_objd); + assert(!err); + } +objd_error: + return ret; +} + /** * lttng_channel_cmd - lttng control through object descriptors * @@ -695,7 +734,15 @@ long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg) return lttng_abi_open_stream(objd, stream); } case LTTNG_UST_EVENT: - return lttng_abi_create_event(objd, (struct lttng_ust_event *) arg); + { + struct lttng_ust_event *event_param = + (struct lttng_ust_event *) arg; + if (event_param->instrumentation == LTTNG_UST_TRACEPOINT_LOGLEVEL) { + return lttng_abi_create_loglevel(objd, event_param); + } else { + return lttng_abi_create_event(objd, event_param); + } + } case LTTNG_UST_CONTEXT: return lttng_abi_add_context(objd, (struct lttng_ust_context *) arg, @@ -902,6 +949,60 @@ static const struct lttng_ust_objd_ops lttng_event_ops = { .cmd = lttng_event_cmd, }; +/** + * lttng_loglevel_cmd - lttng control through object descriptors + * + * @objd: the object descriptor + * @cmd: the command + * @arg: command arg + * + * This object descriptor implements lttng commands: + * LTTNG_UST_CONTEXT + * Prepend a context field to each record of events of this + * loglevel. + * LTTNG_UST_ENABLE + * Enable recording for these loglevel events (weak enable) + * LTTNG_UST_DISABLE + * Disable recording for these loglevel events (strong disable) + */ +static +long lttng_loglevel_cmd(int objd, unsigned int cmd, unsigned long arg) +{ + struct loglevel_entry *loglevel = objd_private(objd); + + switch (cmd) { + case LTTNG_UST_CONTEXT: + return -ENOSYS; /* not implemented yet */ +#if 0 + return lttng_abi_add_context(objd, + (struct lttng_ust_context *) arg, + &loglevel->ctx, loglevel->chan->session); +#endif + case LTTNG_UST_ENABLE: + return ltt_loglevel_enable(loglevel); + case LTTNG_UST_DISABLE: + return ltt_loglevel_disable(loglevel); + default: + return -EINVAL; + } +} + +static +int lttng_loglevel_release(int objd) +{ + struct loglevel_entry *loglevel = objd_private(objd); + + if (loglevel) + return lttng_ust_objd_unref(loglevel->chan->objd); + return 0; +} + +/* TODO: filter control ioctl */ +static const struct lttng_ust_objd_ops lttng_loglevel_ops = { + .release = lttng_loglevel_release, + .cmd = lttng_loglevel_cmd, +}; + void lttng_ust_abi_exit(void) { lttng_ust_abi_close_in_progress = 1; -- 2.34.1