1 /* Licensed under GPLv3+ - see LICENSE file for details */
2 #ifndef CCAN_LBALANCE_H
3 #define CCAN_LBALANCE_H
12 * lbalance_new - initialize a load balancing structure.
15 * struct lbalance *lb = lbalance_new();
22 struct lbalance *lbalance_new(void);
25 * lbalance_free - free a load balancing structure.
26 * @lbalance: the load balancer from lbalance_new.
28 * Also frees any tasks still attached.
30 void lbalance_free(struct lbalance *lbalance);
33 * lbalance_task_new - mark the starting of a new task.
34 * @lbalance: the load balancer from lbalance_new.
37 * static pid_t run_child(struct lbalance *lb, struct lbalance_task **task)
41 * // We are the parent, return.
42 * *task = lbalance_task_new(lb);
45 * // otherwise do some work...
49 struct lbalance_task *lbalance_task_new(struct lbalance *lbalance);
52 * lbalance_task_free - mark the completion of a task.
53 * @task: the lbalance_task from lbalance_task_new, which will be freed.
54 * @usage: the resource usage for that task (or NULL).
56 * If @usage is NULL, you must have already wait()ed for the child so
57 * that lbalance_task_free() can derive it from the difference in
58 * getrusage() for the child processes.
60 * Otherwise, lbalance_task_free() is a noop, which is useful for failure
64 * #include <sys/types.h>
65 * #include <sys/time.h>
66 * #include <sys/resource.h>
67 * #include <sys/wait.h>
69 * static void wait_for_child(struct lbalance_task *task)
72 * // Wait for child to finish, get usage.
73 * wait4(-1, NULL, 0, &ru);
74 * // Tell lbalancer about usage, free struct lbalance_task.
75 * lbalance_task_free(task, &ru);
78 void lbalance_task_free(struct lbalance_task *task,
79 const struct rusage *usage);
82 * lbalance_target - how many tasks in parallel are recommended?
83 * @lbalance: the load balancer from lbalance_new.
85 * Normally you keep creating tasks until this limit is reached. It's
86 * updated by stats from lbalance_task_free.
91 * unsigned int num_running = 0;
92 * struct lbalance *lb = lbalance_new();
95 * // Run more until we reach target.
96 * while (num_running < lbalance_target(lb)) {
100 * // Wait for something to finish.
104 unsigned lbalance_target(struct lbalance *lbalance);
106 #endif /* CCAN_LBALANCE_H */