Add loglevel enable/disable support
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 5 Dec 2011 23:45:04 +0000 (18:45 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 5 Dec 2011 23:45:04 +0000 (18:45 -0500)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
include/lttng/ust-events.h
liblttng-ust/ltt-events.c
liblttng-ust/ltt-probes.c
liblttng-ust/lttng-ust-abi.c

index 0a03193920229cc6406f51b0752b4744309f587c..5867b124878c6993291591094d049634c5c699f1 100644 (file)
@@ -19,6 +19,7 @@
  */
 
 #include <urcu/list.h>
+#include <urcu/hlist.h>
 #include <uuid/uuid.h>
 #include <stdint.h>
 #include <lttng/ust-abi.h>
@@ -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 */
index 06d2f89714feeeaa3771361efd15bff5a003f2ad..c754d6ff5348e3346fc2765bbaad478a6d834bfc 100644 (file)
@@ -31,6 +31,7 @@
 
 #include <usterr-signal-safe.h>
 #include <helper.h>
+#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);
index 957a9b59ba55afd64794e05294b833bad1b6f17a..d2623947aece9e34c7ebdcec89bfb0c6b68e13ab 100644 (file)
 #include <string.h>
 #include <errno.h>
 #include <urcu/list.h>
+#include <urcu/hlist.h>
 #include <lttng/ust-events.h>
 #include <assert.h>
 #include <helper.h>
 
 #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;
+}
index 900f7834e196bcefeb4a953ac39d041d1ebec895..548b3a5298ebdd8c1e86e4099abf2b39c5c07191 100644 (file)
@@ -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, &lttng_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;
This page took 0.031764 seconds and 4 git commands to generate.