SoW-2020-0002: Trace Hit Counters
[lttng-tools.git] / src / bin / lttng-sessiond / buffer-registry.c
index 32fcbfb5f9ad55276444057e1aa389bd277f8356..34574e391bc6848f940504355f81868461d7ccd2 100644 (file)
@@ -136,6 +136,14 @@ int buffer_reg_uid_create(uint64_t session_id, uint32_t bits_per_long, uid_t uid
                goto error_session;
        }
 
+       reg->registry->maps = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
+       if (!reg->registry->maps) {
+               lttng_ht_destroy(reg->registry->channels);
+               ret = -ENOMEM;
+               goto error_session;
+       }
+
+
        cds_lfht_node_init(&reg->node.node);
        *regp = reg;
 
@@ -262,6 +270,13 @@ int buffer_reg_pid_create(uint64_t session_id, struct buffer_reg_pid **regp,
                goto error_session;
        }
 
+       reg->registry->maps = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
+       if (!reg->registry->maps) {
+               lttng_ht_destroy(reg->registry->channels);
+               ret = -ENOMEM;
+               goto error_session;
+       }
+
        lttng_ht_node_init_u64(&reg->node, reg->session_id);
        *regp = reg;
 
@@ -385,6 +400,36 @@ int buffer_reg_channel_create(uint64_t key, struct buffer_reg_channel **regp)
        return 0;
 }
 
+/*
+ * Allocate and initialize a buffer registry map with the given key. Set
+ * regp with the object pointer.
+ *
+ * Return 0 on success or else a negative value keeping regp untouched.
+ */
+int buffer_reg_map_create(uint64_t key, struct buffer_reg_map **regp)
+{
+       struct buffer_reg_map *reg;
+
+       assert(regp);
+
+       DBG3("Buffer registry map create with key: %" PRIu64, key);
+
+       reg = zmalloc(sizeof(*reg));
+       if (!reg) {
+               PERROR("zmalloc buffer registry map");
+               return -ENOMEM;
+       }
+
+       reg->key = key;
+       CDS_INIT_LIST_HEAD(&reg->counters);
+       pthread_mutex_init(&reg->counter_list_lock, NULL);
+
+       lttng_ht_node_init_u64(&reg->node, key);
+       *regp = reg;
+
+       return 0;
+}
+
 /*
  * Allocate and initialize a buffer registry stream. Set regp with the object
  * pointer.
@@ -410,6 +455,31 @@ int buffer_reg_stream_create(struct buffer_reg_stream **regp)
        return 0;
 }
 
+/*
+ * Allocate and initialize a buffer registry map_counter. Set regp with the object
+ * pointer.
+ *
+ * Return 0 on success or else a negative value keeping regp untouched.
+ */
+int buffer_reg_map_counter_create(struct buffer_reg_map_counter **regp)
+{
+       struct buffer_reg_map_counter *reg;
+
+       assert(regp);
+
+       DBG3("Buffer registry creating map_counter");
+
+       reg = zmalloc(sizeof(*reg));
+       if (!reg) {
+               PERROR("zmalloc buffer registry map_counter");
+               return -ENOMEM;
+       }
+
+       *regp = reg;
+
+       return 0;
+}
+
 /*
  * Add stream to the list in the channel.
  */
@@ -425,6 +495,21 @@ void buffer_reg_stream_add(struct buffer_reg_stream *stream,
        pthread_mutex_unlock(&channel->stream_list_lock);
 }
 
+/*
+ * Add map_counter to the list in the map.
+ */
+void buffer_reg_map_counter_add(struct buffer_reg_map_counter *map_counter,
+               struct buffer_reg_map *map)
+{
+       assert(map_counter);
+       assert(map);
+
+       pthread_mutex_lock(&map->counter_list_lock);
+       cds_list_add_tail(&map_counter->lnode, &map->counters);
+       map->counter_count++;
+       pthread_mutex_unlock(&map->counter_list_lock);
+}
+
 /*
  * Add a buffer registry channel object to the given session.
  */
@@ -439,6 +524,20 @@ void buffer_reg_channel_add(struct buffer_reg_session *session,
        rcu_read_unlock();
 }
 
+/*
+ * Add a buffer registry map object to the given session.
+ */
+void buffer_reg_map_add(struct buffer_reg_session *session,
+               struct buffer_reg_map *map)
+{
+       assert(session);
+       assert(map);
+
+       rcu_read_lock();
+       lttng_ht_add_unique_u64(session->maps, &map->node);
+       rcu_read_unlock();
+}
+
 /*
  * Find a buffer registry channel object with the given key. RCU read side lock
  * MUST be acquired and hold on until the object reference is not needed
@@ -476,6 +575,43 @@ end:
        return chan;
 }
 
+/*
+ * Find a buffer registry map object with the given key. RCU read side lock
+ * MUST be acquired and hold on until the object reference is not needed
+ * anymore.
+ *
+ * Return the object pointer or NULL on error.
+ */
+struct buffer_reg_map *buffer_reg_map_find(uint64_t key,
+               struct buffer_reg_uid *reg)
+{
+       struct lttng_ht_node_u64 *node;
+       struct lttng_ht_iter iter;
+       struct buffer_reg_map *map = NULL;
+       struct lttng_ht *ht;
+
+       assert(reg);
+
+       switch (reg->domain) {
+       case LTTNG_DOMAIN_UST:
+               ht = reg->registry->maps;
+               break;
+       default:
+               assert(0);
+               goto end;
+       }
+
+       lttng_ht_lookup(ht, &key, &iter);
+       node = lttng_ht_iter_get_node_u64(&iter);
+       if (!node) {
+               goto end;
+       }
+       map = caa_container_of(node, struct buffer_reg_map, node);
+
+end:
+       return map;
+}
+
 /*
  * Destroy a buffer registry stream with the given domain.
  */
@@ -511,6 +647,41 @@ void buffer_reg_stream_destroy(struct buffer_reg_stream *regp,
        return;
 }
 
+/*
+ * Destroy a buffer registry map_counter with the given domain.
+ */
+void buffer_reg_map_counter_destroy(struct buffer_reg_map_counter *regp,
+               enum lttng_domain_type domain)
+{
+       if (!regp) {
+               return;
+       }
+
+       DBG3("Buffer registry map counter destroy with handle %d",
+                       regp->obj.ust->handle);
+
+       switch (domain) {
+       case LTTNG_DOMAIN_UST:
+       {
+               int ret;
+
+               ret = ust_app_release_object(NULL, regp->obj.ust);
+               if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
+                       ERR("Buffer reg map counter release obj handle %d failed with ret %d",
+                                       regp->obj.ust->handle, ret);
+               }
+               free(regp->obj.ust);
+               lttng_fd_put(LTTNG_FD_APPS, 2);
+               break;
+       }
+       default:
+               assert(0);
+       }
+
+       free(regp);
+       return;
+}
+
 /*
  * Remove buffer registry channel object from the session hash table. RCU read
  * side lock MUST be acquired before calling this.
@@ -529,6 +700,24 @@ void buffer_reg_channel_remove(struct buffer_reg_session *session,
        assert(!ret);
 }
 
+/*
+ * Remove buffer registry map object from the session hash table. RCU read
+ * side lock MUST be acquired before calling this.
+ */
+void buffer_reg_map_remove(struct buffer_reg_session *session,
+               struct buffer_reg_map *regp)
+{
+       int ret;
+       struct lttng_ht_iter iter;
+
+       assert(session);
+       assert(regp);
+
+       iter.iter.node = &regp->node.node;
+       ret = lttng_ht_del(session->maps, &iter);
+       assert(!ret);
+}
+
 /*
  * Destroy a buffer registry channel with the given domain.
  */
@@ -572,6 +761,49 @@ void buffer_reg_channel_destroy(struct buffer_reg_channel *regp,
        return;
 }
 
+/*
+ * Destroy a buffer registry map with the given domain.
+ */
+void buffer_reg_map_destroy(struct buffer_reg_map *regp,
+               enum lttng_domain_type domain)
+{
+       if (!regp) {
+               return;
+       }
+
+       DBG3("Buffer registry map destroy with key %" PRIu32, regp->key);
+
+       switch (domain) {
+       case LTTNG_DOMAIN_UST:
+       {
+               int ret;
+               struct buffer_reg_map_counter *map_counter_reg, *tmp;
+               /* Wipe counter */
+               cds_list_for_each_entry_safe(map_counter_reg, tmp, &regp->counters, lnode) {
+                       cds_list_del(&map_counter_reg->lnode);
+                       regp->counter_count--;
+                       buffer_reg_map_counter_destroy(map_counter_reg, domain);
+               }
+
+               if (regp->obj.ust) {
+                       ret = ust_app_release_object(NULL, regp->obj.ust);
+                       if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
+                               ERR("Buffer reg map release obj handle %d failed with ret %d",
+                                               regp->obj.ust->handle, ret);
+                       }
+                       free(regp->obj.ust);
+               }
+               lttng_fd_put(LTTNG_FD_APPS, 1);
+               break;
+       }
+       default:
+               assert(0);
+       }
+
+       free(regp);
+       return;
+}
+
 /*
  * Destroy a buffer registry session with the given domain.
  *
@@ -583,6 +815,7 @@ static void buffer_reg_session_destroy(struct buffer_reg_session *regp,
        int ret;
        struct lttng_ht_iter iter;
        struct buffer_reg_channel *reg_chan;
+       struct buffer_reg_map *reg_map;
 
        DBG3("Buffer registry session destroy");
 
@@ -594,9 +827,16 @@ static void buffer_reg_session_destroy(struct buffer_reg_session *regp,
                assert(!ret);
                buffer_reg_channel_destroy(reg_chan, domain);
        }
+       cds_lfht_for_each_entry(regp->maps->ht, &iter.iter, reg_map,
+                       node.node) {
+               ret = lttng_ht_del(regp->maps, &iter);
+               assert(!ret);
+               buffer_reg_map_destroy(reg_map, domain);
+       }
        rcu_read_unlock();
 
        ht_cleanup_push(regp->channels);
+       ht_cleanup_push(regp->maps);
 
        switch (domain) {
        case LTTNG_DOMAIN_UST:
This page took 0.026982 seconds and 5 git commands to generate.