hibernate.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /* Copyright 2004 Roger Dingledine, Nick Mathewson. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /**
  5. * \file hibernate.c
  6. * \brief Functions to close listeners, stop allowing new circuits,
  7. * etc in preparation for closing down or going dormant; and to track
  8. * bandwidth and time intervals to know when to hibernate and when to
  9. * stop hibernating.
  10. **/
  11. /*
  12. hibernating, phase 1:
  13. - send destroy in response to create cells
  14. - send end (policy failed) in response to begin cells
  15. - close an OR conn when it has no circuits
  16. hibernating, phase 2:
  17. (entered when bandwidth hard limit reached)
  18. - close all OR/AP/exit conns)
  19. */
  20. #include "or.h"
  21. #define HIBERNATE_STATE_LIVE 1
  22. #define HIBERNATE_STATE_EXITING 2
  23. #define HIBERNATE_STATE_LOWBANDWIDTH 3
  24. #define HIBERNATE_STATE_DORMANT 4
  25. #define SHUTDOWN_WAIT_LENGTH 30 /* seconds */
  26. static int hibernate_state = HIBERNATE_STATE_LIVE;
  27. /** If are hibernating, when do we plan to wake up? Set to 0 if we
  28. * aren't hibernating. */
  29. static time_t hibernate_end_time = 0;
  30. /* Fields for accounting logic. Accounting overview:
  31. *
  32. * Accounting is designed to ensure that no more than N bytes are sent
  33. * in either direction over a given interval (currently, one month,
  34. * starting at 0:00 GMT an arbitrary day within the month). We could
  35. * try to do this by choking our bandwidth to a trickle, but that
  36. * would make our streams useless. Instead, we estimate what our
  37. * bandwidth usage will be, and guess how long we'll be able to
  38. * provide that much bandwidth before hitting our limit. We then
  39. * choose a random time within the accounting interval to come up (so
  40. * that we don't get 50 Tors running on the 1st of the month and none
  41. * on the 30th).
  42. *
  43. * Each interval runs as follows:
  44. *
  45. * 1. We guess our bandwidth usage, based on how much we used
  46. * last time. We choose a "wakeup time" within the interval to come up.
  47. * 2. Until the chosen wakeup time, we hibernate.
  48. * 3. We come up at the wakeup time, and provide bandwidth until we are
  49. * "very close" to running out.
  50. * 4. Then we go into low-bandwidth mode, and stop accepting new
  51. * connections, but provide bandwidth until we run out.
  52. * 5. Then we hibernate until the end of the interval.
  53. *
  54. * If the interval ends before we run out of bandwdith, we go back to
  55. * step one.
  56. */
  57. /** How many bytes have we read/written in this accounting interval? */
  58. static uint64_t n_bytes_read_in_interval = 0;
  59. static uint64_t n_bytes_written_in_interval = 0;
  60. /** How many seconds have we been running this interval? */
  61. static uint32_t n_seconds_active_in_interval = 0;
  62. /** When did this accounting interval start? */
  63. static time_t interval_start_time = 0;
  64. /** When will this accounting interval end? */
  65. static time_t interval_end_time = 0;
  66. /** How far into the accounting interval should we hibernate? */
  67. static time_t interval_wakeup_time = 0;
  68. /** How much bandwidth do we 'expect' to use per minute? */
  69. static uint32_t expected_bandwidth_usage = 0;
  70. static void reset_accounting(time_t now);
  71. static int read_bandwidth_usage(void);
  72. static int record_bandwidth_usage(time_t now);
  73. static time_t start_of_accounting_period_after(time_t now);
  74. static time_t start_of_accounting_period_containing(time_t now);
  75. static void accounting_set_wakeup_time(void);
  76. /* ************
  77. * Functions for bandwidth accounting.
  78. * ************/
  79. /** Called from main.c to tell us that <b>seconds</b> seconds have
  80. * passed, <b>n_read</b> bytes have been read, and <b>n_written</b>
  81. * bytes have been written. */
  82. void
  83. accounting_add_bytes(size_t n_read, size_t n_written, int seconds)
  84. {
  85. n_bytes_read_in_interval += n_read;
  86. n_bytes_written_in_interval += n_written;
  87. /* If we haven't been called in 10 seconds, we're probably jumping
  88. * around in time. */
  89. n_seconds_active_in_interval += (seconds < 10) ? seconds : 0;
  90. }
  91. /** Increment the month field of <b>tm</b> by <b>delta</b> months. */
  92. static INLINE void
  93. incr_month(struct tm *tm, unsigned int delta)
  94. {
  95. tm->tm_mon += delta;
  96. /* officially, we don't have to do this, but some platforms are rumored
  97. * to have broken implementations. */
  98. while (tm->tm_mon > 11) {
  99. ++tm->tm_year;
  100. tm->tm_mon -= 12;
  101. }
  102. }
  103. /** Decrement the month field of <b>tm</b> by <b>delta</b> months. */
  104. static INLINE void
  105. decr_month(struct tm *tm, unsigned int delta)
  106. {
  107. tm->tm_mon -= delta;
  108. while (tm->tm_mon < 0) {
  109. --tm->tm_year;
  110. tm->tm_mon += 12;
  111. }
  112. }
  113. /** Return the start of the accounting period that contains the time
  114. * <b>now</b> */
  115. static time_t
  116. start_of_accounting_period_containing(time_t now)
  117. {
  118. struct tm *tm;
  119. /* Only months are supported. */
  120. tm = gmtime(&now);
  121. /* If this is before the Nth, we want the Nth of last month. */
  122. if (tm->tm_mday < get_options()->AccountingStart) {
  123. decr_month(tm, 1);
  124. }
  125. /* Otherwise, the month and year are correct.*/
  126. tm->tm_mday = get_options()->AccountingStart;
  127. tm->tm_hour = 0;
  128. tm->tm_min = 0;
  129. tm->tm_sec = 0;
  130. return tor_timegm(tm);
  131. }
  132. /** Return the start of the accounting period that comes after the one
  133. * containing the time <b>now</b>. */
  134. static time_t
  135. start_of_accounting_period_after(time_t now)
  136. {
  137. time_t start;
  138. struct tm *tm;
  139. start = start_of_accounting_period_containing(now);
  140. tm = gmtime(&start);
  141. incr_month(tm, 1);
  142. return tor_timegm(tm);
  143. }
  144. /** Initialize the accounting subsystem. */
  145. void
  146. configure_accounting(time_t now)
  147. {
  148. /* Try to remember our recorded usage. */
  149. if (!interval_start_time)
  150. read_bandwidth_usage(); /* If we fail, we'll leave values at zero, and
  151. * reset below.*/
  152. if (!interval_start_time ||
  153. start_of_accounting_period_after(interval_start_time) <= now) {
  154. /* We didn't have recorded usage, or we don't have recorded usage
  155. * for this interval. Start a new interval. */
  156. log_fn(LOG_INFO, "Starting new accounting interval.");
  157. reset_accounting(now);
  158. } if (interval_start_time ==
  159. start_of_accounting_period_containing(interval_start_time)) {
  160. log_fn(LOG_INFO, "Continuing accounting interval.");
  161. /* We are in the interval we thought we were in. Do nothing.*/
  162. interval_end_time = start_of_accounting_period_after(interval_start_time);
  163. } else {
  164. log_fn(LOG_WARN, "Mismatched accounting interval; starting a fresh one.");
  165. reset_accounting(now);
  166. }
  167. accounting_set_wakeup_time();
  168. }
  169. /** Set expected_bandwidth_usage based on how much we sent/received
  170. * per minute last interval (if we were up for at least 30 minutes),
  171. * or based on our declared bandwidth otherwise. */
  172. static void
  173. update_expected_bandwidth(void)
  174. {
  175. uint64_t used;
  176. uint32_t max_configured = (get_options()->BandwidthRateBytes * 60);
  177. /* XXX max_configured will be false if it exceeds
  178. * get_options()->AccountingMaxKB*1000, right? -RD
  179. */
  180. if (n_seconds_active_in_interval < 1800) {
  181. expected_bandwidth_usage = max_configured;
  182. } else {
  183. used = n_bytes_written_in_interval < n_bytes_read_in_interval ?
  184. n_bytes_read_in_interval : n_bytes_written_in_interval;
  185. expected_bandwidth_usage = (uint32_t)
  186. (used / (n_seconds_active_in_interval / 60));
  187. if (expected_bandwidth_usage > max_configured)
  188. expected_bandwidth_usage = max_configured;
  189. }
  190. }
  191. /** Called at the start of a new accounting interval: reset our
  192. * expected bandwidth usage based on what happened last time, set up
  193. * the start and end of the interval, and clear byte/time totals.
  194. */
  195. static void
  196. reset_accounting(time_t now) {
  197. log_fn(LOG_INFO, "Starting new accounting interval.");
  198. update_expected_bandwidth();
  199. interval_start_time = start_of_accounting_period_containing(now);
  200. interval_end_time = start_of_accounting_period_after(interval_start_time);
  201. n_bytes_read_in_interval = 0;
  202. n_bytes_written_in_interval = 0;
  203. n_seconds_active_in_interval = 0;
  204. }
  205. /** Return true iff we should save our bandwidth usage to disk. */
  206. static INLINE int
  207. time_to_record_bandwidth_usage(time_t now)
  208. {
  209. /* Note every 60 sec */
  210. #define NOTE_INTERVAL (60)
  211. /* Or every 20 megabytes */
  212. #define NOTE_BYTES 20*(1024*1024)
  213. static uint64_t last_read_bytes_noted = 0;
  214. static uint64_t last_written_bytes_noted = 0;
  215. static time_t last_time_noted = 0;
  216. if (last_time_noted + NOTE_INTERVAL <= now ||
  217. last_read_bytes_noted + NOTE_BYTES <= n_bytes_read_in_interval ||
  218. last_written_bytes_noted + NOTE_BYTES <= n_bytes_written_in_interval ||
  219. (interval_end_time && interval_end_time <= now)) {
  220. last_time_noted = now;
  221. last_read_bytes_noted = n_bytes_read_in_interval;
  222. last_written_bytes_noted = n_bytes_written_in_interval;
  223. return 1;
  224. }
  225. return 0;
  226. }
  227. void
  228. accounting_run_housekeeping(time_t now)
  229. {
  230. if (now >= interval_end_time) {
  231. configure_accounting(now);
  232. }
  233. if (time_to_record_bandwidth_usage(now)) {
  234. if (record_bandwidth_usage(now)) {
  235. log_fn(LOG_ERR, "Couldn't record bandwidth usage; exiting.");
  236. exit(1);
  237. }
  238. }
  239. }
  240. /** Based on our interval and our estimated bandwidth, choose a
  241. * deterministic (but random-ish) time to wake up. */
  242. static void
  243. accounting_set_wakeup_time(void)
  244. {
  245. struct tm *tm;
  246. char buf[ISO_TIME_LEN+1];
  247. char digest[DIGEST_LEN];
  248. crypto_digest_env_t *d;
  249. int n_days_in_interval;
  250. int n_days_to_exhaust_bw;
  251. int n_days_to_consider;
  252. format_iso_time(buf, interval_start_time);
  253. crypto_pk_get_digest(get_identity_key(), digest);
  254. d = crypto_new_digest_env();
  255. crypto_digest_add_bytes(d, buf, ISO_TIME_LEN);
  256. crypto_digest_add_bytes(d, digest, DIGEST_LEN);
  257. crypto_digest_get_digest(d, digest, DIGEST_LEN);
  258. crypto_free_digest_env(d);
  259. if (expected_bandwidth_usage)
  260. n_days_to_exhaust_bw =
  261. (get_options()->AccountingMaxKB/expected_bandwidth_usage)/(24*60);
  262. else
  263. n_days_to_exhaust_bw = 1;
  264. tm = gmtime(&interval_start_time);
  265. if (++tm->tm_mon > 11) { tm->tm_mon = 0; ++tm->tm_year; }
  266. n_days_in_interval = (tor_timegm(tm)-interval_start_time+1)/(24*60*60);
  267. n_days_to_consider = n_days_in_interval - n_days_to_exhaust_bw;
  268. /* XXX can we simplify this just by picking a random (non-deterministic)
  269. * time to be up? If we go down and come up, then we pick a new one. Is
  270. * that good enough? -RD */
  271. while (((unsigned char)digest[0]) > n_days_to_consider)
  272. crypto_digest(digest, digest, DIGEST_LEN);
  273. interval_wakeup_time = interval_start_time +
  274. 24*60*60 * (unsigned char)digest[0];
  275. }
  276. /* XXXX009 This should also get called on HUP and shutdown. */
  277. #define BW_ACCOUNTING_VERSION 1
  278. /** Save all our bandwidth tracking information to disk. Return 0 on
  279. * success, -1 on failure*/
  280. static int
  281. record_bandwidth_usage(time_t now)
  282. {
  283. char buf[128];
  284. char fname[512];
  285. char time1[ISO_TIME_LEN+1];
  286. char time2[ISO_TIME_LEN+1];
  287. char *cp = buf;
  288. /* Format is:
  289. Version\nTime\nTime\nRead\nWrite\nSeconds\nExpected-Rate\n */
  290. format_iso_time(time1, interval_start_time);
  291. format_iso_time(time2, now);
  292. tor_snprintf(cp, sizeof(buf),
  293. "%d\n%s\n%s\n"U64_FORMAT"\n"U64_FORMAT"\n%lu\n%lu\n",
  294. BW_ACCOUNTING_VERSION,
  295. time1,
  296. time2,
  297. U64_PRINTF_ARG(n_bytes_read_in_interval),
  298. U64_PRINTF_ARG(n_bytes_written_in_interval),
  299. (unsigned long)n_seconds_active_in_interval,
  300. (unsigned long)expected_bandwidth_usage);
  301. tor_snprintf(fname, sizeof(fname), "%s/bw_accounting",
  302. get_options()->DataDirectory);
  303. return write_str_to_file(fname, buf, 0);
  304. }
  305. /** Read stored accounting information from disk. Return 0 on success;
  306. * return -1 and change nothing on failure. */
  307. static int
  308. read_bandwidth_usage(void)
  309. {
  310. char *s = NULL;
  311. char fname[512];
  312. time_t t1, t2;
  313. uint64_t n_read, n_written;
  314. uint32_t expected_bw, n_seconds;
  315. smartlist_t *elts;
  316. int ok;
  317. tor_snprintf(fname, sizeof(fname), "%s/bw_accounting",
  318. get_options()->DataDirectory);
  319. if (!(s = read_file_to_str(fname, 0))) {
  320. return 0;
  321. }
  322. elts = smartlist_create();
  323. smartlist_split_string(elts, s, "\n", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK,0);
  324. tor_free(s);
  325. if (smartlist_len(elts)<1 ||
  326. atoi(smartlist_get(elts,0)) != BW_ACCOUNTING_VERSION) {
  327. log_fn(LOG_WARN, "Unrecognized bw_accounting file version: %s",
  328. (const char*)smartlist_get(elts,0));
  329. goto err;
  330. }
  331. if (smartlist_len(elts) < 7) {
  332. log_fn(LOG_WARN, "Corrupted bw_accounting file: %d lines",
  333. smartlist_len(elts));
  334. goto err;
  335. }
  336. if (parse_iso_time(smartlist_get(elts,1), &t1)) {
  337. log_fn(LOG_WARN, "Error parsing bandwidth usage start time.");
  338. goto err;
  339. }
  340. if (parse_iso_time(smartlist_get(elts,2), &t2)) {
  341. log_fn(LOG_WARN, "Error parsing bandwidth usage last-written time");
  342. goto err;
  343. }
  344. n_read = tor_parse_uint64(smartlist_get(elts,3), 10, 0, UINT64_MAX,
  345. &ok, NULL);
  346. if (!ok) {
  347. log_fn(LOG_WARN, "Error parsing number of bytes read");
  348. goto err;
  349. }
  350. n_written = tor_parse_uint64(smartlist_get(elts,4), 10, 0, UINT64_MAX,
  351. &ok, NULL);
  352. if (!ok) {
  353. log_fn(LOG_WARN, "Error parsing number of bytes read");
  354. goto err;
  355. }
  356. n_seconds = (uint32_t)tor_parse_ulong(smartlist_get(elts,5), 10,0,ULONG_MAX,
  357. &ok, NULL);
  358. if (!ok) {
  359. log_fn(LOG_WARN, "Error parsing number of seconds live");
  360. goto err;
  361. }
  362. expected_bw =(uint32_t)tor_parse_ulong(smartlist_get(elts,6), 10,0,ULONG_MAX,
  363. &ok, NULL);
  364. if (!ok) {
  365. log_fn(LOG_WARN, "Error parsing expected bandwidth");
  366. goto err;
  367. }
  368. n_bytes_read_in_interval = n_read;
  369. n_bytes_written_in_interval = n_written;
  370. n_seconds_active_in_interval = n_seconds;
  371. interval_start_time = t1;
  372. expected_bandwidth_usage = expected_bw;
  373. accounting_set_wakeup_time();
  374. return 0;
  375. err:
  376. SMARTLIST_FOREACH(elts, char *, cp, tor_free(cp));
  377. smartlist_free(elts);
  378. return -1;
  379. }
  380. /** Return true iff we have sent/received all the bytes we are willing
  381. * to send/receive this interval. */
  382. static int
  383. hibernate_hard_limit_reached(void)
  384. {
  385. uint64_t hard_limit = get_options()->AccountingMaxKB<<10;
  386. if (!hard_limit)
  387. return 0;
  388. return n_bytes_read_in_interval >= hard_limit
  389. || n_bytes_written_in_interval >= hard_limit;
  390. }
  391. /** Return true iff we have sent/received almost all the bytes we are willing
  392. * to send/receive this interval. */
  393. static int hibernate_soft_limit_reached(void)
  394. {
  395. uint64_t soft_limit = (uint64_t) ((get_options()->AccountingMaxKB<<10) * .99);
  396. if (!soft_limit)
  397. return 0;
  398. return n_bytes_read_in_interval >= soft_limit
  399. || n_bytes_written_in_interval >= soft_limit;
  400. }
  401. /** Called when we get a SIGINT, or when bandwidth soft limit is
  402. * reached. Puts us into "loose hibernation": we don't accept new
  403. * connections, but we continue handling old ones. */
  404. static void hibernate_begin(int new_state, time_t now) {
  405. connection_t *conn;
  406. if(hibernate_state == HIBERNATE_STATE_EXITING) {
  407. /* we've been called twice now. close immediately. */
  408. log(LOG_NOTICE,"Second sigint received; exiting now.");
  409. tor_cleanup();
  410. exit(0);
  411. }
  412. /* close listeners. leave control listener(s). */
  413. while((conn = connection_get_by_type(CONN_TYPE_OR_LISTENER)) ||
  414. (conn = connection_get_by_type(CONN_TYPE_AP_LISTENER)) ||
  415. (conn = connection_get_by_type(CONN_TYPE_DIR_LISTENER))) {
  416. log_fn(LOG_INFO,"Closing listener type %d", conn->type);
  417. connection_mark_for_close(conn);
  418. }
  419. /* XXX kill intro point circs */
  420. /* XXX upload rendezvous service descriptors with no intro points */
  421. if(new_state == HIBERNATE_STATE_EXITING) {
  422. log(LOG_NOTICE,"Interrupt: will shut down in %d seconds. Interrupt again to exit now.", SHUTDOWN_WAIT_LENGTH);
  423. hibernate_end_time = time(NULL) + SHUTDOWN_WAIT_LENGTH;
  424. } else { /* soft limit reached */
  425. hibernate_end_time = interval_end_time;
  426. }
  427. hibernate_state = new_state;
  428. record_bandwidth_usage(time(NULL));
  429. }
  430. /** Called when we've been hibernating and our timeout is reached. */
  431. static void
  432. hibernate_end(int new_state) {
  433. tor_assert(hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH ||
  434. hibernate_state == HIBERNATE_STATE_DORMANT);
  435. /* listeners will be relaunched in run_scheduled_events() in main.c */
  436. log_fn(LOG_NOTICE,"Hibernation period ended. Resuming normal activity.");
  437. hibernate_state = new_state;
  438. hibernate_end_time = 0; /* no longer hibernating */
  439. }
  440. /** A wrapper around hibernate_begin, for when we get SIGINT. */
  441. void
  442. hibernate_begin_shutdown(void) {
  443. hibernate_begin(HIBERNATE_STATE_EXITING, time(NULL));
  444. }
  445. /** Return true iff we are currently hibernating. */
  446. int
  447. we_are_hibernating(void) {
  448. return hibernate_state != HIBERNATE_STATE_LIVE;
  449. }
  450. /** If we aren't currently dormant, close all connections and become
  451. * dormant. */
  452. static void
  453. hibernate_go_dormant(void) {
  454. connection_t *conn;
  455. if (hibernate_state == HIBERNATE_STATE_DORMANT)
  456. return;
  457. hibernate_state = HIBERNATE_STATE_DORMANT;
  458. log_fn(LOG_NOTICE,"Going dormant. Blowing away remaining connections.");
  459. /* Close all OR/AP/exit conns. Leave dir conns because we still want
  460. * to be able to upload server descriptors so people know we're still
  461. * running, and download directories so we can detect if we're obsolete.
  462. * Leave control conns because we still want to be controllable.
  463. */
  464. while((conn = connection_get_by_type(CONN_TYPE_OR)) ||
  465. (conn = connection_get_by_type(CONN_TYPE_AP)) ||
  466. (conn = connection_get_by_type(CONN_TYPE_EXIT))) {
  467. log_fn(LOG_INFO,"Closing conn type %d", conn->type);
  468. connection_mark_for_close(conn);
  469. }
  470. record_bandwidth_usage(time(NULL));
  471. }
  472. /** Called when hibernate_end_time has arrived. */
  473. static void
  474. hibernate_end_time_elapsed(time_t now)
  475. {
  476. /* The interval has ended, or it is wakeup time. Find out which. */
  477. accounting_run_housekeeping(now);
  478. if (interval_wakeup_time <= now) {
  479. /* The interval hasn't changed, but interval_wakeup_time has passed.
  480. * It's time to wake up and start being a server. */
  481. hibernate_end(HIBERNATE_STATE_LIVE);
  482. return;
  483. } else {
  484. /* The interval has changed, and it isn't time to wake up yet. */
  485. hibernate_end_time = interval_wakeup_time;
  486. if (hibernate_state != HIBERNATE_STATE_DORMANT)
  487. /* We weren't sleeping before; we should sleep now. */
  488. hibernate_go_dormant();
  489. }
  490. }
  491. /** The big function. Consider our environment and decide if it's time
  492. * to start/stop hibernating.
  493. */
  494. void consider_hibernation(time_t now) {
  495. /* If we're in 'exiting' mode, then we just shut down after the interval
  496. * elapses. */
  497. if (hibernate_state == HIBERNATE_STATE_EXITING) {
  498. tor_assert(hibernate_end_time);
  499. if(hibernate_end_time <= now) {
  500. log(LOG_NOTICE,"Clean shutdown finished. Exiting.");
  501. tor_cleanup();
  502. exit(0);
  503. }
  504. return; /* if exiting soon, don't worry about bandwidth limits */
  505. }
  506. if(hibernate_state == HIBERNATE_STATE_DORMANT) {
  507. /* We've been hibernating because of bandwidth accounting. */
  508. tor_assert(hibernate_end_time);
  509. if (hibernate_end_time > now) {
  510. /* If we're hibernating, don't wake up until it's time, regardless of
  511. * whether we're in a new interval. */
  512. return ;
  513. } else {
  514. hibernate_end_time_elapsed(now);
  515. }
  516. }
  517. /* Else, we aren't hibernating. See if it's time to start hibernating, or to
  518. * go dormant. */
  519. if (hibernate_state == HIBERNATE_STATE_LIVE &&
  520. hibernate_soft_limit_reached()) {
  521. log_fn(LOG_NOTICE,"Bandwidth soft limit reached; commencing hibernation.");
  522. hibernate_begin(HIBERNATE_STATE_LOWBANDWIDTH, now);
  523. }
  524. if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH) {
  525. if (hibernate_hard_limit_reached()) {
  526. hibernate_go_dormant();
  527. } else if (hibernate_end_time <= now) {
  528. /* The hibernation period ended while we were still in lowbandwidth.*/
  529. hibernate_end_time_elapsed(now);
  530. }
  531. }
  532. }