1 #include <ccan/lbalance/lbalance.h>
2 #include <ccan/lbalance/lbalance.c>
3 #include <ccan/time/time.h>
4 #include <ccan/jmap/jmap.h>
9 #include <sys/resource.h>
13 JMAP_MEMBERS(unsigned int, struct lbalance_task *);
16 /* Figure out how many loops we need to run for about 1 second. */
17 static unsigned long burn_count;
19 static void calibrate_burn_cpu(void)
21 struct timeabs start = time_now();
23 while (time_before(time_now(),
24 timeabs_add(start, time_from_msec(1000))))
26 printf("Burn count = %lu\n", burn_count);
29 static void burn_cpu(void)
31 unsigned int i, after = 0;
32 struct timeabs start = time_now();
34 /* We do a loop similar to the calibrate_burn_cpu loop. */
35 for (i = 0; i < burn_count; i++) {
36 after += time_before(time_now(),
37 timeabs_add(start, time_from_msec(1000)));
39 /* We use the result so the compiler can't discard it. */
43 static pid_t spawn(char *args[])
52 execvp(args[0], args);
53 err(1, "exec failed");
58 int main(int argc, char *argv[])
60 unsigned int num, fixed_target = 0, num_done = 0, num_running = 0;
62 struct jmap_task *tasks = jmap_new(struct jmap_task);
66 "Usage: lbalance --fixed=<num> <num> [<command>...]\n"
67 "OR: lbalance <num> [<command>...]\n");
71 if (strncmp(argv[1], "--fixed=", strlen("--fixed=")) == 0) {
72 fixed_target = atoi(argv[1] + strlen("--fixed="));
74 errx(1, "Need positive number after --fixed");
88 while (num_done < num) {
89 unsigned int target = fixed_target;
90 struct lbalance_task *task;
95 target = lbalance_target(lb);
96 printf("(%u)", target);
99 while (num_running < target && num_done + num_running < num) {
102 task = lbalance_task_new(lb);
105 jmap_add(tasks, pid, task);
107 printf("+"); fflush(stdout);
110 /* Now wait for something to die! */
111 pid = wait3(NULL, 0, &ru);
112 task = jmap_get(tasks, pid);
114 lbalance_task_free(task, &ru);
117 printf("-"); fflush(stdout);