7 #include "buffer_tree.h"
15 /** The number of references to this buffer. */
19 struct btr_buffer_reference {
20 struct btr_buffer *btrb;
22 /* Each buffer reference belongs to the buffer queue list of some buffer tree node. */
23 struct list_head node;
28 struct btr_node *parent;
29 /* The position of this btr node in the buffer tree. */
30 struct list_head node;
31 /* The children nodes of this btr node are linked together in a list. */
32 struct list_head children;
33 /* Time of first data transfer. */
36 * The input queue is a list of references to btr buffers. Each item on
37 * the list represents an input buffer which has not been completely
38 * used by this btr node.
40 struct list_head input_queue;
41 btr_command_handler execute;
45 #define FOR_EACH_CHILD(_tn, _btrn) list_for_each_entry((_tn), \
46 &((_btrn)->children), node)
47 #define FOR_EACH_CHILD_SAFE(_tn, _tmp, _btrn) \
48 list_for_each_entry_safe((_tn), (_tmp), &((_btrn)->children), node)
50 #define FOR_EACH_BUFFER_REF(_br, _btrn) \
51 list_for_each_entry((_br), &(_btrn)->input_queue, node)
52 #define FOR_EACH_BUFFER_REF_SAFE(_br, _tmp, _btrn) \
53 list_for_each_entry_safe((_br), (_tmp), &(_btrn)->input_queue, node)
55 struct btr_node *btr_new_node(const char *name, struct btr_node *parent,
56 btr_command_handler handler, void *context)
58 struct btr_node *btrn = para_malloc(sizeof(*btrn));
60 btrn->name = para_strdup(name);
61 btrn->parent = parent;
62 btrn->execute = handler;
63 btrn->context = context;
64 btrn->start.tv_sec = 0;
65 btrn->start.tv_usec = 0;
67 list_add_tail(&btrn->node, &parent->children);
68 INIT_LIST_HEAD(&btrn->children);
69 INIT_LIST_HEAD(&btrn->input_queue);
71 PARA_INFO_LOG("added %s as child of %s\n", name, parent->name);
73 PARA_INFO_LOG("added %s as btr root\n", name);
78 * Allocate a new btr buffer.
80 * The freshly allocated buffer will have a zero refcount.
82 static struct btr_buffer *new_btrb(char *buf, size_t size)
84 struct btr_buffer *btrb = para_malloc(sizeof(*btrb));
93 * Deallocate the reference, release the resources if refcount drops to zero.
95 static void btr_drop_buffer_reference(struct btr_buffer_reference *br)
97 struct btr_buffer *btrb = br->btrb;
99 //PARA_CRIT_LOG("dropping buffer reference %p\n", br);
103 if (btrb->refcount == 0) {
109 static void add_btrb_to_children(struct btr_buffer *btrb,
110 struct btr_node *btrn, size_t consumed)
114 if (btrn->start.tv_sec == 0)
116 FOR_EACH_CHILD(ch, btrn) {
117 struct btr_buffer_reference *br = para_malloc(sizeof(*br));
119 br->consumed = consumed;
120 list_add_tail(&br->node, &ch->input_queue);
122 if (ch->start.tv_sec == 0)
127 void btr_add_output(char *buf, size_t size, struct btr_node *btrn)
129 struct btr_buffer *btrb;
132 if (list_empty(&btrn->children)) {
136 btrb = new_btrb(buf, size);
137 add_btrb_to_children(btrb, btrn, 0);
140 static void btr_pushdown_br(struct btr_buffer_reference *br, struct btr_node *btrn)
142 add_btrb_to_children(br->btrb, btrn, br->consumed);
143 btr_drop_buffer_reference(br);
146 void btr_pushdown(struct btr_node *btrn)
148 struct btr_buffer_reference *br, *tmp;
150 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
151 btr_pushdown_br(br, btrn);
154 int btr_pushdown_one(struct btr_node *btrn)
156 struct btr_buffer_reference *br;
158 if (list_empty(&btrn->input_queue))
160 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
161 btr_pushdown_br(br, btrn);
165 /* Return true if this node has no children. */
166 bool btr_no_children(struct btr_node *btrn)
168 return list_empty(&btrn->children);
171 bool btr_no_parent(struct btr_node *btrn)
173 return !btrn->parent;
176 bool btr_inplace_ok(struct btr_node *btrn)
180 return list_is_singular(&btrn->parent->children);
183 static inline size_t br_available_bytes(struct btr_buffer_reference *br)
185 return br->btrb->size - br->consumed;
188 size_t btr_get_buffer_by_reference(struct btr_buffer_reference *br, char **buf)
190 *buf = br->btrb->buf + br->consumed;
191 return br_available_bytes(br);
195 * \return zero if the input buffer queue is empty.
197 size_t btr_next_buffer(struct btr_node *btrn, char **bufp)
199 struct btr_buffer_reference *br;
201 if (list_empty(&btrn->input_queue)) {
205 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
206 return btr_get_buffer_by_reference(br, bufp);
209 void btr_consume(struct btr_node *btrn, size_t numbytes)
211 struct btr_buffer_reference *br;
213 assert(!list_empty(&btrn->input_queue));
214 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
215 assert(br->consumed + numbytes <= br->btrb->size);
216 br->consumed += numbytes;
217 if (br->consumed == br->btrb->size)
218 btr_drop_buffer_reference(br);
221 static void flush_input_queue(struct btr_node *btrn)
223 struct btr_buffer_reference *br, *tmp;
224 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
225 btr_drop_buffer_reference(br);
228 void btr_free_node(struct btr_node *btrn)
236 void btr_remove_node(struct btr_node *btrn)
242 PARA_NOTICE_LOG("removing btr node %s from buffer tree\n", btrn->name);
243 FOR_EACH_CHILD(ch, btrn)
245 flush_input_queue(btrn);
247 list_del(&btrn->node);
250 size_t btr_get_input_queue_size(struct btr_node *btrn)
252 struct btr_buffer_reference *br;
255 FOR_EACH_BUFFER_REF(br, btrn) {
256 //PARA_CRIT_LOG("size: %zu\n", size);
257 size += br_available_bytes(br);
262 void btr_splice_out_node(struct btr_node *btrn)
264 struct btr_node *ch, *tmp;
267 PARA_NOTICE_LOG("splicing out %s\n", btrn->name);
270 list_del(&btrn->node);
271 FOR_EACH_CHILD_SAFE(ch, tmp, btrn) {
272 PARA_INFO_LOG("parent(%s): %s\n", ch->name,
273 btrn->parent? btrn->parent->name : "NULL");
274 ch->parent = btrn->parent;
276 list_move(&ch->node, &btrn->parent->children);
278 assert(list_empty(&btrn->children));
282 * Return the size of the largest input queue.
284 * Iterates over all children of the given node.
286 size_t btr_bytes_pending(struct btr_node *btrn)
291 FOR_EACH_CHILD(ch, btrn) {
292 size_t size = btr_get_input_queue_size(ch);
293 max_size = PARA_MAX(max_size, size);
298 int btr_exec(struct btr_node *btrn, const char *command, char **value_result)
301 return -ERRNO_TO_PARA_ERROR(EINVAL);
303 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
304 return btrn->execute(btrn, command, value_result);
307 int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
311 for (; btrn; btrn = btrn->parent) {
312 struct btr_node *parent = btrn->parent;
314 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
315 if (!parent->execute)
317 PARA_INFO_LOG("parent: %s, cmd: %s\n", parent->name, command);
318 ret = parent->execute(parent, command, value_result);
319 if (ret == -ERRNO_TO_PARA_ERROR(ENOTSUP))
323 if (value_result && *value_result)
324 PARA_NOTICE_LOG("%s(%s): %s\n", command, parent->name,
328 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
331 void *btr_context(struct btr_node *btrn)
333 return btrn->context;
337 * Merge the first two input buffers into one.
339 * This is a quite expensive operation.
341 * \return The number of buffers that have been available (zero, one or two).
343 static int merge_input(struct btr_node *btrn)
345 struct btr_buffer_reference *brs[2], *br;
350 if (list_empty(&btrn->input_queue))
352 if (list_is_singular(&btrn->input_queue))
355 /* get references to the first two buffers */
356 FOR_EACH_BUFFER_REF(br, btrn) {
358 szs[i] = btr_get_buffer_by_reference(brs[i], bufs + i);
363 /* make a new btrb that combines the two buffers and a br to it. */
364 sz = szs[0] + szs[1];
365 PARA_DEBUG_LOG("merging input buffers: (%zu, %zu) -> %zu\n",
367 buf = para_malloc(sz);
368 /* TODO: Avoid this memcopy by introducing btr buffer pool. */
369 memcpy(buf, bufs[0], szs[0]);
370 memcpy(buf + szs[0], bufs[1], szs[1]);
372 br = para_malloc(sizeof(*br));
373 br->btrb = new_btrb(buf, sz);
374 br->btrb->refcount = 1;
377 /* replace the first two refs by the new one */
378 btr_drop_buffer_reference(brs[0]);
379 btr_drop_buffer_reference(brs[1]);
380 para_list_add(&br->node, &btrn->input_queue);
384 void btr_merge(struct btr_node *btrn, size_t dest_size)
388 size_t len = btr_next_buffer(btrn, &buf);
389 if (len >= dest_size)
391 PARA_DEBUG_LOG("input size = %zu < %zu = dest\n", len, dest_size);
392 if (merge_input(btrn) < 2)
397 bool btr_eof(struct btr_node *btrn)
400 size_t len = btr_next_buffer(btrn, &buf);
402 return (len == 0 && btr_no_parent(btrn));
405 void log_tree_recursively(struct btr_node *btrn, int loglevel, int depth)
408 const char spaces[] = " ", *space = spaces + 16 - depth;
412 para_log(loglevel, "%s%s\n", space, btrn->name);
413 FOR_EACH_CHILD(ch, btrn)
414 log_tree_recursively(ch, loglevel, depth + 1);
417 void btr_log_tree(struct btr_node *btrn, int loglevel)
419 return log_tree_recursively(btrn, loglevel, 0);
422 /** 640K ought to be enough for everybody ;) */
423 #define BTRN_MAX_PENDING (640 * 1024)
425 int btr_node_status(struct btr_node *btrn, size_t min_iqs,
426 enum btr_node_type type)
430 if (type != BTR_NT_LEAF) {
431 if (btr_no_children(btrn))
432 return -E_BTR_NO_CHILD;
433 if (btr_bytes_pending(btrn) > BTRN_MAX_PENDING)
436 if (type != BTR_NT_ROOT) {
439 iqs = btr_get_input_queue_size(btrn);
440 if (iqs == 0) /* we have a parent, because not eof */
442 if (iqs < min_iqs && !btr_no_parent(btrn))
448 void btr_get_node_start(struct btr_node *btrn, struct timeval *tv)