hibernate.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /* Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. const char hibernate_c_id[] = "$Id$";
  5. /**
  6. * \file hibernate.c
  7. * \brief Functions to close listeners, stop allowing new circuits,
  8. * etc in preparation for closing down or going dormant; and to track
  9. * bandwidth and time intervals to know when to hibernate and when to
  10. * stop hibernating.
  11. **/
  12. /*
  13. hibernating, phase 1:
  14. - send destroy in response to create cells
  15. - send end (policy failed) in response to begin cells
  16. - close an OR conn when it has no circuits
  17. hibernating, phase 2:
  18. (entered when bandwidth hard limit reached)
  19. - close all OR/AP/exit conns)
  20. */
  21. #include "or.h"
  22. #define HIBERNATE_STATE_LIVE 1
  23. #define HIBERNATE_STATE_EXITING 2
  24. #define HIBERNATE_STATE_LOWBANDWIDTH 3
  25. #define HIBERNATE_STATE_DORMANT 4
  26. extern long stats_n_seconds_working; /* published uptime */
  27. static int hibernate_state = HIBERNATE_STATE_LIVE;
  28. /** If are hibernating, when do we plan to wake up? Set to 0 if we
  29. * aren't hibernating. */
  30. static time_t hibernate_end_time = 0;
  31. typedef enum {
  32. UNIT_MONTH=1, UNIT_WEEK=2, UNIT_DAY=3,
  33. } time_unit_t;
  34. /* Fields for accounting logic. Accounting overview:
  35. *
  36. * Accounting is designed to ensure that no more than N bytes are sent
  37. * in either direction over a given interval (currently, one month,
  38. * starting at 0:00 GMT an arbitrary day within the month). We could
  39. * try to do this by choking our bandwidth to a trickle, but that
  40. * would make our streams useless. Instead, we estimate what our
  41. * bandwidth usage will be, and guess how long we'll be able to
  42. * provide that much bandwidth before hitting our limit. We then
  43. * choose a random time within the accounting interval to come up (so
  44. * that we don't get 50 Tors running on the 1st of the month and none
  45. * on the 30th).
  46. *
  47. * Each interval runs as follows:
  48. *
  49. * 1. We guess our bandwidth usage, based on how much we used
  50. * last time. We choose a "wakeup time" within the interval to come up.
  51. * 2. Until the chosen wakeup time, we hibernate.
  52. * 3. We come up at the wakeup time, and provide bandwidth until we are
  53. * "very close" to running out.
  54. * 4. Then we go into low-bandwidth mode, and stop accepting new
  55. * connections, but provide bandwidth until we run out.
  56. * 5. Then we hibernate until the end of the interval.
  57. *
  58. * If the interval ends before we run out of bandwidth, we go back to
  59. * step one.
  60. */
  61. /** How many bytes have we read/written in this accounting interval? */
  62. static uint64_t n_bytes_read_in_interval = 0;
  63. static uint64_t n_bytes_written_in_interval = 0;
  64. /** How many seconds have we been running this interval? */
  65. static uint32_t n_seconds_active_in_interval = 0;
  66. /** When did this accounting interval start? */
  67. static time_t interval_start_time = 0;
  68. /** When will this accounting interval end? */
  69. static time_t interval_end_time = 0;
  70. /** How far into the accounting interval should we hibernate? */
  71. static time_t interval_wakeup_time = 0;
  72. /** How much bandwidth do we 'expect' to use per minute? (0 if we have no
  73. * info from the last period.) */
  74. static uint32_t expected_bandwidth_usage = 0;
  75. /** What unit are we using for our accounting? */
  76. static time_unit_t cfg_unit = UNIT_MONTH;
  77. /** How many days,hours,minutes into each unit does our accounting interval
  78. * start? */
  79. static int cfg_start_day = 0;
  80. static int cfg_start_hour = 0;
  81. static int cfg_start_min = 0;
  82. static void reset_accounting(time_t now);
  83. static int read_bandwidth_usage(void);
  84. static time_t start_of_accounting_period_after(time_t now);
  85. static time_t start_of_accounting_period_containing(time_t now);
  86. static void accounting_set_wakeup_time(void);
  87. /* ************
  88. * Functions for bandwidth accounting.
  89. * ************/
  90. /** Configure accounting start/end time settings based on
  91. * options->AccountingStart. Return 0 on success, -1 on failure. If
  92. * <b>validate_only</b> is true, do not change the current settings. */
  93. int
  94. accounting_parse_options(or_options_t *options, int validate_only)
  95. {
  96. time_unit_t unit;
  97. int ok, idx;
  98. long d,h,m;
  99. smartlist_t *items;
  100. const char *v = options->AccountingStart;
  101. const char *s;
  102. char *cp;
  103. if (!v) {
  104. if (!validate_only) {
  105. cfg_unit = UNIT_MONTH;
  106. cfg_start_day = 1;
  107. cfg_start_hour = 0;
  108. cfg_start_min = 0;
  109. }
  110. return 0;
  111. }
  112. items = smartlist_create();
  113. smartlist_split_string(items, v, NULL,
  114. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK,0);
  115. if (smartlist_len(items)<2) {
  116. log_fn(LOG_WARN, "Too few arguments to AccountingStart");
  117. goto err;
  118. }
  119. s = smartlist_get(items,0);
  120. if (0==strcasecmp(s, "month")) {
  121. unit = UNIT_MONTH;
  122. } else if (0==strcasecmp(s, "week")) {
  123. unit = UNIT_WEEK;
  124. } else if (0==strcasecmp(s, "day")) {
  125. unit = UNIT_DAY;
  126. } else {
  127. log_fn(LOG_WARN, "Unrecognized accounting unit '%s': only 'month', 'week', and 'day' are supported.", s);
  128. goto err;
  129. }
  130. switch (unit) {
  131. case UNIT_WEEK:
  132. d = tor_parse_long(smartlist_get(items,1), 10, 1, 7, &ok, NULL);
  133. if (!ok) {
  134. log_fn(LOG_WARN, "Weekly accounting must start begin on a day between 1(Monday) and 7 (Sunday)");
  135. goto err;
  136. }
  137. break;
  138. case UNIT_MONTH:
  139. d = tor_parse_long(smartlist_get(items,1), 10, 1, 28, &ok, NULL);
  140. if (!ok) {
  141. log_fn(LOG_WARN, "Monthly accounting must start begin on a day between 1 and 28");
  142. goto err;
  143. }
  144. break;
  145. case UNIT_DAY:
  146. d = 0;
  147. break;
  148. default:
  149. tor_assert(0);
  150. }
  151. idx = unit==UNIT_DAY?1:2;
  152. if (smartlist_len(items) != (idx+1)) {
  153. log_fn(LOG_WARN, "Accounting unit '%s' requires %d arguments",
  154. s, idx+1);
  155. goto err;
  156. }
  157. s = smartlist_get(items, idx);
  158. h = tor_parse_long(s, 10, 0, 23, &ok, &cp);
  159. if (!ok) {
  160. log_fn(LOG_WARN, "Accounting start time not parseable: bad hour.");
  161. goto err;
  162. }
  163. if (!cp || *cp!=':') {
  164. log_fn(LOG_WARN,"Accounting start time not parseable: not in HH:MM format");
  165. goto err;
  166. }
  167. m = tor_parse_long(cp+1, 10, 0, 59, &ok, &cp);
  168. if (!ok) {
  169. log_fn(LOG_WARN, "Accounting start time not parseable: bad minute");
  170. goto err;
  171. }
  172. if (!cp || *cp!='\0') {
  173. log_fn(LOG_WARN,"Accounting start time not parseable: not in HH:MM format");
  174. goto err;
  175. }
  176. if (!validate_only) {
  177. cfg_unit = unit;
  178. cfg_start_day = (int)d;
  179. cfg_start_hour = (int)h;
  180. cfg_start_min = (int)m;
  181. }
  182. SMARTLIST_FOREACH(items, char *, s, tor_free(s));
  183. smartlist_free(items);
  184. return 0;
  185. err:
  186. SMARTLIST_FOREACH(items, char *, s, tor_free(s));
  187. smartlist_free(items);
  188. return -1;
  189. }
  190. /** If we want to manage the accounting system and potentially
  191. * hibernate, return 1, else return 0.
  192. */
  193. int
  194. accounting_is_enabled(or_options_t *options)
  195. {
  196. if (options->AccountingMax)
  197. return 1;
  198. return 0;
  199. }
  200. /** Called from main.c to tell us that <b>seconds</b> seconds have
  201. * passed, <b>n_read</b> bytes have been read, and <b>n_written</b>
  202. * bytes have been written. */
  203. void
  204. accounting_add_bytes(size_t n_read, size_t n_written, int seconds)
  205. {
  206. n_bytes_read_in_interval += n_read;
  207. n_bytes_written_in_interval += n_written;
  208. /* If we haven't been called in 10 seconds, we're probably jumping
  209. * around in time. */
  210. n_seconds_active_in_interval += (seconds < 10) ? seconds : 0;
  211. }
  212. /** If get_end, return the end of the accounting period that contains
  213. * the time <b>now</b>. Else, return the start of the accounting
  214. * period that contains the time <b>now</b> */
  215. static time_t
  216. edge_of_accounting_period_containing(time_t now, int get_end)
  217. {
  218. int before;
  219. struct tm tm;
  220. tor_localtime_r(&now, &tm);
  221. /* Set 'before' to true iff the current time is before the hh:mm
  222. * changeover time for today. */
  223. before = tm.tm_hour < cfg_start_hour ||
  224. (tm.tm_hour == cfg_start_hour && tm.tm_min < cfg_start_min);
  225. /* Dispatch by unit. First, find the start day of the given period;
  226. * then, if get_end is true, increment to the end day. */
  227. switch (cfg_unit)
  228. {
  229. case UNIT_MONTH: {
  230. /* If this is before the Nth, we want the Nth of last month. */
  231. if (tm.tm_mday < cfg_start_day ||
  232. (tm.tm_mday < cfg_start_day && before)) {
  233. --tm.tm_mon;
  234. }
  235. /* Otherwise, the month is correct. */
  236. tm.tm_mday = cfg_start_day;
  237. if (get_end)
  238. ++tm.tm_mon;
  239. break;
  240. }
  241. case UNIT_WEEK: {
  242. /* What is the 'target' day of the week in struct tm format? (We
  243. say Sunday==7; struct tm says Sunday==0.) */
  244. int wday = cfg_start_day % 7;
  245. /* How many days do we subtract from today to get to the right day? */
  246. int delta = (7+tm.tm_wday-wday)%7;
  247. /* If we are on the right day, but the changeover hasn't happened yet,
  248. * then subtract a whole week. */
  249. if (delta == 0 && before)
  250. delta = 7;
  251. tm.tm_mday -= delta;
  252. if (get_end)
  253. tm.tm_mday += 7;
  254. break;
  255. }
  256. case UNIT_DAY:
  257. if (before)
  258. --tm.tm_mday;
  259. if (get_end)
  260. ++tm.tm_mday;
  261. break;
  262. default:
  263. tor_assert(0);
  264. }
  265. tm.tm_hour = cfg_start_hour;
  266. tm.tm_min = cfg_start_min;
  267. tm.tm_sec = 0;
  268. tm.tm_isdst = -1; /* Autodetect DST */
  269. return mktime(&tm);
  270. }
  271. /** Return the start of the accounting period containing the time
  272. * <b>now</b>. */
  273. static time_t
  274. start_of_accounting_period_containing(time_t now)
  275. {
  276. return edge_of_accounting_period_containing(now, 0);
  277. }
  278. /** Return the start of the accounting period that comes after the one
  279. * containing the time <b>now</b>. */
  280. static time_t
  281. start_of_accounting_period_after(time_t now)
  282. {
  283. return edge_of_accounting_period_containing(now, 1);
  284. }
  285. /** Initialize the accounting subsystem. */
  286. void
  287. configure_accounting(time_t now)
  288. {
  289. /* Try to remember our recorded usage. */
  290. if (!interval_start_time)
  291. read_bandwidth_usage(); /* If we fail, we'll leave values at zero, and
  292. * reset below.*/
  293. if (!interval_start_time ||
  294. start_of_accounting_period_after(interval_start_time) <= now) {
  295. /* We didn't have recorded usage, or we don't have recorded usage
  296. * for this interval. Start a new interval. */
  297. log_fn(LOG_INFO, "Starting new accounting interval.");
  298. reset_accounting(now);
  299. } else if (interval_start_time ==
  300. start_of_accounting_period_containing(interval_start_time)) {
  301. log_fn(LOG_INFO, "Continuing accounting interval.");
  302. /* We are in the interval we thought we were in. Do nothing.*/
  303. interval_end_time = start_of_accounting_period_after(interval_start_time);
  304. } else {
  305. log_fn(LOG_WARN, "Mismatched accounting interval; starting a fresh one.");
  306. reset_accounting(now);
  307. }
  308. accounting_set_wakeup_time();
  309. }
  310. /** Set expected_bandwidth_usage based on how much we sent/received
  311. * per minute last interval (if we were up for at least 30 minutes),
  312. * or based on our declared bandwidth otherwise. */
  313. static void
  314. update_expected_bandwidth(void)
  315. {
  316. uint64_t used, expected;
  317. uint64_t max_configured = (get_options()->BandwidthRate * 60);
  318. if (n_seconds_active_in_interval < 1800) {
  319. /* If we haven't gotten enough data last interval, set 'expected'
  320. * to 0. This will set our wakeup to the start of the interval.
  321. * Next interval, we'll choose our starting time based on how much
  322. * we sent this interval.
  323. */
  324. expected = 0;
  325. } else {
  326. used = n_bytes_written_in_interval < n_bytes_read_in_interval ?
  327. n_bytes_read_in_interval : n_bytes_written_in_interval;
  328. expected = used / (n_seconds_active_in_interval / 60);
  329. if (expected > max_configured)
  330. expected = max_configured;
  331. }
  332. if (expected > UINT32_MAX)
  333. expected = UINT32_MAX;
  334. expected_bandwidth_usage = (uint32_t) expected;
  335. }
  336. /** Called at the start of a new accounting interval: reset our
  337. * expected bandwidth usage based on what happened last time, set up
  338. * the start and end of the interval, and clear byte/time totals.
  339. */
  340. static void
  341. reset_accounting(time_t now)
  342. {
  343. log_fn(LOG_INFO, "Starting new accounting interval.");
  344. update_expected_bandwidth();
  345. interval_start_time = start_of_accounting_period_containing(now);
  346. interval_end_time = start_of_accounting_period_after(interval_start_time);
  347. n_bytes_read_in_interval = 0;
  348. n_bytes_written_in_interval = 0;
  349. n_seconds_active_in_interval = 0;
  350. }
  351. /** Return true iff we should save our bandwidth usage to disk. */
  352. static INLINE int
  353. time_to_record_bandwidth_usage(time_t now)
  354. {
  355. /* Note every 60 sec */
  356. #define NOTE_INTERVAL (60)
  357. /* Or every 20 megabytes */
  358. #define NOTE_BYTES 20*(1024*1024)
  359. static uint64_t last_read_bytes_noted = 0;
  360. static uint64_t last_written_bytes_noted = 0;
  361. static time_t last_time_noted = 0;
  362. if (last_time_noted + NOTE_INTERVAL <= now ||
  363. last_read_bytes_noted + NOTE_BYTES <= n_bytes_read_in_interval ||
  364. last_written_bytes_noted + NOTE_BYTES <= n_bytes_written_in_interval ||
  365. (interval_end_time && interval_end_time <= now)) {
  366. last_time_noted = now;
  367. last_read_bytes_noted = n_bytes_read_in_interval;
  368. last_written_bytes_noted = n_bytes_written_in_interval;
  369. return 1;
  370. }
  371. return 0;
  372. }
  373. /** Invoked once per second. Checks whether it is time to hibernate,
  374. * record bandwidth used, etc. */
  375. void
  376. accounting_run_housekeeping(time_t now)
  377. {
  378. if (now >= interval_end_time) {
  379. configure_accounting(now);
  380. }
  381. if (time_to_record_bandwidth_usage(now)) {
  382. if (accounting_record_bandwidth_usage(now)) {
  383. log_fn(LOG_ERR, "Couldn't record bandwidth usage; exiting.");
  384. /* XXX this can fail when you're out of fd's, causing a crash.
  385. * Perhaps the better answer is to hold the file open all the
  386. * time? */
  387. exit(1);
  388. }
  389. }
  390. }
  391. /** Based on our interval and our estimated bandwidth, choose a
  392. * deterministic (but random-ish) time to wake up. */
  393. static void
  394. accounting_set_wakeup_time(void)
  395. {
  396. char buf[ISO_TIME_LEN+1];
  397. char digest[DIGEST_LEN];
  398. crypto_digest_env_t *d_env;
  399. int time_in_interval;
  400. int time_to_exhaust_bw;
  401. int time_to_consider;
  402. if (! identity_key_is_set()) {
  403. if (init_keys() < 0) {
  404. log_fn(LOG_ERR, "Error initializing keys");
  405. tor_assert(0);
  406. }
  407. }
  408. format_iso_time(buf, interval_start_time);
  409. crypto_pk_get_digest(get_identity_key(), digest);
  410. d_env = crypto_new_digest_env();
  411. crypto_digest_add_bytes(d_env, buf, ISO_TIME_LEN);
  412. crypto_digest_add_bytes(d_env, digest, DIGEST_LEN);
  413. crypto_digest_get_digest(d_env, digest, DIGEST_LEN);
  414. crypto_free_digest_env(d_env);
  415. if (!expected_bandwidth_usage) {
  416. char buf1[ISO_TIME_LEN+1];
  417. char buf2[ISO_TIME_LEN+1];
  418. format_local_iso_time(buf1, interval_start_time);
  419. format_local_iso_time(buf2, interval_end_time);
  420. time_to_exhaust_bw = 24*60*60;
  421. interval_wakeup_time = interval_start_time;
  422. log_fn(LOG_NOTICE, "Configured hibernation. This interval begins at %s "
  423. "and ends at %s. We have no prior estimate for bandwidth, so "
  424. "we will start out awake and hibernate when we exhaust our quota.",
  425. buf1, buf2);
  426. return;
  427. }
  428. time_to_exhaust_bw = (int)
  429. (get_options()->AccountingMax/expected_bandwidth_usage)*60;
  430. time_in_interval = interval_end_time - interval_start_time;
  431. time_to_consider = time_in_interval - time_to_exhaust_bw;
  432. if (time_to_consider<=0) {
  433. interval_wakeup_time = interval_start_time;
  434. } else {
  435. /* XXX can we simplify this just by picking a random (non-deterministic)
  436. * time to be up? If we go down and come up, then we pick a new one. Is
  437. * that good enough? -RD */
  438. /* This is not a perfectly unbiased conversion, but it is good enough:
  439. * in the worst case, the first half of the day is 0.06 percent likelier
  440. * to be chosen than the last half. */
  441. interval_wakeup_time = interval_start_time +
  442. (get_uint32(digest) % time_to_consider);
  443. format_iso_time(buf, interval_wakeup_time);
  444. }
  445. {
  446. char buf1[ISO_TIME_LEN+1];
  447. char buf2[ISO_TIME_LEN+1];
  448. char buf3[ISO_TIME_LEN+1];
  449. char buf4[ISO_TIME_LEN+1];
  450. time_t down_time = interval_wakeup_time+time_to_exhaust_bw;
  451. if (down_time>interval_end_time)
  452. down_time = interval_end_time;
  453. format_local_iso_time(buf1, interval_start_time);
  454. format_local_iso_time(buf2, interval_wakeup_time);
  455. format_local_iso_time(buf3,
  456. down_time<interval_end_time?down_time:interval_end_time);
  457. format_local_iso_time(buf4, interval_end_time);
  458. log_fn(LOG_NOTICE, "Configured hibernation. This interval began at %s; "
  459. "the scheduled wake-up time %s %s; "
  460. "we expect%s to exhaust our quota for this interval around %s; "
  461. "the next interval begins at %s (all times local)",
  462. buf1,
  463. time(NULL)<interval_wakeup_time?"is":"was", buf2,
  464. time(NULL)<down_time?"":"ed", buf3,
  465. buf4);
  466. }
  467. }
  468. #define BW_ACCOUNTING_VERSION 1
  469. /** Save all our bandwidth tracking information to disk. Return 0 on
  470. * success, -1 on failure*/
  471. int
  472. accounting_record_bandwidth_usage(time_t now)
  473. {
  474. char buf[128];
  475. char fname[512];
  476. char time1[ISO_TIME_LEN+1];
  477. char time2[ISO_TIME_LEN+1];
  478. char *cp = buf;
  479. /* Format is:
  480. Version\nTime\nTime\nRead\nWrite\nSeconds\nExpected-Rate\n */
  481. format_iso_time(time1, interval_start_time);
  482. format_iso_time(time2, now);
  483. tor_snprintf(cp, sizeof(buf),
  484. "%d\n%s\n%s\n"U64_FORMAT"\n"U64_FORMAT"\n%lu\n%lu\n",
  485. BW_ACCOUNTING_VERSION,
  486. time1,
  487. time2,
  488. U64_PRINTF_ARG(n_bytes_read_in_interval),
  489. U64_PRINTF_ARG(n_bytes_written_in_interval),
  490. (unsigned long)n_seconds_active_in_interval,
  491. (unsigned long)expected_bandwidth_usage);
  492. tor_snprintf(fname, sizeof(fname), "%s/bw_accounting",
  493. get_options()->DataDirectory);
  494. return write_str_to_file(fname, buf, 0);
  495. }
  496. /** Read stored accounting information from disk. Return 0 on success;
  497. * return -1 and change nothing on failure. */
  498. static int
  499. read_bandwidth_usage(void)
  500. {
  501. char *s = NULL;
  502. char fname[512];
  503. time_t t1, t2;
  504. uint64_t n_read, n_written;
  505. uint32_t expected_bw, n_seconds;
  506. smartlist_t *elts;
  507. int ok;
  508. tor_snprintf(fname, sizeof(fname), "%s/bw_accounting",
  509. get_options()->DataDirectory);
  510. if (!(s = read_file_to_str(fname, 0))) {
  511. return 0;
  512. }
  513. elts = smartlist_create();
  514. smartlist_split_string(elts, s, "\n", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK,0);
  515. tor_free(s);
  516. if (smartlist_len(elts)<1 ||
  517. atoi(smartlist_get(elts,0)) != BW_ACCOUNTING_VERSION) {
  518. log_fn(LOG_WARN, "Unrecognized bw_accounting file version: %s",
  519. (const char*)smartlist_get(elts,0));
  520. goto err;
  521. }
  522. if (smartlist_len(elts) < 7) {
  523. log_fn(LOG_WARN, "Corrupted bw_accounting file: %d lines",
  524. smartlist_len(elts));
  525. goto err;
  526. }
  527. if (parse_iso_time(smartlist_get(elts,1), &t1)) {
  528. log_fn(LOG_WARN, "Error parsing bandwidth usage start time.");
  529. goto err;
  530. }
  531. if (parse_iso_time(smartlist_get(elts,2), &t2)) {
  532. log_fn(LOG_WARN, "Error parsing bandwidth usage last-written time");
  533. goto err;
  534. }
  535. n_read = tor_parse_uint64(smartlist_get(elts,3), 10, 0, UINT64_MAX,
  536. &ok, NULL);
  537. if (!ok) {
  538. log_fn(LOG_WARN, "Error parsing number of bytes read");
  539. goto err;
  540. }
  541. n_written = tor_parse_uint64(smartlist_get(elts,4), 10, 0, UINT64_MAX,
  542. &ok, NULL);
  543. if (!ok) {
  544. log_fn(LOG_WARN, "Error parsing number of bytes read");
  545. goto err;
  546. }
  547. n_seconds = (uint32_t)tor_parse_ulong(smartlist_get(elts,5), 10,0,ULONG_MAX,
  548. &ok, NULL);
  549. if (!ok) {
  550. log_fn(LOG_WARN, "Error parsing number of seconds live");
  551. goto err;
  552. }
  553. expected_bw =(uint32_t)tor_parse_ulong(smartlist_get(elts,6), 10,0,ULONG_MAX,
  554. &ok, NULL);
  555. if (!ok) {
  556. log_fn(LOG_WARN, "Error parsing expected bandwidth");
  557. goto err;
  558. }
  559. n_bytes_read_in_interval = n_read;
  560. n_bytes_written_in_interval = n_written;
  561. n_seconds_active_in_interval = n_seconds;
  562. interval_start_time = t1;
  563. expected_bandwidth_usage = expected_bw;
  564. log_fn(LOG_INFO, "Successfully read bandwidth accounting file written at %s for interval starting at %s. We have been active for %lu seconds in this interval. At the start of the interval, we expected to use about %lu KB per second. ("U64_FORMAT" bytes read so far, "U64_FORMAT" bytes written so far)",
  565. (char*)smartlist_get(elts,2),
  566. (char*)smartlist_get(elts,1),
  567. (unsigned long)n_seconds_active_in_interval,
  568. (unsigned long)((uint64_t)expected_bandwidth_usage*1024/60),
  569. U64_PRINTF_ARG(n_bytes_read_in_interval),
  570. U64_PRINTF_ARG(n_bytes_written_in_interval));
  571. SMARTLIST_FOREACH(elts, char *, cp, tor_free(cp));
  572. smartlist_free(elts);
  573. return 0;
  574. err:
  575. SMARTLIST_FOREACH(elts, char *, cp, tor_free(cp));
  576. smartlist_free(elts);
  577. return -1;
  578. }
  579. /** Return true iff we have sent/received all the bytes we are willing
  580. * to send/receive this interval. */
  581. static int
  582. hibernate_hard_limit_reached(void)
  583. {
  584. uint64_t hard_limit = get_options()->AccountingMax;
  585. if (!hard_limit)
  586. return 0;
  587. return n_bytes_read_in_interval >= hard_limit
  588. || n_bytes_written_in_interval >= hard_limit;
  589. }
  590. /** Return true iff we have sent/received almost all the bytes we are willing
  591. * to send/receive this interval. */
  592. static int
  593. hibernate_soft_limit_reached(void)
  594. {
  595. uint64_t soft_limit = (uint64_t) ((get_options()->AccountingMax) * .95);
  596. if (!soft_limit)
  597. return 0;
  598. return n_bytes_read_in_interval >= soft_limit
  599. || n_bytes_written_in_interval >= soft_limit;
  600. }
  601. /** Called when we get a SIGINT, or when bandwidth soft limit is
  602. * reached. Puts us into "loose hibernation": we don't accept new
  603. * connections, but we continue handling old ones. */
  604. static void
  605. hibernate_begin(int new_state, time_t now)
  606. {
  607. connection_t *conn;
  608. or_options_t *options = get_options();
  609. if (new_state == HIBERNATE_STATE_EXITING &&
  610. hibernate_state != HIBERNATE_STATE_LIVE) {
  611. log(LOG_NOTICE,"Sigint received %s; exiting now.",
  612. hibernate_state == HIBERNATE_STATE_EXITING ?
  613. "a second time" : "while hibernating");
  614. tor_cleanup();
  615. exit(0);
  616. }
  617. /* close listeners. leave control listener(s). */
  618. while ((conn = connection_get_by_type(CONN_TYPE_OR_LISTENER)) ||
  619. (conn = connection_get_by_type(CONN_TYPE_AP_LISTENER)) ||
  620. (conn = connection_get_by_type(CONN_TYPE_DIR_LISTENER))) {
  621. log_fn(LOG_INFO,"Closing listener type %d", conn->type);
  622. connection_mark_for_close(conn);
  623. }
  624. /* XXX kill intro point circs */
  625. /* XXX upload rendezvous service descriptors with no intro points */
  626. if (new_state == HIBERNATE_STATE_EXITING) {
  627. log(LOG_NOTICE,"Interrupt: will shut down in %d seconds. Interrupt again to exit now.", options->ShutdownWaitLength);
  628. hibernate_end_time = time(NULL) + options->ShutdownWaitLength;
  629. } else { /* soft limit reached */
  630. hibernate_end_time = interval_end_time;
  631. }
  632. hibernate_state = new_state;
  633. accounting_record_bandwidth_usage(now);
  634. }
  635. /** Called when we've been hibernating and our timeout is reached. */
  636. static void
  637. hibernate_end(int new_state)
  638. {
  639. tor_assert(hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH ||
  640. hibernate_state == HIBERNATE_STATE_DORMANT);
  641. /* listeners will be relaunched in run_scheduled_events() in main.c */
  642. log_fn(LOG_NOTICE,"Hibernation period ended. Resuming normal activity.");
  643. hibernate_state = new_state;
  644. hibernate_end_time = 0; /* no longer hibernating */
  645. stats_n_seconds_working = 0; /* reset published uptime */
  646. }
  647. /** A wrapper around hibernate_begin, for when we get SIGINT. */
  648. void
  649. hibernate_begin_shutdown(void)
  650. {
  651. hibernate_begin(HIBERNATE_STATE_EXITING, time(NULL));
  652. }
  653. /** Return true iff we are currently hibernating. */
  654. int
  655. we_are_hibernating(void)
  656. {
  657. return hibernate_state != HIBERNATE_STATE_LIVE;
  658. }
  659. /** If we aren't currently dormant, close all connections and become
  660. * dormant. */
  661. static void
  662. hibernate_go_dormant(time_t now)
  663. {
  664. connection_t *conn;
  665. if (hibernate_state == HIBERNATE_STATE_DORMANT)
  666. return;
  667. else if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH)
  668. hibernate_state = HIBERNATE_STATE_DORMANT;
  669. else
  670. hibernate_begin(HIBERNATE_STATE_DORMANT, now);
  671. log_fn(LOG_NOTICE,"Going dormant. Blowing away remaining connections.");
  672. /* Close all OR/AP/exit conns. Leave dir conns because we still want
  673. * to be able to upload server descriptors so people know we're still
  674. * running, and download directories so we can detect if we're obsolete.
  675. * Leave control conns because we still want to be controllable.
  676. */
  677. while ((conn = connection_get_by_type(CONN_TYPE_OR)) ||
  678. (conn = connection_get_by_type(CONN_TYPE_AP)) ||
  679. (conn = connection_get_by_type(CONN_TYPE_EXIT))) {
  680. if (CONN_IS_EDGE(conn))
  681. connection_edge_end(conn, END_STREAM_REASON_HIBERNATING,
  682. conn->cpath_layer);
  683. log_fn(LOG_INFO,"Closing conn type %d", conn->type);
  684. if (conn->type == CONN_TYPE_AP) /* send socks failure if needed */
  685. connection_mark_unattached_ap(conn, END_STREAM_REASON_HIBERNATING);
  686. else
  687. connection_mark_for_close(conn);
  688. }
  689. accounting_record_bandwidth_usage(now);
  690. }
  691. /** Called when hibernate_end_time has arrived. */
  692. static void
  693. hibernate_end_time_elapsed(time_t now)
  694. {
  695. char buf[ISO_TIME_LEN+1];
  696. /* The interval has ended, or it is wakeup time. Find out which. */
  697. accounting_run_housekeeping(now);
  698. if (interval_wakeup_time <= now) {
  699. /* The interval hasn't changed, but interval_wakeup_time has passed.
  700. * It's time to wake up and start being a server. */
  701. hibernate_end(HIBERNATE_STATE_LIVE);
  702. return;
  703. } else {
  704. /* The interval has changed, and it isn't time to wake up yet. */
  705. hibernate_end_time = interval_wakeup_time;
  706. format_iso_time(buf,interval_wakeup_time);
  707. if (hibernate_state != HIBERNATE_STATE_DORMANT) {
  708. /* We weren't sleeping before; we should sleep now. */
  709. log_fn(LOG_NOTICE, "Accounting period ended. Commencing hibernation until %s GMT",buf);
  710. hibernate_go_dormant(now);
  711. } else {
  712. log_fn(LOG_NOTICE, "Accounting period ended. This period, we will hibernate until %s GMT",buf);
  713. }
  714. }
  715. }
  716. /** Consider our environment and decide if it's time
  717. * to start/stop hibernating.
  718. */
  719. void
  720. consider_hibernation(time_t now)
  721. {
  722. int accounting_enabled = get_options()->AccountingMax != 0;
  723. char buf[ISO_TIME_LEN+1];
  724. /* If we're in 'exiting' mode, then we just shut down after the interval
  725. * elapses. */
  726. if (hibernate_state == HIBERNATE_STATE_EXITING) {
  727. tor_assert(hibernate_end_time);
  728. if (hibernate_end_time <= now) {
  729. log(LOG_NOTICE,"Clean shutdown finished. Exiting.");
  730. tor_cleanup();
  731. exit(0);
  732. }
  733. return; /* if exiting soon, don't worry about bandwidth limits */
  734. }
  735. if (hibernate_state == HIBERNATE_STATE_DORMANT) {
  736. /* We've been hibernating because of bandwidth accounting. */
  737. tor_assert(hibernate_end_time);
  738. if (hibernate_end_time > now && accounting_enabled) {
  739. /* If we're hibernating, don't wake up until it's time, regardless of
  740. * whether we're in a new interval. */
  741. return ;
  742. } else {
  743. hibernate_end_time_elapsed(now);
  744. }
  745. }
  746. /* Else, we aren't hibernating. See if it's time to start hibernating, or to
  747. * go dormant. */
  748. if (hibernate_state == HIBERNATE_STATE_LIVE) {
  749. if (hibernate_soft_limit_reached()) {
  750. log_fn(LOG_NOTICE,"Bandwidth soft limit reached; commencing hibernation.");
  751. hibernate_begin(HIBERNATE_STATE_LOWBANDWIDTH, now);
  752. } else if (accounting_enabled && now < interval_wakeup_time) {
  753. format_iso_time(buf,interval_wakeup_time);
  754. log_fn(LOG_NOTICE, "Commencing hibernation. We will wake up at %s GMT",buf);
  755. hibernate_go_dormant(now);
  756. }
  757. }
  758. if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH) {
  759. if (!accounting_enabled) {
  760. hibernate_end_time_elapsed(now);
  761. } else if (hibernate_hard_limit_reached()) {
  762. hibernate_go_dormant(now);
  763. } else if (hibernate_end_time <= now) {
  764. /* The hibernation period ended while we were still in lowbandwidth.*/
  765. hibernate_end_time_elapsed(now);
  766. }
  767. }
  768. }