hibernate.c 30 KB

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