From 9fa5211eebb7270fd0d346d7f3a4a35be1f8b091 Mon Sep 17 00:00:00 2001 From: Jonathan Rajotte Date: Thu, 14 Apr 2022 16:01:25 -0400 Subject: [PATCH] Convert ltt_session to c++ This is a minimal patch to move the ltt_ust_session struct to c++ style. This will allow us to add complex type in the struct later on (such as unique_ptr/shared_ptr etc). Use default member initialization when possible. Signed-off-by: Jonathan Rajotte Change-Id: I76b2a0c9befc7f982599bcc9d8072c859a1731e5 --- src/bin/lttng-sessiond/cmd.cpp | 6 +- src/bin/lttng-sessiond/session.cpp | 11 +-- src/bin/lttng-sessiond/session.hpp | 110 +++++++++++++++-------------- 3 files changed, 68 insertions(+), 59 deletions(-) diff --git a/src/bin/lttng-sessiond/cmd.cpp b/src/bin/lttng-sessiond/cmd.cpp index 0db41e127..8f2c37958 100644 --- a/src/bin/lttng-sessiond/cmd.cpp +++ b/src/bin/lttng-sessiond/cmd.cpp @@ -2751,7 +2751,7 @@ int cmd_start_trace(struct ltt_session *session) goto error; } - session->active = 1; + session->active = true; session->rotated_after_last_stop = false; session->cleared_after_last_stop = false; if (session->output_traces && !session->current_trace_chunk) { @@ -2846,9 +2846,9 @@ int cmd_start_trace(struct ltt_session *session) error: if (ret == LTTNG_OK) { /* Flag this after a successful start. */ - session->has_been_started |= 1; + session->has_been_started = true; } else { - session->active = 0; + session->active = false; /* Restore initial state on error. */ session->rotated_after_last_stop = session_rotated_after_last_stop; diff --git a/src/bin/lttng-sessiond/session.cpp b/src/bin/lttng-sessiond/session.cpp index e9d726103..a2671dbae 100644 --- a/src/bin/lttng-sessiond/session.cpp +++ b/src/bin/lttng-sessiond/session.cpp @@ -1036,10 +1036,11 @@ void session_release(struct urcu_ref *ref) } lttng_dynamic_array_reset(&session->destroy_notifiers); lttng_dynamic_array_reset(&session->clear_notifiers); + free(session->last_archived_chunk_name); free(session->base_path); lttng_trigger_put(session->rotate_trigger); - free(session); + delete (session); if (session_published) { /* * Broadcast after free-ing to ensure the memory is @@ -1215,9 +1216,11 @@ enum lttng_error_code session_create(const char *name, uid_t uid, gid_t gid, goto error; } } - new_session = zmalloc(); - if (!new_session) { - PERROR("Failed to allocate an ltt_session structure"); + + try { + new_session = new ltt_session(); + } catch (const std::exception& ex) { + ERR("Failed to create ltt_session: %s", ex.what()); ret_code = LTTNG_ERR_NOMEM; goto error; } diff --git a/src/bin/lttng-sessiond/session.hpp b/src/bin/lttng-sessiond/session.hpp index 12ac3eb73..84aeafee7 100644 --- a/src/bin/lttng-sessiond/session.hpp +++ b/src/bin/lttng-sessiond/session.hpp @@ -83,61 +83,67 @@ struct ltt_session { lttng::sessiond::details::locked_session_release>::deleter>; using sptr = std::shared_ptr; - char name[NAME_MAX]; - bool has_auto_generated_name; - bool name_contains_creation_time; - char hostname[LTTNG_HOST_NAME_MAX]; /* Local hostname. */ + ltt_session() + { + has_been_started = 0; + active = 0; + }; + ~ltt_session() = default; + + char name[NAME_MAX]{0}; + bool has_auto_generated_name{false}; + bool name_contains_creation_time{false}; + char hostname[LTTNG_HOST_NAME_MAX]{0}; /* Local hostname. */ /* Path of the last closed chunk. */ - char last_chunk_path[LTTNG_PATH_MAX]; - time_t creation_time; - struct ltt_kernel_session *kernel_session; - struct ltt_ust_session *ust_session; - struct urcu_ref ref; + char last_chunk_path[LTTNG_PATH_MAX]{0}; + time_t creation_time{}; + struct ltt_kernel_session *kernel_session{}; + struct ltt_ust_session *ust_session{}; + struct urcu_ref ref {}; /* * Protect any read/write on this session data structure. This lock must be * acquired *before* using any public functions declared below. Use * session_lock() and session_unlock() for that. */ - pthread_mutex_t lock; - struct cds_list_head list; - /* session unique identifier */ - id_t id; + pthread_mutex_t lock{}; + struct cds_list_head list {}; + id_t id{}; /* session unique identifier */ /* Indicates if the session has been added to the session list and ht.*/ - bool published; + bool published{false}; /* Indicates if a destroy command has been applied to this session. */ - bool destroyed; + bool destroyed{false}; /* UID/GID of the user owning the session */ - uid_t uid; - gid_t gid; + uid_t uid{}; + gid_t gid{}; /* * Network session handle. A value of 0 means that there is no remote * session established. */ - uint64_t net_handle; + uint64_t net_handle{}; /* * This consumer is only set when the create_session_uri call is made. * This contains the temporary information for a consumer output. Upon * creation of the UST or kernel session, this consumer, if available, is * copied into those sessions. */ - struct consumer_output *consumer; + struct consumer_output *consumer{}; /* * Indicates whether or not the user has specified an output directory * or if it was configured using the default configuration. */ - bool has_user_specified_directory; + bool has_user_specified_directory{false}; /* Did at least ONE start command has been triggered?. */ - unsigned int has_been_started:1; + bool has_been_started{false}; /* - * Is the session active? Start trace command sets this to 1 and the stop - * command reset it to 0. + * Is the session active? Start trace command sets this to true and the stop + * command reset it to false. */ - unsigned int active:1; + bool active{false}; /* Snapshot representation in a session. */ - struct snapshot snapshot; + struct snapshot snapshot {}; /* Indicate if the session has to output the traces or not. */ - unsigned int output_traces; + unsigned int output_traces{}; /* * This session is in snapshot mode. This means that channels enabled * will be set in overwrite mode by default and must be in mmap @@ -145,42 +151,42 @@ struct ltt_session { * is not in "snapshot_mode". This parameter only affects channel * creation defaults. */ - unsigned int snapshot_mode; + unsigned int snapshot_mode{}; /* * A session that has channels that don't use 'mmap' output can't be * used to capture snapshots. This is set to true whenever a * 'splice' kernel channel is enabled. */ - bool has_non_mmap_channel; + bool has_non_mmap_channel{false}; /* * Timer set when the session is created for live reading. */ - unsigned int live_timer; + unsigned int live_timer{0}; /* * Path where to keep the shared memory files. */ - char shm_path[PATH_MAX]; + char shm_path[PATH_MAX]{}; /* * Node in ltt_sessions_ht_by_id. */ - struct lttng_ht_node_u64 node; + struct lttng_ht_node_u64 node {}; /* * Node in ltt_sessions_ht_by_name. */ - struct lttng_ht_node_str node_by_name; + struct lttng_ht_node_str node_by_name {}; /* * Timer to check periodically if a relay and/or consumer has completed * the last rotation. */ - bool rotation_pending_check_timer_enabled; - timer_t rotation_pending_check_timer; + bool rotation_pending_check_timer_enabled{false}; + timer_t rotation_pending_check_timer{}; /* Timer to periodically rotate a session. */ - bool rotation_schedule_timer_enabled; - timer_t rotation_schedule_timer; + bool rotation_schedule_timer_enabled{false}; + timer_t rotation_schedule_timer{}; /* Value for periodic rotations, 0 if disabled. */ - uint64_t rotate_timer_period; + uint64_t rotate_timer_period{}; /* Value for size-based rotations, 0 if disabled. */ - uint64_t rotate_size; + uint64_t rotate_size{}; /* * Keep a state if this session was rotated after the last stop command. * We only allow one rotation after a stop. At destroy, we also need to @@ -188,33 +194,33 @@ struct ltt_session { * chunk. After a stop followed by rotate, all subsequent clear * (without prior start) will succeed, but will be effect-less. */ - bool rotated_after_last_stop; + bool rotated_after_last_stop{false}; /* * Track whether the session was cleared after last stop. All subsequent * clear (without prior start) will succeed, but will be effect-less. A * subsequent rotate (without prior start) will return an error. */ - bool cleared_after_last_stop; + bool cleared_after_last_stop{false}; /* * True if the session has had an explicit non-quiet rotation. */ - bool rotated; + bool rotated{false}; /* * Trigger for size-based rotations. */ - struct lttng_trigger *rotate_trigger; - LTTNG_OPTIONAL(uint64_t) most_recent_chunk_id; - struct lttng_trace_chunk *current_trace_chunk; - struct lttng_trace_chunk *chunk_being_archived; + struct lttng_trigger *rotate_trigger{}; + LTTNG_OPTIONAL(uint64_t) most_recent_chunk_id{}; + struct lttng_trace_chunk *current_trace_chunk{}; + struct lttng_trace_chunk *chunk_being_archived{}; /* Current state of a rotation. */ - enum lttng_rotation_state rotation_state; - bool quiet_rotation; - char *last_archived_chunk_name; - LTTNG_OPTIONAL(uint64_t) last_archived_chunk_id; - struct lttng_dynamic_array destroy_notifiers; - struct lttng_dynamic_array clear_notifiers; + enum lttng_rotation_state rotation_state { LTTNG_ROTATION_STATE_NO_ROTATION }; + bool quiet_rotation{false}; + char *last_archived_chunk_name{}; + LTTNG_OPTIONAL(uint64_t) last_archived_chunk_id{}; + struct lttng_dynamic_array destroy_notifiers {}; + struct lttng_dynamic_array clear_notifiers {}; /* Session base path override. Set non-null. */ - char *base_path; + char *base_path{}; }; enum lttng_error_code session_create(const char *name, uid_t uid, gid_t gid, -- 2.34.1