]> mj.ucw.cz Git - moe.git/blob - lib/sorter/common.h
Added libucw from Sherlock v3.12.2.
[moe.git] / lib / sorter / common.h
1 /*
2  *      UCW Library -- Universal Sorter: Common Declarations
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 #ifndef _UCW_SORTER_COMMON_H
11 #define _UCW_SORTER_COMMON_H
12
13 #include "lib/clists.h"
14
15 /* Configuration variables */
16 extern uns sorter_trace, sorter_trace_array, sorter_stream_bufsize;
17 extern uns sorter_debug, sorter_min_radix_bits, sorter_max_radix_bits, sorter_add_radix_bits;
18 extern uns sorter_min_multiway_bits, sorter_max_multiway_bits;
19 extern uns sorter_threads;
20 extern u64 sorter_bufsize, sorter_small_input;
21 extern u64 sorter_thread_threshold, sorter_thread_chunk, sorter_radix_threshold;
22 extern struct fb_params sorter_fb_params, sorter_small_fb_params;
23
24 #define SORT_TRACE(x...) do { if (sorter_trace) msg(L_DEBUG, x); } while(0)
25 #define SORT_XTRACE(level, x...) do { if (sorter_trace >= level) msg(L_DEBUG, x); } while(0)
26
27 enum sort_debug {
28   SORT_DEBUG_NO_PRESORT = 1,
29   SORT_DEBUG_NO_JOIN = 2,
30   SORT_DEBUG_KEEP_BUCKETS = 4,
31   SORT_DEBUG_NO_RADIX = 8,
32   SORT_DEBUG_NO_MULTIWAY = 16,
33   SORT_DEBUG_ASORT_NO_RADIX = 32,
34   SORT_DEBUG_ASORT_NO_THREADS = 64
35 };
36
37 struct sort_bucket;
38
39 struct sort_context {
40   struct fastbuf *in_fb;
41   struct fastbuf *out_fb;
42   uns hash_bits;
43   u64 in_size;
44   struct fb_params *fb_params;
45
46   struct mempool *pool;
47   clist bucket_list;
48   void *big_buf;
49   size_t big_buf_size;
50
51   int (*custom_presort)(struct fastbuf *dest, void *buf, size_t bufsize);
52
53   // Take as much as possible from the source bucket, sort it in memory and dump to destination bucket.
54   // Return 1 if there is more data available in the source bucket.
55   int (*internal_sort)(struct sort_context *ctx, struct sort_bucket *in, struct sort_bucket *out, struct sort_bucket *out_only);
56
57   // Estimate how much input data from `b' will fit in the internal sorting buffer.
58   u64 (*internal_estimate)(struct sort_context *ctx, struct sort_bucket *b);
59
60   // Two-way split/merge: merge up to 2 source buckets to up to 2 destination buckets.
61   // Bucket arrays are NULL-terminated.
62   void (*twoway_merge)(struct sort_context *ctx, struct sort_bucket **ins, struct sort_bucket **outs);
63
64   // Multi-way merge: merge an arbitrary number of source buckets to a single destination bucket.
65   void (*multiway_merge)(struct sort_context *ctx, struct sort_bucket **ins, struct sort_bucket *out);
66
67   // Radix split according to hash function
68   void (*radix_split)(struct sort_context *ctx, struct sort_bucket *in, struct sort_bucket **outs, uns bitpos, uns numbits);
69
70   // State variables of internal_sort
71   void *key_buf;
72   int more_keys;
73
74   // Timing
75   timestamp_t start_time;
76   uns last_pass_time;
77   uns total_int_time, total_pre_time, total_ext_time;
78 };
79
80 void sorter_run(struct sort_context *ctx);
81
82 /* Buffers */
83
84 void *sorter_alloc(struct sort_context *ctx, uns size);
85 void sorter_prepare_buf(struct sort_context *ctx);
86 void sorter_alloc_buf(struct sort_context *ctx);
87 void sorter_free_buf(struct sort_context *ctx);
88
89 /* Buckets */
90
91 struct sort_bucket {
92   cnode n;
93   struct sort_context *ctx;
94   uns flags;
95   struct fastbuf *fb;
96   byte *filename;
97   u64 size;                             // Size in bytes (not valid when writing)
98   uns runs;                             // Number of runs, 0 if not sorted
99   uns hash_bits;                        // Remaining bits of the hash function
100   byte *ident;                          // Identifier used in debug messages
101 };
102
103 enum sort_bucket_flags {
104   SBF_FINAL = 1,                        // This bucket corresponds to the final output file (always 1 run)
105   SBF_SOURCE = 2,                       // Contains the source file (always 0 runs)
106   SBF_CUSTOM_PRESORT = 4,               // Contains source to read via custom presorter
107   SBF_OPEN_WRITE = 256,                 // We are currently writing to the fastbuf
108   SBF_OPEN_READ = 512,                  // We are reading from the fastbuf
109   SBF_DESTROYED = 1024,                 // Already done with, no further references allowed
110   SBF_SWAPPED_OUT = 2048,               // Swapped out to a named file
111 };
112
113 struct sort_bucket *sbuck_new(struct sort_context *ctx);
114 void sbuck_drop(struct sort_bucket *b);
115 int sbuck_have(struct sort_bucket *b);
116 int sbuck_has_file(struct sort_bucket *b);
117 sh_off_t sbuck_size(struct sort_bucket *b);
118 struct fastbuf *sbuck_read(struct sort_bucket *b);
119 struct fastbuf *sbuck_write(struct sort_bucket *b);
120 void sbuck_swap_out(struct sort_bucket *b);
121
122 /* Contexts and helper functions for the array sorter */
123
124 struct asort_context {
125   // Interface between generic code in array.c and functions generated by array.h
126   void *array;                          // Array to sort
127   void *buffer;                         // Auxiliary buffer (required when radix-sorting)
128   uns num_elts;                         // Number of elements in the array
129   uns elt_size;                         // Bytes per element
130   uns hash_bits;                        // Remaining bits of the hash function
131   uns radix_bits;                       // How many bits to process in a single radix-sort pass
132   void (*quicksort)(void *array_ptr, uns num_elts);
133   void (*quicksplit)(void *array_ptr, uns num_elts, int *leftp, int *rightp);
134   void (*radix_count)(void *src_ptr, uns num_elts, uns *cnt, uns shift);
135   void (*radix_split)(void *src_ptr, void *dest_ptr, uns num_elts, uns *ptrs, uns shift);
136
137   // Used internally by array.c
138   struct rs_work **rs_works;
139   struct work_queue *rs_work_queue;
140   struct eltpool *eltpool;
141
142   // Configured limits translated from bytes to elements
143   uns thread_threshold;
144   uns thread_chunk;
145   uns radix_threshold;
146 };
147
148 void asort_run(struct asort_context *ctx);
149 void asort_start_threads(uns run);
150 void asort_stop_threads(void);
151
152 #endif