r13702@catbus: nickm | 2007-07-12 11:35:51 -0400
[tor.git] / src / or / hibernate.c
bloba3ea9cce0b5823777cc7b3788b481cc26fc6ecff
1 /* Copyright 2004-2007 Roger Dingledine, Nick Mathewson. */
2 /* See LICENSE for licensing information */
3 /* $Id$ */
4 const char hibernate_c_id[] =
5 "$Id$";
7 /**
8 * \file hibernate.c
9 * \brief Functions to close listeners, stop allowing new circuits,
10 * etc in preparation for closing down or going dormant; and to track
11 * bandwidth and time intervals to know when to hibernate and when to
12 * stop hibernating.
13 **/
16 hibernating, phase 1:
17 - send destroy in response to create cells
18 - send end (policy failed) in response to begin cells
19 - close an OR conn when it has no circuits
21 hibernating, phase 2:
22 (entered when bandwidth hard limit reached)
23 - close all OR/AP/exit conns)
26 #include "or.h"
28 /** Possible values of hibernate_state */
29 typedef enum {
30 /** We are running normally. */
31 HIBERNATE_STATE_LIVE=1,
32 /** We're trying to shut down cleanly, and we'll kill all active connections
33 * at shutdown_time. */
34 HIBERNATE_STATE_EXITING=2,
35 /** We're running low on allocated bandwidth for this period, so we won't
36 * accept any new connections. */
37 HIBERNATE_STATE_LOWBANDWIDTH=3,
38 /** We are hibernating, and we won't wake up till there's more bandwidth to
39 * use. */
40 HIBERNATE_STATE_DORMANT=4
41 } hibernate_state_t;
43 extern long stats_n_seconds_working; /* published uptime */
45 /** Are we currently awake, asleep, running out of bandwidth, or shutting
46 * down? */
47 static hibernate_state_t hibernate_state = HIBERNATE_STATE_LIVE;
48 /** If are hibernating, when do we plan to wake up? Set to 0 if we
49 * aren't hibernating. */
50 static time_t hibernate_end_time = 0;
51 /** If we are shutting down, when do we plan finally exit? Set to 0 if
52 * we aren't shutting down. */
53 static time_t shutdown_time = 0;
55 /** Possible accounting periods. */
56 typedef enum {
57 UNIT_MONTH=1, UNIT_WEEK=2, UNIT_DAY=3,
58 } time_unit_t;
60 /* Fields for accounting logic. Accounting overview:
62 * Accounting is designed to ensure that no more than N bytes are sent in
63 * either direction over a given interval (currently, one month, one week, or
64 * one day) We could
65 * try to do this by choking our bandwidth to a trickle, but that
66 * would make our streams useless. Instead, we estimate what our
67 * bandwidth usage will be, and guess how long we'll be able to
68 * provide that much bandwidth before hitting our limit. We then
69 * choose a random time within the accounting interval to come up (so
70 * that we don't get 50 Tors running on the 1st of the month and none
71 * on the 30th).
73 * Each interval runs as follows:
75 * 1. We guess our bandwidth usage, based on how much we used
76 * last time. We choose a "wakeup time" within the interval to come up.
77 * 2. Until the chosen wakeup time, we hibernate.
78 * 3. We come up at the wakeup time, and provide bandwidth until we are
79 * "very close" to running out.
80 * 4. Then we go into low-bandwidth mode, and stop accepting new
81 * connections, but provide bandwidth until we run out.
82 * 5. Then we hibernate until the end of the interval.
84 * If the interval ends before we run out of bandwidth, we go back to
85 * step one.
88 /** How many bytes have we read in this accounting interval? */
89 static uint64_t n_bytes_read_in_interval = 0;
90 /** How many bytes have we written in this accounting interval? */
91 static uint64_t n_bytes_written_in_interval = 0;
92 /** How many seconds have we been running this interval? */
93 static uint32_t n_seconds_active_in_interval = 0;
94 /** When did this accounting interval start? */
95 static time_t interval_start_time = 0;
96 /** When will this accounting interval end? */
97 static time_t interval_end_time = 0;
98 /** How far into the accounting interval should we hibernate? */
99 static time_t interval_wakeup_time = 0;
100 /** How much bandwidth do we 'expect' to use per minute? (0 if we have no
101 * info from the last period.) */
102 static uint64_t expected_bandwidth_usage = 0;
103 /** What unit are we using for our accounting? */
104 static time_unit_t cfg_unit = UNIT_MONTH;
105 /** How many days,hours,minutes into each unit does our accounting interval
106 * start? */
107 static int cfg_start_day = 0;
108 static int cfg_start_hour = 0;
109 static int cfg_start_min = 0;
111 static void reset_accounting(time_t now);
112 static int read_bandwidth_usage(void);
113 static time_t start_of_accounting_period_after(time_t now);
114 static time_t start_of_accounting_period_containing(time_t now);
115 static void accounting_set_wakeup_time(void);
117 /* ************
118 * Functions for bandwidth accounting.
119 * ************/
121 /** Configure accounting start/end time settings based on
122 * options->AccountingStart. Return 0 on success, -1 on failure. If
123 * <b>validate_only</b> is true, do not change the current settings. */
125 accounting_parse_options(or_options_t *options, int validate_only)
127 time_unit_t unit;
128 int ok, idx;
129 long d,h,m;
130 smartlist_t *items;
131 const char *v = options->AccountingStart;
132 const char *s;
133 char *cp;
135 if (!v) {
136 if (!validate_only) {
137 cfg_unit = UNIT_MONTH;
138 cfg_start_day = 1;
139 cfg_start_hour = 0;
140 cfg_start_min = 0;
142 return 0;
145 items = smartlist_create();
146 smartlist_split_string(items, v, NULL,
147 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK,0);
148 if (smartlist_len(items)<2) {
149 log_warn(LD_CONFIG, "Too few arguments to AccountingStart");
150 goto err;
152 s = smartlist_get(items,0);
153 if (0==strcasecmp(s, "month")) {
154 unit = UNIT_MONTH;
155 } else if (0==strcasecmp(s, "week")) {
156 unit = UNIT_WEEK;
157 } else if (0==strcasecmp(s, "day")) {
158 unit = UNIT_DAY;
159 } else {
160 log_warn(LD_CONFIG,
161 "Unrecognized accounting unit '%s': only 'month', 'week',"
162 " and 'day' are supported.", s);
163 goto err;
166 switch (unit) {
167 case UNIT_WEEK:
168 d = tor_parse_long(smartlist_get(items,1), 10, 1, 7, &ok, NULL);
169 if (!ok) {
170 log_warn(LD_CONFIG, "Weekly accounting must begin on a day between "
171 "1 (Monday) and 7 (Sunday)");
172 goto err;
174 break;
175 case UNIT_MONTH:
176 d = tor_parse_long(smartlist_get(items,1), 10, 1, 28, &ok, NULL);
177 if (!ok) {
178 log_warn(LD_CONFIG, "Monthly accounting must begin on a day between "
179 "1 and 28");
180 goto err;
182 break;
183 case UNIT_DAY:
184 d = 0;
185 break;
186 default:
187 tor_assert(0);
190 idx = unit==UNIT_DAY?1:2;
191 if (smartlist_len(items) != (idx+1)) {
192 log_warn(LD_CONFIG,"Accounting unit '%s' requires %d argument%s.",
193 s, idx, (idx>1)?"s":"");
194 goto err;
196 s = smartlist_get(items, idx);
197 h = tor_parse_long(s, 10, 0, 23, &ok, &cp);
198 if (!ok) {
199 log_warn(LD_CONFIG,"Accounting start time not parseable: bad hour.");
200 goto err;
202 if (!cp || *cp!=':') {
203 log_warn(LD_CONFIG,
204 "Accounting start time not parseable: not in HH:MM format");
205 goto err;
207 m = tor_parse_long(cp+1, 10, 0, 59, &ok, &cp);
208 if (!ok) {
209 log_warn(LD_CONFIG, "Accounting start time not parseable: bad minute");
210 goto err;
212 if (!cp || *cp!='\0') {
213 log_warn(LD_CONFIG,
214 "Accounting start time not parseable: not in HH:MM format");
215 goto err;
218 if (!validate_only) {
219 cfg_unit = unit;
220 cfg_start_day = (int)d;
221 cfg_start_hour = (int)h;
222 cfg_start_min = (int)m;
224 SMARTLIST_FOREACH(items, char *, s, tor_free(s));
225 smartlist_free(items);
226 return 0;
227 err:
228 SMARTLIST_FOREACH(items, char *, s, tor_free(s));
229 smartlist_free(items);
230 return -1;
233 /** If we want to manage the accounting system and potentially
234 * hibernate, return 1, else return 0.
237 accounting_is_enabled(or_options_t *options)
239 if (options->AccountingMax)
240 return 1;
241 return 0;
244 /** Called from main.c to tell us that <b>seconds</b> seconds have
245 * passed, <b>n_read</b> bytes have been read, and <b>n_written</b>
246 * bytes have been written. */
247 void
248 accounting_add_bytes(size_t n_read, size_t n_written, int seconds)
250 n_bytes_read_in_interval += n_read;
251 n_bytes_written_in_interval += n_written;
252 /* If we haven't been called in 10 seconds, we're probably jumping
253 * around in time. */
254 n_seconds_active_in_interval += (seconds < 10) ? seconds : 0;
257 /** If get_end, return the end of the accounting period that contains
258 * the time <b>now</b>. Else, return the start of the accounting
259 * period that contains the time <b>now</b> */
260 static time_t
261 edge_of_accounting_period_containing(time_t now, int get_end)
263 int before;
264 struct tm tm;
265 tor_localtime_r(&now, &tm);
267 /* Set 'before' to true iff the current time is before the hh:mm
268 * changeover time for today. */
269 before = tm.tm_hour < cfg_start_hour ||
270 (tm.tm_hour == cfg_start_hour && tm.tm_min < cfg_start_min);
272 /* Dispatch by unit. First, find the start day of the given period;
273 * then, if get_end is true, increment to the end day. */
274 switch (cfg_unit)
276 case UNIT_MONTH: {
277 /* If this is before the Nth, we want the Nth of last month. */
278 if (tm.tm_mday < cfg_start_day ||
279 (tm.tm_mday < cfg_start_day && before)) {
280 --tm.tm_mon;
282 /* Otherwise, the month is correct. */
283 tm.tm_mday = cfg_start_day;
284 if (get_end)
285 ++tm.tm_mon;
286 break;
288 case UNIT_WEEK: {
289 /* What is the 'target' day of the week in struct tm format? (We
290 say Sunday==7; struct tm says Sunday==0.) */
291 int wday = cfg_start_day % 7;
292 /* How many days do we subtract from today to get to the right day? */
293 int delta = (7+tm.tm_wday-wday)%7;
294 /* If we are on the right day, but the changeover hasn't happened yet,
295 * then subtract a whole week. */
296 if (delta == 0 && before)
297 delta = 7;
298 tm.tm_mday -= delta;
299 if (get_end)
300 tm.tm_mday += 7;
301 break;
303 case UNIT_DAY:
304 if (before)
305 --tm.tm_mday;
306 if (get_end)
307 ++tm.tm_mday;
308 break;
309 default:
310 tor_assert(0);
313 tm.tm_hour = cfg_start_hour;
314 tm.tm_min = cfg_start_min;
315 tm.tm_sec = 0;
316 tm.tm_isdst = -1; /* Autodetect DST */
317 return mktime(&tm);
320 /** Return the start of the accounting period containing the time
321 * <b>now</b>. */
322 static time_t
323 start_of_accounting_period_containing(time_t now)
325 return edge_of_accounting_period_containing(now, 0);
328 /** Return the start of the accounting period that comes after the one
329 * containing the time <b>now</b>. */
330 static time_t
331 start_of_accounting_period_after(time_t now)
333 return edge_of_accounting_period_containing(now, 1);
336 /** Initialize the accounting subsystem. */
337 void
338 configure_accounting(time_t now)
340 /* Try to remember our recorded usage. */
341 if (!interval_start_time)
342 read_bandwidth_usage(); /* If we fail, we'll leave values at zero, and
343 * reset below.*/
344 if (!interval_start_time ||
345 start_of_accounting_period_after(interval_start_time) <= now) {
346 /* We didn't have recorded usage, or we don't have recorded usage
347 * for this interval. Start a new interval. */
348 log_info(LD_ACCT, "Starting new accounting interval.");
349 reset_accounting(now);
350 } else if (interval_start_time ==
351 start_of_accounting_period_containing(interval_start_time)) {
352 log_info(LD_ACCT, "Continuing accounting interval.");
353 /* We are in the interval we thought we were in. Do nothing.*/
354 interval_end_time = start_of_accounting_period_after(interval_start_time);
355 } else {
356 log_warn(LD_ACCT,
357 "Mismatched accounting interval; starting a fresh one.");
358 reset_accounting(now);
360 accounting_set_wakeup_time();
363 /** Set expected_bandwidth_usage based on how much we sent/received
364 * per minute last interval (if we were up for at least 30 minutes),
365 * or based on our declared bandwidth otherwise. */
366 static void
367 update_expected_bandwidth(void)
369 uint64_t used, expected;
370 uint64_t max_configured = (get_options()->BandwidthRate * 60);
372 if (n_seconds_active_in_interval < 1800) {
373 /* If we haven't gotten enough data last interval, set 'expected'
374 * to 0. This will set our wakeup to the start of the interval.
375 * Next interval, we'll choose our starting time based on how much
376 * we sent this interval.
378 expected = 0;
379 } else {
380 used = n_bytes_written_in_interval < n_bytes_read_in_interval ?
381 n_bytes_read_in_interval : n_bytes_written_in_interval;
382 expected = used / (n_seconds_active_in_interval / 60);
383 if (expected > max_configured)
384 expected = max_configured;
386 expected_bandwidth_usage = expected;
389 /** Called at the start of a new accounting interval: reset our
390 * expected bandwidth usage based on what happened last time, set up
391 * the start and end of the interval, and clear byte/time totals.
393 static void
394 reset_accounting(time_t now)
396 log_info(LD_ACCT, "Starting new accounting interval.");
397 update_expected_bandwidth();
398 interval_start_time = start_of_accounting_period_containing(now);
399 interval_end_time = start_of_accounting_period_after(interval_start_time);
400 n_bytes_read_in_interval = 0;
401 n_bytes_written_in_interval = 0;
402 n_seconds_active_in_interval = 0;
405 /** Return true iff we should save our bandwidth usage to disk. */
406 static INLINE int
407 time_to_record_bandwidth_usage(time_t now)
409 /* Note every 600 sec */
410 #define NOTE_INTERVAL (600)
411 /* Or every 20 megabytes */
412 #define NOTE_BYTES 20*(1024*1024)
413 static uint64_t last_read_bytes_noted = 0;
414 static uint64_t last_written_bytes_noted = 0;
415 static time_t last_time_noted = 0;
417 if (last_time_noted + NOTE_INTERVAL <= now ||
418 last_read_bytes_noted + NOTE_BYTES <= n_bytes_read_in_interval ||
419 last_written_bytes_noted + NOTE_BYTES <= n_bytes_written_in_interval ||
420 (interval_end_time && interval_end_time <= now)) {
421 last_time_noted = now;
422 last_read_bytes_noted = n_bytes_read_in_interval;
423 last_written_bytes_noted = n_bytes_written_in_interval;
424 return 1;
426 return 0;
429 /** Invoked once per second. Checks whether it is time to hibernate,
430 * record bandwidth used, etc. */
431 void
432 accounting_run_housekeeping(time_t now)
434 if (now >= interval_end_time) {
435 configure_accounting(now);
437 if (time_to_record_bandwidth_usage(now)) {
438 if (accounting_record_bandwidth_usage(now, get_or_state())) {
439 log_warn(LD_FS, "Couldn't record bandwidth usage to disk.");
444 /** When we have no idea how fast we are, how long do we assume it will take
445 * us to exhaust our bandwidth? */
446 #define GUESS_TIME_TO_USE_BANDWIDTH (24*60*60)
448 /** Based on our interval and our estimated bandwidth, choose a
449 * deterministic (but random-ish) time to wake up. */
450 static void
451 accounting_set_wakeup_time(void)
453 char buf[ISO_TIME_LEN+1];
454 char digest[DIGEST_LEN];
455 crypto_digest_env_t *d_env;
456 int time_in_interval;
457 uint64_t time_to_exhaust_bw;
458 int time_to_consider;
460 if (! identity_key_is_set()) {
461 if (init_keys() < 0) {
462 log_err(LD_BUG, "Error initializing keys");
463 tor_assert(0);
467 format_iso_time(buf, interval_start_time);
468 crypto_pk_get_digest(get_identity_key(), digest);
470 d_env = crypto_new_digest_env();
471 crypto_digest_add_bytes(d_env, buf, ISO_TIME_LEN);
472 crypto_digest_add_bytes(d_env, digest, DIGEST_LEN);
473 crypto_digest_get_digest(d_env, digest, DIGEST_LEN);
474 crypto_free_digest_env(d_env);
476 if (!expected_bandwidth_usage) {
477 char buf1[ISO_TIME_LEN+1];
478 char buf2[ISO_TIME_LEN+1];
479 format_local_iso_time(buf1, interval_start_time);
480 format_local_iso_time(buf2, interval_end_time);
481 time_to_exhaust_bw = GUESS_TIME_TO_USE_BANDWIDTH;
482 interval_wakeup_time = interval_start_time;
484 log_notice(LD_ACCT,
485 "Configured hibernation. This interval begins at %s "
486 "and ends at %s. We have no prior estimate for bandwidth, so "
487 "we will start out awake and hibernate when we exhaust our quota.",
488 buf1, buf2);
489 return;
492 time_in_interval = interval_end_time - interval_start_time;
494 time_to_exhaust_bw =
495 (get_options()->AccountingMax/expected_bandwidth_usage)*60;
496 if (time_to_exhaust_bw > TIME_MAX) {
497 time_to_exhaust_bw = TIME_MAX;
498 time_to_consider = 0;
499 } else {
500 time_to_consider = time_in_interval - (int)time_to_exhaust_bw;
503 if (time_to_consider<=0) {
504 interval_wakeup_time = interval_start_time;
505 } else {
506 /* XXX can we simplify this just by picking a random (non-deterministic)
507 * time to be up? If we go down and come up, then we pick a new one. Is
508 * that good enough? -RD */
510 /* This is not a perfectly unbiased conversion, but it is good enough:
511 * in the worst case, the first half of the day is 0.06 percent likelier
512 * to be chosen than the last half. */
513 interval_wakeup_time = interval_start_time +
514 (get_uint32(digest) % time_to_consider);
516 format_iso_time(buf, interval_wakeup_time);
520 char buf1[ISO_TIME_LEN+1];
521 char buf2[ISO_TIME_LEN+1];
522 char buf3[ISO_TIME_LEN+1];
523 char buf4[ISO_TIME_LEN+1];
524 time_t down_time;
525 if (interval_wakeup_time+time_to_exhaust_bw > TIME_MAX)
526 down_time = TIME_MAX;
527 else
528 down_time = (time_t)(interval_wakeup_time+time_to_exhaust_bw);
529 if (down_time>interval_end_time)
530 down_time = interval_end_time;
531 format_local_iso_time(buf1, interval_start_time);
532 format_local_iso_time(buf2, interval_wakeup_time);
533 format_local_iso_time(buf3, down_time);
534 format_local_iso_time(buf4, interval_end_time);
536 log_notice(LD_ACCT,
537 "Configured hibernation. This interval began at %s; "
538 "the scheduled wake-up time %s %s; "
539 "we expect%s to exhaust our quota for this interval around %s; "
540 "the next interval begins at %s (all times local)",
541 buf1,
542 time(NULL)<interval_wakeup_time?"is":"was", buf2,
543 time(NULL)<down_time?"":"ed", buf3,
544 buf4);
548 /* This rounds 0 up to 1000, but that's actually a feature. */
549 #define ROUND_UP(x) (((x) + 0x3ff) & ~0x3ff)
550 #define BW_ACCOUNTING_VERSION 1
551 /** Save all our bandwidth tracking information to disk. Return 0 on
552 * success, -1 on failure. */
554 accounting_record_bandwidth_usage(time_t now, or_state_t *state)
556 char buf[128];
557 char fname[512];
558 char time1[ISO_TIME_LEN+1];
559 char time2[ISO_TIME_LEN+1];
560 char *cp = buf;
561 time_t tmp;
562 int r = 0;
563 uint64_t expected;
564 static time_t last_recorded = 0;
566 /* First, update bw_accounting. Until 0.1.2.5-alpha, this was the only place
567 * we stored this information. The format is:
568 * Version\nTime\nTime\nRead\nWrite\nSeconds\nExpected-Rate\n */
570 format_iso_time(time1, interval_start_time);
571 format_iso_time(time2, now);
572 /* now check to see if they're valid times -- if they're not,
573 * and we write them, then tor will refuse to start next time. */
574 if (parse_iso_time(time1, &tmp) || parse_iso_time(time2, &tmp)) {
575 log_warn(LD_ACCT, "Created a time that we refused to parse.");
576 return -1;
578 expected = expected_bandwidth_usage;
579 /* Cap this value, since older versions won't parse a uint64_t here. */
580 if (expected > UINT32_MAX)
581 expected = UINT32_MAX;
582 tor_snprintf(cp, sizeof(buf),
583 "%d\n%s\n%s\n"U64_FORMAT"\n"U64_FORMAT"\n%lu\n%lu\n",
584 BW_ACCOUNTING_VERSION,
585 time1,
586 time2,
587 U64_PRINTF_ARG(ROUND_UP(n_bytes_read_in_interval)),
588 U64_PRINTF_ARG(ROUND_UP(n_bytes_written_in_interval)),
589 (unsigned long)n_seconds_active_in_interval,
590 (unsigned long)expected);
591 tor_snprintf(fname, sizeof(fname), "%s/bw_accounting",
592 get_options()->DataDirectory);
593 if (!get_options()->AvoidDiskWrites || (last_recorded + 3600 < now)) {
594 r = write_str_to_file(fname, buf, 0);
595 last_recorded = now;
598 /* Now update the state */
599 state->AccountingIntervalStart = interval_start_time;
600 state->AccountingBytesReadInInterval = ROUND_UP(n_bytes_read_in_interval);
601 state->AccountingBytesWrittenInInterval =
602 ROUND_UP(n_bytes_written_in_interval);
603 state->AccountingSecondsActive = n_seconds_active_in_interval;
604 state->AccountingExpectedUsage = expected_bandwidth_usage;
606 or_state_mark_dirty(state,
607 now+(get_options()->AvoidDiskWrites ? 7200 : 60));
609 return r;
611 #undef ROUND_UP
613 /** Read stored accounting information from disk. Return 0 on success;
614 * return -1 and change nothing on failure. */
615 static int
616 read_bandwidth_usage(void)
618 char *s = NULL;
619 char fname[512];
620 time_t t1, t2;
621 uint64_t n_read, n_written;
622 uint32_t expected_bw, n_seconds;
623 smartlist_t *elts = NULL;
624 int ok, use_state=0, r=-1;
625 or_state_t *state = get_or_state();
627 tor_snprintf(fname, sizeof(fname), "%s/bw_accounting",
628 get_options()->DataDirectory);
629 elts = smartlist_create();
630 if ((s = read_file_to_str(fname, 0, NULL)) == NULL) {
631 /* We have an old-format bw_accounting file. */
632 use_state = 1;
634 if (!use_state) {
635 smartlist_split_string(elts, s, "\n",
636 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK,0);
637 tor_free(s);
639 if (smartlist_len(elts)<1 ||
640 atoi(smartlist_get(elts,0)) != BW_ACCOUNTING_VERSION) {
641 log_warn(LD_ACCT, "Unrecognized bw_accounting file version: %s",
642 (const char*)smartlist_get(elts,0));
643 use_state = 1;
646 if (!use_state && smartlist_len(elts) < 7) {
647 log_warn(LD_ACCT, "Corrupted bw_accounting file: %d lines",
648 smartlist_len(elts));
649 use_state = 1;
651 if (!use_state && parse_iso_time(smartlist_get(elts,2), &t2)) {
652 log_warn(LD_ACCT, "Error parsing bandwidth usage last-written time");
653 use_state = 1;
655 if (use_state || t2 <= state->LastWritten) {
656 /* Okay; it looks like the state file is more up-to-date than the
657 * bw_accounting file, or the bw_accounting file is nonexistant,
658 * or the bw_accounting file is corrupt.
660 log_info(LD_ACCT, "Reading bandwdith accounting data from state file");
661 n_bytes_read_in_interval = state->AccountingBytesReadInInterval;
662 n_bytes_written_in_interval = state->AccountingBytesWrittenInInterval;
663 n_seconds_active_in_interval = state->AccountingSecondsActive;
664 interval_start_time = state->AccountingIntervalStart;
665 expected_bandwidth_usage = state->AccountingExpectedUsage;
666 r = 0;
667 goto done;
670 if (parse_iso_time(smartlist_get(elts,1), &t1)) {
671 log_warn(LD_ACCT, "Error parsing bandwidth usage start time.");
672 goto done;
674 n_read = tor_parse_uint64(smartlist_get(elts,3), 10, 0, UINT64_MAX,
675 &ok, NULL);
676 if (!ok) {
677 log_warn(LD_ACCT, "Error parsing number of bytes read");
678 goto done;
680 n_written = tor_parse_uint64(smartlist_get(elts,4), 10, 0, UINT64_MAX,
681 &ok, NULL);
682 if (!ok) {
683 log_warn(LD_ACCT, "Error parsing number of bytes written");
684 goto done;
686 n_seconds = (uint32_t)tor_parse_ulong(smartlist_get(elts,5), 10,0,ULONG_MAX,
687 &ok, NULL);
688 if (!ok) {
689 log_warn(LD_ACCT, "Error parsing number of seconds live");
690 goto done;
692 expected_bw =(uint32_t)tor_parse_ulong(smartlist_get(elts,6), 10,0,ULONG_MAX,
693 &ok, NULL);
694 if (!ok) {
695 log_warn(LD_ACCT, "Error parsing expected bandwidth");
696 goto done;
699 n_bytes_read_in_interval = n_read;
700 n_bytes_written_in_interval = n_written;
701 n_seconds_active_in_interval = n_seconds;
702 interval_start_time = t1;
703 expected_bandwidth_usage = expected_bw;
705 log_info(LD_ACCT,
706 "Successfully read bandwidth accounting file written at %s "
707 "for interval starting at %s. We have been active for %lu seconds in "
708 "this interval. At the start of the interval, we expected to use "
709 "about %lu KB per second. ("U64_FORMAT" bytes read so far, "
710 U64_FORMAT" bytes written so far)",
711 (char*)smartlist_get(elts,2),
712 (char*)smartlist_get(elts,1),
713 (unsigned long)n_seconds_active_in_interval,
714 (unsigned long)(expected_bandwidth_usage*1024/60),
715 U64_PRINTF_ARG(n_bytes_read_in_interval),
716 U64_PRINTF_ARG(n_bytes_written_in_interval));
718 r = 0;
719 done:
720 if (elts) {
721 SMARTLIST_FOREACH(elts, char *, cp, tor_free(cp));
722 smartlist_free(elts);
724 return r;
727 /** Return true iff we have sent/received all the bytes we are willing
728 * to send/receive this interval. */
729 static int
730 hibernate_hard_limit_reached(void)
732 uint64_t hard_limit = get_options()->AccountingMax;
733 if (!hard_limit)
734 return 0;
735 return n_bytes_read_in_interval >= hard_limit
736 || n_bytes_written_in_interval >= hard_limit;
739 /** Return true iff we have sent/received almost all the bytes we are willing
740 * to send/receive this interval. */
741 static int
742 hibernate_soft_limit_reached(void)
744 uint64_t soft_limit = DBL_TO_U64(U64_TO_DBL(get_options()->AccountingMax)
745 * .95);
746 if (!soft_limit)
747 return 0;
748 return n_bytes_read_in_interval >= soft_limit
749 || n_bytes_written_in_interval >= soft_limit;
752 /** Called when we get a SIGINT, or when bandwidth soft limit is
753 * reached. Puts us into "loose hibernation": we don't accept new
754 * connections, but we continue handling old ones. */
755 static void
756 hibernate_begin(int new_state, time_t now)
758 connection_t *conn;
759 or_options_t *options = get_options();
761 if (new_state == HIBERNATE_STATE_EXITING &&
762 hibernate_state != HIBERNATE_STATE_LIVE) {
763 log_notice(LD_GENERAL,"Sigint received %s; exiting now.",
764 hibernate_state == HIBERNATE_STATE_EXITING ?
765 "a second time" : "while hibernating");
766 tor_cleanup();
767 exit(0);
770 /* close listeners. leave control listener(s). */
771 while ((conn = connection_get_by_type(CONN_TYPE_OR_LISTENER)) ||
772 (conn = connection_get_by_type(CONN_TYPE_AP_LISTENER)) ||
773 (conn = connection_get_by_type(CONN_TYPE_AP_TRANS_LISTENER)) ||
774 (conn = connection_get_by_type(CONN_TYPE_AP_NATD_LISTENER)) ||
775 (conn = connection_get_by_type(CONN_TYPE_DIR_LISTENER))) {
776 log_info(LD_NET,"Closing listener type %d", conn->type);
777 connection_mark_for_close(conn);
780 /* XXX kill intro point circs */
781 /* XXX upload rendezvous service descriptors with no intro points */
783 if (new_state == HIBERNATE_STATE_EXITING) {
784 log_notice(LD_GENERAL,"Interrupt: will shut down in %d seconds. Interrupt "
785 "again to exit now.", options->ShutdownWaitLength);
786 shutdown_time = time(NULL) + options->ShutdownWaitLength;
787 } else { /* soft limit reached */
788 hibernate_end_time = interval_end_time;
791 hibernate_state = new_state;
792 accounting_record_bandwidth_usage(now, get_or_state());
794 or_state_mark_dirty(get_or_state(),
795 get_options()->AvoidDiskWrites ? now+600 : 0);
798 /** Called when we've been hibernating and our timeout is reached. */
799 static void
800 hibernate_end(int new_state)
802 tor_assert(hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH ||
803 hibernate_state == HIBERNATE_STATE_DORMANT);
805 /* listeners will be relaunched in run_scheduled_events() in main.c */
806 log_notice(LD_ACCT,"Hibernation period ended. Resuming normal activity.");
808 hibernate_state = new_state;
809 hibernate_end_time = 0; /* no longer hibernating */
810 stats_n_seconds_working = 0; /* reset published uptime */
813 /** A wrapper around hibernate_begin, for when we get SIGINT. */
814 void
815 hibernate_begin_shutdown(void)
817 hibernate_begin(HIBERNATE_STATE_EXITING, time(NULL));
820 /** Return true iff we are currently hibernating. */
822 we_are_hibernating(void)
824 return hibernate_state != HIBERNATE_STATE_LIVE;
827 /** If we aren't currently dormant, close all connections and become
828 * dormant. */
829 static void
830 hibernate_go_dormant(time_t now)
832 connection_t *conn;
834 if (hibernate_state == HIBERNATE_STATE_DORMANT)
835 return;
836 else if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH)
837 hibernate_state = HIBERNATE_STATE_DORMANT;
838 else
839 hibernate_begin(HIBERNATE_STATE_DORMANT, now);
841 log_notice(LD_ACCT,"Going dormant. Blowing away remaining connections.");
843 /* Close all OR/AP/exit conns. Leave dir conns because we still want
844 * to be able to upload server descriptors so people know we're still
845 * running, and download directories so we can detect if we're obsolete.
846 * Leave control conns because we still want to be controllable.
848 while ((conn = connection_get_by_type(CONN_TYPE_OR)) ||
849 (conn = connection_get_by_type(CONN_TYPE_AP)) ||
850 (conn = connection_get_by_type(CONN_TYPE_EXIT))) {
851 if (CONN_IS_EDGE(conn))
852 connection_edge_end(TO_EDGE_CONN(conn), END_STREAM_REASON_HIBERNATING,
853 TO_EDGE_CONN(conn)->cpath_layer);
854 log_info(LD_NET,"Closing conn type %d", conn->type);
855 if (conn->type == CONN_TYPE_AP) /* send socks failure if needed */
856 connection_mark_unattached_ap(TO_EDGE_CONN(conn),
857 END_STREAM_REASON_HIBERNATING);
858 else
859 connection_mark_for_close(conn);
862 if (now < interval_wakeup_time)
863 hibernate_end_time = interval_wakeup_time;
864 else
865 hibernate_end_time = interval_end_time;
867 accounting_record_bandwidth_usage(now, get_or_state());
869 or_state_mark_dirty(get_or_state(),
870 get_options()->AvoidDiskWrites ? now+600 : 0);
873 /** Called when hibernate_end_time has arrived. */
874 static void
875 hibernate_end_time_elapsed(time_t now)
877 char buf[ISO_TIME_LEN+1];
879 /* The interval has ended, or it is wakeup time. Find out which. */
880 accounting_run_housekeeping(now);
881 if (interval_wakeup_time <= now) {
882 /* The interval hasn't changed, but interval_wakeup_time has passed.
883 * It's time to wake up and start being a server. */
884 hibernate_end(HIBERNATE_STATE_LIVE);
885 return;
886 } else {
887 /* The interval has changed, and it isn't time to wake up yet. */
888 hibernate_end_time = interval_wakeup_time;
889 format_iso_time(buf,interval_wakeup_time);
890 if (hibernate_state != HIBERNATE_STATE_DORMANT) {
891 /* We weren't sleeping before; we should sleep now. */
892 log_notice(LD_ACCT,
893 "Accounting period ended. Commencing hibernation until "
894 "%s GMT", buf);
895 hibernate_go_dormant(now);
896 } else {
897 log_notice(LD_ACCT,
898 "Accounting period ended. This period, we will hibernate"
899 " until %s GMT",buf);
904 /** Consider our environment and decide if it's time
905 * to start/stop hibernating.
907 void
908 consider_hibernation(time_t now)
910 int accounting_enabled = get_options()->AccountingMax != 0;
911 char buf[ISO_TIME_LEN+1];
913 /* If we're in 'exiting' mode, then we just shut down after the interval
914 * elapses. */
915 if (hibernate_state == HIBERNATE_STATE_EXITING) {
916 tor_assert(shutdown_time);
917 if (shutdown_time <= now) {
918 log_notice(LD_GENERAL, "Clean shutdown finished. Exiting.");
919 tor_cleanup();
920 exit(0);
922 return; /* if exiting soon, don't worry about bandwidth limits */
925 if (hibernate_state == HIBERNATE_STATE_DORMANT) {
926 /* We've been hibernating because of bandwidth accounting. */
927 tor_assert(hibernate_end_time);
928 if (hibernate_end_time > now && accounting_enabled) {
929 /* If we're hibernating, don't wake up until it's time, regardless of
930 * whether we're in a new interval. */
931 return ;
932 } else {
933 hibernate_end_time_elapsed(now);
937 /* Else, we aren't hibernating. See if it's time to start hibernating, or to
938 * go dormant. */
939 if (hibernate_state == HIBERNATE_STATE_LIVE) {
940 if (hibernate_soft_limit_reached()) {
941 log_notice(LD_ACCT,
942 "Bandwidth soft limit reached; commencing hibernation.");
943 hibernate_begin(HIBERNATE_STATE_LOWBANDWIDTH, now);
944 } else if (accounting_enabled && now < interval_wakeup_time) {
945 format_local_iso_time(buf,interval_wakeup_time);
946 log_notice(LD_ACCT,
947 "Commencing hibernation. We will wake up at %s local time.",
948 buf);
949 hibernate_go_dormant(now);
953 if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH) {
954 if (!accounting_enabled) {
955 hibernate_end_time_elapsed(now);
956 } else if (hibernate_hard_limit_reached()) {
957 hibernate_go_dormant(now);
958 } else if (hibernate_end_time <= now) {
959 /* The hibernation period ended while we were still in lowbandwidth.*/
960 hibernate_end_time_elapsed(now);
965 /** Helper function: called when we get a GETINFO request for an
966 * accounting-related key on the control connection <b>conn</b>. If we can
967 * answer the request for <b>question</b>, then set *<b>answer</b> to a newly
968 * allocated string holding the result. Otherwise, set *<b>answer</b> to
969 * NULL. */
971 getinfo_helper_accounting(control_connection_t *conn,
972 const char *question, char **answer)
974 (void) conn;
975 if (!strcmp(question, "accounting/enabled")) {
976 *answer = tor_strdup(accounting_is_enabled(get_options()) ? "1" : "0");
977 } else if (!strcmp(question, "accounting/hibernating")) {
978 if (hibernate_state == HIBERNATE_STATE_DORMANT)
979 *answer = tor_strdup("hard");
980 else if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH)
981 *answer = tor_strdup("soft");
982 else
983 *answer = tor_strdup("awake");
984 } else if (!strcmp(question, "accounting/bytes")) {
985 *answer = tor_malloc(32);
986 tor_snprintf(*answer, 32, U64_FORMAT" "U64_FORMAT,
987 U64_PRINTF_ARG(n_bytes_read_in_interval),
988 U64_PRINTF_ARG(n_bytes_written_in_interval));
989 } else if (!strcmp(question, "accounting/bytes-left")) {
990 uint64_t limit = get_options()->AccountingMax;
991 uint64_t read_left = 0, write_left = 0;
992 if (n_bytes_read_in_interval < limit)
993 read_left = limit - n_bytes_read_in_interval;
994 if (n_bytes_written_in_interval < limit)
995 write_left = limit - n_bytes_written_in_interval;
996 *answer = tor_malloc(64);
997 tor_snprintf(*answer, 64, U64_FORMAT" "U64_FORMAT,
998 U64_PRINTF_ARG(read_left), U64_PRINTF_ARG(write_left));
999 } else if (!strcmp(question, "accounting/interval-start")) {
1000 *answer = tor_malloc(ISO_TIME_LEN+1);
1001 format_iso_time(*answer, interval_start_time);
1002 } else if (!strcmp(question, "accounting/interval-wake")) {
1003 *answer = tor_malloc(ISO_TIME_LEN+1);
1004 format_iso_time(*answer, interval_wakeup_time);
1005 } else if (!strcmp(question, "accounting/interval-end")) {
1006 *answer = tor_malloc(ISO_TIME_LEN+1);
1007 format_iso_time(*answer, interval_end_time);
1008 } else {
1009 *answer = NULL;
1011 return 0;