]> mj.ucw.cz Git - libucw.git/blobdiff - debug/sorter/asio-test.c
Merge with git+ssh://git.ucw.cz/projects/sherlock/GIT/sherlock.git
[libucw.git] / debug / sorter / asio-test.c
index f3f384ca0bf63dee96ab3457e7d0065e7bf54fe3..40df77b33125c175e9858b6a290f4edbde7b2149 100644 (file)
@@ -8,21 +8,24 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
 
 #define COPY
 #define DIRECT O_DIRECT
 
+static timestamp_t timer;
+
 #define P_INIT do { cnt = 0; cnt_rep = 0; cnt_ms = 1; } while(0)
 #define P_UPDATE(cc) do { \
   cnt += cc; \
-  if (cnt >= cnt_rep) { cnt_ms += get_timer(); \
+  if (cnt >= cnt_rep) { cnt_ms += get_timer(&timer); \
     printf("%d of %d MB (%.2f MB/sec)\r", (int)(cnt >> 20), (int)(total_size >> 20), (double)cnt / 1048576 * 1000 / cnt_ms); \
     fflush(stdout); cnt_rep += 1<<26; } } while(0)
 #define P_FINAL do { \
-  cnt_ms += get_timer(); \
-  log(L_INFO, "Spent %.3f sec (%.2f MB/sec)", (double)cnt_ms/1000, (double)cnt / 1048576 * 1000 / cnt_ms); \
+  cnt_ms += get_timer(&timer); \
+  msg(L_INFO, "Spent %.3f sec (%.2f MB/sec)", (double)cnt_ms/1000, (double)cnt / 1048576 * 1000 / cnt_ms); \
 } while(0)
 
 static struct asio_queue io_queue;
@@ -37,15 +40,16 @@ int main(int argc, char **argv)
   uns cnt_ms;
   int fd[files];
   byte name[files][16];
+  struct asio_request *req[files];
 
-  init_timer();
+  init_timer(&timer);
 
   io_queue.buffer_size = bufsize;
   io_queue.max_writebacks = 2;
   asio_init_queue(&io_queue);
 
 #ifdef COPY
-  log(L_INFO, "Creating input file");
+  msg(L_INFO, "Creating input file");
   int in_fd = sh_open("tmp/ft-in", O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
   ASSERT(in_fd >= 0);
   ASSERT(!(total_size % bufsize));
@@ -68,7 +72,7 @@ int main(int argc, char **argv)
   P_FINAL;
 #endif
 
-  log(L_INFO, "Initializing output files");
+  msg(L_INFO, "Initializing output files");
   for (uns i=0; i<files; i++)
     {
       sprintf(name[i], "tmp/ft-%d", i);
@@ -77,22 +81,27 @@ int main(int argc, char **argv)
        die("Cannot create %s: %m", name[i]);
     }
   sync();
-  get_timer();
+  get_timer(&timer);
 
-  log(L_INFO, "Writing %d MB to %d files in parallel with %d byte buffers", (int)(total_size >> 20), files, bufsize);
+  msg(L_INFO, "Writing %d MB to %d files in parallel with %d byte buffers", (int)(total_size >> 20), files, bufsize);
   P_INIT;
+  for (uns i=0; i<files; i++)
+    req[i] = asio_get(&io_queue);
   for (uns round=0; round<total_size/bufsize/files; round++)
     {
       for (uns i=0; i<files; i++)
        {
-         struct asio_request *r = asio_get(&io_queue);
+         struct asio_request *r = req[i];
 #ifdef COPY
-         r->op = ASIO_READ;
-         r->fd = in_fd;
-         r->len = bufsize;
-         asio_submit(r);
-         struct asio_request *rr = asio_wait(&io_queue);
-         ASSERT(rr == r && r->status == (int)r->len);
+         struct asio_request *rr, *rd = asio_get(&io_queue);
+         rd->op = ASIO_READ;
+         rd->fd = in_fd;
+         rd->len = bufsize;
+         asio_submit(rd);
+         rr = asio_wait(&io_queue);
+         ASSERT(rr == rd && rd->status == (int)rd->len);
+         memcpy(r->buffer, rd->buffer, bufsize);
+         asio_put(rr);
 #else
          for (uns j=0; j<bufsize; j++)
            r->buffer[j] = round+i+j;
@@ -102,17 +111,20 @@ int main(int argc, char **argv)
          r->len = bufsize;
          asio_submit(r);
          P_UPDATE(bufsize);
+         req[i] = asio_get(&io_queue);
        }
     }
+  for (uns i=0; i<files; i++)
+    asio_put(req[i]);
   asio_sync(&io_queue);
 #ifdef COPY
   close(in_fd);
 #endif
-  log(L_INFO, "Syncing");
+  msg(L_INFO, "Syncing");
   sync();
   P_FINAL;
 
-  log(L_INFO, "Reading the files sequentially");
+  msg(L_INFO, "Reading the files sequentially");
   P_INIT;
   for (uns i=0; i<files; i++)
     {
@@ -140,6 +152,6 @@ int main(int argc, char **argv)
 #endif
 
   asio_cleanup_queue(&io_queue);
-  log(L_INFO, "Done");
+  msg(L_INFO, "Done");
   return 0;
 }