1 #include <ccan/lbalance/lbalance.h>
2 #include <ccan/lbalance/lbalance.c>
3 #include <ccan/time/time.h>
4 #include <ccan/jmap/jmap_type.h>
8 /* Defines struct jmap_task. */
9 JMAP_DEFINE_UINTIDX_TYPE(struct lbalance_task, task);
11 /* Figure out how many loops we need to run for about 1 second. */
12 static unsigned long burn_count;
14 static void calibrate_burn_cpu(void)
16 struct timeval start = time_now();
18 while (time_less(time_now(), time_add(start, time_from_msec(1000))))
20 printf("Burn count = %lu\n", burn_count);
23 static void burn_cpu(void)
25 unsigned int i, after = 0;
26 struct timeval start = time_now();
28 /* We do a loop similar to the calibrate_burn_cpu loop. */
29 for (i = 0; i < burn_count; i++) {
30 after += time_less(time_now(),
31 time_add(start, time_from_msec(1000)));
33 /* We use the result so the compiler can't discard it. */
37 static pid_t spawn(char *args[])
46 execvp(args[0], args);
47 err(1, "exec failed");
52 int main(int argc, char *argv[])
54 unsigned int i, num, fixed_target = 0, num_done = 0, num_running = 0;
56 struct jmap_task *tasks = jmap_task_new();
60 "Usage: lbalance --fixed=<num> <num> [<command>...]\n"
61 "OR: lbalance <num> [<command>...]\n");
65 if (strncmp(argv[1], "--fixed=", strlen("--fixed=")) == 0) {
66 fixed_target = atoi(argv[1] + strlen("--fixed="));
68 errx(1, "Need positive number after --fixed");
82 while (num_done < num) {
83 unsigned int j, target = fixed_target;
84 struct lbalance_task *task;
89 target = lbalance_target(lb);
90 printf("(%u)", target);
93 while (num_running < target && num_done + num_running < num) {
96 task = lbalance_task_new(lb);
99 jmap_task_add(tasks, pid, task);
101 printf("+"); fflush(stdout);
104 /* Now wait for something to die! */
105 pid = wait3(NULL, 0, &ru);
106 task = jmap_task_get(tasks, pid);
108 lbalance_task_free(task, &ru);
111 printf("-"); fflush(stdout);