Improve benchmark systems and add create/destroy session
[lttng-tools.git] / ltt-sessiond / main.c
index 5db41869f5b1e8c5176cbe07ca42b79e302e52a8..8d9cfdfbcf31535aa2ee8b7127471fef07247116 100644 (file)
@@ -41,6 +41,7 @@
 
 #include <urcu/list.h>         /* URCU list library (-lurcu) */
 #include <lttng/lttng.h>
+#include <lttng/lttng-kconsumerd.h>
 #include <lttng-sessiond-comm.h>
 
 #include "context.h"
 #include "ust-ctl.h"
 #include "session.h"
 #include "traceable-app.h"
-#include "lttng-kconsumerd.h"
+#include "ltt-kconsumerd.h"
 #include "utils.h"
 
+#include "benchmark.h"
+
 /* Const values */
 const char default_home_dir[] = DEFAULT_HOME_DIR;
 const char default_tracing_group[] = LTTNG_DEFAULT_TRACING_GROUP;
@@ -230,6 +233,14 @@ static void cleanup(void)
 
        DBG("Unloading kernel modules");
        modprobe_remove_kernel_modules();
+
+       /* OUTPUT BENCHMARK RESULTS */
+       bench_init();
+
+       bench_print_boot_process();
+
+       bench_close();
+       /* END BENCHMARK */
 }
 
 /*
@@ -615,6 +626,8 @@ static void *thread_manage_kernel(void *data)
        char tmp;
        int update_poll_flag = 1;
 
+       tracepoint(sessiond_th_kern_start);
+
        DBG("Thread manage kernel started");
 
        while (1) {
@@ -628,6 +641,8 @@ static void *thread_manage_kernel(void *data)
 
                DBG("Polling on %d fds", nb_fd);
 
+               tracepoint(sessiond_th_kern_poll);
+
                /* Poll infinite value of time */
                ret = poll(kernel_pollfd, nb_fd, -1);
                if (ret < 0) {
@@ -696,6 +711,8 @@ static void *thread_manage_kconsumerd(void *data)
        enum lttcomm_return_code code;
        struct pollfd pollfd[2];
 
+       tracepoint(sessiond_th_kcon_start);
+
        DBG("[thread] Manage kconsumerd started");
 
        ret = lttcomm_listen_unix_sock(kconsumerd_err_sock);
@@ -710,6 +727,8 @@ static void *thread_manage_kconsumerd(void *data)
        pollfd[1].fd = kconsumerd_err_sock;
        pollfd[1].events = POLLIN;
 
+       tracepoint(sessiond_th_kcon_poll);
+
        /* Inifinite blocking call, waiting for transmission */
        ret = poll(pollfd, 2, -1);
        if (ret < 0) {
@@ -752,6 +771,25 @@ static void *thread_manage_kconsumerd(void *data)
                goto error;
        }
 
+       /* Kconsumerd err socket */
+       pollfd[1].fd = sock;
+       pollfd[1].events = POLLIN;
+
+       /* Inifinite blocking call, waiting for transmission */
+       ret = poll(pollfd, 2, -1);
+       if (ret < 0) {
+               perror("poll kconsumerd thread");
+               goto error;
+       }
+
+       /* Thread quit pipe has been closed. Killing thread. */
+       if (pollfd[0].revents == POLLNVAL) {
+               goto error;
+       } else if (pollfd[1].revents == POLLERR) {
+               ERR("Kconsumerd err socket second poll error");
+               goto error;
+       }
+
        /* Wait for any kconsumerd error */
        ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
        if (ret <= 0) {
@@ -788,6 +826,8 @@ static void *thread_manage_apps(void *data)
        int sock = 0, ret;
        struct pollfd pollfd[2];
 
+       tracepoint(sessiond_th_apps_start);
+
        /* TODO: Something more elegant is needed but fine for now */
        /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
         * for 32-bit vs 64-bit compat processes. */
@@ -818,6 +858,8 @@ static void *thread_manage_apps(void *data)
        while (1) {
                DBG("Accepting application registration");
 
+               tracepoint(sessiond_th_apps_poll);
+
                /* Inifinite blocking call, waiting for transmission */
                ret = poll(pollfd, 2, -1);
                if (ret < 0) {
@@ -839,10 +881,10 @@ static void *thread_manage_apps(void *data)
                }
 
                /*
-                * Basic recv here to handle the very simple data
-                * that the libust send to register (reg_msg).
+                * Using message-based transmissions to ensure we don't
+                * have to deal with partially received messages.
                 */
-               ret = recv(sock, &reg_msg, sizeof(reg_msg), 0);
+               ret = lttcomm_recv_unix_sock(sock, &reg_msg, sizeof(reg_msg));
                if (ret < 0) {
                        perror("recv");
                        continue;
@@ -851,6 +893,11 @@ static void *thread_manage_apps(void *data)
                /* Add application to the global traceable list */
                if (reg_msg.reg == 1) {
                        /* Registering */
+                       /*
+                        * TODO: socket should be either passed to a
+                        * listener thread (for more messages) or
+                        * closed. It currently leaks.
+                        */
                        ret = register_traceable_app(reg_msg.pid, reg_msg.uid);
                        if (ret < 0) {
                                /* register_traceable_app only return an error with
@@ -1240,7 +1287,8 @@ static int notify_kernel_pollfd(void)
 /*
  * Allocate a channel structure and fill it.
  */
-static struct lttng_channel *init_default_channel(char *name)
+static struct lttng_channel *init_default_channel(enum lttng_domain_type domain_type,
+                                               char *name)
 {
        struct lttng_channel *chan;
 
@@ -1252,18 +1300,29 @@ static struct lttng_channel *init_default_channel(char *name)
 
        if (snprintf(chan->name, NAME_MAX, "%s", name) < 0) {
                perror("snprintf channel name");
-               return NULL;
+               goto error;
        }
 
        chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
-       chan->attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE;
-       chan->attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM;
        chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
        chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
-       chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
 
-error:
+       switch (domain_type) {
+       case LTTNG_DOMAIN_KERNEL:
+               chan->attr.subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
+               chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
+               chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
+               break;
+               /* TODO: add UST */
+       default:
+               goto error;     /* Not implemented */
+       }
+
        return chan;
+
+error:
+       free(chan);
+       return NULL;
 }
 
 /*
@@ -1712,26 +1771,32 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
 
                switch (cmd_ctx->lsm->domain.type) {
                case LTTNG_DOMAIN_KERNEL:
-                       do {
+                       kchan = get_kernel_channel_by_name(channel_name,
+                                       cmd_ctx->session->kernel_session);
+                       if (kchan == NULL) {
+                               DBG("Channel not found. Creating channel %s", channel_name);
+
+                               chan = init_default_channel(cmd_ctx->lsm->domain.type, channel_name);
+                               if (chan == NULL) {
+                                       ret = LTTCOMM_FATAL;
+                                       goto error;
+                               }
+
+                               ret = kernel_create_channel(cmd_ctx->session->kernel_session,
+                                               chan, cmd_ctx->session->kernel_session->trace_path);
+                               if (ret < 0) {
+                                       ret = LTTCOMM_KERN_CHAN_FAIL;
+                                       goto error;
+                               }
                                kchan = get_kernel_channel_by_name(channel_name,
                                                cmd_ctx->session->kernel_session);
                                if (kchan == NULL) {
-                                       DBG("Channel not found. Creating channel %s", channel_name);
-
-                                       chan = init_default_channel(channel_name);
-                                       if (chan == NULL) {
-                                               ret = LTTCOMM_FATAL;
-                                               goto error;
-                                       }
-
-                                       ret = kernel_create_channel(cmd_ctx->session->kernel_session,
-                                                       chan, cmd_ctx->session->kernel_session->trace_path);
-                                       if (ret < 0) {
-                                               ret = LTTCOMM_KERN_CHAN_FAIL;
-                                               goto error;
-                                       }
+                                       ERR("Channel %s not found after creation. Internal error, giving up.",
+                                               channel_name);
+                                       ret = LTTCOMM_FATAL;
+                                       goto error;
                                }
-                       } while (kchan == NULL);
+                       }
 
                        kevent = get_kernel_event_by_name(cmd_ctx->lsm->u.enable.event.name, kchan);
                        if (kevent == NULL) {
@@ -1784,26 +1849,32 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
 
                switch (cmd_ctx->lsm->domain.type) {
                case LTTNG_DOMAIN_KERNEL:
-                       do {
+                       kchan = get_kernel_channel_by_name(channel_name,
+                                       cmd_ctx->session->kernel_session);
+                       if (kchan == NULL) {
+                               DBG("Channel not found. Creating channel %s", channel_name);
+
+                               chan = init_default_channel(cmd_ctx->lsm->domain.type, channel_name);
+                               if (chan == NULL) {
+                                       ret = LTTCOMM_FATAL;
+                                       goto error;
+                               }
+
+                               ret = kernel_create_channel(cmd_ctx->session->kernel_session,
+                                               chan, cmd_ctx->session->kernel_session->trace_path);
+                               if (ret < 0) {
+                                       ret = LTTCOMM_KERN_CHAN_FAIL;
+                                       goto error;
+                               }
                                kchan = get_kernel_channel_by_name(channel_name,
                                                cmd_ctx->session->kernel_session);
                                if (kchan == NULL) {
-                                       DBG("Channel not found. Creating channel %s", channel_name);
-
-                                       chan = init_default_channel(channel_name);
-                                       if (chan == NULL) {
-                                               ret = LTTCOMM_FATAL;
-                                               goto error;
-                                       }
-
-                                       ret = kernel_create_channel(cmd_ctx->session->kernel_session,
-                                                       chan, cmd_ctx->session->kernel_session->trace_path);
-                                       if (ret < 0) {
-                                               ret = LTTCOMM_KERN_CHAN_FAIL;
-                                               goto error;
-                                       }
+                                       ERR("Channel %s not found after creation. Internal error, giving up.",
+                                               channel_name);
+                                       ret = LTTCOMM_FATAL;
+                                       goto error;
                                }
-                       } while (kchan == NULL);
+                       }
 
                        /* For each event in the kernel session */
                        cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
@@ -1936,16 +2007,16 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                                }
                        }
 
-                       DBG("Start kernel tracing");
-                       ret = kernel_start_session(cmd_ctx->session->kernel_session);
+                       ret = start_kernel_trace(cmd_ctx->session->kernel_session);
                        if (ret < 0) {
-                               ERR("Kernel start session failed");
                                ret = LTTCOMM_KERN_START_FAIL;
                                goto error;
                        }
 
-                       ret = start_kernel_trace(cmd_ctx->session->kernel_session);
+                       DBG("Start kernel tracing");
+                       ret = kernel_start_session(cmd_ctx->session->kernel_session);
                        if (ret < 0) {
+                               ERR("Kernel start session failed");
                                ret = LTTCOMM_KERN_START_FAIL;
                                goto error;
                        }
@@ -2008,7 +2079,9 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                        goto setup_error;
                }
 
+               tracepoint(create_session_start);
                ret = create_session(cmd_ctx->lsm->session.name, cmd_ctx->lsm->session.path);
+               tracepoint(create_session_end);
                if (ret < 0) {
                        if (ret == -EEXIST) {
                                ret = LTTCOMM_EXIST_SESS;
@@ -2032,7 +2105,9 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                /* Clean kernel session teardown */
                teardown_kernel_session(cmd_ctx->session);
 
+               tracepoint(destroy_session_start);
                ret = destroy_session(cmd_ctx->lsm->session.name);
+               tracepoint(destroy_session_end);
                if (ret < 0) {
                        ret = LTTCOMM_FATAL;
                        goto error;
@@ -2268,6 +2343,8 @@ static void *thread_manage_clients(void *data)
        struct command_ctx *cmd_ctx = NULL;
        struct pollfd pollfd[2];
 
+       tracepoint(sessiond_th_cli_start);
+
        DBG("[thread] Manage client started");
 
        ret = lttcomm_listen_unix_sock(client_sock);
@@ -2292,6 +2369,8 @@ static void *thread_manage_clients(void *data)
        while (1) {
                DBG("Accepting client command ...");
 
+               tracepoint(sessiond_th_cli_poll);
+
                /* Inifinite blocking call, waiting for transmission */
                ret = poll(pollfd, 2, -1);
                if (ret < 0) {
@@ -2761,6 +2840,8 @@ int main(int argc, char **argv)
        void *status;
        const char *home_path;
 
+       tracepoint(sessiond_boot_start);
+
        /* Create thread quit pipe */
        if ((ret = init_thread_quit_pipe()) < 0) {
                goto error;
@@ -2907,6 +2988,8 @@ int main(int argc, char **argv)
                goto exit_kernel;
        }
 
+       tracepoint(sessiond_boot_end);
+
        ret = pthread_join(kernel_thread, &status);
        if (ret != 0) {
                perror("pthread_join");
This page took 0.029025 seconds and 4 git commands to generate.