Get perf counter list when opening the trace
[lttngtop.git] / src / lttngtop.c
index ad1e7137c5ab5f9a1c7d46de7b8b02622dadc2b0..2ee303a318dccd6028b9994193e3174a1eec377e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011 Julien Desfossez
+ * Copyright (C) 2011-2012 Julien Desfossez
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License Version 2 as
  * 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., 59 Temple Place - Suite 330, Boston,
- * MA 02111-1307, USA.
+ * 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
@@ -23,6 +22,7 @@
 #include <babeltrace/babeltrace.h>
 #include <babeltrace/ctf/events.h>
 #include <babeltrace/ctf/callbacks.h>
+#include <babeltrace/ctf/iterator.h>
 #include <fcntl.h>
 #include <pthread.h>
 #include <popt.h>
@@ -36,6 +36,7 @@
 #include <errno.h>
 #include <sys/types.h>
 #include <fts.h>
+#include <assert.h>
 
 #include "lttngtoptypes.h"
 #include "cputop.h"
@@ -73,6 +74,8 @@ static struct poptOption long_options[] = {
 void *refresh_thread(void *p)
 {
        while (1) {
+               if (quit)
+                       return NULL;
                sem_wait(&pause_sem);
                sem_post(&pause_sem);
                sem_post(&timer);
@@ -85,6 +88,10 @@ void *ncurses_display(void *p)
        unsigned int current_display_index = 0;
 
        sem_wait(&bootstrap);
+       /*
+        * Prevent the 1 second delay when we hit ESC
+        */
+       ESCDELAY = 0;
        init_ncurses();
 
        while (1) {
@@ -93,8 +100,8 @@ void *ncurses_display(void *p)
                sem_wait(&pause_sem);
 
                copy = g_ptr_array_index(copies, current_display_index);
-               if (copy)
-                       display(current_display_index++);
+               assert(copy);
+               display(current_display_index++);
 
                sem_post(&goodtoupdate);
                sem_post(&pause_sem);
@@ -143,7 +150,7 @@ error:
 struct perfcounter *get_perf_counter(const char *name, struct processtop *proc,
                struct cputime *cpu)
 {
-       struct perfcounter *ret, *global;
+       struct perfcounter *ret;
        GHashTable *table;
 
        if (proc)
@@ -160,17 +167,7 @@ struct perfcounter *get_perf_counter(const char *name, struct processtop *proc,
        ret = g_new0(struct perfcounter, 1);
        /* by default, make it visible in the UI */
        ret->visible = 1;
-       g_hash_table_insert(table, (gpointer) name, ret);
-
-       global = g_hash_table_lookup(lttngtop.perf_list, (gpointer) name);
-       if (!global) {
-               global = g_new0(struct perfcounter, 1);
-               memcpy(global, ret, sizeof(struct perfcounter));
-               /* by default, sort on the first perf context */
-               if (g_hash_table_size(lttngtop.perf_list) == 0)
-                       global->sort = 1;
-               g_hash_table_insert(lttngtop.perf_list, (gpointer) name, global);
-       }
+       g_hash_table_insert(table, (gpointer) strdup(name), ret);
 
 end:
        return ret;
@@ -192,14 +189,18 @@ void update_perf_value(struct processtop *proc, struct cputime *cpu,
        }
 }
 
-void extract_perf_counter_scope(struct bt_ctf_event *event,
-               struct definition *scope,
+void extract_perf_counter_scope(const struct bt_ctf_event *event,
+               const struct definition *scope,
                struct processtop *proc,
                struct cputime *cpu)
 {
        struct definition const * const *list = NULL;
+       const struct definition *field;
        unsigned int count;
-       int i, ret;
+       struct perfcounter *perfcounter;
+       GHashTableIter iter;
+       gpointer key;
+       int ret;
 
        if (!scope)
                goto end;
@@ -208,13 +209,17 @@ void extract_perf_counter_scope(struct bt_ctf_event *event,
        if (ret < 0)
                goto end;
 
-       for (i = 0; i < count; i++) {
-               const char *name = bt_ctf_field_name(list[i]);
-               if (strncmp(name, "_perf_", 6) == 0) {
-                       int value = bt_ctf_get_uint64(list[i]);
+       if (count == 0)
+               goto end;
+
+       g_hash_table_iter_init(&iter, global_perf_liszt);
+       while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfcounter)) {
+               field = bt_ctf_get_field(event, scope, (char *) key);
+               if (field) {
+                       int value = bt_ctf_get_uint64(field);
                        if (bt_ctf_field_get_error())
                                continue;
-                       update_perf_value(proc, cpu, name, value);
+                       update_perf_value(proc, cpu, (char *) key, value);
                }
        }
 
@@ -222,19 +227,12 @@ end:
        return;
 }
 
-void update_perf_counter(struct processtop *proc, struct bt_ctf_event *event)
+void update_perf_counter(struct processtop *proc, const struct bt_ctf_event *event)
 {
-       struct definition *scope;
-       uint64_t cpu_id;
        struct cputime *cpu;
+       const struct definition *scope;
 
-       scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT);
-       cpu_id = bt_ctf_get_uint64(bt_ctf_get_field(event, scope, "cpu_id"));
-       if (bt_ctf_field_get_error()) {
-               fprintf(stderr, "[error] get cpu_id\n");
-               goto end;
-       }
-       cpu = get_cpu(cpu_id);
+       cpu = get_cpu(get_cpu_id(event));
 
        scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
        extract_perf_counter_scope(event, scope, proc, cpu);
@@ -244,9 +242,6 @@ void update_perf_counter(struct processtop *proc, struct bt_ctf_event *event)
 
        scope = bt_ctf_get_top_level_scope(event, BT_EVENT_CONTEXT);
        extract_perf_counter_scope(event, scope, proc, cpu);
-
-end:
-       return;
 }
 
 enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data,
@@ -255,35 +250,26 @@ enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data,
        int pid, tid, ppid;
        char *comm;
        struct processtop *parent, *child;
-       struct definition *scope;
        unsigned long timestamp;
 
-       /* FIXME : check context pid, tid, ppid and comm */
-
        timestamp = bt_ctf_get_timestamp(call_data);
        if (timestamp == -1ULL)
                goto error;
 
-       scope = bt_ctf_get_top_level_scope(call_data, BT_STREAM_EVENT_CONTEXT);
-
-       pid = bt_ctf_get_int64(bt_ctf_get_field(call_data, scope, "_pid"));
-       if (bt_ctf_field_get_error()) {
-//             fprintf(stderr, "Missing pid context info\n");
+       pid = get_context_pid(call_data);
+       if (pid == -1ULL) {
                goto error;
        }
-       tid = bt_ctf_get_int64(bt_ctf_get_field(call_data, scope, "_tid"));
-       if (bt_ctf_field_get_error()) {
-//             fprintf(stderr, "Missing tid context info\n");
+       tid = get_context_tid(call_data);
+       if (tid == -1ULL) {
                goto error;
        }
-       ppid = bt_ctf_get_int64(bt_ctf_get_field(call_data, scope, "_ppid"));
-       if (bt_ctf_field_get_error()) {
-//             fprintf(stderr, "Missing ppid context info\n");
+       ppid = get_context_ppid(call_data);
+       if (ppid == -1ULL) {
                goto error;
        }
-       comm = bt_ctf_get_char_array(bt_ctf_get_field(call_data, scope, "_procname"));
-       if (bt_ctf_field_get_error()) {
-//             fprintf(stderr, "Missing procname context info\n");
+       comm = get_context_comm(call_data);
+       if (!comm) {
                goto error;
        }
 
@@ -317,7 +303,7 @@ error:
 void init_lttngtop()
 {
        copies = g_ptr_array_new();
-       lttngtop.perf_list = g_hash_table_new(g_direct_hash, g_direct_equal);
+       global_perf_liszt = g_hash_table_new(g_str_hash, g_str_equal);
 
        sem_init(&goodtodisplay, 0, 0);
        sem_init(&goodtoupdate, 0, 1);
@@ -326,14 +312,20 @@ void init_lttngtop()
        sem_init(&pause_sem, 0, 1);
        sem_init(&end_trace_sem, 0, 0);
 
+       reset_global_counters();
+       lttngtop.nbproc = 0;
+       lttngtop.nbthreads = 0;
+       lttngtop.nbfiles = 0;
+
        lttngtop.process_table = g_ptr_array_new();
        lttngtop.files_table = g_ptr_array_new();
        lttngtop.cpu_table = g_ptr_array_new();
 }
 
-void usage(FILE *fd)
+void usage(FILE *fp)
 {
-
+       fprintf(fp, "LTTngTop %s\n\n", VERSION);
+       fprintf(fp, "Usage : lttngtop /path/to/trace\n");
 }
 
 /*
@@ -359,19 +351,6 @@ static int parse_options(int argc, char **argv)
                                usage(stdout);
                                ret = 1;    /* exit cleanly */
                                goto end;
-                       case OPT_LIST:
-                       //      list_formats(stdout);
-                               ret = 1;
-                               goto end;
-                       case OPT_VERBOSE:
-//                             babeltrace_verbose = 1;
-                               break;
-                       case OPT_DEBUG:
-//                             babeltrace_debug = 1;
-                               break;
-                       case OPT_NAMES:
-//                             opt_field_names = 1;
-                               break;
                        default:
                                ret = -EINVAL;
                                goto end;
@@ -394,7 +373,7 @@ void iter_trace(struct bt_context *bt_ctx)
 {
        struct bt_ctf_iter *iter;
        struct bt_iter_pos begin_pos;
-       struct bt_ctf_event *event;
+       const struct bt_ctf_event *event;
        int ret = 0;
 
        begin_pos.type = BT_SEEK_BEGIN;
@@ -416,6 +395,12 @@ void iter_trace(struct bt_context *bt_ctx)
        bt_ctf_iter_add_callback(iter,
                        g_quark_from_static_string("sched_process_free"),
                        NULL, 0, handle_sched_process_free, NULL, NULL, NULL);
+       /* to get all the process from the statedumps */
+       bt_ctf_iter_add_callback(iter,
+                       g_quark_from_static_string(
+                               "lttng_statedump_process_state"),
+                       NULL, 0, handle_statedump_process_state,
+                       NULL, NULL, NULL);
 
        /* for IO top */
        bt_ctf_iter_add_callback(iter,
@@ -427,6 +412,18 @@ void iter_trace(struct bt_context *bt_ctx)
        bt_ctf_iter_add_callback(iter,
                        g_quark_from_static_string("sys_read"),
                        NULL, 0, handle_sys_read, NULL, NULL, NULL);
+       bt_ctf_iter_add_callback(iter,
+                       g_quark_from_static_string("sys_open"),
+                       NULL, 0, handle_sys_open, NULL, NULL, NULL);
+       bt_ctf_iter_add_callback(iter,
+                       g_quark_from_static_string("sys_close"),
+                       NULL, 0, handle_sys_close, NULL, NULL, NULL);
+       bt_ctf_iter_add_callback(iter,
+                       g_quark_from_static_string(
+                                       "lttng_statedump_file_descriptor"),
+                       NULL, 0, handle_statedump_file_descriptor,
+                       NULL, NULL, NULL);
+
        while ((event = bt_ctf_iter_read_event(iter)) != NULL) {
                ret = bt_iter_next(bt_ctf_get_iter(iter));
                if (ret < 0)
@@ -437,7 +434,7 @@ void iter_trace(struct bt_context *bt_ctx)
        sem_wait(&end_trace_sem);
 
 end_iter:
-       bt_iter_destroy(bt_ctf_get_iter(iter));
+       bt_ctf_iter_destroy(iter);
 }
 
 /*
@@ -462,7 +459,7 @@ int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
        GArray *trace_ids;
        char lpath[PATH_MAX];
        char * const paths[2] = { lpath, NULL };
-       int ret;
+       int ret = -1;
 
        /*
         * Need to copy path, because fts_open can change it.
@@ -495,11 +492,9 @@ int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
                }
                metafd = openat(dirfd, "metadata", O_RDONLY);
                if (metafd < 0) {
-                       ret = close(dirfd);
-                       if (ret < 0) {
-                               perror("close");
-                               goto error;
-                       }
+                       close(dirfd);
+                       ret = -1;
+                       continue;
                } else {
                        int trace_id;
 
@@ -518,22 +513,126 @@ int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
                                node->fts_accpath, format_str,
                                packet_seek, NULL, NULL);
                        if (trace_id < 0) {
-                               fprintf(stderr, "[error] [Context] opening trace \"%s\" from %s "
+                               fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s "
                                        "for reading.\n", node->fts_accpath, path);
-                               ret = trace_id;
-                               goto error;
+                               /* Allow to skip erroneous traces. */
+                               continue;
                        }
                        g_array_append_val(trace_ids, trace_id);
                }
        }
 
        g_array_free(trace_ids, TRUE);
-       return 0;
+       return ret;
 
 error:
        return ret;
 }
 
+static int check_field_requirements(const struct bt_ctf_field_decl *const * field_list,
+               int field_cnt, int *tid_check, int *pid_check,
+               int *procname_check, int *ppid_check)
+{
+       int j;
+       struct perfcounter *global;
+       const char *name;
+
+       for (j = 0; j < field_cnt; j++) {
+               name = bt_ctf_get_decl_field_name(field_list[j]);
+               if (*tid_check == 0) {
+                       if (strncmp(name, "tid", 3) == 0)
+                               (*tid_check)++;
+               }
+               if (*pid_check == 0) {
+                       if (strncmp(name, "tid", 3) == 0)
+                               (*pid_check)++;
+               }
+               if (*ppid_check == 0) {
+                       if (strncmp(name, "ppid", 4) == 0)
+                               (*ppid_check)++;
+               }
+               if (*procname_check == 0) {
+                       if (strncmp(name, "procname", 8) == 0)
+                               (*procname_check)++;
+               }
+               if (strncmp(name, "perf_", 5) == 0) {
+                       global = g_hash_table_lookup(global_perf_liszt, (gpointer) name);
+                       if (!global) {
+                               global = g_new0(struct perfcounter, 1);
+                               /* by default, sort on the first perf context */
+                               if (g_hash_table_size(global_perf_liszt) == 0)
+                                       global->sort = 1;
+                               global->visible = 1;
+                               g_hash_table_insert(global_perf_liszt, (gpointer) strdup(name), global);
+                       }
+               }
+       }
+
+       if (*tid_check == 1 && *pid_check == 1 && *ppid_check == 1 &&
+                       *procname_check == 1)
+               return 0;
+
+       return -1;
+}
+
+/*
+ * check_requirements: check if the required context informations are available
+ *
+ * If each mandatory context information is available for at least in one
+ * event, return 0 otherwise return -1.
+ */
+int check_requirements(struct bt_context *ctx)
+{
+       unsigned int i, evt_cnt, field_cnt;
+       struct bt_ctf_event_decl *const * evt_list;
+       const struct bt_ctf_field_decl *const * field_list;
+       int tid_check = 0;
+       int pid_check = 0;
+       int procname_check = 0;
+       int ppid_check = 0;
+       int ret = 0;
+
+       bt_ctf_get_event_decl_list(0, ctx, &evt_list, &evt_cnt);
+       for (i = 0; i < evt_cnt; i++) {
+               bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_EVENT_CONTEXT,
+                               &field_list, &field_cnt);
+               ret = check_field_requirements(field_list, field_cnt,
+                               &tid_check, &pid_check, &procname_check,
+                               &ppid_check);
+
+               bt_ctf_get_decl_fields(evt_list[i], BT_EVENT_CONTEXT,
+                               &field_list, &field_cnt);
+               ret = check_field_requirements(field_list, field_cnt,
+                               &tid_check, &pid_check, &procname_check,
+                               &ppid_check);
+
+               bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_PACKET_CONTEXT,
+                               &field_list, &field_cnt);
+               ret = check_field_requirements(field_list, field_cnt,
+                               &tid_check, &pid_check, &procname_check,
+                               &ppid_check);
+       }
+
+       if (tid_check == 0) {
+               ret = -1;
+               fprintf(stderr, "[error] missing tid context information\n");
+       }
+       if (pid_check == 0) {
+               ret = -1;
+               fprintf(stderr, "[error] missing pid context information\n");
+       }
+       if (ppid_check == 0) {
+               ret = -1;
+               fprintf(stderr, "[error] missing ppid context information\n");
+       }
+       if (procname_check == 0) {
+               ret = -1;
+               fprintf(stderr, "[error] missing procname context information\n");
+       }
+
+       return ret;
+}
+
 int main(int argc, char **argv)
 {
        int ret;
@@ -553,7 +652,13 @@ int main(int argc, char **argv)
        bt_ctx = bt_context_create();
        ret = bt_context_add_traces_recursive(bt_ctx, opt_input_path, "ctf", NULL);
        if (ret < 0) {
-               printf("[error] Opening the trace\n");
+               fprintf(stderr, "[error] Opening the trace\n");
+               goto end;
+       }
+
+       ret = check_requirements(bt_ctx);
+       if (ret < 0) {
+               fprintf(stderr, "[error] some mandatory contexts were missing, exiting.\n");
                goto end;
        }
 
@@ -564,7 +669,9 @@ int main(int argc, char **argv)
 
        quit = 1;
        pthread_join(display_thread, NULL);
+       pthread_join(timer_thread, NULL);
 
 end:
+       bt_context_put(bt_ctx);
        return 0;
 }
This page took 0.028059 seconds and 4 git commands to generate.