1 /* Copyright (c) 2010-2015, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
6 * \brief Keep status information and log the heartbeat messages.
12 #include "circuituse.h"
18 #include "circuitlist.h"
21 #include "hibernate.h"
23 #include "statefile.h"
25 static void log_accounting(const time_t now
, const or_options_t
*options
);
28 /** Return the total number of circuits. */
32 return smartlist_len(circuit_get_global_list());
35 /** Take seconds <b>secs</b> and return a newly allocated human-readable
38 secs_to_uptime(long secs
)
40 long int days
= secs
/ 86400;
41 int hours
= (int)((secs
- (days
* 86400)) / 3600);
42 int minutes
= (int)((secs
- (days
* 86400) - (hours
* 3600)) / 60);
43 char *uptime_string
= NULL
;
47 tor_asprintf(&uptime_string
, "%d:%02d hours", hours
, minutes
);
50 tor_asprintf(&uptime_string
, "%ld day %d:%02d hours",
51 days
, hours
, minutes
);
54 tor_asprintf(&uptime_string
, "%ld days %d:%02d hours",
55 days
, hours
, minutes
);
62 /** Take <b>bytes</b> and returns a newly allocated human-readable usage
65 bytes_to_usage(uint64_t bytes
)
67 char *bw_string
= NULL
;
69 if (bytes
< (1<<20)) { /* Less than a megabyte. */
70 tor_asprintf(&bw_string
, U64_FORMAT
" kB", U64_PRINTF_ARG(bytes
>>10));
71 } else if (bytes
< (1<<30)) { /* Megabytes. Let's add some precision. */
72 double bw
= U64_TO_DBL(bytes
);
73 tor_asprintf(&bw_string
, "%.2f MB", bw
/(1<<20));
74 } else { /* Gigabytes. */
75 double bw
= U64_TO_DBL(bytes
);
76 tor_asprintf(&bw_string
, "%.2f GB", bw
/(1<<30));
82 /** Log a "heartbeat" message describing Tor's status and history so that the
83 * user can know that there is indeed a running Tor. Return 0 on success and
86 log_heartbeat(time_t now
)
91 const routerinfo_t
*me
;
92 double r
= tls_get_write_overhead_ratio();
93 const int hibernating
= we_are_hibernating();
95 const or_options_t
*options
= get_options();
97 if (public_server_mode(options
) && !hibernating
) {
98 /* Let's check if we are in the current cached consensus. */
99 if (!(me
= router_get_my_routerinfo()))
100 return -1; /* Something stinks, we won't even attempt this. */
102 if (!node_get_by_id(me
->cache_info
.identity_digest
))
103 log_fn(LOG_NOTICE
, LD_HEARTBEAT
, "Heartbeat: It seems like we are not "
104 "in the cached consensus.");
107 uptime
= secs_to_uptime(get_uptime());
108 bw_rcvd
= bytes_to_usage(get_bytes_read());
109 bw_sent
= bytes_to_usage(get_bytes_written());
111 log_fn(LOG_NOTICE
, LD_HEARTBEAT
, "Heartbeat: Tor's uptime is %s, with %d "
112 "circuits open. I've sent %s and received %s.%s",
113 uptime
, count_circuits(), bw_sent
, bw_rcvd
,
114 hibernating
?" We are currently hibernating.":"");
116 if (server_mode(options
) && accounting_is_enabled(options
) && !hibernating
) {
117 log_accounting(now
, options
);
120 double fullness_pct
= 100;
121 if (stats_n_data_cells_packaged
&& !hibernating
) {
123 100*(U64_TO_DBL(stats_n_data_bytes_packaged
) /
124 U64_TO_DBL(stats_n_data_cells_packaged
*RELAY_PAYLOAD_SIZE
));
126 const double overhead_pct
= ( r
- 1.0 ) * 100.0;
128 #define FULLNESS_PCT_THRESHOLD 80
129 #define TLS_OVERHEAD_THRESHOLD 15
131 const int severity
= (fullness_pct
< FULLNESS_PCT_THRESHOLD
||
132 overhead_pct
> TLS_OVERHEAD_THRESHOLD
)
133 ? LOG_NOTICE
: LOG_INFO
;
135 log_fn(severity
, LD_HEARTBEAT
,
136 "Average packaged cell fullness: %2.3f%%. "
137 "TLS write overhead: %.f%%", fullness_pct
, overhead_pct
);
139 if (public_server_mode(options
)) {
140 rep_hist_log_circuit_handshake_stats(now
);
141 rep_hist_log_link_protocol_counts();
144 circuit_log_ancient_one_hop_circuits(1800);
146 if (options
->BridgeRelay
) {
148 msg
= format_client_stats_heartbeat(now
);
150 log_notice(LD_HEARTBEAT
, "%s", msg
);
162 log_accounting(const time_t now
, const or_options_t
*options
)
164 or_state_t
*state
= get_or_state();
165 char *acc_rcvd
= bytes_to_usage(state
->AccountingBytesReadInInterval
);
166 char *acc_sent
= bytes_to_usage(state
->AccountingBytesWrittenInInterval
);
167 uint64_t acc_bytes
= options
->AccountingMax
;
169 time_t interval_end
= accounting_get_end_time();
170 char end_buf
[ISO_TIME_LEN
+ 1];
171 char *remaining
= NULL
;
172 if (options
->AccountingRule
== ACCT_SUM
)
174 acc_max
= bytes_to_usage(acc_bytes
);
175 format_local_iso_time(end_buf
, interval_end
);
176 remaining
= secs_to_uptime(interval_end
- now
);
178 log_notice(LD_HEARTBEAT
, "Heartbeat: Accounting enabled. "
179 "Sent: %s / %s, Received: %s / %s. The "
180 "current accounting interval ends on %s, in %s.",
181 acc_sent
, acc_max
, acc_rcvd
, acc_max
, end_buf
, remaining
);