scheduler_kist.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include <event2/event.h>
  4. #include <netinet/tcp.h>
  5. #include "or.h"
  6. #include "buffers.h"
  7. #include "config.h"
  8. #include "connection.h"
  9. #include "networkstatus.h"
  10. #define TOR_CHANNEL_INTERNAL_
  11. #include "channel.h"
  12. #include "channeltls.h"
  13. #define SCHEDULER_PRIVATE_
  14. #include "scheduler.h"
  15. #define TLS_PER_CELL_OVERHEAD 29
  16. /* Kernel interface needed for KIST. */
  17. #include <linux/sockios.h>
  18. /*****************************************************************************
  19. * Data structures and supporting functions
  20. *****************************************************************************/
  21. /* Indicate if we don't have the kernel support. This can happen if the kernel
  22. * changed and it doesn't recognized the values passed to the syscalls needed
  23. * by KIST. In that case, fallback to the naive approach. */
  24. static unsigned int kist_no_kernel_support = 0;
  25. /* Socket_table hash table stuff. The socket_table keeps track of per-socket
  26. * limit information imposed by kist and used by kist. */
  27. static uint32_t
  28. socket_table_ent_hash(const socket_table_ent_t *ent)
  29. {
  30. return (uint32_t)ent->chan->global_identifier;
  31. }
  32. static unsigned
  33. socket_table_ent_eq(const socket_table_ent_t *a, const socket_table_ent_t *b)
  34. {
  35. return a->chan->global_identifier == b->chan->global_identifier;
  36. }
  37. typedef HT_HEAD(socket_table_s, socket_table_ent_s) socket_table_t;
  38. static socket_table_t socket_table = HT_INITIALIZER();
  39. HT_PROTOTYPE(socket_table_s, socket_table_ent_s, node, socket_table_ent_hash,
  40. socket_table_ent_eq)
  41. HT_GENERATE2(socket_table_s, socket_table_ent_s, node, socket_table_ent_hash,
  42. socket_table_ent_eq, 0.6, tor_reallocarray, tor_free_)
  43. /* outbuf_table hash table stuff. The outbuf_table keeps track of which
  44. * channels have data sitting in their outbuf so the kist scheduler can force
  45. * a write from outbuf to kernel periodically during a run and at the end of a
  46. * run. */
  47. typedef struct outbuf_table_ent_s {
  48. HT_ENTRY(outbuf_table_ent_s) node;
  49. channel_t *chan;
  50. } outbuf_table_ent_t;
  51. static uint32_t
  52. outbuf_table_ent_hash(const outbuf_table_ent_t *ent)
  53. {
  54. return (uint32_t)ent->chan->global_identifier;
  55. }
  56. static unsigned
  57. outbuf_table_ent_eq(const outbuf_table_ent_t *a, const outbuf_table_ent_t *b)
  58. {
  59. return a->chan->global_identifier == b->chan->global_identifier;
  60. }
  61. static outbuf_table_t outbuf_table = HT_INITIALIZER();
  62. HT_PROTOTYPE(outbuf_table_s, outbuf_table_ent_s, node, outbuf_table_ent_hash,
  63. outbuf_table_ent_eq)
  64. HT_GENERATE2(outbuf_table_s, outbuf_table_ent_s, node, outbuf_table_ent_hash,
  65. outbuf_table_ent_eq, 0.6, tor_reallocarray, tor_free_)
  66. /*****************************************************************************
  67. * Other internal data
  68. *****************************************************************************/
  69. /* Store the last time the scheduler was run so we can decide when to next run
  70. * the scheduler based on it. */
  71. static monotime_t scheduler_last_run;
  72. /* This is a factor for the extra_space calculation in kist per-socket limits.
  73. * It is the number of extra congestion windows we want to write to the kernel.
  74. */
  75. static double sock_buf_size_factor = 1.0;
  76. /* How often the scheduler runs. */
  77. STATIC int32_t sched_run_interval = 10;
  78. /* Stores the kist scheduler function pointers. */
  79. static scheduler_t *kist_scheduler = NULL;
  80. /*****************************************************************************
  81. * Internally called function implementations
  82. *****************************************************************************/
  83. /* Little helper function to get the length of a channel's output buffer */
  84. static inline size_t
  85. channel_outbuf_length(channel_t *chan)
  86. {
  87. return buf_datalen(TO_CONN(BASE_CHAN_TO_TLS(chan)->conn)->outbuf);
  88. }
  89. /* Little helper function for HT_FOREACH_FN. */
  90. static int
  91. each_channel_write_to_kernel(outbuf_table_ent_t *ent, void *data)
  92. {
  93. (void) data; /* Make compiler happy. */
  94. channel_write_to_kernel(ent->chan);
  95. return 0; /* Returning non-zero removes the element from the table. */
  96. }
  97. /* Free the given outbuf table entry ent. */
  98. static int
  99. free_outbuf_info_by_ent(outbuf_table_ent_t *ent, void *data)
  100. {
  101. (void) data; /* Make compiler happy. */
  102. log_debug(LD_SCHED, "Freeing outbuf table entry from chan=%" PRIu64,
  103. ent->chan->global_identifier);
  104. tor_free(ent);
  105. return 1; /* So HT_FOREACH_FN will remove the element */
  106. }
  107. /* Clean up outbuf_table. Probably because the KIST sched impl is going away */
  108. static void
  109. free_all_outbuf_info(void)
  110. {
  111. HT_FOREACH_FN(outbuf_table_s, &outbuf_table, free_outbuf_info_by_ent, NULL);
  112. }
  113. /* Free the given socket table entry ent. */
  114. static int
  115. free_socket_info_by_ent(socket_table_ent_t *ent, void *data)
  116. {
  117. (void) data; /* Make compiler happy. */
  118. log_debug(LD_SCHED, "Freeing socket table entry from chan=%" PRIu64,
  119. ent->chan->global_identifier);
  120. tor_free(ent);
  121. return 1; /* So HT_FOREACH_FN will remove the element */
  122. }
  123. /* Clean up socket_table. Probably because the KIST sched impl is going away */
  124. static void
  125. free_all_socket_info(void)
  126. {
  127. HT_FOREACH_FN(socket_table_s, &socket_table, free_socket_info_by_ent, NULL);
  128. }
  129. static socket_table_ent_t *
  130. socket_table_search(socket_table_t *table, const channel_t *chan)
  131. {
  132. socket_table_ent_t search, *ent = NULL;
  133. search.chan = chan;
  134. ent = HT_FIND(socket_table_s, table, &search);
  135. return ent;
  136. }
  137. /* Free a socket entry in table for the given chan. */
  138. static void
  139. free_socket_info_by_chan(socket_table_t *table, const channel_t *chan)
  140. {
  141. socket_table_ent_t *ent = NULL;
  142. ent = socket_table_search(table, chan);
  143. if (!ent)
  144. return;
  145. log_debug(LD_SCHED, "scheduler free socket info for chan=%" PRIu64,
  146. chan->global_identifier);
  147. HT_REMOVE(socket_table_s, table, ent);
  148. free_socket_info_by_ent(ent, NULL);
  149. }
  150. /* Perform system calls for the given socket in order to calculate kist's
  151. * per-socket limit as documented in the function body. */
  152. MOCK_IMPL(void,
  153. update_socket_info_impl, (socket_table_ent_t *ent))
  154. {
  155. int64_t tcp_space, extra_space;
  156. const tor_socket_t sock =
  157. TO_CONN(BASE_CHAN_TO_TLS((channel_t *) ent->chan)->conn)->s;
  158. struct tcp_info tcp;
  159. socklen_t tcp_info_len = sizeof(tcp);
  160. if (kist_no_kernel_support) {
  161. goto fallback;
  162. }
  163. /* Gather information */
  164. if (getsockopt(sock, SOL_TCP, TCP_INFO, (void *)&(tcp), &tcp_info_len) < 0) {
  165. if (errno == EINVAL) {
  166. /* Oops, this option is not provided by the kernel, we'll have to
  167. * disable KIST entirely. This can happen if tor was built on a machine
  168. * with the support previously or if the kernel was updated and lost the
  169. * support. */
  170. log_notice(LD_SCHED, "Looks like our kernel doesn't have the support "
  171. "for KIST anymore. We will fallback to the naive "
  172. "approach. Set KISTSchedRunInterval=-1 to disable "
  173. "KIST.");
  174. kist_no_kernel_support = 1;
  175. }
  176. goto fallback;
  177. }
  178. if (ioctl(sock, SIOCOUTQNSD, &(ent->notsent)) < 0) {
  179. if (errno == EINVAL) {
  180. log_notice(LD_SCHED, "Looks like our kernel doesn't have the support "
  181. "for KIST anymore. We will fallback to the naive "
  182. "approach. Set KISTSchedRunInterval=-1 to disable "
  183. "KIST.");
  184. /* Same reason as the above. */
  185. kist_no_kernel_support = 1;
  186. }
  187. goto fallback;
  188. }
  189. ent->cwnd = tcp.tcpi_snd_cwnd;
  190. ent->unacked = tcp.tcpi_unacked;
  191. ent->mss = tcp.tcpi_snd_mss;
  192. /* TCP space is the number of bytes would could give to the kernel and it
  193. * would be able to immediately push them to the network. */
  194. tcp_space = (ent->cwnd - ent->unacked) * ent->mss;
  195. if (tcp_space < 0) {
  196. tcp_space = 0;
  197. }
  198. /* Imagine we have filled up tcp_space already for a socket and the scheduler
  199. * isn't going to run again for a while. We should write a little extra to the
  200. * kernel so it has some data to send between scheduling runs if it gets ACKs
  201. * back so it doesn't sit idle. With the suggested sock_buf_size_factor of
  202. * 1.0, a socket can have at most 2*cwnd data in the kernel: 1 cwnd on the
  203. * wire waiting for ACKs and 1 cwnd ready and waiting to be sent when those
  204. * ACKs come. */
  205. extra_space =
  206. clamp_double_to_int64((ent->cwnd * ent->mss) * sock_buf_size_factor) -
  207. ent->notsent;
  208. if (extra_space < 0) {
  209. extra_space = 0;
  210. }
  211. ent->limit = tcp_space + extra_space;
  212. return;
  213. fallback:
  214. /* If all of a sudden we don't have kist support, we just zero out all the
  215. * variables for this socket since we don't know what they should be.
  216. * We also effectively allow the socket write as much as it wants to the
  217. * kernel, effectively returning it to vanilla scheduler behavior. Writes
  218. * are still limited by the lower layers of Tor: socket blocking, full
  219. * outbuf, etc. */
  220. ent->cwnd = ent->unacked = ent->mss = ent->notsent = 0;
  221. ent->limit = INT_MAX;
  222. }
  223. /* Given a socket that isn't in the table, add it.
  224. * Given a socket that is in the table, reinit values that need init-ing
  225. * every scheduling run
  226. */
  227. static void
  228. init_socket_info(socket_table_t *table, const channel_t *chan)
  229. {
  230. socket_table_ent_t *ent = NULL;
  231. ent = socket_table_search(table, chan);
  232. if (!ent) {
  233. log_debug(LD_SCHED, "scheduler init socket info for chan=%" PRIu64,
  234. chan->global_identifier);
  235. ent = tor_malloc_zero(sizeof(*ent));
  236. ent->chan = chan;
  237. HT_INSERT(socket_table_s, table, ent);
  238. }
  239. ent->written = 0;
  240. }
  241. /* Add chan to the outbuf table if it isn't already in it. If it is, then don't
  242. * do anything */
  243. static void
  244. outbuf_table_add(outbuf_table_t *table, channel_t *chan)
  245. {
  246. outbuf_table_ent_t search, *ent;
  247. search.chan = chan;
  248. ent = HT_FIND(outbuf_table_s, table, &search);
  249. if (!ent) {
  250. log_debug(LD_SCHED, "scheduler init outbuf info for chan=%" PRIu64,
  251. chan->global_identifier);
  252. ent = tor_malloc_zero(sizeof(*ent));
  253. ent->chan = chan;
  254. HT_INSERT(outbuf_table_s, table, ent);
  255. }
  256. }
  257. static void
  258. outbuf_table_remove(outbuf_table_t *table, channel_t *chan)
  259. {
  260. outbuf_table_ent_t search, *ent;
  261. search.chan = chan;
  262. ent = HT_FIND(outbuf_table_s, table, &search);
  263. if (ent) {
  264. HT_REMOVE(outbuf_table_s, table, ent);
  265. free_outbuf_info_by_ent(ent, NULL);
  266. }
  267. }
  268. /* Set the scheduler running interval. */
  269. static void
  270. set_scheduler_run_interval(const networkstatus_t *ns)
  271. {
  272. int32_t old_sched_run_interval = sched_run_interval;
  273. sched_run_interval = kist_scheduler_run_interval(ns);
  274. if (old_sched_run_interval != sched_run_interval) {
  275. log_info(LD_SCHED, "Scheduler KIST changing its running interval "
  276. "from %" PRId32 " to %" PRId32,
  277. old_sched_run_interval, sched_run_interval);
  278. }
  279. }
  280. /* Return true iff the channel associated socket can write to the kernel that
  281. * is hasn't reach the limit. */
  282. static int
  283. socket_can_write(socket_table_t *table, const channel_t *chan)
  284. {
  285. socket_table_ent_t *ent = NULL;
  286. ent = socket_table_search(table, chan);
  287. tor_assert(ent);
  288. int64_t kist_limit_space =
  289. (int64_t) (ent->limit - ent->written) /
  290. (CELL_MAX_NETWORK_SIZE + TLS_PER_CELL_OVERHEAD);
  291. return kist_limit_space > 0;
  292. }
  293. /* Update the channel's socket kernel information. */
  294. static void
  295. update_socket_info(socket_table_t *table, const channel_t *chan)
  296. {
  297. socket_table_ent_t *ent = NULL;
  298. ent = socket_table_search(table, chan);
  299. tor_assert(ent);
  300. update_socket_info_impl(ent);
  301. }
  302. /* Increament the channel's socket written value by the number of bytes. */
  303. static void
  304. update_socket_written(socket_table_t *table, channel_t *chan, size_t bytes)
  305. {
  306. socket_table_ent_t *ent = NULL;
  307. ent = socket_table_search(table, chan);
  308. tor_assert(ent);
  309. log_debug(LD_SCHED, "chan=%" PRIu64 " wrote %lu bytes, old was %" PRIi64,
  310. chan->global_identifier, bytes, ent->written);
  311. ent->written += bytes;
  312. }
  313. /*
  314. * A naive KIST impl would write every single cell all the way to the kernel.
  315. * That would take a lot of system calls. A less bad KIST impl would write a
  316. * channel's outbuf to the kernel only when we are switching to a different
  317. * channel. But if we have two channels with equal priority, we end up writing
  318. * one cell for each and bouncing back and forth. This KIST impl avoids that
  319. * by only writing a channel's outbuf to the kernel if it has 8 cells or more
  320. * in it.
  321. */
  322. MOCK_IMPL(int, channel_should_write_to_kernel,
  323. (outbuf_table_t *table, channel_t *chan))
  324. {
  325. outbuf_table_add(table, chan);
  326. /* CELL_MAX_NETWORK_SIZE * 8 because we only want to write the outbuf to the
  327. * kernel if there's 8 or more cells waiting */
  328. return channel_outbuf_length(chan) > (CELL_MAX_NETWORK_SIZE * 8);
  329. }
  330. /* Little helper function to write a channel's outbuf all the way to the
  331. * kernel */
  332. MOCK_IMPL(void, channel_write_to_kernel, (channel_t *chan))
  333. {
  334. log_debug(LD_SCHED, "Writing %lu bytes to kernel for chan %" PRIu64,
  335. channel_outbuf_length(chan), chan->global_identifier);
  336. connection_handle_write(TO_CONN(BASE_CHAN_TO_TLS(chan)->conn), 0);
  337. }
  338. /* Return true iff the scheduler has work to perform. */
  339. static int
  340. have_work(void)
  341. {
  342. smartlist_t *cp = get_channels_pending();
  343. tor_assert(cp);
  344. return smartlist_len(cp) > 0;
  345. }
  346. /* Function of the scheduler interface: free_all() */
  347. static void
  348. kist_free_all(void)
  349. {
  350. free_all_outbuf_info();
  351. free_all_socket_info();
  352. }
  353. /* Function of the scheduler interface: on_channel_free() */
  354. static void
  355. kist_on_channel_free(const channel_t *chan)
  356. {
  357. free_socket_info_by_chan(&socket_table, chan);
  358. }
  359. /* Function of the scheduler interface: on_new_consensus() */
  360. static void
  361. kist_scheduler_on_new_consensus(const networkstatus_t *old_c,
  362. const networkstatus_t *new_c)
  363. {
  364. (void) old_c;
  365. (void) new_c;
  366. set_scheduler_run_interval(new_c);
  367. }
  368. /* Function of the scheduler interface: on_new_options() */
  369. static void
  370. kist_scheduler_on_new_options(void)
  371. {
  372. sock_buf_size_factor = get_options()->KISTSockBufSizeFactor;
  373. /* Calls kist_scheduler_run_interval which calls get_options(). */
  374. set_scheduler_run_interval(NULL);
  375. }
  376. /* Function of the scheduler interface: init() */
  377. static void
  378. kist_scheduler_init(void)
  379. {
  380. kist_scheduler_on_new_options();
  381. tor_assert(sched_run_interval > 0);
  382. }
  383. /* Function of the scheduler interface: schedule() */
  384. static void
  385. kist_scheduler_schedule(void)
  386. {
  387. struct monotime_t now;
  388. struct timeval next_run;
  389. int32_t diff;
  390. struct event *ev = get_run_sched_ev();
  391. tor_assert(ev);
  392. if (!have_work()) {
  393. return;
  394. }
  395. monotime_get(&now);
  396. diff = (int32_t) monotime_diff_msec(&scheduler_last_run, &now);
  397. if (diff < sched_run_interval) {
  398. next_run.tv_sec = 0;
  399. /* 1000 for ms -> us */
  400. next_run.tv_usec = (sched_run_interval - diff) * 1000;
  401. /* Readding an event reschedules it. It does not duplicate it. */
  402. event_add(ev, &next_run);
  403. } else {
  404. event_active(ev, EV_TIMEOUT, 1);
  405. }
  406. }
  407. /* Function of the scheduler interface: run() */
  408. static void
  409. kist_scheduler_run(void)
  410. {
  411. /* Define variables */
  412. channel_t *chan = NULL; // current working channel
  413. /* The last distinct chan served in a sched loop. */
  414. channel_t *prev_chan = NULL;
  415. int flush_result; // temporarily store results from flush calls
  416. /* Channels to be readding to pending at the end */
  417. smartlist_t *to_readd = NULL;
  418. smartlist_t *cp = get_channels_pending();
  419. /* For each pending channel, collect new kernel information */
  420. SMARTLIST_FOREACH_BEGIN(cp, const channel_t *, pchan) {
  421. init_socket_info(&socket_table, pchan);
  422. update_socket_info(&socket_table, pchan);
  423. } SMARTLIST_FOREACH_END(pchan);
  424. log_debug(LD_SCHED, "Running the scheduler. %d channels pending",
  425. smartlist_len(cp));
  426. /* The main scheduling loop. Loop until there are no more pending channels */
  427. while (smartlist_len(cp) > 0) {
  428. /* get best channel */
  429. chan = smartlist_pqueue_pop(cp, scheduler_compare_channels,
  430. offsetof(channel_t, sched_heap_idx));
  431. tor_assert(chan);
  432. outbuf_table_add(&outbuf_table, chan);
  433. /* if we have switched to a new channel, consider writing the previous
  434. * channel's outbuf to the kernel. */
  435. if (!prev_chan) {
  436. prev_chan = chan;
  437. }
  438. if (prev_chan != chan) {
  439. if (channel_should_write_to_kernel(&outbuf_table, prev_chan)) {
  440. channel_write_to_kernel(prev_chan);
  441. outbuf_table_remove(&outbuf_table, prev_chan);
  442. }
  443. prev_chan = chan;
  444. }
  445. /* Only flush and write if the per-socket limit hasn't been hit */
  446. if (socket_can_write(&socket_table, chan)) {
  447. /* flush to channel queue/outbuf */
  448. flush_result = (int)channel_flush_some_cells(chan, 1); // 1 for num cells
  449. /* flush_result has the # cells flushed */
  450. if (flush_result > 0) {
  451. update_socket_written(&socket_table, chan, flush_result *
  452. (CELL_MAX_NETWORK_SIZE + TLS_PER_CELL_OVERHEAD));
  453. }
  454. /* XXX What if we didn't flush? */
  455. }
  456. /* Decide what to do with the channel now */
  457. if (!channel_more_to_flush(chan) &&
  458. !socket_can_write(&socket_table, chan)) {
  459. /* Case 1: no more cells to send, and cannot write */
  460. /*
  461. * You might think we should put the channel in SCHED_CHAN_IDLE. And
  462. * you're probably correct. While implementing KIST, we found that the
  463. * scheduling system would sometimes lose track of channels when we did
  464. * that. We suspect it has to do with the difference between "can't
  465. * write because socket/outbuf is full" and KIST's "can't write because
  466. * we've arbitrarily decided that that's enough for now." Sometimes
  467. * channels run out of cells at the same time they hit their
  468. * kist-imposed write limit and maybe the rest of Tor doesn't put the
  469. * channel back in pending when it is supposed to.
  470. *
  471. * This should be investigated again. It is as simple as changing
  472. * SCHED_CHAN_WAITING_FOR_CELLS to SCHED_CHAN_IDLE and seeing if Tor
  473. * starts having serious throughput issues. Best done in shadow/chutney.
  474. */
  475. chan->scheduler_state = SCHED_CHAN_WAITING_FOR_CELLS;
  476. log_debug(LD_SCHED, "chan=%" PRIu64 " now waiting_for_cells",
  477. chan->global_identifier);
  478. } else if (!channel_more_to_flush(chan)) {
  479. /* Case 2: no more cells to send, but still open for writes */
  480. chan->scheduler_state = SCHED_CHAN_WAITING_FOR_CELLS;
  481. log_debug(LD_SCHED, "chan=%" PRIu64 " now waiting_for_cells",
  482. chan->global_identifier);
  483. } else if (!socket_can_write(&socket_table, chan)) {
  484. /* Case 3: cells to send, but cannot write */
  485. /*
  486. * We want to write, but can't. If we left the channel in
  487. * channels_pending, we would never exit the scheduling loop. We need to
  488. * add it to a temporary list of channels to be added to channels_pending
  489. * after the scheduling loop is over. They can hopefully be taken care of
  490. * in the next scheduling round.
  491. */
  492. chan->scheduler_state = SCHED_CHAN_WAITING_TO_WRITE;
  493. if (!to_readd) {
  494. to_readd = smartlist_new();
  495. }
  496. smartlist_add(to_readd, chan);
  497. log_debug(LD_SCHED, "chan=%" PRIu64 " now waiting_to_write",
  498. chan->global_identifier);
  499. } else {
  500. /* Case 4: cells to send, and still open for writes */
  501. chan->scheduler_state = SCHED_CHAN_PENDING;
  502. smartlist_pqueue_add(cp, scheduler_compare_channels,
  503. offsetof(channel_t, sched_heap_idx), chan);
  504. }
  505. } /* End of main scheduling loop */
  506. /* Write the outbuf of any channels that still have data */
  507. HT_FOREACH_FN(outbuf_table_s, &outbuf_table, each_channel_write_to_kernel,
  508. NULL);
  509. free_all_outbuf_info();
  510. HT_CLEAR(outbuf_table_s, &outbuf_table);
  511. log_debug(LD_SCHED, "len pending=%d, len to_readd=%d",
  512. smartlist_len(cp),
  513. (to_readd ? smartlist_len(to_readd) : -1));
  514. /* Readd any channels we need to */
  515. if (to_readd) {
  516. SMARTLIST_FOREACH_BEGIN(to_readd, channel_t *, readd_chan) {
  517. readd_chan->scheduler_state = SCHED_CHAN_PENDING;
  518. if (!smartlist_contains(cp, readd_chan)) {
  519. smartlist_pqueue_add(cp, scheduler_compare_channels,
  520. offsetof(channel_t, sched_heap_idx), readd_chan);
  521. }
  522. } SMARTLIST_FOREACH_END(readd_chan);
  523. smartlist_free(to_readd);
  524. }
  525. monotime_get(&scheduler_last_run);
  526. }
  527. /*****************************************************************************
  528. * Externally called function implementations not called through scheduler_t
  529. *****************************************************************************/
  530. /* Return the KIST scheduler object. If it didn't exists, return a newly
  531. * allocated one but init() is not called. */
  532. scheduler_t *
  533. get_kist_scheduler(void)
  534. {
  535. if (!kist_scheduler) {
  536. log_debug(LD_SCHED, "Allocating kist scheduler struct");
  537. kist_scheduler = tor_malloc_zero(sizeof(*kist_scheduler));
  538. kist_scheduler->free_all = kist_free_all;
  539. kist_scheduler->on_channel_free = kist_on_channel_free;
  540. kist_scheduler->init = kist_scheduler_init;
  541. kist_scheduler->on_new_consensus = kist_scheduler_on_new_consensus;
  542. kist_scheduler->schedule = kist_scheduler_schedule;
  543. kist_scheduler->run = kist_scheduler_run;
  544. kist_scheduler->on_new_options = kist_scheduler_on_new_options;
  545. }
  546. return kist_scheduler;
  547. }
  548. /* Default interval that KIST runs (in ms). */
  549. #define KIST_SCHED_RUN_INTERVAL_DEFAULT 10
  550. /* Minimum interval that KIST runs. This value disables KIST. */
  551. #define KIST_SCHED_RUN_INTERVAL_MIN 0
  552. /* Maximum interval that KIST runs (in ms). */
  553. #define KIST_SCHED_RUN_INTERVAL_MAX 100
  554. /* Check the torrc for the configured KIST scheduler run interval.
  555. * - If torrc < 0, then return the negative torrc value (shouldn't even be
  556. * using KIST)
  557. * - If torrc > 0, then return the positive torrc value (should use KIST, and
  558. * should use the set value)
  559. * - If torrc == 0, then look in the consensus for what the value should be.
  560. * - If == 0, then return -1 (don't use KIST)
  561. * - If > 0, then return the positive consensus value
  562. * - If consensus doesn't say anything, return 10 milliseconds
  563. */
  564. int32_t
  565. kist_scheduler_run_interval(const networkstatus_t *ns)
  566. {
  567. int32_t run_interval = (int32_t)get_options()->KISTSchedRunInterval;
  568. if (run_interval != 0) {
  569. log_debug(LD_SCHED, "Found KISTSchedRunInterval in torrc. Using that.");
  570. return run_interval;
  571. }
  572. log_debug(LD_SCHED, "Turning to the consensus for KISTSchedRunInterval");
  573. run_interval = networkstatus_get_param(ns, "KISTSchedRunInterval",
  574. KIST_SCHED_RUN_INTERVAL_DEFAULT,
  575. KIST_SCHED_RUN_INTERVAL_MIN,
  576. KIST_SCHED_RUN_INTERVAL_MAX);
  577. if (run_interval <= 0)
  578. return -1;
  579. return run_interval;
  580. }
  581. #ifdef HAVE_KIST_SUPPORT
  582. /* Return true iff the scheduler subsystem should use KIST. */
  583. int
  584. scheduler_should_use_kist(void)
  585. {
  586. int64_t run_interval = kist_scheduler_run_interval(NULL);
  587. log_info(LD_SCHED, "Determined sched_run_interval should be %" PRId64 ". "
  588. "Will%s use KIST.",
  589. run_interval, (run_interval > 0 ? "" : " not"));
  590. return run_interval > 0;
  591. }
  592. #else /* HAVE_KIST_SUPPORT */
  593. int
  594. scheduler_should_use_kist(void)
  595. {
  596. return 0;
  597. }
  598. #endif /* HAVE_KIST_SUPPORT */