SoW-2020-0003: Trace Hit Counters
[lttng-tools.git] / src / bin / lttng-sessiond / buffer-registry.c
index 2df98d7360445b42ae4aba1941cd976d16e6d723..34574e391bc6848f940504355f81868461d7ccd2 100644 (file)
@@ -1,21 +1,11 @@
 /*
- * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
+ * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
  *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License, version 2 only, as
- * published by the Free Software Foundation.
+ * SPDX-License-Identifier: GPL-2.0-only
  *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 51
- * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#define _GNU_SOURCE
+#define _LGPL_SOURCE
 #include <inttypes.h>
 
 #include <common/common.h>
@@ -24,7 +14,9 @@
 #include "buffer-registry.h"
 #include "fd-limit.h"
 #include "ust-consumer.h"
-#include "ust-ctl.h"
+#include "lttng-ust-ctl.h"
+#include "lttng-ust-error.h"
+#include "utils.h"
 
 /*
  * Set in main.c during initialization process of the daemon. This contains
@@ -72,10 +64,10 @@ no_match:
  * Hash function for the per UID registry hash table. This XOR the triplet
  * together.
  */
-static unsigned long ht_hash_reg_uid(void *_key, unsigned long seed)
+static unsigned long ht_hash_reg_uid(const void *_key, unsigned long seed)
 {
        uint64_t xored_key;
-       struct buffer_reg_uid *key = _key;
+       const struct buffer_reg_uid *key = _key;
 
        assert(key);
 
@@ -103,8 +95,9 @@ void buffer_reg_init_uid_registry(void)
  *
  * Return 0 on success else a negative value and regp is untouched.
  */
-int buffer_reg_uid_create(int session_id, uint32_t bits_per_long, uid_t uid,
-               enum lttng_domain_type domain, struct buffer_reg_uid **regp)
+int buffer_reg_uid_create(uint64_t session_id, uint32_t bits_per_long, uid_t uid,
+               enum lttng_domain_type domain, struct buffer_reg_uid **regp,
+               const char *root_shm_path, const char *shm_path)
 {
        int ret = 0;
        struct buffer_reg_uid *reg = NULL;
@@ -119,7 +112,7 @@ int buffer_reg_uid_create(int session_id, uint32_t bits_per_long, uid_t uid,
        }
 
        reg->registry = zmalloc(sizeof(struct buffer_reg_session));
-       if (!reg) {
+       if (!reg->registry) {
                PERROR("zmalloc buffer registry uid session");
                ret = -ENOMEM;
                goto error;
@@ -129,17 +122,32 @@ int buffer_reg_uid_create(int session_id, uint32_t bits_per_long, uid_t uid,
        reg->bits_per_long = bits_per_long;
        reg->uid = uid;
        reg->domain = domain;
-
+       if (shm_path[0]) {
+               strncpy(reg->root_shm_path, root_shm_path, sizeof(reg->root_shm_path));
+               reg->root_shm_path[sizeof(reg->root_shm_path) - 1] = '\0';
+               strncpy(reg->shm_path, shm_path, sizeof(reg->shm_path));
+               reg->shm_path[sizeof(reg->shm_path) - 1] = '\0';
+               DBG3("shm path '%s' is assigned to uid buffer registry for session id %" PRIu64,
+                       reg->shm_path, session_id);
+       }
        reg->registry->channels = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
        if (!reg->registry->channels) {
                ret = -ENOMEM;
                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;
 
-       DBG3("Buffer registry per UID created id: %d, ABI: %u, uid: %d, domain: %d",
+       DBG3("Buffer registry per UID created id: %" PRIu64 ", ABI: %u, uid: %d, domain: %d",
                        session_id, bits_per_long, uid, domain);
 
        return 0;
@@ -161,7 +169,7 @@ void buffer_reg_uid_add(struct buffer_reg_uid *reg)
 
        assert(reg);
 
-       DBG3("Buffer registry per UID adding to global registry with id: %d",
+       DBG3("Buffer registry per UID adding to global registry with id: %" PRIu64 ,
                        reg->session_id);
 
        rcu_read_lock();
@@ -177,7 +185,7 @@ void buffer_reg_uid_add(struct buffer_reg_uid *reg)
  *
  * Return the object pointer or NULL on error.
  */
-struct buffer_reg_uid *buffer_reg_uid_find(int session_id,
+struct buffer_reg_uid *buffer_reg_uid_find(uint64_t session_id,
                uint32_t bits_per_long, uid_t uid)
 {
        struct lttng_ht_node_u64 *node;
@@ -190,7 +198,7 @@ struct buffer_reg_uid *buffer_reg_uid_find(int session_id,
        key.bits_per_long = bits_per_long;
        key.uid = uid;
 
-       DBG3("Buffer registry per UID find id: %d, ABI: %u, uid: %d",
+       DBG3("Buffer registry per UID find id: %" PRIu64 ", ABI: %u, uid: %d",
                        session_id, bits_per_long, uid);
 
        /* Custom lookup function since it's a different key. */
@@ -213,7 +221,7 @@ void buffer_reg_init_pid_registry(void)
 {
        /* Should be called once. */
        assert(!buffer_registry_pid);
-       buffer_registry_pid = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
+       buffer_registry_pid = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
        assert(buffer_registry_pid);
 
        DBG3("Global buffer per PID registry initialized");
@@ -224,7 +232,8 @@ void buffer_reg_init_pid_registry(void)
  *
  * Return 0 on success else a negative value and regp is untouched.
  */
-int buffer_reg_pid_create(int session_id, struct buffer_reg_pid **regp)
+int buffer_reg_pid_create(uint64_t session_id, struct buffer_reg_pid **regp,
+               const char *root_shm_path, const char *shm_path)
 {
        int ret = 0;
        struct buffer_reg_pid *reg = NULL;
@@ -239,7 +248,7 @@ int buffer_reg_pid_create(int session_id, struct buffer_reg_pid **regp)
        }
 
        reg->registry = zmalloc(sizeof(struct buffer_reg_session));
-       if (!reg) {
+       if (!reg->registry) {
                PERROR("zmalloc buffer registry pid session");
                ret = -ENOMEM;
                goto error;
@@ -247,17 +256,32 @@ int buffer_reg_pid_create(int session_id, struct buffer_reg_pid **regp)
 
        /* A cast is done here so we can use the session ID as a u64 ht node. */
        reg->session_id = session_id;
-
+       if (shm_path[0]) {
+               strncpy(reg->root_shm_path, root_shm_path, sizeof(reg->root_shm_path));
+               reg->root_shm_path[sizeof(reg->root_shm_path) - 1] = '\0';
+               strncpy(reg->shm_path, shm_path, sizeof(reg->shm_path));
+               reg->shm_path[sizeof(reg->shm_path) - 1] = '\0';
+               DBG3("shm path '%s' is assigned to pid buffer registry for session id %" PRIu64,
+                               reg->shm_path, session_id);
+       }
        reg->registry->channels = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
        if (!reg->registry->channels) {
                ret = -ENOMEM;
                goto error_session;
        }
 
-       lttng_ht_node_init_ulong(&reg->node, reg->session_id);
+       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;
 
-       DBG3("Buffer registry per PID created with session id: %d", session_id);
+       DBG3("Buffer registry per PID created with session id: %" PRIu64,
+                       session_id);
 
        return 0;
 
@@ -275,11 +299,11 @@ void buffer_reg_pid_add(struct buffer_reg_pid *reg)
 {
        assert(reg);
 
-       DBG3("Buffer registry per PID adding to global registry with id: %d",
+       DBG3("Buffer registry per PID adding to global registry with id: %" PRIu64,
                        reg->session_id);
 
        rcu_read_lock();
-       lttng_ht_add_unique_ulong(buffer_registry_pid, &reg->node);
+       lttng_ht_add_unique_u64(buffer_registry_pid, &reg->node);
        rcu_read_unlock();
 }
 
@@ -289,17 +313,17 @@ void buffer_reg_pid_add(struct buffer_reg_pid *reg)
  *
  * Return the object pointer or NULL on error.
  */
-struct buffer_reg_pid *buffer_reg_pid_find(int session_id)
+struct buffer_reg_pid *buffer_reg_pid_find(uint64_t session_id)
 {
-       struct lttng_ht_node_ulong *node;
+       struct lttng_ht_node_u64 *node;
        struct lttng_ht_iter iter;
        struct buffer_reg_pid *reg = NULL;
        struct lttng_ht *ht = buffer_registry_pid;
 
-       DBG3("Buffer registry per PID find id: %d", session_id);
+       DBG3("Buffer registry per PID find id: %" PRIu64, session_id);
 
-       lttng_ht_lookup(ht, (void *)((unsigned long) session_id), &iter);
-       node = lttng_ht_iter_get_node_ulong(&iter);
+       lttng_ht_lookup(ht, &session_id, &iter);
+       node = lttng_ht_iter_get_node_u64(&iter);
        if (!node) {
                goto end;
        }
@@ -309,6 +333,43 @@ end:
        return reg;
 }
 
+/*
+ * Find the consumer channel key from a UST session per-uid channel key.
+ *
+ * Return the matching key or -1 if not found.
+ */
+int buffer_reg_uid_consumer_channel_key(
+               struct cds_list_head *buffer_reg_uid_list,
+               uint64_t chan_key, uint64_t *consumer_chan_key)
+{
+       struct lttng_ht_iter iter;
+       struct buffer_reg_uid *uid_reg = NULL;
+       struct buffer_reg_session *session_reg = NULL;
+       struct buffer_reg_channel *reg_chan;
+       int ret = -1;
+
+       rcu_read_lock();
+       /*
+        * For the per-uid registry, we have to iterate since we don't have the
+        * uid and bitness key.
+        */
+       cds_list_for_each_entry(uid_reg, buffer_reg_uid_list, lnode) {
+               session_reg = uid_reg->registry;
+               cds_lfht_for_each_entry(session_reg->channels->ht,
+                               &iter.iter, reg_chan, node.node) {
+                       if (reg_chan->key == chan_key) {
+                               *consumer_chan_key = reg_chan->consumer_key;
+                               ret = 0;
+                               goto end;
+                       }
+               }
+       }
+
+end:
+       rcu_read_unlock();
+       return ret;
+}
+
 /*
  * Allocate and initialize a buffer registry channel with the given key. Set
  * regp with the object pointer.
@@ -339,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.
@@ -364,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.
  */
@@ -375,9 +491,25 @@ void buffer_reg_stream_add(struct buffer_reg_stream *stream,
 
        pthread_mutex_lock(&channel->stream_list_lock);
        cds_list_add_tail(&stream->lnode, &channel->streams);
+       channel->stream_count++;
        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.
  */
@@ -392,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
@@ -429,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.
  */
@@ -447,7 +630,7 @@ void buffer_reg_stream_destroy(struct buffer_reg_stream *regp,
        {
                int ret;
 
-               ret = ust_ctl_release_object(-1, 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 stream release obj handle %d failed with ret %d",
                                        regp->obj.ust->handle, ret);
@@ -464,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.
@@ -482,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.
  */
@@ -492,8 +728,7 @@ void buffer_reg_channel_destroy(struct buffer_reg_channel *regp,
                return;
        }
 
-       DBG3("Buffer registry channel destroy with key %" PRIu32 " and handle %d",
-                       regp->key, regp->obj.ust->handle);
+       DBG3("Buffer registry channel destroy with key %" PRIu32, regp->key);
 
        switch (domain) {
        case LTTNG_DOMAIN_UST:
@@ -503,11 +738,12 @@ void buffer_reg_channel_destroy(struct buffer_reg_channel *regp,
                /* Wipe stream */
                cds_list_for_each_entry_safe(sreg, stmp, &regp->streams, lnode) {
                        cds_list_del(&sreg->lnode);
+                       regp->stream_count--;
                        buffer_reg_stream_destroy(sreg, domain);
                }
 
                if (regp->obj.ust) {
-                       ret = ust_ctl_release_object(-1, 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 channel release obj handle %d failed with ret %d",
                                                regp->obj.ust->handle, ret);
@@ -525,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.
  *
@@ -536,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");
 
@@ -547,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();
 
-       lttng_ht_destroy(regp->channels);
+       ht_cleanup_push(regp->channels);
+       ht_cleanup_push(regp->maps);
 
        switch (domain) {
        case LTTNG_DOMAIN_UST:
@@ -594,8 +881,8 @@ static void rcu_free_buffer_reg_uid(struct rcu_head *head)
 
 static void rcu_free_buffer_reg_pid(struct rcu_head *head)
 {
-       struct lttng_ht_node_ulong *node =
-               caa_container_of(head, struct lttng_ht_node_ulong, head);
+       struct lttng_ht_node_u64 *node =
+               caa_container_of(head, struct lttng_ht_node_u64, head);
        struct buffer_reg_pid *reg =
                caa_container_of(node, struct buffer_reg_pid, node);
 
@@ -617,7 +904,7 @@ void buffer_reg_uid_destroy(struct buffer_reg_uid *regp,
                return;
        }
 
-       DBG3("Buffer registry per UID destroy with id: %d, ABI: %u, uid: %d",
+       DBG3("Buffer registry per UID destroy with id: %" PRIu64 ", ABI: %u, uid: %d",
                        regp->session_id, regp->bits_per_long, regp->uid);
 
        if (!consumer) {
@@ -679,7 +966,8 @@ void buffer_reg_pid_destroy(struct buffer_reg_pid *regp)
                return;
        }
 
-       DBG3("Buffer registry per PID destroy with id: %d", regp->session_id);
+       DBG3("Buffer registry per PID destroy with id: %" PRIu64,
+                       regp->session_id);
 
        /* This registry is only used by UST. */
        call_rcu(&regp->node.head, rcu_free_buffer_reg_pid);
@@ -693,6 +981,6 @@ void buffer_reg_pid_destroy(struct buffer_reg_pid *regp)
 void buffer_reg_destroy_registries(void)
 {
        DBG3("Buffer registry destroy all registry");
-       lttng_ht_destroy(buffer_registry_uid);
-       lttng_ht_destroy(buffer_registry_pid);
+       ht_cleanup_push(buffer_registry_uid);
+       ht_cleanup_push(buffer_registry_pid);
 }
This page took 0.047281 seconds and 5 git commands to generate.