7 #include "buffer_tree.h"
14 /** The number of references to this buffer. */
18 struct btr_buffer_reference {
19 struct btr_buffer *btrb;
21 /* Each buffer reference belongs to the buffer queue list of some buffer tree node. */
22 struct list_head node;
27 struct btr_node *parent;
28 /* The position of this btr node in the buffer tree. */
29 struct list_head node;
30 /* The children nodes of this btr node are linked together in a list. */
31 struct list_head children;
33 * The input queue is a list of references to btr buffers. Each item on
34 * the list represents an input buffer which has not been completely
35 * used by this btr node.
37 struct list_head input_queue;
38 btr_command_handler execute;
42 #define FOR_EACH_CHILD(_tn, _btrn) list_for_each_entry((_tn), \
43 &((_btrn)->children), node)
44 #define FOR_EACH_CHILD_SAFE(_tn, _tmp, _btrn) \
45 list_for_each_entry_safe((_tn), (_tmp), &((_btrn)->children), node)
47 #define FOR_EACH_BUFFER_REF(_br, _btrn) \
48 list_for_each_entry((_br), &(_btrn)->input_queue, node)
49 #define FOR_EACH_BUFFER_REF_SAFE(_br, _tmp, _btrn) \
50 list_for_each_entry_safe((_br), (_tmp), &(_btrn)->input_queue, node)
52 struct btr_node *btr_new_node(const char *name, struct btr_node *parent,
53 btr_command_handler handler, void *context)
55 struct btr_node *btrn = para_malloc(sizeof(*btrn));
57 btrn->name = para_strdup(name);
58 btrn->parent = parent;
59 btrn->execute = handler;
60 btrn->context = context;
62 list_add_tail(&btrn->node, &parent->children);
63 INIT_LIST_HEAD(&btrn->children);
64 INIT_LIST_HEAD(&btrn->input_queue);
66 PARA_INFO_LOG("added %s as child of %s\n", name, parent->name);
68 PARA_INFO_LOG("added %s as btr root\n", name);
73 * Allocate a new btr buffer.
75 * The freshly allocated buffer will have a zero refcount.
77 static struct btr_buffer *new_btrb(char *buf, size_t size)
79 struct btr_buffer *btrb = para_malloc(sizeof(*btrb));
88 * Deallocate the reference, release the resources if refcount drops to zero.
90 static void btr_drop_buffer_reference(struct btr_buffer_reference *br)
92 struct btr_buffer *btrb = br->btrb;
94 //PARA_CRIT_LOG("dropping buffer reference %p\n", br);
98 if (btrb->refcount == 0) {
104 static void add_btrb_to_children(struct btr_buffer *btrb,
105 struct btr_node *btrn, size_t consumed)
109 FOR_EACH_CHILD(ch, btrn) {
110 struct btr_buffer_reference *br = para_malloc(sizeof(*br));
112 br->consumed = consumed;
113 list_add_tail(&br->node, &ch->input_queue);
118 void btr_add_output(char *buf, size_t size, struct btr_node *btrn)
120 struct btr_buffer *btrb;
123 if (list_empty(&btrn->children)) {
127 btrb = new_btrb(buf, size);
128 add_btrb_to_children(btrb, btrn, 0);
131 static void btr_pushdown_br(struct btr_buffer_reference *br, struct btr_node *btrn)
133 add_btrb_to_children(br->btrb, btrn, br->consumed);
134 btr_drop_buffer_reference(br);
137 void btr_pushdown(struct btr_node *btrn)
139 struct btr_buffer_reference *br, *tmp;
141 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
142 btr_pushdown_br(br, btrn);
145 int btr_pushdown_one(struct btr_node *btrn)
147 struct btr_buffer_reference *br;
149 if (list_empty(&btrn->input_queue))
151 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
152 btr_pushdown_br(br, btrn);
156 /* Return true if this node has no children. */
157 bool btr_no_children(struct btr_node *btrn)
159 return list_empty(&btrn->children);
162 bool btr_no_parent(struct btr_node *btrn)
164 return !btrn->parent;
167 bool btr_inplace_ok(struct btr_node *btrn)
171 return list_is_singular(&btrn->parent->children);
174 static inline size_t br_available_bytes(struct btr_buffer_reference *br)
176 return br->btrb->size - br->consumed;
179 size_t btr_get_buffer_by_reference(struct btr_buffer_reference *br, char **buf)
181 *buf = br->btrb->buf + br->consumed;
182 return br_available_bytes(br);
186 * \return zero if the input buffer queue is empty.
188 size_t btr_next_buffer(struct btr_node *btrn, char **bufp)
190 struct btr_buffer_reference *br;
192 if (list_empty(&btrn->input_queue)) {
196 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
197 return btr_get_buffer_by_reference(br, bufp);
200 void btr_consume(struct btr_node *btrn, size_t numbytes)
202 struct btr_buffer_reference *br;
204 assert(!list_empty(&btrn->input_queue));
205 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
206 assert(br->consumed + numbytes <= br->btrb->size);
207 br->consumed += numbytes;
208 if (br->consumed == br->btrb->size)
209 btr_drop_buffer_reference(br);
212 static void flush_input_queue(struct btr_node *btrn)
214 struct btr_buffer_reference *br, *tmp;
215 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
216 btr_drop_buffer_reference(br);
219 void btr_free_node(struct btr_node *btrn)
227 void btr_remove_node(struct btr_node *btrn)
233 PARA_NOTICE_LOG("removing btr node %s from buffer tree\n", btrn->name);
234 FOR_EACH_CHILD(ch, btrn)
236 flush_input_queue(btrn);
238 list_del(&btrn->node);
241 size_t btr_get_input_queue_size(struct btr_node *btrn)
243 struct btr_buffer_reference *br;
246 FOR_EACH_BUFFER_REF(br, btrn) {
247 //PARA_CRIT_LOG("size: %zu\n", size);
248 size += br_available_bytes(br);
253 void btr_splice_out_node(struct btr_node *btrn)
255 struct btr_node *ch, *tmp;
258 PARA_NOTICE_LOG("splicing out %s\n", btrn->name);
261 list_del(&btrn->node);
262 FOR_EACH_CHILD_SAFE(ch, tmp, btrn) {
263 PARA_INFO_LOG("parent(%s): %s\n", ch->name,
264 btrn->parent? btrn->parent->name : "NULL");
265 ch->parent = btrn->parent;
267 list_move(&ch->node, &btrn->parent->children);
269 assert(list_empty(&btrn->children));
273 * Return the size of the largest input queue.
275 * Iterates over all children of the given node.
277 size_t btr_bytes_pending(struct btr_node *btrn)
282 FOR_EACH_CHILD(ch, btrn) {
283 size_t size = btr_get_input_queue_size(ch);
284 max_size = PARA_MAX(max_size, size);
289 int btr_exec(struct btr_node *btrn, const char *command, char **value_result)
292 return -ERRNO_TO_PARA_ERROR(EINVAL);
294 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
295 return btrn->execute(btrn, command, value_result);
298 int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
302 for (; btrn; btrn = btrn->parent) {
303 struct btr_node *parent = btrn->parent;
305 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
306 if (!parent->execute)
308 PARA_INFO_LOG("parent: %s, cmd: %s\n", parent->name, command);
309 ret = parent->execute(parent, command, value_result);
310 if (ret == -ERRNO_TO_PARA_ERROR(ENOTSUP))
314 if (value_result && *value_result)
315 PARA_NOTICE_LOG("%s(%s): %s\n", command, parent->name,
319 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
322 void *btr_context(struct btr_node *btrn)
324 return btrn->context;
328 * Merge the first two input buffers into one.
330 * This is a quite expensive operation.
332 * \return The number of buffers that have been available (zero, one or two).
334 static int merge_input(struct btr_node *btrn)
336 struct btr_buffer_reference *brs[2], *br;
341 if (list_empty(&btrn->input_queue))
343 if (list_is_singular(&btrn->input_queue))
346 /* get references to the first two buffers */
347 FOR_EACH_BUFFER_REF(br, btrn) {
349 szs[i] = btr_get_buffer_by_reference(brs[i], bufs + i);
354 /* make a new btrb that combines the two buffers and a br to it. */
355 sz = szs[0] + szs[1];
356 //PARA_CRIT_LOG("merging input buffers: (%zu, %zu) -> %zu\n",
357 // szs[0], szs[1], sz);
358 buf = para_malloc(sz);
359 /* TODO: Avoid this memcopy by introducing btr buffer pool. */
360 memcpy(buf, bufs[0], szs[0]);
361 memcpy(buf + szs[0], bufs[1], szs[1]);
363 br = para_malloc(sizeof(*br));
364 br->btrb = new_btrb(buf, sz);
365 br->btrb->refcount = 1;
368 /* replace the first two refs by the new one */
369 btr_drop_buffer_reference(brs[0]);
370 btr_drop_buffer_reference(brs[1]);
371 para_list_add(&br->node, &btrn->input_queue);
375 void btr_merge(struct btr_node *btrn, size_t dest_size)
379 size_t len = btr_next_buffer(btrn, &buf);
380 if (len >= dest_size)
382 if (merge_input(btrn) < 2)
387 bool btr_eof(struct btr_node *btrn)
390 size_t len = btr_next_buffer(btrn, &buf);
392 return (len == 0 && btr_no_parent(btrn));
395 void log_tree_recursively(struct btr_node *btrn, int loglevel, int depth)
398 const char spaces[] = " ", *space = spaces + 16 - depth;
402 para_log(loglevel, "%s%s\n", space, btrn->name);
403 FOR_EACH_CHILD(ch, btrn)
404 log_tree_recursively(ch, loglevel, depth + 1);
407 void btr_log_tree(struct btr_node *btrn, int loglevel)
409 return log_tree_recursively(btrn, loglevel, 0);
412 /** 640K ought to be enough for everybody ;) */
413 #define BTRN_MAX_PENDING (640 * 1024)
415 int btr_node_status(struct btr_node *btrn, size_t min_iqs)
421 if (btr_bytes_pending(btrn) > BTRN_MAX_PENDING)
423 iqs = btr_get_input_queue_size(btrn);
424 if (iqs == 0) /* we have a parent, because not eof */
426 if (iqs < min_iqs && !btr_no_parent(btrn))