Add link protocol version counts to the heartbeat message
[tor.git] / src / or / rephist.c
blobfe0997c891ae69f55931da25e8a71671aa3d9e22
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 rephist.c
7 * \brief Basic history and "reputation" functionality to remember
8 * which servers have worked in the past, how much bandwidth we've
9 * been using, which ports we tend to want, and so on; further,
10 * exit port statistics, cell statistics, and connection statistics.
11 **/
13 #include "or.h"
14 #include "circuitlist.h"
15 #include "circuituse.h"
16 #include "config.h"
17 #include "networkstatus.h"
18 #include "nodelist.h"
19 #include "rephist.h"
20 #include "router.h"
21 #include "routerlist.h"
22 #include "ht.h"
24 static void bw_arrays_init(void);
25 static void predicted_ports_init(void);
27 /** Total number of bytes currently allocated in fields used by rephist.c. */
28 uint64_t rephist_total_alloc=0;
29 /** Number of or_history_t objects currently allocated. */
30 uint32_t rephist_total_num=0;
32 /** If the total weighted run count of all runs for a router ever falls
33 * below this amount, the router can be treated as having 0 MTBF. */
34 #define STABILITY_EPSILON 0.0001
35 /** Value by which to discount all old intervals for MTBF purposes. This
36 * is compounded every STABILITY_INTERVAL. */
37 #define STABILITY_ALPHA 0.95
38 /** Interval at which to discount all old intervals for MTBF purposes. */
39 #define STABILITY_INTERVAL (12*60*60)
40 /* (This combination of ALPHA, INTERVAL, and EPSILON makes it so that an
41 * interval that just ended counts twice as much as one that ended a week ago,
42 * 20X as much as one that ended a month ago, and routers that have had no
43 * uptime data for about half a year will get forgotten.) */
45 /** History of an OR-\>OR link. */
46 typedef struct link_history_t {
47 /** When did we start tracking this list? */
48 time_t since;
49 /** When did we most recently note a change to this link */
50 time_t changed;
51 /** How many times did extending from OR1 to OR2 succeed? */
52 unsigned long n_extend_ok;
53 /** How many times did extending from OR1 to OR2 fail? */
54 unsigned long n_extend_fail;
55 } link_history_t;
57 /** History of an OR. */
58 typedef struct or_history_t {
59 /** When did we start tracking this OR? */
60 time_t since;
61 /** When did we most recently note a change to this OR? */
62 time_t changed;
63 /** How many times did we successfully connect? */
64 unsigned long n_conn_ok;
65 /** How many times did we try to connect and fail?*/
66 unsigned long n_conn_fail;
67 /** How many seconds have we been connected to this OR before
68 * 'up_since'? */
69 unsigned long uptime;
70 /** How many seconds have we been unable to connect to this OR before
71 * 'down_since'? */
72 unsigned long downtime;
73 /** If nonzero, we have been connected since this time. */
74 time_t up_since;
75 /** If nonzero, we have been unable to connect since this time. */
76 time_t down_since;
78 /** The address at which we most recently connected to this OR
79 * successfully. */
80 tor_addr_t last_reached_addr;
82 /** The port at which we most recently connected to this OR successfully */
83 uint16_t last_reached_port;
85 /* === For MTBF tracking: */
86 /** Weighted sum total of all times that this router has been online.
88 unsigned long weighted_run_length;
89 /** If the router is now online (according to stability-checking rules),
90 * when did it come online? */
91 time_t start_of_run;
92 /** Sum of weights for runs in weighted_run_length. */
93 double total_run_weights;
94 /* === For fractional uptime tracking: */
95 time_t start_of_downtime;
96 unsigned long weighted_uptime;
97 unsigned long total_weighted_time;
99 /** Map from hex OR2 identity digest to a link_history_t for the link
100 * from this OR to OR2. */
101 digestmap_t *link_history_map;
102 } or_history_t;
104 /** When did we last multiply all routers' weighted_run_length and
105 * total_run_weights by STABILITY_ALPHA? */
106 static time_t stability_last_downrated = 0;
108 /** */
109 static time_t started_tracking_stability = 0;
111 /** Map from hex OR identity digest to or_history_t. */
112 static digestmap_t *history_map = NULL;
114 /** Return the or_history_t for the OR with identity digest <b>id</b>,
115 * creating it if necessary. */
116 static or_history_t *
117 get_or_history(const char* id)
119 or_history_t *hist;
121 if (tor_digest_is_zero(id))
122 return NULL;
124 hist = digestmap_get(history_map, id);
125 if (!hist) {
126 hist = tor_malloc_zero(sizeof(or_history_t));
127 rephist_total_alloc += sizeof(or_history_t);
128 rephist_total_num++;
129 hist->link_history_map = digestmap_new();
130 hist->since = hist->changed = time(NULL);
131 tor_addr_make_unspec(&hist->last_reached_addr);
132 digestmap_set(history_map, id, hist);
134 return hist;
137 /** Return the link_history_t for the link from the first named OR to
138 * the second, creating it if necessary. (ORs are identified by
139 * identity digest.)
141 static link_history_t *
142 get_link_history(const char *from_id, const char *to_id)
144 or_history_t *orhist;
145 link_history_t *lhist;
146 orhist = get_or_history(from_id);
147 if (!orhist)
148 return NULL;
149 if (tor_digest_is_zero(to_id))
150 return NULL;
151 lhist = (link_history_t*) digestmap_get(orhist->link_history_map, to_id);
152 if (!lhist) {
153 lhist = tor_malloc_zero(sizeof(link_history_t));
154 rephist_total_alloc += sizeof(link_history_t);
155 lhist->since = lhist->changed = time(NULL);
156 digestmap_set(orhist->link_history_map, to_id, lhist);
158 return lhist;
161 /** Helper: free storage held by a single link history entry. */
162 static void
163 free_link_history_(void *val)
165 rephist_total_alloc -= sizeof(link_history_t);
166 tor_free(val);
169 /** Helper: free storage held by a single OR history entry. */
170 static void
171 free_or_history(void *_hist)
173 or_history_t *hist = _hist;
174 digestmap_free(hist->link_history_map, free_link_history_);
175 rephist_total_alloc -= sizeof(or_history_t);
176 rephist_total_num--;
177 tor_free(hist);
180 /** Update an or_history_t object <b>hist</b> so that its uptime/downtime
181 * count is up-to-date as of <b>when</b>.
183 static void
184 update_or_history(or_history_t *hist, time_t when)
186 tor_assert(hist);
187 if (hist->up_since) {
188 tor_assert(!hist->down_since);
189 hist->uptime += (when - hist->up_since);
190 hist->up_since = when;
191 } else if (hist->down_since) {
192 hist->downtime += (when - hist->down_since);
193 hist->down_since = when;
197 /** Initialize the static data structures for tracking history. */
198 void
199 rep_hist_init(void)
201 history_map = digestmap_new();
202 bw_arrays_init();
203 predicted_ports_init();
206 /** Helper: note that we are no longer connected to the router with history
207 * <b>hist</b>. If <b>failed</b>, the connection failed; otherwise, it was
208 * closed correctly. */
209 static void
210 mark_or_down(or_history_t *hist, time_t when, int failed)
212 if (hist->up_since) {
213 hist->uptime += (when - hist->up_since);
214 hist->up_since = 0;
216 if (failed && !hist->down_since) {
217 hist->down_since = when;
221 /** Helper: note that we are connected to the router with history
222 * <b>hist</b>. */
223 static void
224 mark_or_up(or_history_t *hist, time_t when)
226 if (hist->down_since) {
227 hist->downtime += (when - hist->down_since);
228 hist->down_since = 0;
230 if (!hist->up_since) {
231 hist->up_since = when;
235 /** Remember that an attempt to connect to the OR with identity digest
236 * <b>id</b> failed at <b>when</b>.
238 void
239 rep_hist_note_connect_failed(const char* id, time_t when)
241 or_history_t *hist;
242 hist = get_or_history(id);
243 if (!hist)
244 return;
245 ++hist->n_conn_fail;
246 mark_or_down(hist, when, 1);
247 hist->changed = when;
250 /** Remember that an attempt to connect to the OR with identity digest
251 * <b>id</b> succeeded at <b>when</b>.
253 void
254 rep_hist_note_connect_succeeded(const char* id, time_t when)
256 or_history_t *hist;
257 hist = get_or_history(id);
258 if (!hist)
259 return;
260 ++hist->n_conn_ok;
261 mark_or_up(hist, when);
262 hist->changed = when;
265 /** Remember that we intentionally closed our connection to the OR
266 * with identity digest <b>id</b> at <b>when</b>.
268 void
269 rep_hist_note_disconnect(const char* id, time_t when)
271 or_history_t *hist;
272 hist = get_or_history(id);
273 if (!hist)
274 return;
275 mark_or_down(hist, when, 0);
276 hist->changed = when;
279 /** Remember that our connection to the OR with identity digest
280 * <b>id</b> had an error and stopped working at <b>when</b>.
282 void
283 rep_hist_note_connection_died(const char* id, time_t when)
285 or_history_t *hist;
286 if (!id) {
287 /* If conn has no identity, it didn't complete its handshake, or something
288 * went wrong. Ignore it.
290 return;
292 hist = get_or_history(id);
293 if (!hist)
294 return;
295 mark_or_down(hist, when, 1);
296 hist->changed = when;
299 /** We have just decided that this router with identity digest <b>id</b> is
300 * reachable, meaning we will give it a "Running" flag for the next while. */
301 void
302 rep_hist_note_router_reachable(const char *id, const tor_addr_t *at_addr,
303 const uint16_t at_port, time_t when)
305 or_history_t *hist = get_or_history(id);
306 int was_in_run = 1;
307 char tbuf[ISO_TIME_LEN+1];
308 int addr_changed, port_changed;
310 tor_assert(hist);
311 tor_assert((!at_addr && !at_port) || (at_addr && at_port));
313 addr_changed = at_addr && !tor_addr_is_null(&hist->last_reached_addr) &&
314 tor_addr_compare(at_addr, &hist->last_reached_addr, CMP_EXACT) != 0;
315 port_changed = at_port && hist->last_reached_port &&
316 at_port != hist->last_reached_port;
318 if (!started_tracking_stability)
319 started_tracking_stability = time(NULL);
320 if (!hist->start_of_run) {
321 hist->start_of_run = when;
322 was_in_run = 0;
324 if (hist->start_of_downtime) {
325 long down_length;
327 format_local_iso_time(tbuf, hist->start_of_downtime);
328 log_info(LD_HIST, "Router %s is now Running; it had been down since %s.",
329 hex_str(id, DIGEST_LEN), tbuf);
330 if (was_in_run)
331 log_info(LD_HIST, " (Paradoxically, it was already Running too.)");
333 down_length = when - hist->start_of_downtime;
334 hist->total_weighted_time += down_length;
335 hist->start_of_downtime = 0;
336 } else if (addr_changed || port_changed) {
337 /* If we're reachable, but the address changed, treat this as some
338 * downtime. */
339 int penalty = get_options()->TestingTorNetwork ? 240 : 3600;
340 networkstatus_t *ns;
342 if ((ns = networkstatus_get_latest_consensus())) {
343 int fresh_interval = (int)(ns->fresh_until - ns->valid_after);
344 int live_interval = (int)(ns->valid_until - ns->valid_after);
345 /* on average, a descriptor addr change takes .5 intervals to make it
346 * into a consensus, and half a liveness period to make it to
347 * clients. */
348 penalty = (int)(fresh_interval + live_interval) / 2;
350 format_local_iso_time(tbuf, hist->start_of_run);
351 log_info(LD_HIST,"Router %s still seems Running, but its address appears "
352 "to have changed since the last time it was reachable. I'm "
353 "going to treat it as having been down for %d seconds",
354 hex_str(id, DIGEST_LEN), penalty);
355 rep_hist_note_router_unreachable(id, when-penalty);
356 rep_hist_note_router_reachable(id, NULL, 0, when);
357 } else {
358 format_local_iso_time(tbuf, hist->start_of_run);
359 if (was_in_run)
360 log_debug(LD_HIST, "Router %s is still Running; it has been Running "
361 "since %s", hex_str(id, DIGEST_LEN), tbuf);
362 else
363 log_info(LD_HIST,"Router %s is now Running; it was previously untracked",
364 hex_str(id, DIGEST_LEN));
366 if (at_addr)
367 tor_addr_copy(&hist->last_reached_addr, at_addr);
368 if (at_port)
369 hist->last_reached_port = at_port;
372 /** We have just decided that this router is unreachable, meaning
373 * we are taking away its "Running" flag. */
374 void
375 rep_hist_note_router_unreachable(const char *id, time_t when)
377 or_history_t *hist = get_or_history(id);
378 char tbuf[ISO_TIME_LEN+1];
379 int was_running = 0;
380 if (!started_tracking_stability)
381 started_tracking_stability = time(NULL);
383 tor_assert(hist);
384 if (hist->start_of_run) {
385 /*XXXX We could treat failed connections differently from failed
386 * connect attempts. */
387 long run_length = when - hist->start_of_run;
388 format_local_iso_time(tbuf, hist->start_of_run);
390 hist->total_run_weights += 1.0;
391 hist->start_of_run = 0;
392 if (run_length < 0) {
393 unsigned long penalty = -run_length;
394 #define SUBTRACT_CLAMPED(var, penalty) \
395 do { (var) = (var) < (penalty) ? 0 : (var) - (penalty); } while (0)
397 SUBTRACT_CLAMPED(hist->weighted_run_length, penalty);
398 SUBTRACT_CLAMPED(hist->weighted_uptime, penalty);
399 } else {
400 hist->weighted_run_length += run_length;
401 hist->weighted_uptime += run_length;
402 hist->total_weighted_time += run_length;
404 was_running = 1;
405 log_info(LD_HIST, "Router %s is now non-Running: it had previously been "
406 "Running since %s. Its total weighted uptime is %lu/%lu.",
407 hex_str(id, DIGEST_LEN), tbuf, hist->weighted_uptime,
408 hist->total_weighted_time);
410 if (!hist->start_of_downtime) {
411 hist->start_of_downtime = when;
413 if (!was_running)
414 log_info(LD_HIST, "Router %s is now non-Running; it was previously "
415 "untracked.", hex_str(id, DIGEST_LEN));
416 } else {
417 if (!was_running) {
418 format_local_iso_time(tbuf, hist->start_of_downtime);
420 log_info(LD_HIST, "Router %s is still non-Running; it has been "
421 "non-Running since %s.", hex_str(id, DIGEST_LEN), tbuf);
426 /** Mark a router with ID <b>id</b> as non-Running, and retroactively declare
427 * that it has never been running: give it no stability and no WFU. */
428 void
429 rep_hist_make_router_pessimal(const char *id, time_t when)
431 or_history_t *hist = get_or_history(id);
432 tor_assert(hist);
434 rep_hist_note_router_unreachable(id, when);
435 mark_or_down(hist, when, 1);
437 hist->weighted_run_length = 0;
438 hist->weighted_uptime = 0;
441 /** Helper: Discount all old MTBF data, if it is time to do so. Return
442 * the time at which we should next discount MTBF data. */
443 time_t
444 rep_hist_downrate_old_runs(time_t now)
446 digestmap_iter_t *orhist_it;
447 const char *digest1;
448 or_history_t *hist;
449 void *hist_p;
450 double alpha = 1.0;
452 if (!history_map)
453 history_map = digestmap_new();
454 if (!stability_last_downrated)
455 stability_last_downrated = now;
456 if (stability_last_downrated + STABILITY_INTERVAL > now)
457 return stability_last_downrated + STABILITY_INTERVAL;
459 /* Okay, we should downrate the data. By how much? */
460 while (stability_last_downrated + STABILITY_INTERVAL < now) {
461 stability_last_downrated += STABILITY_INTERVAL;
462 alpha *= STABILITY_ALPHA;
465 log_info(LD_HIST, "Discounting all old stability info by a factor of %f",
466 alpha);
468 /* Multiply every w_r_l, t_r_w pair by alpha. */
469 for (orhist_it = digestmap_iter_init(history_map);
470 !digestmap_iter_done(orhist_it);
471 orhist_it = digestmap_iter_next(history_map,orhist_it)) {
472 digestmap_iter_get(orhist_it, &digest1, &hist_p);
473 hist = hist_p;
475 hist->weighted_run_length =
476 (unsigned long)(hist->weighted_run_length * alpha);
477 hist->total_run_weights *= alpha;
479 hist->weighted_uptime = (unsigned long)(hist->weighted_uptime * alpha);
480 hist->total_weighted_time = (unsigned long)
481 (hist->total_weighted_time * alpha);
484 return stability_last_downrated + STABILITY_INTERVAL;
487 /** Helper: Return the weighted MTBF of the router with history <b>hist</b>. */
488 static double
489 get_stability(or_history_t *hist, time_t when)
491 long total = hist->weighted_run_length;
492 double total_weights = hist->total_run_weights;
494 if (hist->start_of_run) {
495 /* We're currently in a run. Let total and total_weights hold the values
496 * they would hold if the current run were to end now. */
497 total += (when-hist->start_of_run);
498 total_weights += 1.0;
500 if (total_weights < STABILITY_EPSILON) {
501 /* Round down to zero, and avoid divide-by-zero. */
502 return 0.0;
505 return total / total_weights;
508 /** Return the total amount of time we've been observing, with each run of
509 * time downrated by the appropriate factor. */
510 static long
511 get_total_weighted_time(or_history_t *hist, time_t when)
513 long total = hist->total_weighted_time;
514 if (hist->start_of_run) {
515 total += (when - hist->start_of_run);
516 } else if (hist->start_of_downtime) {
517 total += (when - hist->start_of_downtime);
519 return total;
522 /** Helper: Return the weighted percent-of-time-online of the router with
523 * history <b>hist</b>. */
524 static double
525 get_weighted_fractional_uptime(or_history_t *hist, time_t when)
527 long total = hist->total_weighted_time;
528 long up = hist->weighted_uptime;
530 if (hist->start_of_run) {
531 long run_length = (when - hist->start_of_run);
532 up += run_length;
533 total += run_length;
534 } else if (hist->start_of_downtime) {
535 total += (when - hist->start_of_downtime);
538 if (!total) {
539 /* Avoid calling anybody's uptime infinity (which should be impossible if
540 * the code is working), or NaN (which can happen for any router we haven't
541 * observed up or down yet). */
542 return 0.0;
545 return ((double) up) / total;
548 /** Return how long the router whose identity digest is <b>id</b> has
549 * been reachable. Return 0 if the router is unknown or currently deemed
550 * unreachable. */
551 long
552 rep_hist_get_uptime(const char *id, time_t when)
554 or_history_t *hist = get_or_history(id);
555 if (!hist)
556 return 0;
557 if (!hist->start_of_run || when < hist->start_of_run)
558 return 0;
559 return when - hist->start_of_run;
562 /** Return an estimated MTBF for the router whose identity digest is
563 * <b>id</b>. Return 0 if the router is unknown. */
564 double
565 rep_hist_get_stability(const char *id, time_t when)
567 or_history_t *hist = get_or_history(id);
568 if (!hist)
569 return 0.0;
571 return get_stability(hist, when);
574 /** Return an estimated percent-of-time-online for the router whose identity
575 * digest is <b>id</b>. Return 0 if the router is unknown. */
576 double
577 rep_hist_get_weighted_fractional_uptime(const char *id, time_t when)
579 or_history_t *hist = get_or_history(id);
580 if (!hist)
581 return 0.0;
583 return get_weighted_fractional_uptime(hist, when);
586 /** Return a number representing how long we've known about the router whose
587 * digest is <b>id</b>. Return 0 if the router is unknown.
589 * Be careful: this measure increases monotonically as we know the router for
590 * longer and longer, but it doesn't increase linearly.
592 long
593 rep_hist_get_weighted_time_known(const char *id, time_t when)
595 or_history_t *hist = get_or_history(id);
596 if (!hist)
597 return 0;
599 return get_total_weighted_time(hist, when);
602 /** Return true if we've been measuring MTBFs for long enough to
603 * pronounce on Stability. */
605 rep_hist_have_measured_enough_stability(void)
607 /* XXXX023 This doesn't do so well when we change our opinion
608 * as to whether we're tracking router stability. */
609 return started_tracking_stability < time(NULL) - 4*60*60;
612 /** Remember that we successfully extended from the OR with identity
613 * digest <b>from_id</b> to the OR with identity digest
614 * <b>to_name</b>.
616 void
617 rep_hist_note_extend_succeeded(const char *from_id, const char *to_id)
619 link_history_t *hist;
620 /* log_fn(LOG_WARN, "EXTEND SUCCEEDED: %s->%s",from_name,to_name); */
621 hist = get_link_history(from_id, to_id);
622 if (!hist)
623 return;
624 ++hist->n_extend_ok;
625 hist->changed = time(NULL);
628 /** Remember that we tried to extend from the OR with identity digest
629 * <b>from_id</b> to the OR with identity digest <b>to_name</b>, but
630 * failed.
632 void
633 rep_hist_note_extend_failed(const char *from_id, const char *to_id)
635 link_history_t *hist;
636 /* log_fn(LOG_WARN, "EXTEND FAILED: %s->%s",from_name,to_name); */
637 hist = get_link_history(from_id, to_id);
638 if (!hist)
639 return;
640 ++hist->n_extend_fail;
641 hist->changed = time(NULL);
644 /** Log all the reliability data we have remembered, with the chosen
645 * severity.
647 void
648 rep_hist_dump_stats(time_t now, int severity)
650 digestmap_iter_t *lhist_it;
651 digestmap_iter_t *orhist_it;
652 const char *name1, *name2, *digest1, *digest2;
653 char hexdigest1[HEX_DIGEST_LEN+1];
654 char hexdigest2[HEX_DIGEST_LEN+1];
655 or_history_t *or_history;
656 link_history_t *link_history;
657 void *or_history_p, *link_history_p;
658 double uptime;
659 char buffer[2048];
660 size_t len;
661 int ret;
662 unsigned long upt, downt;
663 const node_t *node;
665 rep_history_clean(now - get_options()->RephistTrackTime);
667 tor_log(severity, LD_HIST, "--------------- Dumping history information:");
669 for (orhist_it = digestmap_iter_init(history_map);
670 !digestmap_iter_done(orhist_it);
671 orhist_it = digestmap_iter_next(history_map,orhist_it)) {
672 double s;
673 long stability;
674 digestmap_iter_get(orhist_it, &digest1, &or_history_p);
675 or_history = (or_history_t*) or_history_p;
677 if ((node = node_get_by_id(digest1)) && node_get_nickname(node))
678 name1 = node_get_nickname(node);
679 else
680 name1 = "(unknown)";
681 base16_encode(hexdigest1, sizeof(hexdigest1), digest1, DIGEST_LEN);
682 update_or_history(or_history, now);
683 upt = or_history->uptime;
684 downt = or_history->downtime;
685 s = get_stability(or_history, now);
686 stability = (long)s;
687 if (upt+downt) {
688 uptime = ((double)upt) / (upt+downt);
689 } else {
690 uptime=1.0;
692 tor_log(severity, LD_HIST,
693 "OR %s [%s]: %ld/%ld good connections; uptime %ld/%ld sec (%.2f%%); "
694 "wmtbf %lu:%02lu:%02lu",
695 name1, hexdigest1,
696 or_history->n_conn_ok, or_history->n_conn_fail+or_history->n_conn_ok,
697 upt, upt+downt, uptime*100.0,
698 stability/3600, (stability/60)%60, stability%60);
700 if (!digestmap_isempty(or_history->link_history_map)) {
701 strlcpy(buffer, " Extend attempts: ", sizeof(buffer));
702 len = strlen(buffer);
703 for (lhist_it = digestmap_iter_init(or_history->link_history_map);
704 !digestmap_iter_done(lhist_it);
705 lhist_it = digestmap_iter_next(or_history->link_history_map,
706 lhist_it)) {
707 digestmap_iter_get(lhist_it, &digest2, &link_history_p);
708 if ((node = node_get_by_id(digest2)) && node_get_nickname(node))
709 name2 = node_get_nickname(node);
710 else
711 name2 = "(unknown)";
713 link_history = (link_history_t*) link_history_p;
715 base16_encode(hexdigest2, sizeof(hexdigest2), digest2, DIGEST_LEN);
716 ret = tor_snprintf(buffer+len, 2048-len, "%s [%s](%ld/%ld); ",
717 name2,
718 hexdigest2,
719 link_history->n_extend_ok,
720 link_history->n_extend_ok+link_history->n_extend_fail);
721 if (ret<0)
722 break;
723 else
724 len += ret;
726 tor_log(severity, LD_HIST, "%s", buffer);
731 /** Remove history info for routers/links that haven't changed since
732 * <b>before</b>.
734 void
735 rep_history_clean(time_t before)
737 int authority = authdir_mode(get_options());
738 or_history_t *or_history;
739 link_history_t *link_history;
740 void *or_history_p, *link_history_p;
741 digestmap_iter_t *orhist_it, *lhist_it;
742 const char *d1, *d2;
744 orhist_it = digestmap_iter_init(history_map);
745 while (!digestmap_iter_done(orhist_it)) {
746 int remove;
747 digestmap_iter_get(orhist_it, &d1, &or_history_p);
748 or_history = or_history_p;
750 remove = authority ? (or_history->total_run_weights < STABILITY_EPSILON &&
751 !or_history->start_of_run)
752 : (or_history->changed < before);
753 if (remove) {
754 orhist_it = digestmap_iter_next_rmv(history_map, orhist_it);
755 free_or_history(or_history);
756 continue;
758 for (lhist_it = digestmap_iter_init(or_history->link_history_map);
759 !digestmap_iter_done(lhist_it); ) {
760 digestmap_iter_get(lhist_it, &d2, &link_history_p);
761 link_history = link_history_p;
762 if (link_history->changed < before) {
763 lhist_it = digestmap_iter_next_rmv(or_history->link_history_map,
764 lhist_it);
765 rephist_total_alloc -= sizeof(link_history_t);
766 tor_free(link_history);
767 continue;
769 lhist_it = digestmap_iter_next(or_history->link_history_map,lhist_it);
771 orhist_it = digestmap_iter_next(history_map, orhist_it);
775 /** Write MTBF data to disk. Return 0 on success, negative on failure.
777 * If <b>missing_means_down</b>, then if we're about to write an entry
778 * that is still considered up but isn't in our routerlist, consider it
779 * to be down. */
781 rep_hist_record_mtbf_data(time_t now, int missing_means_down)
783 char time_buf[ISO_TIME_LEN+1];
785 digestmap_iter_t *orhist_it;
786 const char *digest;
787 void *or_history_p;
788 or_history_t *hist;
789 open_file_t *open_file = NULL;
790 FILE *f;
793 char *filename = get_datadir_fname("router-stability");
794 f = start_writing_to_stdio_file(filename, OPEN_FLAGS_REPLACE|O_TEXT, 0600,
795 &open_file);
796 tor_free(filename);
797 if (!f)
798 return -1;
801 /* File format is:
802 * FormatLine *KeywordLine Data
804 * FormatLine = "format 1" NL
805 * KeywordLine = Keyword SP Arguments NL
806 * Data = "data" NL *RouterMTBFLine "." NL
807 * RouterMTBFLine = Fingerprint SP WeightedRunLen SP
808 * TotalRunWeights [SP S=StartRunTime] NL
810 #define PUT(s) STMT_BEGIN if (fputs((s),f)<0) goto err; STMT_END
811 #define PRINTF(args) STMT_BEGIN if (fprintf args <0) goto err; STMT_END
813 PUT("format 2\n");
815 format_iso_time(time_buf, time(NULL));
816 PRINTF((f, "stored-at %s\n", time_buf));
818 if (started_tracking_stability) {
819 format_iso_time(time_buf, started_tracking_stability);
820 PRINTF((f, "tracked-since %s\n", time_buf));
822 if (stability_last_downrated) {
823 format_iso_time(time_buf, stability_last_downrated);
824 PRINTF((f, "last-downrated %s\n", time_buf));
827 PUT("data\n");
829 /* XXX Nick: now bridge auths record this for all routers too.
830 * Should we make them record it only for bridge routers? -RD
831 * Not for 0.2.0. -NM */
832 for (orhist_it = digestmap_iter_init(history_map);
833 !digestmap_iter_done(orhist_it);
834 orhist_it = digestmap_iter_next(history_map,orhist_it)) {
835 char dbuf[HEX_DIGEST_LEN+1];
836 const char *t = NULL;
837 digestmap_iter_get(orhist_it, &digest, &or_history_p);
838 hist = (or_history_t*) or_history_p;
840 base16_encode(dbuf, sizeof(dbuf), digest, DIGEST_LEN);
842 if (missing_means_down && hist->start_of_run &&
843 !router_get_by_id_digest(digest)) {
844 /* We think this relay is running, but it's not listed in our
845 * routerlist. Somehow it fell out without telling us it went
846 * down. Complain and also correct it. */
847 log_info(LD_HIST,
848 "Relay '%s' is listed as up in rephist, but it's not in "
849 "our routerlist. Correcting.", dbuf);
850 rep_hist_note_router_unreachable(digest, now);
853 PRINTF((f, "R %s\n", dbuf));
854 if (hist->start_of_run > 0) {
855 format_iso_time(time_buf, hist->start_of_run);
856 t = time_buf;
858 PRINTF((f, "+MTBF %lu %.5f%s%s\n",
859 hist->weighted_run_length, hist->total_run_weights,
860 t ? " S=" : "", t ? t : ""));
861 t = NULL;
862 if (hist->start_of_downtime > 0) {
863 format_iso_time(time_buf, hist->start_of_downtime);
864 t = time_buf;
866 PRINTF((f, "+WFU %lu %lu%s%s\n",
867 hist->weighted_uptime, hist->total_weighted_time,
868 t ? " S=" : "", t ? t : ""));
871 PUT(".\n");
873 #undef PUT
874 #undef PRINTF
876 return finish_writing_to_file(open_file);
877 err:
878 abort_writing_to_file(open_file);
879 return -1;
882 /** Helper: return the first j >= i such that !strcmpstart(sl[j], prefix) and
883 * such that no line sl[k] with i <= k < j starts with "R ". Return -1 if no
884 * such line exists. */
885 static int
886 find_next_with(smartlist_t *sl, int i, const char *prefix)
888 for ( ; i < smartlist_len(sl); ++i) {
889 const char *line = smartlist_get(sl, i);
890 if (!strcmpstart(line, prefix))
891 return i;
892 if (!strcmpstart(line, "R "))
893 return -1;
895 return -1;
898 /** How many bad times has parse_possibly_bad_iso_time() parsed? */
899 static int n_bogus_times = 0;
900 /** Parse the ISO-formatted time in <b>s</b> into *<b>time_out</b>, but
901 * round any pre-1970 date to Jan 1, 1970. */
902 static int
903 parse_possibly_bad_iso_time(const char *s, time_t *time_out)
905 int year;
906 char b[5];
907 strlcpy(b, s, sizeof(b));
908 b[4] = '\0';
909 year = (int)tor_parse_long(b, 10, 0, INT_MAX, NULL, NULL);
910 if (year < 1970) {
911 *time_out = 0;
912 ++n_bogus_times;
913 return 0;
914 } else
915 return parse_iso_time(s, time_out);
918 /** We've read a time <b>t</b> from a file stored at <b>stored_at</b>, which
919 * says we started measuring at <b>started_measuring</b>. Return a new number
920 * that's about as much before <b>now</b> as <b>t</b> was before
921 * <b>stored_at</b>.
923 static INLINE time_t
924 correct_time(time_t t, time_t now, time_t stored_at, time_t started_measuring)
926 if (t < started_measuring - 24*60*60*365)
927 return 0;
928 else if (t < started_measuring)
929 return started_measuring;
930 else if (t > stored_at)
931 return 0;
932 else {
933 long run_length = stored_at - t;
934 t = (time_t)(now - run_length);
935 if (t < started_measuring)
936 t = started_measuring;
937 return t;
941 /** Load MTBF data from disk. Returns 0 on success or recoverable error, -1
942 * on failure. */
944 rep_hist_load_mtbf_data(time_t now)
946 /* XXXX won't handle being called while history is already populated. */
947 smartlist_t *lines;
948 const char *line = NULL;
949 int r=0, i;
950 time_t last_downrated = 0, stored_at = 0, tracked_since = 0;
951 time_t latest_possible_start = now;
952 long format = -1;
955 char *filename = get_datadir_fname("router-stability");
956 char *d = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
957 tor_free(filename);
958 if (!d)
959 return -1;
960 lines = smartlist_new();
961 smartlist_split_string(lines, d, "\n", SPLIT_SKIP_SPACE, 0);
962 tor_free(d);
966 const char *firstline;
967 if (smartlist_len(lines)>4) {
968 firstline = smartlist_get(lines, 0);
969 if (!strcmpstart(firstline, "format "))
970 format = tor_parse_long(firstline+strlen("format "),
971 10, -1, LONG_MAX, NULL, NULL);
974 if (format != 1 && format != 2) {
975 log_warn(LD_HIST,
976 "Unrecognized format in mtbf history file. Skipping.");
977 goto err;
979 for (i = 1; i < smartlist_len(lines); ++i) {
980 line = smartlist_get(lines, i);
981 if (!strcmp(line, "data"))
982 break;
983 if (!strcmpstart(line, "last-downrated ")) {
984 if (parse_iso_time(line+strlen("last-downrated "), &last_downrated)<0)
985 log_warn(LD_HIST,"Couldn't parse downrate time in mtbf "
986 "history file.");
988 if (!strcmpstart(line, "stored-at ")) {
989 if (parse_iso_time(line+strlen("stored-at "), &stored_at)<0)
990 log_warn(LD_HIST,"Couldn't parse stored time in mtbf "
991 "history file.");
993 if (!strcmpstart(line, "tracked-since ")) {
994 if (parse_iso_time(line+strlen("tracked-since "), &tracked_since)<0)
995 log_warn(LD_HIST,"Couldn't parse started-tracking time in mtbf "
996 "history file.");
999 if (last_downrated > now)
1000 last_downrated = now;
1001 if (tracked_since > now)
1002 tracked_since = now;
1004 if (!stored_at) {
1005 log_warn(LD_HIST, "No stored time recorded.");
1006 goto err;
1009 if (line && !strcmp(line, "data"))
1010 ++i;
1012 n_bogus_times = 0;
1014 for (; i < smartlist_len(lines); ++i) {
1015 char digest[DIGEST_LEN];
1016 char hexbuf[HEX_DIGEST_LEN+1];
1017 char mtbf_timebuf[ISO_TIME_LEN+1];
1018 char wfu_timebuf[ISO_TIME_LEN+1];
1019 time_t start_of_run = 0;
1020 time_t start_of_downtime = 0;
1021 int have_mtbf = 0, have_wfu = 0;
1022 long wrl = 0;
1023 double trw = 0;
1024 long wt_uptime = 0, total_wt_time = 0;
1025 int n;
1026 or_history_t *hist;
1027 line = smartlist_get(lines, i);
1028 if (!strcmp(line, "."))
1029 break;
1031 mtbf_timebuf[0] = '\0';
1032 wfu_timebuf[0] = '\0';
1034 if (format == 1) {
1035 n = tor_sscanf(line, "%40s %ld %lf S=%10s %8s",
1036 hexbuf, &wrl, &trw, mtbf_timebuf, mtbf_timebuf+11);
1037 if (n != 3 && n != 5) {
1038 log_warn(LD_HIST, "Couldn't scan line %s", escaped(line));
1039 continue;
1041 have_mtbf = 1;
1042 } else {
1043 // format == 2.
1044 int mtbf_idx, wfu_idx;
1045 if (strcmpstart(line, "R ") || strlen(line) < 2+HEX_DIGEST_LEN)
1046 continue;
1047 strlcpy(hexbuf, line+2, sizeof(hexbuf));
1048 mtbf_idx = find_next_with(lines, i+1, "+MTBF ");
1049 wfu_idx = find_next_with(lines, i+1, "+WFU ");
1050 if (mtbf_idx >= 0) {
1051 const char *mtbfline = smartlist_get(lines, mtbf_idx);
1052 n = tor_sscanf(mtbfline, "+MTBF %lu %lf S=%10s %8s",
1053 &wrl, &trw, mtbf_timebuf, mtbf_timebuf+11);
1054 if (n == 2 || n == 4) {
1055 have_mtbf = 1;
1056 } else {
1057 log_warn(LD_HIST, "Couldn't scan +MTBF line %s",
1058 escaped(mtbfline));
1061 if (wfu_idx >= 0) {
1062 const char *wfuline = smartlist_get(lines, wfu_idx);
1063 n = tor_sscanf(wfuline, "+WFU %lu %lu S=%10s %8s",
1064 &wt_uptime, &total_wt_time,
1065 wfu_timebuf, wfu_timebuf+11);
1066 if (n == 2 || n == 4) {
1067 have_wfu = 1;
1068 } else {
1069 log_warn(LD_HIST, "Couldn't scan +WFU line %s", escaped(wfuline));
1072 if (wfu_idx > i)
1073 i = wfu_idx;
1074 if (mtbf_idx > i)
1075 i = mtbf_idx;
1077 if (base16_decode(digest, DIGEST_LEN, hexbuf, HEX_DIGEST_LEN) < 0) {
1078 log_warn(LD_HIST, "Couldn't hex string %s", escaped(hexbuf));
1079 continue;
1081 hist = get_or_history(digest);
1082 if (!hist)
1083 continue;
1085 if (have_mtbf) {
1086 if (mtbf_timebuf[0]) {
1087 mtbf_timebuf[10] = ' ';
1088 if (parse_possibly_bad_iso_time(mtbf_timebuf, &start_of_run)<0)
1089 log_warn(LD_HIST, "Couldn't parse time %s",
1090 escaped(mtbf_timebuf));
1092 hist->start_of_run = correct_time(start_of_run, now, stored_at,
1093 tracked_since);
1094 if (hist->start_of_run < latest_possible_start + wrl)
1095 latest_possible_start = (time_t)(hist->start_of_run - wrl);
1097 hist->weighted_run_length = wrl;
1098 hist->total_run_weights = trw;
1100 if (have_wfu) {
1101 if (wfu_timebuf[0]) {
1102 wfu_timebuf[10] = ' ';
1103 if (parse_possibly_bad_iso_time(wfu_timebuf, &start_of_downtime)<0)
1104 log_warn(LD_HIST, "Couldn't parse time %s", escaped(wfu_timebuf));
1107 hist->start_of_downtime = correct_time(start_of_downtime, now, stored_at,
1108 tracked_since);
1109 hist->weighted_uptime = wt_uptime;
1110 hist->total_weighted_time = total_wt_time;
1112 if (strcmp(line, "."))
1113 log_warn(LD_HIST, "Truncated MTBF file.");
1115 if (tracked_since < 86400*365) /* Recover from insanely early value. */
1116 tracked_since = latest_possible_start;
1118 stability_last_downrated = last_downrated;
1119 started_tracking_stability = tracked_since;
1121 goto done;
1122 err:
1123 r = -1;
1124 done:
1125 SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
1126 smartlist_free(lines);
1127 return r;
1130 /** For how many seconds do we keep track of individual per-second bandwidth
1131 * totals? */
1132 #define NUM_SECS_ROLLING_MEASURE 10
1133 /** How large are the intervals for which we track and report bandwidth use? */
1134 #define NUM_SECS_BW_SUM_INTERVAL (4*60*60)
1135 /** How far in the past do we remember and publish bandwidth use? */
1136 #define NUM_SECS_BW_SUM_IS_VALID (24*60*60)
1137 /** How many bandwidth usage intervals do we remember? (derived) */
1138 #define NUM_TOTALS (NUM_SECS_BW_SUM_IS_VALID/NUM_SECS_BW_SUM_INTERVAL)
1140 /** Structure to track bandwidth use, and remember the maxima for a given
1141 * time period.
1143 typedef struct bw_array_t {
1144 /** Observation array: Total number of bytes transferred in each of the last
1145 * NUM_SECS_ROLLING_MEASURE seconds. This is used as a circular array. */
1146 uint64_t obs[NUM_SECS_ROLLING_MEASURE];
1147 int cur_obs_idx; /**< Current position in obs. */
1148 time_t cur_obs_time; /**< Time represented in obs[cur_obs_idx] */
1149 uint64_t total_obs; /**< Total for all members of obs except
1150 * obs[cur_obs_idx] */
1151 uint64_t max_total; /**< Largest value that total_obs has taken on in the
1152 * current period. */
1153 uint64_t total_in_period; /**< Total bytes transferred in the current
1154 * period. */
1156 /** When does the next period begin? */
1157 time_t next_period;
1158 /** Where in 'maxima' should the maximum bandwidth usage for the current
1159 * period be stored? */
1160 int next_max_idx;
1161 /** How many values in maxima/totals have been set ever? */
1162 int num_maxes_set;
1163 /** Circular array of the maximum
1164 * bandwidth-per-NUM_SECS_ROLLING_MEASURE usage for the last
1165 * NUM_TOTALS periods */
1166 uint64_t maxima[NUM_TOTALS];
1167 /** Circular array of the total bandwidth usage for the last NUM_TOTALS
1168 * periods */
1169 uint64_t totals[NUM_TOTALS];
1170 } bw_array_t;
1172 /** Shift the current period of b forward by one. */
1173 static void
1174 commit_max(bw_array_t *b)
1176 /* Store total from current period. */
1177 b->totals[b->next_max_idx] = b->total_in_period;
1178 /* Store maximum from current period. */
1179 b->maxima[b->next_max_idx++] = b->max_total;
1180 /* Advance next_period and next_max_idx */
1181 b->next_period += NUM_SECS_BW_SUM_INTERVAL;
1182 if (b->next_max_idx == NUM_TOTALS)
1183 b->next_max_idx = 0;
1184 if (b->num_maxes_set < NUM_TOTALS)
1185 ++b->num_maxes_set;
1186 /* Reset max_total. */
1187 b->max_total = 0;
1188 /* Reset total_in_period. */
1189 b->total_in_period = 0;
1192 /** Shift the current observation time of <b>b</b> forward by one second. */
1193 static INLINE void
1194 advance_obs(bw_array_t *b)
1196 int nextidx;
1197 uint64_t total;
1199 /* Calculate the total bandwidth for the last NUM_SECS_ROLLING_MEASURE
1200 * seconds; adjust max_total as needed.*/
1201 total = b->total_obs + b->obs[b->cur_obs_idx];
1202 if (total > b->max_total)
1203 b->max_total = total;
1205 nextidx = b->cur_obs_idx+1;
1206 if (nextidx == NUM_SECS_ROLLING_MEASURE)
1207 nextidx = 0;
1209 b->total_obs = total - b->obs[nextidx];
1210 b->obs[nextidx]=0;
1211 b->cur_obs_idx = nextidx;
1213 if (++b->cur_obs_time >= b->next_period)
1214 commit_max(b);
1217 /** Add <b>n</b> bytes to the number of bytes in <b>b</b> for second
1218 * <b>when</b>. */
1219 static INLINE void
1220 add_obs(bw_array_t *b, time_t when, uint64_t n)
1222 if (when < b->cur_obs_time)
1223 return; /* Don't record data in the past. */
1225 /* If we're currently adding observations for an earlier second than
1226 * 'when', advance b->cur_obs_time and b->cur_obs_idx by an
1227 * appropriate number of seconds, and do all the other housekeeping. */
1228 while (when > b->cur_obs_time) {
1229 /* Doing this one second at a time is potentially inefficient, if we start
1230 with a state file that is very old. Fortunately, it doesn't seem to
1231 show up in profiles, so we can just ignore it for now. */
1232 advance_obs(b);
1235 b->obs[b->cur_obs_idx] += n;
1236 b->total_in_period += n;
1239 /** Allocate, initialize, and return a new bw_array. */
1240 static bw_array_t *
1241 bw_array_new(void)
1243 bw_array_t *b;
1244 time_t start;
1245 b = tor_malloc_zero(sizeof(bw_array_t));
1246 rephist_total_alloc += sizeof(bw_array_t);
1247 start = time(NULL);
1248 b->cur_obs_time = start;
1249 b->next_period = start + NUM_SECS_BW_SUM_INTERVAL;
1250 return b;
1253 /** Recent history of bandwidth observations for read operations. */
1254 static bw_array_t *read_array = NULL;
1255 /** Recent history of bandwidth observations for write operations. */
1256 static bw_array_t *write_array = NULL;
1257 /** Recent history of bandwidth observations for read operations for the
1258 directory protocol. */
1259 static bw_array_t *dir_read_array = NULL;
1260 /** Recent history of bandwidth observations for write operations for the
1261 directory protocol. */
1262 static bw_array_t *dir_write_array = NULL;
1264 /** Set up [dir-]read_array and [dir-]write_array, freeing them if they
1265 * already exist. */
1266 static void
1267 bw_arrays_init(void)
1269 tor_free(read_array);
1270 tor_free(write_array);
1271 tor_free(dir_read_array);
1272 tor_free(dir_write_array);
1273 read_array = bw_array_new();
1274 write_array = bw_array_new();
1275 dir_read_array = bw_array_new();
1276 dir_write_array = bw_array_new();
1279 /** Remember that we read <b>num_bytes</b> bytes in second <b>when</b>.
1281 * Add num_bytes to the current running total for <b>when</b>.
1283 * <b>when</b> can go back to time, but it's safe to ignore calls
1284 * earlier than the latest <b>when</b> you've heard of.
1286 void
1287 rep_hist_note_bytes_written(size_t num_bytes, time_t when)
1289 /* Maybe a circular array for recent seconds, and step to a new point
1290 * every time a new second shows up. Or simpler is to just to have
1291 * a normal array and push down each item every second; it's short.
1293 /* When a new second has rolled over, compute the sum of the bytes we've
1294 * seen over when-1 to when-1-NUM_SECS_ROLLING_MEASURE, and stick it
1295 * somewhere. See rep_hist_bandwidth_assess() below.
1297 add_obs(write_array, when, num_bytes);
1300 /** Remember that we wrote <b>num_bytes</b> bytes in second <b>when</b>.
1301 * (like rep_hist_note_bytes_written() above)
1303 void
1304 rep_hist_note_bytes_read(size_t num_bytes, time_t when)
1306 /* if we're smart, we can make this func and the one above share code */
1307 add_obs(read_array, when, num_bytes);
1310 /** Remember that we wrote <b>num_bytes</b> directory bytes in second
1311 * <b>when</b>. (like rep_hist_note_bytes_written() above)
1313 void
1314 rep_hist_note_dir_bytes_written(size_t num_bytes, time_t when)
1316 add_obs(dir_write_array, when, num_bytes);
1319 /** Remember that we read <b>num_bytes</b> directory bytes in second
1320 * <b>when</b>. (like rep_hist_note_bytes_written() above)
1322 void
1323 rep_hist_note_dir_bytes_read(size_t num_bytes, time_t when)
1325 add_obs(dir_read_array, when, num_bytes);
1328 /** Helper: Return the largest value in b->maxima. (This is equal to the
1329 * most bandwidth used in any NUM_SECS_ROLLING_MEASURE period for the last
1330 * NUM_SECS_BW_SUM_IS_VALID seconds.)
1332 static uint64_t
1333 find_largest_max(bw_array_t *b)
1335 int i;
1336 uint64_t max;
1337 max=0;
1338 for (i=0; i<NUM_TOTALS; ++i) {
1339 if (b->maxima[i]>max)
1340 max = b->maxima[i];
1342 return max;
1345 /** Find the largest sums in the past NUM_SECS_BW_SUM_IS_VALID (roughly)
1346 * seconds. Find one sum for reading and one for writing. They don't have
1347 * to be at the same time.
1349 * Return the smaller of these sums, divided by NUM_SECS_ROLLING_MEASURE.
1352 rep_hist_bandwidth_assess(void)
1354 uint64_t w,r;
1355 r = find_largest_max(read_array);
1356 w = find_largest_max(write_array);
1357 if (r>w)
1358 return (int)(U64_TO_DBL(w)/NUM_SECS_ROLLING_MEASURE);
1359 else
1360 return (int)(U64_TO_DBL(r)/NUM_SECS_ROLLING_MEASURE);
1363 /** Print the bandwidth history of b (either [dir-]read_array or
1364 * [dir-]write_array) into the buffer pointed to by buf. The format is
1365 * simply comma separated numbers, from oldest to newest.
1367 * It returns the number of bytes written.
1369 static size_t
1370 rep_hist_fill_bandwidth_history(char *buf, size_t len, const bw_array_t *b)
1372 char *cp = buf;
1373 int i, n;
1374 const or_options_t *options = get_options();
1375 uint64_t cutoff;
1377 if (b->num_maxes_set <= b->next_max_idx) {
1378 /* We haven't been through the circular array yet; time starts at i=0.*/
1379 i = 0;
1380 } else {
1381 /* We've been around the array at least once. The next i to be
1382 overwritten is the oldest. */
1383 i = b->next_max_idx;
1386 if (options->RelayBandwidthRate) {
1387 /* We don't want to report that we used more bandwidth than the max we're
1388 * willing to relay; otherwise everybody will know how much traffic
1389 * we used ourself. */
1390 cutoff = options->RelayBandwidthRate * NUM_SECS_BW_SUM_INTERVAL;
1391 } else {
1392 cutoff = UINT64_MAX;
1395 for (n=0; n<b->num_maxes_set; ++n,++i) {
1396 uint64_t total;
1397 if (i >= NUM_TOTALS)
1398 i -= NUM_TOTALS;
1399 tor_assert(i < NUM_TOTALS);
1400 /* Round the bandwidth used down to the nearest 1k. */
1401 total = b->totals[i] & ~0x3ff;
1402 if (total > cutoff)
1403 total = cutoff;
1405 if (n==(b->num_maxes_set-1))
1406 tor_snprintf(cp, len-(cp-buf), U64_FORMAT, U64_PRINTF_ARG(total));
1407 else
1408 tor_snprintf(cp, len-(cp-buf), U64_FORMAT",", U64_PRINTF_ARG(total));
1409 cp += strlen(cp);
1411 return cp-buf;
1414 /** Allocate and return lines for representing this server's bandwidth
1415 * history in its descriptor. We publish these lines in our extra-info
1416 * descriptor.
1418 char *
1419 rep_hist_get_bandwidth_lines(void)
1421 char *buf, *cp;
1422 char t[ISO_TIME_LEN+1];
1423 int r;
1424 bw_array_t *b = NULL;
1425 const char *desc = NULL;
1426 size_t len;
1428 /* [dirreq-](read|write)-history yyyy-mm-dd HH:MM:SS (n s) n,n,n... */
1429 /* The n,n,n part above. Largest representation of a uint64_t is 20 chars
1430 * long, plus the comma. */
1431 #define MAX_HIST_VALUE_LEN (21*NUM_TOTALS)
1432 len = (67+MAX_HIST_VALUE_LEN)*4;
1433 buf = tor_malloc_zero(len);
1434 cp = buf;
1435 for (r=0;r<4;++r) {
1436 char tmp[MAX_HIST_VALUE_LEN];
1437 size_t slen;
1438 switch (r) {
1439 case 0:
1440 b = write_array;
1441 desc = "write-history";
1442 break;
1443 case 1:
1444 b = read_array;
1445 desc = "read-history";
1446 break;
1447 case 2:
1448 b = dir_write_array;
1449 desc = "dirreq-write-history";
1450 break;
1451 case 3:
1452 b = dir_read_array;
1453 desc = "dirreq-read-history";
1454 break;
1456 tor_assert(b);
1457 slen = rep_hist_fill_bandwidth_history(tmp, MAX_HIST_VALUE_LEN, b);
1458 /* If we don't have anything to write, skip to the next entry. */
1459 if (slen == 0)
1460 continue;
1461 format_iso_time(t, b->next_period-NUM_SECS_BW_SUM_INTERVAL);
1462 tor_snprintf(cp, len-(cp-buf), "%s %s (%d s) ",
1463 desc, t, NUM_SECS_BW_SUM_INTERVAL);
1464 cp += strlen(cp);
1465 strlcat(cp, tmp, len-(cp-buf));
1466 cp += slen;
1467 strlcat(cp, "\n", len-(cp-buf));
1468 ++cp;
1470 return buf;
1473 /** Write a single bw_array_t into the Values, Ends, Interval, and Maximum
1474 * entries of an or_state_t. Done before writing out a new state file. */
1475 static void
1476 rep_hist_update_bwhist_state_section(or_state_t *state,
1477 const bw_array_t *b,
1478 smartlist_t **s_values,
1479 smartlist_t **s_maxima,
1480 time_t *s_begins,
1481 int *s_interval)
1483 int i,j;
1484 uint64_t maxval;
1486 if (*s_values) {
1487 SMARTLIST_FOREACH(*s_values, char *, val, tor_free(val));
1488 smartlist_free(*s_values);
1490 if (*s_maxima) {
1491 SMARTLIST_FOREACH(*s_maxima, char *, val, tor_free(val));
1492 smartlist_free(*s_maxima);
1494 if (! server_mode(get_options())) {
1495 /* Clients don't need to store bandwidth history persistently;
1496 * force these values to the defaults. */
1497 /* FFFF we should pull the default out of config.c's state table,
1498 * so we don't have two defaults. */
1499 if (*s_begins != 0 || *s_interval != 900) {
1500 time_t now = time(NULL);
1501 time_t save_at = get_options()->AvoidDiskWrites ? now+3600 : now+600;
1502 or_state_mark_dirty(state, save_at);
1504 *s_begins = 0;
1505 *s_interval = 900;
1506 *s_values = smartlist_new();
1507 *s_maxima = smartlist_new();
1508 return;
1510 *s_begins = b->next_period;
1511 *s_interval = NUM_SECS_BW_SUM_INTERVAL;
1513 *s_values = smartlist_new();
1514 *s_maxima = smartlist_new();
1515 /* Set i to first position in circular array */
1516 i = (b->num_maxes_set <= b->next_max_idx) ? 0 : b->next_max_idx;
1517 for (j=0; j < b->num_maxes_set; ++j,++i) {
1518 if (i >= NUM_TOTALS)
1519 i = 0;
1520 smartlist_add_asprintf(*s_values, U64_FORMAT,
1521 U64_PRINTF_ARG(b->totals[i] & ~0x3ff));
1522 maxval = b->maxima[i] / NUM_SECS_ROLLING_MEASURE;
1523 smartlist_add_asprintf(*s_maxima, U64_FORMAT,
1524 U64_PRINTF_ARG(maxval & ~0x3ff));
1526 smartlist_add_asprintf(*s_values, U64_FORMAT,
1527 U64_PRINTF_ARG(b->total_in_period & ~0x3ff));
1528 maxval = b->max_total / NUM_SECS_ROLLING_MEASURE;
1529 smartlist_add_asprintf(*s_maxima, U64_FORMAT,
1530 U64_PRINTF_ARG(maxval & ~0x3ff));
1533 /** Update <b>state</b> with the newest bandwidth history. Done before
1534 * writing out a new state file. */
1535 void
1536 rep_hist_update_state(or_state_t *state)
1538 #define UPDATE(arrname,st) \
1539 rep_hist_update_bwhist_state_section(state,\
1540 (arrname),\
1541 &state->BWHistory ## st ## Values, \
1542 &state->BWHistory ## st ## Maxima, \
1543 &state->BWHistory ## st ## Ends, \
1544 &state->BWHistory ## st ## Interval)
1546 UPDATE(write_array, Write);
1547 UPDATE(read_array, Read);
1548 UPDATE(dir_write_array, DirWrite);
1549 UPDATE(dir_read_array, DirRead);
1551 if (server_mode(get_options())) {
1552 or_state_mark_dirty(state, time(NULL)+(2*3600));
1554 #undef UPDATE
1557 /** Load a single bw_array_t from its Values, Ends, Maxima, and Interval
1558 * entries in an or_state_t. Done while reading the state file. */
1559 static int
1560 rep_hist_load_bwhist_state_section(bw_array_t *b,
1561 const smartlist_t *s_values,
1562 const smartlist_t *s_maxima,
1563 const time_t s_begins,
1564 const int s_interval)
1566 time_t now = time(NULL);
1567 int retval = 0;
1568 time_t start;
1570 uint64_t v, mv;
1571 int i,ok,ok_m = 0;
1572 int have_maxima = s_maxima && s_values &&
1573 (smartlist_len(s_values) == smartlist_len(s_maxima));
1575 if (s_values && s_begins >= now - NUM_SECS_BW_SUM_INTERVAL*NUM_TOTALS) {
1576 start = s_begins - s_interval*(smartlist_len(s_values));
1577 if (start > now)
1578 return 0;
1579 b->cur_obs_time = start;
1580 b->next_period = start + NUM_SECS_BW_SUM_INTERVAL;
1581 SMARTLIST_FOREACH_BEGIN(s_values, const char *, cp) {
1582 const char *maxstr = NULL;
1583 v = tor_parse_uint64(cp, 10, 0, UINT64_MAX, &ok, NULL);
1584 if (have_maxima) {
1585 maxstr = smartlist_get(s_maxima, cp_sl_idx);
1586 mv = tor_parse_uint64(maxstr, 10, 0, UINT64_MAX, &ok_m, NULL);
1587 mv *= NUM_SECS_ROLLING_MEASURE;
1588 } else {
1589 /* No maxima known; guess average rate to be conservative. */
1590 mv = (v / s_interval) * NUM_SECS_ROLLING_MEASURE;
1592 if (!ok) {
1593 retval = -1;
1594 log_notice(LD_HIST, "Could not parse value '%s' into a number.'",cp);
1596 if (maxstr && !ok_m) {
1597 retval = -1;
1598 log_notice(LD_HIST, "Could not parse maximum '%s' into a number.'",
1599 maxstr);
1602 if (start < now) {
1603 time_t cur_start = start;
1604 time_t actual_interval_len = s_interval;
1605 uint64_t cur_val = 0;
1606 /* Calculate the average per second. This is the best we can do
1607 * because our state file doesn't have per-second resolution. */
1608 if (start + s_interval > now)
1609 actual_interval_len = now - start;
1610 cur_val = v / actual_interval_len;
1611 /* This is potentially inefficient, but since we don't do it very
1612 * often it should be ok. */
1613 while (cur_start < start + actual_interval_len) {
1614 add_obs(b, cur_start, cur_val);
1615 ++cur_start;
1617 b->max_total = mv;
1618 /* This will result in some fairly choppy history if s_interval
1619 * is not the same as NUM_SECS_BW_SUM_INTERVAL. XXXX */
1620 start += actual_interval_len;
1622 } SMARTLIST_FOREACH_END(cp);
1625 /* Clean up maxima and observed */
1626 for (i=0; i<NUM_SECS_ROLLING_MEASURE; ++i) {
1627 b->obs[i] = 0;
1629 b->total_obs = 0;
1631 return retval;
1634 /** Set bandwidth history from the state file we just loaded. */
1636 rep_hist_load_state(or_state_t *state, char **err)
1638 int all_ok = 1;
1640 /* Assert they already have been malloced */
1641 tor_assert(read_array && write_array);
1642 tor_assert(dir_read_array && dir_write_array);
1644 #define LOAD(arrname,st) \
1645 if (rep_hist_load_bwhist_state_section( \
1646 (arrname), \
1647 state->BWHistory ## st ## Values, \
1648 state->BWHistory ## st ## Maxima, \
1649 state->BWHistory ## st ## Ends, \
1650 state->BWHistory ## st ## Interval)<0) \
1651 all_ok = 0
1653 LOAD(write_array, Write);
1654 LOAD(read_array, Read);
1655 LOAD(dir_write_array, DirWrite);
1656 LOAD(dir_read_array, DirRead);
1658 #undef LOAD
1659 if (!all_ok) {
1660 *err = tor_strdup("Parsing of bandwidth history values failed");
1661 /* and create fresh arrays */
1662 bw_arrays_init();
1663 return -1;
1665 return 0;
1668 /*********************************************************************/
1670 /** A single predicted port: used to remember which ports we've made
1671 * connections to, so that we can try to keep making circuits that can handle
1672 * those ports. */
1673 typedef struct predicted_port_t {
1674 /** The port we connected to */
1675 uint16_t port;
1676 /** The time at which we last used it */
1677 time_t time;
1678 } predicted_port_t;
1680 /** A list of port numbers that have been used recently. */
1681 static smartlist_t *predicted_ports_list=NULL;
1683 /** We just got an application request for a connection with
1684 * port <b>port</b>. Remember it for the future, so we can keep
1685 * some circuits open that will exit to this port.
1687 static void
1688 add_predicted_port(time_t now, uint16_t port)
1690 predicted_port_t *pp = tor_malloc(sizeof(predicted_port_t));
1691 pp->port = port;
1692 pp->time = now;
1693 rephist_total_alloc += sizeof(*pp);
1694 smartlist_add(predicted_ports_list, pp);
1697 /** Initialize whatever memory and structs are needed for predicting
1698 * which ports will be used. Also seed it with port 80, so we'll build
1699 * circuits on start-up.
1701 static void
1702 predicted_ports_init(void)
1704 predicted_ports_list = smartlist_new();
1705 add_predicted_port(time(NULL), 80); /* add one to kickstart us */
1708 /** Free whatever memory is needed for predicting which ports will
1709 * be used.
1711 static void
1712 predicted_ports_free(void)
1714 rephist_total_alloc -=
1715 smartlist_len(predicted_ports_list)*sizeof(predicted_port_t);
1716 SMARTLIST_FOREACH(predicted_ports_list, predicted_port_t *,
1717 pp, tor_free(pp));
1718 smartlist_free(predicted_ports_list);
1721 /** Remember that <b>port</b> has been asked for as of time <b>now</b>.
1722 * This is used for predicting what sorts of streams we'll make in the
1723 * future and making exit circuits to anticipate that.
1725 void
1726 rep_hist_note_used_port(time_t now, uint16_t port)
1728 tor_assert(predicted_ports_list);
1730 if (!port) /* record nothing */
1731 return;
1733 SMARTLIST_FOREACH_BEGIN(predicted_ports_list, predicted_port_t *, pp) {
1734 if (pp->port == port) {
1735 pp->time = now;
1736 return;
1738 } SMARTLIST_FOREACH_END(pp);
1739 /* it's not there yet; we need to add it */
1740 add_predicted_port(now, port);
1743 /** Return a newly allocated pointer to a list of uint16_t * for ports that
1744 * are likely to be asked for in the near future.
1746 smartlist_t *
1747 rep_hist_get_predicted_ports(time_t now)
1749 int predicted_circs_relevance_time;
1750 smartlist_t *out = smartlist_new();
1751 tor_assert(predicted_ports_list);
1752 predicted_circs_relevance_time = get_options()->PredictedPortsRelevanceTime;
1754 /* clean out obsolete entries */
1755 SMARTLIST_FOREACH_BEGIN(predicted_ports_list, predicted_port_t *, pp) {
1756 if (pp->time + predicted_circs_relevance_time < now) {
1757 log_debug(LD_CIRC, "Expiring predicted port %d", pp->port);
1759 rephist_total_alloc -= sizeof(predicted_port_t);
1760 tor_free(pp);
1761 SMARTLIST_DEL_CURRENT(predicted_ports_list, pp);
1762 } else {
1763 smartlist_add(out, tor_memdup(&pp->port, sizeof(uint16_t)));
1765 } SMARTLIST_FOREACH_END(pp);
1766 return out;
1770 * Take a list of uint16_t *, and remove every port in the list from the
1771 * current list of predicted ports.
1773 void
1774 rep_hist_remove_predicted_ports(const smartlist_t *rmv_ports)
1776 /* Let's do this on O(N), not O(N^2). */
1777 bitarray_t *remove_ports = bitarray_init_zero(UINT16_MAX);
1778 SMARTLIST_FOREACH(rmv_ports, const uint16_t *, p,
1779 bitarray_set(remove_ports, *p));
1780 SMARTLIST_FOREACH_BEGIN(predicted_ports_list, predicted_port_t *, pp) {
1781 if (bitarray_is_set(remove_ports, pp->port)) {
1782 tor_free(pp);
1783 SMARTLIST_DEL_CURRENT(predicted_ports_list, pp);
1785 } SMARTLIST_FOREACH_END(pp);
1786 bitarray_free(remove_ports);
1789 /** The user asked us to do a resolve. Rather than keeping track of
1790 * timings and such of resolves, we fake it for now by treating
1791 * it the same way as a connection to port 80. This way we will continue
1792 * to have circuits lying around if the user only uses Tor for resolves.
1794 void
1795 rep_hist_note_used_resolve(time_t now)
1797 rep_hist_note_used_port(now, 80);
1800 /** The last time at which we needed an internal circ. */
1801 static time_t predicted_internal_time = 0;
1802 /** The last time we needed an internal circ with good uptime. */
1803 static time_t predicted_internal_uptime_time = 0;
1804 /** The last time we needed an internal circ with good capacity. */
1805 static time_t predicted_internal_capacity_time = 0;
1807 /** Remember that we used an internal circ at time <b>now</b>. */
1808 void
1809 rep_hist_note_used_internal(time_t now, int need_uptime, int need_capacity)
1811 predicted_internal_time = now;
1812 if (need_uptime)
1813 predicted_internal_uptime_time = now;
1814 if (need_capacity)
1815 predicted_internal_capacity_time = now;
1818 /** Return 1 if we've used an internal circ recently; else return 0. */
1820 rep_hist_get_predicted_internal(time_t now, int *need_uptime,
1821 int *need_capacity)
1823 int predicted_circs_relevance_time;
1824 predicted_circs_relevance_time = get_options()->PredictedPortsRelevanceTime;
1826 if (!predicted_internal_time) { /* initialize it */
1827 predicted_internal_time = now;
1828 predicted_internal_uptime_time = now;
1829 predicted_internal_capacity_time = now;
1831 if (predicted_internal_time + predicted_circs_relevance_time < now)
1832 return 0; /* too long ago */
1833 if (predicted_internal_uptime_time + predicted_circs_relevance_time >= now)
1834 *need_uptime = 1;
1835 // Always predict that we need capacity.
1836 *need_capacity = 1;
1837 return 1;
1840 /** Any ports used lately? These are pre-seeded if we just started
1841 * up or if we're running a hidden service. */
1843 any_predicted_circuits(time_t now)
1845 int predicted_circs_relevance_time;
1846 predicted_circs_relevance_time = get_options()->PredictedPortsRelevanceTime;
1848 return smartlist_len(predicted_ports_list) ||
1849 predicted_internal_time + predicted_circs_relevance_time >= now;
1852 /** Return 1 if we have no need for circuits currently, else return 0. */
1854 rep_hist_circbuilding_dormant(time_t now)
1856 if (any_predicted_circuits(now))
1857 return 0;
1859 /* see if we'll still need to build testing circuits */
1860 if (server_mode(get_options()) &&
1861 (!check_whether_orport_reachable() || !circuit_enough_testing_circs()))
1862 return 0;
1863 if (!check_whether_dirport_reachable())
1864 return 0;
1866 return 1;
1869 /** Structure to track how many times we've done each public key operation. */
1870 static struct {
1871 /** How many directory objects have we signed? */
1872 unsigned long n_signed_dir_objs;
1873 /** How many routerdescs have we signed? */
1874 unsigned long n_signed_routerdescs;
1875 /** How many directory objects have we verified? */
1876 unsigned long n_verified_dir_objs;
1877 /** How many routerdescs have we verified */
1878 unsigned long n_verified_routerdescs;
1879 /** How many onionskins have we encrypted to build circuits? */
1880 unsigned long n_onionskins_encrypted;
1881 /** How many onionskins have we decrypted to do circuit build requests? */
1882 unsigned long n_onionskins_decrypted;
1883 /** How many times have we done the TLS handshake as a client? */
1884 unsigned long n_tls_client_handshakes;
1885 /** How many times have we done the TLS handshake as a server? */
1886 unsigned long n_tls_server_handshakes;
1887 /** How many PK operations have we done as a hidden service client? */
1888 unsigned long n_rend_client_ops;
1889 /** How many PK operations have we done as a hidden service midpoint? */
1890 unsigned long n_rend_mid_ops;
1891 /** How many PK operations have we done as a hidden service provider? */
1892 unsigned long n_rend_server_ops;
1893 } pk_op_counts = {0,0,0,0,0,0,0,0,0,0,0};
1895 /** Increment the count of the number of times we've done <b>operation</b>. */
1896 void
1897 note_crypto_pk_op(pk_op_t operation)
1899 switch (operation)
1901 case SIGN_DIR:
1902 pk_op_counts.n_signed_dir_objs++;
1903 break;
1904 case SIGN_RTR:
1905 pk_op_counts.n_signed_routerdescs++;
1906 break;
1907 case VERIFY_DIR:
1908 pk_op_counts.n_verified_dir_objs++;
1909 break;
1910 case VERIFY_RTR:
1911 pk_op_counts.n_verified_routerdescs++;
1912 break;
1913 case ENC_ONIONSKIN:
1914 pk_op_counts.n_onionskins_encrypted++;
1915 break;
1916 case DEC_ONIONSKIN:
1917 pk_op_counts.n_onionskins_decrypted++;
1918 break;
1919 case TLS_HANDSHAKE_C:
1920 pk_op_counts.n_tls_client_handshakes++;
1921 break;
1922 case TLS_HANDSHAKE_S:
1923 pk_op_counts.n_tls_server_handshakes++;
1924 break;
1925 case REND_CLIENT:
1926 pk_op_counts.n_rend_client_ops++;
1927 break;
1928 case REND_MID:
1929 pk_op_counts.n_rend_mid_ops++;
1930 break;
1931 case REND_SERVER:
1932 pk_op_counts.n_rend_server_ops++;
1933 break;
1934 default:
1935 log_warn(LD_BUG, "Unknown pk operation %d", operation);
1939 /** Log the number of times we've done each public/private-key operation. */
1940 void
1941 dump_pk_ops(int severity)
1943 tor_log(severity, LD_HIST,
1944 "PK operations: %lu directory objects signed, "
1945 "%lu directory objects verified, "
1946 "%lu routerdescs signed, "
1947 "%lu routerdescs verified, "
1948 "%lu onionskins encrypted, "
1949 "%lu onionskins decrypted, "
1950 "%lu client-side TLS handshakes, "
1951 "%lu server-side TLS handshakes, "
1952 "%lu rendezvous client operations, "
1953 "%lu rendezvous middle operations, "
1954 "%lu rendezvous server operations.",
1955 pk_op_counts.n_signed_dir_objs,
1956 pk_op_counts.n_verified_dir_objs,
1957 pk_op_counts.n_signed_routerdescs,
1958 pk_op_counts.n_verified_routerdescs,
1959 pk_op_counts.n_onionskins_encrypted,
1960 pk_op_counts.n_onionskins_decrypted,
1961 pk_op_counts.n_tls_client_handshakes,
1962 pk_op_counts.n_tls_server_handshakes,
1963 pk_op_counts.n_rend_client_ops,
1964 pk_op_counts.n_rend_mid_ops,
1965 pk_op_counts.n_rend_server_ops);
1968 /*** Exit port statistics ***/
1970 /* Some constants */
1971 /** To what multiple should byte numbers be rounded up? */
1972 #define EXIT_STATS_ROUND_UP_BYTES 1024
1973 /** To what multiple should stream counts be rounded up? */
1974 #define EXIT_STATS_ROUND_UP_STREAMS 4
1975 /** Number of TCP ports */
1976 #define EXIT_STATS_NUM_PORTS 65536
1977 /** Top n ports that will be included in exit stats. */
1978 #define EXIT_STATS_TOP_N_PORTS 10
1980 /* The following data structures are arrays and no fancy smartlists or maps,
1981 * so that all write operations can be done in constant time. This comes at
1982 * the price of some memory (1.25 MB) and linear complexity when writing
1983 * stats for measuring relays. */
1984 /** Number of bytes read in current period by exit port */
1985 static uint64_t *exit_bytes_read = NULL;
1986 /** Number of bytes written in current period by exit port */
1987 static uint64_t *exit_bytes_written = NULL;
1988 /** Number of streams opened in current period by exit port */
1989 static uint32_t *exit_streams = NULL;
1991 /** Start time of exit stats or 0 if we're not collecting exit stats. */
1992 static time_t start_of_exit_stats_interval;
1994 /** Initialize exit port stats. */
1995 void
1996 rep_hist_exit_stats_init(time_t now)
1998 start_of_exit_stats_interval = now;
1999 exit_bytes_read = tor_calloc(EXIT_STATS_NUM_PORTS, sizeof(uint64_t));
2000 exit_bytes_written = tor_calloc(EXIT_STATS_NUM_PORTS, sizeof(uint64_t));
2001 exit_streams = tor_calloc(EXIT_STATS_NUM_PORTS, sizeof(uint32_t));
2004 /** Reset counters for exit port statistics. */
2005 void
2006 rep_hist_reset_exit_stats(time_t now)
2008 start_of_exit_stats_interval = now;
2009 memset(exit_bytes_read, 0, EXIT_STATS_NUM_PORTS * sizeof(uint64_t));
2010 memset(exit_bytes_written, 0, EXIT_STATS_NUM_PORTS * sizeof(uint64_t));
2011 memset(exit_streams, 0, EXIT_STATS_NUM_PORTS * sizeof(uint32_t));
2014 /** Stop collecting exit port stats in a way that we can re-start doing
2015 * so in rep_hist_exit_stats_init(). */
2016 void
2017 rep_hist_exit_stats_term(void)
2019 start_of_exit_stats_interval = 0;
2020 tor_free(exit_bytes_read);
2021 tor_free(exit_bytes_written);
2022 tor_free(exit_streams);
2025 /** Helper for qsort: compare two ints. Does not handle overflow properly,
2026 * but works fine for sorting an array of port numbers, which is what we use
2027 * it for. */
2028 static int
2029 compare_int_(const void *x, const void *y)
2031 return (*(int*)x - *(int*)y);
2034 /** Return a newly allocated string containing the exit port statistics
2035 * until <b>now</b>, or NULL if we're not collecting exit stats. Caller
2036 * must ensure start_of_exit_stats_interval is in the past. */
2037 char *
2038 rep_hist_format_exit_stats(time_t now)
2040 int i, j, top_elements = 0, cur_min_idx = 0, cur_port;
2041 uint64_t top_bytes[EXIT_STATS_TOP_N_PORTS];
2042 int top_ports[EXIT_STATS_TOP_N_PORTS];
2043 uint64_t cur_bytes = 0, other_read = 0, other_written = 0,
2044 total_read = 0, total_written = 0;
2045 uint32_t total_streams = 0, other_streams = 0;
2046 smartlist_t *written_strings, *read_strings, *streams_strings;
2047 char *written_string, *read_string, *streams_string;
2048 char t[ISO_TIME_LEN+1];
2049 char *result;
2051 if (!start_of_exit_stats_interval)
2052 return NULL; /* Not initialized. */
2054 tor_assert(now >= start_of_exit_stats_interval);
2056 /* Go through all ports to find the n ports that saw most written and
2057 * read bytes.
2059 * Invariant: at the end of the loop for iteration i,
2060 * total_read is the sum of all exit_bytes_read[0..i]
2061 * total_written is the sum of all exit_bytes_written[0..i]
2062 * total_stream is the sum of all exit_streams[0..i]
2064 * top_elements = MAX(EXIT_STATS_TOP_N_PORTS,
2065 * #{j | 0 <= j <= i && volume(i) > 0})
2067 * For all 0 <= j < top_elements,
2068 * top_bytes[j] > 0
2069 * 0 <= top_ports[j] <= 65535
2070 * top_bytes[j] = volume(top_ports[j])
2072 * There is no j in 0..i and k in 0..top_elements such that:
2073 * volume(j) > top_bytes[k] AND j is not in top_ports[0..top_elements]
2075 * There is no j!=cur_min_idx in 0..top_elements such that:
2076 * top_bytes[j] < top_bytes[cur_min_idx]
2078 * where volume(x) == exit_bytes_read[x]+exit_bytes_written[x]
2080 * Worst case: O(EXIT_STATS_NUM_PORTS * EXIT_STATS_TOP_N_PORTS)
2082 for (i = 1; i < EXIT_STATS_NUM_PORTS; i++) {
2083 total_read += exit_bytes_read[i];
2084 total_written += exit_bytes_written[i];
2085 total_streams += exit_streams[i];
2086 cur_bytes = exit_bytes_read[i] + exit_bytes_written[i];
2087 if (cur_bytes == 0) {
2088 continue;
2090 if (top_elements < EXIT_STATS_TOP_N_PORTS) {
2091 top_bytes[top_elements] = cur_bytes;
2092 top_ports[top_elements++] = i;
2093 } else if (cur_bytes > top_bytes[cur_min_idx]) {
2094 top_bytes[cur_min_idx] = cur_bytes;
2095 top_ports[cur_min_idx] = i;
2096 } else {
2097 continue;
2099 cur_min_idx = 0;
2100 for (j = 1; j < top_elements; j++) {
2101 if (top_bytes[j] < top_bytes[cur_min_idx]) {
2102 cur_min_idx = j;
2107 /* Add observations of top ports to smartlists. */
2108 written_strings = smartlist_new();
2109 read_strings = smartlist_new();
2110 streams_strings = smartlist_new();
2111 other_read = total_read;
2112 other_written = total_written;
2113 other_streams = total_streams;
2114 /* Sort the ports; this puts them out of sync with top_bytes, but we
2115 * won't be using top_bytes again anyway */
2116 qsort(top_ports, top_elements, sizeof(int), compare_int_);
2117 for (j = 0; j < top_elements; j++) {
2118 cur_port = top_ports[j];
2119 if (exit_bytes_written[cur_port] > 0) {
2120 uint64_t num = round_uint64_to_next_multiple_of(
2121 exit_bytes_written[cur_port],
2122 EXIT_STATS_ROUND_UP_BYTES);
2123 num /= 1024;
2124 smartlist_add_asprintf(written_strings, "%d="U64_FORMAT,
2125 cur_port, U64_PRINTF_ARG(num));
2126 other_written -= exit_bytes_written[cur_port];
2128 if (exit_bytes_read[cur_port] > 0) {
2129 uint64_t num = round_uint64_to_next_multiple_of(
2130 exit_bytes_read[cur_port],
2131 EXIT_STATS_ROUND_UP_BYTES);
2132 num /= 1024;
2133 smartlist_add_asprintf(read_strings, "%d="U64_FORMAT,
2134 cur_port, U64_PRINTF_ARG(num));
2135 other_read -= exit_bytes_read[cur_port];
2137 if (exit_streams[cur_port] > 0) {
2138 uint32_t num = round_uint32_to_next_multiple_of(
2139 exit_streams[cur_port],
2140 EXIT_STATS_ROUND_UP_STREAMS);
2141 smartlist_add_asprintf(streams_strings, "%d=%u", cur_port, num);
2142 other_streams -= exit_streams[cur_port];
2146 /* Add observations of other ports in a single element. */
2147 other_written = round_uint64_to_next_multiple_of(other_written,
2148 EXIT_STATS_ROUND_UP_BYTES);
2149 other_written /= 1024;
2150 smartlist_add_asprintf(written_strings, "other="U64_FORMAT,
2151 U64_PRINTF_ARG(other_written));
2152 other_read = round_uint64_to_next_multiple_of(other_read,
2153 EXIT_STATS_ROUND_UP_BYTES);
2154 other_read /= 1024;
2155 smartlist_add_asprintf(read_strings, "other="U64_FORMAT,
2156 U64_PRINTF_ARG(other_read));
2157 other_streams = round_uint32_to_next_multiple_of(other_streams,
2158 EXIT_STATS_ROUND_UP_STREAMS);
2159 smartlist_add_asprintf(streams_strings, "other=%u", other_streams);
2161 /* Join all observations in single strings. */
2162 written_string = smartlist_join_strings(written_strings, ",", 0, NULL);
2163 read_string = smartlist_join_strings(read_strings, ",", 0, NULL);
2164 streams_string = smartlist_join_strings(streams_strings, ",", 0, NULL);
2165 SMARTLIST_FOREACH(written_strings, char *, cp, tor_free(cp));
2166 SMARTLIST_FOREACH(read_strings, char *, cp, tor_free(cp));
2167 SMARTLIST_FOREACH(streams_strings, char *, cp, tor_free(cp));
2168 smartlist_free(written_strings);
2169 smartlist_free(read_strings);
2170 smartlist_free(streams_strings);
2172 /* Put everything together. */
2173 format_iso_time(t, now);
2174 tor_asprintf(&result, "exit-stats-end %s (%d s)\n"
2175 "exit-kibibytes-written %s\n"
2176 "exit-kibibytes-read %s\n"
2177 "exit-streams-opened %s\n",
2178 t, (unsigned) (now - start_of_exit_stats_interval),
2179 written_string,
2180 read_string,
2181 streams_string);
2182 tor_free(written_string);
2183 tor_free(read_string);
2184 tor_free(streams_string);
2185 return result;
2188 /** If 24 hours have passed since the beginning of the current exit port
2189 * stats period, write exit stats to $DATADIR/stats/exit-stats (possibly
2190 * overwriting an existing file) and reset counters. Return when we would
2191 * next want to write exit stats or 0 if we never want to write. */
2192 time_t
2193 rep_hist_exit_stats_write(time_t now)
2195 char *str = NULL;
2197 if (!start_of_exit_stats_interval)
2198 return 0; /* Not initialized. */
2199 if (start_of_exit_stats_interval + WRITE_STATS_INTERVAL > now)
2200 goto done; /* Not ready to write. */
2202 log_info(LD_HIST, "Writing exit port statistics to disk.");
2204 /* Generate history string. */
2205 str = rep_hist_format_exit_stats(now);
2207 /* Reset counters. */
2208 rep_hist_reset_exit_stats(now);
2210 /* Try to write to disk. */
2211 if (!check_or_create_data_subdir("stats")) {
2212 write_to_data_subdir("stats", "exit-stats", str, "exit port statistics");
2215 done:
2216 tor_free(str);
2217 return start_of_exit_stats_interval + WRITE_STATS_INTERVAL;
2220 /** Note that we wrote <b>num_written</b> bytes and read <b>num_read</b>
2221 * bytes to/from an exit connection to <b>port</b>. */
2222 void
2223 rep_hist_note_exit_bytes(uint16_t port, size_t num_written,
2224 size_t num_read)
2226 if (!start_of_exit_stats_interval)
2227 return; /* Not initialized. */
2228 exit_bytes_written[port] += num_written;
2229 exit_bytes_read[port] += num_read;
2230 log_debug(LD_HIST, "Written %lu bytes and read %lu bytes to/from an "
2231 "exit connection to port %d.",
2232 (unsigned long)num_written, (unsigned long)num_read, port);
2235 /** Note that we opened an exit stream to <b>port</b>. */
2236 void
2237 rep_hist_note_exit_stream_opened(uint16_t port)
2239 if (!start_of_exit_stats_interval)
2240 return; /* Not initialized. */
2241 exit_streams[port]++;
2242 log_debug(LD_HIST, "Opened exit stream to port %d", port);
2245 /*** cell statistics ***/
2247 /** Start of the current buffer stats interval or 0 if we're not
2248 * collecting buffer statistics. */
2249 static time_t start_of_buffer_stats_interval;
2251 /** Initialize buffer stats. */
2252 void
2253 rep_hist_buffer_stats_init(time_t now)
2255 start_of_buffer_stats_interval = now;
2258 /** Statistics from a single circuit. Collected when the circuit closes, or
2259 * when we flush statistics to disk. */
2260 typedef struct circ_buffer_stats_t {
2261 /** Average number of cells in the circuit's queue */
2262 double mean_num_cells_in_queue;
2263 /** Average time a cell waits in the queue. */
2264 double mean_time_cells_in_queue;
2265 /** Total number of cells sent over this circuit */
2266 uint32_t processed_cells;
2267 } circ_buffer_stats_t;
2269 /** List of circ_buffer_stats_t. */
2270 static smartlist_t *circuits_for_buffer_stats = NULL;
2272 /** Remember cell statistics <b>mean_num_cells_in_queue</b>,
2273 * <b>mean_time_cells_in_queue</b>, and <b>processed_cells</b> of a
2274 * circuit. */
2275 void
2276 rep_hist_add_buffer_stats(double mean_num_cells_in_queue,
2277 double mean_time_cells_in_queue, uint32_t processed_cells)
2279 circ_buffer_stats_t *stat;
2280 if (!start_of_buffer_stats_interval)
2281 return; /* Not initialized. */
2282 stat = tor_malloc_zero(sizeof(circ_buffer_stats_t));
2283 stat->mean_num_cells_in_queue = mean_num_cells_in_queue;
2284 stat->mean_time_cells_in_queue = mean_time_cells_in_queue;
2285 stat->processed_cells = processed_cells;
2286 if (!circuits_for_buffer_stats)
2287 circuits_for_buffer_stats = smartlist_new();
2288 smartlist_add(circuits_for_buffer_stats, stat);
2291 /** Remember cell statistics for circuit <b>circ</b> at time
2292 * <b>end_of_interval</b> and reset cell counters in case the circuit
2293 * remains open in the next measurement interval. */
2294 void
2295 rep_hist_buffer_stats_add_circ(circuit_t *circ, time_t end_of_interval)
2297 time_t start_of_interval;
2298 int interval_length;
2299 or_circuit_t *orcirc;
2300 double mean_num_cells_in_queue, mean_time_cells_in_queue;
2301 uint32_t processed_cells;
2302 if (CIRCUIT_IS_ORIGIN(circ))
2303 return;
2304 orcirc = TO_OR_CIRCUIT(circ);
2305 if (!orcirc->processed_cells)
2306 return;
2307 start_of_interval = (circ->timestamp_created.tv_sec >
2308 start_of_buffer_stats_interval) ?
2309 (time_t)circ->timestamp_created.tv_sec :
2310 start_of_buffer_stats_interval;
2311 interval_length = (int) (end_of_interval - start_of_interval);
2312 if (interval_length <= 0)
2313 return;
2314 processed_cells = orcirc->processed_cells;
2315 /* 1000.0 for s -> ms; 2.0 because of app-ward and exit-ward queues */
2316 mean_num_cells_in_queue = (double) orcirc->total_cell_waiting_time /
2317 (double) interval_length / 1000.0 / 2.0;
2318 mean_time_cells_in_queue =
2319 (double) orcirc->total_cell_waiting_time /
2320 (double) orcirc->processed_cells;
2321 orcirc->total_cell_waiting_time = 0;
2322 orcirc->processed_cells = 0;
2323 rep_hist_add_buffer_stats(mean_num_cells_in_queue,
2324 mean_time_cells_in_queue,
2325 processed_cells);
2328 /** Sorting helper: return -1, 1, or 0 based on comparison of two
2329 * circ_buffer_stats_t */
2330 static int
2331 buffer_stats_compare_entries_(const void **_a, const void **_b)
2333 const circ_buffer_stats_t *a = *_a, *b = *_b;
2334 if (a->processed_cells < b->processed_cells)
2335 return 1;
2336 else if (a->processed_cells > b->processed_cells)
2337 return -1;
2338 else
2339 return 0;
2342 /** Stop collecting cell stats in a way that we can re-start doing so in
2343 * rep_hist_buffer_stats_init(). */
2344 void
2345 rep_hist_buffer_stats_term(void)
2347 rep_hist_reset_buffer_stats(0);
2350 /** Clear history of circuit statistics and set the measurement interval
2351 * start to <b>now</b>. */
2352 void
2353 rep_hist_reset_buffer_stats(time_t now)
2355 if (!circuits_for_buffer_stats)
2356 circuits_for_buffer_stats = smartlist_new();
2357 SMARTLIST_FOREACH(circuits_for_buffer_stats, circ_buffer_stats_t *,
2358 stat, tor_free(stat));
2359 smartlist_clear(circuits_for_buffer_stats);
2360 start_of_buffer_stats_interval = now;
2363 /** Return a newly allocated string containing the buffer statistics until
2364 * <b>now</b>, or NULL if we're not collecting buffer stats. Caller must
2365 * ensure start_of_buffer_stats_interval is in the past. */
2366 char *
2367 rep_hist_format_buffer_stats(time_t now)
2369 #define SHARES 10
2370 uint64_t processed_cells[SHARES];
2371 uint32_t circs_in_share[SHARES];
2372 int number_of_circuits, i;
2373 double queued_cells[SHARES], time_in_queue[SHARES];
2374 smartlist_t *processed_cells_strings, *queued_cells_strings,
2375 *time_in_queue_strings;
2376 char *processed_cells_string, *queued_cells_string,
2377 *time_in_queue_string;
2378 char t[ISO_TIME_LEN+1];
2379 char *result;
2381 if (!start_of_buffer_stats_interval)
2382 return NULL; /* Not initialized. */
2384 tor_assert(now >= start_of_buffer_stats_interval);
2386 /* Calculate deciles if we saw at least one circuit. */
2387 memset(processed_cells, 0, SHARES * sizeof(uint64_t));
2388 memset(circs_in_share, 0, SHARES * sizeof(uint32_t));
2389 memset(queued_cells, 0, SHARES * sizeof(double));
2390 memset(time_in_queue, 0, SHARES * sizeof(double));
2391 if (!circuits_for_buffer_stats)
2392 circuits_for_buffer_stats = smartlist_new();
2393 number_of_circuits = smartlist_len(circuits_for_buffer_stats);
2394 if (number_of_circuits > 0) {
2395 smartlist_sort(circuits_for_buffer_stats,
2396 buffer_stats_compare_entries_);
2397 i = 0;
2398 SMARTLIST_FOREACH_BEGIN(circuits_for_buffer_stats,
2399 circ_buffer_stats_t *, stat)
2401 int share = i++ * SHARES / number_of_circuits;
2402 processed_cells[share] += stat->processed_cells;
2403 queued_cells[share] += stat->mean_num_cells_in_queue;
2404 time_in_queue[share] += stat->mean_time_cells_in_queue;
2405 circs_in_share[share]++;
2407 SMARTLIST_FOREACH_END(stat);
2410 /* Write deciles to strings. */
2411 processed_cells_strings = smartlist_new();
2412 queued_cells_strings = smartlist_new();
2413 time_in_queue_strings = smartlist_new();
2414 for (i = 0; i < SHARES; i++) {
2415 smartlist_add_asprintf(processed_cells_strings,
2416 U64_FORMAT, !circs_in_share[i] ? 0 :
2417 U64_PRINTF_ARG(processed_cells[i] /
2418 circs_in_share[i]));
2420 for (i = 0; i < SHARES; i++) {
2421 smartlist_add_asprintf(queued_cells_strings, "%.2f",
2422 circs_in_share[i] == 0 ? 0.0 :
2423 queued_cells[i] / (double) circs_in_share[i]);
2425 for (i = 0; i < SHARES; i++) {
2426 smartlist_add_asprintf(time_in_queue_strings, "%.0f",
2427 circs_in_share[i] == 0 ? 0.0 :
2428 time_in_queue[i] / (double) circs_in_share[i]);
2431 /* Join all observations in single strings. */
2432 processed_cells_string = smartlist_join_strings(processed_cells_strings,
2433 ",", 0, NULL);
2434 queued_cells_string = smartlist_join_strings(queued_cells_strings,
2435 ",", 0, NULL);
2436 time_in_queue_string = smartlist_join_strings(time_in_queue_strings,
2437 ",", 0, NULL);
2438 SMARTLIST_FOREACH(processed_cells_strings, char *, cp, tor_free(cp));
2439 SMARTLIST_FOREACH(queued_cells_strings, char *, cp, tor_free(cp));
2440 SMARTLIST_FOREACH(time_in_queue_strings, char *, cp, tor_free(cp));
2441 smartlist_free(processed_cells_strings);
2442 smartlist_free(queued_cells_strings);
2443 smartlist_free(time_in_queue_strings);
2445 /* Put everything together. */
2446 format_iso_time(t, now);
2447 tor_asprintf(&result, "cell-stats-end %s (%d s)\n"
2448 "cell-processed-cells %s\n"
2449 "cell-queued-cells %s\n"
2450 "cell-time-in-queue %s\n"
2451 "cell-circuits-per-decile %d\n",
2452 t, (unsigned) (now - start_of_buffer_stats_interval),
2453 processed_cells_string,
2454 queued_cells_string,
2455 time_in_queue_string,
2456 (number_of_circuits + SHARES - 1) / SHARES);
2457 tor_free(processed_cells_string);
2458 tor_free(queued_cells_string);
2459 tor_free(time_in_queue_string);
2460 return result;
2461 #undef SHARES
2464 /** If 24 hours have passed since the beginning of the current buffer
2465 * stats period, write buffer stats to $DATADIR/stats/buffer-stats
2466 * (possibly overwriting an existing file) and reset counters. Return
2467 * when we would next want to write buffer stats or 0 if we never want to
2468 * write. */
2469 time_t
2470 rep_hist_buffer_stats_write(time_t now)
2472 char *str = NULL;
2474 if (!start_of_buffer_stats_interval)
2475 return 0; /* Not initialized. */
2476 if (start_of_buffer_stats_interval + WRITE_STATS_INTERVAL > now)
2477 goto done; /* Not ready to write */
2479 /* Add open circuits to the history. */
2480 SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
2481 rep_hist_buffer_stats_add_circ(circ, now);
2483 SMARTLIST_FOREACH_END(circ);
2485 /* Generate history string. */
2486 str = rep_hist_format_buffer_stats(now);
2488 /* Reset both buffer history and counters of open circuits. */
2489 rep_hist_reset_buffer_stats(now);
2491 /* Try to write to disk. */
2492 if (!check_or_create_data_subdir("stats")) {
2493 write_to_data_subdir("stats", "buffer-stats", str, "buffer statistics");
2496 done:
2497 tor_free(str);
2498 return start_of_buffer_stats_interval + WRITE_STATS_INTERVAL;
2501 /*** Descriptor serving statistics ***/
2503 /** Digestmap to track which descriptors were downloaded this stats
2504 * collection interval. It maps descriptor digest to pointers to 1,
2505 * effectively turning this into a list. */
2506 static digestmap_t *served_descs = NULL;
2508 /** Number of how many descriptors were downloaded in total during this
2509 * interval. */
2510 static unsigned long total_descriptor_downloads;
2512 /** Start time of served descs stats or 0 if we're not collecting those. */
2513 static time_t start_of_served_descs_stats_interval;
2515 /** Initialize descriptor stats. */
2516 void
2517 rep_hist_desc_stats_init(time_t now)
2519 if (served_descs) {
2520 log_warn(LD_BUG, "Called rep_hist_desc_stats_init() when desc stats were "
2521 "already initialized. This is probably harmless.");
2522 return; // Already initialized
2524 served_descs = digestmap_new();
2525 total_descriptor_downloads = 0;
2526 start_of_served_descs_stats_interval = now;
2529 /** Reset served descs stats to empty, starting a new interval <b>now</b>. */
2530 static void
2531 rep_hist_reset_desc_stats(time_t now)
2533 rep_hist_desc_stats_term();
2534 rep_hist_desc_stats_init(now);
2537 /** Stop collecting served descs stats, so that rep_hist_desc_stats_init() is
2538 * safe to be called again. */
2539 void
2540 rep_hist_desc_stats_term(void)
2542 digestmap_free(served_descs, NULL);
2543 served_descs = NULL;
2544 start_of_served_descs_stats_interval = 0;
2545 total_descriptor_downloads = 0;
2548 /** Helper for rep_hist_desc_stats_write(). Return a newly allocated string
2549 * containing the served desc statistics until now, or NULL if we're not
2550 * collecting served desc stats. Caller must ensure that now is not before
2551 * start_of_served_descs_stats_interval. */
2552 static char *
2553 rep_hist_format_desc_stats(time_t now)
2555 char t[ISO_TIME_LEN+1];
2556 char *result;
2558 digestmap_iter_t *iter;
2559 const char *key;
2560 void *val;
2561 unsigned size;
2562 int *vals, max = 0, q3 = 0, md = 0, q1 = 0, min = 0;
2563 int n = 0;
2565 if (!start_of_served_descs_stats_interval)
2566 return NULL;
2568 size = digestmap_size(served_descs);
2569 if (size > 0) {
2570 vals = tor_calloc(size, sizeof(int));
2571 for (iter = digestmap_iter_init(served_descs);
2572 !digestmap_iter_done(iter);
2573 iter = digestmap_iter_next(served_descs, iter)) {
2574 uintptr_t count;
2575 digestmap_iter_get(iter, &key, &val);
2576 count = (uintptr_t)val;
2577 vals[n++] = (int)count;
2578 (void)key;
2580 max = find_nth_int(vals, size, size-1);
2581 q3 = find_nth_int(vals, size, (3*size-1)/4);
2582 md = find_nth_int(vals, size, (size-1)/2);
2583 q1 = find_nth_int(vals, size, (size-1)/4);
2584 min = find_nth_int(vals, size, 0);
2585 tor_free(vals);
2588 format_iso_time(t, now);
2590 tor_asprintf(&result,
2591 "served-descs-stats-end %s (%d s) total=%lu unique=%u "
2592 "max=%d q3=%d md=%d q1=%d min=%d\n",
2594 (unsigned) (now - start_of_served_descs_stats_interval),
2595 total_descriptor_downloads,
2596 size, max, q3, md, q1, min);
2598 return result;
2601 /** If WRITE_STATS_INTERVAL seconds have passed since the beginning of
2602 * the current served desc stats interval, write the stats to
2603 * $DATADIR/stats/served-desc-stats (possibly appending to an existing file)
2604 * and reset the state for the next interval. Return when we would next want
2605 * to write served desc stats or 0 if we won't want to write. */
2606 time_t
2607 rep_hist_desc_stats_write(time_t now)
2609 char *filename = NULL, *str = NULL;
2611 if (!start_of_served_descs_stats_interval)
2612 return 0; /* We're not collecting stats. */
2613 if (start_of_served_descs_stats_interval + WRITE_STATS_INTERVAL > now)
2614 return start_of_served_descs_stats_interval + WRITE_STATS_INTERVAL;
2616 str = rep_hist_format_desc_stats(now);
2617 tor_assert(str != NULL);
2619 if (check_or_create_data_subdir("stats") < 0) {
2620 goto done;
2622 filename = get_datadir_fname2("stats", "served-desc-stats");
2623 if (append_bytes_to_file(filename, str, strlen(str), 0) < 0)
2624 log_warn(LD_HIST, "Unable to write served descs statistics to disk!");
2626 rep_hist_reset_desc_stats(now);
2628 done:
2629 tor_free(filename);
2630 tor_free(str);
2631 return start_of_served_descs_stats_interval + WRITE_STATS_INTERVAL;
2634 /* DOCDOC rep_hist_note_desc_served */
2635 void
2636 rep_hist_note_desc_served(const char * desc)
2638 void *val;
2639 uintptr_t count;
2640 if (!served_descs)
2641 return; // We're not collecting stats
2642 val = digestmap_get(served_descs, desc);
2643 count = (uintptr_t)val;
2644 if (count != INT_MAX)
2645 ++count;
2646 digestmap_set(served_descs, desc, (void*)count);
2647 total_descriptor_downloads++;
2650 /*** Connection statistics ***/
2652 /** Start of the current connection stats interval or 0 if we're not
2653 * collecting connection statistics. */
2654 static time_t start_of_conn_stats_interval;
2656 /** Initialize connection stats. */
2657 void
2658 rep_hist_conn_stats_init(time_t now)
2660 start_of_conn_stats_interval = now;
2663 /* Count connections that we read and wrote less than these many bytes
2664 * from/to as below threshold. */
2665 #define BIDI_THRESHOLD 20480
2667 /* Count connections that we read or wrote at least this factor as many
2668 * bytes from/to than we wrote or read to/from as mostly reading or
2669 * writing. */
2670 #define BIDI_FACTOR 10
2672 /* Interval length in seconds for considering read and written bytes for
2673 * connection stats. */
2674 #define BIDI_INTERVAL 10
2676 /** Start of next BIDI_INTERVAL second interval. */
2677 static time_t bidi_next_interval = 0;
2679 /** Number of connections that we read and wrote less than BIDI_THRESHOLD
2680 * bytes from/to in BIDI_INTERVAL seconds. */
2681 static uint32_t below_threshold = 0;
2683 /** Number of connections that we read at least BIDI_FACTOR times more
2684 * bytes from than we wrote to in BIDI_INTERVAL seconds. */
2685 static uint32_t mostly_read = 0;
2687 /** Number of connections that we wrote at least BIDI_FACTOR times more
2688 * bytes to than we read from in BIDI_INTERVAL seconds. */
2689 static uint32_t mostly_written = 0;
2691 /** Number of connections that we read and wrote at least BIDI_THRESHOLD
2692 * bytes from/to, but not BIDI_FACTOR times more in either direction in
2693 * BIDI_INTERVAL seconds. */
2694 static uint32_t both_read_and_written = 0;
2696 /** Entry in a map from connection ID to the number of read and written
2697 * bytes on this connection in a BIDI_INTERVAL second interval. */
2698 typedef struct bidi_map_entry_t {
2699 HT_ENTRY(bidi_map_entry_t) node;
2700 uint64_t conn_id; /**< Connection ID */
2701 size_t read; /**< Number of read bytes */
2702 size_t written; /**< Number of written bytes */
2703 } bidi_map_entry_t;
2705 /** Map of OR connections together with the number of read and written
2706 * bytes in the current BIDI_INTERVAL second interval. */
2707 static HT_HEAD(bidimap, bidi_map_entry_t) bidi_map =
2708 HT_INITIALIZER();
2710 static int
2711 bidi_map_ent_eq(const bidi_map_entry_t *a, const bidi_map_entry_t *b)
2713 return a->conn_id == b->conn_id;
2716 /* DOCDOC bidi_map_ent_hash */
2717 static unsigned
2718 bidi_map_ent_hash(const bidi_map_entry_t *entry)
2720 return (unsigned) entry->conn_id;
2723 HT_PROTOTYPE(bidimap, bidi_map_entry_t, node, bidi_map_ent_hash,
2724 bidi_map_ent_eq);
2725 HT_GENERATE2(bidimap, bidi_map_entry_t, node, bidi_map_ent_hash,
2726 bidi_map_ent_eq, 0.6, tor_reallocarray_, tor_free_)
2728 /* DOCDOC bidi_map_free */
2729 static void
2730 bidi_map_free(void)
2732 bidi_map_entry_t **ptr, **next, *ent;
2733 for (ptr = HT_START(bidimap, &bidi_map); ptr; ptr = next) {
2734 ent = *ptr;
2735 next = HT_NEXT_RMV(bidimap, &bidi_map, ptr);
2736 tor_free(ent);
2738 HT_CLEAR(bidimap, &bidi_map);
2741 /** Reset counters for conn statistics. */
2742 void
2743 rep_hist_reset_conn_stats(time_t now)
2745 start_of_conn_stats_interval = now;
2746 below_threshold = 0;
2747 mostly_read = 0;
2748 mostly_written = 0;
2749 both_read_and_written = 0;
2750 bidi_map_free();
2753 /** Stop collecting connection stats in a way that we can re-start doing
2754 * so in rep_hist_conn_stats_init(). */
2755 void
2756 rep_hist_conn_stats_term(void)
2758 rep_hist_reset_conn_stats(0);
2761 /** We read <b>num_read</b> bytes and wrote <b>num_written</b> from/to OR
2762 * connection <b>conn_id</b> in second <b>when</b>. If this is the first
2763 * observation in a new interval, sum up the last observations. Add bytes
2764 * for this connection. */
2765 void
2766 rep_hist_note_or_conn_bytes(uint64_t conn_id, size_t num_read,
2767 size_t num_written, time_t when)
2769 if (!start_of_conn_stats_interval)
2770 return;
2771 /* Initialize */
2772 if (bidi_next_interval == 0)
2773 bidi_next_interval = when + BIDI_INTERVAL;
2774 /* Sum up last period's statistics */
2775 if (when >= bidi_next_interval) {
2776 bidi_map_entry_t **ptr, **next, *ent;
2777 for (ptr = HT_START(bidimap, &bidi_map); ptr; ptr = next) {
2778 ent = *ptr;
2779 if (ent->read + ent->written < BIDI_THRESHOLD)
2780 below_threshold++;
2781 else if (ent->read >= ent->written * BIDI_FACTOR)
2782 mostly_read++;
2783 else if (ent->written >= ent->read * BIDI_FACTOR)
2784 mostly_written++;
2785 else
2786 both_read_and_written++;
2787 next = HT_NEXT_RMV(bidimap, &bidi_map, ptr);
2788 tor_free(ent);
2790 while (when >= bidi_next_interval)
2791 bidi_next_interval += BIDI_INTERVAL;
2792 log_info(LD_GENERAL, "%d below threshold, %d mostly read, "
2793 "%d mostly written, %d both read and written.",
2794 below_threshold, mostly_read, mostly_written,
2795 both_read_and_written);
2797 /* Add this connection's bytes. */
2798 if (num_read > 0 || num_written > 0) {
2799 bidi_map_entry_t *entry, lookup;
2800 lookup.conn_id = conn_id;
2801 entry = HT_FIND(bidimap, &bidi_map, &lookup);
2802 if (entry) {
2803 entry->written += num_written;
2804 entry->read += num_read;
2805 } else {
2806 entry = tor_malloc_zero(sizeof(bidi_map_entry_t));
2807 entry->conn_id = conn_id;
2808 entry->written = num_written;
2809 entry->read = num_read;
2810 HT_INSERT(bidimap, &bidi_map, entry);
2815 /** Return a newly allocated string containing the connection statistics
2816 * until <b>now</b>, or NULL if we're not collecting conn stats. Caller must
2817 * ensure start_of_conn_stats_interval is in the past. */
2818 char *
2819 rep_hist_format_conn_stats(time_t now)
2821 char *result, written[ISO_TIME_LEN+1];
2823 if (!start_of_conn_stats_interval)
2824 return NULL; /* Not initialized. */
2826 tor_assert(now >= start_of_conn_stats_interval);
2828 format_iso_time(written, now);
2829 tor_asprintf(&result, "conn-bi-direct %s (%d s) %d,%d,%d,%d\n",
2830 written,
2831 (unsigned) (now - start_of_conn_stats_interval),
2832 below_threshold,
2833 mostly_read,
2834 mostly_written,
2835 both_read_and_written);
2836 return result;
2839 /** If 24 hours have passed since the beginning of the current conn stats
2840 * period, write conn stats to $DATADIR/stats/conn-stats (possibly
2841 * overwriting an existing file) and reset counters. Return when we would
2842 * next want to write conn stats or 0 if we never want to write. */
2843 time_t
2844 rep_hist_conn_stats_write(time_t now)
2846 char *str = NULL;
2848 if (!start_of_conn_stats_interval)
2849 return 0; /* Not initialized. */
2850 if (start_of_conn_stats_interval + WRITE_STATS_INTERVAL > now)
2851 goto done; /* Not ready to write */
2853 /* Generate history string. */
2854 str = rep_hist_format_conn_stats(now);
2856 /* Reset counters. */
2857 rep_hist_reset_conn_stats(now);
2859 /* Try to write to disk. */
2860 if (!check_or_create_data_subdir("stats")) {
2861 write_to_data_subdir("stats", "conn-stats", str, "connection statistics");
2864 done:
2865 tor_free(str);
2866 return start_of_conn_stats_interval + WRITE_STATS_INTERVAL;
2869 /** Internal statistics to track how many requests of each type of
2870 * handshake we've received, and how many we've assigned to cpuworkers.
2871 * Useful for seeing trends in cpu load.
2872 * @{ */
2873 STATIC int onion_handshakes_requested[MAX_ONION_HANDSHAKE_TYPE+1] = {0};
2874 STATIC int onion_handshakes_assigned[MAX_ONION_HANDSHAKE_TYPE+1] = {0};
2875 /**@}*/
2877 /** A new onionskin (using the <b>type</b> handshake) has arrived. */
2878 void
2879 rep_hist_note_circuit_handshake_requested(uint16_t type)
2881 if (type <= MAX_ONION_HANDSHAKE_TYPE)
2882 onion_handshakes_requested[type]++;
2885 /** We've sent an onionskin (using the <b>type</b> handshake) to a
2886 * cpuworker. */
2887 void
2888 rep_hist_note_circuit_handshake_assigned(uint16_t type)
2890 if (type <= MAX_ONION_HANDSHAKE_TYPE)
2891 onion_handshakes_assigned[type]++;
2894 /** Log our onionskin statistics since the last time we were called. */
2895 void
2896 rep_hist_log_circuit_handshake_stats(time_t now)
2898 (void)now;
2899 log_notice(LD_HEARTBEAT, "Circuit handshake stats since last time: "
2900 "%d/%d TAP, %d/%d NTor.",
2901 onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_TAP],
2902 onion_handshakes_requested[ONION_HANDSHAKE_TYPE_TAP],
2903 onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_NTOR],
2904 onion_handshakes_requested[ONION_HANDSHAKE_TYPE_NTOR]);
2905 memset(onion_handshakes_assigned, 0, sizeof(onion_handshakes_assigned));
2906 memset(onion_handshakes_requested, 0, sizeof(onion_handshakes_requested));
2909 /* Hidden service statistics section */
2911 /** Start of the current hidden service stats interval or 0 if we're
2912 * not collecting hidden service statistics. */
2913 static time_t start_of_hs_stats_interval;
2915 /** Carries the various hidden service statistics, and any other
2916 * information needed. */
2917 typedef struct hs_stats_t {
2918 /** How many relay cells have we seen as rendezvous points? */
2919 int64_t rp_relay_cells_seen;
2921 /** Set of unique public key digests we've seen this stat period
2922 * (could also be implemented as sorted smartlist). */
2923 digestmap_t *onions_seen_this_period;
2924 } hs_stats_t;
2926 /** Our statistics structure singleton. */
2927 static hs_stats_t *hs_stats = NULL;
2929 /** Allocate, initialize and return an hs_stats_t structure. */
2930 static hs_stats_t *
2931 hs_stats_new(void)
2933 hs_stats_t * hs_stats = tor_malloc_zero(sizeof(hs_stats_t));
2934 hs_stats->onions_seen_this_period = digestmap_new();
2936 return hs_stats;
2939 /** Free an hs_stats_t structure. */
2940 static void
2941 hs_stats_free(hs_stats_t *hs_stats)
2943 if (!hs_stats) {
2944 return;
2947 digestmap_free(hs_stats->onions_seen_this_period, NULL);
2948 tor_free(hs_stats);
2951 /** Initialize hidden service statistics. */
2952 void
2953 rep_hist_hs_stats_init(time_t now)
2955 if (!hs_stats) {
2956 hs_stats = hs_stats_new();
2959 start_of_hs_stats_interval = now;
2962 /** Clear history of hidden service statistics and set the measurement
2963 * interval start to <b>now</b>. */
2964 static void
2965 rep_hist_reset_hs_stats(time_t now)
2967 if (!hs_stats) {
2968 hs_stats = hs_stats_new();
2971 hs_stats->rp_relay_cells_seen = 0;
2973 digestmap_free(hs_stats->onions_seen_this_period, NULL);
2974 hs_stats->onions_seen_this_period = digestmap_new();
2976 start_of_hs_stats_interval = now;
2979 /** Stop collecting hidden service stats in a way that we can re-start
2980 * doing so in rep_hist_buffer_stats_init(). */
2981 void
2982 rep_hist_hs_stats_term(void)
2984 rep_hist_reset_hs_stats(0);
2987 /** We saw a new HS relay cell, Count it! */
2988 void
2989 rep_hist_seen_new_rp_cell(void)
2991 if (!hs_stats) {
2992 return; // We're not collecting stats
2995 hs_stats->rp_relay_cells_seen++;
2998 /** As HSDirs, we saw another hidden service with public key
2999 * <b>pubkey</b>. Check whether we have counted it before, if not
3000 * count it now! */
3001 void
3002 rep_hist_stored_maybe_new_hs(const crypto_pk_t *pubkey)
3004 char pubkey_hash[DIGEST_LEN];
3006 if (!hs_stats) {
3007 return; // We're not collecting stats
3010 /* Get the digest of the pubkey which will be used to detect whether
3011 we've seen this hidden service before or not. */
3012 if (crypto_pk_get_digest(pubkey, pubkey_hash) < 0) {
3013 /* This fail should not happen; key has been validated by
3014 descriptor parsing code first. */
3015 return;
3018 /* Check if this is the first time we've seen this hidden
3019 service. If it is, count it as new. */
3020 if (!digestmap_get(hs_stats->onions_seen_this_period,
3021 pubkey_hash)) {
3022 digestmap_set(hs_stats->onions_seen_this_period,
3023 pubkey_hash, (void*)(uintptr_t)1);
3027 /* The number of cells that are supposed to be hidden from the adversary
3028 * by adding noise from the Laplace distribution. This value, divided by
3029 * EPSILON, is Laplace parameter b. */
3030 #define REND_CELLS_DELTA_F 2048
3031 /* Security parameter for obfuscating number of cells with a value between
3032 * 0 and 1. Smaller values obfuscate observations more, but at the same
3033 * time make statistics less usable. */
3034 #define REND_CELLS_EPSILON 0.3
3035 /* The number of cells that are supposed to be hidden from the adversary
3036 * by rounding up to the next multiple of this number. */
3037 #define REND_CELLS_BIN_SIZE 1024
3038 /* The number of service identities that are supposed to be hidden from
3039 * the adversary by adding noise from the Laplace distribution. This
3040 * value, divided by EPSILON, is Laplace parameter b. */
3041 #define ONIONS_SEEN_DELTA_F 8
3042 /* Security parameter for obfuscating number of service identities with a
3043 * value between 0 and 1. Smaller values obfuscate observations more, but
3044 * at the same time make statistics less usable. */
3045 #define ONIONS_SEEN_EPSILON 0.3
3046 /* The number of service identities that are supposed to be hidden from
3047 * the adversary by rounding up to the next multiple of this number. */
3048 #define ONIONS_SEEN_BIN_SIZE 8
3050 /** Allocate and return a string containing hidden service stats that
3051 * are meant to be placed in the extra-info descriptor. */
3052 static char *
3053 rep_hist_format_hs_stats(time_t now)
3055 char t[ISO_TIME_LEN+1];
3056 char *hs_stats_string;
3057 int64_t obfuscated_cells_seen;
3058 int64_t obfuscated_onions_seen;
3060 obfuscated_cells_seen = round_int64_to_next_multiple_of(
3061 hs_stats->rp_relay_cells_seen,
3062 REND_CELLS_BIN_SIZE);
3063 obfuscated_cells_seen = add_laplace_noise(obfuscated_cells_seen,
3064 crypto_rand_double(),
3065 REND_CELLS_DELTA_F, REND_CELLS_EPSILON);
3066 obfuscated_onions_seen = round_int64_to_next_multiple_of(digestmap_size(
3067 hs_stats->onions_seen_this_period),
3068 ONIONS_SEEN_BIN_SIZE);
3069 obfuscated_onions_seen = add_laplace_noise(obfuscated_onions_seen,
3070 crypto_rand_double(), ONIONS_SEEN_DELTA_F,
3071 ONIONS_SEEN_EPSILON);
3073 format_iso_time(t, now);
3074 tor_asprintf(&hs_stats_string, "hidserv-stats-end %s (%d s)\n"
3075 "hidserv-rend-relayed-cells "I64_FORMAT" delta_f=%d "
3076 "epsilon=%.2f bin_size=%d\n"
3077 "hidserv-dir-onions-seen "I64_FORMAT" delta_f=%d "
3078 "epsilon=%.2f bin_size=%d\n",
3079 t, (unsigned) (now - start_of_hs_stats_interval),
3080 I64_PRINTF_ARG(obfuscated_cells_seen), REND_CELLS_DELTA_F,
3081 REND_CELLS_EPSILON, REND_CELLS_BIN_SIZE,
3082 I64_PRINTF_ARG(obfuscated_onions_seen),
3083 ONIONS_SEEN_DELTA_F,
3084 ONIONS_SEEN_EPSILON, ONIONS_SEEN_BIN_SIZE);
3086 return hs_stats_string;
3089 /** If 24 hours have passed since the beginning of the current HS
3090 * stats period, write buffer stats to $DATADIR/stats/hidserv-stats
3091 * (possibly overwriting an existing file) and reset counters. Return
3092 * when we would next want to write buffer stats or 0 if we never want to
3093 * write. */
3094 time_t
3095 rep_hist_hs_stats_write(time_t now)
3097 char *str = NULL;
3099 if (!start_of_hs_stats_interval) {
3100 return 0; /* Not initialized. */
3103 if (start_of_hs_stats_interval + WRITE_STATS_INTERVAL > now) {
3104 goto done; /* Not ready to write */
3107 /* Generate history string. */
3108 str = rep_hist_format_hs_stats(now);
3110 /* Reset HS history. */
3111 rep_hist_reset_hs_stats(now);
3113 /* Try to write to disk. */
3114 if (!check_or_create_data_subdir("stats")) {
3115 write_to_data_subdir("stats", "hidserv-stats", str,
3116 "hidden service stats");
3119 done:
3120 tor_free(str);
3121 return start_of_hs_stats_interval + WRITE_STATS_INTERVAL;
3124 #define MAX_LINK_PROTO_TO_LOG 4
3125 static uint64_t link_proto_count[MAX_LINK_PROTO_TO_LOG+1][2];
3127 /** Note that we negotiated link protocol version <b>link_proto</b>, on
3128 * a connection that started here iff <b>started_here</b> is true.
3130 void
3131 rep_hist_note_negotiated_link_proto(unsigned link_proto, int started_here)
3133 started_here = !!started_here; /* force to 0 or 1 */
3134 if (link_proto > MAX_LINK_PROTO_TO_LOG) {
3135 log_warn(LD_BUG, "Can't log link protocol %u", link_proto);
3136 return;
3139 link_proto_count[link_proto][started_here]++;
3142 /** Log a heartbeat message explaining how many connections of each link
3143 * protocol version we have used.
3145 void
3146 rep_hist_log_link_protocol_counts(void)
3148 log_notice(LD_HEARTBEAT,
3149 "Since startup, we have initiated "
3150 U64_FORMAT" v1 connections, "
3151 U64_FORMAT" v2 connections, "
3152 U64_FORMAT" v3 connections, and "
3153 U64_FORMAT" v4 connections; and received "
3154 U64_FORMAT" v1 connections, "
3155 U64_FORMAT" v2 connections, "
3156 U64_FORMAT" v3 connections, and "
3157 U64_FORMAT" v4 connections.",
3158 U64_PRINTF_ARG(link_proto_count[1][1]),
3159 U64_PRINTF_ARG(link_proto_count[2][1]),
3160 U64_PRINTF_ARG(link_proto_count[3][1]),
3161 U64_PRINTF_ARG(link_proto_count[4][1]),
3162 U64_PRINTF_ARG(link_proto_count[1][0]),
3163 U64_PRINTF_ARG(link_proto_count[2][0]),
3164 U64_PRINTF_ARG(link_proto_count[3][0]),
3165 U64_PRINTF_ARG(link_proto_count[4][0]));
3168 /** Free all storage held by the OR/link history caches, by the
3169 * bandwidth history arrays, by the port history, or by statistics . */
3170 void
3171 rep_hist_free_all(void)
3173 hs_stats_free(hs_stats);
3174 digestmap_free(history_map, free_or_history);
3175 tor_free(read_array);
3176 tor_free(write_array);
3177 tor_free(dir_read_array);
3178 tor_free(dir_write_array);
3179 tor_free(exit_bytes_read);
3180 tor_free(exit_bytes_written);
3181 tor_free(exit_streams);
3182 predicted_ports_free();
3183 bidi_map_free();
3185 if (circuits_for_buffer_stats) {
3186 SMARTLIST_FOREACH(circuits_for_buffer_stats, circ_buffer_stats_t *, s,
3187 tor_free(s));
3188 smartlist_free(circuits_for_buffer_stats);
3189 circuits_for_buffer_stats = NULL;
3191 rep_hist_desc_stats_term();
3192 total_descriptor_downloads = 0;