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