1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2008, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
5 const char rephist_c_id
[] =
10 * \brief Basic history and "reputation" functionality to remember
11 * which servers have worked in the past, how much bandwidth we've
12 * been using, which ports we tend to want, and so on.
18 static void bw_arrays_init(void);
19 static void predicted_ports_init(void);
20 static void hs_usage_init(void);
22 /** Total number of bytes currently allocated in fields used by rephist.c. */
23 uint64_t rephist_total_alloc
=0;
24 /** Number of or_history_t objects currently allocated. */
25 uint32_t rephist_total_num
=0;
27 /** If the total weighted run count of all runs for a router ever falls
28 * below this amount, the router can be treated as having 0 MTBF. */
29 #define STABILITY_EPSILON 0.0001
30 /** Value by which to discount all old intervals for MTBF purposses. This
31 * is compounded every STABILITY_INTERVAL. */
32 #define STABILITY_ALPHA 0.95
33 /** Interval at which to discount all old intervals for MTBF purposes. */
34 #define STABILITY_INTERVAL (12*60*60)
35 /* (This combination of ALPHA, INTERVAL, and EPSILON makes it so that an
36 * interval that just ended counts twice as much as one that ended a week ago,
37 * 20X as much as one that ended a month ago, and routers that have had no
38 * uptime data for about half a year will get forgotten.) */
40 /** History of an OR-\>OR link. */
41 typedef struct link_history_t
{
42 /** When did we start tracking this list? */
44 /** When did we most recently note a change to this link */
46 /** How many times did extending from OR1 to OR2 succeed? */
47 unsigned long n_extend_ok
;
48 /** How many times did extending from OR1 to OR2 fail? */
49 unsigned long n_extend_fail
;
52 /** History of an OR. */
53 typedef struct or_history_t
{
54 /** When did we start tracking this OR? */
56 /** When did we most recently note a change to this OR? */
58 /** How many times did we successfully connect? */
59 unsigned long n_conn_ok
;
60 /** How many times did we try to connect and fail?*/
61 unsigned long n_conn_fail
;
62 /** How many seconds have we been connected to this OR before
65 /** How many seconds have we been unable to connect to this OR before
67 unsigned long downtime
;
68 /** If nonzero, we have been connected since this time. */
70 /** If nonzero, we have been unable to connect since this time. */
73 /* === For MTBF tracking: */
74 /** Weighted sum total of all times that this router has been online.
76 unsigned long weighted_run_length
;
77 /** If the router is now online (according to stability-checking rules),
78 * when did it come online? */
80 /** Sum of weights for runs in weighted_run_length. */
81 double total_run_weights
;
82 /* === For fractional uptime tracking: */
83 time_t start_of_downtime
;
84 unsigned long weighted_uptime
;
85 unsigned long total_weighted_time
;
87 /** Map from hex OR2 identity digest to a link_history_t for the link
88 * from this OR to OR2. */
89 digestmap_t
*link_history_map
;
92 /** When did we last multiply all routers' weighted_run_length and
93 * total_run_weights by STABILITY_ALPHA? */
94 static time_t stability_last_downrated
= 0;
97 static time_t started_tracking_stability
= 0;
99 /** Map from hex OR identity digest to or_history_t. */
100 static digestmap_t
*history_map
= NULL
;
102 /** Return the or_history_t for the OR with identity digest <b>id</b>,
103 * creating it if necessary. */
104 static or_history_t
*
105 get_or_history(const char* id
)
109 if (tor_mem_is_zero(id
, DIGEST_LEN
))
112 hist
= digestmap_get(history_map
, id
);
114 hist
= tor_malloc_zero(sizeof(or_history_t
));
115 rephist_total_alloc
+= sizeof(or_history_t
);
117 hist
->link_history_map
= digestmap_new();
118 hist
->since
= hist
->changed
= time(NULL
);
119 digestmap_set(history_map
, id
, hist
);
124 /** Return the link_history_t for the link from the first named OR to
125 * the second, creating it if necessary. (ORs are identified by
128 static link_history_t
*
129 get_link_history(const char *from_id
, const char *to_id
)
131 or_history_t
*orhist
;
132 link_history_t
*lhist
;
133 orhist
= get_or_history(from_id
);
136 if (tor_mem_is_zero(to_id
, DIGEST_LEN
))
138 lhist
= (link_history_t
*) digestmap_get(orhist
->link_history_map
, to_id
);
140 lhist
= tor_malloc_zero(sizeof(link_history_t
));
141 rephist_total_alloc
+= sizeof(link_history_t
);
142 lhist
->since
= lhist
->changed
= time(NULL
);
143 digestmap_set(orhist
->link_history_map
, to_id
, lhist
);
148 /** Helper: free storage held by a single link history entry. */
150 _free_link_history(void *val
)
152 rephist_total_alloc
-= sizeof(link_history_t
);
156 /** Helper: free storage held by a single OR history entry. */
158 free_or_history(void *_hist
)
160 or_history_t
*hist
= _hist
;
161 digestmap_free(hist
->link_history_map
, _free_link_history
);
162 rephist_total_alloc
-= sizeof(or_history_t
);
167 /** Update an or_history_t object <b>hist</b> so that its uptime/downtime
168 * count is up-to-date as of <b>when</b>.
171 update_or_history(or_history_t
*hist
, time_t when
)
174 if (hist
->up_since
) {
175 tor_assert(!hist
->down_since
);
176 hist
->uptime
+= (when
- hist
->up_since
);
177 hist
->up_since
= when
;
178 } else if (hist
->down_since
) {
179 hist
->downtime
+= (when
- hist
->down_since
);
180 hist
->down_since
= when
;
184 /** Initialize the static data structures for tracking history. */
188 history_map
= digestmap_new();
190 predicted_ports_init();
194 /** Helper: note that we are no longer connected to the router with history
195 * <b>hist</b>. If <b>failed</b>, the connection failed; otherwise, it was
196 * closed correctly. */
198 mark_or_down(or_history_t
*hist
, time_t when
, int failed
)
200 if (hist
->up_since
) {
201 hist
->uptime
+= (when
- hist
->up_since
);
204 if (failed
&& !hist
->down_since
) {
205 hist
->down_since
= when
;
209 /** Helper: note that we are connected to the router with history
212 mark_or_up(or_history_t
*hist
, time_t when
)
214 if (hist
->down_since
) {
215 hist
->downtime
+= (when
- hist
->down_since
);
216 hist
->down_since
= 0;
218 if (!hist
->up_since
) {
219 hist
->up_since
= when
;
223 /** Remember that an attempt to connect to the OR with identity digest
224 * <b>id</b> failed at <b>when</b>.
227 rep_hist_note_connect_failed(const char* id
, time_t when
)
230 hist
= get_or_history(id
);
234 mark_or_down(hist
, when
, 1);
235 hist
->changed
= when
;
238 /** Remember that an attempt to connect to the OR with identity digest
239 * <b>id</b> succeeded at <b>when</b>.
242 rep_hist_note_connect_succeeded(const char* id
, time_t when
)
245 hist
= get_or_history(id
);
249 mark_or_up(hist
, when
);
250 hist
->changed
= when
;
253 /** Remember that we intentionally closed our connection to the OR
254 * with identity digest <b>id</b> at <b>when</b>.
257 rep_hist_note_disconnect(const char* id
, time_t when
)
260 hist
= get_or_history(id
);
263 mark_or_down(hist
, when
, 0);
264 hist
->changed
= when
;
267 /** Remember that our connection to the OR with identity digest
268 * <b>id</b> had an error and stopped working at <b>when</b>.
271 rep_hist_note_connection_died(const char* id
, time_t when
)
275 /* If conn has no identity, it didn't complete its handshake, or something
276 * went wrong. Ignore it.
280 hist
= get_or_history(id
);
283 mark_or_down(hist
, when
, 1);
284 hist
->changed
= when
;
287 /** We have just decided that this router is reachable, meaning
288 * we will give it a "Running" flag for the next while. */
290 rep_hist_note_router_reachable(const char *id
, time_t when
)
292 or_history_t
*hist
= get_or_history(id
);
293 if (!started_tracking_stability
)
294 started_tracking_stability
= time(NULL
);
295 if (hist
&& !hist
->start_of_run
) {
296 hist
->start_of_run
= when
;
298 if (hist
&& hist
->start_of_downtime
) {
299 long down_length
= when
- hist
->start_of_downtime
;
300 hist
->total_weighted_time
+= down_length
;
301 hist
->start_of_downtime
= 0;
305 /** We have just decided that this router is unreachable, meaning
306 * we are taking away its "Running" flag. */
308 rep_hist_note_router_unreachable(const char *id
, time_t when
)
310 or_history_t
*hist
= get_or_history(id
);
311 if (!started_tracking_stability
)
312 started_tracking_stability
= time(NULL
);
313 if (hist
&& hist
->start_of_run
) {
314 /*XXXX We could treat failed connections differently from failed
315 * conect attempts. */
316 long run_length
= when
- hist
->start_of_run
;
317 hist
->weighted_run_length
+= run_length
;
318 hist
->total_run_weights
+= 1.0;
319 hist
->start_of_run
= 0;
321 hist
->weighted_uptime
+= run_length
;
322 hist
->total_weighted_time
+= run_length
;
324 if (hist
&& !hist
->start_of_downtime
) {
325 hist
->start_of_downtime
= when
;
329 /** Helper: Discount all old MTBF data, if it is time to do so. Return
330 * the time at which we should next discount MTBF data. */
332 rep_hist_downrate_old_runs(time_t now
)
334 digestmap_iter_t
*orhist_it
;
341 history_map
= digestmap_new();
342 if (!stability_last_downrated
)
343 stability_last_downrated
= now
;
344 if (stability_last_downrated
+ STABILITY_INTERVAL
> now
)
345 return stability_last_downrated
+ STABILITY_INTERVAL
;
347 /* Okay, we should downrate the data. By how much? */
348 while (stability_last_downrated
+ STABILITY_INTERVAL
< now
) {
349 stability_last_downrated
+= STABILITY_INTERVAL
;
350 alpha
*= STABILITY_ALPHA
;
353 /* Multiply every w_r_l, t_r_w pair by alpha. */
354 for (orhist_it
= digestmap_iter_init(history_map
);
355 !digestmap_iter_done(orhist_it
);
356 orhist_it
= digestmap_iter_next(history_map
,orhist_it
)) {
357 digestmap_iter_get(orhist_it
, &digest1
, &hist_p
);
360 hist
->weighted_run_length
=
361 (unsigned long)(hist
->weighted_run_length
* alpha
);
362 hist
->total_run_weights
*= alpha
;
364 hist
->weighted_uptime
*= alpha
;
365 hist
->total_weighted_time
*= alpha
;
368 return stability_last_downrated
+ STABILITY_INTERVAL
;
371 /** Helper: Return the weighted MTBF of the router with history <b>hist</b>. */
373 get_stability(or_history_t
*hist
, time_t when
)
375 unsigned long total
= hist
->weighted_run_length
;
376 double total_weights
= hist
->total_run_weights
;
378 if (hist
->start_of_run
) {
379 /* We're currently in a run. Let total and total_weights hold the values
380 * they would hold if the current run were to end now. */
381 total
+= (when
-hist
->start_of_run
);
382 total_weights
+= 1.0;
384 if (total_weights
< STABILITY_EPSILON
) {
385 /* Round down to zero, and avoid divide-by-zero. */
389 return total
/ total_weights
;
392 /** Return the total amount of time we've been observing, with each run of
393 * time downrated by the appropriate factor. */
395 get_total_weighted_time(or_history_t
*hist
, time_t when
)
397 long total
= hist
->total_weighted_time
;
398 if (hist
->start_of_run
) {
399 total
+= (when
- hist
->start_of_run
);
400 } else if (hist
->start_of_downtime
) {
401 total
+= (when
- hist
->start_of_downtime
);
406 /** Helper: Return the weighted percent-of-time-online of the router with
407 * history <b>hist</b>. */
409 get_weighted_fractional_uptime(or_history_t
*hist
, time_t when
)
411 unsigned long total
= hist
->total_weighted_time
;
412 unsigned long up
= hist
->weighted_uptime
;
414 if (hist
->start_of_run
) {
415 long run_length
= (when
- hist
->start_of_run
);
418 } else if (hist
->start_of_downtime
) {
419 total
+= (when
- hist
->start_of_downtime
);
421 return ((double) up
) / total
;
424 /** Return an estimated MTBF for the router whose identity digest is
425 * <b>id</b>. Return 0 if the router is unknown. */
427 rep_hist_get_stability(const char *id
, time_t when
)
429 or_history_t
*hist
= get_or_history(id
);
433 return get_stability(hist
, when
);
436 /** Return an estimated percent-of-time-online for the router whose identity
437 * digest is <b>id</b>. Return 0 if the router is unknown. */
439 rep_hist_get_weighted_fractional_uptime(const char *id
, time_t when
)
441 or_history_t
*hist
= get_or_history(id
);
445 return get_weighted_fractional_uptime(hist
, when
);
448 /** Return a number representing how long we've known about the router whose
449 * digest is <b>id</b>. Return 0 if the router is unknown.
451 * Be careful: this measure incresases monotonically as we know the router for
452 * longer and longer, but it doesn't increase linearly.
455 rep_hist_get_weighted_time_known(const char *id
, time_t when
)
457 or_history_t
*hist
= get_or_history(id
);
461 return get_total_weighted_time(hist
, when
);
464 /** Return true if we've been measuring MTBFs for long enough to
465 * pronounce on Stability. */
467 rep_hist_have_measured_enough_stability(void)
469 /* XXXX021 This doesn't do so well when we change our opinion
470 * as to whether we're tracking router stability. */
471 return started_tracking_stability
< time(NULL
) - 4*60*60;
474 /** Remember that we successfully extended from the OR with identity
475 * digest <b>from_id</b> to the OR with identity digest
479 rep_hist_note_extend_succeeded(const char *from_id
, const char *to_id
)
481 link_history_t
*hist
;
482 /* log_fn(LOG_WARN, "EXTEND SUCCEEDED: %s->%s",from_name,to_name); */
483 hist
= get_link_history(from_id
, to_id
);
487 hist
->changed
= time(NULL
);
490 /** Remember that we tried to extend from the OR with identity digest
491 * <b>from_id</b> to the OR with identity digest <b>to_name</b>, but
495 rep_hist_note_extend_failed(const char *from_id
, const char *to_id
)
497 link_history_t
*hist
;
498 /* log_fn(LOG_WARN, "EXTEND FAILED: %s->%s",from_name,to_name); */
499 hist
= get_link_history(from_id
, to_id
);
502 ++hist
->n_extend_fail
;
503 hist
->changed
= time(NULL
);
506 /** Log all the reliability data we have remembered, with the chosen
510 rep_hist_dump_stats(time_t now
, int severity
)
512 digestmap_iter_t
*lhist_it
;
513 digestmap_iter_t
*orhist_it
;
514 const char *name1
, *name2
, *digest1
, *digest2
;
515 char hexdigest1
[HEX_DIGEST_LEN
+1];
516 or_history_t
*or_history
;
517 link_history_t
*link_history
;
518 void *or_history_p
, *link_history_p
;
523 unsigned long upt
, downt
;
526 rep_history_clean(now
- get_options()->RephistTrackTime
);
528 log(severity
, LD_GENERAL
, "--------------- Dumping history information:");
530 for (orhist_it
= digestmap_iter_init(history_map
);
531 !digestmap_iter_done(orhist_it
);
532 orhist_it
= digestmap_iter_next(history_map
,orhist_it
)) {
535 digestmap_iter_get(orhist_it
, &digest1
, &or_history_p
);
536 or_history
= (or_history_t
*) or_history_p
;
538 if ((r
= router_get_by_digest(digest1
)))
542 base16_encode(hexdigest1
, sizeof(hexdigest1
), digest1
, DIGEST_LEN
);
543 update_or_history(or_history
, now
);
544 upt
= or_history
->uptime
;
545 downt
= or_history
->downtime
;
546 s
= get_stability(or_history
, now
);
549 uptime
= ((double)upt
) / (upt
+downt
);
553 log(severity
, LD_GENERAL
,
554 "OR %s [%s]: %ld/%ld good connections; uptime %ld/%ld sec (%.2f%%); "
555 "wmtbf %lu:%02lu:%02lu",
557 or_history
->n_conn_ok
, or_history
->n_conn_fail
+or_history
->n_conn_ok
,
558 upt
, upt
+downt
, uptime
*100.0,
559 stability
/3600, (stability
/60)%60, stability
%60);
561 if (!digestmap_isempty(or_history
->link_history_map
)) {
562 strlcpy(buffer
, " Extend attempts: ", sizeof(buffer
));
563 len
= strlen(buffer
);
564 for (lhist_it
= digestmap_iter_init(or_history
->link_history_map
);
565 !digestmap_iter_done(lhist_it
);
566 lhist_it
= digestmap_iter_next(or_history
->link_history_map
,
568 digestmap_iter_get(lhist_it
, &digest2
, &link_history_p
);
569 if ((r
= router_get_by_digest(digest2
)))
574 link_history
= (link_history_t
*) link_history_p
;
576 ret
= tor_snprintf(buffer
+len
, 2048-len
, "%s(%ld/%ld); ", name2
,
577 link_history
->n_extend_ok
,
578 link_history
->n_extend_ok
+link_history
->n_extend_fail
);
584 log(severity
, LD_GENERAL
, "%s", buffer
);
589 /** Remove history info for routers/links that haven't changed since
593 rep_history_clean(time_t before
)
595 int authority
= authdir_mode(get_options());
596 or_history_t
*or_history
;
597 link_history_t
*link_history
;
598 void *or_history_p
, *link_history_p
;
599 digestmap_iter_t
*orhist_it
, *lhist_it
;
602 orhist_it
= digestmap_iter_init(history_map
);
603 while (!digestmap_iter_done(orhist_it
)) {
605 digestmap_iter_get(orhist_it
, &d1
, &or_history_p
);
606 or_history
= or_history_p
;
608 remove
= authority
? (or_history
->total_run_weights
< STABILITY_EPSILON
&&
609 !or_history
->start_of_run
)
610 : (or_history
->changed
< before
);
612 orhist_it
= digestmap_iter_next_rmv(history_map
, orhist_it
);
613 free_or_history(or_history
);
616 for (lhist_it
= digestmap_iter_init(or_history
->link_history_map
);
617 !digestmap_iter_done(lhist_it
); ) {
618 digestmap_iter_get(lhist_it
, &d2
, &link_history_p
);
619 link_history
= link_history_p
;
620 if (link_history
->changed
< before
) {
621 lhist_it
= digestmap_iter_next_rmv(or_history
->link_history_map
,
623 rephist_total_alloc
-= sizeof(link_history_t
);
624 tor_free(link_history
);
627 lhist_it
= digestmap_iter_next(or_history
->link_history_map
,lhist_it
);
629 orhist_it
= digestmap_iter_next(history_map
, orhist_it
);
633 /** Write MTBF data to disk. Returns 0 on success, negative on failure. */
635 rep_hist_record_mtbf_data(void)
637 char time_buf
[ISO_TIME_LEN
+1];
639 digestmap_iter_t
*orhist_it
;
643 open_file_t
*open_file
= NULL
;
647 char *filename
= get_datadir_fname("router-stability");
648 f
= start_writing_to_stdio_file(filename
, OPEN_FLAGS_REPLACE
|O_TEXT
, 0600,
656 * FormatLine *KeywordLine Data
658 * FormatLine = "format 1" NL
659 * KeywordLine = Keyword SP Arguments NL
660 * Data = "data" NL *RouterMTBFLine "." NL
661 * RouterMTBFLine = Fingerprint SP WeightedRunLen SP
662 * TotalRunWeights [SP S=StartRunTime] NL
664 #define PUT(s) STMT_BEGIN if (fputs((s),f)<0) goto err; STMT_END
665 #define PRINTF(args) STMT_BEGIN if (fprintf args <0) goto err; STMT_END
669 format_iso_time(time_buf
, time(NULL
));
670 PRINTF((f
, "stored-at %s\n", time_buf
));
672 if (started_tracking_stability
) {
673 format_iso_time(time_buf
, started_tracking_stability
);
674 PRINTF((f
, "tracked-since %s\n", time_buf
));
676 if (stability_last_downrated
) {
677 format_iso_time(time_buf
, stability_last_downrated
);
678 PRINTF((f
, "last-downrated %s\n", time_buf
));
683 /* XXX021 Nick: now bridge auths record this for all routers too.
684 * Should we make them record it only for bridge routers? -RD
685 * Not for 0.2.0. -NM */
686 for (orhist_it
= digestmap_iter_init(history_map
);
687 !digestmap_iter_done(orhist_it
);
688 orhist_it
= digestmap_iter_next(history_map
,orhist_it
)) {
689 char dbuf
[HEX_DIGEST_LEN
+1];
690 const char *t
= NULL
;
691 digestmap_iter_get(orhist_it
, &digest
, &or_history_p
);
692 hist
= (or_history_t
*) or_history_p
;
694 base16_encode(dbuf
, sizeof(dbuf
), digest
, DIGEST_LEN
);
695 PRINTF((f
, "R %s\n", dbuf
));
696 if (hist
->start_of_run
> 0) {
697 format_iso_time(time_buf
, hist
->start_of_run
);
700 PRINTF((f
, "+MTBF %lu %.5lf%s%s\n",
701 hist
->weighted_run_length
, hist
->total_run_weights
,
702 t
? " S=" : "", t
? t
: ""));
704 if (hist
->start_of_downtime
> 0) {
705 format_iso_time(time_buf
, hist
->start_of_downtime
);
708 PRINTF((f
, "+WFU %lu %lu%s%s\n",
709 hist
->weighted_uptime
, hist
->total_weighted_time
,
710 t
? " S=" : "", t
? t
: ""));
718 return finish_writing_to_file(open_file
);
720 abort_writing_to_file(open_file
);
724 /** Helper: return the first j >= i such that !strcmpstart(sl[j], prefix) and
725 * such that no line sl[k] with i <= k < j starts with "R ". Return -1 if no
726 * such line exists. */
728 find_next_with(smartlist_t
*sl
, int i
, const char *prefix
)
730 for ( ; i
< smartlist_len(sl
); ++i
) {
731 const char *line
= smartlist_get(sl
, i
);
732 if (!strcmpstart(line
, prefix
))
734 if (!strcmpstart(line
, "R "))
740 /** How many bad times has parse_possibly_bad_iso_time parsed? */
741 static int n_bogus_times
= 0;
742 /** Parse the ISO-formatted time in <b>s</b> into *<b>time_out</b>, but
743 * rounds any pre-1970 date to Jan 1, 1970. */
745 parse_possibly_bad_iso_time(const char *s
, time_t *time_out
)
749 strlcpy(b
, s
, sizeof(b
));
751 year
= (int)tor_parse_long(b
, 10, 0, INT_MAX
, NULL
, NULL
);
757 return parse_iso_time(s
, time_out
);
760 /** We've read a time <b>t</b> from a file stored at <b>stored_at</b>, which
761 * says we started measuring at <b>started_measuring</b>. Return a new number
762 * that's about as much before <b>now</b> as <b>t</b> was before
766 correct_time(time_t t
, time_t now
, time_t stored_at
, time_t started_measuring
)
768 if (t
< started_measuring
- 24*60*60*365)
770 else if (t
< started_measuring
)
771 return started_measuring
;
772 else if (t
> stored_at
)
775 long run_length
= stored_at
- t
;
776 t
= now
- run_length
;
777 if (t
< started_measuring
)
778 t
= started_measuring
;
783 /** Load MTBF data from disk. Returns 0 on success or recoverable error, -1
786 rep_hist_load_mtbf_data(time_t now
)
788 /* XXXX won't handle being called while history is already populated. */
790 const char *line
= NULL
;
792 time_t last_downrated
= 0, stored_at
= 0, tracked_since
= 0;
793 time_t latest_possible_start
= now
;
797 char *filename
= get_datadir_fname("router-stability");
798 char *d
= read_file_to_str(filename
, RFTS_IGNORE_MISSING
, NULL
);
802 lines
= smartlist_create();
803 smartlist_split_string(lines
, d
, "\n", SPLIT_SKIP_SPACE
, 0);
808 const char *firstline
;
809 if (smartlist_len(lines
)>4) {
810 firstline
= smartlist_get(lines
, 0);
811 if (!strcmpstart(firstline
, "format "))
812 format
= tor_parse_long(firstline
+strlen("format "),
813 10, -1, LONG_MAX
, NULL
, NULL
);
816 if (format
!= 1 && format
!= 2) {
818 "Unrecognized format in mtbf history file. Skipping.");
821 for (i
= 1; i
< smartlist_len(lines
); ++i
) {
822 line
= smartlist_get(lines
, i
);
823 if (!strcmp(line
, "data"))
825 if (!strcmpstart(line
, "last-downrated ")) {
826 if (parse_iso_time(line
+strlen("last-downrated "), &last_downrated
)<0)
827 log_warn(LD_GENERAL
,"Couldn't parse downrate time in mtbf "
830 if (!strcmpstart(line
, "stored-at ")) {
831 if (parse_iso_time(line
+strlen("stored-at "), &stored_at
)<0)
832 log_warn(LD_GENERAL
,"Couldn't parse stored time in mtbf "
835 if (!strcmpstart(line
, "tracked-since ")) {
836 if (parse_iso_time(line
+strlen("tracked-since "), &tracked_since
)<0)
837 log_warn(LD_GENERAL
,"Couldn't parse started-tracking time in mtbf "
841 if (last_downrated
> now
)
842 last_downrated
= now
;
843 if (tracked_since
> now
)
847 log_warn(LD_GENERAL
, "No stored time recorded.");
851 if (line
&& !strcmp(line
, "data"))
856 for (; i
< smartlist_len(lines
); ++i
) {
857 char digest
[DIGEST_LEN
];
858 char hexbuf
[HEX_DIGEST_LEN
+1];
859 char mtbf_timebuf
[ISO_TIME_LEN
+1];
860 char wfu_timebuf
[ISO_TIME_LEN
+1];
861 time_t start_of_run
= 0;
862 time_t start_of_downtime
= 0;
863 int have_mtbf
= 0, have_wfu
= 0;
866 long wt_uptime
= 0, total_wt_time
= 0;
869 line
= smartlist_get(lines
, i
);
870 if (!strcmp(line
, "."))
873 mtbf_timebuf
[0] = '\0';
874 wfu_timebuf
[0] = '\0';
877 n
= sscanf(line
, "%40s %ld %lf S=%10s %8s",
878 hexbuf
, &wrl
, &trw
, mtbf_timebuf
, mtbf_timebuf
+11);
879 if (n
!= 3 && n
!= 5) {
880 log_warn(LD_GENERAL
, "Couldn't scan line %s", escaped(line
));
886 int mtbf_idx
, wfu_idx
;
887 if (strcmpstart(line
, "R ") || strlen(line
) < 2+HEX_DIGEST_LEN
)
889 strlcpy(hexbuf
, line
+2, sizeof(hexbuf
));
890 mtbf_idx
= find_next_with(lines
, i
+1, "+MTBF ");
891 wfu_idx
= find_next_with(lines
, i
+1, "+WFU ");
893 const char *mtbfline
= smartlist_get(lines
, mtbf_idx
);
894 n
= sscanf(mtbfline
, "+MTBF %lu %lf S=%10s %8s",
895 &wrl
, &trw
, mtbf_timebuf
, mtbf_timebuf
+11);
896 if (n
== 2 || n
== 4) {
899 log_warn(LD_GENERAL
, "Couldn't scan +MTBF line %s",
904 const char *wfuline
= smartlist_get(lines
, wfu_idx
);
905 n
= sscanf(wfuline
, "+WFU %lu %lu S=%10s %8s",
906 &wt_uptime
, &total_wt_time
,
907 wfu_timebuf
, wfu_timebuf
+11);
908 if (n
== 2 || n
== 4) {
911 log_warn(LD_GENERAL
, "Couldn't scan +WFU line %s", escaped(wfuline
));
919 if (base16_decode(digest
, DIGEST_LEN
, hexbuf
, HEX_DIGEST_LEN
) < 0) {
920 log_warn(LD_GENERAL
, "Couldn't hex string %s", escaped(hexbuf
));
923 hist
= get_or_history(digest
);
928 if (mtbf_timebuf
[0]) {
929 mtbf_timebuf
[10] = ' ';
930 if (parse_possibly_bad_iso_time(mtbf_timebuf
, &start_of_run
)<0)
931 log_warn(LD_GENERAL
, "Couldn't parse time %s",
932 escaped(mtbf_timebuf
));
934 hist
->start_of_run
= correct_time(start_of_run
, now
, stored_at
,
936 if (hist
->start_of_run
< latest_possible_start
+ wrl
)
937 latest_possible_start
= hist
->start_of_run
- wrl
;
939 hist
->weighted_run_length
= wrl
;
940 hist
->total_run_weights
= trw
;
943 if (wfu_timebuf
[0]) {
944 wfu_timebuf
[10] = ' ';
945 if (parse_possibly_bad_iso_time(wfu_timebuf
, &start_of_downtime
)<0)
946 log_warn(LD_GENERAL
, "Couldn't parse time %s", escaped(wfu_timebuf
));
949 hist
->start_of_downtime
= correct_time(start_of_downtime
, now
, stored_at
,
951 hist
->weighted_uptime
= wt_uptime
;
952 hist
->total_weighted_time
= total_wt_time
;
954 if (strcmp(line
, "."))
955 log_warn(LD_GENERAL
, "Truncated MTBF file.");
958 tracked_since
= latest_possible_start
;
960 stability_last_downrated
= last_downrated
;
961 started_tracking_stability
= tracked_since
;
967 SMARTLIST_FOREACH(lines
, char *, cp
, tor_free(cp
));
968 smartlist_free(lines
);
972 /** For how many seconds do we keep track of individual per-second bandwidth
974 #define NUM_SECS_ROLLING_MEASURE 10
975 /** How large are the intervals for which we track and report bandwidth use? */
976 #define NUM_SECS_BW_SUM_INTERVAL (15*60)
977 /** How far in the past do we remember and publish bandwidth use? */
978 #define NUM_SECS_BW_SUM_IS_VALID (24*60*60)
979 /** How many bandwidth usage intervals do we remember? (derived) */
980 #define NUM_TOTALS (NUM_SECS_BW_SUM_IS_VALID/NUM_SECS_BW_SUM_INTERVAL)
982 /** Structure to track bandwidth use, and remember the maxima for a given
985 typedef struct bw_array_t
{
986 /** Observation array: Total number of bytes transferred in each of the last
987 * NUM_SECS_ROLLING_MEASURE seconds. This is used as a circular array. */
988 uint64_t obs
[NUM_SECS_ROLLING_MEASURE
];
989 int cur_obs_idx
; /**< Current position in obs. */
990 time_t cur_obs_time
; /**< Time represented in obs[cur_obs_idx] */
991 uint64_t total_obs
; /**< Total for all members of obs except
992 * obs[cur_obs_idx] */
993 uint64_t max_total
; /**< Largest value that total_obs has taken on in the
995 uint64_t total_in_period
; /**< Total bytes transferred in the current
998 /** When does the next period begin? */
1000 /** Where in 'maxima' should the maximum bandwidth usage for the current
1001 * period be stored? */
1003 /** How many values in maxima/totals have been set ever? */
1005 /** Circular array of the maximum
1006 * bandwidth-per-NUM_SECS_ROLLING_MEASURE usage for the last
1007 * NUM_TOTALS periods */
1008 uint64_t maxima
[NUM_TOTALS
];
1009 /** Circular array of the total bandwidth usage for the last NUM_TOTALS
1011 uint64_t totals
[NUM_TOTALS
];
1014 /** Shift the current period of b forward by one. */
1016 commit_max(bw_array_t
*b
)
1018 /* Store total from current period. */
1019 b
->totals
[b
->next_max_idx
] = b
->total_in_period
;
1020 /* Store maximum from current period. */
1021 b
->maxima
[b
->next_max_idx
++] = b
->max_total
;
1022 /* Advance next_period and next_max_idx */
1023 b
->next_period
+= NUM_SECS_BW_SUM_INTERVAL
;
1024 if (b
->next_max_idx
== NUM_TOTALS
)
1025 b
->next_max_idx
= 0;
1026 if (b
->num_maxes_set
< NUM_TOTALS
)
1028 /* Reset max_total. */
1030 /* Reset total_in_period. */
1031 b
->total_in_period
= 0;
1034 /** Shift the current observation time of 'b' forward by one second. */
1036 advance_obs(bw_array_t
*b
)
1041 /* Calculate the total bandwidth for the last NUM_SECS_ROLLING_MEASURE
1042 * seconds; adjust max_total as needed.*/
1043 total
= b
->total_obs
+ b
->obs
[b
->cur_obs_idx
];
1044 if (total
> b
->max_total
)
1045 b
->max_total
= total
;
1047 nextidx
= b
->cur_obs_idx
+1;
1048 if (nextidx
== NUM_SECS_ROLLING_MEASURE
)
1051 b
->total_obs
= total
- b
->obs
[nextidx
];
1053 b
->cur_obs_idx
= nextidx
;
1055 if (++b
->cur_obs_time
>= b
->next_period
)
1059 /** Add <b>n</b> bytes to the number of bytes in <b>b</b> for second
1062 add_obs(bw_array_t
*b
, time_t when
, uint64_t n
)
1064 /* Don't record data in the past. */
1065 if (when
<b
->cur_obs_time
)
1067 /* If we're currently adding observations for an earlier second than
1068 * 'when', advance b->cur_obs_time and b->cur_obs_idx by an
1069 * appropriate number of seconds, and do all the other housekeeping */
1070 while (when
>b
->cur_obs_time
)
1073 b
->obs
[b
->cur_obs_idx
] += n
;
1074 b
->total_in_period
+= n
;
1077 /** Allocate, initialize, and return a new bw_array. */
1083 b
= tor_malloc_zero(sizeof(bw_array_t
));
1084 rephist_total_alloc
+= sizeof(bw_array_t
);
1086 b
->cur_obs_time
= start
;
1087 b
->next_period
= start
+ NUM_SECS_BW_SUM_INTERVAL
;
1091 static bw_array_t
*read_array
= NULL
;
1092 static bw_array_t
*write_array
= NULL
;
1094 /** Set up read_array and write_array. */
1096 bw_arrays_init(void)
1098 read_array
= bw_array_new();
1099 write_array
= bw_array_new();
1102 /** We read <b>num_bytes</b> more bytes in second <b>when</b>.
1104 * Add num_bytes to the current running total for <b>when</b>.
1106 * <b>when</b> can go back to time, but it's safe to ignore calls
1107 * earlier than the latest <b>when</b> you've heard of.
1110 rep_hist_note_bytes_written(size_t num_bytes
, time_t when
)
1112 /* Maybe a circular array for recent seconds, and step to a new point
1113 * every time a new second shows up. Or simpler is to just to have
1114 * a normal array and push down each item every second; it's short.
1116 /* When a new second has rolled over, compute the sum of the bytes we've
1117 * seen over when-1 to when-1-NUM_SECS_ROLLING_MEASURE, and stick it
1118 * somewhere. See rep_hist_bandwidth_assess() below.
1120 add_obs(write_array
, when
, num_bytes
);
1123 /** We wrote <b>num_bytes</b> more bytes in second <b>when</b>.
1124 * (like rep_hist_note_bytes_written() above)
1127 rep_hist_note_bytes_read(size_t num_bytes
, time_t when
)
1129 /* if we're smart, we can make this func and the one above share code */
1130 add_obs(read_array
, when
, num_bytes
);
1133 /** Helper: Return the largest value in b->maxima. (This is equal to the
1134 * most bandwidth used in any NUM_SECS_ROLLING_MEASURE period for the last
1135 * NUM_SECS_BW_SUM_IS_VALID seconds.)
1138 find_largest_max(bw_array_t
*b
)
1143 for (i
=0; i
<NUM_TOTALS
; ++i
) {
1144 if (b
->maxima
[i
]>max
)
1150 /** Find the largest sums in the past NUM_SECS_BW_SUM_IS_VALID (roughly)
1151 * seconds. Find one sum for reading and one for writing. They don't have
1152 * to be at the same time.
1154 * Return the smaller of these sums, divided by NUM_SECS_ROLLING_MEASURE.
1157 rep_hist_bandwidth_assess(void)
1160 r
= find_largest_max(read_array
);
1161 w
= find_largest_max(write_array
);
1163 return (int)(U64_TO_DBL(w
)/NUM_SECS_ROLLING_MEASURE
);
1165 return (int)(U64_TO_DBL(r
)/NUM_SECS_ROLLING_MEASURE
);
1168 /** Print the bandwidth history of b (either read_array or write_array)
1169 * into the buffer pointed to by buf. The format is simply comma
1170 * separated numbers, from oldest to newest.
1172 * It returns the number of bytes written.
1175 rep_hist_fill_bandwidth_history(char *buf
, size_t len
, bw_array_t
*b
)
1179 or_options_t
*options
= get_options();
1182 if (b
->num_maxes_set
<= b
->next_max_idx
) {
1183 /* We haven't been through the circular array yet; time starts at i=0.*/
1186 /* We've been around the array at least once. The next i to be
1187 overwritten is the oldest. */
1188 i
= b
->next_max_idx
;
1191 if (options
->RelayBandwidthRate
) {
1192 /* We don't want to report that we used more bandwidth than the max we're
1193 * willing to relay; otherwise everybody will know how much traffic
1194 * we used ourself. */
1195 cutoff
= options
->RelayBandwidthRate
* NUM_SECS_BW_SUM_INTERVAL
;
1197 cutoff
= UINT64_MAX
;
1200 for (n
=0; n
<b
->num_maxes_set
; ++n
,++i
) {
1202 if (i
>= NUM_TOTALS
)
1204 tor_assert(i
< NUM_TOTALS
);
1205 /* Round the bandwidth used down to the nearest 1k. */
1206 total
= b
->totals
[i
] & ~0x3ff;
1210 if (n
==(b
->num_maxes_set
-1))
1211 tor_snprintf(cp
, len
-(cp
-buf
), U64_FORMAT
, U64_PRINTF_ARG(total
));
1213 tor_snprintf(cp
, len
-(cp
-buf
), U64_FORMAT
",", U64_PRINTF_ARG(total
));
1219 /** Allocate and return lines for representing this server's bandwidth
1220 * history in its descriptor.
1223 rep_hist_get_bandwidth_lines(int for_extrainfo
)
1226 char t
[ISO_TIME_LEN
+1];
1231 /* opt (read|write)-history yyyy-mm-dd HH:MM:SS (n s) n,n,n,n,n... */
1232 len
= (60+20*NUM_TOTALS
)*2;
1233 buf
= tor_malloc_zero(len
);
1236 b
= r
?read_array
:write_array
;
1238 format_iso_time(t
, b
->next_period
-NUM_SECS_BW_SUM_INTERVAL
);
1239 tor_snprintf(cp
, len
-(cp
-buf
), "%s%s %s (%d s) ",
1240 for_extrainfo
? "" : "opt ",
1241 r
? "read-history" : "write-history", t
,
1242 NUM_SECS_BW_SUM_INTERVAL
);
1244 cp
+= rep_hist_fill_bandwidth_history(cp
, len
-(cp
-buf
), b
);
1245 strlcat(cp
, "\n", len
-(cp
-buf
));
1251 /** Update <b>state</b> with the newest bandwidth history. */
1253 rep_hist_update_state(or_state_t
*state
)
1257 smartlist_t
**s_values
;
1262 len
= 20*NUM_TOTALS
+1;
1263 buf
= tor_malloc_zero(len
);
1266 b
= r
?read_array
:write_array
;
1267 s_begins
= r
?&state
->BWHistoryReadEnds
:&state
->BWHistoryWriteEnds
;
1268 s_interval
= r
?&state
->BWHistoryReadInterval
:&state
->BWHistoryWriteInterval
;
1269 s_values
= r
?&state
->BWHistoryReadValues
:&state
->BWHistoryWriteValues
;
1272 SMARTLIST_FOREACH(*s_values
, char *, val
, tor_free(val
));
1273 smartlist_free(*s_values
);
1275 if (! server_mode(get_options())) {
1276 /* Clients don't need to store bandwidth history persistently;
1277 * force these values to the defaults. */
1278 /* FFFF we should pull the default out of config.c's state table,
1279 * so we don't have two defaults. */
1280 if (*s_begins
!= 0 || *s_interval
!= 900) {
1281 time_t now
= time(NULL
);
1282 time_t save_at
= get_options()->AvoidDiskWrites
? now
+3600 : now
+600;
1283 or_state_mark_dirty(state
, save_at
);
1287 *s_values
= smartlist_create();
1290 *s_begins
= b
->next_period
;
1291 *s_interval
= NUM_SECS_BW_SUM_INTERVAL
;
1293 cp
+= rep_hist_fill_bandwidth_history(cp
, len
, b
);
1294 tor_snprintf(cp
, len
-(cp
-buf
), cp
== buf
? U64_FORMAT
: ","U64_FORMAT
,
1295 U64_PRINTF_ARG(b
->total_in_period
));
1296 *s_values
= smartlist_create();
1297 if (server_mode(get_options()))
1298 smartlist_split_string(*s_values
, buf
, ",", SPLIT_SKIP_SPACE
, 0);
1301 if (server_mode(get_options())) {
1302 or_state_mark_dirty(get_or_state(), time(NULL
)+(2*3600));
1306 /** Set bandwidth history from our saved state. */
1308 rep_hist_load_state(or_state_t
*state
, char **err
)
1310 time_t s_begins
, start
;
1311 time_t now
= time(NULL
);
1316 smartlist_t
*s_values
;
1319 /* Assert they already have been malloced */
1320 tor_assert(read_array
&& write_array
);
1323 b
= r
?read_array
:write_array
;
1324 s_begins
= r
?state
->BWHistoryReadEnds
:state
->BWHistoryWriteEnds
;
1325 s_interval
= r
?state
->BWHistoryReadInterval
:state
->BWHistoryWriteInterval
;
1326 s_values
= r
?state
->BWHistoryReadValues
:state
->BWHistoryWriteValues
;
1327 if (s_values
&& s_begins
>= now
- NUM_SECS_BW_SUM_INTERVAL
*NUM_TOTALS
) {
1328 start
= s_begins
- s_interval
*(smartlist_len(s_values
));
1331 b
->cur_obs_time
= start
;
1332 b
->next_period
= start
+ NUM_SECS_BW_SUM_INTERVAL
;
1333 SMARTLIST_FOREACH(s_values
, char *, cp
, {
1334 v
= tor_parse_uint64(cp
, 10, 0, UINT64_MAX
, &ok
, NULL
);
1337 log_notice(LD_GENERAL
, "Could not parse '%s' into a number.'", cp
);
1340 add_obs(b
, start
, v
);
1341 start
+= NUM_SECS_BW_SUM_INTERVAL
;
1346 /* Clean up maxima and observed */
1347 /* Do we really want to zero this for the purpose of max capacity? */
1348 for (i
=0; i
<NUM_SECS_ROLLING_MEASURE
; ++i
) {
1352 for (i
=0; i
<NUM_TOTALS
; ++i
) {
1359 *err
= tor_strdup("Parsing of bandwidth history values failed");
1360 /* and create fresh arrays */
1361 tor_free(read_array
);
1362 tor_free(write_array
);
1363 read_array
= bw_array_new();
1364 write_array
= bw_array_new();
1370 /*********************************************************************/
1372 /** A list of port numbers that have been used recently. */
1373 static smartlist_t
*predicted_ports_list
=NULL
;
1374 /** The corresponding most recently used time for each port. */
1375 static smartlist_t
*predicted_ports_times
=NULL
;
1377 /** We just got an application request for a connection with
1378 * port <b>port</b>. Remember it for the future, so we can keep
1379 * some circuits open that will exit to this port.
1382 add_predicted_port(uint16_t port
, time_t now
)
1384 /* XXXX we could just use uintptr_t here, I think. */
1385 uint16_t *tmp_port
= tor_malloc(sizeof(uint16_t));
1386 time_t *tmp_time
= tor_malloc(sizeof(time_t));
1389 rephist_total_alloc
+= sizeof(uint16_t) + sizeof(time_t);
1390 smartlist_add(predicted_ports_list
, tmp_port
);
1391 smartlist_add(predicted_ports_times
, tmp_time
);
1394 /** Initialize whatever memory and structs are needed for predicting
1395 * which ports will be used. Also seed it with port 80, so we'll build
1396 * circuits on start-up.
1399 predicted_ports_init(void)
1401 predicted_ports_list
= smartlist_create();
1402 predicted_ports_times
= smartlist_create();
1403 add_predicted_port(80, time(NULL
)); /* add one to kickstart us */
1406 /** Free whatever memory is needed for predicting which ports will
1410 predicted_ports_free(void)
1412 rephist_total_alloc
-= smartlist_len(predicted_ports_list
)*sizeof(uint16_t);
1413 SMARTLIST_FOREACH(predicted_ports_list
, char *, cp
, tor_free(cp
));
1414 smartlist_free(predicted_ports_list
);
1415 rephist_total_alloc
-= smartlist_len(predicted_ports_times
)*sizeof(time_t);
1416 SMARTLIST_FOREACH(predicted_ports_times
, char *, cp
, tor_free(cp
));
1417 smartlist_free(predicted_ports_times
);
1420 /** Remember that <b>port</b> has been asked for as of time <b>now</b>.
1421 * This is used for predicting what sorts of streams we'll make in the
1422 * future and making exit circuits to anticipate that.
1425 rep_hist_note_used_port(uint16_t port
, time_t now
)
1431 tor_assert(predicted_ports_list
);
1432 tor_assert(predicted_ports_times
);
1434 if (!port
) /* record nothing */
1437 for (i
= 0; i
< smartlist_len(predicted_ports_list
); ++i
) {
1438 tmp_port
= smartlist_get(predicted_ports_list
, i
);
1439 tmp_time
= smartlist_get(predicted_ports_times
, i
);
1440 if (*tmp_port
== port
) {
1445 /* it's not there yet; we need to add it */
1446 add_predicted_port(port
, now
);
1449 /** For this long after we've seen a request for a given port, assume that
1450 * we'll want to make connections to the same port in the future. */
1451 #define PREDICTED_CIRCS_RELEVANCE_TIME (60*60)
1453 /** Return a pointer to the list of port numbers that
1454 * are likely to be asked for in the near future.
1456 * The caller promises not to mess with it.
1459 rep_hist_get_predicted_ports(time_t now
)
1465 tor_assert(predicted_ports_list
);
1466 tor_assert(predicted_ports_times
);
1468 /* clean out obsolete entries */
1469 for (i
= 0; i
< smartlist_len(predicted_ports_list
); ++i
) {
1470 tmp_time
= smartlist_get(predicted_ports_times
, i
);
1471 if (*tmp_time
+ PREDICTED_CIRCS_RELEVANCE_TIME
< now
) {
1472 tmp_port
= smartlist_get(predicted_ports_list
, i
);
1473 log_debug(LD_CIRC
, "Expiring predicted port %d", *tmp_port
);
1474 smartlist_del(predicted_ports_list
, i
);
1475 smartlist_del(predicted_ports_times
, i
);
1476 rephist_total_alloc
-= sizeof(uint16_t)+sizeof(time_t);
1482 return predicted_ports_list
;
1485 /** The user asked us to do a resolve. Rather than keeping track of
1486 * timings and such of resolves, we fake it for now by making treating
1487 * it the same way as a connection to port 80. This way we will continue
1488 * to have circuits lying around if the user only uses Tor for resolves.
1491 rep_hist_note_used_resolve(time_t now
)
1493 rep_hist_note_used_port(80, now
);
1496 /** The last time at which we needed an internal circ. */
1497 static time_t predicted_internal_time
= 0;
1498 /** The last time we needed an internal circ with good uptime. */
1499 static time_t predicted_internal_uptime_time
= 0;
1500 /** The last time we needed an internal circ with good capacity. */
1501 static time_t predicted_internal_capacity_time
= 0;
1503 /** Remember that we used an internal circ at time <b>now</b>. */
1505 rep_hist_note_used_internal(time_t now
, int need_uptime
, int need_capacity
)
1507 predicted_internal_time
= now
;
1509 predicted_internal_uptime_time
= now
;
1511 predicted_internal_capacity_time
= now
;
1514 /** Return 1 if we've used an internal circ recently; else return 0. */
1516 rep_hist_get_predicted_internal(time_t now
, int *need_uptime
,
1519 if (!predicted_internal_time
) { /* initialize it */
1520 predicted_internal_time
= now
;
1521 predicted_internal_uptime_time
= now
;
1522 predicted_internal_capacity_time
= now
;
1524 if (predicted_internal_time
+ PREDICTED_CIRCS_RELEVANCE_TIME
< now
)
1525 return 0; /* too long ago */
1526 if (predicted_internal_uptime_time
+ PREDICTED_CIRCS_RELEVANCE_TIME
>= now
)
1528 if (predicted_internal_capacity_time
+ PREDICTED_CIRCS_RELEVANCE_TIME
>= now
)
1533 /** Any ports used lately? These are pre-seeded if we just started
1534 * up or if we're running a hidden service. */
1536 any_predicted_circuits(time_t now
)
1538 return smartlist_len(predicted_ports_list
) ||
1539 predicted_internal_time
+ PREDICTED_CIRCS_RELEVANCE_TIME
>= now
;
1542 /** Return 1 if we have no need for circuits currently, else return 0. */
1544 rep_hist_circbuilding_dormant(time_t now
)
1546 if (any_predicted_circuits(now
))
1549 /* see if we'll still need to build testing circuits */
1550 if (server_mode(get_options()) &&
1551 (!check_whether_orport_reachable() || !circuit_enough_testing_circs()))
1553 if (!check_whether_dirport_reachable())
1559 static uint32_t n_signed_dir_objs
= 0;
1560 static uint32_t n_signed_routerdescs
= 0;
1561 static uint32_t n_verified_dir_objs
= 0;
1562 static uint32_t n_verified_routerdescs
= 0;
1563 static uint32_t n_onionskins_encrypted
= 0;
1564 static uint32_t n_onionskins_decrypted
= 0;
1565 static uint32_t n_tls_client_handshakes
= 0;
1566 static uint32_t n_tls_server_handshakes
= 0;
1567 static uint32_t n_rend_client_ops
= 0;
1568 static uint32_t n_rend_mid_ops
= 0;
1569 static uint32_t n_rend_server_ops
= 0;
1571 /** Increment the count of the number of times we've done <b>operation</b>. */
1573 note_crypto_pk_op(pk_op_t operation
)
1578 n_signed_dir_objs
++;
1581 n_signed_routerdescs
++;
1584 n_verified_dir_objs
++;
1587 n_verified_routerdescs
++;
1590 n_onionskins_encrypted
++;
1593 n_onionskins_decrypted
++;
1595 case TLS_HANDSHAKE_C
:
1596 n_tls_client_handshakes
++;
1598 case TLS_HANDSHAKE_S
:
1599 n_tls_server_handshakes
++;
1602 n_rend_client_ops
++;
1608 n_rend_server_ops
++;
1611 log_warn(LD_BUG
, "Unknown pk operation %d", operation
);
1615 /** Log the number of times we've done each public/private-key operation. */
1617 dump_pk_ops(int severity
)
1619 log(severity
, LD_GENERAL
,
1620 "PK operations: %lu directory objects signed, "
1621 "%lu directory objects verified, "
1622 "%lu routerdescs signed, "
1623 "%lu routerdescs verified, "
1624 "%lu onionskins encrypted, "
1625 "%lu onionskins decrypted, "
1626 "%lu client-side TLS handshakes, "
1627 "%lu server-side TLS handshakes, "
1628 "%lu rendezvous client operations, "
1629 "%lu rendezvous middle operations, "
1630 "%lu rendezvous server operations.",
1631 (unsigned long) n_signed_dir_objs
,
1632 (unsigned long) n_verified_dir_objs
,
1633 (unsigned long) n_signed_routerdescs
,
1634 (unsigned long) n_verified_routerdescs
,
1635 (unsigned long) n_onionskins_encrypted
,
1636 (unsigned long) n_onionskins_decrypted
,
1637 (unsigned long) n_tls_client_handshakes
,
1638 (unsigned long) n_tls_server_handshakes
,
1639 (unsigned long) n_rend_client_ops
,
1640 (unsigned long) n_rend_mid_ops
,
1641 (unsigned long) n_rend_server_ops
);
1644 /** Free all storage held by the OR/link history caches, by the
1645 * bandwidth history arrays, or by the port history. */
1647 rep_hist_free_all(void)
1649 digestmap_free(history_map
, free_or_history
);
1650 tor_free(read_array
);
1651 tor_free(write_array
);
1652 predicted_ports_free();
1655 /****************** hidden service usage statistics ******************/
1657 /** How large are the intervals for which we track and report hidden service
1659 #define NUM_SECS_HS_USAGE_SUM_INTERVAL (15*60)
1660 /** How far in the past do we remember and publish hidden service use? */
1661 #define NUM_SECS_HS_USAGE_SUM_IS_VALID (24*60*60)
1662 /** How many hidden service usage intervals do we remember? (derived) */
1663 #define NUM_TOTALS_HS_USAGE (NUM_SECS_HS_USAGE_SUM_IS_VALID/ \
1664 NUM_SECS_HS_USAGE_SUM_INTERVAL)
1666 /** List element containing a service id and the count. */
1667 typedef struct hs_usage_list_elem_t
{
1668 /** Service id of this elem. */
1669 char service_id
[REND_SERVICE_ID_LEN_BASE32
+1];
1670 /** Number of occurrences for the given service id. */
1672 /* Pointer to next list elem */
1673 struct hs_usage_list_elem_t
*next
;
1674 } hs_usage_list_elem_t
;
1676 /** Ordered list that stores service ids and the number of observations. It is
1677 * ordered by the number of occurrences in descending order. Its purpose is to
1678 * calculate the frequency distribution when the period is over. */
1679 typedef struct hs_usage_list_t
{
1680 /* Pointer to the first element in the list. */
1681 hs_usage_list_elem_t
*start
;
1682 /* Number of total occurrences for all list elements. */
1683 uint32_t total_count
;
1684 /* Number of service ids, i.e. number of list elements. */
1685 uint32_t total_service_ids
;
1688 /** Tracks service-related observations in the current period and their
1690 typedef struct hs_usage_service_related_observation_t
{
1691 /** Ordered list that stores service ids and the number of observations in
1692 * the current period. It is ordered by the number of occurrences in
1693 * descending order. Its purpose is to calculate the frequency distribution
1694 * when the period is over. */
1695 hs_usage_list_t
*list
;
1696 /** Circular arrays that store the history of observations. totals stores all
1697 * observations, twenty (ten, five) the number of observations related to a
1698 * service id being accounted for the top 20 (10, 5) percent of all
1700 uint32_t totals
[NUM_TOTALS_HS_USAGE
];
1701 uint32_t five
[NUM_TOTALS_HS_USAGE
];
1702 uint32_t ten
[NUM_TOTALS_HS_USAGE
];
1703 uint32_t twenty
[NUM_TOTALS_HS_USAGE
];
1704 } hs_usage_service_related_observation_t
;
1706 /** Tracks the history of general period-related observations, i.e. those that
1707 * cannot be related to a specific service id. */
1708 typedef struct hs_usage_general_period_related_observations_t
{
1709 /** Circular array that stores the history of observations. */
1710 uint32_t totals
[NUM_TOTALS_HS_USAGE
];
1711 } hs_usage_general_period_related_observations_t
;
1713 /** Keeps information about the current observation period and its relation to
1714 * the histories of observations. */
1715 typedef struct hs_usage_current_observation_period_t
{
1716 /** Where do we write the next history entry? */
1718 /** How many values in history have been set ever? (upper bound!) */
1720 /** When did this period begin? */
1721 time_t start_of_current_period
;
1722 /** When does the next period begin? */
1723 time_t start_of_next_period
;
1724 } hs_usage_current_observation_period_t
;
1726 static hs_usage_current_observation_period_t
*current_period
= NULL
;
1727 static hs_usage_service_related_observation_t
*publish_total
= NULL
;
1728 static hs_usage_service_related_observation_t
*publish_novel
= NULL
;
1729 static hs_usage_service_related_observation_t
*fetch_total
= NULL
;
1730 static hs_usage_service_related_observation_t
*fetch_successful
= NULL
;
1731 static hs_usage_general_period_related_observations_t
*descs
= NULL
;
1733 /** Creates an empty ordered list element. */
1734 static hs_usage_list_elem_t
*
1735 hs_usage_list_elem_new(void)
1737 hs_usage_list_elem_t
*e
;
1738 e
= tor_malloc_zero(sizeof(hs_usage_list_elem_t
));
1739 rephist_total_alloc
+= sizeof(hs_usage_list_elem_t
);
1745 /** Creates an empty ordered list. */
1746 static hs_usage_list_t
*
1747 hs_usage_list_new(void)
1750 l
= tor_malloc_zero(sizeof(hs_usage_list_t
));
1751 rephist_total_alloc
+= sizeof(hs_usage_list_t
);
1754 l
->total_service_ids
= 0;
1758 /** Creates an empty structure for storing service-related observations. */
1759 static hs_usage_service_related_observation_t
*
1760 hs_usage_service_related_observation_new(void)
1762 hs_usage_service_related_observation_t
*h
;
1763 h
= tor_malloc_zero(sizeof(hs_usage_service_related_observation_t
));
1764 rephist_total_alloc
+= sizeof(hs_usage_service_related_observation_t
);
1765 h
->list
= hs_usage_list_new();
1769 /** Creates an empty structure for storing general period-related
1771 static hs_usage_general_period_related_observations_t
*
1772 hs_usage_general_period_related_observations_new(void)
1774 hs_usage_general_period_related_observations_t
*p
;
1775 p
= tor_malloc_zero(sizeof(hs_usage_general_period_related_observations_t
));
1776 rephist_total_alloc
+= sizeof(hs_usage_general_period_related_observations_t
);
1780 /** Creates an empty structure for storing period-specific information. */
1781 static hs_usage_current_observation_period_t
*
1782 hs_usage_current_observation_period_new(void)
1784 hs_usage_current_observation_period_t
*c
;
1786 c
= tor_malloc_zero(sizeof(hs_usage_current_observation_period_t
));
1787 rephist_total_alloc
+= sizeof(hs_usage_current_observation_period_t
);
1789 c
->start_of_current_period
= now
;
1790 c
->start_of_next_period
= now
+ NUM_SECS_HS_USAGE_SUM_INTERVAL
;
1794 /** Initializes the structures for collecting hidden service usage data. */
1798 current_period
= hs_usage_current_observation_period_new();
1799 publish_total
= hs_usage_service_related_observation_new();
1800 publish_novel
= hs_usage_service_related_observation_new();
1801 fetch_total
= hs_usage_service_related_observation_new();
1802 fetch_successful
= hs_usage_service_related_observation_new();
1803 descs
= hs_usage_general_period_related_observations_new();
1806 /** Clears the given ordered list by resetting its attributes and releasing
1807 * the memory allocated by its elements. */
1809 hs_usage_list_clear(hs_usage_list_t
*lst
)
1811 /* walk through elements and free memory */
1812 hs_usage_list_elem_t
*current
= lst
->start
;
1813 hs_usage_list_elem_t
*tmp
;
1814 while (current
!= NULL
) {
1815 tmp
= current
->next
;
1816 rephist_total_alloc
-= sizeof(hs_usage_list_elem_t
);
1820 /* reset attributes */
1822 lst
->total_count
= 0;
1823 lst
->total_service_ids
= 0;
1827 /** Frees the memory used by the given list. */
1829 hs_usage_list_free(hs_usage_list_t
*lst
)
1833 hs_usage_list_clear(lst
);
1834 rephist_total_alloc
-= sizeof(hs_usage_list_t
);
1838 /** Frees the memory used by the given service-related observations. */
1840 hs_usage_service_related_observation_free(
1841 hs_usage_service_related_observation_t
*s
)
1845 hs_usage_list_free(s
->list
);
1846 rephist_total_alloc
-= sizeof(hs_usage_service_related_observation_t
);
1850 /** Frees the memory used by the given period-specific observations. */
1852 hs_usage_general_period_related_observations_free(
1853 hs_usage_general_period_related_observations_t
*s
)
1855 rephist_total_alloc
-=sizeof(hs_usage_general_period_related_observations_t
);
1859 /** Frees the memory used by period-specific information. */
1861 hs_usage_current_observation_period_free(
1862 hs_usage_current_observation_period_t
*s
)
1864 rephist_total_alloc
-= sizeof(hs_usage_current_observation_period_t
);
1868 /** Frees all memory that was used for collecting hidden service usage data. */
1870 hs_usage_free_all(void)
1872 hs_usage_general_period_related_observations_free(descs
);
1874 hs_usage_service_related_observation_free(fetch_successful
);
1875 hs_usage_service_related_observation_free(fetch_total
);
1876 hs_usage_service_related_observation_free(publish_novel
);
1877 hs_usage_service_related_observation_free(publish_total
);
1878 fetch_successful
= fetch_total
= publish_novel
= publish_total
= NULL
;
1879 hs_usage_current_observation_period_free(current_period
);
1880 current_period
= NULL
;
1883 /** Inserts a new occurrence for the given service id to the given ordered
1886 hs_usage_insert_value(hs_usage_list_t
*lst
, const char *service_id
)
1888 /* search if there is already an elem with same service_id in list */
1889 hs_usage_list_elem_t
*current
= lst
->start
;
1890 hs_usage_list_elem_t
*previous
= NULL
;
1891 while (current
!= NULL
&& strcasecmp(current
->service_id
,service_id
)) {
1893 current
= current
->next
;
1895 /* found an element with same service_id? */
1896 if (current
== NULL
) {
1897 /* not found! append to end (which could also be the end of a zero-length
1898 * list), don't need to sort (1 is smallest value). */
1900 hs_usage_list_elem_t
*e
= hs_usage_list_elem_new();
1901 /* update list attributes (one new elem, one new occurrence) */
1903 lst
->total_service_ids
++;
1904 /* copy service id to elem */
1905 strlcpy(e
->service_id
,service_id
,sizeof(e
->service_id
));
1906 /* let either l->start or previously last elem point to new elem */
1907 if (lst
->start
== NULL
) {
1908 /* this is the first elem */
1911 /* there were elems in the list before */
1915 /* found! add occurrence to elem and consider resorting */
1916 /* update list attributes (no new elem, but one new occurrence) */
1918 /* add occurrence to elem */
1920 /* is it another than the first list elem? and has previous elem fewer
1921 * count than current? then we need to resort */
1922 if (previous
!= NULL
&& previous
->count
< current
->count
) {
1923 /* yes! we need to resort */
1924 /* remove current elem first */
1925 previous
->next
= current
->next
;
1926 /* can we prepend elem to all other elements? */
1927 if (lst
->start
->count
<= current
->count
) {
1928 /* yes! prepend elem */
1929 current
->next
= lst
->start
;
1930 lst
->start
= current
;
1932 /* no! walk through list a second time and insert at correct place */
1933 hs_usage_list_elem_t
*insert_current
= lst
->start
->next
;
1934 hs_usage_list_elem_t
*insert_previous
= lst
->start
;
1935 while (insert_current
!= NULL
&&
1936 insert_current
->count
> current
->count
) {
1937 insert_previous
= insert_current
;
1938 insert_current
= insert_current
->next
;
1941 current
->next
= insert_current
;
1942 insert_previous
->next
= current
;
1948 /** Writes the current service-related observations to the history array and
1949 * clears the observations of the current period. */
1951 hs_usage_write_service_related_observations_to_history(
1952 hs_usage_current_observation_period_t
*p
,
1953 hs_usage_service_related_observation_t
*h
)
1955 /* walk through the first 20 % of list elements and calculate frequency
1957 /* maximum indices for the three frequencies */
1958 int five_percent_idx
= h
->list
->total_service_ids
/20;
1959 int ten_percent_idx
= h
->list
->total_service_ids
/10;
1960 int twenty_percent_idx
= h
->list
->total_service_ids
/5;
1962 uint32_t five_percent
= 0;
1963 uint32_t ten_percent
= 0;
1964 uint32_t twenty_percent
= 0;
1965 /* walk through list */
1966 hs_usage_list_elem_t
*current
= h
->list
->start
;
1968 while (current
!= NULL
&& i
<= twenty_percent_idx
) {
1969 twenty_percent
+= current
->count
;
1970 if (i
<= ten_percent_idx
)
1971 ten_percent
+= current
->count
;
1972 if (i
<= five_percent_idx
)
1973 five_percent
+= current
->count
;
1974 current
= current
->next
;
1977 /* copy frequencies */
1978 h
->twenty
[p
->next_idx
] = twenty_percent
;
1979 h
->ten
[p
->next_idx
] = ten_percent
;
1980 h
->five
[p
->next_idx
] = five_percent
;
1981 /* copy total number of observations */
1982 h
->totals
[p
->next_idx
] = h
->list
->total_count
;
1983 /* free memory of old list */
1984 hs_usage_list_clear(h
->list
);
1987 /** Advances to next observation period. */
1989 hs_usage_advance_current_observation_period(void)
1991 /* aggregate observations to history, including frequency distribution
1993 hs_usage_write_service_related_observations_to_history(
1994 current_period
, publish_total
);
1995 hs_usage_write_service_related_observations_to_history(
1996 current_period
, publish_novel
);
1997 hs_usage_write_service_related_observations_to_history(
1998 current_period
, fetch_total
);
1999 hs_usage_write_service_related_observations_to_history(
2000 current_period
, fetch_successful
);
2001 /* write current number of descriptors to descs history */
2002 descs
->totals
[current_period
->next_idx
] = rend_cache_size();
2003 /* advance to next period */
2004 current_period
->next_idx
++;
2005 if (current_period
->next_idx
== NUM_TOTALS_HS_USAGE
)
2006 current_period
->next_idx
= 0;
2007 if (current_period
->num_set
< NUM_TOTALS_HS_USAGE
)
2008 ++current_period
->num_set
;
2009 current_period
->start_of_current_period
=current_period
->start_of_next_period
;
2010 current_period
->start_of_next_period
+= NUM_SECS_HS_USAGE_SUM_INTERVAL
;
2013 /** Checks if the current period is up to date, and if not, advances it. */
2015 hs_usage_check_if_current_period_is_up_to_date(time_t now
)
2017 while (now
> current_period
->start_of_next_period
) {
2018 hs_usage_advance_current_observation_period();
2022 /** Adds a service-related observation, maybe after advancing to next
2023 * observation period. */
2025 hs_usage_add_service_related_observation(
2026 hs_usage_service_related_observation_t
*h
,
2028 const char *service_id
)
2030 if (now
< current_period
->start_of_current_period
) {
2031 /* don't record old data */
2034 /* check if we are up-to-date */
2035 hs_usage_check_if_current_period_is_up_to_date(now
);
2036 /* add observation */
2037 hs_usage_insert_value(h
->list
, service_id
);
2040 /** Adds the observation of storing a rendezvous service descriptor to our
2041 * cache in our role as HS authoritative directory. */
2043 hs_usage_note_publish_total(const char *service_id
, time_t now
)
2045 hs_usage_add_service_related_observation(publish_total
, now
, service_id
);
2048 /** Adds the observation of storing a novel rendezvous service descriptor to
2049 * our cache in our role as HS authoritative directory. */
2051 hs_usage_note_publish_novel(const char *service_id
, time_t now
)
2053 hs_usage_add_service_related_observation(publish_novel
, now
, service_id
);
2056 /** Adds the observation of being requested for a rendezvous service descriptor
2057 * in our role as HS authoritative directory. */
2059 hs_usage_note_fetch_total(const char *service_id
, time_t now
)
2061 hs_usage_add_service_related_observation(fetch_total
, now
, service_id
);
2064 /** Adds the observation of being requested for a rendezvous service descriptor
2065 * in our role as HS authoritative directory and being able to answer that
2066 * request successfully. */
2068 hs_usage_note_fetch_successful(const char *service_id
, time_t now
)
2070 hs_usage_add_service_related_observation(fetch_successful
, now
, service_id
);
2073 /** Writes the given circular array to a string. */
2075 hs_usage_format_history(char *buf
, size_t len
, uint32_t *data
)
2077 char *cp
= buf
; /* pointer where we are in the buffer */
2079 if (current_period
->num_set
<= current_period
->next_idx
) {
2080 i
= 0; /* not been through circular array */
2082 i
= current_period
->next_idx
;
2084 for (n
= 0; n
< current_period
->num_set
; ++n
,++i
) {
2085 if (i
>= NUM_TOTALS_HS_USAGE
)
2086 i
-= NUM_TOTALS_HS_USAGE
;
2087 tor_assert(i
< NUM_TOTALS_HS_USAGE
);
2088 if (n
== (current_period
->num_set
-1))
2089 tor_snprintf(cp
, len
-(cp
-buf
), "%d", data
[i
]);
2091 tor_snprintf(cp
, len
-(cp
-buf
), "%d,", data
[i
]);
2097 /** Writes the complete usage history as hidden service authoritative directory
2100 hs_usage_format_statistics(void)
2102 char *buf
, *cp
, *s
= NULL
;
2103 char t
[ISO_TIME_LEN
+1];
2105 uint32_t *data
= NULL
;
2107 len
= (70+20*NUM_TOTALS_HS_USAGE
)*11;
2108 buf
= tor_malloc_zero(len
);
2110 for (r
= 0; r
< 11; ++r
) {
2113 s
= (char*) "publish-total-history";
2114 data
= publish_total
->totals
;
2117 s
= (char*) "publish-novel-history";
2118 data
= publish_novel
->totals
;
2121 s
= (char*) "publish-top-5-percent-history";
2122 data
= publish_total
->five
;
2125 s
= (char*) "publish-top-10-percent-history";
2126 data
= publish_total
->ten
;
2129 s
= (char*) "publish-top-20-percent-history";
2130 data
= publish_total
->twenty
;
2133 s
= (char*) "fetch-total-history";
2134 data
= fetch_total
->totals
;
2137 s
= (char*) "fetch-successful-history";
2138 data
= fetch_successful
->totals
;
2141 s
= (char*) "fetch-top-5-percent-history";
2142 data
= fetch_total
->five
;
2145 s
= (char*) "fetch-top-10-percent-history";
2146 data
= fetch_total
->ten
;
2149 s
= (char*) "fetch-top-20-percent-history";
2150 data
= fetch_total
->twenty
;
2153 s
= (char*) "desc-total-history";
2154 data
= descs
->totals
;
2157 format_iso_time(t
, current_period
->start_of_current_period
);
2158 tor_snprintf(cp
, len
-(cp
-buf
), "%s %s (%d s) ", s
, t
,
2159 NUM_SECS_HS_USAGE_SUM_INTERVAL
);
2161 cp
+= hs_usage_format_history(cp
, len
-(cp
-buf
), data
);
2162 strlcat(cp
, "\n", len
-(cp
-buf
));
2168 /** Write current statistics about hidden service usage to file. */
2170 hs_usage_write_statistics_to_file(time_t now
)
2175 or_options_t
*options
= get_options();
2176 /* check if we are up-to-date */
2177 hs_usage_check_if_current_period_is_up_to_date(now
);
2178 buf
= hs_usage_format_statistics();
2179 len
= strlen(options
->DataDirectory
) + 16;
2180 fname
= tor_malloc(len
);
2181 tor_snprintf(fname
, len
, "%s"PATH_SEPARATOR
"hsusage",
2182 options
->DataDirectory
);
2183 write_str_to_file(fname
,buf
,0);