Switch audiod over to the buffer tree API.
[paraslash.git] / buffer_tree.c
1 #include <regex.h>
2 #include <stdbool.h>
3
4 #include "para.h"
5 #include "list.h"
6 #include "string.h"
7 #include "buffer_tree.h"
8 #include "error.h"
9 #include "sched.h"
10
11
12 struct btr_buffer {
13 char *buf;
14 size_t size;
15 /** The number of references to this buffer. */
16 int refcount;
17 };
18
19 struct btr_buffer_reference {
20 struct btr_buffer *btrb;
21 size_t consumed;
22 /* Each buffer reference belongs to the buffer queue list of some buffer tree node. */
23 struct list_head node;
24 };
25
26 struct btr_node {
27 char *name;
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. */
34 struct timeval start;
35 /**
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.
39 */
40 struct list_head input_queue;
41 btr_command_handler execute;
42 void *context;
43 };
44
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)
49
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)
54
55 struct btr_node *btr_new_node(const char *name, struct btr_node *parent,
56 btr_command_handler handler, void *context)
57 {
58 struct btr_node *btrn = para_malloc(sizeof(*btrn));
59
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;
66 if (parent)
67 list_add_tail(&btrn->node, &parent->children);
68 INIT_LIST_HEAD(&btrn->children);
69 INIT_LIST_HEAD(&btrn->input_queue);
70 if (parent)
71 PARA_INFO_LOG("added %s as child of %s\n", name, parent->name);
72 else
73 PARA_INFO_LOG("added %s as btr root\n", name);
74 return btrn;
75 }
76
77 /*
78 * Allocate a new btr buffer.
79 *
80 * The freshly allocated buffer will have a zero refcount.
81 */
82 static struct btr_buffer *new_btrb(char *buf, size_t size)
83 {
84 struct btr_buffer *btrb = para_malloc(sizeof(*btrb));
85
86 btrb->buf = buf;
87 btrb->size = size;
88 btrb->refcount = 0;
89 return btrb;
90 }
91
92 /*
93 * Deallocate the reference, release the resources if refcount drops to zero.
94 */
95 static void btr_drop_buffer_reference(struct btr_buffer_reference *br)
96 {
97 struct btr_buffer *btrb = br->btrb;
98
99 //PARA_CRIT_LOG("dropping buffer reference %p\n", br);
100 list_del(&br->node);
101 free(br);
102 btrb->refcount--;
103 if (btrb->refcount == 0) {
104 free(btrb->buf);
105 free(btrb);
106 }
107 }
108
109 static void add_btrb_to_children(struct btr_buffer *btrb,
110 struct btr_node *btrn, size_t consumed)
111 {
112 struct btr_node *ch;
113
114 if (btrn->start.tv_sec == 0)
115 btrn->start = *now;
116 FOR_EACH_CHILD(ch, btrn) {
117 struct btr_buffer_reference *br = para_malloc(sizeof(*br));
118 br->btrb = btrb;
119 br->consumed = consumed;
120 list_add_tail(&br->node, &ch->input_queue);
121 btrb->refcount++;
122 if (ch->start.tv_sec == 0)
123 ch->start = *now;
124 }
125 }
126
127 void btr_add_output(char *buf, size_t size, struct btr_node *btrn)
128 {
129 struct btr_buffer *btrb;
130
131 assert(size != 0);
132 if (list_empty(&btrn->children)) {
133 free(buf);
134 return;
135 }
136 btrb = new_btrb(buf, size);
137 add_btrb_to_children(btrb, btrn, 0);
138 }
139
140 static void btr_pushdown_br(struct btr_buffer_reference *br, struct btr_node *btrn)
141 {
142 add_btrb_to_children(br->btrb, btrn, br->consumed);
143 btr_drop_buffer_reference(br);
144 }
145
146 void btr_pushdown(struct btr_node *btrn)
147 {
148 struct btr_buffer_reference *br, *tmp;
149
150 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
151 btr_pushdown_br(br, btrn);
152 }
153
154 int btr_pushdown_one(struct btr_node *btrn)
155 {
156 struct btr_buffer_reference *br;
157
158 if (list_empty(&btrn->input_queue))
159 return 0;
160 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
161 btr_pushdown_br(br, btrn);
162 return 1;
163 }
164
165 /* Return true if this node has no children. */
166 bool btr_no_children(struct btr_node *btrn)
167 {
168 return list_empty(&btrn->children);
169 }
170
171 bool btr_no_parent(struct btr_node *btrn)
172 {
173 return !btrn->parent;
174 }
175
176 bool btr_inplace_ok(struct btr_node *btrn)
177 {
178 if (!btrn->parent)
179 return true;
180 return list_is_singular(&btrn->parent->children);
181 }
182
183 static inline size_t br_available_bytes(struct btr_buffer_reference *br)
184 {
185 return br->btrb->size - br->consumed;
186 }
187
188 size_t btr_get_buffer_by_reference(struct btr_buffer_reference *br, char **buf)
189 {
190 *buf = br->btrb->buf + br->consumed;
191 return br_available_bytes(br);
192 }
193
194 /**
195 * \return zero if the input buffer queue is empty.
196 */
197 size_t btr_next_buffer(struct btr_node *btrn, char **bufp)
198 {
199 struct btr_buffer_reference *br;
200
201 if (list_empty(&btrn->input_queue)) {
202 *bufp = NULL;
203 return 0;
204 }
205 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
206 return btr_get_buffer_by_reference(br, bufp);
207 }
208
209 void btr_consume(struct btr_node *btrn, size_t numbytes)
210 {
211 struct btr_buffer_reference *br;
212
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);
219 }
220
221 static void flush_input_queue(struct btr_node *btrn)
222 {
223 struct btr_buffer_reference *br, *tmp;
224 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
225 btr_drop_buffer_reference(br);
226 }
227
228 void btr_free_node(struct btr_node *btrn)
229 {
230 if (!btrn)
231 return;
232 free(btrn->name);
233 free(btrn);
234 }
235
236 void btr_remove_node(struct btr_node *btrn)
237 {
238 struct btr_node *ch;
239
240 if (!btrn)
241 return;
242 PARA_NOTICE_LOG("removing btr node %s from buffer tree\n", btrn->name);
243 FOR_EACH_CHILD(ch, btrn)
244 ch->parent = NULL;
245 flush_input_queue(btrn);
246 if (btrn->parent)
247 list_del(&btrn->node);
248 }
249
250 size_t btr_get_input_queue_size(struct btr_node *btrn)
251 {
252 struct btr_buffer_reference *br;
253 size_t size = 0;
254
255 FOR_EACH_BUFFER_REF(br, btrn) {
256 //PARA_CRIT_LOG("size: %zu\n", size);
257 size += br_available_bytes(br);
258 }
259 return size;
260 }
261
262 void btr_splice_out_node(struct btr_node *btrn)
263 {
264 struct btr_node *ch, *tmp;
265
266 assert(btrn);
267 PARA_NOTICE_LOG("splicing out %s\n", btrn->name);
268 btr_pushdown(btrn);
269 if (btrn->parent)
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;
275 if (btrn->parent)
276 list_move(&ch->node, &btrn->parent->children);
277 }
278 assert(list_empty(&btrn->children));
279 }
280
281 /**
282 * Return the size of the largest input queue.
283 *
284 * Iterates over all children of the given node.
285 */
286 size_t btr_bytes_pending(struct btr_node *btrn)
287 {
288 size_t max_size = 0;
289 struct btr_node *ch;
290
291 FOR_EACH_CHILD(ch, btrn) {
292 size_t size = btr_get_input_queue_size(ch);
293 max_size = PARA_MAX(max_size, size);
294 }
295 return max_size;
296 }
297
298 int btr_exec(struct btr_node *btrn, const char *command, char **value_result)
299 {
300 if (!btrn)
301 return -ERRNO_TO_PARA_ERROR(EINVAL);
302 if (!btrn->execute)
303 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
304 return btrn->execute(btrn, command, value_result);
305 }
306
307 int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
308 {
309 int ret;
310
311 for (; btrn; btrn = btrn->parent) {
312 struct btr_node *parent = btrn->parent;
313 if (!parent)
314 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
315 if (!parent->execute)
316 continue;
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))
320 continue;
321 if (ret < 0)
322 return ret;
323 if (value_result && *value_result)
324 PARA_NOTICE_LOG("%s(%s): %s\n", command, parent->name,
325 *value_result);
326 return 1;
327 }
328 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
329 }
330
331 void *btr_context(struct btr_node *btrn)
332 {
333 return btrn->context;
334 }
335
336 /**
337 * Merge the first two input buffers into one.
338 *
339 * This is a quite expensive operation.
340 *
341 * \return The number of buffers that have been available (zero, one or two).
342 */
343 static int merge_input(struct btr_node *btrn)
344 {
345 struct btr_buffer_reference *brs[2], *br;
346 char *bufs[2], *buf;
347 size_t szs[2], sz;
348 int i;
349
350 if (list_empty(&btrn->input_queue))
351 return 0;
352 if (list_is_singular(&btrn->input_queue))
353 return 1;
354 i = 0;
355 /* get references to the first two buffers */
356 FOR_EACH_BUFFER_REF(br, btrn) {
357 brs[i] = br;
358 szs[i] = btr_get_buffer_by_reference(brs[i], bufs + i);
359 i++;
360 if (i == 2)
361 break;
362 }
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",
366 szs[0], szs[1], sz);
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]);
371
372 br = para_malloc(sizeof(*br));
373 br->btrb = new_btrb(buf, sz);
374 br->btrb->refcount = 1;
375 br->consumed = 0;
376
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);
381 return 2;
382 }
383
384 void btr_merge(struct btr_node *btrn, size_t dest_size)
385 {
386 for (;;) {
387 char *buf;
388 size_t len = btr_next_buffer(btrn, &buf);
389 if (len >= dest_size)
390 return;
391 PARA_DEBUG_LOG("input size = %zu < %zu = dest\n", len, dest_size);
392 if (merge_input(btrn) < 2)
393 return;
394 }
395 }
396
397 bool btr_eof(struct btr_node *btrn)
398 {
399 char *buf;
400 size_t len = btr_next_buffer(btrn, &buf);
401
402 return (len == 0 && btr_no_parent(btrn));
403 }
404
405 void log_tree_recursively(struct btr_node *btrn, int loglevel, int depth)
406 {
407 struct btr_node *ch;
408 const char spaces[] = " ", *space = spaces + 16 - depth;
409
410 if (depth > 16)
411 return;
412 para_log(loglevel, "%s%s\n", space, btrn->name);
413 FOR_EACH_CHILD(ch, btrn)
414 log_tree_recursively(ch, loglevel, depth + 1);
415 }
416
417 void btr_log_tree(struct btr_node *btrn, int loglevel)
418 {
419 return log_tree_recursively(btrn, loglevel, 0);
420 }
421
422 /** 640K ought to be enough for everybody ;) */
423 #define BTRN_MAX_PENDING (640 * 1024)
424
425 int btr_node_status(struct btr_node *btrn, size_t min_iqs,
426 enum btr_node_type type)
427 {
428 size_t iqs;
429
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)
434 return 0;
435 }
436 if (type != BTR_NT_ROOT) {
437 if (btr_eof(btrn))
438 return -E_BTR_EOF;
439 iqs = btr_get_input_queue_size(btrn);
440 if (iqs == 0) /* we have a parent, because not eof */
441 return 0;
442 if (iqs < min_iqs && !btr_no_parent(btrn))
443 return 0;
444 }
445 return 1;
446 }
447
448 void btr_get_node_start(struct btr_node *btrn, struct timeval *tv)
449 {
450 *tv = btrn->start;
451 }