3 #include <sys/resource.h>
7 static int fake_gettimeofday(struct timeval *tv, struct timezone *tz);
8 static int fake_getrusage(int who, struct rusage *usage);
9 #define gettimeofday fake_gettimeofday
10 #define getrusage fake_getrusage
12 #include <ccan/lbalance/lbalance.c>
13 #include <ccan/tap/tap.h>
15 static unsigned faketime_ms = 0;
16 static struct rusage total_usage;
18 static int fake_gettimeofday(struct timeval *tv, struct timezone *tz)
21 tv->tv_usec = (faketime_ms % 1000) * 1000;
22 tv->tv_sec = faketime_ms / 1000;
26 static int fake_getrusage(int who, struct rusage *usage)
28 assert(who == RUSAGE_CHILDREN);
33 static void test_optimum(struct lbalance *lb, unsigned int optimum)
35 unsigned int j, i, num_tasks = 0, usec, num_counted = 0;
37 struct lbalance_task *tasks[1000];
39 for (j = 0; j < 1000; j++) {
40 diag("lbalance_target is %u\n", lbalance_target(lb));
41 /* We measure average once we try optimum once. */
42 if (lbalance_target(lb) == optimum && num_counted == 0) {
43 average = lbalance_target(lb);
45 } else if (num_counted) {
46 average += lbalance_target(lb);
50 /* Create tasks until we reach target. */
51 for (i = 0; i < lbalance_target(lb); i++) {
52 tasks[i] = lbalance_task_new(lb);
57 /* If we're under optimum, set utilization to 100% */
58 if (num_tasks <= optimum) {
61 usec = 100000 * optimum / num_tasks;
64 for (i = 0; i < num_tasks; i++) {
65 total_usage.ru_utime.tv_usec += usec / 2;
66 if (total_usage.ru_utime.tv_usec > 1000000) {
67 total_usage.ru_utime.tv_usec -= 1000000;
68 total_usage.ru_utime.tv_sec++;
70 total_usage.ru_stime.tv_usec += usec / 2;
71 if (total_usage.ru_stime.tv_usec > 1000000) {
72 total_usage.ru_stime.tv_usec -= 1000000;
73 total_usage.ru_stime.tv_sec++;
75 lbalance_task_free(tasks[i], NULL);
79 /* We should have stayed close to optimum. */
80 ok1(num_counted && (int)(average / num_counted + 0.5) == optimum);