1 /* Licensed under GPLv3+ - see LICENSE file for details */
7 * lbalance - helpers for loadbalancing parallel tasks
9 * This code helps when you have a large number of one-shot tasks; it tries
10 * to determine the maximum amount of useful parallelism.
13 * Author: Rusty Russell <rusty@rustcorp.com.au>
16 * // Run 1000 of the given commandline at best-known parallel rate.
17 * // See tools/lbalance.c for a sligtly more serious example.
18 * #include <ccan/lbalance/lbalance.h>
19 * #include <sys/types.h>
20 * #include <sys/time.h>
21 * #include <sys/resource.h>
22 * #include <sys/wait.h>
27 * static pid_t spawn(char *args[])
34 * execvp(args[0], args);
35 * err(1, "exec failed");
40 * int main(int argc, char *argv[])
42 * unsigned int num = 0, num_running = 0;
44 * struct lbalance_task *tasks[MAX];
45 * struct lbalance *lb;
48 * errx(1, "Usage: %s cmdline...", argv[0]);
50 * lb = lbalance_new();
52 * while (num - num_running < MAX) {
57 * // Make sure we're running as many as lbalance says to.
58 * while (num_running < lbalance_target(lb) && num < MAX) {
59 * pids[num] = spawn(argv+1);
60 * tasks[num] = lbalance_task_new(lb);
65 * // Now wait for something to die.
66 * pid = wait3(NULL, 0, &ru);
67 * // Find it, tell lbalance it's finished.
68 * for (i = 0; i < num; i++) {
69 * if (pids[i] == pid) {
70 * lbalance_task_free(tasks[i], &ru);
81 int main(int argc, char *argv[])
83 /* Expect exactly one argument */
87 if (strcmp(argv[1], "depends") == 0) {
88 printf("ccan/tlist\n");