]> mj.ucw.cz Git - libucw.git/blob - lib/sorter/govern.c
Fixed bug in calculation of radix split width.
[libucw.git] / lib / sorter / govern.c
1 /*
2  *      UCW Library -- Universal Sorter: Governing Routines
3  *
4  *      (c) 2007 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/fastbuf.h"
12 #include "lib/mempool.h"
13 #include "lib/stkstring.h"
14 #include "lib/sorter/common.h"
15
16 #include <string.h>
17 #include <sys/time.h>
18 #include <time.h>
19
20 #define F_BSIZE(b) stk_fsize(sbuck_size(b))
21
22 static void
23 sorter_start_timer(struct sort_context *ctx)
24 {
25   init_timer(&ctx->start_time);
26 }
27
28 static void
29 sorter_stop_timer(struct sort_context *ctx, uns *account_to)
30 {
31   ctx->last_pass_time = get_timer(&ctx->start_time);
32   *account_to += ctx->last_pass_time;
33 }
34
35 static uns
36 sorter_speed(struct sort_context *ctx, u64 size)
37 {
38   if (!size)
39     return 0;
40   if (!ctx->last_pass_time)
41     return -1;
42   return (uns)((double)size / (1<<20) * 1000 / ctx->last_pass_time);
43 }
44
45 static int
46 sorter_presort(struct sort_context *ctx, struct sort_bucket *in, struct sort_bucket *out, struct sort_bucket *out_only)
47 {
48   sorter_alloc_buf(ctx);
49   if (in->flags & SBF_CUSTOM_PRESORT)
50     {
51       struct fastbuf *f = sbuck_write(out);
52       out->runs++;
53       return ctx->custom_presort(f, ctx->big_buf, ctx->big_buf_size);   // FIXME: out_only optimization?
54     }
55   return ctx->internal_sort(ctx, in, out, out_only);
56 }
57
58 static inline struct sort_bucket *
59 sbuck_join_to(struct sort_bucket *b)
60 {
61   if (sorter_debug & SORT_DEBUG_NO_JOIN)
62     return NULL;
63
64   struct sort_bucket *out = (struct sort_bucket *) b->n.prev;   // Such bucket is guaranteed to exist
65   return (out->flags & SBF_FINAL) ? out : NULL;
66 }
67
68 static void
69 sorter_join(struct sort_bucket *b)
70 {
71   struct sort_bucket *join = (struct sort_bucket *) b->n.prev;
72   ASSERT(join->flags & SBF_FINAL);
73   ASSERT(b->runs == 1);
74
75   if (!sbuck_has_file(join))
76     {
77       // The final bucket doesn't have any file associated yet, so replace
78       // it with the new bucket.
79       SORT_XTRACE(2, "Replaced final bucket");
80       b->flags |= SBF_FINAL;
81       sbuck_drop(join);
82     }
83   else
84     {
85       SORT_TRACE("Copying to output file: %s", F_BSIZE(b));
86       struct fastbuf *src = sbuck_read(b);
87       struct fastbuf *dest = sbuck_write(join);
88       bbcopy(src, dest, ~0U);
89       sbuck_drop(b);
90     }
91 }
92
93 static void
94 sorter_twoway(struct sort_context *ctx, struct sort_bucket *b)
95 {
96   struct sort_bucket *ins[3] = { NULL }, *outs[3] = { NULL };
97   cnode *list_pos = b->n.prev;
98   struct sort_bucket *join = sbuck_join_to(b);
99
100   if (!(sorter_debug & SORT_DEBUG_NO_PRESORT) || (b->flags & SBF_CUSTOM_PRESORT))
101     {
102       SORT_XTRACE(3, "%s", ((b->flags & SBF_CUSTOM_PRESORT) ? "Custom presorting" : "Presorting"));
103       sorter_start_timer(ctx);
104       ins[0] = sbuck_new(ctx);
105       if (!sorter_presort(ctx, b, ins[0], join ? : ins[0]))
106         {
107           sorter_stop_timer(ctx, &ctx->total_pre_time);
108           SORT_XTRACE(((b->flags & SBF_SOURCE) ? 1 : 2), "Sorted in memory");
109           if (join)
110             sbuck_drop(ins[0]);
111           else
112             clist_insert_after(&ins[0]->n, list_pos);
113           sbuck_drop(b);
114           return;
115         }
116
117       ins[1] = sbuck_new(ctx);
118       int i = 1;
119       while (sorter_presort(ctx, b, ins[i], ins[i]))
120         i = 1-i;
121       sbuck_drop(b);
122       sorter_stop_timer(ctx, &ctx->total_pre_time);
123       SORT_TRACE("Presorting pass (%d+%d runs, %s+%s, %dMB/s)",
124                  ins[0]->runs, ins[1]->runs,
125                  F_BSIZE(ins[0]), F_BSIZE(ins[1]),
126                  sorter_speed(ctx, sbuck_size(ins[0]) + sbuck_size(ins[1])));
127     }
128   else
129     {
130       SORT_XTRACE(2, "Presorting disabled");
131       ins[0] = b;
132     }
133
134   SORT_XTRACE(3, "Main sorting");
135   uns pass = 0;
136   do {
137     ++pass;
138     sorter_start_timer(ctx);
139     if (ins[0]->runs == 1 && ins[1]->runs == 1 && join)
140       {
141         // This is guaranteed to produce a single run, so join if possible
142         sh_off_t join_size = sbuck_size(join);
143         outs[0] = join;
144         outs[1] = NULL;
145         ctx->twoway_merge(ctx, ins, outs);
146         ASSERT(join->runs == 2);
147         join->runs--;
148         join_size = sbuck_size(join) - join_size;
149         sorter_stop_timer(ctx, &ctx->total_ext_time);
150         SORT_TRACE("Mergesort pass %d (final run, %s, %dMB/s)", pass, stk_fsize(join_size), sorter_speed(ctx, join_size));
151         sbuck_drop(ins[0]);
152         sbuck_drop(ins[1]);
153         return;
154       }
155     outs[0] = sbuck_new(ctx);
156     outs[1] = sbuck_new(ctx);
157     outs[2] = NULL;
158     ctx->twoway_merge(ctx, ins, outs);
159     sorter_stop_timer(ctx, &ctx->total_ext_time);
160     SORT_TRACE("Mergesort pass %d (%d+%d runs, %s+%s, %dMB/s)", pass,
161                outs[0]->runs, outs[1]->runs,
162                F_BSIZE(outs[0]), F_BSIZE(outs[1]),
163                sorter_speed(ctx, sbuck_size(outs[0]) + sbuck_size(outs[1])));
164     sbuck_drop(ins[0]);
165     sbuck_drop(ins[1]);
166     memcpy(ins, outs, 3*sizeof(struct sort_bucket *));
167   } while (sbuck_have(ins[1]));
168
169   sbuck_drop(ins[1]);
170   clist_insert_after(&ins[0]->n, list_pos);
171 }
172
173 static uns
174 sorter_radix_bits(struct sort_context *ctx, struct sort_bucket *b)
175 {
176   if (!b->hash_bits || b->hash_bits < sorter_min_radix_bits ||
177       !ctx->radix_split ||
178       (b->flags & SBF_CUSTOM_PRESORT) ||
179       (sorter_debug & SORT_DEBUG_NO_RADIX))
180     return 0;
181
182   u64 in = sbuck_size(b);
183   u64 mem = ctx->internal_estimate(ctx, b) * 0.8;       // FIXME: Magical factor for hash non-uniformity
184   if (in <= mem)
185     return 0;
186
187   uns n = sorter_min_radix_bits;
188   while (n < sorter_max_radix_bits && n < b->hash_bits && (in >> n) > mem)
189     n++;
190   return n;
191 }
192
193 static void
194 sorter_radix(struct sort_context *ctx, struct sort_bucket *b, uns bits)
195 {
196   uns nbuck = 1 << bits;
197   SORT_XTRACE(2, "Running radix split on %s with hash %d bits of %d (expecting %s buckets)",
198               F_BSIZE(b), bits, b->hash_bits, stk_fsize(sbuck_size(b) / nbuck));
199   sorter_free_buf(ctx);
200   sorter_start_timer(ctx);
201
202   struct sort_bucket **outs = alloca(nbuck * sizeof(struct sort_bucket *));
203   for (uns i=nbuck; i--; )
204     {
205       outs[i] = sbuck_new(ctx);
206       outs[i]->hash_bits = b->hash_bits - bits;
207       clist_insert_after(&outs[i]->n, &b->n);
208     }
209
210   ctx->radix_split(ctx, b, outs, b->hash_bits - bits, bits);
211
212   u64 min = ~(u64)0, max = 0, sum = 0;
213   for (uns i=0; i<nbuck; i++)
214     {
215       u64 s = sbuck_size(outs[i]);
216       min = MIN(min, s);
217       max = MAX(max, s);
218       sum += s;
219       if (nbuck > 4)
220         sbuck_swap_out(outs[i]);
221     }
222
223   sorter_stop_timer(ctx, &ctx->total_ext_time);
224   SORT_TRACE("Radix split (%d buckets, %s min, %s max, %s avg, %dMB/s)", nbuck,
225              stk_fsize(min), stk_fsize(max), stk_fsize(sum / nbuck), sorter_speed(ctx, sum));
226   sbuck_drop(b);
227 }
228
229 void
230 sorter_run(struct sort_context *ctx)
231 {
232   ctx->pool = mp_new(4096);
233   clist_init(&ctx->bucket_list);
234   sorter_prepare_buf(ctx);
235
236   // Create bucket containing the source
237   struct sort_bucket *bin = sbuck_new(ctx);
238   bin->flags = SBF_SOURCE | SBF_OPEN_READ;
239   if (ctx->custom_presort)
240     bin->flags |= SBF_CUSTOM_PRESORT;
241   else
242     bin->fb = ctx->in_fb;
243   bin->ident = "in";
244   bin->size = ctx->in_size;
245   bin->hash_bits = ctx->hash_bits;
246   clist_add_tail(&ctx->bucket_list, &bin->n);
247   SORT_XTRACE(2, "Input size: %s", F_BSIZE(bin));
248
249   // Create bucket for the output
250   struct sort_bucket *bout = sbuck_new(ctx);
251   bout->flags = SBF_FINAL;
252   if (bout->fb = ctx->out_fb)
253     bout->flags |= SBF_OPEN_WRITE;
254   bout->ident = "out";
255   bout->runs = 1;
256   clist_add_head(&ctx->bucket_list, &bout->n);
257
258   struct sort_bucket *b;
259   uns bits;
260   while (bout = clist_head(&ctx->bucket_list), b = clist_next(&ctx->bucket_list, &bout->n))
261     {
262       SORT_XTRACE(2, "Next block: %s, %d hash bits", F_BSIZE(b), b->hash_bits);
263       if (!sbuck_have(b))
264         sbuck_drop(b);
265       else if (b->runs == 1)
266         sorter_join(b);
267       else if (bits = sorter_radix_bits(ctx, b))
268         sorter_radix(ctx, b, bits);
269       else
270         sorter_twoway(ctx, b);
271     }
272
273   sorter_free_buf(ctx);
274   sbuck_write(bout);            // Force empty bucket to a file
275   SORT_XTRACE(2, "Final size: %s", F_BSIZE(bout));
276   SORT_XTRACE(2, "Final timings: %.3fs external sorting, %.3fs presorting, %.3fs internal sorting",
277               ctx->total_ext_time/1000., ctx->total_pre_time/1000., ctx->total_int_time/1000.);
278   ctx->out_fb = sbuck_read(bout);
279 }