]> mj.ucw.cz Git - libucw.git/blobdiff - lib/sorter/sbuck.c
Better messages and sort-test controls.
[libucw.git] / lib / sorter / sbuck.c
index 0895840112644e1149364af4741f3c94e2890c5c..5bc6bbe488dd2a08ddf046efc2cd9f67ec369a83 100644 (file)
@@ -71,9 +71,9 @@ sbuck_swap_in(struct sort_bucket *b)
       b->fb = bopen(b->filename, O_RDWR, sorter_stream_bufsize);
       if (b->flags & SBF_OPEN_WRITE)
        bseek(b->fb, 0, SEEK_END);
-      bconfig(b->fb, BCONFIG_IS_TEMP_FILE, 1);
+      bconfig(b->fb, BCONFIG_IS_TEMP_FILE, 1); /* FIXME: Was it always so? */
       b->flags &= ~SBF_SWAPPED_OUT;
-      SORT_XTRACE("Swapped in %s", b->filename);
+      SORT_XTRACE(2, "Swapped in %s", b->filename);
     }
 }
 
@@ -104,6 +104,8 @@ sbuck_write(struct sort_bucket *b)
     {
       ASSERT(!(b->flags & (SBF_OPEN_READ | SBF_DESTROYED)));
       b->fb = bopen_tmp(sorter_stream_bufsize);
+      if (sorter_debug & SORT_DEBUG_KEEP_BUCKETS)
+       bconfig(b->fb, BCONFIG_IS_TEMP_FILE, 0);
       b->flags |= SBF_OPEN_WRITE;
       b->filename = mp_strdup(b->ctx->pool, b->fb->name);
     }
@@ -121,7 +123,7 @@ sbuck_swap_out(struct sort_bucket *b)
       bclose(b->fb);
       b->fb = NULL;
       b->flags |= SBF_SWAPPED_OUT;
-      SORT_XTRACE("Swapped out %s", b->filename);
+      SORT_XTRACE(2, "Swapped out %s", b->filename);
     }
 }
 
@@ -136,7 +138,7 @@ sorter_alloc_buf(struct sort_context *ctx)
   ctx->big_buf_size = 2*bs;
   ctx->big_buf_half = ((byte*) ctx->big_buf) + bs;
   ctx->big_buf_half_size = bs;
-  SORT_XTRACE("Allocated sorting buffer (%jd bytes)", (uintmax_t) bs);
+  SORT_XTRACE(2, "Allocated sorting buffer (%jd bytes)", (uintmax_t) bs);
 }
 
 void
@@ -146,5 +148,22 @@ sorter_free_buf(struct sort_context *ctx)
     return;
   big_free(ctx->big_buf, ctx->big_buf_size);
   ctx->big_buf = NULL;
-  SORT_XTRACE("Freed sorting buffer");
+  SORT_XTRACE(2, "Freed sorting buffer");
+}
+
+void
+format_size(byte *buf, u64 x)
+{
+  if (x < 10<<10)
+    sprintf(buf, "%.1fK", (double)x/(1<<10));
+  else if (x < 1<<20)
+    sprintf(buf, "%dK", (int)(x/(1<<10)));
+  else if (x < 10<<20)
+    sprintf(buf, "%.1fM", (double)x/(1<<20));
+  else if (x < 1<<30)
+    sprintf(buf, "%dM", (int)(x/(1<<20)));
+  else if (x < (u64)10<<30)
+    sprintf(buf, "%.1fG", (double)x/(1<<30));
+  else
+    sprintf(buf, "%dG", (int)(x/(1<<30)));
 }