hibernate.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. time_t tmp;
  501. /* Format is:
  502. Version\nTime\nTime\nRead\nWrite\nSeconds\nExpected-Rate\n */
  503. format_iso_time(time1, interval_start_time);
  504. format_iso_time(time2, now);
  505. /* now check to see if they're valid times -- if they're not,
  506. * and we write them, then tor will refuse to start next time. */
  507. if (parse_iso_time(time1, &tmp) || parse_iso_time(time2, &tmp)) {
  508. log_warn(LD_ACCT, "Created a time that we refused to parse.");
  509. return -1;
  510. }
  511. tor_snprintf(cp, sizeof(buf),
  512. "%d\n%s\n%s\n"U64_FORMAT"\n"U64_FORMAT"\n%lu\n%lu\n",
  513. BW_ACCOUNTING_VERSION,
  514. time1,
  515. time2,
  516. U64_PRINTF_ARG(n_bytes_read_in_interval),
  517. U64_PRINTF_ARG(n_bytes_written_in_interval),
  518. (unsigned long)n_seconds_active_in_interval,
  519. (unsigned long)expected_bandwidth_usage);
  520. tor_snprintf(fname, sizeof(fname), "%s/bw_accounting",
  521. get_options()->DataDirectory);
  522. return write_str_to_file(fname, buf, 0);
  523. }
  524. /** Read stored accounting information from disk. Return 0 on success;
  525. * return -1 and change nothing on failure. */
  526. static int
  527. read_bandwidth_usage(void)
  528. {
  529. char *s = NULL;
  530. char fname[512];
  531. time_t t1, t2;
  532. uint64_t n_read, n_written;
  533. uint32_t expected_bw, n_seconds;
  534. smartlist_t *elts;
  535. int ok;
  536. tor_snprintf(fname, sizeof(fname), "%s/bw_accounting",
  537. get_options()->DataDirectory);
  538. if (!(s = read_file_to_str(fname, 0, NULL))) {
  539. return 0;
  540. }
  541. elts = smartlist_create();
  542. smartlist_split_string(elts, s, "\n", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK,0);
  543. tor_free(s);
  544. if (smartlist_len(elts)<1 ||
  545. atoi(smartlist_get(elts,0)) != BW_ACCOUNTING_VERSION) {
  546. log_warn(LD_ACCT, "Unrecognized bw_accounting file version: %s",
  547. (const char*)smartlist_get(elts,0));
  548. goto err;
  549. }
  550. if (smartlist_len(elts) < 7) {
  551. log_warn(LD_ACCT, "Corrupted bw_accounting file: %d lines",
  552. smartlist_len(elts));
  553. goto err;
  554. }
  555. if (parse_iso_time(smartlist_get(elts,1), &t1)) {
  556. log_warn(LD_ACCT, "Error parsing bandwidth usage start time.");
  557. goto err;
  558. }
  559. if (parse_iso_time(smartlist_get(elts,2), &t2)) {
  560. log_warn(LD_ACCT, "Error parsing bandwidth usage last-written time");
  561. goto err;
  562. }
  563. n_read = tor_parse_uint64(smartlist_get(elts,3), 10, 0, UINT64_MAX,
  564. &ok, NULL);
  565. if (!ok) {
  566. log_warn(LD_ACCT, "Error parsing number of bytes read");
  567. goto err;
  568. }
  569. n_written = tor_parse_uint64(smartlist_get(elts,4), 10, 0, UINT64_MAX,
  570. &ok, NULL);
  571. if (!ok) {
  572. log_warn(LD_ACCT, "Error parsing number of bytes written");
  573. goto err;
  574. }
  575. n_seconds = (uint32_t)tor_parse_ulong(smartlist_get(elts,5), 10,0,ULONG_MAX,
  576. &ok, NULL);
  577. if (!ok) {
  578. log_warn(LD_ACCT, "Error parsing number of seconds live");
  579. goto err;
  580. }
  581. expected_bw =(uint32_t)tor_parse_ulong(smartlist_get(elts,6), 10,0,ULONG_MAX,
  582. &ok, NULL);
  583. if (!ok) {
  584. log_warn(LD_ACCT, "Error parsing expected bandwidth");
  585. goto err;
  586. }
  587. n_bytes_read_in_interval = n_read;
  588. n_bytes_written_in_interval = n_written;
  589. n_seconds_active_in_interval = n_seconds;
  590. interval_start_time = t1;
  591. expected_bandwidth_usage = expected_bw;
  592. log_info(LD_ACCT,
  593. "Successfully read bandwidth accounting file written at %s "
  594. "for interval starting at %s. We have been active for %lu seconds in "
  595. "this interval. At the start of the interval, we expected to use "
  596. "about %lu KB per second. ("U64_FORMAT" bytes read so far, "
  597. U64_FORMAT" bytes written so far)",
  598. (char*)smartlist_get(elts,2),
  599. (char*)smartlist_get(elts,1),
  600. (unsigned long)n_seconds_active_in_interval,
  601. (unsigned long)((uint64_t)expected_bandwidth_usage*1024/60),
  602. U64_PRINTF_ARG(n_bytes_read_in_interval),
  603. U64_PRINTF_ARG(n_bytes_written_in_interval));
  604. SMARTLIST_FOREACH(elts, char *, cp, tor_free(cp));
  605. smartlist_free(elts);
  606. return 0;
  607. err:
  608. SMARTLIST_FOREACH(elts, char *, cp, tor_free(cp));
  609. smartlist_free(elts);
  610. return -1;
  611. }
  612. /** Return true iff we have sent/received all the bytes we are willing
  613. * to send/receive this interval. */
  614. static int
  615. hibernate_hard_limit_reached(void)
  616. {
  617. uint64_t hard_limit = get_options()->AccountingMax;
  618. if (!hard_limit)
  619. return 0;
  620. return n_bytes_read_in_interval >= hard_limit
  621. || n_bytes_written_in_interval >= hard_limit;
  622. }
  623. /** Return true iff we have sent/received almost all the bytes we are willing
  624. * to send/receive this interval. */
  625. static int
  626. hibernate_soft_limit_reached(void)
  627. {
  628. uint64_t soft_limit = DBL_TO_U64(U64_TO_DBL(get_options()->AccountingMax)
  629. * .95);
  630. if (!soft_limit)
  631. return 0;
  632. return n_bytes_read_in_interval >= soft_limit
  633. || n_bytes_written_in_interval >= soft_limit;
  634. }
  635. /** Called when we get a SIGINT, or when bandwidth soft limit is
  636. * reached. Puts us into "loose hibernation": we don't accept new
  637. * connections, but we continue handling old ones. */
  638. static void
  639. hibernate_begin(int new_state, time_t now)
  640. {
  641. connection_t *conn;
  642. or_options_t *options = get_options();
  643. if (new_state == HIBERNATE_STATE_EXITING &&
  644. hibernate_state != HIBERNATE_STATE_LIVE) {
  645. log_notice(LD_GENERAL,"Sigint received %s; exiting now.",
  646. hibernate_state == HIBERNATE_STATE_EXITING ?
  647. "a second time" : "while hibernating");
  648. tor_cleanup();
  649. exit(0);
  650. }
  651. /* close listeners. leave control listener(s). */
  652. while ((conn = connection_get_by_type(CONN_TYPE_OR_LISTENER)) ||
  653. (conn = connection_get_by_type(CONN_TYPE_AP_LISTENER)) ||
  654. (conn = connection_get_by_type(CONN_TYPE_AP_TRANS_LISTENER)) ||
  655. (conn = connection_get_by_type(CONN_TYPE_AP_NATD_LISTENER)) ||
  656. (conn = connection_get_by_type(CONN_TYPE_DIR_LISTENER))) {
  657. log_info(LD_NET,"Closing listener type %d", conn->type);
  658. connection_mark_for_close(conn);
  659. }
  660. /* XXX kill intro point circs */
  661. /* XXX upload rendezvous service descriptors with no intro points */
  662. if (new_state == HIBERNATE_STATE_EXITING) {
  663. log_notice(LD_GENERAL,"Interrupt: will shut down in %d seconds. Interrupt "
  664. "again to exit now.", options->ShutdownWaitLength);
  665. hibernate_end_time = time(NULL) + options->ShutdownWaitLength;
  666. } else { /* soft limit reached */
  667. hibernate_end_time = interval_end_time;
  668. }
  669. hibernate_state = new_state;
  670. accounting_record_bandwidth_usage(now);
  671. }
  672. /** Called when we've been hibernating and our timeout is reached. */
  673. static void
  674. hibernate_end(int new_state)
  675. {
  676. tor_assert(hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH ||
  677. hibernate_state == HIBERNATE_STATE_DORMANT);
  678. /* listeners will be relaunched in run_scheduled_events() in main.c */
  679. log_notice(LD_ACCT,"Hibernation period ended. Resuming normal activity.");
  680. hibernate_state = new_state;
  681. hibernate_end_time = 0; /* no longer hibernating */
  682. stats_n_seconds_working = 0; /* reset published uptime */
  683. }
  684. /** A wrapper around hibernate_begin, for when we get SIGINT. */
  685. void
  686. hibernate_begin_shutdown(void)
  687. {
  688. hibernate_begin(HIBERNATE_STATE_EXITING, time(NULL));
  689. }
  690. /** Return true iff we are currently hibernating. */
  691. int
  692. we_are_hibernating(void)
  693. {
  694. return hibernate_state != HIBERNATE_STATE_LIVE;
  695. }
  696. /** If we aren't currently dormant, close all connections and become
  697. * dormant. */
  698. static void
  699. hibernate_go_dormant(time_t now)
  700. {
  701. connection_t *conn;
  702. if (hibernate_state == HIBERNATE_STATE_DORMANT)
  703. return;
  704. else if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH)
  705. hibernate_state = HIBERNATE_STATE_DORMANT;
  706. else
  707. hibernate_begin(HIBERNATE_STATE_DORMANT, now);
  708. log_notice(LD_ACCT,"Going dormant. Blowing away remaining connections.");
  709. /* Close all OR/AP/exit conns. Leave dir conns because we still want
  710. * to be able to upload server descriptors so people know we're still
  711. * running, and download directories so we can detect if we're obsolete.
  712. * Leave control conns because we still want to be controllable.
  713. */
  714. while ((conn = connection_get_by_type(CONN_TYPE_OR)) ||
  715. (conn = connection_get_by_type(CONN_TYPE_AP)) ||
  716. (conn = connection_get_by_type(CONN_TYPE_EXIT))) {
  717. if (CONN_IS_EDGE(conn))
  718. connection_edge_end(TO_EDGE_CONN(conn), END_STREAM_REASON_HIBERNATING,
  719. TO_EDGE_CONN(conn)->cpath_layer);
  720. log_info(LD_NET,"Closing conn type %d", conn->type);
  721. if (conn->type == CONN_TYPE_AP) /* send socks failure if needed */
  722. connection_mark_unattached_ap(TO_EDGE_CONN(conn),
  723. END_STREAM_REASON_HIBERNATING);
  724. else
  725. connection_mark_for_close(conn);
  726. }
  727. accounting_record_bandwidth_usage(now);
  728. }
  729. /** Called when hibernate_end_time has arrived. */
  730. static void
  731. hibernate_end_time_elapsed(time_t now)
  732. {
  733. char buf[ISO_TIME_LEN+1];
  734. /* The interval has ended, or it is wakeup time. Find out which. */
  735. accounting_run_housekeeping(now);
  736. if (interval_wakeup_time <= now) {
  737. /* The interval hasn't changed, but interval_wakeup_time has passed.
  738. * It's time to wake up and start being a server. */
  739. hibernate_end(HIBERNATE_STATE_LIVE);
  740. return;
  741. } else {
  742. /* The interval has changed, and it isn't time to wake up yet. */
  743. hibernate_end_time = interval_wakeup_time;
  744. format_iso_time(buf,interval_wakeup_time);
  745. if (hibernate_state != HIBERNATE_STATE_DORMANT) {
  746. /* We weren't sleeping before; we should sleep now. */
  747. log_notice(LD_ACCT,
  748. "Accounting period ended. Commencing hibernation until "
  749. "%s GMT", buf);
  750. hibernate_go_dormant(now);
  751. } else {
  752. log_notice(LD_ACCT,
  753. "Accounting period ended. This period, we will hibernate"
  754. " until %s GMT",buf);
  755. }
  756. }
  757. }
  758. /** Consider our environment and decide if it's time
  759. * to start/stop hibernating.
  760. */
  761. void
  762. consider_hibernation(time_t now)
  763. {
  764. int accounting_enabled = get_options()->AccountingMax != 0;
  765. char buf[ISO_TIME_LEN+1];
  766. /* If we're in 'exiting' mode, then we just shut down after the interval
  767. * elapses. */
  768. if (hibernate_state == HIBERNATE_STATE_EXITING) {
  769. tor_assert(hibernate_end_time);
  770. if (hibernate_end_time <= now) {
  771. log_notice(LD_GENERAL, "Clean shutdown finished. Exiting.");
  772. tor_cleanup();
  773. exit(0);
  774. }
  775. return; /* if exiting soon, don't worry about bandwidth limits */
  776. }
  777. if (hibernate_state == HIBERNATE_STATE_DORMANT) {
  778. /* We've been hibernating because of bandwidth accounting. */
  779. tor_assert(hibernate_end_time);
  780. if (hibernate_end_time > now && accounting_enabled) {
  781. /* If we're hibernating, don't wake up until it's time, regardless of
  782. * whether we're in a new interval. */
  783. return ;
  784. } else {
  785. hibernate_end_time_elapsed(now);
  786. }
  787. }
  788. /* Else, we aren't hibernating. See if it's time to start hibernating, or to
  789. * go dormant. */
  790. if (hibernate_state == HIBERNATE_STATE_LIVE) {
  791. if (hibernate_soft_limit_reached()) {
  792. log_notice(LD_ACCT,
  793. "Bandwidth soft limit reached; commencing hibernation.");
  794. hibernate_begin(HIBERNATE_STATE_LOWBANDWIDTH, now);
  795. } else if (accounting_enabled && now < interval_wakeup_time) {
  796. format_local_iso_time(buf,interval_wakeup_time);
  797. log_notice(LD_ACCT,
  798. "Commencing hibernation. We will wake up at %s local time.",
  799. buf);
  800. hibernate_go_dormant(now);
  801. }
  802. }
  803. if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH) {
  804. if (!accounting_enabled) {
  805. hibernate_end_time_elapsed(now);
  806. } else if (hibernate_hard_limit_reached()) {
  807. hibernate_go_dormant(now);
  808. } else if (hibernate_end_time <= now) {
  809. /* The hibernation period ended while we were still in lowbandwidth.*/
  810. hibernate_end_time_elapsed(now);
  811. }
  812. }
  813. }
  814. /** DOCDOC */
  815. int
  816. accounting_getinfo_helper(const char *question, char **answer)
  817. {
  818. if (!strcmp(question, "accounting/enabled")) {
  819. *answer = tor_strdup(get_options()->AccountingMax ? "1" : "0");
  820. } else if (!strcmp(question, "accounting/hibernating")) {
  821. if (hibernate_state == HIBERNATE_STATE_DORMANT)
  822. *answer = tor_strdup("hard");
  823. else if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH)
  824. *answer = tor_strdup("soft");
  825. else
  826. *answer = tor_strdup("awake");
  827. } else if (!strcmp(question, "accounting/bytes")) {
  828. *answer = tor_malloc(32);
  829. tor_snprintf(*answer, 32, U64_FORMAT" "U64_FORMAT,
  830. U64_PRINTF_ARG(n_bytes_read_in_interval),
  831. U64_PRINTF_ARG(n_bytes_written_in_interval));
  832. } else if (!strcmp(question, "accounting/bytes-left")) {
  833. uint64_t limit = get_options()->AccountingMax;
  834. *answer = tor_malloc(32);
  835. tor_snprintf(*answer, 32, U64_FORMAT" "U64_FORMAT,
  836. U64_PRINTF_ARG(limit - n_bytes_read_in_interval),
  837. U64_PRINTF_ARG(limit - n_bytes_written_in_interval));
  838. } else if (!strcmp(question, "accounting/interval-start")) {
  839. *answer = tor_malloc(ISO_TIME_LEN+1);
  840. format_iso_time(*answer, interval_start_time);
  841. } else if (!strcmp(question, "accounting/interval-wake")) {
  842. *answer = tor_malloc(ISO_TIME_LEN+1);
  843. format_iso_time(*answer, interval_wakeup_time);
  844. } else if (!strcmp(question, "accounting/interval-end")) {
  845. *answer = tor_malloc(ISO_TIME_LEN+1);
  846. format_iso_time(*answer, interval_end_time);
  847. } else {
  848. *answer = NULL;
  849. }
  850. return 0;
  851. }