[PATCH] blktrace: add ->ops for the read/get/flush methods
authorJens Axboe <axboe@suse.de>
Wed, 15 Feb 2006 08:03:55 +0000 (09:03 +0100)
committerJens Axboe <axboe@suse.de>
Wed, 15 Feb 2006 08:03:55 +0000 (09:03 +0100)
blktrace.c

index 5936540fabf4318b0f89c75aeaa3cd2ed2e44d75..d61b2d9d2fd9623d5e8bc3842ff568439c7ca427 100644 (file)
@@ -177,6 +177,10 @@ struct thread_information {
        int ofile_stdout;
        int ofile_mmap;
 
+       int (*get_subbuf)(struct thread_information *, unsigned int);
+       int (*flush_subbuf)(struct thread_information *, struct tip_subbuf *);
+       int (*read_data)(struct thread_information *, void *, unsigned int);
+
        unsigned long events_processed;
        unsigned long long data_read;
        struct device_information *device;
@@ -375,7 +379,8 @@ static void wait_for_data(struct thread_information *tip)
        } while (!is_done());
 }
 
-static int read_data_file(struct thread_information *tip, void *buf, int len)
+static int read_data_file(struct thread_information *tip, void *buf,
+                         unsigned int len)
 {
        int ret = 0;
 
@@ -402,7 +407,8 @@ static int read_data_file(struct thread_information *tip, void *buf, int len)
 
 }
 
-static int read_data_net(struct thread_information *tip, void *buf, int len)
+static int read_data_net(struct thread_information *tip, void *buf,
+                        unsigned int len)
 {
        unsigned int bytes_left = len;
        int ret = 0;
@@ -428,14 +434,10 @@ static int read_data_net(struct thread_information *tip, void *buf, int len)
        return len - bytes_left;
 }
 
-static int read_data(struct thread_information *tip, void *buf, int len)
+static int read_data(struct thread_information *tip, void *buf,
+                    unsigned int len)
 {
-       int ret;
-
-       if (net_mode == Net_server)
-               ret = read_data_net(tip, buf, len);
-       else
-               ret = read_data_file(tip, buf, len);
+       int ret = tip->read_data(tip, buf, len);
 
        if (ret > 0)
                tip->data_read += ret;
@@ -526,13 +528,13 @@ static int mmap_subbuf(struct thread_information *tip, unsigned int maxlen)
 /*
  * Use the copy approach for pipes and network
  */
-static int get_subbuf(struct thread_information *tip)
+static int get_subbuf(struct thread_information *tip, unsigned int maxlen)
 {
        struct tip_subbuf *ts = malloc(sizeof(*ts));
        int ret;
 
        ts->buf = malloc(buf_size);
-       ts->max_len = buf_size;
+       ts->max_len = maxlen;
 
        ret = read_data(tip, ts->buf, ts->max_len);
        if (ret > 0) {
@@ -600,13 +602,8 @@ static void *thread_main(void *arg)
        }
 
        while (!is_done()) {
-               if (tip->ofile_mmap && net_mode != Net_client) {
-                       if (mmap_subbuf(tip, buf_size))
-                               break;
-               } else {
-                       if (get_subbuf(tip))
-                               break;
-               }
+               if (tip->get_subbuf(tip, buf_size))
+                       break;
        }
 
        tip_ftrunc_final(tip);
@@ -749,12 +746,8 @@ static int write_tip_events(struct thread_information *tip)
 {
        struct tip_subbuf *ts = subbuf_fifo_dequeue(tip);
 
-       if (ts) {
-               if (net_mode == Net_client)
-                       return flush_subbuf_net(tip, ts);
-
-               return flush_subbuf_file(tip, ts);
-       }
+       if (ts)
+               return tip->flush_subbuf(tip, ts);
 
        return 0;
 }
@@ -839,6 +832,27 @@ static void fill_ofname(char *dst, char *buts_name, int cpu)
                sprintf(dst + len, "%s.blktrace.%d", buts_name, cpu);
 }
 
+static void fill_ops(struct thread_information *tip)
+{
+       /*
+        * setup ops
+        */
+       if (tip->ofile_mmap && net_mode != Net_client)
+               tip->get_subbuf = mmap_subbuf;
+       else
+               tip->get_subbuf = get_subbuf;
+
+       if (net_mode == Net_client)
+               tip->flush_subbuf = flush_subbuf_net;
+       else
+               tip->flush_subbuf = flush_subbuf_file;
+
+       if (net_mode == Net_server)
+               tip->read_data = read_data_net;
+       else
+               tip->read_data = read_data_file;
+}
+
 static int start_threads(struct device_information *dip)
 {
        struct thread_information *tip;
@@ -880,6 +894,8 @@ static int start_threads(struct device_information *dip)
                        return 1;
                }
 
+               fill_ops(tip);
+
                if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
                        perror("pthread_create");
                        close_thread(tip);