[PATCH] blktrace: no need to track ts->offset anymore
[blktrace.git] / blktrace.c
index 94fba1a1eda109ed5e69331c21362cfd01f7131f..24b48d5b2a214ecc9d86222a6263366afbfbe2b7 100644 (file)
@@ -160,7 +160,6 @@ struct tip_subbuf {
        void *buf;
        unsigned int len;
        unsigned int max_len;
-       off_t offset;
 };
 
 #define FIFO_SIZE      (1024)  /* should be plenty big! */
@@ -190,6 +189,7 @@ struct thread_information {
        off_t ofile_offset;
        int ofile_stdout;
        int ofile_mmap;
+       volatile int sendfile_pending;
 
        int (*get_subbuf)(struct thread_information *, unsigned int);
        int (*flush_subbuf)(struct thread_information *, struct tip_subbuf *);
@@ -290,7 +290,7 @@ enum {
 static char hostname[MAXHOSTNAMELEN];
 static int net_port = TRACE_NET_PORT;
 static int net_mode = 0;
-static int net_sendfile;
+static int net_use_sendfile;
 
 static int net_in_fd = -1;
 static int net_out_fd = -1;
@@ -304,6 +304,8 @@ static void handle_sigint(__attribute__((__unused__)) int sig)
         * stop trace so we can reap currently produced data
         */
        for_each_dip(dip, i) {
+               if (dip->fd == -1)
+                       continue;
                if (ioctl(dip->fd, BLKTRACESTOP) < 0)
                        perror("BLKTRACESTOP");
        }
@@ -342,21 +344,6 @@ static int get_dropped_count(const char *buts_name)
        return atoi(tmp);
 }
 
-static size_t get_subbuf_padding(struct thread_information *tip,
-                                unsigned subbuf)
-{
-       size_t padding_size = buf_nr * sizeof(size_t);
-       size_t ret;
-
-       if (read(tip->pfd, tip->pfd_buf, padding_size) < 0) {
-               perror("tip pad read");
-               ret = -1;
-       } else
-               ret = tip->pfd_buf[subbuf];
-
-       return ret;
-}
-
 static int start_trace(struct device_information *dip)
 {
        struct blk_user_trace_setup buts;
@@ -591,8 +578,6 @@ static int get_subbuf_sendfile(struct thread_information *tip,
                               unsigned int maxlen)
 {
        struct tip_subbuf *ts;
-       struct stat sb;
-       unsigned int ready, this_size, total;
 
        wait_for_data(tip);
 
@@ -602,43 +587,20 @@ static int get_subbuf_sendfile(struct thread_information *tip,
        if (is_done())
                return get_subbuf(tip, maxlen);
 
-       if (fstat(tip->fd, &sb) < 0) {
-               perror("trace stat");
-               return -1;
-       }
-
-       ready = sb.st_size - tip->ofile_offset;
-       if (!ready) {
-               /*
-                * delay a little, since poll() will return data available
-                * until sendfile() is run
-                */
+       if (tip->sendfile_pending) {
                usleep(100);
                return 0;
        }
 
-       this_size = buf_size;
-       total = ready;
-       while (ready) {
-               if (this_size > ready)
-                       this_size = ready;
+       ts = malloc(sizeof(*ts));
+       ts->buf = NULL;
+       ts->max_len = 0;
 
-               ts = malloc(sizeof(*ts));
-
-               ts->buf = NULL;
-               ts->max_len = 0;
-
-               ts->len = this_size;
-               ts->offset = tip->ofile_offset;
-               tip->ofile_offset += ts->len;
-
-               if (subbuf_fifo_queue(tip, ts))
-                       return -1;
-
-               ready -= this_size;
-       }
+       if (subbuf_fifo_queue(tip, ts))
+               return -1;
 
-       return total;
+       tip->sendfile_pending++;
+       return buf_size;
 }
 
 static void close_thread(struct thread_information *tip)
@@ -702,7 +664,7 @@ static void *thread_main(void *arg)
                exit_trace(1);
        }
 
-       if (net_mode == Net_client && net_sendfile) {
+       if (net_mode == Net_client && net_use_sendfile) {
                char tmp[MAXPATHLEN + 64];
 
                snprintf(tmp, sizeof(tmp), "%s/block/%s/trace%d.padding",
@@ -793,12 +755,45 @@ static int flush_subbuf_net(struct thread_information *tip,
        return 0;
 }
 
+static int net_sendfile(struct thread_information *tip, struct tip_subbuf *ts)
+{
+       int ret = sendfile(net_out_fd, tip->fd, NULL, ts->len);
+
+       if (ret < 0) {
+               perror("sendfile");
+               return 1;
+       } else if (ret < (int) ts->len) {
+               fprintf(stderr, "short sendfile send (%d of %d)\n", ret, ts->len);
+               return 1;
+       }
+
+       return 0;
+}
+
+static int get_subbuf_padding(struct thread_information *tip, off_t off)
+{
+       int padding_size = buf_nr * sizeof(size_t);
+       int ret;
+
+       ret = read(tip->pfd, tip->pfd_buf, padding_size);
+       if (ret == padding_size) {
+               int subbuf = (off / buf_size) % buf_nr;
+
+               ret = tip->pfd_buf[subbuf];
+       } else if (ret < 0)
+               perror("tip pad read");
+       else {
+               fprintf(stderr, "bad pad size read\n");
+               ret = -1;
+       }
+
+       return ret;
+}
+
 static int flush_subbuf_sendfile(struct thread_information *tip,
                                 struct tip_subbuf *ts)
 {
-       size_t padding;
-       unsigned subbuf;
-       unsigned len;
+       int pad, ret = 1;
 
        /*
         * currently we cannot use sendfile() on the last bytes read, as they
@@ -808,20 +803,24 @@ static int flush_subbuf_sendfile(struct thread_information *tip,
        if (ts->buf)
                return flush_subbuf_net(tip, ts);
        
-       subbuf = (ts->offset / buf_size) % buf_nr;
-       padding = get_subbuf_padding(tip, subbuf);
-       len = ts->len - padding;
+       pad = get_subbuf_padding(tip, tip->ofile_offset);
+       if (pad == -1)
+               goto err;
 
-       if (net_send_header(tip, len))
-               return 1;
-       if (sendfile(net_out_fd, tip->fd, &ts->offset, len) < 0) {
-               perror("sendfile");
-               return 1;
-       }
+       ts->len = buf_size - pad;
 
-       tip->data_read += len;
+       if (net_send_header(tip, ts->len))
+               goto err;
+       if (net_sendfile(tip, ts))
+               goto err;
+
+       tip->data_read += ts->len;
+       tip->ofile_offset += buf_size;
+       ret = 0;
+err:
+       tip->sendfile_pending--;
        free(ts);
-       return 0;
+       return ret;
 }
 
 static int write_data(struct thread_information *tip, void *buf,
@@ -1036,7 +1035,7 @@ static void fill_ops(struct thread_information *tip)
         * setup ops
         */
        if (net_mode == Net_client) {
-               if (net_sendfile) {
+               if (net_use_sendfile) {
                        tip->get_subbuf = get_subbuf_sendfile;
                        tip->flush_subbuf = flush_subbuf_sendfile;
                } else {
@@ -1069,8 +1068,7 @@ static int tip_open_output(struct device_information *dip,
                tip->ofile = NULL;
                tip->ofile_stdout = 0;
                tip->ofile_mmap = 0;
-               vbuf_size = 0;
-               mode = 0; /* gcc 4.x issues a bogus warning */
+               goto done;
        } else if (pipeline) {
                tip->ofile = fdopen(STDOUT_FILENO, "w");
                tip->ofile_stdout = 1;
@@ -1087,20 +1085,19 @@ static int tip_open_output(struct device_information *dip,
                vbuf_size = OFILE_BUF;
        }
 
-       if (net_mode != Net_client && tip->ofile == NULL) {
+       if (tip->ofile == NULL) {
                perror(op);
                return 1;
        }
 
-       if (vbuf_size) {
-               tip->ofile_buffer = malloc(vbuf_size);
-               if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
-                       perror("setvbuf");
-                       close_thread(tip);
-                       return 1;
-               }
+       tip->ofile_buffer = malloc(vbuf_size);
+       if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
+               perror("setvbuf");
+               close_thread(tip);
+               return 1;
        }
 
+done:
        fill_ops(tip);
        return 0;
 }
@@ -1307,8 +1304,10 @@ static struct device_information *net_get_dip(char *buts_name,
 
        device_information = realloc(device_information, (ndevs + 1) * sizeof(*dip));
        dip = &device_information[ndevs];
+       memset(dip, 0, sizeof(*dip));
+       dip->fd = -1;
        strcpy(dip->buts_name, buts_name);
-       strcpy(dip->path, buts_name);
+       dip->path = strdup(buts_name);
        ndevs++;
        dip->threads = malloc(ncpus * sizeof(struct thread_information));
        memset(dip->threads, 0, ncpus * sizeof(struct thread_information));
@@ -1368,7 +1367,7 @@ static int net_get_header(struct blktrace_net_hdr *bnh)
                }
        }
        fcntl(net_in_fd, F_SETFL, fl & ~O_NONBLOCK);
-       return 0;
+       return bytes_left;
 }
 
 static int net_server_loop(struct in_addr *cl_in_addr)
@@ -1385,10 +1384,16 @@ static int net_server_loop(struct in_addr *cl_in_addr)
        }
 
        if (!data_is_native) {
+               bnh.magic = be32_to_cpu(bnh.magic);
                bnh.cpu = be32_to_cpu(bnh.cpu);
                bnh.len = be32_to_cpu(bnh.len);
        }
 
+       if ((bnh.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
+               fprintf(stderr, "server: bad data magic\n");
+               return 1;
+       }
+
        /*
         * len == 0 means that the other end signalled end-of-run
         */
@@ -1490,6 +1495,7 @@ repeat:
                        fclose(tip->ofile);
 
                free(dip->threads);
+               free(dip->path);
        }
 
        free(device_information);
@@ -1656,7 +1662,7 @@ int main(int argc, char *argv[])
                        net_port = atoi(optarg);
                        break;
                case 's':
-                       net_sendfile = 1;
+                       net_use_sendfile = 1;
                        break;
                default:
                        show_usage(argv[0]);