cleanup, live textdump working
[lttngtop.git] / src / common.c
index 0d39f303559eeafe729699e5591b54f921e7962b..bff5b00715685b312c7ad3745811776b7a473e3d 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.
  */
 
+#include <babeltrace/ctf/events.h>
 #include <stdlib.h>
+#include <linux/unistd.h>
 #include <string.h>
 #include "common.h"
 
+uint64_t get_cpu_id(const struct bt_ctf_event *event)
+{
+       const struct bt_definition *scope;
+       uint64_t cpu_id;
+
+       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");
+               return -1ULL;
+       }
+
+       return cpu_id;
+}
+
+uint64_t get_context_tid(const struct bt_ctf_event *event)
+{
+       const struct bt_definition *scope;
+       uint64_t tid;
+
+       scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
+       tid = bt_ctf_get_int64(bt_ctf_get_field(event,
+                               scope, "_tid"));
+       if (bt_ctf_field_get_error()) {
+               fprintf(stderr, "Missing tid context info\n");
+               return -1ULL;
+       }
+
+       return tid;
+}
+
+uint64_t get_context_pid(const struct bt_ctf_event *event)
+{
+       const struct bt_definition *scope;
+       uint64_t pid;
+
+       scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
+       pid = bt_ctf_get_int64(bt_ctf_get_field(event,
+                               scope, "_pid"));
+       if (bt_ctf_field_get_error()) {
+               fprintf(stderr, "Missing pid context info\n");
+               return -1ULL;
+       }
+
+       return pid;
+}
+
+uint64_t get_context_ppid(const struct bt_ctf_event *event)
+{
+       const struct bt_definition *scope;
+       uint64_t ppid;
+
+       scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
+       ppid = bt_ctf_get_int64(bt_ctf_get_field(event,
+                               scope, "_ppid"));
+       if (bt_ctf_field_get_error()) {
+               fprintf(stderr, "Missing ppid context info\n");
+               return -1ULL;
+       }
+
+       return ppid;
+}
+
+char *get_context_comm(const struct bt_ctf_event *event)
+{
+       const struct bt_definition *scope;
+       char *comm;
+
+       scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
+       comm = bt_ctf_get_char_array(bt_ctf_get_field(event,
+                               scope, "_procname"));
+       if (bt_ctf_field_get_error()) {
+               fprintf(stderr, "Missing comm context info\n");
+               return NULL;
+       }
+
+       return comm;
+}
+
+/*
+ * To get the parent process, put the pid in the tid field
+ * because the parent process gets pid = tid
+ *
+ * FIXME : char *comm useful ???
+ */
 struct processtop *find_process_tid(struct lttngtop *ctx, int tid, char *comm)
 {
        gint i;
@@ -46,14 +132,19 @@ struct processtop* add_proc(struct lttngtop *ctx, int tid, char *comm,
                newproc->tid = tid;
                newproc->birth = timestamp;
                newproc->process_files_table = g_ptr_array_new();
+               newproc->files_history = NULL;
+               newproc->totalfileread = 0;
+               newproc->totalfilewrite = 0;
+               newproc->fileread = 0;
+               newproc->filewrite = 0;
+               newproc->syscall_info = NULL;
+               newproc->threadparent = NULL;
                newproc->threads = g_ptr_array_new();
                newproc->perf = g_hash_table_new(g_str_hash, g_str_equal);
-               newproc->iostream = g_new0(struct iostream, 1);
-               newproc->iostream->ret_read = 0;
-               newproc->iostream->ret_write = 0;
-               newproc->iostream->ret_total = 0;
-               newproc->iostream->syscall_info = NULL;
                g_ptr_array_add(ctx->process_table, newproc);
+
+               ctx->nbnewthreads++;
+               ctx->nbthreads++;
        }
        newproc->comm = strdup(comm);
 
@@ -84,8 +175,11 @@ void death_proc(struct lttngtop *ctx, int tid, char *comm,
 {
        struct processtop *tmp;
        tmp = find_process_tid(ctx, tid, comm);
-       if (tmp && strcmp(tmp->comm, comm) == 0)
+       if (tmp && strcmp(tmp->comm, comm) == 0) {
                tmp->death = timestamp;
+               ctx->nbdeadthreads++;
+               ctx->nbthreads--;
+       }
 }
 
 struct processtop* get_proc(struct lttngtop *ctx, int tid, char *comm,
@@ -98,6 +192,16 @@ struct processtop* get_proc(struct lttngtop *ctx, int tid, char *comm,
        return add_proc(ctx, tid, comm, timestamp);
 }
 
+struct processtop *get_proc_pid(struct lttngtop *ctx, int tid, int pid,
+               unsigned long timestamp)
+{
+       struct processtop *tmp;
+       tmp = find_process_tid(ctx, tid, NULL);
+       if (tmp && tmp->pid == pid)
+               return tmp;
+       return add_proc(ctx, tid, "Unknown", timestamp);
+}
+
 void add_thread(struct processtop *parent, struct processtop *thread)
 {
        gint i;
@@ -171,7 +275,7 @@ void reset_perf_counter(gpointer key, gpointer value, gpointer user_data)
 void copy_perf_counter(gpointer key, gpointer value, gpointer new_table)
 {
        struct perfcounter *newperf;
-       
+
        newperf = g_new0(struct perfcounter, 1);
        newperf->count = ((struct perfcounter *) value)->count;
        newperf->visible = ((struct perfcounter *) value)->visible;
@@ -190,18 +294,56 @@ void rotate_perfcounter() {
 
 void cleanup_processtop()
 {
-       gint i;
+       gint i, j;
        struct processtop *tmp;
+       struct files *tmpf; /* a temporary file */
 
        for (i = 0; i < lttngtop.process_table->len; i++) {
                tmp = g_ptr_array_index(lttngtop.process_table, i);
                tmp->totalcpunsec = 0;
                tmp->threadstotalcpunsec = 0;
-               tmp->iostream->ret_read = 0;
-               tmp->iostream->ret_write = 0;
+               tmp->fileread = 0;
+               tmp->filewrite = 0;
+
+               for (j = 0; j < tmp->process_files_table->len; j++) {
+                       tmpf = g_ptr_array_index(tmp->process_files_table, j);
+                       if (tmpf != NULL) {
+                               tmpf->read = 0;
+                               tmpf->write = 0;
+
+                               if (tmpf->flag == __NR_close)
+                                       g_ptr_array_index(
+                                               tmp->process_files_table, j
+                                       ) = NULL;
+                       }
+               }
        }
 }
 
+void reset_global_counters()
+{
+       lttngtop.nbnewproc = 0;
+       lttngtop.nbdeadproc = 0;
+       lttngtop.nbnewthreads = 0;
+       lttngtop.nbdeadthreads = 0;
+       lttngtop.nbnewfiles = 0;
+       lttngtop.nbclosedfiles = 0;
+}
+
+void copy_global_counters(struct lttngtop *dst)
+{
+       dst->nbproc = lttngtop.nbproc;
+       dst->nbnewproc = lttngtop.nbnewproc;
+       dst->nbdeadproc = lttngtop.nbdeadproc;
+       dst->nbthreads = lttngtop.nbthreads;
+       dst->nbnewthreads = lttngtop.nbnewthreads;
+       dst->nbdeadthreads = lttngtop.nbdeadthreads;
+       dst->nbfiles = lttngtop.nbfiles;
+       dst->nbnewfiles = lttngtop.nbnewfiles;
+       dst->nbclosedfiles = lttngtop.nbclosedfiles;
+       reset_global_counters();
+}
+
 struct lttngtop* get_copy_lttngtop(unsigned long start, unsigned long end)
 {
        gint i, j;
@@ -214,14 +356,13 @@ struct lttngtop* get_copy_lttngtop(unsigned long start, unsigned long end)
        dst = g_new0(struct lttngtop, 1);
        dst->start = start;
        dst->end = end;
+       copy_global_counters(dst);
        dst->process_table = g_ptr_array_new();
        dst->files_table = g_ptr_array_new();
        dst->cpu_table = g_ptr_array_new();
-       dst->perf_list = g_hash_table_new(g_str_hash, g_str_equal);
 
        rotate_cputime(end);
 
-       g_hash_table_foreach(lttngtop.perf_list, copy_perf_counter, dst->perf_list);
        for (i = 0; i < lttngtop.process_table->len; i++) {
                tmp = g_ptr_array_index(lttngtop.process_table, i);
                new = g_new0(struct processtop, 1);
@@ -230,35 +371,39 @@ struct lttngtop* get_copy_lttngtop(unsigned long start, unsigned long end)
                new->threads = g_ptr_array_new();
                new->comm = strdup(tmp->comm);
                new->process_files_table = g_ptr_array_new();
+               new->files_history = tmp->files_history;
                new->perf = g_hash_table_new(g_str_hash, g_str_equal);
                g_hash_table_foreach(tmp->perf, copy_perf_counter, new->perf);
 
-               new->iostream = g_new0(struct iostream, 1);
-               memcpy(new->iostream, tmp->iostream, sizeof(struct iostream));
                /* compute the stream speed */
                if (end - start != 0) {
                        time = (end - start) / NSEC_PER_SEC;
-                       new->iostream->ret_read = new->iostream->ret_read / time;
-                       new->iostream->ret_write = new->iostream->ret_write / time;
+                       new->fileread = new->fileread/(time);
+                       new->filewrite = new->filewrite/(time);
                }
 
                for (j = 0; j < tmp->process_files_table->len; j++) {
                        tmpfile = g_ptr_array_index(tmp->process_files_table, j);
-                       newfile = g_new0(struct files, 1);
-
-                       memcpy(newfile, tmpfile, sizeof(struct files));
-
-                       newfile->name = strdup(tmpfile->name);
-                       newfile->ref = new;
-
-                       g_ptr_array_add(new->process_files_table, newfile);
-                       g_ptr_array_add(dst->files_table, newfile);
 
+                       newfile = malloc(sizeof(struct files));
+
+                       if (tmpfile != NULL) {
+                               memcpy(newfile, tmpfile, sizeof(struct files));
+                               newfile->name = strdup(tmpfile->name);
+                               newfile->ref = new;
+                               g_ptr_array_add(new->process_files_table,
+                                               newfile);
+                               g_ptr_array_add(dst->files_table, newfile);
+                       } else {
+                               g_ptr_array_add(new->process_files_table, NULL);
+                               g_ptr_array_add(dst->files_table, NULL);
+                       }
                        /*
                         * if the process died during the last period, we remove all
                         * files associated with if after the copy
                         */
                        if (tmp->death > 0 && tmp->death < end) {
+                               /* FIXME : close the files before */
                                g_ptr_array_remove(tmp->process_files_table, tmpfile);
                                g_free(tmpfile);
                        }
@@ -315,3 +460,89 @@ struct lttngtop* get_copy_lttngtop(unsigned long start, unsigned long end)
        return dst;
 }
 
+
+enum bt_cb_ret handle_statedump_process_state(struct bt_ctf_event *call_data,
+               void *private_data)
+{
+       const struct bt_definition *scope;
+       struct processtop *proc;
+       unsigned long timestamp;
+       int64_t pid, tid;
+       char *procname;
+
+       timestamp = bt_ctf_get_timestamp(call_data);
+       if (timestamp == -1ULL)
+               goto error;
+
+       scope = bt_ctf_get_top_level_scope(call_data,
+                        BT_EVENT_FIELDS);
+       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");
+               goto error;
+       }
+
+       scope = bt_ctf_get_top_level_scope(call_data,
+                       BT_EVENT_FIELDS);
+       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");
+               goto error;
+       }
+
+       /*
+        * FIXME
+        * I first tried with bt_ctf_get_string but doesn`t work at all
+        * It couldn`t find the field _name because it is an integer in
+        * the metadata and not a string like _filename for the
+        * statedump_file_descriptor
+        */
+       scope = bt_ctf_get_top_level_scope(call_data,
+                       BT_EVENT_FIELDS);
+       procname = bt_ctf_get_char_array(bt_ctf_get_field(call_data,
+                               scope, "_name"));
+       if (bt_ctf_field_get_error()) {
+               fprintf(stderr, "Missing process name context info\n");
+               goto error;
+       }
+
+       proc = find_process_tid(&lttngtop, tid, procname);
+       if (proc == NULL)
+               proc = add_proc(&lttngtop, tid, procname, timestamp);
+
+       free(proc->comm);
+       proc->comm = strdup(procname);
+       proc->pid = pid;
+
+       /*
+        * FIXME
+        * I would like to free procname because it is duplicated
+        * when the process is created but it segfaults...
+        *
+        * free(procname);
+        */
+
+       return BT_CB_OK;
+
+error:
+       return BT_CB_ERROR_STOP;
+}
+
+struct tm format_timestamp(uint64_t timestamp)
+{
+       struct tm tm;
+       uint64_t ts_sec = 0, ts_nsec;
+       time_t time_s;
+
+       ts_nsec = timestamp;
+       ts_sec += ts_nsec / NSEC_PER_SEC;
+       ts_nsec = ts_nsec % NSEC_PER_SEC;
+
+       time_s = (time_t) ts_sec;
+
+       localtime_r(&time_s, &tm);
+
+       return tm;
+}
This page took 0.026472 seconds and 4 git commands to generate.