[PATCH] blktrace: remember to initialize mutex
[blktrace.git] / blktrace.c
index aa7ac32679d25ea8de831ce6d1462f0f5c95fdef..992b4eb9af988005aeb6e1b41f9468b0ca42f296 100644 (file)
@@ -160,7 +160,6 @@ struct thread_information {
        pthread_mutex_t lock;
        struct list_head subbuf_list;
        struct tip_subbuf *leftover_ts;
-       unsigned int leftover_ts_offset;
 };
 
 struct device_information {
@@ -286,7 +285,7 @@ static void wait_for_data(struct thread_information *tip)
        struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };
 
        do {
-               poll(&pfd, 1, 10);
+               poll(&pfd, 1, 100);
                if (pfd.revents & POLLIN)
                        break;
                if (tip->ofile_stdout)
@@ -333,21 +332,13 @@ static inline void tip_fd_lock(struct thread_information *tip)
 static int get_subbuf(struct thread_information *tip)
 {
        struct tip_subbuf *ts;
-       int ts_size, ret;
-
-       /*
-        * live tracing should get a lower count to make it more "realtime"
-        */
-       if (tip->ofile_stdout)
-               ts_size = 1024;
-       else
-               ts_size = buf_size;
+       int ret;
 
        ts = malloc(sizeof(*ts));
-       ts->buf = malloc(ts_size);
-       ts->max_len = ts_size;
+       ts->buf = malloc(buf_size);
+       ts->max_len = buf_size;
 
-       ret = read_data(tip, ts->buf, ts_size);
+       ret = read_data(tip, ts->buf, ts->max_len);
        if (ret > 0) {
                ts->len = ret;
                tip_fd_lock(tip);
@@ -445,13 +436,12 @@ static int flush_subbuf(struct thread_information *tip, struct tip_subbuf *ts)
        if (tip->leftover_ts) {
                struct tip_subbuf *prev_ts = tip->leftover_ts;
 
-               offset = tip->leftover_ts_offset;
-               if (offset + prev_ts->len + ts->len > prev_ts->max_len) {
+               if (prev_ts->len + ts->len > prev_ts->max_len) {
                        prev_ts->max_len += ts->len;
                        prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
                }
 
-               memcpy(prev_ts->buf + offset + ts->len, ts->buf, ts->len);
+               memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len);
                prev_ts->len += ts->len;
 
                free(ts->buf);
@@ -487,7 +477,8 @@ static int flush_subbuf(struct thread_information *tip, struct tip_subbuf *ts)
         */
        if (offset != ts->len) {
                tip->leftover_ts = ts;
-               tip->leftover_ts_offset = offset;
+               ts->len -= offset;
+               memmove(ts->buf, ts->buf + offset, ts->len);
        } else {
                free(ts->buf);
                free(ts);
@@ -520,7 +511,7 @@ static void get_and_write_events(void)
 {
        struct device_information *dip;
        struct thread_information *tip;
-       int i, j, events, ret, all_exited;
+       int i, j, events, ret, tips_running;
 
        while (!is_done()) {
                events = 0;
@@ -542,17 +533,17 @@ static void get_and_write_events(void)
         */
        do {
                events = 0;
-               all_exited = 0;
+               tips_running = 0;
                for_each_dip(dip, i) {
                        for_each_tip(dip, tip, j) {
                                ret = write_tip_events(tip);
                                if (ret > 0)
                                        events += ret;
-                               all_exited += !tip->exited;
+                               tips_running += !tip->exited;
                        }
                }
                usleep(10);
-       } while (events || !all_exited);
+       } while (events || tips_running);
 }
 
 static int start_threads(struct device_information *dip)
@@ -566,9 +557,9 @@ static int start_threads(struct device_information *dip)
                tip->cpu = j;
                tip->device = dip;
                tip->events_processed = 0;
+               pthread_mutex_init(&tip->lock, NULL);
                INIT_LIST_HEAD(&tip->subbuf_list);
                tip->leftover_ts = NULL;
-               tip->leftover_ts_offset = 0;
 
                if (pipeline) {
                        tip->ofile = fdopen(STDOUT_FILENO, "w");