Cleanup: remove duplicated code in snapshot record command
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 22 May 2019 20:28:39 +0000 (16:28 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 5 Sep 2019 20:39:13 +0000 (16:39 -0400)
The session daemon's snapshot record command contains duplicated
code depending on the configuration of the snapshot output. This
makes it harder to modify the code in a follow-up commit.

Those code paths are made const-correct by the same occasion.

No behaviour change is intended.

Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/bin/lttng-sessiond/cmd.c
src/bin/lttng-sessiond/consumer.c
src/bin/lttng-sessiond/consumer.h
src/bin/lttng-sessiond/kernel.c
src/bin/lttng-sessiond/kernel.h
src/bin/lttng-sessiond/ust-app.c
src/bin/lttng-sessiond/ust-app.h
src/common/hashtable/hashtable.c
src/common/hashtable/hashtable.h
src/common/relayd/relayd.c
src/common/relayd/relayd.h

index 0ba1ec4f778a9682081f11c656d6132c488eee13..3cf8c518c9493aefa7a6e650b5b7f2dffaa2047e 100644 (file)
@@ -1030,7 +1030,8 @@ static enum lttng_error_code send_consumer_relayd_socket(
                struct lttng_uri *relayd_uri,
                struct consumer_output *consumer,
                struct consumer_socket *consumer_sock,
-               char *session_name, char *hostname, int session_live_timer)
+               const char *session_name, const char *hostname,
+               int session_live_timer)
 {
        int ret;
        struct lttcomm_relayd_sock *rsock = NULL;
@@ -1104,8 +1105,8 @@ relayd_comm_error:
 static enum lttng_error_code send_consumer_relayd_sockets(
                enum lttng_domain_type domain,
                unsigned int session_id, struct consumer_output *consumer,
-               struct consumer_socket *sock, char *session_name,
-               char *hostname, int session_live_timer)
+               struct consumer_socket *sock, const char *session_name,
+               const char *hostname, int session_live_timer)
 {
        enum lttng_error_code status = LTTNG_OK;
 
@@ -4103,8 +4104,8 @@ end:
  */
 static enum lttng_error_code set_relayd_for_snapshot(
                struct consumer_output *consumer,
-               struct snapshot_output *snap_output,
-               struct ltt_session *session)
+               const struct snapshot_output *snap_output,
+               const struct ltt_session *session)
 {
        enum lttng_error_code status = LTTNG_OK;
        struct lttng_ht_iter iter;
@@ -4150,8 +4151,10 @@ error:
  *
  * Return LTTNG_OK on success or a LTTNG_ERR code.
  */
-static enum lttng_error_code record_kernel_snapshot(struct ltt_kernel_session *ksess,
-               struct snapshot_output *output, struct ltt_session *session,
+static enum lttng_error_code record_kernel_snapshot(
+               struct ltt_kernel_session *ksess,
+               const struct snapshot_output *output,
+               const struct ltt_session *session,
                int wait, uint64_t nb_packets_per_stream)
 {
        int ret;
@@ -4197,8 +4200,9 @@ end:
  * Returns LTTNG_OK on success or a LTTNG_ERR error code.
  */
 static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess,
-               struct snapshot_output *output, struct ltt_session *session,
-               int wait, uint64_t nb_packets_per_stream)
+               const struct snapshot_output *output,
+               const struct ltt_session *session, int wait,
+               uint64_t nb_packets_per_stream)
 {
        int ret;
        enum lttng_error_code status;
@@ -4222,7 +4226,8 @@ static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess,
                goto error_snapshot;
        }
 
-       status = ust_app_snapshot_record(usess, output, wait, nb_packets_per_stream);
+       status = ust_app_snapshot_record(usess, output, wait,
+                       nb_packets_per_stream);
        if (status != LTTNG_OK) {
                goto error_snapshot;
        }
@@ -4238,14 +4243,15 @@ end:
 }
 
 static
-uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session *session,
-       uint64_t cur_nr_packets)
+uint64_t get_session_size_one_more_packet_per_stream(
+               const struct ltt_session *session, uint64_t cur_nr_packets)
 {
        uint64_t tot_size = 0;
 
        if (session->kernel_session) {
                struct ltt_kernel_channel *chan;
-               struct ltt_kernel_session *ksess = session->kernel_session;
+               const struct ltt_kernel_session *ksess =
+                               session->kernel_session;
 
                cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
                        if (cur_nr_packets >= chan->channel->attr.num_subbuf) {
@@ -4261,7 +4267,7 @@ uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session *session
        }
 
        if (session->ust_session) {
-               struct ltt_ust_session *usess = session->ust_session;
+               const struct ltt_ust_session *usess = session->ust_session;
 
                tot_size += ust_app_get_size_one_more_packet_per_stream(usess,
                                cur_nr_packets);
@@ -4291,7 +4297,8 @@ uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session *session
  * in between this call and actually grabbing data.
  */
 static
-int64_t get_session_nb_packets_per_stream(struct ltt_session *session, uint64_t max_size)
+int64_t get_session_nb_packets_per_stream(const struct ltt_session *session,
+               uint64_t max_size)
 {
        int64_t size_left;
        uint64_t cur_nb_packets = 0;
@@ -4304,8 +4311,8 @@ int64_t get_session_nb_packets_per_stream(struct ltt_session *session, uint64_t
        for (;;) {
                uint64_t one_more_packet_tot_size;
 
-               one_more_packet_tot_size = get_session_size_one_more_packet_per_stream(session,
-                                       cur_nb_packets);
+               one_more_packet_tot_size = get_session_size_one_more_packet_per_stream(
+                               session, cur_nb_packets);
                if (!one_more_packet_tot_size) {
                        /* We are already grabbing all packets. */
                        break;
@@ -4323,6 +4330,41 @@ int64_t get_session_nb_packets_per_stream(struct ltt_session *session, uint64_t
        return cur_nb_packets;
 }
 
+static
+enum lttng_error_code snapshot_record(const struct ltt_session *session,
+               const struct snapshot_output *snapshot_output, int wait)
+{
+       int64_t nb_packets_per_stream;
+       enum lttng_error_code ret = LTTNG_OK;
+
+       nb_packets_per_stream = get_session_nb_packets_per_stream(session,
+                       snapshot_output->max_size);
+       if (nb_packets_per_stream < 0) {
+               ret = LTTNG_ERR_MAX_SIZE_INVALID;
+               goto end;
+       }
+
+       if (session->kernel_session) {
+               ret = record_kernel_snapshot(session->kernel_session,
+                               snapshot_output, session,
+                               wait, nb_packets_per_stream);
+               if (ret != LTTNG_OK) {
+                       goto end;
+               }
+       }
+
+       if (session->ust_session) {
+               ret = record_ust_snapshot(session->ust_session,
+                               snapshot_output, session,
+                               wait, nb_packets_per_stream);
+               if (ret != LTTNG_OK) {
+                       goto end;
+               }
+       }
+end:
+       return ret;
+}
+
 /*
  * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
  *
@@ -4393,33 +4435,10 @@ int cmd_snapshot_record(struct ltt_session *session,
        }
 
        if (use_tmp_output) {
-               int64_t nb_packets_per_stream;
-
-               nb_packets_per_stream = get_session_nb_packets_per_stream(session,
-                               tmp_output.max_size);
-               if (nb_packets_per_stream < 0) {
-                       cmd_ret = LTTNG_ERR_MAX_SIZE_INVALID;
+               cmd_ret = snapshot_record(session, &tmp_output, wait);
+               if (cmd_ret != LTTNG_OK) {
                        goto error;
                }
-
-               if (session->kernel_session) {
-                       cmd_ret = record_kernel_snapshot(session->kernel_session,
-                                       &tmp_output, session,
-                                       wait, nb_packets_per_stream);
-                       if (cmd_ret != LTTNG_OK) {
-                               goto error;
-                       }
-               }
-
-               if (session->ust_session) {
-                       cmd_ret = record_ust_snapshot(session->ust_session,
-                                       &tmp_output, session,
-                                       wait, nb_packets_per_stream);
-                       if (cmd_ret != LTTNG_OK) {
-                               goto error;
-                       }
-               }
-
                snapshot_success = 1;
        } else {
                struct snapshot_output *sout;
@@ -4428,26 +4447,18 @@ int cmd_snapshot_record(struct ltt_session *session,
                rcu_read_lock();
                cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
                                &iter.iter, sout, node.node) {
-                       int64_t nb_packets_per_stream;
-
                        /*
-                        * Make a local copy of the output and assign the possible
-                        * temporary value given by the caller.
+                        * Make a local copy of the output and assign the
+                        * possible temporary value given by the caller.
                         */
-                       memset(&tmp_output, 0, sizeof(tmp_output));
                        memcpy(&tmp_output, sout, sizeof(tmp_output));
 
                        if (output->max_size != (uint64_t) -1ULL) {
                                tmp_output.max_size = output->max_size;
                        }
 
-                       nb_packets_per_stream = get_session_nb_packets_per_stream(session,
-                                       tmp_output.max_size);
-                       if (nb_packets_per_stream < 0) {
-                               cmd_ret = LTTNG_ERR_MAX_SIZE_INVALID;
-                               rcu_read_unlock();
-                               goto error;
-                       }
+                       tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
+                       memcpy(tmp_output.datetime, datetime, sizeof(datetime));
 
                        /* Use temporary name. */
                        if (*output->name != '\0') {
@@ -4459,27 +4470,10 @@ int cmd_snapshot_record(struct ltt_session *session,
                                }
                        }
 
-                       tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
-                       memcpy(tmp_output.datetime, datetime, sizeof(datetime));
-
-                       if (session->kernel_session) {
-                               cmd_ret = record_kernel_snapshot(session->kernel_session,
-                                               &tmp_output, session,
-                                               wait, nb_packets_per_stream);
-                               if (cmd_ret != LTTNG_OK) {
-                                       rcu_read_unlock();
-                                       goto error;
-                               }
-                       }
-
-                       if (session->ust_session) {
-                               cmd_ret = record_ust_snapshot(session->ust_session,
-                                               &tmp_output, session,
-                                               wait, nb_packets_per_stream);
-                               if (cmd_ret != LTTNG_OK) {
-                                       rcu_read_unlock();
-                                       goto error;
-                               }
+                       cmd_ret = snapshot_record(session, &tmp_output, wait);
+                       if (cmd_ret != LTTNG_OK) {
+                               rcu_read_unlock();
+                               goto error;
                        }
                        snapshot_success = 1;
                }
index 171301ab5688fafc305210d955c5daa94e204977..99b22210d3ffff07538900b465a32e46008ba8e4 100644 (file)
@@ -1058,7 +1058,8 @@ error:
 int consumer_send_relayd_socket(struct consumer_socket *consumer_sock,
                struct lttcomm_relayd_sock *rsock, struct consumer_output *consumer,
                enum lttng_stream_type type, uint64_t session_id,
-               char *session_name, char *hostname, int session_live_timer)
+               const char *session_name, const char *hostname,
+               int session_live_timer)
 {
        int ret;
        struct lttcomm_consumer_msg msg;
@@ -1418,7 +1419,7 @@ end:
  * Returns LTTNG_OK on success or else an LTTng error code.
  */
 enum lttng_error_code consumer_snapshot_channel(struct consumer_socket *socket,
-               uint64_t key, struct snapshot_output *output, int metadata,
+               uint64_t key, const struct snapshot_output *output, int metadata,
                uid_t uid, gid_t gid, const char *session_path, int wait,
                uint64_t nb_packets_per_stream, uint64_t trace_archive_id)
 {
index caa076c8993a8157ca634c06a8dde1ab61fe7b02..c0f7fee360874d687c8939e11496f7e15e429ffe 100644 (file)
@@ -212,7 +212,8 @@ int consumer_send_channel(struct consumer_socket *sock,
 int consumer_send_relayd_socket(struct consumer_socket *consumer_sock,
                struct lttcomm_relayd_sock *rsock, struct consumer_output *consumer,
                enum lttng_stream_type type, uint64_t session_id,
-               char *session_name, char *hostname, int session_live_timer);
+               const char *session_name, const char *hostname,
+               int session_live_timer);
 int consumer_send_channel_monitor_pipe(struct consumer_socket *consumer_sock,
                int pipe);
 int consumer_send_destroy_relayd(struct consumer_socket *sock,
@@ -294,7 +295,7 @@ int consumer_get_lost_packets(uint64_t session_id, uint64_t channel_key,
 
 /* Snapshot command. */
 enum lttng_error_code consumer_snapshot_channel(struct consumer_socket *socket,
-               uint64_t key, struct snapshot_output *output, int metadata,
+               uint64_t key, const struct snapshot_output *output, int metadata,
                uid_t uid, gid_t gid, const char *session_path, int wait,
                uint64_t nb_packets_per_stream, uint64_t trace_archive_id);
 
index 7173fac16348d59cab3f2e220cb927797761fcf3..9268a277956aab08ff2d1d04d2e93258c4e3ce5e 100644 (file)
@@ -1240,8 +1240,9 @@ void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
  *
  * Return LTTNG_OK on success or else return a LTTNG_ERR code.
  */
-enum lttng_error_code kernel_snapshot_record(struct ltt_kernel_session *ksess,
-               struct snapshot_output *output, int wait,
+enum lttng_error_code kernel_snapshot_record(
+               struct ltt_kernel_session *ksess,
+               const struct snapshot_output *output, int wait,
                uint64_t nb_packets_per_stream)
 {
        int err, ret, saved_metadata_fd;
index 9b0c15cd14fd866fe71654b9ee41134b9d144de3..5fcd7e4c56681f505d0f59c1bc956a725d9a999c 100644 (file)
@@ -59,8 +59,9 @@ int kernel_validate_version(int tracer_fd,
                struct lttng_kernel_tracer_abi_version *kernel_tracer_abi_version);
 void kernel_destroy_session(struct ltt_kernel_session *ksess);
 void kernel_destroy_channel(struct ltt_kernel_channel *kchan);
-enum lttng_error_code kernel_snapshot_record(struct ltt_kernel_session *ksess,
-               struct snapshot_output *output, int wait,
+enum lttng_error_code kernel_snapshot_record(
+               struct ltt_kernel_session *ksess,
+               const struct snapshot_output *output, int wait,
                uint64_t nb_packets_per_stream);
 int kernel_syscall_mask(int chan_fd, char **syscall_mask, uint32_t *nr_bits);
 enum lttng_error_code kernel_rotate_session(struct ltt_session *session);
index 6b9afe3abf4ce0180cb3957da00cdd8036ac6cf7..4cc21b42cab971bd954447b55023f941db077804 100644 (file)
@@ -1923,7 +1923,7 @@ error:
  * Lookup sesison wrapper.
  */
 static
-void __lookup_session_by_app(struct ltt_ust_session *usess,
+void __lookup_session_by_app(const struct ltt_ust_session *usess,
                        struct ust_app *app, struct lttng_ht_iter *iter)
 {
        /* Get right UST app session from app */
@@ -1935,7 +1935,7 @@ void __lookup_session_by_app(struct ltt_ust_session *usess,
  * id.
  */
 static struct ust_app_session *lookup_session_by_app(
-               struct ltt_ust_session *usess, struct ust_app *app)
+               const struct ltt_ust_session *usess, struct ust_app *app)
 {
        struct lttng_ht_iter iter;
        struct lttng_ht_node_u64 *node;
@@ -5874,8 +5874,9 @@ void ust_app_destroy(struct ust_app *app)
  *
  * Returns LTTNG_OK on success or a LTTNG_ERR error code.
  */
-enum lttng_error_code ust_app_snapshot_record(struct ltt_ust_session *usess,
-               struct snapshot_output *output, int wait,
+enum lttng_error_code ust_app_snapshot_record(
+               const struct ltt_ust_session *usess,
+               const struct snapshot_output *output, int wait,
                uint64_t nb_packets_per_stream)
 {
        int ret = 0;
@@ -6040,8 +6041,8 @@ error:
 /*
  * Return the size taken by one more packet per stream.
  */
-uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session *usess,
-               uint64_t cur_nr_packets)
+uint64_t ust_app_get_size_one_more_packet_per_stream(
+               const struct ltt_ust_session *usess, uint64_t cur_nr_packets)
 {
        uint64_t tot_size = 0;
        struct ust_app *app;
index b90ff4bcef8833459268ba02f62dfc2e40bfc7e8..0627dd904b939dbe334076ba3870038b0d75581e 100644 (file)
@@ -340,11 +340,12 @@ void ust_app_notify_sock_unregister(int sock);
 ssize_t ust_app_push_metadata(struct ust_registry_session *registry,
                struct consumer_socket *socket, int send_zero_data);
 void ust_app_destroy(struct ust_app *app);
-enum lttng_error_code ust_app_snapshot_record(struct ltt_ust_session *usess,
-               struct snapshot_output *output, int wait,
+enum lttng_error_code ust_app_snapshot_record(
+               const struct ltt_ust_session *usess,
+               const struct snapshot_output *output, int wait,
                uint64_t nb_packets_per_stream);
 uint64_t ust_app_get_size_one_more_packet_per_stream(
-               struct ltt_ust_session *usess, uint64_t cur_nr_packets);
+               const struct ltt_ust_session *usess, uint64_t cur_nr_packets);
 struct ust_app *ust_app_find_by_sock(int sock);
 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id,
                struct cds_list_head *buffer_reg_uid_list,
index 4fb19cffda923bf1f19062be891f6180f0e48c14..78175ca6d508d24b2ffd4fa29a82ce8fba3633ff 100644 (file)
@@ -259,7 +259,7 @@ void lttng_ht_node_free_two_u64(struct lttng_ht_node_two_u64 *node)
  * Lookup function in hashtable.
  */
 LTTNG_HIDDEN
-void lttng_ht_lookup(struct lttng_ht *ht, void *key,
+void lttng_ht_lookup(struct lttng_ht *ht, const void *key,
                struct lttng_ht_iter *iter)
 {
        assert(ht);
index ea58bfe12010c694339e178d9645cb8c853a49be..0dd925645fcd9d725105c9cfda3157efe48106ff 100644 (file)
@@ -103,7 +103,7 @@ LTTNG_HIDDEN
 void lttng_ht_node_free_two_u64(struct lttng_ht_node_two_u64 *node);
 
 LTTNG_HIDDEN
-void lttng_ht_lookup(struct lttng_ht *ht, void *key,
+void lttng_ht_lookup(struct lttng_ht *ht, const void *key,
                struct lttng_ht_iter *iter);
 
 /* Specialized add unique functions */
index ff0be32a17190f2a45ae1a4b91987f5a1a774619..08bbad23bc89a15339496ff62ab944c1a699b1bc 100644 (file)
@@ -126,7 +126,7 @@ error:
  * payload size is introduced.
  */
 static int relayd_create_session_2_11(struct lttcomm_relayd_sock *rsock,
-               char *session_name, char *hostname,
+               const char *session_name, const char *hostname,
                int session_live_timer, unsigned int snapshot,
                uint64_t sessiond_session_id, const lttng_uuid sessiond_uuid)
 {
@@ -183,8 +183,8 @@ error:
  * support the live reading capability.
  */
 static int relayd_create_session_2_4(struct lttcomm_relayd_sock *rsock,
-               char *session_name, char *hostname, int session_live_timer,
-               unsigned int snapshot)
+               const char *session_name, const char *hostname,
+               int session_live_timer, unsigned int snapshot)
 {
        int ret;
        struct lttcomm_relayd_create_session_2_4 msg;
@@ -235,8 +235,10 @@ error:
  * On success, return 0 else a negative value which is either an errno error or
  * a lttng error code from the relayd.
  */
-int relayd_create_session(struct lttcomm_relayd_sock *rsock, uint64_t *relayd_session_id,
-               char *session_name, char *hostname, int session_live_timer,
+int relayd_create_session(struct lttcomm_relayd_sock *rsock,
+               uint64_t *relayd_session_id,
+               const char *session_name, const char *hostname,
+               int session_live_timer,
                unsigned int snapshot, uint64_t sessiond_session_id,
                const lttng_uuid sessiond_uuid)
 {
index 2fcdcf4c76b5a3c2e84dcd749101bfa614784332..af93188eeb1df520df3d92cc570dde3fd54dcb59 100644 (file)
 int relayd_connect(struct lttcomm_relayd_sock *sock);
 int relayd_close(struct lttcomm_relayd_sock *sock);
 int relayd_create_session(struct lttcomm_relayd_sock *sock,
-               uint64_t *relayd_session_id, char *session_name, char *hostname,
-               int session_live_timer, unsigned int snapshot,
-               uint64_t sessiond_session_id, const lttng_uuid sessiond_uuid);
+               uint64_t *relayd_session_id, const char *session_name,
+               const char *hostname, int session_live_timer,
+               unsigned int snapshot, uint64_t sessiond_session_id,
+               const lttng_uuid sessiond_uuid);
 int relayd_add_stream(struct lttcomm_relayd_sock *sock, const char *channel_name,
                const char *pathname, uint64_t *stream_id,
                uint64_t tracefile_size, uint64_t tracefile_count,
This page took 0.036221 seconds and 4 git commands to generate.