Report only the top 10 ports in exit-port stats.
[tor/rransom.git] / src / or / rephist.c
blobcdb596ba8fdcaf2c244c3c5947343bc5e09394af
1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2010, 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 and cell statistics.
11 **/
13 #include "or.h"
14 #include "circuitlist.h"
15 #include "circuituse.h"
16 #include "config.h"
17 #include "rephist.h"
18 #include "router.h"
19 #include "routerlist.h"
20 #include "ht.h"
22 static void bw_arrays_init(void);
23 static void predicted_ports_init(void);
25 /** Total number of bytes currently allocated in fields used by rephist.c. */
26 uint64_t rephist_total_alloc=0;
27 /** Number of or_history_t objects currently allocated. */
28 uint32_t rephist_total_num=0;
30 /** If the total weighted run count of all runs for a router ever falls
31 * below this amount, the router can be treated as having 0 MTBF. */
32 #define STABILITY_EPSILON 0.0001
33 /** Value by which to discount all old intervals for MTBF purposes. This
34 * is compounded every STABILITY_INTERVAL. */
35 #define STABILITY_ALPHA 0.95
36 /** Interval at which to discount all old intervals for MTBF purposes. */
37 #define STABILITY_INTERVAL (12*60*60)
38 /* (This combination of ALPHA, INTERVAL, and EPSILON makes it so that an
39 * interval that just ended counts twice as much as one that ended a week ago,
40 * 20X as much as one that ended a month ago, and routers that have had no
41 * uptime data for about half a year will get forgotten.) */
43 /** History of an OR-\>OR link. */
44 typedef struct link_history_t {
45 /** When did we start tracking this list? */
46 time_t since;
47 /** When did we most recently note a change to this link */
48 time_t changed;
49 /** How many times did extending from OR1 to OR2 succeed? */
50 unsigned long n_extend_ok;
51 /** How many times did extending from OR1 to OR2 fail? */
52 unsigned long n_extend_fail;
53 } link_history_t;
55 /** History of an OR. */
56 typedef struct or_history_t {
57 /** When did we start tracking this OR? */
58 time_t since;
59 /** When did we most recently note a change to this OR? */
60 time_t changed;
61 /** How many times did we successfully connect? */
62 unsigned long n_conn_ok;
63 /** How many times did we try to connect and fail?*/
64 unsigned long n_conn_fail;
65 /** How many seconds have we been connected to this OR before
66 * 'up_since'? */
67 unsigned long uptime;
68 /** How many seconds have we been unable to connect to this OR before
69 * 'down_since'? */
70 unsigned long downtime;
71 /** If nonzero, we have been connected since this time. */
72 time_t up_since;
73 /** If nonzero, we have been unable to connect since this time. */
74 time_t down_since;
76 /* === For MTBF tracking: */
77 /** Weighted sum total of all times that this router has been online.
79 unsigned long weighted_run_length;
80 /** If the router is now online (according to stability-checking rules),
81 * when did it come online? */
82 time_t start_of_run;
83 /** Sum of weights for runs in weighted_run_length. */
84 double total_run_weights;
85 /* === For fractional uptime tracking: */
86 time_t start_of_downtime;
87 unsigned long weighted_uptime;
88 unsigned long total_weighted_time;
90 /** Map from hex OR2 identity digest to a link_history_t for the link
91 * from this OR to OR2. */
92 digestmap_t *link_history_map;
93 } or_history_t;
95 /** When did we last multiply all routers' weighted_run_length and
96 * total_run_weights by STABILITY_ALPHA? */
97 static time_t stability_last_downrated = 0;
99 /** */
100 static time_t started_tracking_stability = 0;
102 /** Map from hex OR identity digest to or_history_t. */
103 static digestmap_t *history_map = NULL;
105 /** Return the or_history_t for the OR with identity digest <b>id</b>,
106 * creating it if necessary. */
107 static or_history_t *
108 get_or_history(const char* id)
110 or_history_t *hist;
112 if (tor_mem_is_zero(id, DIGEST_LEN))
113 return NULL;
115 hist = digestmap_get(history_map, id);
116 if (!hist) {
117 hist = tor_malloc_zero(sizeof(or_history_t));
118 rephist_total_alloc += sizeof(or_history_t);
119 rephist_total_num++;
120 hist->link_history_map = digestmap_new();
121 hist->since = hist->changed = time(NULL);
122 digestmap_set(history_map, id, hist);
124 return hist;
127 /** Return the link_history_t for the link from the first named OR to
128 * the second, creating it if necessary. (ORs are identified by
129 * identity digest.)
131 static link_history_t *
132 get_link_history(const char *from_id, const char *to_id)
134 or_history_t *orhist;
135 link_history_t *lhist;
136 orhist = get_or_history(from_id);
137 if (!orhist)
138 return NULL;
139 if (tor_mem_is_zero(to_id, DIGEST_LEN))
140 return NULL;
141 lhist = (link_history_t*) digestmap_get(orhist->link_history_map, to_id);
142 if (!lhist) {
143 lhist = tor_malloc_zero(sizeof(link_history_t));
144 rephist_total_alloc += sizeof(link_history_t);
145 lhist->since = lhist->changed = time(NULL);
146 digestmap_set(orhist->link_history_map, to_id, lhist);
148 return lhist;
151 /** Helper: free storage held by a single link history entry. */
152 static void
153 _free_link_history(void *val)
155 rephist_total_alloc -= sizeof(link_history_t);
156 tor_free(val);
159 /** Helper: free storage held by a single OR history entry. */
160 static void
161 free_or_history(void *_hist)
163 or_history_t *hist = _hist;
164 digestmap_free(hist->link_history_map, _free_link_history);
165 rephist_total_alloc -= sizeof(or_history_t);
166 rephist_total_num--;
167 tor_free(hist);
170 /** Update an or_history_t object <b>hist</b> so that its uptime/downtime
171 * count is up-to-date as of <b>when</b>.
173 static void
174 update_or_history(or_history_t *hist, time_t when)
176 tor_assert(hist);
177 if (hist->up_since) {
178 tor_assert(!hist->down_since);
179 hist->uptime += (when - hist->up_since);
180 hist->up_since = when;
181 } else if (hist->down_since) {
182 hist->downtime += (when - hist->down_since);
183 hist->down_since = when;
187 /** Initialize the static data structures for tracking history. */
188 void
189 rep_hist_init(void)
191 history_map = digestmap_new();
192 bw_arrays_init();
193 predicted_ports_init();
196 /** Helper: note that we are no longer connected to the router with history
197 * <b>hist</b>. If <b>failed</b>, the connection failed; otherwise, it was
198 * closed correctly. */
199 static void
200 mark_or_down(or_history_t *hist, time_t when, int failed)
202 if (hist->up_since) {
203 hist->uptime += (when - hist->up_since);
204 hist->up_since = 0;
206 if (failed && !hist->down_since) {
207 hist->down_since = when;
211 /** Helper: note that we are connected to the router with history
212 * <b>hist</b>. */
213 static void
214 mark_or_up(or_history_t *hist, time_t when)
216 if (hist->down_since) {
217 hist->downtime += (when - hist->down_since);
218 hist->down_since = 0;
220 if (!hist->up_since) {
221 hist->up_since = when;
225 /** Remember that an attempt to connect to the OR with identity digest
226 * <b>id</b> failed at <b>when</b>.
228 void
229 rep_hist_note_connect_failed(const char* id, time_t when)
231 or_history_t *hist;
232 hist = get_or_history(id);
233 if (!hist)
234 return;
235 ++hist->n_conn_fail;
236 mark_or_down(hist, when, 1);
237 hist->changed = when;
240 /** Remember that an attempt to connect to the OR with identity digest
241 * <b>id</b> succeeded at <b>when</b>.
243 void
244 rep_hist_note_connect_succeeded(const char* id, time_t when)
246 or_history_t *hist;
247 hist = get_or_history(id);
248 if (!hist)
249 return;
250 ++hist->n_conn_ok;
251 mark_or_up(hist, when);
252 hist->changed = when;
255 /** Remember that we intentionally closed our connection to the OR
256 * with identity digest <b>id</b> at <b>when</b>.
258 void
259 rep_hist_note_disconnect(const char* id, time_t when)
261 or_history_t *hist;
262 hist = get_or_history(id);
263 if (!hist)
264 return;
265 mark_or_down(hist, when, 0);
266 hist->changed = when;
269 /** Remember that our connection to the OR with identity digest
270 * <b>id</b> had an error and stopped working at <b>when</b>.
272 void
273 rep_hist_note_connection_died(const char* id, time_t when)
275 or_history_t *hist;
276 if (!id) {
277 /* If conn has no identity, it didn't complete its handshake, or something
278 * went wrong. Ignore it.
280 return;
282 hist = get_or_history(id);
283 if (!hist)
284 return;
285 mark_or_down(hist, when, 1);
286 hist->changed = when;
289 /** We have just decided that this router with identity digest <b>id</b> is
290 * reachable, meaning we will give it a "Running" flag for the next while. */
291 void
292 rep_hist_note_router_reachable(const char *id, time_t when)
294 or_history_t *hist = get_or_history(id);
295 int was_in_run = 1;
296 char tbuf[ISO_TIME_LEN+1];
298 tor_assert(hist);
300 if (!started_tracking_stability)
301 started_tracking_stability = time(NULL);
302 if (!hist->start_of_run) {
303 hist->start_of_run = when;
304 was_in_run = 0;
306 if (hist->start_of_downtime) {
307 long down_length;
309 format_local_iso_time(tbuf, hist->start_of_downtime);
310 log_info(LD_HIST, "Router %s is now Running; it had been down since %s.",
311 hex_str(id, DIGEST_LEN), tbuf);
312 if (was_in_run)
313 log_info(LD_HIST, " (Paradoxically, it was already Running too.)");
315 down_length = when - hist->start_of_downtime;
316 hist->total_weighted_time += down_length;
317 hist->start_of_downtime = 0;
318 } else {
319 format_local_iso_time(tbuf, hist->start_of_run);
320 if (was_in_run)
321 log_debug(LD_HIST, "Router %s is still Running; it has been Running "
322 "since %s", hex_str(id, DIGEST_LEN), tbuf);
323 else
324 log_info(LD_HIST,"Router %s is now Running; it was previously untracked",
325 hex_str(id, DIGEST_LEN));
329 /** We have just decided that this router is unreachable, meaning
330 * we are taking away its "Running" flag. */
331 void
332 rep_hist_note_router_unreachable(const char *id, time_t when)
334 or_history_t *hist = get_or_history(id);
335 char tbuf[ISO_TIME_LEN+1];
336 int was_running = 0;
337 if (!started_tracking_stability)
338 started_tracking_stability = time(NULL);
340 tor_assert(hist);
341 if (hist->start_of_run) {
342 /*XXXX We could treat failed connections differently from failed
343 * connect attempts. */
344 long run_length = when - hist->start_of_run;
345 format_local_iso_time(tbuf, hist->start_of_run);
347 hist->weighted_run_length += run_length;
348 hist->total_run_weights += 1.0;
349 hist->start_of_run = 0;
350 hist->weighted_uptime += run_length;
351 hist->total_weighted_time += run_length;
353 was_running = 1;
354 log_info(LD_HIST, "Router %s is now non-Running: it had previously been "
355 "Running since %s. Its total weighted uptime is %lu/%lu.",
356 hex_str(id, DIGEST_LEN), tbuf, hist->weighted_uptime,
357 hist->total_weighted_time);
359 if (!hist->start_of_downtime) {
360 hist->start_of_downtime = when;
362 if (!was_running)
363 log_info(LD_HIST, "Router %s is now non-Running; it was previously "
364 "untracked.", hex_str(id, DIGEST_LEN));
365 } else {
366 if (!was_running) {
367 format_local_iso_time(tbuf, hist->start_of_downtime);
369 log_info(LD_HIST, "Router %s is still non-Running; it has been "
370 "non-Running since %s.", hex_str(id, DIGEST_LEN), tbuf);
375 /** Helper: Discount all old MTBF data, if it is time to do so. Return
376 * the time at which we should next discount MTBF data. */
377 time_t
378 rep_hist_downrate_old_runs(time_t now)
380 digestmap_iter_t *orhist_it;
381 const char *digest1;
382 or_history_t *hist;
383 void *hist_p;
384 double alpha = 1.0;
386 if (!history_map)
387 history_map = digestmap_new();
388 if (!stability_last_downrated)
389 stability_last_downrated = now;
390 if (stability_last_downrated + STABILITY_INTERVAL > now)
391 return stability_last_downrated + STABILITY_INTERVAL;
393 /* Okay, we should downrate the data. By how much? */
394 while (stability_last_downrated + STABILITY_INTERVAL < now) {
395 stability_last_downrated += STABILITY_INTERVAL;
396 alpha *= STABILITY_ALPHA;
399 log_info(LD_HIST, "Discounting all old stability info by a factor of %lf",
400 alpha);
402 /* Multiply every w_r_l, t_r_w pair by alpha. */
403 for (orhist_it = digestmap_iter_init(history_map);
404 !digestmap_iter_done(orhist_it);
405 orhist_it = digestmap_iter_next(history_map,orhist_it)) {
406 digestmap_iter_get(orhist_it, &digest1, &hist_p);
407 hist = hist_p;
409 hist->weighted_run_length =
410 (unsigned long)(hist->weighted_run_length * alpha);
411 hist->total_run_weights *= alpha;
413 hist->weighted_uptime = (unsigned long)(hist->weighted_uptime * alpha);
414 hist->total_weighted_time = (unsigned long)
415 (hist->total_weighted_time * alpha);
418 return stability_last_downrated + STABILITY_INTERVAL;
421 /** Helper: Return the weighted MTBF of the router with history <b>hist</b>. */
422 static double
423 get_stability(or_history_t *hist, time_t when)
425 unsigned long total = hist->weighted_run_length;
426 double total_weights = hist->total_run_weights;
428 if (hist->start_of_run) {
429 /* We're currently in a run. Let total and total_weights hold the values
430 * they would hold if the current run were to end now. */
431 total += (when-hist->start_of_run);
432 total_weights += 1.0;
434 if (total_weights < STABILITY_EPSILON) {
435 /* Round down to zero, and avoid divide-by-zero. */
436 return 0.0;
439 return total / total_weights;
442 /** Return the total amount of time we've been observing, with each run of
443 * time downrated by the appropriate factor. */
444 static long
445 get_total_weighted_time(or_history_t *hist, time_t when)
447 long total = hist->total_weighted_time;
448 if (hist->start_of_run) {
449 total += (when - hist->start_of_run);
450 } else if (hist->start_of_downtime) {
451 total += (when - hist->start_of_downtime);
453 return total;
456 /** Helper: Return the weighted percent-of-time-online of the router with
457 * history <b>hist</b>. */
458 static double
459 get_weighted_fractional_uptime(or_history_t *hist, time_t when)
461 unsigned long total = hist->total_weighted_time;
462 unsigned long up = hist->weighted_uptime;
464 if (hist->start_of_run) {
465 long run_length = (when - hist->start_of_run);
466 up += run_length;
467 total += run_length;
468 } else if (hist->start_of_downtime) {
469 total += (when - hist->start_of_downtime);
472 if (!total) {
473 /* Avoid calling anybody's uptime infinity (which should be impossible if
474 * the code is working), or NaN (which can happen for any router we haven't
475 * observed up or down yet). */
476 return 0.0;
479 return ((double) up) / total;
482 /** Return an estimated MTBF for the router whose identity digest is
483 * <b>id</b>. Return 0 if the router is unknown. */
484 double
485 rep_hist_get_stability(const char *id, time_t when)
487 or_history_t *hist = get_or_history(id);
488 if (!hist)
489 return 0.0;
491 return get_stability(hist, when);
494 /** Return an estimated percent-of-time-online for the router whose identity
495 * digest is <b>id</b>. Return 0 if the router is unknown. */
496 double
497 rep_hist_get_weighted_fractional_uptime(const char *id, time_t when)
499 or_history_t *hist = get_or_history(id);
500 if (!hist)
501 return 0.0;
503 return get_weighted_fractional_uptime(hist, when);
506 /** Return a number representing how long we've known about the router whose
507 * digest is <b>id</b>. Return 0 if the router is unknown.
509 * Be careful: this measure increases monotonically as we know the router for
510 * longer and longer, but it doesn't increase linearly.
512 long
513 rep_hist_get_weighted_time_known(const char *id, time_t when)
515 or_history_t *hist = get_or_history(id);
516 if (!hist)
517 return 0;
519 return get_total_weighted_time(hist, when);
522 /** Return true if we've been measuring MTBFs for long enough to
523 * pronounce on Stability. */
525 rep_hist_have_measured_enough_stability(void)
527 /* XXXX021 This doesn't do so well when we change our opinion
528 * as to whether we're tracking router stability. */
529 return started_tracking_stability < time(NULL) - 4*60*60;
532 /** Remember that we successfully extended from the OR with identity
533 * digest <b>from_id</b> to the OR with identity digest
534 * <b>to_name</b>.
536 void
537 rep_hist_note_extend_succeeded(const char *from_id, const char *to_id)
539 link_history_t *hist;
540 /* log_fn(LOG_WARN, "EXTEND SUCCEEDED: %s->%s",from_name,to_name); */
541 hist = get_link_history(from_id, to_id);
542 if (!hist)
543 return;
544 ++hist->n_extend_ok;
545 hist->changed = time(NULL);
548 /** Remember that we tried to extend from the OR with identity digest
549 * <b>from_id</b> to the OR with identity digest <b>to_name</b>, but
550 * failed.
552 void
553 rep_hist_note_extend_failed(const char *from_id, const char *to_id)
555 link_history_t *hist;
556 /* log_fn(LOG_WARN, "EXTEND FAILED: %s->%s",from_name,to_name); */
557 hist = get_link_history(from_id, to_id);
558 if (!hist)
559 return;
560 ++hist->n_extend_fail;
561 hist->changed = time(NULL);
564 /** Log all the reliability data we have remembered, with the chosen
565 * severity.
567 void
568 rep_hist_dump_stats(time_t now, int severity)
570 digestmap_iter_t *lhist_it;
571 digestmap_iter_t *orhist_it;
572 const char *name1, *name2, *digest1, *digest2;
573 char hexdigest1[HEX_DIGEST_LEN+1];
574 or_history_t *or_history;
575 link_history_t *link_history;
576 void *or_history_p, *link_history_p;
577 double uptime;
578 char buffer[2048];
579 size_t len;
580 int ret;
581 unsigned long upt, downt;
582 routerinfo_t *r;
584 rep_history_clean(now - get_options()->RephistTrackTime);
586 log(severity, LD_HIST, "--------------- Dumping history information:");
588 for (orhist_it = digestmap_iter_init(history_map);
589 !digestmap_iter_done(orhist_it);
590 orhist_it = digestmap_iter_next(history_map,orhist_it)) {
591 double s;
592 long stability;
593 digestmap_iter_get(orhist_it, &digest1, &or_history_p);
594 or_history = (or_history_t*) or_history_p;
596 if ((r = router_get_by_digest(digest1)))
597 name1 = r->nickname;
598 else
599 name1 = "(unknown)";
600 base16_encode(hexdigest1, sizeof(hexdigest1), digest1, DIGEST_LEN);
601 update_or_history(or_history, now);
602 upt = or_history->uptime;
603 downt = or_history->downtime;
604 s = get_stability(or_history, now);
605 stability = (long)s;
606 if (upt+downt) {
607 uptime = ((double)upt) / (upt+downt);
608 } else {
609 uptime=1.0;
611 log(severity, LD_HIST,
612 "OR %s [%s]: %ld/%ld good connections; uptime %ld/%ld sec (%.2f%%); "
613 "wmtbf %lu:%02lu:%02lu",
614 name1, hexdigest1,
615 or_history->n_conn_ok, or_history->n_conn_fail+or_history->n_conn_ok,
616 upt, upt+downt, uptime*100.0,
617 stability/3600, (stability/60)%60, stability%60);
619 if (!digestmap_isempty(or_history->link_history_map)) {
620 strlcpy(buffer, " Extend attempts: ", sizeof(buffer));
621 len = strlen(buffer);
622 for (lhist_it = digestmap_iter_init(or_history->link_history_map);
623 !digestmap_iter_done(lhist_it);
624 lhist_it = digestmap_iter_next(or_history->link_history_map,
625 lhist_it)) {
626 digestmap_iter_get(lhist_it, &digest2, &link_history_p);
627 if ((r = router_get_by_digest(digest2)))
628 name2 = r->nickname;
629 else
630 name2 = "(unknown)";
632 link_history = (link_history_t*) link_history_p;
634 ret = tor_snprintf(buffer+len, 2048-len, "%s(%ld/%ld); ", name2,
635 link_history->n_extend_ok,
636 link_history->n_extend_ok+link_history->n_extend_fail);
637 if (ret<0)
638 break;
639 else
640 len += ret;
642 log(severity, LD_HIST, "%s", buffer);
647 /** Remove history info for routers/links that haven't changed since
648 * <b>before</b>.
650 void
651 rep_history_clean(time_t before)
653 int authority = authdir_mode(get_options());
654 or_history_t *or_history;
655 link_history_t *link_history;
656 void *or_history_p, *link_history_p;
657 digestmap_iter_t *orhist_it, *lhist_it;
658 const char *d1, *d2;
660 orhist_it = digestmap_iter_init(history_map);
661 while (!digestmap_iter_done(orhist_it)) {
662 int remove;
663 digestmap_iter_get(orhist_it, &d1, &or_history_p);
664 or_history = or_history_p;
666 remove = authority ? (or_history->total_run_weights < STABILITY_EPSILON &&
667 !or_history->start_of_run)
668 : (or_history->changed < before);
669 if (remove) {
670 orhist_it = digestmap_iter_next_rmv(history_map, orhist_it);
671 free_or_history(or_history);
672 continue;
674 for (lhist_it = digestmap_iter_init(or_history->link_history_map);
675 !digestmap_iter_done(lhist_it); ) {
676 digestmap_iter_get(lhist_it, &d2, &link_history_p);
677 link_history = link_history_p;
678 if (link_history->changed < before) {
679 lhist_it = digestmap_iter_next_rmv(or_history->link_history_map,
680 lhist_it);
681 rephist_total_alloc -= sizeof(link_history_t);
682 tor_free(link_history);
683 continue;
685 lhist_it = digestmap_iter_next(or_history->link_history_map,lhist_it);
687 orhist_it = digestmap_iter_next(history_map, orhist_it);
691 /** Write MTBF data to disk. Return 0 on success, negative on failure.
693 * If <b>missing_means_down</b>, then if we're about to write an entry
694 * that is still considered up but isn't in our routerlist, consider it
695 * to be down. */
697 rep_hist_record_mtbf_data(time_t now, int missing_means_down)
699 char time_buf[ISO_TIME_LEN+1];
701 digestmap_iter_t *orhist_it;
702 const char *digest;
703 void *or_history_p;
704 or_history_t *hist;
705 open_file_t *open_file = NULL;
706 FILE *f;
709 char *filename = get_datadir_fname("router-stability");
710 f = start_writing_to_stdio_file(filename, OPEN_FLAGS_REPLACE|O_TEXT, 0600,
711 &open_file);
712 tor_free(filename);
713 if (!f)
714 return -1;
717 /* File format is:
718 * FormatLine *KeywordLine Data
720 * FormatLine = "format 1" NL
721 * KeywordLine = Keyword SP Arguments NL
722 * Data = "data" NL *RouterMTBFLine "." NL
723 * RouterMTBFLine = Fingerprint SP WeightedRunLen SP
724 * TotalRunWeights [SP S=StartRunTime] NL
726 #define PUT(s) STMT_BEGIN if (fputs((s),f)<0) goto err; STMT_END
727 #define PRINTF(args) STMT_BEGIN if (fprintf args <0) goto err; STMT_END
729 PUT("format 2\n");
731 format_iso_time(time_buf, time(NULL));
732 PRINTF((f, "stored-at %s\n", time_buf));
734 if (started_tracking_stability) {
735 format_iso_time(time_buf, started_tracking_stability);
736 PRINTF((f, "tracked-since %s\n", time_buf));
738 if (stability_last_downrated) {
739 format_iso_time(time_buf, stability_last_downrated);
740 PRINTF((f, "last-downrated %s\n", time_buf));
743 PUT("data\n");
745 /* XXX Nick: now bridge auths record this for all routers too.
746 * Should we make them record it only for bridge routers? -RD
747 * Not for 0.2.0. -NM */
748 for (orhist_it = digestmap_iter_init(history_map);
749 !digestmap_iter_done(orhist_it);
750 orhist_it = digestmap_iter_next(history_map,orhist_it)) {
751 char dbuf[HEX_DIGEST_LEN+1];
752 const char *t = NULL;
753 digestmap_iter_get(orhist_it, &digest, &or_history_p);
754 hist = (or_history_t*) or_history_p;
756 base16_encode(dbuf, sizeof(dbuf), digest, DIGEST_LEN);
758 if (missing_means_down && hist->start_of_run &&
759 !router_get_by_digest(digest)) {
760 /* We think this relay is running, but it's not listed in our
761 * routerlist. Somehow it fell out without telling us it went
762 * down. Complain and also correct it. */
763 log_info(LD_HIST,
764 "Relay '%s' is listed as up in rephist, but it's not in "
765 "our routerlist. Correcting.", dbuf);
766 rep_hist_note_router_unreachable(digest, now);
769 PRINTF((f, "R %s\n", dbuf));
770 if (hist->start_of_run > 0) {
771 format_iso_time(time_buf, hist->start_of_run);
772 t = time_buf;
774 PRINTF((f, "+MTBF %lu %.5lf%s%s\n",
775 hist->weighted_run_length, hist->total_run_weights,
776 t ? " S=" : "", t ? t : ""));
777 t = NULL;
778 if (hist->start_of_downtime > 0) {
779 format_iso_time(time_buf, hist->start_of_downtime);
780 t = time_buf;
782 PRINTF((f, "+WFU %lu %lu%s%s\n",
783 hist->weighted_uptime, hist->total_weighted_time,
784 t ? " S=" : "", t ? t : ""));
787 PUT(".\n");
789 #undef PUT
790 #undef PRINTF
792 return finish_writing_to_file(open_file);
793 err:
794 abort_writing_to_file(open_file);
795 return -1;
798 /** Format the current tracked status of the router in <b>hist</b> at time
799 * <b>now</b> for analysis; return it in a newly allocated string. */
800 static char *
801 rep_hist_format_router_status(or_history_t *hist, time_t now)
803 char sor_buf[ISO_TIME_LEN+1];
804 char sod_buf[ISO_TIME_LEN+1];
805 double wfu;
806 double mtbf;
807 int up = 0, down = 0;
808 char *cp = NULL;
810 if (hist->start_of_run) {
811 format_iso_time(sor_buf, hist->start_of_run);
812 up = 1;
814 if (hist->start_of_downtime) {
815 format_iso_time(sod_buf, hist->start_of_downtime);
816 down = 1;
819 wfu = get_weighted_fractional_uptime(hist, now);
820 mtbf = get_stability(hist, now);
821 tor_asprintf(&cp,
822 "%s%s%s"
823 "%s%s%s"
824 "wfu %0.3lf\n"
825 " weighted-time %lu\n"
826 " weighted-uptime %lu\n"
827 "mtbf %0.1lf\n"
828 " weighted-run-length %lu\n"
829 " total-run-weights %lf\n",
830 up?"uptime-started ":"", up?sor_buf:"", up?" UTC\n":"",
831 down?"downtime-started ":"", down?sod_buf:"", down?" UTC\n":"",
832 wfu,
833 hist->total_weighted_time,
834 hist->weighted_uptime,
835 mtbf,
836 hist->weighted_run_length,
837 hist->total_run_weights
839 return cp;
842 /** The last stability analysis document that we created, or NULL if we never
843 * have created one. */
844 static char *last_stability_doc = NULL;
845 /** The last time we created a stability analysis document, or 0 if we never
846 * have created one. */
847 static time_t built_last_stability_doc_at = 0;
848 /** Shortest allowable time between building two stability documents. */
849 #define MAX_STABILITY_DOC_BUILD_RATE (3*60)
851 /** Return a pointer to a NUL-terminated document describing our view of the
852 * stability of the routers we've been tracking. Return NULL on failure. */
853 const char *
854 rep_hist_get_router_stability_doc(time_t now)
856 char *result;
857 smartlist_t *chunks;
858 if (built_last_stability_doc_at + MAX_STABILITY_DOC_BUILD_RATE > now)
859 return last_stability_doc;
861 if (!history_map)
862 return NULL;
864 tor_free(last_stability_doc);
865 chunks = smartlist_create();
867 if (rep_hist_have_measured_enough_stability()) {
868 smartlist_add(chunks, tor_strdup("we-have-enough-measurements\n"));
869 } else {
870 smartlist_add(chunks, tor_strdup("we-do-not-have-enough-measurements\n"));
873 DIGESTMAP_FOREACH(history_map, id, or_history_t *, hist) {
874 routerinfo_t *ri;
875 char dbuf[BASE64_DIGEST_LEN+1];
876 char header_buf[512];
877 char *info;
878 digest_to_base64(dbuf, id);
879 ri = router_get_by_digest(id);
880 if (ri) {
881 char *ip = tor_dup_ip(ri->addr);
882 char tbuf[ISO_TIME_LEN+1];
883 format_iso_time(tbuf, ri->cache_info.published_on);
884 tor_snprintf(header_buf, sizeof(header_buf),
885 "router %s %s %s\n"
886 "published %s\n"
887 "relevant-flags %s%s%s\n"
888 "declared-uptime %ld\n",
889 dbuf, ri->nickname, ip,
890 tbuf,
891 ri->is_running ? "Running " : "",
892 ri->is_valid ? "Valid " : "",
893 ri->is_hibernating ? "Hibernating " : "",
894 ri->uptime);
895 tor_free(ip);
896 } else {
897 tor_snprintf(header_buf, sizeof(header_buf),
898 "router %s {no descriptor}\n", dbuf);
900 smartlist_add(chunks, tor_strdup(header_buf));
901 info = rep_hist_format_router_status(hist, now);
902 if (info)
903 smartlist_add(chunks, info);
905 } DIGESTMAP_FOREACH_END;
907 result = smartlist_join_strings(chunks, "", 0, NULL);
908 SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
909 smartlist_free(chunks);
911 last_stability_doc = result;
912 built_last_stability_doc_at = time(NULL);
913 return result;
916 /** Helper: return the first j >= i such that !strcmpstart(sl[j], prefix) and
917 * such that no line sl[k] with i <= k < j starts with "R ". Return -1 if no
918 * such line exists. */
919 static int
920 find_next_with(smartlist_t *sl, int i, const char *prefix)
922 for ( ; i < smartlist_len(sl); ++i) {
923 const char *line = smartlist_get(sl, i);
924 if (!strcmpstart(line, prefix))
925 return i;
926 if (!strcmpstart(line, "R "))
927 return -1;
929 return -1;
932 /** How many bad times has parse_possibly_bad_iso_time parsed? */
933 static int n_bogus_times = 0;
934 /** Parse the ISO-formatted time in <b>s</b> into *<b>time_out</b>, but
935 * rounds any pre-1970 date to Jan 1, 1970. */
936 static int
937 parse_possibly_bad_iso_time(const char *s, time_t *time_out)
939 int year;
940 char b[5];
941 strlcpy(b, s, sizeof(b));
942 b[4] = '\0';
943 year = (int)tor_parse_long(b, 10, 0, INT_MAX, NULL, NULL);
944 if (year < 1970) {
945 *time_out = 0;
946 ++n_bogus_times;
947 return 0;
948 } else
949 return parse_iso_time(s, time_out);
952 /** We've read a time <b>t</b> from a file stored at <b>stored_at</b>, which
953 * says we started measuring at <b>started_measuring</b>. Return a new number
954 * that's about as much before <b>now</b> as <b>t</b> was before
955 * <b>stored_at</b>.
957 static INLINE time_t
958 correct_time(time_t t, time_t now, time_t stored_at, time_t started_measuring)
960 if (t < started_measuring - 24*60*60*365)
961 return 0;
962 else if (t < started_measuring)
963 return started_measuring;
964 else if (t > stored_at)
965 return 0;
966 else {
967 long run_length = stored_at - t;
968 t = now - run_length;
969 if (t < started_measuring)
970 t = started_measuring;
971 return t;
975 /** Load MTBF data from disk. Returns 0 on success or recoverable error, -1
976 * on failure. */
978 rep_hist_load_mtbf_data(time_t now)
980 /* XXXX won't handle being called while history is already populated. */
981 smartlist_t *lines;
982 const char *line = NULL;
983 int r=0, i;
984 time_t last_downrated = 0, stored_at = 0, tracked_since = 0;
985 time_t latest_possible_start = now;
986 long format = -1;
989 char *filename = get_datadir_fname("router-stability");
990 char *d = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
991 tor_free(filename);
992 if (!d)
993 return -1;
994 lines = smartlist_create();
995 smartlist_split_string(lines, d, "\n", SPLIT_SKIP_SPACE, 0);
996 tor_free(d);
1000 const char *firstline;
1001 if (smartlist_len(lines)>4) {
1002 firstline = smartlist_get(lines, 0);
1003 if (!strcmpstart(firstline, "format "))
1004 format = tor_parse_long(firstline+strlen("format "),
1005 10, -1, LONG_MAX, NULL, NULL);
1008 if (format != 1 && format != 2) {
1009 log_warn(LD_HIST,
1010 "Unrecognized format in mtbf history file. Skipping.");
1011 goto err;
1013 for (i = 1; i < smartlist_len(lines); ++i) {
1014 line = smartlist_get(lines, i);
1015 if (!strcmp(line, "data"))
1016 break;
1017 if (!strcmpstart(line, "last-downrated ")) {
1018 if (parse_iso_time(line+strlen("last-downrated "), &last_downrated)<0)
1019 log_warn(LD_HIST,"Couldn't parse downrate time in mtbf "
1020 "history file.");
1022 if (!strcmpstart(line, "stored-at ")) {
1023 if (parse_iso_time(line+strlen("stored-at "), &stored_at)<0)
1024 log_warn(LD_HIST,"Couldn't parse stored time in mtbf "
1025 "history file.");
1027 if (!strcmpstart(line, "tracked-since ")) {
1028 if (parse_iso_time(line+strlen("tracked-since "), &tracked_since)<0)
1029 log_warn(LD_HIST,"Couldn't parse started-tracking time in mtbf "
1030 "history file.");
1033 if (last_downrated > now)
1034 last_downrated = now;
1035 if (tracked_since > now)
1036 tracked_since = now;
1038 if (!stored_at) {
1039 log_warn(LD_HIST, "No stored time recorded.");
1040 goto err;
1043 if (line && !strcmp(line, "data"))
1044 ++i;
1046 n_bogus_times = 0;
1048 for (; i < smartlist_len(lines); ++i) {
1049 char digest[DIGEST_LEN];
1050 char hexbuf[HEX_DIGEST_LEN+1];
1051 char mtbf_timebuf[ISO_TIME_LEN+1];
1052 char wfu_timebuf[ISO_TIME_LEN+1];
1053 time_t start_of_run = 0;
1054 time_t start_of_downtime = 0;
1055 int have_mtbf = 0, have_wfu = 0;
1056 long wrl = 0;
1057 double trw = 0;
1058 long wt_uptime = 0, total_wt_time = 0;
1059 int n;
1060 or_history_t *hist;
1061 line = smartlist_get(lines, i);
1062 if (!strcmp(line, "."))
1063 break;
1065 mtbf_timebuf[0] = '\0';
1066 wfu_timebuf[0] = '\0';
1068 if (format == 1) {
1069 n = sscanf(line, "%40s %ld %lf S=%10s %8s",
1070 hexbuf, &wrl, &trw, mtbf_timebuf, mtbf_timebuf+11);
1071 if (n != 3 && n != 5) {
1072 log_warn(LD_HIST, "Couldn't scan line %s", escaped(line));
1073 continue;
1075 have_mtbf = 1;
1076 } else {
1077 // format == 2.
1078 int mtbf_idx, wfu_idx;
1079 if (strcmpstart(line, "R ") || strlen(line) < 2+HEX_DIGEST_LEN)
1080 continue;
1081 strlcpy(hexbuf, line+2, sizeof(hexbuf));
1082 mtbf_idx = find_next_with(lines, i+1, "+MTBF ");
1083 wfu_idx = find_next_with(lines, i+1, "+WFU ");
1084 if (mtbf_idx >= 0) {
1085 const char *mtbfline = smartlist_get(lines, mtbf_idx);
1086 n = sscanf(mtbfline, "+MTBF %lu %lf S=%10s %8s",
1087 &wrl, &trw, mtbf_timebuf, mtbf_timebuf+11);
1088 if (n == 2 || n == 4) {
1089 have_mtbf = 1;
1090 } else {
1091 log_warn(LD_HIST, "Couldn't scan +MTBF line %s",
1092 escaped(mtbfline));
1095 if (wfu_idx >= 0) {
1096 const char *wfuline = smartlist_get(lines, wfu_idx);
1097 n = sscanf(wfuline, "+WFU %lu %lu S=%10s %8s",
1098 &wt_uptime, &total_wt_time,
1099 wfu_timebuf, wfu_timebuf+11);
1100 if (n == 2 || n == 4) {
1101 have_wfu = 1;
1102 } else {
1103 log_warn(LD_HIST, "Couldn't scan +WFU line %s", escaped(wfuline));
1106 if (wfu_idx > i)
1107 i = wfu_idx;
1108 if (mtbf_idx > i)
1109 i = mtbf_idx;
1111 if (base16_decode(digest, DIGEST_LEN, hexbuf, HEX_DIGEST_LEN) < 0) {
1112 log_warn(LD_HIST, "Couldn't hex string %s", escaped(hexbuf));
1113 continue;
1115 hist = get_or_history(digest);
1116 if (!hist)
1117 continue;
1119 if (have_mtbf) {
1120 if (mtbf_timebuf[0]) {
1121 mtbf_timebuf[10] = ' ';
1122 if (parse_possibly_bad_iso_time(mtbf_timebuf, &start_of_run)<0)
1123 log_warn(LD_HIST, "Couldn't parse time %s",
1124 escaped(mtbf_timebuf));
1126 hist->start_of_run = correct_time(start_of_run, now, stored_at,
1127 tracked_since);
1128 if (hist->start_of_run < latest_possible_start + wrl)
1129 latest_possible_start = hist->start_of_run - wrl;
1131 hist->weighted_run_length = wrl;
1132 hist->total_run_weights = trw;
1134 if (have_wfu) {
1135 if (wfu_timebuf[0]) {
1136 wfu_timebuf[10] = ' ';
1137 if (parse_possibly_bad_iso_time(wfu_timebuf, &start_of_downtime)<0)
1138 log_warn(LD_HIST, "Couldn't parse time %s", escaped(wfu_timebuf));
1141 hist->start_of_downtime = correct_time(start_of_downtime, now, stored_at,
1142 tracked_since);
1143 hist->weighted_uptime = wt_uptime;
1144 hist->total_weighted_time = total_wt_time;
1146 if (strcmp(line, "."))
1147 log_warn(LD_HIST, "Truncated MTBF file.");
1149 if (tracked_since < 86400*365) /* Recover from insanely early value. */
1150 tracked_since = latest_possible_start;
1152 stability_last_downrated = last_downrated;
1153 started_tracking_stability = tracked_since;
1155 goto done;
1156 err:
1157 r = -1;
1158 done:
1159 SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
1160 smartlist_free(lines);
1161 return r;
1164 /** For how many seconds do we keep track of individual per-second bandwidth
1165 * totals? */
1166 #define NUM_SECS_ROLLING_MEASURE 10
1167 /** How large are the intervals for which we track and report bandwidth use? */
1168 #define NUM_SECS_BW_SUM_INTERVAL (15*60)
1169 /** How far in the past do we remember and publish bandwidth use? */
1170 #define NUM_SECS_BW_SUM_IS_VALID (24*60*60)
1171 /** How many bandwidth usage intervals do we remember? (derived) */
1172 #define NUM_TOTALS (NUM_SECS_BW_SUM_IS_VALID/NUM_SECS_BW_SUM_INTERVAL)
1174 /** Structure to track bandwidth use, and remember the maxima for a given
1175 * time period.
1177 typedef struct bw_array_t {
1178 /** Observation array: Total number of bytes transferred in each of the last
1179 * NUM_SECS_ROLLING_MEASURE seconds. This is used as a circular array. */
1180 uint64_t obs[NUM_SECS_ROLLING_MEASURE];
1181 int cur_obs_idx; /**< Current position in obs. */
1182 time_t cur_obs_time; /**< Time represented in obs[cur_obs_idx] */
1183 uint64_t total_obs; /**< Total for all members of obs except
1184 * obs[cur_obs_idx] */
1185 uint64_t max_total; /**< Largest value that total_obs has taken on in the
1186 * current period. */
1187 uint64_t total_in_period; /**< Total bytes transferred in the current
1188 * period. */
1190 /** When does the next period begin? */
1191 time_t next_period;
1192 /** Where in 'maxima' should the maximum bandwidth usage for the current
1193 * period be stored? */
1194 int next_max_idx;
1195 /** How many values in maxima/totals have been set ever? */
1196 int num_maxes_set;
1197 /** Circular array of the maximum
1198 * bandwidth-per-NUM_SECS_ROLLING_MEASURE usage for the last
1199 * NUM_TOTALS periods */
1200 uint64_t maxima[NUM_TOTALS];
1201 /** Circular array of the total bandwidth usage for the last NUM_TOTALS
1202 * periods */
1203 uint64_t totals[NUM_TOTALS];
1204 } bw_array_t;
1206 /** Shift the current period of b forward by one. */
1207 static void
1208 commit_max(bw_array_t *b)
1210 /* Store total from current period. */
1211 b->totals[b->next_max_idx] = b->total_in_period;
1212 /* Store maximum from current period. */
1213 b->maxima[b->next_max_idx++] = b->max_total;
1214 /* Advance next_period and next_max_idx */
1215 b->next_period += NUM_SECS_BW_SUM_INTERVAL;
1216 if (b->next_max_idx == NUM_TOTALS)
1217 b->next_max_idx = 0;
1218 if (b->num_maxes_set < NUM_TOTALS)
1219 ++b->num_maxes_set;
1220 /* Reset max_total. */
1221 b->max_total = 0;
1222 /* Reset total_in_period. */
1223 b->total_in_period = 0;
1226 /** Shift the current observation time of 'b' forward by one second. */
1227 static INLINE void
1228 advance_obs(bw_array_t *b)
1230 int nextidx;
1231 uint64_t total;
1233 /* Calculate the total bandwidth for the last NUM_SECS_ROLLING_MEASURE
1234 * seconds; adjust max_total as needed.*/
1235 total = b->total_obs + b->obs[b->cur_obs_idx];
1236 if (total > b->max_total)
1237 b->max_total = total;
1239 nextidx = b->cur_obs_idx+1;
1240 if (nextidx == NUM_SECS_ROLLING_MEASURE)
1241 nextidx = 0;
1243 b->total_obs = total - b->obs[nextidx];
1244 b->obs[nextidx]=0;
1245 b->cur_obs_idx = nextidx;
1247 if (++b->cur_obs_time >= b->next_period)
1248 commit_max(b);
1251 /** Add <b>n</b> bytes to the number of bytes in <b>b</b> for second
1252 * <b>when</b>. */
1253 static INLINE void
1254 add_obs(bw_array_t *b, time_t when, uint64_t n)
1256 /* Don't record data in the past. */
1257 if (when<b->cur_obs_time)
1258 return;
1259 /* If we're currently adding observations for an earlier second than
1260 * 'when', advance b->cur_obs_time and b->cur_obs_idx by an
1261 * appropriate number of seconds, and do all the other housekeeping */
1262 while (when>b->cur_obs_time)
1263 advance_obs(b);
1265 b->obs[b->cur_obs_idx] += n;
1266 b->total_in_period += n;
1269 /** Allocate, initialize, and return a new bw_array. */
1270 static bw_array_t *
1271 bw_array_new(void)
1273 bw_array_t *b;
1274 time_t start;
1275 b = tor_malloc_zero(sizeof(bw_array_t));
1276 rephist_total_alloc += sizeof(bw_array_t);
1277 start = time(NULL);
1278 b->cur_obs_time = start;
1279 b->next_period = start + NUM_SECS_BW_SUM_INTERVAL;
1280 return b;
1283 /** Recent history of bandwidth observations for read operations. */
1284 static bw_array_t *read_array = NULL;
1285 /** Recent history of bandwidth observations for write operations. */
1286 static bw_array_t *write_array = NULL;
1287 /** Recent history of bandwidth observations for read operations for the
1288 directory protocol. */
1289 static bw_array_t *dir_read_array = NULL;
1290 /** Recent history of bandwidth observations for write operations for the
1291 directory protocol. */
1292 static bw_array_t *dir_write_array = NULL;
1294 /** Set up [dir-]read_array and [dir-]write_array. */
1295 static void
1296 bw_arrays_init(void)
1298 read_array = bw_array_new();
1299 write_array = bw_array_new();
1300 dir_read_array = bw_array_new();
1301 dir_write_array = bw_array_new();
1304 /** We read <b>num_bytes</b> more bytes in second <b>when</b>.
1306 * Add num_bytes to the current running total for <b>when</b>.
1308 * <b>when</b> can go back to time, but it's safe to ignore calls
1309 * earlier than the latest <b>when</b> you've heard of.
1311 void
1312 rep_hist_note_bytes_written(size_t num_bytes, time_t when)
1314 /* Maybe a circular array for recent seconds, and step to a new point
1315 * every time a new second shows up. Or simpler is to just to have
1316 * a normal array and push down each item every second; it's short.
1318 /* When a new second has rolled over, compute the sum of the bytes we've
1319 * seen over when-1 to when-1-NUM_SECS_ROLLING_MEASURE, and stick it
1320 * somewhere. See rep_hist_bandwidth_assess() below.
1322 add_obs(write_array, when, num_bytes);
1325 /** We wrote <b>num_bytes</b> more bytes in second <b>when</b>.
1326 * (like rep_hist_note_bytes_written() above)
1328 void
1329 rep_hist_note_bytes_read(size_t num_bytes, time_t when)
1331 /* if we're smart, we can make this func and the one above share code */
1332 add_obs(read_array, when, num_bytes);
1335 /** We wrote <b>num_bytes</b> more directory bytes in second <b>when</b>.
1336 * (like rep_hist_note_bytes_written() above)
1338 void
1339 rep_hist_note_dir_bytes_written(size_t num_bytes, time_t when)
1341 add_obs(dir_write_array, when, num_bytes);
1344 /** We read <b>num_bytes</b> more directory bytes in second <b>when</b>.
1345 * (like rep_hist_note_bytes_written() above)
1347 void
1348 rep_hist_note_dir_bytes_read(size_t num_bytes, time_t when)
1350 add_obs(dir_read_array, when, num_bytes);
1353 /** Helper: Return the largest value in b->maxima. (This is equal to the
1354 * most bandwidth used in any NUM_SECS_ROLLING_MEASURE period for the last
1355 * NUM_SECS_BW_SUM_IS_VALID seconds.)
1357 static uint64_t
1358 find_largest_max(bw_array_t *b)
1360 int i;
1361 uint64_t max;
1362 max=0;
1363 for (i=0; i<NUM_TOTALS; ++i) {
1364 if (b->maxima[i]>max)
1365 max = b->maxima[i];
1367 return max;
1370 /** Find the largest sums in the past NUM_SECS_BW_SUM_IS_VALID (roughly)
1371 * seconds. Find one sum for reading and one for writing. They don't have
1372 * to be at the same time.
1374 * Return the smaller of these sums, divided by NUM_SECS_ROLLING_MEASURE.
1377 rep_hist_bandwidth_assess(void)
1379 uint64_t w,r;
1380 r = find_largest_max(read_array);
1381 w = find_largest_max(write_array);
1382 if (r>w)
1383 return (int)(U64_TO_DBL(w)/NUM_SECS_ROLLING_MEASURE);
1384 else
1385 return (int)(U64_TO_DBL(r)/NUM_SECS_ROLLING_MEASURE);
1388 /** Print the bandwidth history of b (either [dir-]read_array or
1389 * [dir-]write_array) into the buffer pointed to by buf. The format is
1390 * simply comma separated numbers, from oldest to newest.
1392 * It returns the number of bytes written.
1394 static size_t
1395 rep_hist_fill_bandwidth_history(char *buf, size_t len, bw_array_t *b)
1397 char *cp = buf;
1398 int i, n;
1399 or_options_t *options = get_options();
1400 uint64_t cutoff;
1402 if (b->num_maxes_set <= b->next_max_idx) {
1403 /* We haven't been through the circular array yet; time starts at i=0.*/
1404 i = 0;
1405 } else {
1406 /* We've been around the array at least once. The next i to be
1407 overwritten is the oldest. */
1408 i = b->next_max_idx;
1411 if (options->RelayBandwidthRate) {
1412 /* We don't want to report that we used more bandwidth than the max we're
1413 * willing to relay; otherwise everybody will know how much traffic
1414 * we used ourself. */
1415 cutoff = options->RelayBandwidthRate * NUM_SECS_BW_SUM_INTERVAL;
1416 } else {
1417 cutoff = UINT64_MAX;
1420 for (n=0; n<b->num_maxes_set; ++n,++i) {
1421 uint64_t total;
1422 if (i >= NUM_TOTALS)
1423 i -= NUM_TOTALS;
1424 tor_assert(i < NUM_TOTALS);
1425 /* Round the bandwidth used down to the nearest 1k. */
1426 total = b->totals[i] & ~0x3ff;
1427 if (total > cutoff)
1428 total = cutoff;
1430 if (n==(b->num_maxes_set-1))
1431 tor_snprintf(cp, len-(cp-buf), U64_FORMAT, U64_PRINTF_ARG(total));
1432 else
1433 tor_snprintf(cp, len-(cp-buf), U64_FORMAT",", U64_PRINTF_ARG(total));
1434 cp += strlen(cp);
1436 return cp-buf;
1439 /** Allocate and return lines for representing this server's bandwidth
1440 * history in its descriptor.
1442 char *
1443 rep_hist_get_bandwidth_lines(void)
1445 char *buf, *cp;
1446 char t[ISO_TIME_LEN+1];
1447 int r;
1448 bw_array_t *b = NULL;
1449 const char *desc = NULL;
1450 size_t len;
1452 /* opt [dirreq-](read|write)-history yyyy-mm-dd HH:MM:SS (n s) n,n,n... */
1453 len = (67+21*NUM_TOTALS)*4;
1454 buf = tor_malloc_zero(len);
1455 cp = buf;
1456 for (r=0;r<4;++r) {
1457 switch (r) {
1458 case 0:
1459 b = write_array;
1460 desc = "write-history";
1461 break;
1462 case 1:
1463 b = read_array;
1464 desc = "read-history";
1465 break;
1466 case 2:
1467 b = dir_write_array;
1468 desc = "dirreq-write-history";
1469 break;
1470 case 3:
1471 b = dir_read_array;
1472 desc = "dirreq-read-history";
1473 break;
1475 tor_assert(b);
1476 format_iso_time(t, b->next_period-NUM_SECS_BW_SUM_INTERVAL);
1477 tor_snprintf(cp, len-(cp-buf), "%s %s (%d s) ",
1478 desc, t, NUM_SECS_BW_SUM_INTERVAL);
1479 cp += strlen(cp);
1480 cp += rep_hist_fill_bandwidth_history(cp, len-(cp-buf), b);
1481 strlcat(cp, "\n", len-(cp-buf));
1482 ++cp;
1484 return buf;
1487 /** Update <b>state</b> with the newest bandwidth history. */
1488 void
1489 rep_hist_update_state(or_state_t *state)
1491 int len, r;
1492 char *buf, *cp;
1493 smartlist_t **s_values = NULL;
1494 time_t *s_begins = NULL;
1495 int *s_interval = NULL;
1496 bw_array_t *b = NULL;
1498 len = 20*NUM_TOTALS+1;
1499 buf = tor_malloc_zero(len);
1501 for (r=0;r<4;++r) {
1502 switch (r) {
1503 case 0:
1504 b = write_array;
1505 s_begins = &state->BWHistoryWriteEnds;
1506 s_interval = &state->BWHistoryWriteInterval;
1507 s_values = &state->BWHistoryWriteValues;
1508 break;
1509 case 1:
1510 b = read_array;
1511 s_begins = &state->BWHistoryReadEnds;
1512 s_interval = &state->BWHistoryReadInterval;
1513 s_values = &state->BWHistoryReadValues;
1514 break;
1515 case 2:
1516 b = dir_write_array;
1517 s_begins = &state->BWHistoryDirWriteEnds;
1518 s_interval = &state->BWHistoryDirWriteInterval;
1519 s_values = &state->BWHistoryDirWriteValues;
1520 break;
1521 case 3:
1522 b = dir_read_array;
1523 s_begins = &state->BWHistoryDirReadEnds;
1524 s_interval = &state->BWHistoryDirReadInterval;
1525 s_values = &state->BWHistoryDirReadValues;
1526 break;
1528 if (*s_values) {
1529 SMARTLIST_FOREACH(*s_values, char *, val, tor_free(val));
1530 smartlist_free(*s_values);
1532 if (! server_mode(get_options())) {
1533 /* Clients don't need to store bandwidth history persistently;
1534 * force these values to the defaults. */
1535 /* FFFF we should pull the default out of config.c's state table,
1536 * so we don't have two defaults. */
1537 if (*s_begins != 0 || *s_interval != 900) {
1538 time_t now = time(NULL);
1539 time_t save_at = get_options()->AvoidDiskWrites ? now+3600 : now+600;
1540 or_state_mark_dirty(state, save_at);
1542 *s_begins = 0;
1543 *s_interval = 900;
1544 *s_values = smartlist_create();
1545 continue;
1547 *s_begins = b->next_period;
1548 *s_interval = NUM_SECS_BW_SUM_INTERVAL;
1549 cp = buf;
1550 cp += rep_hist_fill_bandwidth_history(cp, len, b);
1551 tor_snprintf(cp, len-(cp-buf), cp == buf ? U64_FORMAT : ","U64_FORMAT,
1552 U64_PRINTF_ARG(b->total_in_period));
1553 *s_values = smartlist_create();
1554 if (server_mode(get_options()))
1555 smartlist_split_string(*s_values, buf, ",", SPLIT_SKIP_SPACE, 0);
1557 tor_free(buf);
1558 if (server_mode(get_options())) {
1559 or_state_mark_dirty(get_or_state(), time(NULL)+(2*3600));
1563 /** Set bandwidth history from our saved state. */
1565 rep_hist_load_state(or_state_t *state, char **err)
1567 time_t s_begins = 0, start;
1568 time_t now = time(NULL);
1569 uint64_t v;
1570 int r,i,ok;
1571 int all_ok = 1;
1572 int s_interval = 0;
1573 smartlist_t *s_values = NULL;
1574 bw_array_t *b = NULL;
1576 /* Assert they already have been malloced */
1577 tor_assert(read_array && write_array);
1579 for (r=0;r<4;++r) {
1580 switch (r) {
1581 case 0:
1582 b = write_array;
1583 s_begins = state->BWHistoryWriteEnds;
1584 s_interval = state->BWHistoryWriteInterval;
1585 s_values = state->BWHistoryWriteValues;
1586 break;
1587 case 1:
1588 b = read_array;
1589 s_begins = state->BWHistoryReadEnds;
1590 s_interval = state->BWHistoryReadInterval;
1591 s_values = state->BWHistoryReadValues;
1592 break;
1593 case 2:
1594 b = dir_write_array;
1595 s_begins = state->BWHistoryDirWriteEnds;
1596 s_interval = state->BWHistoryDirWriteInterval;
1597 s_values = state->BWHistoryDirWriteValues;
1598 break;
1599 case 3:
1600 b = dir_read_array;
1601 s_begins = state->BWHistoryDirReadEnds;
1602 s_interval = state->BWHistoryDirReadInterval;
1603 s_values = state->BWHistoryDirReadValues;
1604 break;
1606 if (s_values && s_begins >= now - NUM_SECS_BW_SUM_INTERVAL*NUM_TOTALS) {
1607 start = s_begins - s_interval*(smartlist_len(s_values));
1608 if (start > now)
1609 continue;
1610 b->cur_obs_time = start;
1611 b->next_period = start + NUM_SECS_BW_SUM_INTERVAL;
1612 SMARTLIST_FOREACH(s_values, char *, cp, {
1613 v = tor_parse_uint64(cp, 10, 0, UINT64_MAX, &ok, NULL);
1614 if (!ok) {
1615 all_ok=0;
1616 log_notice(LD_HIST, "Could not parse '%s' into a number.'", cp);
1618 if (start < now) {
1619 add_obs(b, start, v);
1620 start += NUM_SECS_BW_SUM_INTERVAL;
1625 /* Clean up maxima and observed */
1626 /* Do we really want to zero this for the purpose of max capacity? */
1627 for (i=0; i<NUM_SECS_ROLLING_MEASURE; ++i) {
1628 b->obs[i] = 0;
1630 b->total_obs = 0;
1631 for (i=0; i<NUM_TOTALS; ++i) {
1632 b->maxima[i] = 0;
1634 b->max_total = 0;
1637 if (!all_ok) {
1638 *err = tor_strdup("Parsing of bandwidth history values failed");
1639 /* and create fresh arrays */
1640 tor_free(read_array);
1641 tor_free(write_array);
1642 read_array = bw_array_new();
1643 write_array = bw_array_new();
1644 return -1;
1646 return 0;
1649 /*********************************************************************/
1651 /** A list of port numbers that have been used recently. */
1652 static smartlist_t *predicted_ports_list=NULL;
1653 /** The corresponding most recently used time for each port. */
1654 static smartlist_t *predicted_ports_times=NULL;
1656 /** We just got an application request for a connection with
1657 * port <b>port</b>. Remember it for the future, so we can keep
1658 * some circuits open that will exit to this port.
1660 static void
1661 add_predicted_port(time_t now, uint16_t port)
1663 /* XXXX we could just use uintptr_t here, I think. */
1664 uint16_t *tmp_port = tor_malloc(sizeof(uint16_t));
1665 time_t *tmp_time = tor_malloc(sizeof(time_t));
1666 *tmp_port = port;
1667 *tmp_time = now;
1668 rephist_total_alloc += sizeof(uint16_t) + sizeof(time_t);
1669 smartlist_add(predicted_ports_list, tmp_port);
1670 smartlist_add(predicted_ports_times, tmp_time);
1673 /** Initialize whatever memory and structs are needed for predicting
1674 * which ports will be used. Also seed it with port 80, so we'll build
1675 * circuits on start-up.
1677 static void
1678 predicted_ports_init(void)
1680 predicted_ports_list = smartlist_create();
1681 predicted_ports_times = smartlist_create();
1682 add_predicted_port(time(NULL), 80); /* add one to kickstart us */
1685 /** Free whatever memory is needed for predicting which ports will
1686 * be used.
1688 static void
1689 predicted_ports_free(void)
1691 rephist_total_alloc -= smartlist_len(predicted_ports_list)*sizeof(uint16_t);
1692 SMARTLIST_FOREACH(predicted_ports_list, char *, cp, tor_free(cp));
1693 smartlist_free(predicted_ports_list);
1694 rephist_total_alloc -= smartlist_len(predicted_ports_times)*sizeof(time_t);
1695 SMARTLIST_FOREACH(predicted_ports_times, char *, cp, tor_free(cp));
1696 smartlist_free(predicted_ports_times);
1699 /** Remember that <b>port</b> has been asked for as of time <b>now</b>.
1700 * This is used for predicting what sorts of streams we'll make in the
1701 * future and making exit circuits to anticipate that.
1703 void
1704 rep_hist_note_used_port(time_t now, uint16_t port)
1706 int i;
1707 uint16_t *tmp_port;
1708 time_t *tmp_time;
1710 tor_assert(predicted_ports_list);
1711 tor_assert(predicted_ports_times);
1713 if (!port) /* record nothing */
1714 return;
1716 for (i = 0; i < smartlist_len(predicted_ports_list); ++i) {
1717 tmp_port = smartlist_get(predicted_ports_list, i);
1718 tmp_time = smartlist_get(predicted_ports_times, i);
1719 if (*tmp_port == port) {
1720 *tmp_time = now;
1721 return;
1724 /* it's not there yet; we need to add it */
1725 add_predicted_port(now, port);
1728 /** For this long after we've seen a request for a given port, assume that
1729 * we'll want to make connections to the same port in the future. */
1730 #define PREDICTED_CIRCS_RELEVANCE_TIME (60*60)
1732 /** Return a pointer to the list of port numbers that
1733 * are likely to be asked for in the near future.
1735 * The caller promises not to mess with it.
1737 smartlist_t *
1738 rep_hist_get_predicted_ports(time_t now)
1740 int i;
1741 uint16_t *tmp_port;
1742 time_t *tmp_time;
1744 tor_assert(predicted_ports_list);
1745 tor_assert(predicted_ports_times);
1747 /* clean out obsolete entries */
1748 for (i = 0; i < smartlist_len(predicted_ports_list); ++i) {
1749 tmp_time = smartlist_get(predicted_ports_times, i);
1750 if (*tmp_time + PREDICTED_CIRCS_RELEVANCE_TIME < now) {
1751 tmp_port = smartlist_get(predicted_ports_list, i);
1752 log_debug(LD_CIRC, "Expiring predicted port %d", *tmp_port);
1753 smartlist_del(predicted_ports_list, i);
1754 smartlist_del(predicted_ports_times, i);
1755 rephist_total_alloc -= sizeof(uint16_t)+sizeof(time_t);
1756 tor_free(tmp_port);
1757 tor_free(tmp_time);
1758 i--;
1761 return predicted_ports_list;
1764 /** The user asked us to do a resolve. Rather than keeping track of
1765 * timings and such of resolves, we fake it for now by treating
1766 * it the same way as a connection to port 80. This way we will continue
1767 * to have circuits lying around if the user only uses Tor for resolves.
1769 void
1770 rep_hist_note_used_resolve(time_t now)
1772 rep_hist_note_used_port(now, 80);
1775 /** The last time at which we needed an internal circ. */
1776 static time_t predicted_internal_time = 0;
1777 /** The last time we needed an internal circ with good uptime. */
1778 static time_t predicted_internal_uptime_time = 0;
1779 /** The last time we needed an internal circ with good capacity. */
1780 static time_t predicted_internal_capacity_time = 0;
1782 /** Remember that we used an internal circ at time <b>now</b>. */
1783 void
1784 rep_hist_note_used_internal(time_t now, int need_uptime, int need_capacity)
1786 predicted_internal_time = now;
1787 if (need_uptime)
1788 predicted_internal_uptime_time = now;
1789 if (need_capacity)
1790 predicted_internal_capacity_time = now;
1793 /** Return 1 if we've used an internal circ recently; else return 0. */
1795 rep_hist_get_predicted_internal(time_t now, int *need_uptime,
1796 int *need_capacity)
1798 if (!predicted_internal_time) { /* initialize it */
1799 predicted_internal_time = now;
1800 predicted_internal_uptime_time = now;
1801 predicted_internal_capacity_time = now;
1803 if (predicted_internal_time + PREDICTED_CIRCS_RELEVANCE_TIME < now)
1804 return 0; /* too long ago */
1805 if (predicted_internal_uptime_time + PREDICTED_CIRCS_RELEVANCE_TIME >= now)
1806 *need_uptime = 1;
1807 // Always predict that we need capacity.
1808 *need_capacity = 1;
1809 return 1;
1812 /** Any ports used lately? These are pre-seeded if we just started
1813 * up or if we're running a hidden service. */
1815 any_predicted_circuits(time_t now)
1817 return smartlist_len(predicted_ports_list) ||
1818 predicted_internal_time + PREDICTED_CIRCS_RELEVANCE_TIME >= now;
1821 /** Return 1 if we have no need for circuits currently, else return 0. */
1823 rep_hist_circbuilding_dormant(time_t now)
1825 if (any_predicted_circuits(now))
1826 return 0;
1828 /* see if we'll still need to build testing circuits */
1829 if (server_mode(get_options()) &&
1830 (!check_whether_orport_reachable() || !circuit_enough_testing_circs()))
1831 return 0;
1832 if (!check_whether_dirport_reachable())
1833 return 0;
1835 return 1;
1838 /** Structure to track how many times we've done each public key operation. */
1839 static struct {
1840 /** How many directory objects have we signed? */
1841 unsigned long n_signed_dir_objs;
1842 /** How many routerdescs have we signed? */
1843 unsigned long n_signed_routerdescs;
1844 /** How many directory objects have we verified? */
1845 unsigned long n_verified_dir_objs;
1846 /** How many routerdescs have we verified */
1847 unsigned long n_verified_routerdescs;
1848 /** How many onionskins have we encrypted to build circuits? */
1849 unsigned long n_onionskins_encrypted;
1850 /** How many onionskins have we decrypted to do circuit build requests? */
1851 unsigned long n_onionskins_decrypted;
1852 /** How many times have we done the TLS handshake as a client? */
1853 unsigned long n_tls_client_handshakes;
1854 /** How many times have we done the TLS handshake as a server? */
1855 unsigned long n_tls_server_handshakes;
1856 /** How many PK operations have we done as a hidden service client? */
1857 unsigned long n_rend_client_ops;
1858 /** How many PK operations have we done as a hidden service midpoint? */
1859 unsigned long n_rend_mid_ops;
1860 /** How many PK operations have we done as a hidden service provider? */
1861 unsigned long n_rend_server_ops;
1862 } pk_op_counts = {0,0,0,0,0,0,0,0,0,0,0};
1864 /** Increment the count of the number of times we've done <b>operation</b>. */
1865 void
1866 note_crypto_pk_op(pk_op_t operation)
1868 switch (operation)
1870 case SIGN_DIR:
1871 pk_op_counts.n_signed_dir_objs++;
1872 break;
1873 case SIGN_RTR:
1874 pk_op_counts.n_signed_routerdescs++;
1875 break;
1876 case VERIFY_DIR:
1877 pk_op_counts.n_verified_dir_objs++;
1878 break;
1879 case VERIFY_RTR:
1880 pk_op_counts.n_verified_routerdescs++;
1881 break;
1882 case ENC_ONIONSKIN:
1883 pk_op_counts.n_onionskins_encrypted++;
1884 break;
1885 case DEC_ONIONSKIN:
1886 pk_op_counts.n_onionskins_decrypted++;
1887 break;
1888 case TLS_HANDSHAKE_C:
1889 pk_op_counts.n_tls_client_handshakes++;
1890 break;
1891 case TLS_HANDSHAKE_S:
1892 pk_op_counts.n_tls_server_handshakes++;
1893 break;
1894 case REND_CLIENT:
1895 pk_op_counts.n_rend_client_ops++;
1896 break;
1897 case REND_MID:
1898 pk_op_counts.n_rend_mid_ops++;
1899 break;
1900 case REND_SERVER:
1901 pk_op_counts.n_rend_server_ops++;
1902 break;
1903 default:
1904 log_warn(LD_BUG, "Unknown pk operation %d", operation);
1908 /** Log the number of times we've done each public/private-key operation. */
1909 void
1910 dump_pk_ops(int severity)
1912 log(severity, LD_HIST,
1913 "PK operations: %lu directory objects signed, "
1914 "%lu directory objects verified, "
1915 "%lu routerdescs signed, "
1916 "%lu routerdescs verified, "
1917 "%lu onionskins encrypted, "
1918 "%lu onionskins decrypted, "
1919 "%lu client-side TLS handshakes, "
1920 "%lu server-side TLS handshakes, "
1921 "%lu rendezvous client operations, "
1922 "%lu rendezvous middle operations, "
1923 "%lu rendezvous server operations.",
1924 pk_op_counts.n_signed_dir_objs,
1925 pk_op_counts.n_verified_dir_objs,
1926 pk_op_counts.n_signed_routerdescs,
1927 pk_op_counts.n_verified_routerdescs,
1928 pk_op_counts.n_onionskins_encrypted,
1929 pk_op_counts.n_onionskins_decrypted,
1930 pk_op_counts.n_tls_client_handshakes,
1931 pk_op_counts.n_tls_server_handshakes,
1932 pk_op_counts.n_rend_client_ops,
1933 pk_op_counts.n_rend_mid_ops,
1934 pk_op_counts.n_rend_server_ops);
1937 /*** Exit port statistics ***/
1939 /* Some constants */
1940 /** To what multiple should byte numbers be rounded up? */
1941 #define EXIT_STATS_ROUND_UP_BYTES 1024
1942 /** To what multiple should stream counts be rounded up? */
1943 #define EXIT_STATS_ROUND_UP_STREAMS 4
1944 /** Number of TCP ports */
1945 #define EXIT_STATS_NUM_PORTS 65536
1946 /** Top n ports that will be included in exit stats. */
1947 #define EXIT_STATS_TOP_N_PORTS 10
1949 /* The following data structures are arrays and no fancy smartlists or maps,
1950 * so that all write operations can be done in constant time. This comes at
1951 * the price of some memory (1.25 MB) and linear complexity when writing
1952 * stats for measuring relays. */
1953 /** Number of bytes read in current period by exit port */
1954 static uint64_t *exit_bytes_read = NULL;
1955 /** Number of bytes written in current period by exit port */
1956 static uint64_t *exit_bytes_written = NULL;
1957 /** Number of streams opened in current period by exit port */
1958 static uint32_t *exit_streams = NULL;
1960 /** Start time of exit stats or 0 if we're not collecting exit stats. */
1961 static time_t start_of_exit_stats_interval;
1963 /** Initialize exit port stats. */
1964 void
1965 rep_hist_exit_stats_init(time_t now)
1967 start_of_exit_stats_interval = now;
1968 exit_bytes_read = tor_malloc_zero(EXIT_STATS_NUM_PORTS *
1969 sizeof(uint64_t));
1970 exit_bytes_written = tor_malloc_zero(EXIT_STATS_NUM_PORTS *
1971 sizeof(uint64_t));
1972 exit_streams = tor_malloc_zero(EXIT_STATS_NUM_PORTS *
1973 sizeof(uint32_t));
1976 /** Reset counters for exit port statistics. */
1977 void
1978 rep_hist_reset_exit_stats(time_t now)
1980 start_of_exit_stats_interval = now;
1981 memset(exit_bytes_read, 0, EXIT_STATS_NUM_PORTS * sizeof(uint64_t));
1982 memset(exit_bytes_written, 0, EXIT_STATS_NUM_PORTS * sizeof(uint64_t));
1983 memset(exit_streams, 0, EXIT_STATS_NUM_PORTS * sizeof(uint32_t));
1986 /** Stop collecting exit port stats in a way that we can re-start doing
1987 * so in rep_hist_exit_stats_init(). */
1988 void
1989 rep_hist_exit_stats_term(void)
1991 start_of_exit_stats_interval = 0;
1992 tor_free(exit_bytes_read);
1993 tor_free(exit_bytes_written);
1994 tor_free(exit_streams);
1997 /** Helper: compare two ints. */
1998 static int
1999 _compare_int(const void *x, const void *y) {
2000 return (*(int*)x - *(int*)y);
2003 /** Return a newly allocated string containing the exit port statistics
2004 * until <b>now</b>, or NULL if we're not collecting exit stats. */
2005 char *
2006 rep_hist_format_exit_stats(time_t now)
2008 int i, j, top_elements = 0, cur_min_idx = 0, cur_port;
2009 uint64_t top_bytes[EXIT_STATS_TOP_N_PORTS];
2010 int top_ports[EXIT_STATS_TOP_N_PORTS];
2011 uint64_t cur_bytes = 0, other_read = 0, other_written = 0,
2012 total_read = 0, total_written = 0;
2013 uint32_t total_streams = 0, other_streams = 0;
2014 char *buf;
2015 smartlist_t *written_strings, *read_strings, *streams_strings;
2016 char *written_string, *read_string, *streams_string;
2017 char t[ISO_TIME_LEN+1];
2018 char *result;
2020 if (!start_of_exit_stats_interval)
2021 return NULL; /* Not initialized. */
2023 /* Go through all ports to find the n ports that saw most written and
2024 * read bytes. */
2025 for (i = 1; i < EXIT_STATS_NUM_PORTS; i++) {
2026 total_read += exit_bytes_read[i];
2027 total_written += exit_bytes_written[i];
2028 total_streams += exit_streams[i];
2029 cur_bytes = exit_bytes_read[i] + exit_bytes_written[i];
2030 if (cur_bytes == 0) {
2031 continue;
2033 if (top_elements < EXIT_STATS_TOP_N_PORTS) {
2034 top_bytes[top_elements] = cur_bytes;
2035 top_ports[top_elements++] = i;
2036 } else if (cur_bytes > top_bytes[cur_min_idx]) {
2037 top_bytes[cur_min_idx] = cur_bytes;
2038 top_ports[cur_min_idx] = i;
2039 } else {
2040 continue;
2042 cur_min_idx = 0;
2043 for (j = 1; j < top_elements; j++) {
2044 if (top_bytes[j] < top_bytes[cur_min_idx]) {
2045 cur_min_idx = j;
2050 /* Add observations of top ports to smartlists. */
2051 written_strings = smartlist_create();
2052 read_strings = smartlist_create();
2053 streams_strings = smartlist_create();
2054 other_read = total_read;
2055 other_written = total_written;
2056 other_streams = total_streams;
2057 qsort(top_ports, top_elements, sizeof(int), _compare_int);
2058 for (j = 0; j < top_elements; j++) {
2059 cur_port = top_ports[j];
2060 if (exit_bytes_written[cur_port] > 0) {
2061 uint64_t num = round_uint64_to_next_multiple_of(
2062 exit_bytes_written[cur_port],
2063 EXIT_STATS_ROUND_UP_BYTES);
2064 num /= 1024;
2065 buf = NULL;
2066 tor_asprintf(&buf, "%d="U64_FORMAT, cur_port, U64_PRINTF_ARG(num));
2067 smartlist_add(written_strings, buf);
2068 other_written -= exit_bytes_written[cur_port];
2070 if (exit_bytes_read[cur_port] > 0) {
2071 uint64_t num = round_uint64_to_next_multiple_of(
2072 exit_bytes_read[cur_port],
2073 EXIT_STATS_ROUND_UP_BYTES);
2074 num /= 1024;
2075 buf = NULL;
2076 tor_asprintf(&buf, "%d="U64_FORMAT, cur_port, U64_PRINTF_ARG(num));
2077 smartlist_add(read_strings, buf);
2078 other_read -= exit_bytes_read[cur_port];
2080 if (exit_streams[cur_port] > 0) {
2081 uint32_t num = round_uint32_to_next_multiple_of(
2082 exit_streams[cur_port],
2083 EXIT_STATS_ROUND_UP_STREAMS);
2084 buf = NULL;
2085 tor_asprintf(&buf, "%d=%u", cur_port, num);
2086 smartlist_add(streams_strings, buf);
2087 other_streams -= exit_streams[cur_port];
2091 /* Add observations of other ports in a single element. */
2092 other_written = round_uint64_to_next_multiple_of(other_written,
2093 EXIT_STATS_ROUND_UP_BYTES);
2094 other_written /= 1024;
2095 buf = NULL;
2096 tor_asprintf(&buf, "other="U64_FORMAT, U64_PRINTF_ARG(other_written));
2097 smartlist_add(written_strings, buf);
2098 other_read = round_uint64_to_next_multiple_of(other_read,
2099 EXIT_STATS_ROUND_UP_BYTES);
2100 other_read /= 1024;
2101 buf = NULL;
2102 tor_asprintf(&buf, "other="U64_FORMAT, U64_PRINTF_ARG(other_read));
2103 smartlist_add(read_strings, buf);
2104 other_streams = round_uint32_to_next_multiple_of(other_streams,
2105 EXIT_STATS_ROUND_UP_STREAMS);
2106 buf = NULL;
2107 tor_asprintf(&buf, "other=%u", other_streams);
2108 smartlist_add(streams_strings, buf);
2110 /* Join all observations in single strings. */
2111 written_string = smartlist_join_strings(written_strings, ",", 0, NULL);
2112 read_string = smartlist_join_strings(read_strings, ",", 0, NULL);
2113 streams_string = smartlist_join_strings(streams_strings, ",", 0, NULL);
2114 SMARTLIST_FOREACH(written_strings, char *, cp, tor_free(cp));
2115 SMARTLIST_FOREACH(read_strings, char *, cp, tor_free(cp));
2116 SMARTLIST_FOREACH(streams_strings, char *, cp, tor_free(cp));
2117 smartlist_free(written_strings);
2118 smartlist_free(read_strings);
2119 smartlist_free(streams_strings);
2121 /* Put everything together. */
2122 format_iso_time(t, now);
2123 tor_asprintf(&result, "exit-stats-end %s (%d s)\n"
2124 "exit-kibibytes-written %s\n"
2125 "exit-kibibytes-read %s\n"
2126 "exit-streams-opened %s\n",
2127 t, (unsigned) (now - start_of_exit_stats_interval),
2128 written_string,
2129 read_string,
2130 streams_string);
2131 tor_free(written_string);
2132 tor_free(read_string);
2133 tor_free(streams_string);
2134 return result;
2137 /** If 24 hours have passed since the beginning of the current exit port
2138 * stats period, write exit stats to $DATADIR/stats/exit-stats (possibly
2139 * overwriting an existing file) and reset counters. Return when we would
2140 * next want to write exit stats or 0 if we never want to write. */
2141 time_t
2142 rep_hist_exit_stats_write(time_t now)
2144 char *statsdir = NULL, *filename = NULL, *str = NULL;
2146 if (!start_of_exit_stats_interval)
2147 return 0; /* Not initialized. */
2148 if (start_of_exit_stats_interval + WRITE_STATS_INTERVAL > now)
2149 goto done; /* Not ready to write. */
2151 log_info(LD_HIST, "Writing exit port statistics to disk.");
2153 /* Generate history string. */
2154 str = rep_hist_format_exit_stats(now);
2156 /* Reset counters. */
2157 rep_hist_reset_exit_stats(now);
2159 /* Try to write to disk. */
2160 statsdir = get_datadir_fname("stats");
2161 if (check_private_dir(statsdir, CPD_CREATE) < 0) {
2162 log_warn(LD_HIST, "Unable to create stats/ directory!");
2163 goto done;
2165 filename = get_datadir_fname2("stats", "exit-stats");
2166 if (write_str_to_file(filename, str, 0) < 0)
2167 log_warn(LD_HIST, "Unable to write exit port statistics to disk!");
2169 done:
2170 tor_free(str);
2171 tor_free(statsdir);
2172 tor_free(filename);
2173 return start_of_exit_stats_interval + WRITE_STATS_INTERVAL;
2176 /** Note that we wrote <b>num_written</b> bytes and read <b>num_read</b>
2177 * bytes to/from an exit connection to <b>port</b>. */
2178 void
2179 rep_hist_note_exit_bytes(uint16_t port, size_t num_written,
2180 size_t num_read)
2182 if (!start_of_exit_stats_interval)
2183 return; /* Not initialized. */
2184 exit_bytes_written[port] += num_written;
2185 exit_bytes_read[port] += num_read;
2186 log_debug(LD_HIST, "Written %lu bytes and read %lu bytes to/from an "
2187 "exit connection to port %d.",
2188 (unsigned long)num_written, (unsigned long)num_read, port);
2191 /** Note that we opened an exit stream to <b>port</b>. */
2192 void
2193 rep_hist_note_exit_stream_opened(uint16_t port)
2195 if (!start_of_exit_stats_interval)
2196 return; /* Not initialized. */
2197 exit_streams[port]++;
2198 log_debug(LD_HIST, "Opened exit stream to port %d", port);
2201 /*** cell statistics ***/
2203 /** Start of the current buffer stats interval or 0 if we're not
2204 * collecting buffer statistics. */
2205 static time_t start_of_buffer_stats_interval;
2207 /** Initialize buffer stats. */
2208 void
2209 rep_hist_buffer_stats_init(time_t now)
2211 start_of_buffer_stats_interval = now;
2214 typedef struct circ_buffer_stats_t {
2215 uint32_t processed_cells;
2216 double mean_num_cells_in_queue;
2217 double mean_time_cells_in_queue;
2218 uint32_t local_circ_id;
2219 } circ_buffer_stats_t;
2221 /** Holds stats. */
2222 smartlist_t *circuits_for_buffer_stats = NULL;
2224 /** Remember cell statistics for circuit <b>circ</b> at time
2225 * <b>end_of_interval</b> and reset cell counters in case the circuit
2226 * remains open in the next measurement interval. */
2227 void
2228 rep_hist_buffer_stats_add_circ(circuit_t *circ, time_t end_of_interval)
2230 circ_buffer_stats_t *stat;
2231 time_t start_of_interval;
2232 int interval_length;
2233 or_circuit_t *orcirc;
2234 if (CIRCUIT_IS_ORIGIN(circ))
2235 return;
2236 orcirc = TO_OR_CIRCUIT(circ);
2237 if (!orcirc->processed_cells)
2238 return;
2239 if (!circuits_for_buffer_stats)
2240 circuits_for_buffer_stats = smartlist_create();
2241 start_of_interval = circ->timestamp_created >
2242 start_of_buffer_stats_interval ?
2243 circ->timestamp_created :
2244 start_of_buffer_stats_interval;
2245 interval_length = (int) (end_of_interval - start_of_interval);
2246 stat = tor_malloc_zero(sizeof(circ_buffer_stats_t));
2247 stat->processed_cells = orcirc->processed_cells;
2248 /* 1000.0 for s -> ms; 2.0 because of app-ward and exit-ward queues */
2249 stat->mean_num_cells_in_queue = interval_length == 0 ? 0.0 :
2250 (double) orcirc->total_cell_waiting_time /
2251 (double) interval_length / 1000.0 / 2.0;
2252 stat->mean_time_cells_in_queue =
2253 (double) orcirc->total_cell_waiting_time /
2254 (double) orcirc->processed_cells;
2255 smartlist_add(circuits_for_buffer_stats, stat);
2256 orcirc->total_cell_waiting_time = 0;
2257 orcirc->processed_cells = 0;
2260 /** Sorting helper: return -1, 1, or 0 based on comparison of two
2261 * circ_buffer_stats_t */
2262 static int
2263 _buffer_stats_compare_entries(const void **_a, const void **_b)
2265 const circ_buffer_stats_t *a = *_a, *b = *_b;
2266 if (a->processed_cells < b->processed_cells)
2267 return 1;
2268 else if (a->processed_cells > b->processed_cells)
2269 return -1;
2270 else
2271 return 0;
2274 /** Stop collecting cell stats in a way that we can re-start doing so in
2275 * rep_hist_buffer_stats_init(). */
2276 void
2277 rep_hist_buffer_stats_term(void)
2279 start_of_buffer_stats_interval = 0;
2280 if (!circuits_for_buffer_stats)
2281 circuits_for_buffer_stats = smartlist_create();
2282 SMARTLIST_FOREACH(circuits_for_buffer_stats, circ_buffer_stats_t *,
2283 stat, tor_free(stat));
2284 smartlist_clear(circuits_for_buffer_stats);
2287 /** Write buffer statistics to $DATADIR/stats/buffer-stats and return when
2288 * we would next want to write exit stats. */
2289 time_t
2290 rep_hist_buffer_stats_write(time_t now)
2292 char *statsdir = NULL, *filename = NULL;
2293 char written[ISO_TIME_LEN+1];
2294 open_file_t *open_file = NULL;
2295 FILE *out;
2296 #define SHARES 10
2297 int processed_cells[SHARES], circs_in_share[SHARES],
2298 number_of_circuits, i;
2299 double queued_cells[SHARES], time_in_queue[SHARES];
2300 smartlist_t *str_build = smartlist_create();
2301 char *str = NULL, *buf=NULL;
2302 circuit_t *circ;
2304 if (!start_of_buffer_stats_interval)
2305 return 0; /* Not initialized. */
2306 if (start_of_buffer_stats_interval + WRITE_STATS_INTERVAL > now)
2307 goto done; /* Not ready to write */
2309 /* add current circuits to stats */
2310 for (circ = _circuit_get_global_list(); circ; circ = circ->next)
2311 rep_hist_buffer_stats_add_circ(circ, now);
2312 /* calculate deciles */
2313 memset(processed_cells, 0, SHARES * sizeof(int));
2314 memset(circs_in_share, 0, SHARES * sizeof(int));
2315 memset(queued_cells, 0, SHARES * sizeof(double));
2316 memset(time_in_queue, 0, SHARES * sizeof(double));
2317 if (!circuits_for_buffer_stats)
2318 circuits_for_buffer_stats = smartlist_create();
2319 smartlist_sort(circuits_for_buffer_stats,
2320 _buffer_stats_compare_entries);
2321 number_of_circuits = smartlist_len(circuits_for_buffer_stats);
2322 if (number_of_circuits < 1) {
2323 log_info(LD_HIST, "Attempt to write cell statistics to disk failed. "
2324 "We haven't seen a single circuit to report about.");
2325 goto done;
2327 i = 0;
2328 SMARTLIST_FOREACH_BEGIN(circuits_for_buffer_stats,
2329 circ_buffer_stats_t *, stat)
2331 int share = i++ * SHARES / number_of_circuits;
2332 processed_cells[share] += stat->processed_cells;
2333 queued_cells[share] += stat->mean_num_cells_in_queue;
2334 time_in_queue[share] += stat->mean_time_cells_in_queue;
2335 circs_in_share[share]++;
2337 SMARTLIST_FOREACH_END(stat);
2338 /* clear buffer stats history */
2339 SMARTLIST_FOREACH(circuits_for_buffer_stats, circ_buffer_stats_t *,
2340 stat, tor_free(stat));
2341 smartlist_clear(circuits_for_buffer_stats);
2342 /* write to file */
2343 statsdir = get_datadir_fname("stats");
2344 if (check_private_dir(statsdir, CPD_CREATE) < 0)
2345 goto done;
2346 filename = get_datadir_fname2("stats", "buffer-stats");
2347 out = start_writing_to_stdio_file(filename, OPEN_FLAGS_APPEND,
2348 0600, &open_file);
2349 if (!out)
2350 goto done;
2351 format_iso_time(written, now);
2352 if (fprintf(out, "cell-stats-end %s (%d s)\n", written,
2353 (unsigned) (now - start_of_buffer_stats_interval)) < 0)
2354 goto done;
2355 for (i = 0; i < SHARES; i++) {
2356 tor_asprintf(&buf,"%d", !circs_in_share[i] ? 0 :
2357 processed_cells[i] / circs_in_share[i]);
2358 smartlist_add(str_build, buf);
2360 str = smartlist_join_strings(str_build, ",", 0, NULL);
2361 if (fprintf(out, "cell-processed-cells %s\n", str) < 0)
2362 goto done;
2363 tor_free(str);
2364 SMARTLIST_FOREACH(str_build, char *, c, tor_free(c));
2365 smartlist_clear(str_build);
2366 for (i = 0; i < SHARES; i++) {
2367 tor_asprintf(&buf, "%.2f", circs_in_share[i] == 0 ? 0.0 :
2368 queued_cells[i] / (double) circs_in_share[i]);
2369 smartlist_add(str_build, buf);
2371 str = smartlist_join_strings(str_build, ",", 0, NULL);
2372 if (fprintf(out, "cell-queued-cells %s\n", str) < 0)
2373 goto done;
2374 tor_free(str);
2375 SMARTLIST_FOREACH(str_build, char *, c, tor_free(c));
2376 smartlist_clear(str_build);
2377 for (i = 0; i < SHARES; i++) {
2378 tor_asprintf(&buf, "%.0f", circs_in_share[i] == 0 ? 0.0 :
2379 time_in_queue[i] / (double) circs_in_share[i]);
2380 smartlist_add(str_build, buf);
2382 str = smartlist_join_strings(str_build, ",", 0, NULL);
2383 if (fprintf(out, "cell-time-in-queue %s\n", str) < 0)
2384 goto done;
2385 tor_free(str);
2386 SMARTLIST_FOREACH(str_build, char *, c, tor_free(c));
2387 smartlist_free(str_build);
2388 str_build = NULL;
2389 if (fprintf(out, "cell-circuits-per-decile %d\n",
2390 (number_of_circuits + SHARES - 1) / SHARES) < 0)
2391 goto done;
2392 finish_writing_to_file(open_file);
2393 open_file = NULL;
2394 start_of_buffer_stats_interval = now;
2395 done:
2396 if (open_file)
2397 abort_writing_to_file(open_file);
2398 tor_free(filename);
2399 tor_free(statsdir);
2400 if (str_build) {
2401 SMARTLIST_FOREACH(str_build, char *, c, tor_free(c));
2402 smartlist_free(str_build);
2404 tor_free(str);
2405 #undef SHARES
2406 return start_of_buffer_stats_interval + WRITE_STATS_INTERVAL;
2409 /** Free all storage held by the OR/link history caches, by the
2410 * bandwidth history arrays, by the port history, or by statistics . */
2411 void
2412 rep_hist_free_all(void)
2414 digestmap_free(history_map, free_or_history);
2415 tor_free(read_array);
2416 tor_free(write_array);
2417 tor_free(last_stability_doc);
2418 tor_free(exit_bytes_read);
2419 tor_free(exit_bytes_written);
2420 tor_free(exit_streams);
2421 built_last_stability_doc_at = 0;
2422 predicted_ports_free();