1 /* Copyright 2004 Roger Dingledine, Nick Mathewson. */
2 /* See LICENSE for licensing information */
4 const char hibernate_c_id
[] = "$Id$";
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
16 - send destroy in response to create cells
17 - send end (policy failed) in response to begin cells
18 - close an OR conn when it has no circuits
21 (entered when bandwidth hard limit reached)
22 - close all OR/AP/exit conns)
27 #define HIBERNATE_STATE_LIVE 1
28 #define HIBERNATE_STATE_EXITING 2
29 #define HIBERNATE_STATE_LOWBANDWIDTH 3
30 #define HIBERNATE_STATE_DORMANT 4
32 #define SHUTDOWN_WAIT_LENGTH 30 /* seconds */
34 extern long stats_n_seconds_working
; /* published uptime */
36 static int hibernate_state
= HIBERNATE_STATE_LIVE
;
37 /** If are hibernating, when do we plan to wake up? Set to 0 if we
38 * aren't hibernating. */
39 static time_t hibernate_end_time
= 0;
42 UNIT_MONTH
=1, UNIT_WEEK
=2, UNIT_DAY
=3,
45 /* Fields for accounting logic. Accounting overview:
47 * Accounting is designed to ensure that no more than N bytes are sent
48 * in either direction over a given interval (currently, one month,
49 * starting at 0:00 GMT an arbitrary day within the month). We could
50 * try to do this by choking our bandwidth to a trickle, but that
51 * would make our streams useless. Instead, we estimate what our
52 * bandwidth usage will be, and guess how long we'll be able to
53 * provide that much bandwidth before hitting our limit. We then
54 * choose a random time within the accounting interval to come up (so
55 * that we don't get 50 Tors running on the 1st of the month and none
58 * Each interval runs as follows:
60 * 1. We guess our bandwidth usage, based on how much we used
61 * last time. We choose a "wakeup time" within the interval to come up.
62 * 2. Until the chosen wakeup time, we hibernate.
63 * 3. We come up at the wakeup time, and provide bandwidth until we are
64 * "very close" to running out.
65 * 4. Then we go into low-bandwidth mode, and stop accepting new
66 * connections, but provide bandwidth until we run out.
67 * 5. Then we hibernate until the end of the interval.
69 * If the interval ends before we run out of bandwidth, we go back to
73 /** How many bytes have we read/written in this accounting interval? */
74 static uint64_t n_bytes_read_in_interval
= 0;
75 static uint64_t n_bytes_written_in_interval
= 0;
76 /** How many seconds have we been running this interval? */
77 static uint32_t n_seconds_active_in_interval
= 0;
78 /** When did this accounting interval start? */
79 static time_t interval_start_time
= 0;
80 /** When will this accounting interval end? */
81 static time_t interval_end_time
= 0;
82 /** How far into the accounting interval should we hibernate? */
83 static time_t interval_wakeup_time
= 0;
84 /** How much bandwidth do we 'expect' to use per minute? (0 if we have no
85 * info from the last period.) */
86 static uint32_t expected_bandwidth_usage
= 0;
87 /** What unit are we using for our accounting? */
88 static time_unit_t cfg_unit
= UNIT_MONTH
;
89 /** How many days,hours,minutes into each unit does our accounting interval
91 static int cfg_start_day
= 0;
92 static int cfg_start_hour
= 0;
93 static int cfg_start_min
= 0;
95 static void reset_accounting(time_t now
);
96 static int read_bandwidth_usage(void);
97 static time_t start_of_accounting_period_after(time_t now
);
98 static time_t start_of_accounting_period_containing(time_t now
);
99 static void accounting_set_wakeup_time(void);
102 * Functions for bandwidth accounting.
105 /** Configure accounting start/end time settings based on
106 * options->AccountingStart. Return 0 on success, -1 on failure. If
107 * <b>validate_only</b> is true, do not change the current settings. */
109 accounting_parse_options(or_options_t
*options
, int validate_only
)
115 const char *v
= options
->AccountingStart
;
120 if (!validate_only
) {
121 cfg_unit
= UNIT_MONTH
;
129 items
= smartlist_create();
130 smartlist_split_string(items
, v
, NULL
,
131 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
,0);
132 if (smartlist_len(items
)<2) {
133 log_fn(LOG_WARN
, "Too few arguments to AccountingStart");
136 s
= smartlist_get(items
,0);
137 if (0==strcasecmp(s
, "month")) {
139 } else if (0==strcasecmp(s
, "week")) {
141 } else if (0==strcasecmp(s
, "day")) {
144 log_fn(LOG_WARN
, "Unrecognized accounting unit '%s': only 'month', 'week', and 'day' are supported.", s
);
150 d
= tor_parse_long(smartlist_get(items
,1), 10, 1, 7, &ok
, NULL
);
152 log_fn(LOG_WARN
, "Weekly accounting must start begin on a day between 1(Monday) and 7 (Sunday)");
157 d
= tor_parse_long(smartlist_get(items
,1), 10, 1, 28, &ok
, NULL
);
159 log_fn(LOG_WARN
, "Monthly accounting must start begin on a day between 1 and 28");
170 idx
= unit
==UNIT_DAY
?1:2;
171 if (smartlist_len(items
) != (idx
+1)) {
172 log_fn(LOG_WARN
, "Accounting unit '%s' requires %d arguments",
176 s
= smartlist_get(items
, idx
);
177 h
= tor_parse_long(s
, 10, 0, 23, &ok
, &cp
);
179 log_fn(LOG_WARN
, "Accounting start time not parseable: bad hour.");
182 if (!cp
|| *cp
!=':') {
183 log_fn(LOG_WARN
,"Accounting start time not parseable: not in HH:MM format");
186 m
= tor_parse_long(cp
+1, 10, 0, 59, &ok
, &cp
);
188 log_fn(LOG_WARN
, "Accounting start time not parseable: bad minute");
191 if (!cp
|| *cp
!='\0') {
192 log_fn(LOG_WARN
,"Accounting start time not parseable: not in HH:MM format");
196 if (!validate_only
) {
198 cfg_start_day
= (int)d
;
199 cfg_start_hour
= (int)h
;
200 cfg_start_min
= (int)m
;
202 SMARTLIST_FOREACH(items
, char *, s
, tor_free(s
));
203 smartlist_free(items
);
206 SMARTLIST_FOREACH(items
, char *, s
, tor_free(s
));
207 smartlist_free(items
);
211 /** If we want to manage the accounting system and potentially
212 * hibernate, return 1, else return 0.
214 int accounting_is_enabled(or_options_t
*options
) {
215 if (options
->AccountingMax
)
220 /** Called from main.c to tell us that <b>seconds</b> seconds have
221 * passed, <b>n_read</b> bytes have been read, and <b>n_written</b>
222 * bytes have been written. */
224 accounting_add_bytes(size_t n_read
, size_t n_written
, int seconds
)
226 n_bytes_read_in_interval
+= n_read
;
227 n_bytes_written_in_interval
+= n_written
;
228 /* If we haven't been called in 10 seconds, we're probably jumping
230 n_seconds_active_in_interval
+= (seconds
< 10) ? seconds
: 0;
233 /** If get_end, return the end of the accounting period that contains
234 * the time <b>now</b>. Else, return the start of the accounting
235 * period that contains the time <b>now</b> */
237 edge_of_accounting_period_containing(time_t now
, int get_end
)
241 tm
= localtime(&now
);
243 /* Set 'before' to true iff the current time is before the hh:mm
244 * changeover time for today. */
245 before
= tm
->tm_hour
< cfg_start_hour
||
246 (tm
->tm_hour
== cfg_start_hour
&& tm
->tm_min
< cfg_start_min
);
248 /* Dispatch by unit. First, find the start day of the given period;
249 * then, if get_end is true, increment to the end day. */
253 /* If this is before the Nth, we want the Nth of last month. */
254 if (tm
->tm_mday
< cfg_start_day
||
255 (tm
->tm_mday
< cfg_start_day
&& before
)) {
258 /* Otherwise, the month is correct. */
259 tm
->tm_mday
= cfg_start_day
;
265 /* What is the 'target' day of the week in struct tm format? (We
266 say Sunday==7; struct tm says Sunday==0.) */
267 int wday
= cfg_start_day
% 7;
268 /* How many days do we subtract from today to get to the right day? */
269 int delta
= (7+tm
->tm_wday
-wday
)%7;
270 /* If we are on the right day, but the changeover hasn't happened yet,
271 * then subtract a whole week. */
272 if (delta
== 0 && before
)
274 tm
->tm_mday
-= delta
;
289 tm
->tm_hour
= cfg_start_hour
;
290 tm
->tm_min
= cfg_start_min
;
292 tm
->tm_isdst
= -1; /* Autodetect DST */
296 /** Return the start of the accounting period containing the time
299 start_of_accounting_period_containing(time_t now
)
301 return edge_of_accounting_period_containing(now
, 0);
304 /** Return the start of the accounting period that comes after the one
305 * containing the time <b>now</b>. */
307 start_of_accounting_period_after(time_t now
)
309 return edge_of_accounting_period_containing(now
, 1);
312 /** Initialize the accounting subsystem. */
314 configure_accounting(time_t now
)
316 /* Try to remember our recorded usage. */
317 if (!interval_start_time
)
318 read_bandwidth_usage(); /* If we fail, we'll leave values at zero, and
320 if (!interval_start_time
||
321 start_of_accounting_period_after(interval_start_time
) <= now
) {
322 /* We didn't have recorded usage, or we don't have recorded usage
323 * for this interval. Start a new interval. */
324 log_fn(LOG_INFO
, "Starting new accounting interval.");
325 reset_accounting(now
);
326 } else if (interval_start_time
==
327 start_of_accounting_period_containing(interval_start_time
)) {
328 log_fn(LOG_INFO
, "Continuing accounting interval.");
329 /* We are in the interval we thought we were in. Do nothing.*/
330 interval_end_time
= start_of_accounting_period_after(interval_start_time
);
332 log_fn(LOG_WARN
, "Mismatched accounting interval; starting a fresh one.");
333 reset_accounting(now
);
335 accounting_set_wakeup_time();
338 /** Set expected_bandwidth_usage based on how much we sent/received
339 * per minute last interval (if we were up for at least 30 minutes),
340 * or based on our declared bandwidth otherwise. */
342 update_expected_bandwidth(void)
344 uint64_t used
, expected
;
345 uint64_t max_configured
= (get_options()->BandwidthRate
* 60);
347 if (n_seconds_active_in_interval
< 1800) {
348 /* If we haven't gotten enough data last interval, set 'expected'
349 * to 0. This will set our wakeup to the start of the interval.
350 * Next interval, we'll choose our starting time based on how much
351 * we sent this interval.
355 used
= n_bytes_written_in_interval
< n_bytes_read_in_interval
?
356 n_bytes_read_in_interval
: n_bytes_written_in_interval
;
357 expected
= used
/ (n_seconds_active_in_interval
/ 60);
358 if (expected
> max_configured
)
359 expected
= max_configured
;
361 if (expected
> UINT32_MAX
)
362 expected
= UINT32_MAX
;
363 expected_bandwidth_usage
= (uint32_t) expected
;
366 /** Called at the start of a new accounting interval: reset our
367 * expected bandwidth usage based on what happened last time, set up
368 * the start and end of the interval, and clear byte/time totals.
371 reset_accounting(time_t now
) {
372 log_fn(LOG_INFO
, "Starting new accounting interval.");
373 update_expected_bandwidth();
374 interval_start_time
= start_of_accounting_period_containing(now
);
375 interval_end_time
= start_of_accounting_period_after(interval_start_time
);
376 n_bytes_read_in_interval
= 0;
377 n_bytes_written_in_interval
= 0;
378 n_seconds_active_in_interval
= 0;
381 /** Return true iff we should save our bandwidth usage to disk. */
383 time_to_record_bandwidth_usage(time_t now
)
385 /* Note every 60 sec */
386 #define NOTE_INTERVAL (60)
387 /* Or every 20 megabytes */
388 #define NOTE_BYTES 20*(1024*1024)
389 static uint64_t last_read_bytes_noted
= 0;
390 static uint64_t last_written_bytes_noted
= 0;
391 static time_t last_time_noted
= 0;
393 if (last_time_noted
+ NOTE_INTERVAL
<= now
||
394 last_read_bytes_noted
+ NOTE_BYTES
<= n_bytes_read_in_interval
||
395 last_written_bytes_noted
+ NOTE_BYTES
<= n_bytes_written_in_interval
||
396 (interval_end_time
&& interval_end_time
<= now
)) {
397 last_time_noted
= now
;
398 last_read_bytes_noted
= n_bytes_read_in_interval
;
399 last_written_bytes_noted
= n_bytes_written_in_interval
;
406 accounting_run_housekeeping(time_t now
)
408 if (now
>= interval_end_time
) {
409 configure_accounting(now
);
411 if (time_to_record_bandwidth_usage(now
)) {
412 if (accounting_record_bandwidth_usage(now
)) {
413 log_fn(LOG_ERR
, "Couldn't record bandwidth usage; exiting.");
414 /* XXX this can fail when you're out of fd's, causing a crash.
415 * Perhaps the better answer is to hold the file open all the
422 /** Based on our interval and our estimated bandwidth, choose a
423 * deterministic (but random-ish) time to wake up. */
425 accounting_set_wakeup_time(void)
427 char buf
[ISO_TIME_LEN
+1];
428 char digest
[DIGEST_LEN
];
429 crypto_digest_env_t
*d_env
;
430 int time_in_interval
;
431 int time_to_exhaust_bw
;
432 int time_to_consider
;
434 if (! identity_key_is_set()) {
435 if (init_keys() < 0) {
436 log_fn(LOG_ERR
, "Error initializing keys");
441 format_iso_time(buf
, interval_start_time
);
442 crypto_pk_get_digest(get_identity_key(), digest
);
444 d_env
= crypto_new_digest_env();
445 crypto_digest_add_bytes(d_env
, buf
, ISO_TIME_LEN
);
446 crypto_digest_add_bytes(d_env
, digest
, DIGEST_LEN
);
447 crypto_digest_get_digest(d_env
, digest
, DIGEST_LEN
);
448 crypto_free_digest_env(d_env
);
450 if (!expected_bandwidth_usage
) {
451 char buf1
[ISO_TIME_LEN
+1];
452 char buf2
[ISO_TIME_LEN
+1];
453 format_local_iso_time(buf1
, interval_start_time
);
454 format_local_iso_time(buf2
, interval_end_time
);
455 time_to_exhaust_bw
= 24*60*60;
456 interval_wakeup_time
= interval_start_time
;
458 log_fn(LOG_NOTICE
, "Configured hibernation. This interval begins at %s "
459 "and ends at %s. We have no prior estimate for bandwidth, so "
460 "we will start out awake and hibernate when we exhaust our quota.",
465 time_to_exhaust_bw
= (int)
466 (get_options()->AccountingMax
/expected_bandwidth_usage
)*60;
467 time_in_interval
= interval_end_time
- interval_start_time
;
468 time_to_consider
= time_in_interval
- time_to_exhaust_bw
;
470 if (time_to_consider
<=0) {
471 interval_wakeup_time
= interval_start_time
;
473 /* XXX can we simplify this just by picking a random (non-deterministic)
474 * time to be up? If we go down and come up, then we pick a new one. Is
475 * that good enough? -RD */
477 /* This is not a perfectly unbiased conversion, but it is good enough:
478 * in the worst case, the first half of the day is 0.06 percent likelier
479 * to be chosen than the last half. */
480 interval_wakeup_time
= interval_start_time
+
481 (get_uint32(digest
) % time_to_consider
);
483 format_iso_time(buf
, interval_wakeup_time
);
487 char buf1
[ISO_TIME_LEN
+1];
488 char buf2
[ISO_TIME_LEN
+1];
489 char buf3
[ISO_TIME_LEN
+1];
490 char buf4
[ISO_TIME_LEN
+1];
491 time_t down_time
= interval_wakeup_time
+time_to_exhaust_bw
;
492 if (down_time
>interval_end_time
)
493 down_time
= interval_end_time
;
494 format_local_iso_time(buf1
, interval_start_time
);
495 format_local_iso_time(buf2
, interval_wakeup_time
);
496 format_local_iso_time(buf3
,
497 down_time
<interval_end_time
?down_time
:interval_end_time
);
498 format_local_iso_time(buf4
, interval_end_time
);
500 log_fn(LOG_NOTICE
, "Configured hibernation. This interval began at %s; "
501 "the scheduled wake-up time %s %s; "
502 "we expect%s to exhaust our quota for this interval around %s; "
503 "the next interval begins at %s (all times local)",
505 time(NULL
)<interval_wakeup_time
?"is":"was", buf2
,
506 time(NULL
)<down_time
?"":"ed", buf3
,
511 #define BW_ACCOUNTING_VERSION 1
512 /** Save all our bandwidth tracking information to disk. Return 0 on
513 * success, -1 on failure*/
515 accounting_record_bandwidth_usage(time_t now
)
519 char time1
[ISO_TIME_LEN
+1];
520 char time2
[ISO_TIME_LEN
+1];
523 Version\nTime\nTime\nRead\nWrite\nSeconds\nExpected-Rate\n */
525 format_iso_time(time1
, interval_start_time
);
526 format_iso_time(time2
, now
);
527 tor_snprintf(cp
, sizeof(buf
),
528 "%d\n%s\n%s\n"U64_FORMAT
"\n"U64_FORMAT
"\n%lu\n%lu\n",
529 BW_ACCOUNTING_VERSION
,
532 U64_PRINTF_ARG(n_bytes_read_in_interval
),
533 U64_PRINTF_ARG(n_bytes_written_in_interval
),
534 (unsigned long)n_seconds_active_in_interval
,
535 (unsigned long)expected_bandwidth_usage
);
536 tor_snprintf(fname
, sizeof(fname
), "%s/bw_accounting",
537 get_options()->DataDirectory
);
539 return write_str_to_file(fname
, buf
, 0);
542 /** Read stored accounting information from disk. Return 0 on success;
543 * return -1 and change nothing on failure. */
545 read_bandwidth_usage(void)
550 uint64_t n_read
, n_written
;
551 uint32_t expected_bw
, n_seconds
;
555 tor_snprintf(fname
, sizeof(fname
), "%s/bw_accounting",
556 get_options()->DataDirectory
);
557 if (!(s
= read_file_to_str(fname
, 0))) {
560 elts
= smartlist_create();
561 smartlist_split_string(elts
, s
, "\n", SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
,0);
564 if (smartlist_len(elts
)<1 ||
565 atoi(smartlist_get(elts
,0)) != BW_ACCOUNTING_VERSION
) {
566 log_fn(LOG_WARN
, "Unrecognized bw_accounting file version: %s",
567 (const char*)smartlist_get(elts
,0));
570 if (smartlist_len(elts
) < 7) {
571 log_fn(LOG_WARN
, "Corrupted bw_accounting file: %d lines",
572 smartlist_len(elts
));
575 if (parse_iso_time(smartlist_get(elts
,1), &t1
)) {
576 log_fn(LOG_WARN
, "Error parsing bandwidth usage start time.");
579 if (parse_iso_time(smartlist_get(elts
,2), &t2
)) {
580 log_fn(LOG_WARN
, "Error parsing bandwidth usage last-written time");
583 n_read
= tor_parse_uint64(smartlist_get(elts
,3), 10, 0, UINT64_MAX
,
586 log_fn(LOG_WARN
, "Error parsing number of bytes read");
589 n_written
= tor_parse_uint64(smartlist_get(elts
,4), 10, 0, UINT64_MAX
,
592 log_fn(LOG_WARN
, "Error parsing number of bytes read");
595 n_seconds
= (uint32_t)tor_parse_ulong(smartlist_get(elts
,5), 10,0,ULONG_MAX
,
598 log_fn(LOG_WARN
, "Error parsing number of seconds live");
601 expected_bw
=(uint32_t)tor_parse_ulong(smartlist_get(elts
,6), 10,0,ULONG_MAX
,
604 log_fn(LOG_WARN
, "Error parsing expected bandwidth");
608 n_bytes_read_in_interval
= n_read
;
609 n_bytes_written_in_interval
= n_written
;
610 n_seconds_active_in_interval
= n_seconds
;
611 interval_start_time
= t1
;
612 expected_bandwidth_usage
= expected_bw
;
614 log_fn(LOG_INFO
, "Successfully read bandwidth accounting file written at %s for interval starting at %s. We have been active for %lu seconds in this interval. At the start of the interval, we expected to use about %lu KB per second. ("U64_FORMAT
" bytes read so far, "U64_FORMAT
" bytes written so far)",
615 (char*)smartlist_get(elts
,2),
616 (char*)smartlist_get(elts
,1),
617 (unsigned long)n_seconds_active_in_interval
,
618 (unsigned long)((uint64_t)expected_bandwidth_usage
*1024/60),
619 U64_PRINTF_ARG(n_bytes_read_in_interval
),
620 U64_PRINTF_ARG(n_bytes_written_in_interval
));
621 SMARTLIST_FOREACH(elts
, char *, cp
, tor_free(cp
));
622 smartlist_free(elts
);
626 SMARTLIST_FOREACH(elts
, char *, cp
, tor_free(cp
));
627 smartlist_free(elts
);
631 /** Return true iff we have sent/received all the bytes we are willing
632 * to send/receive this interval. */
634 hibernate_hard_limit_reached(void)
636 uint64_t hard_limit
= get_options()->AccountingMax
;
639 return n_bytes_read_in_interval
>= hard_limit
640 || n_bytes_written_in_interval
>= hard_limit
;
643 /** Return true iff we have sent/received almost all the bytes we are willing
644 * to send/receive this interval. */
645 static int hibernate_soft_limit_reached(void)
647 uint64_t soft_limit
= (uint64_t) ((get_options()->AccountingMax
) * .95);
650 return n_bytes_read_in_interval
>= soft_limit
651 || n_bytes_written_in_interval
>= soft_limit
;
654 /** Called when we get a SIGINT, or when bandwidth soft limit is
655 * reached. Puts us into "loose hibernation": we don't accept new
656 * connections, but we continue handling old ones. */
657 static void hibernate_begin(int new_state
, time_t now
) {
660 if (hibernate_state
== HIBERNATE_STATE_EXITING
) {
661 /* we've been called twice now. close immediately. */
662 log(LOG_NOTICE
,"Second sigint received; exiting now.");
667 /* close listeners. leave control listener(s). */
668 while ((conn
= connection_get_by_type(CONN_TYPE_OR_LISTENER
)) ||
669 (conn
= connection_get_by_type(CONN_TYPE_AP_LISTENER
)) ||
670 (conn
= connection_get_by_type(CONN_TYPE_DIR_LISTENER
))) {
671 log_fn(LOG_INFO
,"Closing listener type %d", conn
->type
);
672 connection_mark_for_close(conn
);
675 /* XXX kill intro point circs */
676 /* XXX upload rendezvous service descriptors with no intro points */
678 if (new_state
== HIBERNATE_STATE_EXITING
) {
679 log(LOG_NOTICE
,"Interrupt: will shut down in %d seconds. Interrupt again to exit now.", SHUTDOWN_WAIT_LENGTH
);
680 hibernate_end_time
= time(NULL
) + SHUTDOWN_WAIT_LENGTH
;
681 } else { /* soft limit reached */
682 hibernate_end_time
= interval_end_time
;
685 hibernate_state
= new_state
;
686 accounting_record_bandwidth_usage(now
);
689 /** Called when we've been hibernating and our timeout is reached. */
691 hibernate_end(int new_state
) {
693 tor_assert(hibernate_state
== HIBERNATE_STATE_LOWBANDWIDTH
||
694 hibernate_state
== HIBERNATE_STATE_DORMANT
);
696 /* listeners will be relaunched in run_scheduled_events() in main.c */
697 log_fn(LOG_NOTICE
,"Hibernation period ended. Resuming normal activity.");
699 hibernate_state
= new_state
;
700 hibernate_end_time
= 0; /* no longer hibernating */
701 stats_n_seconds_working
= 0; /* reset published uptime */
704 /** A wrapper around hibernate_begin, for when we get SIGINT. */
706 hibernate_begin_shutdown(void) {
707 hibernate_begin(HIBERNATE_STATE_EXITING
, time(NULL
));
710 /** Return true iff we are currently hibernating. */
712 we_are_hibernating(void) {
713 return hibernate_state
!= HIBERNATE_STATE_LIVE
;
716 /** If we aren't currently dormant, close all connections and become
719 hibernate_go_dormant(time_t now
) {
722 if (hibernate_state
== HIBERNATE_STATE_DORMANT
)
724 else if (hibernate_state
== HIBERNATE_STATE_LOWBANDWIDTH
)
725 hibernate_state
= HIBERNATE_STATE_DORMANT
;
727 hibernate_begin(HIBERNATE_STATE_DORMANT
, now
);
729 log_fn(LOG_NOTICE
,"Going dormant. Blowing away remaining connections.");
731 /* Close all OR/AP/exit conns. Leave dir conns because we still want
732 * to be able to upload server descriptors so people know we're still
733 * running, and download directories so we can detect if we're obsolete.
734 * Leave control conns because we still want to be controllable.
736 while ((conn
= connection_get_by_type(CONN_TYPE_OR
)) ||
737 (conn
= connection_get_by_type(CONN_TYPE_AP
)) ||
738 (conn
= connection_get_by_type(CONN_TYPE_EXIT
))) {
739 if (CONN_IS_EDGE(conn
))
740 connection_edge_end(conn
, END_STREAM_REASON_MISC
, conn
->cpath_layer
);
741 log_fn(LOG_INFO
,"Closing conn type %d", conn
->type
);
742 connection_mark_for_close(conn
);
745 accounting_record_bandwidth_usage(now
);
748 /** Called when hibernate_end_time has arrived. */
750 hibernate_end_time_elapsed(time_t now
)
752 char buf
[ISO_TIME_LEN
+1];
754 /* The interval has ended, or it is wakeup time. Find out which. */
755 accounting_run_housekeeping(now
);
756 if (interval_wakeup_time
<= now
) {
757 /* The interval hasn't changed, but interval_wakeup_time has passed.
758 * It's time to wake up and start being a server. */
759 hibernate_end(HIBERNATE_STATE_LIVE
);
762 /* The interval has changed, and it isn't time to wake up yet. */
763 hibernate_end_time
= interval_wakeup_time
;
764 format_iso_time(buf
,interval_wakeup_time
);
765 if (hibernate_state
!= HIBERNATE_STATE_DORMANT
) {
766 /* We weren't sleeping before; we should sleep now. */
767 log_fn(LOG_NOTICE
, "Accounting period ended. Commencing hibernation until %s GMT",buf
);
768 hibernate_go_dormant(now
);
770 log_fn(LOG_NOTICE
, "Accounting period ended. This period, we will hibernate until %s GMT",buf
);
775 /** Consider our environment and decide if it's time
776 * to start/stop hibernating.
778 void consider_hibernation(time_t now
) {
779 int accounting_enabled
= get_options()->AccountingMax
!= 0;
780 char buf
[ISO_TIME_LEN
+1];
782 /* If we're in 'exiting' mode, then we just shut down after the interval
784 if (hibernate_state
== HIBERNATE_STATE_EXITING
) {
785 tor_assert(hibernate_end_time
);
786 if (hibernate_end_time
<= now
) {
787 log(LOG_NOTICE
,"Clean shutdown finished. Exiting.");
791 return; /* if exiting soon, don't worry about bandwidth limits */
794 if (hibernate_state
== HIBERNATE_STATE_DORMANT
) {
795 /* We've been hibernating because of bandwidth accounting. */
796 tor_assert(hibernate_end_time
);
797 if (hibernate_end_time
> now
&& accounting_enabled
) {
798 /* If we're hibernating, don't wake up until it's time, regardless of
799 * whether we're in a new interval. */
802 hibernate_end_time_elapsed(now
);
806 /* Else, we aren't hibernating. See if it's time to start hibernating, or to
808 if (hibernate_state
== HIBERNATE_STATE_LIVE
) {
809 if (hibernate_soft_limit_reached()) {
810 log_fn(LOG_NOTICE
,"Bandwidth soft limit reached; commencing hibernation.");
811 hibernate_begin(HIBERNATE_STATE_LOWBANDWIDTH
, now
);
812 } else if (accounting_enabled
&& now
< interval_wakeup_time
) {
813 format_iso_time(buf
,interval_wakeup_time
);
814 log_fn(LOG_NOTICE
, "Commencing hibernation. We will wake up at %s GMT",buf
);
815 hibernate_go_dormant(now
);
819 if (hibernate_state
== HIBERNATE_STATE_LOWBANDWIDTH
) {
820 if (!accounting_enabled
) {
821 hibernate_end_time_elapsed(now
);
822 } else if (hibernate_hard_limit_reached()) {
823 hibernate_go_dormant(now
);
824 } else if (hibernate_end_time
<= now
) {
825 /* The hibernation period ended while we were still in lowbandwidth.*/
826 hibernate_end_time_elapsed(now
);