Fix a minor ugliness
[ust.git] / libust / tracectl.c
index 5f942cd8306a3cdb661c73bb45484782bb6a245d..e877e6289f7ef0e32ca88c6ab595f7117a81298b 100644 (file)
@@ -39,6 +39,7 @@
 #include <ust/marker.h>
 #include <ust/tracepoint.h>
 #include <ust/tracectl.h>
+#include <ust/clock.h>
 #include "tracer.h"
 #include "usterr.h"
 #include "ustcomm.h"
@@ -66,13 +67,15 @@ static struct ustcomm_sock *listen_sock;
 
 extern struct chan_info_struct chan_infos[];
 
-static struct list_head open_buffers_list = LIST_HEAD_INIT(open_buffers_list);
+static struct cds_list_head open_buffers_list = CDS_LIST_HEAD_INIT(open_buffers_list);
 
-static struct list_head ust_socks = LIST_HEAD_INIT(ust_socks);
+static struct cds_list_head ust_socks = CDS_LIST_HEAD_INIT(ust_socks);
 
 /* volatile because shared between the listener and the main thread */
 int buffers_to_export = 0;
 
+int ust_clock_source;
+
 static long long make_pidunique(void)
 {
        s64 retval;
@@ -122,10 +125,10 @@ static void print_trace_events(FILE *fp)
        unlock_trace_events();
 }
 
-static int connect_ustd(void)
+static int connect_ustconsumer(void)
 {
        int result, fd;
-       char default_daemon_path[] = SOCK_DIR "/ustd";
+       char default_daemon_path[] = SOCK_DIR "/ustconsumer";
        char *explicit_daemon_path, *daemon_path;
 
        explicit_daemon_path = getenv("UST_DAEMON_SOCKET");
@@ -139,7 +142,7 @@ static int connect_ustd(void)
 
        result = ustcomm_connect_path(daemon_path, &fd);
        if (result < 0) {
-               WARN("connect_ustd failed, daemon_path: %s",
+               WARN("connect_ustconsumer failed, daemon_path: %s",
                     daemon_path);
                return result;
        }
@@ -149,8 +152,9 @@ static int connect_ustd(void)
 
 
 static void request_buffer_consumer(int sock,
-                                  const char *channel,
-                                  int cpu)
+                                   const char *trace,
+                                   const char *channel,
+                                   int cpu)
 {
        struct ustcomm_header send_header, recv_header;
        struct ustcomm_buffer_info buf_inf;
@@ -158,6 +162,7 @@ static void request_buffer_consumer(int sock,
 
        result = ustcomm_pack_buffer_info(&send_header,
                                          &buf_inf,
+                                         trace,
                                          channel,
                                          cpu);
 
@@ -192,12 +197,12 @@ static void inform_consumer_daemon(const char *trace_name)
        struct ust_trace *trace;
        const char *ch_name;
 
-       sock = connect_ustd();
+       sock = connect_ustconsumer();
        if (sock < 0) {
                return;
        }
 
-       DBG("Connected to ustd");
+       DBG("Connected to ustconsumer");
 
        ltt_lock_traces();
 
@@ -212,9 +217,10 @@ static void inform_consumer_daemon(const char *trace_name)
                        /* iterate on all cpus */
                        for (j=0; j<trace->channels[i].n_cpus; j++) {
                                ch_name = trace->channels[i].channel_name;
-                               request_buffer_consumer(sock, ch_name, j);
-                               STORE_SHARED(buffers_to_export,
-                                            LOAD_SHARED(buffers_to_export)+1);
+                               request_buffer_consumer(sock, trace_name,
+                                                       ch_name, j);
+                               CMM_STORE_SHARED(buffers_to_export,
+                                            CMM_LOAD_SHARED(buffers_to_export)+1);
                        }
                }
        }
@@ -470,13 +476,13 @@ static int notify_buffer_mapped(const char *trace_name,
         */
        if (uatomic_read(&buf->consumed) == 0) {
                DBG("decrementing buffers_to_export");
-               STORE_SHARED(buffers_to_export, LOAD_SHARED(buffers_to_export)-1);
+               CMM_STORE_SHARED(buffers_to_export, CMM_LOAD_SHARED(buffers_to_export)-1);
        }
 
        /* The buffer has been exported, ergo, we can add it to the
         * list of open buffers
         */
-       list_add(&buf->open_buffers_list, &open_buffers_list);
+       cds_list_add(&buf->open_buffers_list, &open_buffers_list);
 
 unlock_traces:
        ltt_unlock_traces();
@@ -536,7 +542,7 @@ static void force_subbuf_switch()
 {
        struct ust_buffer *buf;
 
-       list_for_each_entry(buf, &open_buffers_list,
+       cds_list_for_each_entry(buf, &open_buffers_list,
                            open_buffers_list) {
                ltt_force_switch(buf, FORCE_FLUSH);
        }
@@ -546,24 +552,39 @@ static void force_subbuf_switch()
 static int process_simple_client_cmd(int command, char *recv_buf)
 {
        int result;
-       char trace_type[] = "ustrelay";
-       char trace_name[] = "auto";
 
        switch(command) {
        case SET_SOCK_PATH:
        {
-               struct ustcomm_sock_path *sock_msg;
-               sock_msg = (struct ustcomm_sock_path *)recv_buf;
-               sock_msg->sock_path =
-                       ustcomm_restore_ptr(sock_msg->sock_path,
-                                           sock_msg->data,
-                                           sizeof(sock_msg->data));
-               if (!sock_msg->sock_path) {
-
-                       return -EINVAL;
+               struct ustcomm_single_field *sock_msg;
+               sock_msg = (struct ustcomm_single_field *)recv_buf;
+               result = ustcomm_unpack_single_field(sock_msg);
+               if (result < 0) {
+                       return result;
                }
-               return setenv("UST_DAEMON_SOCKET", sock_msg->sock_path, 1);
+               return setenv("UST_DAEMON_SOCKET", sock_msg->field, 1);
+       }
+
+       case FORCE_SUBBUF_SWITCH:
+               /* FIXME: return codes? */
+               force_subbuf_switch();
+
+               break;
+
+       default:
+               return -EINVAL;
        }
+
+       return 0;
+}
+
+
+static int process_trace_cmd(int command, char *trace_name)
+{
+       int result;
+       char trace_type[] = "ustrelay";
+
+       switch(command) {
        case START:
                /* start is an operation that setups the trace, allocates it and starts it */
                result = ltt_trace_setup(trace_name);
@@ -675,19 +696,12 @@ static int process_simple_client_cmd(int command, char *recv_buf)
                        return result;
                }
                return 0;
-       case FORCE_SUBBUF_SWITCH:
-               /* FIXME: return codes? */
-               force_subbuf_switch();
-
-               break;
-
-       default:
-               return -EINVAL;
        }
 
        return 0;
 }
 
+
 static void process_channel_cmd(int sock, int command,
                                struct ustcomm_channel_info *ch_inf)
 {
@@ -695,14 +709,13 @@ static void process_channel_cmd(int sock, int command,
        struct ustcomm_header *reply_header = &_reply_header;
        struct ustcomm_channel_info *reply_msg =
                (struct ustcomm_channel_info *)send_buffer;
-       char trace_name[] = "auto";
        int result, offset = 0, num, size;
 
        memset(reply_header, 0, sizeof(*reply_header));
 
        switch (command) {
        case GET_SUBBUF_NUM_SIZE:
-               result = get_subbuf_num_size(trace_name,
+               result = get_subbuf_num_size(ch_inf->trace,
                                             ch_inf->channel,
                                             &num, &size);
                if (result < 0) {
@@ -719,13 +732,13 @@ static void process_channel_cmd(int sock, int command,
 
                break;
        case SET_SUBBUF_NUM:
-               reply_header->result = set_subbuf_num(trace_name,
+               reply_header->result = set_subbuf_num(ch_inf->trace,
                                                      ch_inf->channel,
                                                      ch_inf->subbuf_num);
 
                break;
        case SET_SUBBUF_SIZE:
-               reply_header->result = set_subbuf_size(trace_name,
+               reply_header->result = set_subbuf_size(ch_inf->trace,
                                                       ch_inf->channel,
                                                       ch_inf->subbuf_size);
 
@@ -744,7 +757,6 @@ static void process_buffer_cmd(int sock, int command,
        struct ustcomm_header *reply_header = &_reply_header;
        struct ustcomm_buffer_info *reply_msg =
                (struct ustcomm_buffer_info *)send_buffer;
-       char trace_name[] = "auto";
        int result, offset = 0, buf_shmid, buf_struct_shmid, buf_pipe_fd;
        long consumed_old;
 
@@ -752,7 +764,8 @@ static void process_buffer_cmd(int sock, int command,
 
        switch (command) {
        case GET_BUF_SHMID_PIPE_FD:
-               result = get_buffer_shmid_pipe_fd(trace_name, buf_inf->channel,
+               result = get_buffer_shmid_pipe_fd(buf_inf->trace,
+                                                 buf_inf->channel,
                                                  buf_inf->ch_cpu,
                                                  &buf_shmid,
                                                  &buf_struct_shmid,
@@ -777,12 +790,12 @@ static void process_buffer_cmd(int sock, int command,
 
        case NOTIFY_BUF_MAPPED:
                reply_header->result =
-                       notify_buffer_mapped(trace_name,
+                       notify_buffer_mapped(buf_inf->trace,
                                             buf_inf->channel,
                                             buf_inf->ch_cpu);
                break;
        case GET_SUBBUFFER:
-               result = get_subbuffer(trace_name, buf_inf->channel,
+               result = get_subbuffer(buf_inf->trace, buf_inf->channel,
                                       buf_inf->ch_cpu, &consumed_old);
                if (result < 0) {
                        reply_header->result = result;
@@ -796,7 +809,7 @@ static void process_buffer_cmd(int sock, int command,
 
                break;
        case PUT_SUBBUFFER:
-               result = put_subbuffer(trace_name, buf_inf->channel,
+               result = put_subbuffer(buf_inf->trace, buf_inf->channel,
                                       buf_inf->ch_cpu,
                                       buf_inf->consumed_old);
                reply_header->result = result;
@@ -987,27 +1000,51 @@ static void process_client_cmd(struct ustcomm_header *recv_header,
        }
        case GET_SOCK_PATH:
        {
-               struct ustcomm_sock_path *sock_msg;
+               struct ustcomm_single_field *sock_msg;
                char *sock_path_env;
 
-               sock_msg = (struct ustcomm_sock_path *)send_buf;
+               sock_msg = (struct ustcomm_single_field *)send_buf;
 
                sock_path_env = getenv("UST_DAEMON_SOCKET");
 
                if (!sock_path_env) {
-                       result = ustcomm_pack_sock_path(reply_header,
-                                                       sock_msg,
-                                                       SOCK_DIR "/ustd");
+                       result = ustcomm_pack_single_field(reply_header,
+                                                          sock_msg,
+                                                          SOCK_DIR "/ustconsumer");
 
                } else {
-                       result = ustcomm_pack_sock_path(reply_header,
-                                                       sock_msg,
-                                                       sock_path_env);
+                       result = ustcomm_pack_single_field(reply_header,
+                                                          sock_msg,
+                                                          sock_path_env);
                }
                reply_header->result = result;
 
                goto send_response;
        }
+       case START:
+       case SETUP_TRACE:
+       case ALLOC_TRACE:
+       case CREATE_TRACE:
+       case START_TRACE:
+       case STOP_TRACE:
+       case DESTROY_TRACE:
+       {
+               struct ustcomm_single_field *trace_inf =
+                       (struct ustcomm_single_field *)recv_buf;
+
+               result = ustcomm_unpack_single_field(trace_inf);
+               if (result < 0) {
+                       ERR("couldn't unpack trace info");
+                       reply_header->result = -EINVAL;
+                       goto send_response;
+               }
+
+               reply_header->result =
+                       process_trace_cmd(recv_header->command,
+                                         trace_inf->field);
+               goto send_response;
+
+       }
        default:
                reply_header->result =
                        process_simple_client_cmd(recv_header->command,
@@ -1195,6 +1232,7 @@ free_name:
 
 static void __attribute__((constructor)) init()
 {
+       struct timespec ts;
        int result;
        char* autoprobe_val = NULL;
        char* subbuffer_size_val = NULL;
@@ -1228,6 +1266,15 @@ static void __attribute__((constructor)) init()
 
        create_listener();
 
+       /* Get clock the clock source type */
+
+       /* Default clock source */
+       ust_clock_source = CLOCK_TRACE;
+       if (clock_gettime(ust_clock_source, &ts) != 0) {
+               ust_clock_source = CLOCK_MONOTONIC;
+               DBG("UST traces will not be synchronized with LTTng traces");
+       }
+
        autoprobe_val = getenv("UST_AUTOPROBE");
        if (autoprobe_val) {
                struct marker_iter iter;
@@ -1276,7 +1323,7 @@ static void __attribute__((constructor)) init()
        if (getenv("UST_OVERWRITE")) {
                int val = atoi(getenv("UST_OVERWRITE"));
                if (val == 0 || val == 1) {
-                       STORE_SHARED(ust_channels_overwrite_by_default, val);
+                       CMM_STORE_SHARED(ust_channels_overwrite_by_default, val);
                } else {
                        WARN("invalid value for UST_OVERWRITE");
                }
@@ -1285,7 +1332,7 @@ static void __attribute__((constructor)) init()
        if (getenv("UST_AUTOCOLLECT")) {
                int val = atoi(getenv("UST_AUTOCOLLECT"));
                if (val == 0 || val == 1) {
-                       STORE_SHARED(ust_channels_request_collection_by_default, val);
+                       CMM_STORE_SHARED(ust_channels_request_collection_by_default, val);
                } else {
                        WARN("invalid value for UST_AUTOCOLLECT");
                }
@@ -1417,7 +1464,7 @@ static int trace_recording(void)
 
        ltt_lock_traces();
 
-       list_for_each_entry(trace, &ltt_traces.head, list) {
+       cds_list_for_each_entry(trace, &ltt_traces.head, list) {
                if (trace->active) {
                        retval = 1;
                        break;
@@ -1462,7 +1509,7 @@ static void stop_listener(void)
 }
 
 /* This destructor keeps the process alive for a few seconds in order
- * to leave time to ustd to connect to its buffers. This is necessary
+ * to leave time for ustconsumer to connect to its buffers. This is necessary
  * for programs whose execution is very short. It is also useful in all
  * programs when tracing is started close to the end of the program
  * execution.
@@ -1477,10 +1524,10 @@ static void __attribute__((destructor)) keepalive()
                return;
        }
 
-       if (trace_recording() && LOAD_SHARED(buffers_to_export)) {
+       if (trace_recording() && CMM_LOAD_SHARED(buffers_to_export)) {
                int total = 0;
                DBG("Keeping process alive for consumer daemon...");
-               while (LOAD_SHARED(buffers_to_export)) {
+               while (CMM_LOAD_SHARED(buffers_to_export)) {
                        const int interv = 200000;
                        restarting_usleep(interv);
                        total += interv;
@@ -1536,12 +1583,12 @@ static void ust_fork(void)
        ltt_trace_stop("auto");
        ltt_trace_destroy("auto", 1);
        /* Delete all active connections, but leave them in the epoll set */
-       list_for_each_entry_safe(sock, sock_tmp, &ust_socks, list) {
+       cds_list_for_each_entry_safe(sock, sock_tmp, &ust_socks, list) {
                ustcomm_del_sock(sock, 1);
        }
 
        /* Delete all blocked consumers */
-       list_for_each_entry_safe(buf, buf_tmp, &open_buffers_list,
+       cds_list_for_each_entry_safe(buf, buf_tmp, &open_buffers_list,
                                 open_buffers_list) {
                result = close(buf->data_ready_fd_read);
                if (result == -1) {
@@ -1551,7 +1598,7 @@ static void ust_fork(void)
                if (result == -1) {
                        PERROR("close");
                }
-               list_del(&buf->open_buffers_list);
+               cds_list_del(&buf->open_buffers_list);
        }
 
        /* Clean up the listener socket and epoll, keeping the scoket file */
@@ -1559,7 +1606,7 @@ static void ust_fork(void)
        close(epoll_fd);
 
        /* Re-start the launch sequence */
-       STORE_SHARED(buffers_to_export, 0);
+       CMM_STORE_SHARED(buffers_to_export, 0);
        have_listener = 0;
 
        /* Set up epoll */
This page took 0.028591 seconds and 4 git commands to generate.