Logs and debug info that I used for finding bug 16844
[tor.git] / src / or / hibernate.c
blob356e11f6ec55500ac090973a61ed5ac461acb4c3
1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2015, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
5 /**
6 * \file hibernate.c
7 * \brief Functions to close listeners, stop allowing new circuits,
8 * etc in preparation for closing down or going dormant; and to track
9 * bandwidth and time intervals to know when to hibernate and when to
10 * stop hibernating.
11 **/
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
19 hibernating, phase 2:
20 (entered when bandwidth hard limit reached)
21 - close all OR/AP/exit conns)
24 #define HIBERNATE_PRIVATE
25 #include "or.h"
26 #include "channel.h"
27 #include "channeltls.h"
28 #include "config.h"
29 #include "connection.h"
30 #include "connection_edge.h"
31 #include "hibernate.h"
32 #include "main.h"
33 #include "router.h"
34 #include "statefile.h"
36 extern long stats_n_seconds_working; /* published uptime */
38 /** Are we currently awake, asleep, running out of bandwidth, or shutting
39 * down? */
40 static hibernate_state_t hibernate_state = HIBERNATE_STATE_INITIAL;
41 /** If are hibernating, when do we plan to wake up? Set to 0 if we
42 * aren't hibernating. */
43 static time_t hibernate_end_time = 0;
44 /** If we are shutting down, when do we plan finally exit? Set to 0 if
45 * we aren't shutting down. */
46 static time_t shutdown_time = 0;
48 /** Possible accounting periods. */
49 typedef enum {
50 UNIT_MONTH=1, UNIT_WEEK=2, UNIT_DAY=3,
51 } time_unit_t;
53 /* Fields for accounting logic. Accounting overview:
55 * Accounting is designed to ensure that no more than N bytes are sent in
56 * either direction over a given interval (currently, one month, one week, or
57 * one day) We could
58 * try to do this by choking our bandwidth to a trickle, but that
59 * would make our streams useless. Instead, we estimate what our
60 * bandwidth usage will be, and guess how long we'll be able to
61 * provide that much bandwidth before hitting our limit. We then
62 * choose a random time within the accounting interval to come up (so
63 * that we don't get 50 Tors running on the 1st of the month and none
64 * on the 30th).
66 * Each interval runs as follows:
68 * 1. We guess our bandwidth usage, based on how much we used
69 * last time. We choose a "wakeup time" within the interval to come up.
70 * 2. Until the chosen wakeup time, we hibernate.
71 * 3. We come up at the wakeup time, and provide bandwidth until we are
72 * "very close" to running out.
73 * 4. Then we go into low-bandwidth mode, and stop accepting new
74 * connections, but provide bandwidth until we run out.
75 * 5. Then we hibernate until the end of the interval.
77 * If the interval ends before we run out of bandwidth, we go back to
78 * step one.
81 /** How many bytes have we read in this accounting interval? */
82 static uint64_t n_bytes_read_in_interval = 0;
83 /** How many bytes have we written in this accounting interval? */
84 static uint64_t n_bytes_written_in_interval = 0;
85 /** How many seconds have we been running this interval? */
86 static uint32_t n_seconds_active_in_interval = 0;
87 /** How many seconds were we active in this interval before we hit our soft
88 * limit? */
89 static int n_seconds_to_hit_soft_limit = 0;
90 /** When in this interval was the soft limit hit. */
91 static time_t soft_limit_hit_at = 0;
92 /** How many bytes had we read/written when we hit the soft limit? */
93 static uint64_t n_bytes_at_soft_limit = 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;
106 /** How many days,hours,minutes into each unit does our accounting interval
107 * start? */
108 /** @{ */
109 static int cfg_start_day = 0,
110 cfg_start_hour = 0,
111 cfg_start_min = 0;
112 /** @} */
114 static void reset_accounting(time_t now);
115 static int read_bandwidth_usage(void);
116 static time_t start_of_accounting_period_after(time_t now);
117 static time_t start_of_accounting_period_containing(time_t now);
118 static void accounting_set_wakeup_time(void);
120 /* ************
121 * Functions for bandwidth accounting.
122 * ************/
124 /** Configure accounting start/end time settings based on
125 * options->AccountingStart. Return 0 on success, -1 on failure. If
126 * <b>validate_only</b> is true, do not change the current settings. */
128 accounting_parse_options(const or_options_t *options, int validate_only)
130 time_unit_t unit;
131 int ok, idx;
132 long d,h,m;
133 smartlist_t *items;
134 const char *v = options->AccountingStart;
135 const char *s;
136 char *cp;
138 if (!v) {
139 if (!validate_only) {
140 cfg_unit = UNIT_MONTH;
141 cfg_start_day = 1;
142 cfg_start_hour = 0;
143 cfg_start_min = 0;
145 return 0;
148 items = smartlist_new();
149 smartlist_split_string(items, v, NULL,
150 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK,0);
151 if (smartlist_len(items)<2) {
152 log_warn(LD_CONFIG, "Too few arguments to AccountingStart");
153 goto err;
155 s = smartlist_get(items,0);
156 if (0==strcasecmp(s, "month")) {
157 unit = UNIT_MONTH;
158 } else if (0==strcasecmp(s, "week")) {
159 unit = UNIT_WEEK;
160 } else if (0==strcasecmp(s, "day")) {
161 unit = UNIT_DAY;
162 } else {
163 log_warn(LD_CONFIG,
164 "Unrecognized accounting unit '%s': only 'month', 'week',"
165 " and 'day' are supported.", s);
166 goto err;
169 switch (unit) {
170 case UNIT_WEEK:
171 d = tor_parse_long(smartlist_get(items,1), 10, 1, 7, &ok, NULL);
172 if (!ok) {
173 log_warn(LD_CONFIG, "Weekly accounting must begin on a day between "
174 "1 (Monday) and 7 (Sunday)");
175 goto err;
177 break;
178 case UNIT_MONTH:
179 d = tor_parse_long(smartlist_get(items,1), 10, 1, 28, &ok, NULL);
180 if (!ok) {
181 log_warn(LD_CONFIG, "Monthly accounting must begin on a day between "
182 "1 and 28");
183 goto err;
185 break;
186 case UNIT_DAY:
187 d = 0;
188 break;
189 /* Coverity dislikes unreachable default cases; some compilers warn on
190 * switch statements missing a case. Tell Coverity not to worry. */
191 /* coverity[dead_error_begin] */
192 default:
193 tor_assert(0);
196 idx = unit==UNIT_DAY?1:2;
197 if (smartlist_len(items) != (idx+1)) {
198 log_warn(LD_CONFIG,"Accounting unit '%s' requires %d argument%s.",
199 s, idx, (idx>1)?"s":"");
200 goto err;
202 s = smartlist_get(items, idx);
203 h = tor_parse_long(s, 10, 0, 23, &ok, &cp);
204 if (!ok) {
205 log_warn(LD_CONFIG,"Accounting start time not parseable: bad hour.");
206 goto err;
208 if (!cp || *cp!=':') {
209 log_warn(LD_CONFIG,
210 "Accounting start time not parseable: not in HH:MM format");
211 goto err;
213 m = tor_parse_long(cp+1, 10, 0, 59, &ok, &cp);
214 if (!ok) {
215 log_warn(LD_CONFIG, "Accounting start time not parseable: bad minute");
216 goto err;
218 if (!cp || *cp!='\0') {
219 log_warn(LD_CONFIG,
220 "Accounting start time not parseable: not in HH:MM format");
221 goto err;
224 if (!validate_only) {
225 cfg_unit = unit;
226 cfg_start_day = (int)d;
227 cfg_start_hour = (int)h;
228 cfg_start_min = (int)m;
230 SMARTLIST_FOREACH(items, char *, item, tor_free(item));
231 smartlist_free(items);
232 return 0;
233 err:
234 SMARTLIST_FOREACH(items, char *, item, tor_free(item));
235 smartlist_free(items);
236 return -1;
239 /** If we want to manage the accounting system and potentially
240 * hibernate, return 1, else return 0.
242 MOCK_IMPL(int,
243 accounting_is_enabled,(const or_options_t *options))
245 if (options->AccountingMax)
246 return 1;
247 return 0;
250 /** If accounting is enabled, return how long (in seconds) this
251 * interval lasts. */
253 accounting_get_interval_length(void)
255 return (int)(interval_end_time - interval_start_time);
258 /** Return the time at which the current accounting interval will end. */
259 MOCK_IMPL(time_t,
260 accounting_get_end_time,(void))
262 return interval_end_time;
265 /** Called from main.c to tell us that <b>seconds</b> seconds have
266 * passed, <b>n_read</b> bytes have been read, and <b>n_written</b>
267 * bytes have been written. */
268 void
269 accounting_add_bytes(size_t n_read, size_t n_written, int seconds)
271 n_bytes_read_in_interval += n_read;
272 n_bytes_written_in_interval += n_written;
273 /* If we haven't been called in 10 seconds, we're probably jumping
274 * around in time. */
275 n_seconds_active_in_interval += (seconds < 10) ? seconds : 0;
278 /** If get_end, return the end of the accounting period that contains
279 * the time <b>now</b>. Else, return the start of the accounting
280 * period that contains the time <b>now</b> */
281 static time_t
282 edge_of_accounting_period_containing(time_t now, int get_end)
284 int before;
285 struct tm tm;
286 tor_localtime_r(&now, &tm);
288 /* Set 'before' to true iff the current time is before the hh:mm
289 * changeover time for today. */
290 before = tm.tm_hour < cfg_start_hour ||
291 (tm.tm_hour == cfg_start_hour && tm.tm_min < cfg_start_min);
293 /* Dispatch by unit. First, find the start day of the given period;
294 * then, if get_end is true, increment to the end day. */
295 switch (cfg_unit)
297 case UNIT_MONTH: {
298 /* If this is before the Nth, we want the Nth of last month. */
299 if (tm.tm_mday < cfg_start_day ||
300 (tm.tm_mday < cfg_start_day && before)) {
301 --tm.tm_mon;
303 /* Otherwise, the month is correct. */
304 tm.tm_mday = cfg_start_day;
305 if (get_end)
306 ++tm.tm_mon;
307 break;
309 case UNIT_WEEK: {
310 /* What is the 'target' day of the week in struct tm format? (We
311 say Sunday==7; struct tm says Sunday==0.) */
312 int wday = cfg_start_day % 7;
313 /* How many days do we subtract from today to get to the right day? */
314 int delta = (7+tm.tm_wday-wday)%7;
315 /* If we are on the right day, but the changeover hasn't happened yet,
316 * then subtract a whole week. */
317 if (delta == 0 && before)
318 delta = 7;
319 tm.tm_mday -= delta;
320 if (get_end)
321 tm.tm_mday += 7;
322 break;
324 case UNIT_DAY:
325 if (before)
326 --tm.tm_mday;
327 if (get_end)
328 ++tm.tm_mday;
329 break;
330 default:
331 tor_assert(0);
334 tm.tm_hour = cfg_start_hour;
335 tm.tm_min = cfg_start_min;
336 tm.tm_sec = 0;
337 tm.tm_isdst = -1; /* Autodetect DST */
338 return mktime(&tm);
341 /** Return the start of the accounting period containing the time
342 * <b>now</b>. */
343 static time_t
344 start_of_accounting_period_containing(time_t now)
346 return edge_of_accounting_period_containing(now, 0);
349 /** Return the start of the accounting period that comes after the one
350 * containing the time <b>now</b>. */
351 static time_t
352 start_of_accounting_period_after(time_t now)
354 return edge_of_accounting_period_containing(now, 1);
357 /** Return the length of the accounting period containing the time
358 * <b>now</b>. */
359 static long
360 length_of_accounting_period_containing(time_t now)
362 return edge_of_accounting_period_containing(now, 1) -
363 edge_of_accounting_period_containing(now, 0);
366 /** Initialize the accounting subsystem. */
367 void
368 configure_accounting(time_t now)
370 time_t s_now;
371 /* Try to remember our recorded usage. */
372 if (!interval_start_time)
373 read_bandwidth_usage(); /* If we fail, we'll leave values at zero, and
374 * reset below.*/
376 s_now = start_of_accounting_period_containing(now);
378 if (!interval_start_time) {
379 /* We didn't have recorded usage; Start a new interval. */
380 log_info(LD_ACCT, "Starting new accounting interval.");
381 reset_accounting(now);
382 } else if (s_now == interval_start_time) {
383 log_info(LD_ACCT, "Continuing accounting interval.");
384 /* We are in the interval we thought we were in. Do nothing.*/
385 interval_end_time = start_of_accounting_period_after(interval_start_time);
386 } else {
387 long duration =
388 length_of_accounting_period_containing(interval_start_time);
389 double delta = ((double)(s_now - interval_start_time)) / duration;
390 if (-0.50 <= delta && delta <= 0.50) {
391 /* The start of the period is now a little later or earlier than we
392 * remembered. That's fine; we might lose some bytes we could otherwise
393 * have written, but better to err on the side of obeying people's
394 * accounting settings. */
395 log_info(LD_ACCT, "Accounting interval moved by %.02f%%; "
396 "that's fine.", delta*100);
397 interval_end_time = start_of_accounting_period_after(now);
398 } else if (delta >= 0.99) {
399 /* This is the regular time-moved-forward case; don't be too noisy
400 * about it or people will complain */
401 log_info(LD_ACCT, "Accounting interval elapsed; starting a new one");
402 reset_accounting(now);
403 } else {
404 log_warn(LD_ACCT,
405 "Mismatched accounting interval: moved by %.02f%%. "
406 "Starting a fresh one.", delta*100);
407 reset_accounting(now);
410 accounting_set_wakeup_time();
413 /** Return the relevant number of bytes sent/received this interval
414 * based on the set AccountingRule */
415 static uint64_t
416 get_accounting_bytes(void)
418 if (get_options()->AccountingRule == ACCT_SUM)
419 return n_bytes_read_in_interval+n_bytes_written_in_interval;
420 else
421 return MAX(n_bytes_read_in_interval, n_bytes_written_in_interval);
424 /** Set expected_bandwidth_usage based on how much we sent/received
425 * per minute last interval (if we were up for at least 30 minutes),
426 * or based on our declared bandwidth otherwise. */
427 static void
428 update_expected_bandwidth(void)
430 uint64_t expected;
431 const or_options_t *options= get_options();
432 uint64_t max_configured = (options->RelayBandwidthRate > 0 ?
433 options->RelayBandwidthRate :
434 options->BandwidthRate) * 60;
435 /* max_configured is the larger of bytes read and bytes written
436 * If we are accounting based on sum, worst case is both are
437 * at max, doubling the expected sum of bandwidth */
438 if (get_options()->AccountingRule == ACCT_SUM)
439 max_configured *= 2;
441 #define MIN_TIME_FOR_MEASUREMENT (1800)
443 if (soft_limit_hit_at > interval_start_time && n_bytes_at_soft_limit &&
444 (soft_limit_hit_at - interval_start_time) > MIN_TIME_FOR_MEASUREMENT) {
445 /* If we hit our soft limit last time, only count the bytes up to that
446 * time. This is a better predictor of our actual bandwidth than
447 * considering the entirety of the last interval, since we likely started
448 * using bytes very slowly once we hit our soft limit. */
449 expected = n_bytes_at_soft_limit /
450 (soft_limit_hit_at - interval_start_time);
451 expected /= 60;
452 } else if (n_seconds_active_in_interval >= MIN_TIME_FOR_MEASUREMENT) {
453 /* Otherwise, we either measured enough time in the last interval but
454 * never hit our soft limit, or we're using a state file from a Tor that
455 * doesn't know to store soft-limit info. Just take rate at which
456 * we were reading/writing in the last interval as our expected rate.
458 uint64_t used = get_accounting_bytes();
459 expected = used / (n_seconds_active_in_interval / 60);
460 } else {
461 /* If we haven't gotten enough data last interval, set 'expected'
462 * to 0. This will set our wakeup to the start of the interval.
463 * Next interval, we'll choose our starting time based on how much
464 * we sent this interval.
466 expected = 0;
468 if (expected > max_configured)
469 expected = max_configured;
470 expected_bandwidth_usage = expected;
473 /** Called at the start of a new accounting interval: reset our
474 * expected bandwidth usage based on what happened last time, set up
475 * the start and end of the interval, and clear byte/time totals.
477 static void
478 reset_accounting(time_t now)
480 log_info(LD_ACCT, "Starting new accounting interval.");
481 update_expected_bandwidth();
482 interval_start_time = start_of_accounting_period_containing(now);
483 interval_end_time = start_of_accounting_period_after(interval_start_time);
484 n_bytes_read_in_interval = 0;
485 n_bytes_written_in_interval = 0;
486 n_seconds_active_in_interval = 0;
487 n_bytes_at_soft_limit = 0;
488 soft_limit_hit_at = 0;
489 n_seconds_to_hit_soft_limit = 0;
492 /** Return true iff we should save our bandwidth usage to disk. */
493 static INLINE int
494 time_to_record_bandwidth_usage(time_t now)
496 /* Note every 600 sec */
497 #define NOTE_INTERVAL (600)
498 /* Or every 20 megabytes */
499 #define NOTE_BYTES 20*(1024*1024)
500 static uint64_t last_read_bytes_noted = 0;
501 static uint64_t last_written_bytes_noted = 0;
502 static time_t last_time_noted = 0;
504 if (last_time_noted + NOTE_INTERVAL <= now ||
505 last_read_bytes_noted + NOTE_BYTES <= n_bytes_read_in_interval ||
506 last_written_bytes_noted + NOTE_BYTES <= n_bytes_written_in_interval ||
507 (interval_end_time && interval_end_time <= now)) {
508 last_time_noted = now;
509 last_read_bytes_noted = n_bytes_read_in_interval;
510 last_written_bytes_noted = n_bytes_written_in_interval;
511 return 1;
513 return 0;
516 /** Invoked once per second. Checks whether it is time to hibernate,
517 * record bandwidth used, etc. */
518 void
519 accounting_run_housekeeping(time_t now)
521 if (now >= interval_end_time) {
522 configure_accounting(now);
524 if (time_to_record_bandwidth_usage(now)) {
525 if (accounting_record_bandwidth_usage(now, get_or_state())) {
526 log_warn(LD_FS, "Couldn't record bandwidth usage to disk.");
531 /** Based on our interval and our estimated bandwidth, choose a
532 * deterministic (but random-ish) time to wake up. */
533 static void
534 accounting_set_wakeup_time(void)
536 char digest[DIGEST_LEN];
537 crypto_digest_t *d_env;
538 uint64_t time_to_exhaust_bw;
539 int time_to_consider;
541 if (! server_identity_key_is_set()) {
542 if (init_keys() < 0) {
543 log_err(LD_BUG, "Error initializing keys");
544 tor_assert(0);
548 if (server_identity_key_is_set()) {
549 char buf[ISO_TIME_LEN+1];
550 format_iso_time(buf, interval_start_time);
552 crypto_pk_get_digest(get_server_identity_key(), digest);
554 d_env = crypto_digest_new();
555 crypto_digest_add_bytes(d_env, buf, ISO_TIME_LEN);
556 crypto_digest_add_bytes(d_env, digest, DIGEST_LEN);
557 crypto_digest_get_digest(d_env, digest, DIGEST_LEN);
558 crypto_digest_free(d_env);
559 } else {
560 crypto_rand(digest, DIGEST_LEN);
563 if (!expected_bandwidth_usage) {
564 char buf1[ISO_TIME_LEN+1];
565 char buf2[ISO_TIME_LEN+1];
566 format_local_iso_time(buf1, interval_start_time);
567 format_local_iso_time(buf2, interval_end_time);
568 interval_wakeup_time = interval_start_time;
570 log_notice(LD_ACCT,
571 "Configured hibernation. This interval begins at %s "
572 "and ends at %s. We have no prior estimate for bandwidth, so "
573 "we will start out awake and hibernate when we exhaust our quota.",
574 buf1, buf2);
575 return;
578 time_to_exhaust_bw =
579 (get_options()->AccountingMax/expected_bandwidth_usage)*60;
580 if (time_to_exhaust_bw > INT_MAX) {
581 time_to_exhaust_bw = INT_MAX;
582 time_to_consider = 0;
583 } else {
584 time_to_consider = accounting_get_interval_length() -
585 (int)time_to_exhaust_bw;
588 if (time_to_consider<=0) {
589 interval_wakeup_time = interval_start_time;
590 } else {
591 /* XXX can we simplify this just by picking a random (non-deterministic)
592 * time to be up? If we go down and come up, then we pick a new one. Is
593 * that good enough? -RD */
595 /* This is not a perfectly unbiased conversion, but it is good enough:
596 * in the worst case, the first half of the day is 0.06 percent likelier
597 * to be chosen than the last half. */
598 interval_wakeup_time = interval_start_time +
599 (get_uint32(digest) % time_to_consider);
603 char buf1[ISO_TIME_LEN+1];
604 char buf2[ISO_TIME_LEN+1];
605 char buf3[ISO_TIME_LEN+1];
606 char buf4[ISO_TIME_LEN+1];
607 time_t down_time;
608 if (interval_wakeup_time+time_to_exhaust_bw > TIME_MAX)
609 down_time = TIME_MAX;
610 else
611 down_time = (time_t)(interval_wakeup_time+time_to_exhaust_bw);
612 if (down_time>interval_end_time)
613 down_time = interval_end_time;
614 format_local_iso_time(buf1, interval_start_time);
615 format_local_iso_time(buf2, interval_wakeup_time);
616 format_local_iso_time(buf3, down_time);
617 format_local_iso_time(buf4, interval_end_time);
619 log_notice(LD_ACCT,
620 "Configured hibernation. This interval began at %s; "
621 "the scheduled wake-up time %s %s; "
622 "we expect%s to exhaust our quota for this interval around %s; "
623 "the next interval begins at %s (all times local)",
624 buf1,
625 time(NULL)<interval_wakeup_time?"is":"was", buf2,
626 time(NULL)<down_time?"":"ed", buf3,
627 buf4);
631 /* This rounds 0 up to 1000, but that's actually a feature. */
632 #define ROUND_UP(x) (((x) + 0x3ff) & ~0x3ff)
633 /** Save all our bandwidth tracking information to disk. Return 0 on
634 * success, -1 on failure. */
636 accounting_record_bandwidth_usage(time_t now, or_state_t *state)
638 /* Just update the state */
639 state->AccountingIntervalStart = interval_start_time;
640 state->AccountingBytesReadInInterval = ROUND_UP(n_bytes_read_in_interval);
641 state->AccountingBytesWrittenInInterval =
642 ROUND_UP(n_bytes_written_in_interval);
643 state->AccountingSecondsActive = n_seconds_active_in_interval;
644 state->AccountingExpectedUsage = expected_bandwidth_usage;
646 state->AccountingSecondsToReachSoftLimit = n_seconds_to_hit_soft_limit;
647 state->AccountingSoftLimitHitAt = soft_limit_hit_at;
648 state->AccountingBytesAtSoftLimit = n_bytes_at_soft_limit;
650 or_state_mark_dirty(state,
651 now+(get_options()->AvoidDiskWrites ? 7200 : 60));
653 return 0;
655 #undef ROUND_UP
657 /** Read stored accounting information from disk. Return 0 on success;
658 * return -1 and change nothing on failure. */
659 static int
660 read_bandwidth_usage(void)
662 or_state_t *state = get_or_state();
665 char *fname = get_datadir_fname("bw_accounting");
666 int res;
668 res = unlink(fname);
669 if (res != 0) {
670 log_warn(LD_FS,
671 "Failed to unlink %s: %s",
672 fname, strerror(errno));
675 tor_free(fname);
678 if (!state)
679 return -1;
681 log_info(LD_ACCT, "Reading bandwidth accounting data from state file");
682 n_bytes_read_in_interval = state->AccountingBytesReadInInterval;
683 n_bytes_written_in_interval = state->AccountingBytesWrittenInInterval;
684 n_seconds_active_in_interval = state->AccountingSecondsActive;
685 interval_start_time = state->AccountingIntervalStart;
686 expected_bandwidth_usage = state->AccountingExpectedUsage;
688 /* Older versions of Tor (before 0.2.2.17-alpha or so) didn't generate these
689 * fields. If you switch back and forth, you might get an
690 * AccountingSoftLimitHitAt value from long before the most recent
691 * interval_start_time. If that's so, then ignore the softlimit-related
692 * values. */
693 if (state->AccountingSoftLimitHitAt > interval_start_time) {
694 soft_limit_hit_at = state->AccountingSoftLimitHitAt;
695 n_bytes_at_soft_limit = state->AccountingBytesAtSoftLimit;
696 n_seconds_to_hit_soft_limit = state->AccountingSecondsToReachSoftLimit;
697 } else {
698 soft_limit_hit_at = 0;
699 n_bytes_at_soft_limit = 0;
700 n_seconds_to_hit_soft_limit = 0;
704 char tbuf1[ISO_TIME_LEN+1];
705 char tbuf2[ISO_TIME_LEN+1];
706 format_iso_time(tbuf1, state->LastWritten);
707 format_iso_time(tbuf2, state->AccountingIntervalStart);
709 log_info(LD_ACCT,
710 "Successfully read bandwidth accounting info from state written at %s "
711 "for interval starting at %s. We have been active for %lu seconds in "
712 "this interval. At the start of the interval, we expected to use "
713 "about %lu KB per second. ("U64_FORMAT" bytes read so far, "
714 U64_FORMAT" bytes written so far)",
715 tbuf1, tbuf2,
716 (unsigned long)n_seconds_active_in_interval,
717 (unsigned long)(expected_bandwidth_usage*1024/60),
718 U64_PRINTF_ARG(n_bytes_read_in_interval),
719 U64_PRINTF_ARG(n_bytes_written_in_interval));
722 return 0;
725 /** Return true iff we have sent/received all the bytes we are willing
726 * to send/receive this interval. */
727 static int
728 hibernate_hard_limit_reached(void)
730 uint64_t hard_limit = get_options()->AccountingMax;
731 if (!hard_limit)
732 return 0;
733 return get_accounting_bytes() >= hard_limit;
736 /** Return true iff we have sent/received almost all the bytes we are willing
737 * to send/receive this interval. */
738 static int
739 hibernate_soft_limit_reached(void)
741 const uint64_t acct_max = get_options()->AccountingMax;
742 #define SOFT_LIM_PCT (.95)
743 #define SOFT_LIM_BYTES (500*1024*1024)
744 #define SOFT_LIM_MINUTES (3*60)
745 /* The 'soft limit' is a fair bit more complicated now than once it was.
746 * We want to stop accepting connections when ALL of the following are true:
747 * - We expect to use up the remaining bytes in under 3 hours
748 * - We have used up 95% of our bytes.
749 * - We have less than 500MB of bytes left.
751 uint64_t soft_limit = DBL_TO_U64(U64_TO_DBL(acct_max) * SOFT_LIM_PCT);
752 if (acct_max > SOFT_LIM_BYTES && acct_max - SOFT_LIM_BYTES > soft_limit) {
753 soft_limit = acct_max - SOFT_LIM_BYTES;
755 if (expected_bandwidth_usage) {
756 const uint64_t expected_usage =
757 expected_bandwidth_usage * SOFT_LIM_MINUTES;
758 if (acct_max > expected_usage && acct_max - expected_usage > soft_limit)
759 soft_limit = acct_max - expected_usage;
762 if (!soft_limit)
763 return 0;
764 return get_accounting_bytes() >= soft_limit;
767 /** Called when we get a SIGINT, or when bandwidth soft limit is
768 * reached. Puts us into "loose hibernation": we don't accept new
769 * connections, but we continue handling old ones. */
770 static void
771 hibernate_begin(hibernate_state_t new_state, time_t now)
773 const or_options_t *options = get_options();
775 if (new_state == HIBERNATE_STATE_EXITING &&
776 hibernate_state != HIBERNATE_STATE_LIVE) {
777 log_notice(LD_GENERAL,"SIGINT received %s; exiting now.",
778 hibernate_state == HIBERNATE_STATE_EXITING ?
779 "a second time" : "while hibernating");
780 tor_cleanup();
781 exit(0);
784 if (new_state == HIBERNATE_STATE_LOWBANDWIDTH &&
785 hibernate_state == HIBERNATE_STATE_LIVE) {
786 soft_limit_hit_at = now;
787 n_seconds_to_hit_soft_limit = n_seconds_active_in_interval;
788 n_bytes_at_soft_limit = get_accounting_bytes();
791 /* close listeners. leave control listener(s). */
792 connection_mark_all_noncontrol_listeners();
794 /* XXX kill intro point circs */
795 /* XXX upload rendezvous service descriptors with no intro points */
797 if (new_state == HIBERNATE_STATE_EXITING) {
798 log_notice(LD_GENERAL,"Interrupt: we have stopped accepting new "
799 "connections, and will shut down in %d seconds. Interrupt "
800 "again to exit now.", options->ShutdownWaitLength);
801 shutdown_time = time(NULL) + options->ShutdownWaitLength;
802 } else { /* soft limit reached */
803 hibernate_end_time = interval_end_time;
806 hibernate_state = new_state;
807 accounting_record_bandwidth_usage(now, get_or_state());
809 or_state_mark_dirty(get_or_state(),
810 get_options()->AvoidDiskWrites ? now+600 : 0);
813 /** Called when we've been hibernating and our timeout is reached. */
814 static void
815 hibernate_end(hibernate_state_t new_state)
817 tor_assert(hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH ||
818 hibernate_state == HIBERNATE_STATE_DORMANT ||
819 hibernate_state == HIBERNATE_STATE_INITIAL);
821 /* listeners will be relaunched in run_scheduled_events() in main.c */
822 if (hibernate_state != HIBERNATE_STATE_INITIAL)
823 log_notice(LD_ACCT,"Hibernation period ended. Resuming normal activity.");
825 hibernate_state = new_state;
826 hibernate_end_time = 0; /* no longer hibernating */
827 stats_n_seconds_working = 0; /* reset published uptime */
830 /** A wrapper around hibernate_begin, for when we get SIGINT. */
831 void
832 hibernate_begin_shutdown(void)
834 hibernate_begin(HIBERNATE_STATE_EXITING, time(NULL));
837 /** Return true iff we are currently hibernating. */
838 MOCK_IMPL(int,
839 we_are_hibernating,(void))
841 return hibernate_state != HIBERNATE_STATE_LIVE;
844 /** If we aren't currently dormant, close all connections and become
845 * dormant. */
846 static void
847 hibernate_go_dormant(time_t now)
849 connection_t *conn;
851 if (hibernate_state == HIBERNATE_STATE_DORMANT)
852 return;
853 else if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH)
854 hibernate_state = HIBERNATE_STATE_DORMANT;
855 else
856 hibernate_begin(HIBERNATE_STATE_DORMANT, now);
858 log_notice(LD_ACCT,"Going dormant. Blowing away remaining connections.");
860 /* Close all OR/AP/exit conns. Leave dir conns because we still want
861 * to be able to upload server descriptors so people know we're still
862 * running, and download directories so we can detect if we're obsolete.
863 * Leave control conns because we still want to be controllable.
865 while ((conn = connection_get_by_type(CONN_TYPE_OR)) ||
866 (conn = connection_get_by_type(CONN_TYPE_AP)) ||
867 (conn = connection_get_by_type(CONN_TYPE_EXIT))) {
868 if (CONN_IS_EDGE(conn))
869 connection_edge_end(TO_EDGE_CONN(conn), END_STREAM_REASON_HIBERNATING);
870 log_info(LD_NET,"Closing conn type %d", conn->type);
871 if (conn->type == CONN_TYPE_AP) /* send socks failure if needed */
872 connection_mark_unattached_ap(TO_ENTRY_CONN(conn),
873 END_STREAM_REASON_HIBERNATING);
874 else if (conn->type == CONN_TYPE_OR) {
875 if (TO_OR_CONN(conn)->chan) {
876 channel_mark_for_close(TLS_CHAN_TO_BASE(TO_OR_CONN(conn)->chan));
877 } else {
878 connection_mark_for_close(conn);
880 } else
881 connection_mark_for_close(conn);
884 if (now < interval_wakeup_time)
885 hibernate_end_time = interval_wakeup_time;
886 else
887 hibernate_end_time = interval_end_time;
889 accounting_record_bandwidth_usage(now, get_or_state());
891 or_state_mark_dirty(get_or_state(),
892 get_options()->AvoidDiskWrites ? now+600 : 0);
895 /** Called when hibernate_end_time has arrived. */
896 static void
897 hibernate_end_time_elapsed(time_t now)
899 char buf[ISO_TIME_LEN+1];
901 /* The interval has ended, or it is wakeup time. Find out which. */
902 accounting_run_housekeeping(now);
903 if (interval_wakeup_time <= now) {
904 /* The interval hasn't changed, but interval_wakeup_time has passed.
905 * It's time to wake up and start being a server. */
906 hibernate_end(HIBERNATE_STATE_LIVE);
907 return;
908 } else {
909 /* The interval has changed, and it isn't time to wake up yet. */
910 hibernate_end_time = interval_wakeup_time;
911 format_iso_time(buf,interval_wakeup_time);
912 if (hibernate_state != HIBERNATE_STATE_DORMANT) {
913 /* We weren't sleeping before; we should sleep now. */
914 log_notice(LD_ACCT,
915 "Accounting period ended. Commencing hibernation until "
916 "%s UTC", buf);
917 hibernate_go_dormant(now);
918 } else {
919 log_notice(LD_ACCT,
920 "Accounting period ended. This period, we will hibernate"
921 " until %s UTC",buf);
926 /** Consider our environment and decide if it's time
927 * to start/stop hibernating.
929 void
930 consider_hibernation(time_t now)
932 int accounting_enabled = get_options()->AccountingMax != 0;
933 char buf[ISO_TIME_LEN+1];
935 /* If we're in 'exiting' mode, then we just shut down after the interval
936 * elapses. */
937 if (hibernate_state == HIBERNATE_STATE_EXITING) {
938 tor_assert(shutdown_time);
939 if (shutdown_time <= now) {
940 log_notice(LD_GENERAL, "Clean shutdown finished. Exiting.");
941 tor_cleanup();
942 exit(0);
944 return; /* if exiting soon, don't worry about bandwidth limits */
947 if (hibernate_state == HIBERNATE_STATE_DORMANT) {
948 /* We've been hibernating because of bandwidth accounting. */
949 tor_assert(hibernate_end_time);
950 if (hibernate_end_time > now && accounting_enabled) {
951 /* If we're hibernating, don't wake up until it's time, regardless of
952 * whether we're in a new interval. */
953 return ;
954 } else {
955 hibernate_end_time_elapsed(now);
959 /* Else, we aren't hibernating. See if it's time to start hibernating, or to
960 * go dormant. */
961 if (hibernate_state == HIBERNATE_STATE_LIVE ||
962 hibernate_state == HIBERNATE_STATE_INITIAL) {
963 if (hibernate_soft_limit_reached()) {
964 log_notice(LD_ACCT,
965 "Bandwidth soft limit reached; commencing hibernation. "
966 "No new connections will be accepted");
967 hibernate_begin(HIBERNATE_STATE_LOWBANDWIDTH, now);
968 } else if (accounting_enabled && now < interval_wakeup_time) {
969 format_local_iso_time(buf,interval_wakeup_time);
970 log_notice(LD_ACCT,
971 "Commencing hibernation. We will wake up at %s local time.",
972 buf);
973 hibernate_go_dormant(now);
974 } else if (hibernate_state == HIBERNATE_STATE_INITIAL) {
975 hibernate_end(HIBERNATE_STATE_LIVE);
979 if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH) {
980 if (!accounting_enabled) {
981 hibernate_end_time_elapsed(now);
982 } else if (hibernate_hard_limit_reached()) {
983 hibernate_go_dormant(now);
984 } else if (hibernate_end_time <= now) {
985 /* The hibernation period ended while we were still in lowbandwidth.*/
986 hibernate_end_time_elapsed(now);
991 /** Helper function: called when we get a GETINFO request for an
992 * accounting-related key on the control connection <b>conn</b>. If we can
993 * answer the request for <b>question</b>, then set *<b>answer</b> to a newly
994 * allocated string holding the result. Otherwise, set *<b>answer</b> to
995 * NULL. */
997 getinfo_helper_accounting(control_connection_t *conn,
998 const char *question, char **answer,
999 const char **errmsg)
1001 (void) conn;
1002 (void) errmsg;
1003 if (!strcmp(question, "accounting/enabled")) {
1004 *answer = tor_strdup(accounting_is_enabled(get_options()) ? "1" : "0");
1005 } else if (!strcmp(question, "accounting/hibernating")) {
1006 if (hibernate_state == HIBERNATE_STATE_DORMANT)
1007 *answer = tor_strdup("hard");
1008 else if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH)
1009 *answer = tor_strdup("soft");
1010 else
1011 *answer = tor_strdup("awake");
1012 } else if (!strcmp(question, "accounting/bytes")) {
1013 tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
1014 U64_PRINTF_ARG(n_bytes_read_in_interval),
1015 U64_PRINTF_ARG(n_bytes_written_in_interval));
1016 } else if (!strcmp(question, "accounting/bytes-left")) {
1017 uint64_t limit = get_options()->AccountingMax;
1018 if (get_options()->AccountingRule == ACCT_SUM) {
1019 uint64_t total_left = 0;
1020 uint64_t total_bytes = get_accounting_bytes();
1021 if (total_bytes < limit)
1022 total_left = limit - total_bytes;
1023 tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
1024 U64_PRINTF_ARG(total_left), U64_PRINTF_ARG(total_left));
1025 } else {
1026 uint64_t read_left = 0, write_left = 0;
1027 if (n_bytes_read_in_interval < limit)
1028 read_left = limit - n_bytes_read_in_interval;
1029 if (n_bytes_written_in_interval < limit)
1030 write_left = limit - n_bytes_written_in_interval;
1031 tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
1032 U64_PRINTF_ARG(read_left), U64_PRINTF_ARG(write_left));
1034 } else if (!strcmp(question, "accounting/interval-start")) {
1035 *answer = tor_malloc(ISO_TIME_LEN+1);
1036 format_iso_time(*answer, interval_start_time);
1037 } else if (!strcmp(question, "accounting/interval-wake")) {
1038 *answer = tor_malloc(ISO_TIME_LEN+1);
1039 format_iso_time(*answer, interval_wakeup_time);
1040 } else if (!strcmp(question, "accounting/interval-end")) {
1041 *answer = tor_malloc(ISO_TIME_LEN+1);
1042 format_iso_time(*answer, interval_end_time);
1043 } else {
1044 *answer = NULL;
1046 return 0;
1049 #ifdef TOR_UNIT_TESTS
1051 * Manually change the hibernation state. Private; used only by the unit
1052 * tests.
1054 void
1055 hibernate_set_state_for_testing_(hibernate_state_t newstate)
1057 hibernate_state = newstate;
1059 #endif