X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=buffer_tree.c;h=1be2037f5a7f3b9a2eb231debebdcf0be0ecedba;hp=1bc9529884dc16b779ee232022e6bb37ccee0e2d;hb=3010ef96e10cb15d423eef8f9802fbed78744393;hpb=509881dc2004a449e1da378ca22b164546a50d32 diff --git a/buffer_tree.c b/buffer_tree.c index 1bc95298..1be2037f 100644 --- a/buffer_tree.c +++ b/buffer_tree.c @@ -6,6 +6,7 @@ #include "string.h" #include "buffer_tree.h" #include "error.h" +#include "sched.h" struct btr_buffer { @@ -29,6 +30,8 @@ struct btr_node { struct list_head node; /* The children nodes of this btr node are linked together in a list. */ struct list_head children; + /* Time of first data transfer. */ + struct timeval start; /** * The input queue is a list of references to btr buffers. Each item on * the list represents an input buffer which has not been completely @@ -58,6 +61,8 @@ struct btr_node *btr_new_node(const char *name, struct btr_node *parent, btrn->parent = parent; btrn->execute = handler; btrn->context = context; + btrn->start.tv_sec = 0; + btrn->start.tv_usec = 0; if (parent) list_add_tail(&btrn->node, &parent->children); INIT_LIST_HEAD(&btrn->children); @@ -106,12 +111,16 @@ static void add_btrb_to_children(struct btr_buffer *btrb, { struct btr_node *ch; + if (btrn->start.tv_sec == 0) + btrn->start = *now; FOR_EACH_CHILD(ch, btrn) { struct btr_buffer_reference *br = para_malloc(sizeof(*br)); br->btrb = btrb; br->consumed = consumed; list_add_tail(&br->node, &ch->input_queue); btrb->refcount++; + if (ch->start.tv_sec == 0) + ch->start = *now; } } @@ -119,6 +128,11 @@ void btr_add_output(char *buf, size_t size, struct btr_node *btrn) { struct btr_buffer *btrb; + assert(size != 0); + if (list_empty(&btrn->children)) { + free(buf); + return; + } btrb = new_btrb(buf, size); add_btrb_to_children(btrb, btrn, 0); } @@ -137,6 +151,17 @@ void btr_pushdown(struct btr_node *btrn) btr_pushdown_br(br, btrn); } +int btr_pushdown_one(struct btr_node *btrn) +{ + struct btr_buffer_reference *br; + + if (list_empty(&btrn->input_queue)) + return 0; + br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node); + btr_pushdown_br(br, btrn); + return 1; +} + /* Return true if this node has no children. */ bool btr_no_children(struct btr_node *btrn) { @@ -166,6 +191,9 @@ size_t btr_get_buffer_by_reference(struct btr_buffer_reference *br, char **buf) return br_available_bytes(br); } +/** + * \return zero if the input buffer queue is empty. + */ size_t btr_next_buffer(struct btr_node *btrn, char **bufp) { struct btr_buffer_reference *br; @@ -197,20 +225,26 @@ static void flush_input_queue(struct btr_node *btrn) btr_drop_buffer_reference(br); } -void btr_del_node(struct btr_node *btrn) +void btr_free_node(struct btr_node *btrn) +{ + if (!btrn) + return; + free(btrn->name); + free(btrn); +} + +void btr_remove_node(struct btr_node *btrn) { struct btr_node *ch; if (!btrn) return; - PARA_NOTICE_LOG("deleting %s\n", btrn->name); + PARA_NOTICE_LOG("removing btr node %s from buffer tree\n", btrn->name); FOR_EACH_CHILD(ch, btrn) ch->parent = NULL; flush_input_queue(btrn); if (btrn->parent) list_del(&btrn->node); - free(btrn->name); - free(btrn); } size_t btr_get_input_queue_size(struct btr_node *btrn) @@ -225,15 +259,13 @@ size_t btr_get_input_queue_size(struct btr_node *btrn) return size; } -int btr_splice_out_node(struct btr_node *btrn) +void btr_splice_out_node(struct btr_node *btrn) { struct btr_node *ch, *tmp; - if (!btrn) - return -ERRNO_TO_PARA_ERROR(EINVAL); - if (btr_get_input_queue_size(btrn) != 0) - return -ERRNO_TO_PARA_ERROR(EINVAL); + assert(btrn); PARA_NOTICE_LOG("splicing out %s\n", btrn->name); + btr_pushdown(btrn); if (btrn->parent) list_del(&btrn->node); FOR_EACH_CHILD_SAFE(ch, tmp, btrn) { @@ -244,9 +276,6 @@ int btr_splice_out_node(struct btr_node *btrn) list_move(&ch->node, &btrn->parent->children); } assert(list_empty(&btrn->children)); - free(btrn->name); - free(btrn); - return 1; } /** @@ -309,9 +338,9 @@ void *btr_context(struct btr_node *btrn) * * This is a quite expensive operation. * - * \return The number of buffers that have been merged (zero, one or two). + * \return The number of buffers that have been available (zero, one or two). */ -int btr_merge(struct btr_node *btrn) +static int merge_input(struct btr_node *btrn) { struct btr_buffer_reference *brs[2], *br; char *bufs[2], *buf; @@ -333,8 +362,8 @@ int btr_merge(struct btr_node *btrn) } /* make a new btrb that combines the two buffers and a br to it. */ sz = szs[0] + szs[1]; - //PARA_CRIT_LOG("merging input buffers: (%zu, %zu) -> %zu\n", - // szs[0], szs[1], sz); + PARA_DEBUG_LOG("merging input buffers: (%zu, %zu) -> %zu\n", + szs[0], szs[1], sz); buf = para_malloc(sz); /* TODO: Avoid this memcopy by introducing btr buffer pool. */ memcpy(buf, bufs[0], szs[0]); @@ -351,3 +380,72 @@ int btr_merge(struct btr_node *btrn) para_list_add(&br->node, &btrn->input_queue); return 2; } + +void btr_merge(struct btr_node *btrn, size_t dest_size) +{ + for (;;) { + char *buf; + size_t len = btr_next_buffer(btrn, &buf); + if (len >= dest_size) + return; + PARA_DEBUG_LOG("input size = %zu < %zu = dest\n", len, dest_size); + if (merge_input(btrn) < 2) + return; + } +} + +bool btr_eof(struct btr_node *btrn) +{ + char *buf; + size_t len = btr_next_buffer(btrn, &buf); + + return (len == 0 && btr_no_parent(btrn)); +} + +void log_tree_recursively(struct btr_node *btrn, int loglevel, int depth) +{ + struct btr_node *ch; + const char spaces[] = " ", *space = spaces + 16 - depth; + + if (depth > 16) + return; + para_log(loglevel, "%s%s\n", space, btrn->name); + FOR_EACH_CHILD(ch, btrn) + log_tree_recursively(ch, loglevel, depth + 1); +} + +void btr_log_tree(struct btr_node *btrn, int loglevel) +{ + return log_tree_recursively(btrn, loglevel, 0); +} + +/** 640K ought to be enough for everybody ;) */ +#define BTRN_MAX_PENDING (640 * 1024) + +int btr_node_status(struct btr_node *btrn, size_t min_iqs, + enum btr_node_type type) +{ + size_t iqs; + + if (type != BTR_NT_LEAF) { + if (btr_no_children(btrn)) + return -E_BTR_NO_CHILD; + if (btr_bytes_pending(btrn) > BTRN_MAX_PENDING) + return 0; + } + if (type != BTR_NT_ROOT) { + if (btr_eof(btrn)) + return -E_BTR_EOF; + iqs = btr_get_input_queue_size(btrn); + if (iqs == 0) /* we have a parent, because not eof */ + return 0; + if (iqs < min_iqs && !btr_no_parent(btrn)) + return 0; + } + return 1; +} + +void btr_get_node_start(struct btr_node *btrn, struct timeval *tv) +{ + *tv = btrn->start; +}