]> git.ozlabs.org Git - ccan/blob - ccan/lbalance/_info
lbalance: add examples.
[ccan] / ccan / lbalance / _info
1 #include "config.h"
2 #include <string.h>
3
4 /**
5  * lbalance - helpers for loadbalancing parallel tasks
6  *
7  * This code helps when you have a large number of one-shot tasks; it tries
8  * to determine the maximum amount of useful parallelism.
9  *
10  * License: GPL
11  * Author: Rusty Russell <rusty@rustcorp.com.au>
12  *
13  * Example:
14  *      // Run 1000 of the given commandline at best-known parallel rate.
15  *      // See tools/lbalance.c for a sligtly more serious example.
16  *      #include <ccan/lbalance/lbalance.h>
17  *      #include <sys/types.h>
18  *      #include <sys/time.h>
19  *      #include <sys/resource.h>
20  *      #include <sys/wait.h>
21  *      #include <err.h>
22  *
23  *      #define MAX 1000
24  *
25  *      static pid_t spawn(char *args[])
26  *      {
27  *              pid_t pid = fork();
28  *
29  *              if (pid == -1)
30  *                      err(1, "forking");
31  *              if (pid == 0) {
32  *                      execvp(args[0], args);
33  *                      err(1, "exec failed");
34  *              }
35  *              return pid;
36  *      }
37  *
38  *      int main(int argc, char *argv[])
39  *      {
40  *              unsigned int num = 0, num_running = 0;
41  *              pid_t pids[MAX];
42  *              struct lbalance_task *tasks[MAX];
43  *              struct lbalance *lb;
44  *
45  *              if (argc == 1)
46  *                      errx(1, "Usage: %s cmdline...", argv[0]);
47  *
48  *              lb = lbalance_new();
49  *
50  *              while (num - num_running < MAX) {
51  *                      struct rusage ru;
52  *                      pid_t pid;
53  *                      unsigned int i;
54  *
55  *                      // Make sure we're running as many as lbalance says to. 
56  *                      while (num_running < lbalance_target(lb) && num < MAX) {
57  *                              pids[num] = spawn(argv+1);
58  *                              tasks[num] = lbalance_task_new(lb);
59  *                              num++;
60  *                              num_running++;
61  *                      }
62  *
63  *                      // Now wait for something to die.
64  *                      pid = wait3(NULL, 0, &ru);
65  *                      // Find it, tell lbalance it's finished.
66  *                      for (i = 0; i < num; i++) {
67  *                              if (pids[i] == pid) {
68  *                                      lbalance_task_free(tasks[i], &ru);
69  *                                      pids[i] = 0;
70  *                                      break;
71  *                              }
72  *                      }
73  *                      num_running--;
74  *              }
75  *              lbalance_free(lb);
76  *              return 0;
77  *      }
78  */
79 int main(int argc, char *argv[])
80 {
81         /* Expect exactly one argument */
82         if (argc != 2)
83                 return 1;
84
85         if (strcmp(argv[1], "depends") == 0) {
86                 printf("ccan/tlist\n");
87                 return 0;
88         }
89
90         return 1;
91 }