X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fsorter%2Fsbuck.c;h=515115e282533e9ed00454b23e771ae13c76a4e9;hb=e34560a76a7af3fb428604e4da3cd14cfd1bf454;hp=5bc6bbe488dd2a08ddf046efc2cd9f67ec369a83;hpb=889bb33b738e79732f3fb1ca79b361ac0375aa76;p=libucw.git diff --git a/lib/sorter/sbuck.c b/lib/sorter/sbuck.c index 5bc6bbe4..515115e2 100644 --- a/lib/sorter/sbuck.c +++ b/lib/sorter/sbuck.c @@ -10,6 +10,7 @@ #include "lib/lib.h" #include "lib/fastbuf.h" #include "lib/mempool.h" +#include "lib/stkstring.h" #include "lib/sorter/common.h" #include @@ -68,10 +69,13 @@ sbuck_swap_in(struct sort_bucket *b) { if (b->flags & SBF_SWAPPED_OUT) { - b->fb = bopen(b->filename, O_RDWR, sorter_stream_bufsize); + if (sorter_stream_bufsize) /* FIXME: Needs better configuration, probably semi-automatic one */ + b->fb = bopen(b->filename, O_RDWR, sorter_stream_bufsize); + else + b->fb = fbdir_open(b->filename, O_RDWR, NULL); if (b->flags & SBF_OPEN_WRITE) bseek(b->fb, 0, SEEK_END); - bconfig(b->fb, BCONFIG_IS_TEMP_FILE, 1); /* FIXME: Was it always so? */ + bconfig(b->fb, BCONFIG_IS_TEMP_FILE, 1); b->flags &= ~SBF_SWAPPED_OUT; SORT_XTRACE(2, "Swapped in %s", b->filename); } @@ -103,7 +107,10 @@ sbuck_write(struct sort_bucket *b) else { ASSERT(!(b->flags & (SBF_OPEN_READ | SBF_DESTROYED))); - b->fb = bopen_tmp(sorter_stream_bufsize); + if (sorter_stream_bufsize) + b->fb = bopen_tmp(sorter_stream_bufsize); + else + b->fb = fbdir_open_tmp(NULL); if (sorter_debug & SORT_DEBUG_KEEP_BUCKETS) bconfig(b->fb, BCONFIG_IS_TEMP_FILE, 0); b->flags |= SBF_OPEN_WRITE; @@ -115,7 +122,7 @@ sbuck_write(struct sort_bucket *b) void sbuck_swap_out(struct sort_bucket *b) { - if ((b->flags & (SBF_OPEN_READ | SBF_OPEN_WRITE)) && b->fb) + if ((b->flags & (SBF_OPEN_READ | SBF_OPEN_WRITE)) && b->fb && !(b->flags & SBF_SOURCE)) { if (b->flags & SBF_OPEN_WRITE) b->size = btell(b->fb); @@ -128,17 +135,22 @@ sbuck_swap_out(struct sort_bucket *b) } void -sorter_alloc_buf(struct sort_context *ctx) +sorter_prepare_buf(struct sort_context *ctx) { - if (ctx->big_buf) - return; u64 bs = MAX(sorter_bufsize/2, 1); bs = ALIGN_TO(bs, (u64)CPU_PAGE_SIZE); - ctx->big_buf = big_alloc(2*bs); ctx->big_buf_size = 2*bs; - ctx->big_buf_half = ((byte*) ctx->big_buf) + bs; ctx->big_buf_half_size = bs; - SORT_XTRACE(2, "Allocated sorting buffer (%jd bytes)", (uintmax_t) bs); +} + +void +sorter_alloc_buf(struct sort_context *ctx) +{ + if (ctx->big_buf) + return; + ctx->big_buf = big_alloc(ctx->big_buf_size); + ctx->big_buf_half = ((byte*) ctx->big_buf) + ctx->big_buf_half_size; + SORT_XTRACE(2, "Allocated sorting buffer (2*%s)", stk_fsize(ctx->big_buf_half_size)); } void @@ -150,20 +162,3 @@ sorter_free_buf(struct sort_context *ctx) ctx->big_buf = NULL; 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))); -}