]> git.ozlabs.org Git - ccan/blob - ccan/lbalance/_info
lbalance: Switch to tlist2
[ccan] / ccan / lbalance / _info
1 /* Licensed under GPLv3+ - see LICENSE file for details */
2 #include "config.h"
3 #include <stdio.h>
4 #include <string.h>
5
6 /**
7  * lbalance - helpers for loadbalancing parallel tasks
8  *
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.
11  *
12  * License: GPL
13  * Author: Rusty Russell <rusty@rustcorp.com.au>
14  *
15  * Example:
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>
23  *      #include <err.h>
24  *
25  *      #define MAX 1000
26  *
27  *      static pid_t spawn(char *args[])
28  *      {
29  *              pid_t pid = fork();
30  *
31  *              if (pid == -1)
32  *                      err(1, "forking");
33  *              if (pid == 0) {
34  *                      execvp(args[0], args);
35  *                      err(1, "exec failed");
36  *              }
37  *              return pid;
38  *      }
39  *
40  *      int main(int argc, char *argv[])
41  *      {
42  *              unsigned int num = 0, num_running = 0;
43  *              pid_t pids[MAX];
44  *              struct lbalance_task *tasks[MAX];
45  *              struct lbalance *lb;
46  *
47  *              if (argc == 1)
48  *                      errx(1, "Usage: %s cmdline...", argv[0]);
49  *
50  *              lb = lbalance_new();
51  *
52  *              while (num - num_running < MAX) {
53  *                      struct rusage ru;
54  *                      pid_t pid;
55  *                      unsigned int i;
56  *
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);
61  *                              num++;
62  *                              num_running++;
63  *                      }
64  *
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);
71  *                                      pids[i] = 0;
72  *                                      break;
73  *                              }
74  *                      }
75  *                      num_running--;
76  *              }
77  *              lbalance_free(lb);
78  *              return 0;
79  *      }
80  */
81 int main(int argc, char *argv[])
82 {
83         /* Expect exactly one argument */
84         if (argc != 2)
85                 return 1;
86
87         if (strcmp(argv[1], "depends") == 0) {
88                 printf("ccan/tlist2\n");
89                 return 0;
90         }
91
92         return 1;
93 }