lbalance.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* Licensed under GPLv3+ - see LICENSE file for details */
  2. #include <ccan/lbalance/lbalance.h>
  3. #include <ccan/tlist/tlist.h>
  4. #include <sys/time.h>
  5. #include <sys/resource.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <assert.h>
  9. #include <stdlib.h>
  10. /* Define tlist_lbalance_task */
  11. TLIST_TYPE(lbalance_task, struct lbalance_task);
  12. struct stats {
  13. /* How many stats of for this value do we have? */
  14. unsigned int num_stats;
  15. /* What was our total work rate? */
  16. float work_rate;
  17. };
  18. struct lbalance {
  19. struct tlist_lbalance_task tasks;
  20. unsigned int num_tasks;
  21. /* We figured out how many we want to run. */
  22. unsigned int target;
  23. /* We need to recalc once a report comes in via lbalance_task_free. */
  24. bool target_uptodate;
  25. /* Integral of how many tasks were running so far */
  26. struct timeval prev_tasks_time;
  27. float tasks_sum;
  28. /* For differential rusage. */
  29. struct rusage prev_usage;
  30. /* How many stats we have collected (we invalidate old ones). */
  31. unsigned int total_stats;
  32. /* Array of stats, indexed by number of tasks we were running. */
  33. unsigned int max_stats;
  34. struct stats *stats;
  35. };
  36. struct lbalance_task {
  37. struct lbalance *lb;
  38. struct list_node list;
  39. /* The time this task started */
  40. struct timeval start;
  41. float tasks_sum_start;
  42. };
  43. struct lbalance *lbalance_new(void)
  44. {
  45. struct lbalance *lb = malloc(sizeof *lb);
  46. if (!lb)
  47. return NULL;
  48. tlist_init(&lb->tasks);
  49. lb->num_tasks = 0;
  50. gettimeofday(&lb->prev_tasks_time, NULL);
  51. lb->tasks_sum = 0.0;
  52. getrusage(RUSAGE_CHILDREN, &lb->prev_usage);
  53. lb->max_stats = 1;
  54. lb->stats = malloc(sizeof(lb->stats[0]) * lb->max_stats);
  55. if (!lb->stats) {
  56. free(lb);
  57. return NULL;
  58. }
  59. lb->stats[0].num_stats = 0;
  60. lb->stats[0].work_rate = 0.0;
  61. lb->total_stats = 0;
  62. /* Start with # CPUS as a guess. */
  63. lb->target = -1L;
  64. #ifdef _SC_NPROCESSORS_ONLN
  65. lb->target = sysconf(_SC_NPROCESSORS_ONLN);
  66. #elif defined(_SC_NPROCESSORS_CONF)
  67. if (lb->target == (unsigned int)-1L)
  68. lb->target = sysconf(_SC_NPROCESSORS_CONF);
  69. #endif
  70. /* Otherwise, two is a good number. */
  71. if (lb->target == (unsigned int)-1L || lb->target < 2)
  72. lb->target = 2;
  73. lb->target_uptodate = true;
  74. return lb;
  75. }
  76. /* Return time differences in usec */
  77. static float timeval_sub(struct timeval recent, struct timeval old)
  78. {
  79. float diff;
  80. if (old.tv_usec > recent.tv_usec) {
  81. diff = 1000000 + recent.tv_usec - old.tv_usec;
  82. recent.tv_sec--;
  83. } else
  84. diff = recent.tv_usec - old.tv_usec;
  85. diff += (float)(recent.tv_sec - old.tv_sec) * 1000000;
  86. return diff;
  87. }
  88. /* There were num_tasks running between prev_tasks_time and now. */
  89. static void update_tasks_sum(struct lbalance *lb,
  90. const struct timeval *now)
  91. {
  92. lb->tasks_sum += timeval_sub(*now, lb->prev_tasks_time)
  93. * lb->num_tasks;
  94. lb->prev_tasks_time = *now;
  95. }
  96. struct lbalance_task *lbalance_task_new(struct lbalance *lb)
  97. {
  98. struct lbalance_task *task = malloc(sizeof *task);
  99. if (!task)
  100. return NULL;
  101. if (lb->num_tasks + 1 == lb->max_stats) {
  102. struct stats *s = realloc(lb->stats,
  103. sizeof(*s) * (lb->max_stats + 1));
  104. if (!s) {
  105. free(task);
  106. return NULL;
  107. }
  108. lb->stats = s;
  109. lb->stats[lb->max_stats].num_stats = 0;
  110. lb->stats[lb->max_stats].work_rate = 0.0;
  111. lb->max_stats++;
  112. }
  113. task->lb = lb;
  114. gettimeofday(&task->start, NULL);
  115. /* Record that we ran num_tasks up until now. */
  116. update_tasks_sum(lb, &task->start);
  117. task->tasks_sum_start = lb->tasks_sum;
  118. tlist_add_tail(&lb->tasks, task, list);
  119. lb->num_tasks++;
  120. return task;
  121. }
  122. /* We slowly erase old stats, once we have enough. */
  123. static void degrade_stats(struct lbalance *lb)
  124. {
  125. unsigned int i;
  126. if (lb->total_stats < lb->max_stats * 16)
  127. return;
  128. #if 0
  129. fprintf(stderr, ".");
  130. #endif
  131. for (i = 0; i < lb->max_stats; i++) {
  132. struct stats *s = &lb->stats[i];
  133. unsigned int stats_lost = (s->num_stats + 1) / 2;
  134. s->work_rate *= (float)(s->num_stats - stats_lost)
  135. / s->num_stats;
  136. s->num_stats -= stats_lost;
  137. lb->total_stats -= stats_lost;
  138. if (s->num_stats == 0)
  139. s->work_rate = 0.0;
  140. }
  141. }
  142. static void add_to_stats(struct lbalance *lb,
  143. unsigned int num_tasks,
  144. float work_rate)
  145. {
  146. #if 0
  147. fprintf(stderr, "With %.2f running, work rate was %.5f\n",
  148. num_tasks, work_rate);
  149. #endif
  150. assert(num_tasks >= 1);
  151. assert(num_tasks < lb->max_stats);
  152. lb->stats[num_tasks].num_stats++;
  153. lb->stats[num_tasks].work_rate += work_rate;
  154. lb->total_stats++;
  155. lb->target_uptodate = false;
  156. }
  157. void lbalance_task_free(struct lbalance_task *task,
  158. const struct rusage *usage)
  159. {
  160. float work_done, duration;
  161. unsigned int num_tasks;
  162. struct timeval now;
  163. struct rusage ru;
  164. gettimeofday(&now, NULL);
  165. duration = timeval_sub(now, task->start);
  166. getrusage(RUSAGE_CHILDREN, &ru);
  167. if (usage) {
  168. work_done = usage->ru_utime.tv_usec + usage->ru_stime.tv_usec
  169. + (usage->ru_utime.tv_sec + usage->ru_stime.tv_sec)
  170. * 1000000;
  171. } else {
  172. /* Take difference in rusage as rusage of that task. */
  173. work_done = timeval_sub(ru.ru_utime,
  174. task->lb->prev_usage.ru_utime)
  175. + timeval_sub(ru.ru_stime,
  176. task->lb->prev_usage.ru_utime);
  177. }
  178. /* Update previous usage. */
  179. task->lb->prev_usage = ru;
  180. /* Record that we ran num_tasks up until now. */
  181. update_tasks_sum(task->lb, &now);
  182. /* So, on average, how many tasks were running during this time? */
  183. num_tasks = (task->lb->tasks_sum - task->tasks_sum_start)
  184. / duration + 0.5;
  185. /* Record the work rate for that many tasks. */
  186. add_to_stats(task->lb, num_tasks, work_done / duration);
  187. /* We throw away old stats. */
  188. degrade_stats(task->lb);
  189. /* We need to recalculate the target. */
  190. task->lb->target_uptodate = false;
  191. /* Remove this task. */
  192. tlist_del_from(&task->lb->tasks, task, list);
  193. task->lb->num_tasks--;
  194. free(task);
  195. }
  196. /* We look for the point where the work rate starts to drop. Say you have
  197. * 4 cpus, we'd expect the work rate for 5 processes to drop 20%.
  198. *
  199. * If we're within 1/4 of that ideal ratio, we assume it's still
  200. * optimal. Any drop of more than 1/2 is interpreted as the point we
  201. * are overloaded. */
  202. static unsigned int best_target(const struct lbalance *lb)
  203. {
  204. unsigned int i, found_drop = 0;
  205. float best_f_max = -1.0, cliff = -1.0;
  206. #if 0
  207. for (i = 1; i < lb->max_stats; i++) {
  208. printf("%u: %f (%u)\n", i,
  209. lb->stats[i].work_rate / lb->stats[i].num_stats,
  210. lb->stats[i].num_stats);
  211. }
  212. #endif
  213. for (i = 1; i < lb->max_stats; i++) {
  214. float f;
  215. if (!lb->stats[i].num_stats)
  216. f = 0;
  217. else
  218. f = lb->stats[i].work_rate / lb->stats[i].num_stats;
  219. if (f > best_f_max) {
  220. #if 0
  221. printf("Best is %i\n", i);
  222. #endif
  223. best_f_max = f - (f / (i + 1)) / 4;
  224. cliff = f - (f / (i + 1)) / 2;
  225. found_drop = 0;
  226. } else if (!found_drop && f < cliff) {
  227. #if 0
  228. printf("Found drop at %i\n", i);
  229. #endif
  230. found_drop = i;
  231. }
  232. }
  233. if (found_drop) {
  234. return found_drop - 1;
  235. }
  236. return i - 1;
  237. }
  238. static unsigned int calculate_target(struct lbalance *lb)
  239. {
  240. unsigned int target;
  241. target = best_target(lb);
  242. /* Jitter if the adjacent ones are unknown. */
  243. if (target >= lb->max_stats || lb->stats[target].num_stats == 0)
  244. return target;
  245. if (target + 1 == lb->max_stats || lb->stats[target+1].num_stats == 0)
  246. return target + 1;
  247. if (target > 1 && lb->stats[target-1].num_stats == 0)
  248. return target - 1;
  249. return target;
  250. }
  251. unsigned lbalance_target(struct lbalance *lb)
  252. {
  253. if (!lb->target_uptodate) {
  254. lb->target = calculate_target(lb);
  255. lb->target_uptodate = true;
  256. }
  257. return lb->target;
  258. }
  259. void lbalance_free(struct lbalance *lb)
  260. {
  261. struct lbalance_task *task;
  262. while ((task = tlist_top(&lb->tasks, list))) {
  263. assert(task->lb == lb);
  264. tlist_del_from(&lb->tasks, task, list);
  265. lb->num_tasks--;
  266. free(task);
  267. }
  268. assert(lb->num_tasks == 0);
  269. free(lb->stats);
  270. free(lb);
  271. }