1 #ifndef CCAN_LBALANCE_H
2 #define CCAN_LBALANCE_H
11 * lbalance_new - initialize a load balancing structure.
14 * struct lbalance *lb = lbalance_new();
21 struct lbalance *lbalance_new(void);
24 * lbalance_free - free a load balancing structure.
25 * @lbalance: the load balancer from lbalance_new.
27 * Also frees any tasks still attached.
29 void lbalance_free(struct lbalance *lbalance);
32 * lbalance_task_new - mark the starting of a new task.
33 * @lbalance: the load balancer from lbalance_new.
36 * static pid_t run_child(struct lbalance *lb, struct lbalance_task **task)
40 * // We are the parent, return.
41 * *task = lbalance_task_new(lb);
44 * // otherwise do some work...
48 struct lbalance_task *lbalance_task_new(struct lbalance *lbalance);
51 * lbalance_task_free - mark the completion of a task.
52 * @task: the lbalance_task from lbalance_task_new, which will be freed.
53 * @usage: the resource usage for that task (or NULL).
55 * If @usage is NULL, you must have already wait()ed for the child so
56 * that lbalance_task_free() can derive it from the difference in
57 * getrusage() for the child processes.
59 * Otherwise, lbalance_task_free() is a noop, which is useful for failure
63 * #include <sys/types.h>
64 * #include <sys/time.h>
65 * #include <sys/resource.h>
66 * #include <sys/wait.h>
68 * static void wait_for_child(struct lbalance_task *task)
71 * // Wait for child to finish, get usage.
72 * wait4(-1, NULL, 0, &ru);
73 * // Tell lbalancer about usage, free struct lbalance_task.
74 * lbalance_task_free(task, &ru);
77 void lbalance_task_free(struct lbalance_task *task,
78 const struct rusage *usage);
81 * lbalance_target - how many tasks in parallel are recommended?
82 * @lbalance: the load balancer from lbalance_new.
84 * Normally you keep creating tasks until this limit is reached. It's
85 * updated by stats from lbalance_task_free.
90 * unsigned int num_running = 0;
91 * struct lbalance *lb = lbalance_new();
94 * // Run more until we reach target.
95 * while (num_running < lbalance_target(lb)) {
99 * // Wait for something to finish.
103 unsigned lbalance_target(struct lbalance *lbalance);
105 #endif /* CCAN_LBALANCE_H */