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