2 * UCW Library -- Universal Sorter: Governing Routines
4 * (c) 2007 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include "lib/fastbuf.h"
12 #include "lib/mempool.h"
13 #include "lib/sorter/common.h"
16 sorter_alloc(struct sort_context *ctx, uns size)
18 return mp_alloc_zero(ctx->pool, size);
22 sbuck_new(struct sort_context *ctx)
24 return sorter_alloc(ctx, sizeof(struct sort_bucket));
28 sbuck_drop(struct sort_bucket *b)
32 ASSERT(!(b->flags & SBF_DESTROYED));
37 b->flags = SBF_DESTROYED;
42 sbuck_size(struct sort_bucket *b)
44 if (b->flags & SBF_OPEN_WRITE)
51 sbuck_have(struct sort_bucket *b)
53 return b && sbuck_size(b);
57 sbuck_read(struct sort_bucket *b)
59 /* FIXME: These functions should handle buckets with no fb and only name. */
61 if (b->flags & SBF_OPEN_READ)
63 else if (b->flags & SBF_OPEN_WRITE)
65 b->size = btell(b->fb);
66 b->flags = (b->flags & ~SBF_OPEN_WRITE) | SBF_OPEN_READ;
75 sbuck_write(struct sort_bucket *b)
77 if (b->flags & SBF_OPEN_WRITE)
81 ASSERT(!(b->flags & (SBF_OPEN_READ | SBF_DESTROYED)));
82 b->fb = bopen_tmp(sorter_stream_bufsize);
83 b->flags |= SBF_OPEN_WRITE;
89 sorter_alloc_buf(struct sort_context *ctx)
93 u64 bs = MAX(sorter_bufsize/2, 1);
94 bs = ALIGN_TO(bs, (u64)CPU_PAGE_SIZE);
95 ctx->big_buf = big_alloc(2*bs);
96 ctx->big_buf_size = 2*bs;
97 ctx->big_buf_half = ((byte*) ctx->big_buf) + bs;
98 ctx->big_buf_half_size = bs;
99 SORT_XTRACE("Allocated sorting buffer (%jd bytes)", (uintmax_t) bs);
103 sorter_free_buf(struct sort_context *ctx)
107 big_free(ctx->big_buf, ctx->big_buf_size);
109 SORT_XTRACE("Freed sorting buffer");
112 static int sorter_presort(struct sort_context *ctx, struct sort_bucket *in, struct sort_bucket *out, struct sort_bucket *out_only)
114 /* FIXME: Mode with no presorting (mostly for debugging) */
115 sorter_alloc_buf(ctx);
116 if (in->flags & SBF_CUSTOM_PRESORT)
118 struct fastbuf *f = sbuck_write(out);
119 return ctx->custom_presort(f, ctx->big_buf, ctx->big_buf_size); // FIXME: out_only optimization?
121 return ctx->internal_sort(ctx, in, out, out_only);
124 static inline struct sort_bucket *
125 sbuck_join_to(struct sort_bucket *b)
127 struct sort_bucket *out = (struct sort_bucket *) b->n.prev; // Such bucket is guaranteed to exist
128 return (out->flags & SBF_FINAL) ? out : NULL;
132 sorter_join(struct sort_bucket *b)
134 struct sort_bucket *join = sbuck_join_to(b);
137 // FIXME: What if the final bucket doesn't contain any file yet?
139 SORT_TRACE("Copying %jd bytes to output file", (uintmax_t) sbuck_size(b));
140 struct fastbuf *src = sbuck_read(b);
141 struct fastbuf *dest = sbuck_write(join);
142 bbcopy(src, dest, ~0U);
147 sorter_twoway(struct sort_context *ctx, struct sort_bucket *b)
149 struct sort_bucket *ins[3] = { NULL }, *outs[3] = { NULL };
150 cnode *list_pos = b->n.prev;
151 struct sort_bucket *join = sbuck_join_to(b);
152 if (sorter_debug & SORT_DEBUG_NO_JOIN)
155 if (!(sorter_debug & SORT_DEBUG_NO_PRESORT) || (b->flags & SBF_CUSTOM_PRESORT))
157 SORT_TRACE("Presorting");
158 ins[0] = sbuck_new(ctx);
160 if (!sorter_presort(ctx, b, ins[0], join ? : ins[0]))
165 clist_insert_after(&ins[0]->n, list_pos);
170 ins[1] = sbuck_new(ctx);
172 while (sorter_presort(ctx, b, ins[i], ins[i]))
178 SORT_TRACE("Skipped presorting");
182 SORT_TRACE("Main sorting");
184 if (ins[0]->runs == 1 && ins[1]->runs == 1 && join)
186 // This is guaranteed to produce a single run, so join if possible
189 ctx->twoway_merge(ctx, ins, outs);
190 ASSERT(outs[0]->runs == 2);
192 SORT_TRACE("Pass done (joined final run)");
197 outs[0] = sbuck_new(ctx);
198 outs[1] = sbuck_new(ctx);
200 ctx->twoway_merge(ctx, ins, outs);
201 SORT_TRACE("Pass done (%d+%d runs, %jd+%jd bytes)", outs[0]->runs, outs[1]->runs, (uintmax_t) sbuck_size(outs[0]), (uintmax_t) sbuck_size(outs[1]));
204 memcpy(ins, outs, 3*sizeof(struct sort_bucket *));
205 } while (sbuck_have(ins[1]));
208 clist_insert_after(&ins[0]->n, list_pos);
212 sorter_run(struct sort_context *ctx)
214 ctx->pool = mp_new(4096);
215 clist_init(&ctx->bucket_list);
217 /* FIXME: There should be a way how to detect size of the input file */
218 /* FIXME: Remember to test sorting of empty files */
220 // Create bucket containing the source
221 struct sort_bucket *bin = sbuck_new(ctx);
222 bin->flags = SBF_SOURCE | SBF_OPEN_READ;
223 if (ctx->custom_presort)
224 bin->flags |= SBF_CUSTOM_PRESORT;
226 bin->fb = ctx->in_fb;
229 bin->hash_bits = ctx->hash_bits;
230 clist_add_tail(&ctx->bucket_list, &bin->n);
232 // Create bucket for the output
233 struct sort_bucket *bout = sbuck_new(ctx);
234 bout->flags = SBF_FINAL;
235 bout->fb = ctx->out_fb;
238 clist_add_head(&ctx->bucket_list, &bout->n);
240 struct sort_bucket *b;
241 while (b = clist_next(&ctx->bucket_list, &bout->n))
245 else if (b->runs == 1)
248 sorter_twoway(ctx, b);
251 sorter_free_buf(ctx);
252 SORT_XTRACE("Final size: %jd", (uintmax_t) sbuck_size(bout));
253 ctx->out_fb = sbuck_read(bout);