Make changes to latest bridge-stats fixes as suggested by Nick.
[tor/rransom.git] / src / or / rephist.c
blob681588bff7101ba193b05e6b643c09b043b385e3
1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2009, 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.
10 **/
12 #include "or.h"
13 #include "ht.h"
15 static void bw_arrays_init(void);
16 static void predicted_ports_init(void);
17 static void hs_usage_init(void);
19 /** Total number of bytes currently allocated in fields used by rephist.c. */
20 uint64_t rephist_total_alloc=0;
21 /** Number of or_history_t objects currently allocated. */
22 uint32_t rephist_total_num=0;
24 /** If the total weighted run count of all runs for a router ever falls
25 * below this amount, the router can be treated as having 0 MTBF. */
26 #define STABILITY_EPSILON 0.0001
27 /** Value by which to discount all old intervals for MTBF purposes. This
28 * is compounded every STABILITY_INTERVAL. */
29 #define STABILITY_ALPHA 0.95
30 /** Interval at which to discount all old intervals for MTBF purposes. */
31 #define STABILITY_INTERVAL (12*60*60)
32 /* (This combination of ALPHA, INTERVAL, and EPSILON makes it so that an
33 * interval that just ended counts twice as much as one that ended a week ago,
34 * 20X as much as one that ended a month ago, and routers that have had no
35 * uptime data for about half a year will get forgotten.) */
37 /** History of an OR-\>OR link. */
38 typedef struct link_history_t {
39 /** When did we start tracking this list? */
40 time_t since;
41 /** When did we most recently note a change to this link */
42 time_t changed;
43 /** How many times did extending from OR1 to OR2 succeed? */
44 unsigned long n_extend_ok;
45 /** How many times did extending from OR1 to OR2 fail? */
46 unsigned long n_extend_fail;
47 } link_history_t;
49 /** History of an OR. */
50 typedef struct or_history_t {
51 /** When did we start tracking this OR? */
52 time_t since;
53 /** When did we most recently note a change to this OR? */
54 time_t changed;
55 /** How many times did we successfully connect? */
56 unsigned long n_conn_ok;
57 /** How many times did we try to connect and fail?*/
58 unsigned long n_conn_fail;
59 /** How many seconds have we been connected to this OR before
60 * 'up_since'? */
61 unsigned long uptime;
62 /** How many seconds have we been unable to connect to this OR before
63 * 'down_since'? */
64 unsigned long downtime;
65 /** If nonzero, we have been connected since this time. */
66 time_t up_since;
67 /** If nonzero, we have been unable to connect since this time. */
68 time_t down_since;
70 /* === For MTBF tracking: */
71 /** Weighted sum total of all times that this router has been online.
73 unsigned long weighted_run_length;
74 /** If the router is now online (according to stability-checking rules),
75 * when did it come online? */
76 time_t start_of_run;
77 /** Sum of weights for runs in weighted_run_length. */
78 double total_run_weights;
79 /* === For fractional uptime tracking: */
80 time_t start_of_downtime;
81 unsigned long weighted_uptime;
82 unsigned long total_weighted_time;
84 /** Map from hex OR2 identity digest to a link_history_t for the link
85 * from this OR to OR2. */
86 digestmap_t *link_history_map;
87 } or_history_t;
89 /** When did we last multiply all routers' weighted_run_length and
90 * total_run_weights by STABILITY_ALPHA? */
91 static time_t stability_last_downrated = 0;
93 /** */
94 static time_t started_tracking_stability = 0;
96 /** Map from hex OR identity digest to or_history_t. */
97 static digestmap_t *history_map = NULL;
99 /** Return the or_history_t for the OR with identity digest <b>id</b>,
100 * creating it if necessary. */
101 static or_history_t *
102 get_or_history(const char* id)
104 or_history_t *hist;
106 if (tor_mem_is_zero(id, DIGEST_LEN))
107 return NULL;
109 hist = digestmap_get(history_map, id);
110 if (!hist) {
111 hist = tor_malloc_zero(sizeof(or_history_t));
112 rephist_total_alloc += sizeof(or_history_t);
113 rephist_total_num++;
114 hist->link_history_map = digestmap_new();
115 hist->since = hist->changed = time(NULL);
116 digestmap_set(history_map, id, hist);
118 return hist;
121 /** Return the link_history_t for the link from the first named OR to
122 * the second, creating it if necessary. (ORs are identified by
123 * identity digest.)
125 static link_history_t *
126 get_link_history(const char *from_id, const char *to_id)
128 or_history_t *orhist;
129 link_history_t *lhist;
130 orhist = get_or_history(from_id);
131 if (!orhist)
132 return NULL;
133 if (tor_mem_is_zero(to_id, DIGEST_LEN))
134 return NULL;
135 lhist = (link_history_t*) digestmap_get(orhist->link_history_map, to_id);
136 if (!lhist) {
137 lhist = tor_malloc_zero(sizeof(link_history_t));
138 rephist_total_alloc += sizeof(link_history_t);
139 lhist->since = lhist->changed = time(NULL);
140 digestmap_set(orhist->link_history_map, to_id, lhist);
142 return lhist;
145 /** Helper: free storage held by a single link history entry. */
146 static void
147 _free_link_history(void *val)
149 rephist_total_alloc -= sizeof(link_history_t);
150 tor_free(val);
153 /** Helper: free storage held by a single OR history entry. */
154 static void
155 free_or_history(void *_hist)
157 or_history_t *hist = _hist;
158 digestmap_free(hist->link_history_map, _free_link_history);
159 rephist_total_alloc -= sizeof(or_history_t);
160 rephist_total_num--;
161 tor_free(hist);
164 /** Update an or_history_t object <b>hist</b> so that its uptime/downtime
165 * count is up-to-date as of <b>when</b>.
167 static void
168 update_or_history(or_history_t *hist, time_t when)
170 tor_assert(hist);
171 if (hist->up_since) {
172 tor_assert(!hist->down_since);
173 hist->uptime += (when - hist->up_since);
174 hist->up_since = when;
175 } else if (hist->down_since) {
176 hist->downtime += (when - hist->down_since);
177 hist->down_since = when;
181 /** Initialize the static data structures for tracking history. */
182 void
183 rep_hist_init(void)
185 history_map = digestmap_new();
186 bw_arrays_init();
187 predicted_ports_init();
188 hs_usage_init();
191 /** Helper: note that we are no longer connected to the router with history
192 * <b>hist</b>. If <b>failed</b>, the connection failed; otherwise, it was
193 * closed correctly. */
194 static void
195 mark_or_down(or_history_t *hist, time_t when, int failed)
197 if (hist->up_since) {
198 hist->uptime += (when - hist->up_since);
199 hist->up_since = 0;
201 if (failed && !hist->down_since) {
202 hist->down_since = when;
206 /** Helper: note that we are connected to the router with history
207 * <b>hist</b>. */
208 static void
209 mark_or_up(or_history_t *hist, time_t when)
211 if (hist->down_since) {
212 hist->downtime += (when - hist->down_since);
213 hist->down_since = 0;
215 if (!hist->up_since) {
216 hist->up_since = when;
220 /** Remember that an attempt to connect to the OR with identity digest
221 * <b>id</b> failed at <b>when</b>.
223 void
224 rep_hist_note_connect_failed(const char* id, time_t when)
226 or_history_t *hist;
227 hist = get_or_history(id);
228 if (!hist)
229 return;
230 ++hist->n_conn_fail;
231 mark_or_down(hist, when, 1);
232 hist->changed = when;
235 /** Remember that an attempt to connect to the OR with identity digest
236 * <b>id</b> succeeded at <b>when</b>.
238 void
239 rep_hist_note_connect_succeeded(const char* id, time_t when)
241 or_history_t *hist;
242 hist = get_or_history(id);
243 if (!hist)
244 return;
245 ++hist->n_conn_ok;
246 mark_or_up(hist, when);
247 hist->changed = when;
250 /** Remember that we intentionally closed our connection to the OR
251 * with identity digest <b>id</b> at <b>when</b>.
253 void
254 rep_hist_note_disconnect(const char* id, time_t when)
256 or_history_t *hist;
257 hist = get_or_history(id);
258 if (!hist)
259 return;
260 mark_or_down(hist, when, 0);
261 hist->changed = when;
264 /** Remember that our connection to the OR with identity digest
265 * <b>id</b> had an error and stopped working at <b>when</b>.
267 void
268 rep_hist_note_connection_died(const char* id, time_t when)
270 or_history_t *hist;
271 if (!id) {
272 /* If conn has no identity, it didn't complete its handshake, or something
273 * went wrong. Ignore it.
275 return;
277 hist = get_or_history(id);
278 if (!hist)
279 return;
280 mark_or_down(hist, when, 1);
281 hist->changed = when;
284 /** We have just decided that this router with identity digest <b>id</b> is
285 * reachable, meaning we will give it a "Running" flag for the next while. */
286 void
287 rep_hist_note_router_reachable(const char *id, time_t when)
289 or_history_t *hist = get_or_history(id);
290 int was_in_run = 1;
291 char tbuf[ISO_TIME_LEN+1];
293 tor_assert(hist);
295 if (!started_tracking_stability)
296 started_tracking_stability = time(NULL);
297 if (!hist->start_of_run) {
298 hist->start_of_run = when;
299 was_in_run = 0;
301 if (hist->start_of_downtime) {
302 long down_length;
304 format_local_iso_time(tbuf, hist->start_of_downtime);
305 log_info(LD_HIST, "Router %s is now Running; it had been down since %s.",
306 hex_str(id, DIGEST_LEN), tbuf);
307 if (was_in_run)
308 log_info(LD_HIST, " (Paradoxically, it was already Running too.)");
310 down_length = when - hist->start_of_downtime;
311 hist->total_weighted_time += down_length;
312 hist->start_of_downtime = 0;
313 } else {
314 format_local_iso_time(tbuf, hist->start_of_run);
315 if (was_in_run)
316 log_debug(LD_HIST, "Router %s is still Running; it has been Running "
317 "since %s", hex_str(id, DIGEST_LEN), tbuf);
318 else
319 log_info(LD_HIST,"Router %s is now Running; it was previously untracked",
320 hex_str(id, DIGEST_LEN));
324 /** We have just decided that this router is unreachable, meaning
325 * we are taking away its "Running" flag. */
326 void
327 rep_hist_note_router_unreachable(const char *id, time_t when)
329 or_history_t *hist = get_or_history(id);
330 char tbuf[ISO_TIME_LEN+1];
331 int was_running = 0;
332 if (!started_tracking_stability)
333 started_tracking_stability = time(NULL);
335 tor_assert(hist);
336 if (hist->start_of_run) {
337 /*XXXX We could treat failed connections differently from failed
338 * connect attempts. */
339 long run_length = when - hist->start_of_run;
340 format_local_iso_time(tbuf, hist->start_of_run);
342 hist->weighted_run_length += run_length;
343 hist->total_run_weights += 1.0;
344 hist->start_of_run = 0;
345 hist->weighted_uptime += run_length;
346 hist->total_weighted_time += run_length;
348 was_running = 1;
349 log_info(LD_HIST, "Router %s is now non-Running: it had previously been "
350 "Running since %s. Its total weighted uptime is %lu/%lu.",
351 hex_str(id, DIGEST_LEN), tbuf, hist->weighted_uptime,
352 hist->total_weighted_time);
354 if (!hist->start_of_downtime) {
355 hist->start_of_downtime = when;
357 if (!was_running)
358 log_info(LD_HIST, "Router %s is now non-Running; it was previously "
359 "untracked.", hex_str(id, DIGEST_LEN));
360 } else {
361 if (!was_running) {
362 format_local_iso_time(tbuf, hist->start_of_downtime);
364 log_info(LD_HIST, "Router %s is still non-Running; it has been "
365 "non-Running since %s.", hex_str(id, DIGEST_LEN), tbuf);
370 /** Helper: Discount all old MTBF data, if it is time to do so. Return
371 * the time at which we should next discount MTBF data. */
372 time_t
373 rep_hist_downrate_old_runs(time_t now)
375 digestmap_iter_t *orhist_it;
376 const char *digest1;
377 or_history_t *hist;
378 void *hist_p;
379 double alpha = 1.0;
381 if (!history_map)
382 history_map = digestmap_new();
383 if (!stability_last_downrated)
384 stability_last_downrated = now;
385 if (stability_last_downrated + STABILITY_INTERVAL > now)
386 return stability_last_downrated + STABILITY_INTERVAL;
388 /* Okay, we should downrate the data. By how much? */
389 while (stability_last_downrated + STABILITY_INTERVAL < now) {
390 stability_last_downrated += STABILITY_INTERVAL;
391 alpha *= STABILITY_ALPHA;
394 log_info(LD_HIST, "Discounting all old stability info by a factor of %lf",
395 alpha);
397 /* Multiply every w_r_l, t_r_w pair by alpha. */
398 for (orhist_it = digestmap_iter_init(history_map);
399 !digestmap_iter_done(orhist_it);
400 orhist_it = digestmap_iter_next(history_map,orhist_it)) {
401 digestmap_iter_get(orhist_it, &digest1, &hist_p);
402 hist = hist_p;
404 hist->weighted_run_length =
405 (unsigned long)(hist->weighted_run_length * alpha);
406 hist->total_run_weights *= alpha;
408 hist->weighted_uptime = (unsigned long)(hist->weighted_uptime * alpha);
409 hist->total_weighted_time = (unsigned long)
410 (hist->total_weighted_time * alpha);
413 return stability_last_downrated + STABILITY_INTERVAL;
416 /** Helper: Return the weighted MTBF of the router with history <b>hist</b>. */
417 static double
418 get_stability(or_history_t *hist, time_t when)
420 unsigned long total = hist->weighted_run_length;
421 double total_weights = hist->total_run_weights;
423 if (hist->start_of_run) {
424 /* We're currently in a run. Let total and total_weights hold the values
425 * they would hold if the current run were to end now. */
426 total += (when-hist->start_of_run);
427 total_weights += 1.0;
429 if (total_weights < STABILITY_EPSILON) {
430 /* Round down to zero, and avoid divide-by-zero. */
431 return 0.0;
434 return total / total_weights;
437 /** Return the total amount of time we've been observing, with each run of
438 * time downrated by the appropriate factor. */
439 static long
440 get_total_weighted_time(or_history_t *hist, time_t when)
442 long total = hist->total_weighted_time;
443 if (hist->start_of_run) {
444 total += (when - hist->start_of_run);
445 } else if (hist->start_of_downtime) {
446 total += (when - hist->start_of_downtime);
448 return total;
451 /** Helper: Return the weighted percent-of-time-online of the router with
452 * history <b>hist</b>. */
453 static double
454 get_weighted_fractional_uptime(or_history_t *hist, time_t when)
456 unsigned long total = hist->total_weighted_time;
457 unsigned long up = hist->weighted_uptime;
459 if (hist->start_of_run) {
460 long run_length = (when - hist->start_of_run);
461 up += run_length;
462 total += run_length;
463 } else if (hist->start_of_downtime) {
464 total += (when - hist->start_of_downtime);
467 if (!total) {
468 /* Avoid calling anybody's uptime infinity (which should be impossible if
469 * the code is working), or NaN (which can happen for any router we haven't
470 * observed up or down yet). */
471 return 0.0;
474 return ((double) up) / total;
477 /** Return an estimated MTBF for the router whose identity digest is
478 * <b>id</b>. Return 0 if the router is unknown. */
479 double
480 rep_hist_get_stability(const char *id, time_t when)
482 or_history_t *hist = get_or_history(id);
483 if (!hist)
484 return 0.0;
486 return get_stability(hist, when);
489 /** Return an estimated percent-of-time-online for the router whose identity
490 * digest is <b>id</b>. Return 0 if the router is unknown. */
491 double
492 rep_hist_get_weighted_fractional_uptime(const char *id, time_t when)
494 or_history_t *hist = get_or_history(id);
495 if (!hist)
496 return 0.0;
498 return get_weighted_fractional_uptime(hist, when);
501 /** Return a number representing how long we've known about the router whose
502 * digest is <b>id</b>. Return 0 if the router is unknown.
504 * Be careful: this measure increases monotonically as we know the router for
505 * longer and longer, but it doesn't increase linearly.
507 long
508 rep_hist_get_weighted_time_known(const char *id, time_t when)
510 or_history_t *hist = get_or_history(id);
511 if (!hist)
512 return 0;
514 return get_total_weighted_time(hist, when);
517 /** Return true if we've been measuring MTBFs for long enough to
518 * pronounce on Stability. */
520 rep_hist_have_measured_enough_stability(void)
522 /* XXXX021 This doesn't do so well when we change our opinion
523 * as to whether we're tracking router stability. */
524 return started_tracking_stability < time(NULL) - 4*60*60;
527 /** Remember that we successfully extended from the OR with identity
528 * digest <b>from_id</b> to the OR with identity digest
529 * <b>to_name</b>.
531 void
532 rep_hist_note_extend_succeeded(const char *from_id, const char *to_id)
534 link_history_t *hist;
535 /* log_fn(LOG_WARN, "EXTEND SUCCEEDED: %s->%s",from_name,to_name); */
536 hist = get_link_history(from_id, to_id);
537 if (!hist)
538 return;
539 ++hist->n_extend_ok;
540 hist->changed = time(NULL);
543 /** Remember that we tried to extend from the OR with identity digest
544 * <b>from_id</b> to the OR with identity digest <b>to_name</b>, but
545 * failed.
547 void
548 rep_hist_note_extend_failed(const char *from_id, const char *to_id)
550 link_history_t *hist;
551 /* log_fn(LOG_WARN, "EXTEND FAILED: %s->%s",from_name,to_name); */
552 hist = get_link_history(from_id, to_id);
553 if (!hist)
554 return;
555 ++hist->n_extend_fail;
556 hist->changed = time(NULL);
559 /** Log all the reliability data we have remembered, with the chosen
560 * severity.
562 void
563 rep_hist_dump_stats(time_t now, int severity)
565 digestmap_iter_t *lhist_it;
566 digestmap_iter_t *orhist_it;
567 const char *name1, *name2, *digest1, *digest2;
568 char hexdigest1[HEX_DIGEST_LEN+1];
569 or_history_t *or_history;
570 link_history_t *link_history;
571 void *or_history_p, *link_history_p;
572 double uptime;
573 char buffer[2048];
574 size_t len;
575 int ret;
576 unsigned long upt, downt;
577 routerinfo_t *r;
579 rep_history_clean(now - get_options()->RephistTrackTime);
581 log(severity, LD_HIST, "--------------- Dumping history information:");
583 for (orhist_it = digestmap_iter_init(history_map);
584 !digestmap_iter_done(orhist_it);
585 orhist_it = digestmap_iter_next(history_map,orhist_it)) {
586 double s;
587 long stability;
588 digestmap_iter_get(orhist_it, &digest1, &or_history_p);
589 or_history = (or_history_t*) or_history_p;
591 if ((r = router_get_by_digest(digest1)))
592 name1 = r->nickname;
593 else
594 name1 = "(unknown)";
595 base16_encode(hexdigest1, sizeof(hexdigest1), digest1, DIGEST_LEN);
596 update_or_history(or_history, now);
597 upt = or_history->uptime;
598 downt = or_history->downtime;
599 s = get_stability(or_history, now);
600 stability = (long)s;
601 if (upt+downt) {
602 uptime = ((double)upt) / (upt+downt);
603 } else {
604 uptime=1.0;
606 log(severity, LD_HIST,
607 "OR %s [%s]: %ld/%ld good connections; uptime %ld/%ld sec (%.2f%%); "
608 "wmtbf %lu:%02lu:%02lu",
609 name1, hexdigest1,
610 or_history->n_conn_ok, or_history->n_conn_fail+or_history->n_conn_ok,
611 upt, upt+downt, uptime*100.0,
612 stability/3600, (stability/60)%60, stability%60);
614 if (!digestmap_isempty(or_history->link_history_map)) {
615 strlcpy(buffer, " Extend attempts: ", sizeof(buffer));
616 len = strlen(buffer);
617 for (lhist_it = digestmap_iter_init(or_history->link_history_map);
618 !digestmap_iter_done(lhist_it);
619 lhist_it = digestmap_iter_next(or_history->link_history_map,
620 lhist_it)) {
621 digestmap_iter_get(lhist_it, &digest2, &link_history_p);
622 if ((r = router_get_by_digest(digest2)))
623 name2 = r->nickname;
624 else
625 name2 = "(unknown)";
627 link_history = (link_history_t*) link_history_p;
629 ret = tor_snprintf(buffer+len, 2048-len, "%s(%ld/%ld); ", name2,
630 link_history->n_extend_ok,
631 link_history->n_extend_ok+link_history->n_extend_fail);
632 if (ret<0)
633 break;
634 else
635 len += ret;
637 log(severity, LD_HIST, "%s", buffer);
642 /** Remove history info for routers/links that haven't changed since
643 * <b>before</b>.
645 void
646 rep_history_clean(time_t before)
648 int authority = authdir_mode(get_options());
649 or_history_t *or_history;
650 link_history_t *link_history;
651 void *or_history_p, *link_history_p;
652 digestmap_iter_t *orhist_it, *lhist_it;
653 const char *d1, *d2;
655 orhist_it = digestmap_iter_init(history_map);
656 while (!digestmap_iter_done(orhist_it)) {
657 int remove;
658 digestmap_iter_get(orhist_it, &d1, &or_history_p);
659 or_history = or_history_p;
661 remove = authority ? (or_history->total_run_weights < STABILITY_EPSILON &&
662 !or_history->start_of_run)
663 : (or_history->changed < before);
664 if (remove) {
665 orhist_it = digestmap_iter_next_rmv(history_map, orhist_it);
666 free_or_history(or_history);
667 continue;
669 for (lhist_it = digestmap_iter_init(or_history->link_history_map);
670 !digestmap_iter_done(lhist_it); ) {
671 digestmap_iter_get(lhist_it, &d2, &link_history_p);
672 link_history = link_history_p;
673 if (link_history->changed < before) {
674 lhist_it = digestmap_iter_next_rmv(or_history->link_history_map,
675 lhist_it);
676 rephist_total_alloc -= sizeof(link_history_t);
677 tor_free(link_history);
678 continue;
680 lhist_it = digestmap_iter_next(or_history->link_history_map,lhist_it);
682 orhist_it = digestmap_iter_next(history_map, orhist_it);
686 /** Write MTBF data to disk. Return 0 on success, negative on failure.
688 * If <b>missing_means_down</b>, then if we're about to write an entry
689 * that is still considered up but isn't in our routerlist, consider it
690 * to be down. */
692 rep_hist_record_mtbf_data(time_t now, int missing_means_down)
694 char time_buf[ISO_TIME_LEN+1];
696 digestmap_iter_t *orhist_it;
697 const char *digest;
698 void *or_history_p;
699 or_history_t *hist;
700 open_file_t *open_file = NULL;
701 FILE *f;
704 char *filename = get_datadir_fname("router-stability");
705 f = start_writing_to_stdio_file(filename, OPEN_FLAGS_REPLACE|O_TEXT, 0600,
706 &open_file);
707 tor_free(filename);
708 if (!f)
709 return -1;
712 /* File format is:
713 * FormatLine *KeywordLine Data
715 * FormatLine = "format 1" NL
716 * KeywordLine = Keyword SP Arguments NL
717 * Data = "data" NL *RouterMTBFLine "." NL
718 * RouterMTBFLine = Fingerprint SP WeightedRunLen SP
719 * TotalRunWeights [SP S=StartRunTime] NL
721 #define PUT(s) STMT_BEGIN if (fputs((s),f)<0) goto err; STMT_END
722 #define PRINTF(args) STMT_BEGIN if (fprintf args <0) goto err; STMT_END
724 PUT("format 2\n");
726 format_iso_time(time_buf, time(NULL));
727 PRINTF((f, "stored-at %s\n", time_buf));
729 if (started_tracking_stability) {
730 format_iso_time(time_buf, started_tracking_stability);
731 PRINTF((f, "tracked-since %s\n", time_buf));
733 if (stability_last_downrated) {
734 format_iso_time(time_buf, stability_last_downrated);
735 PRINTF((f, "last-downrated %s\n", time_buf));
738 PUT("data\n");
740 /* XXX Nick: now bridge auths record this for all routers too.
741 * Should we make them record it only for bridge routers? -RD
742 * Not for 0.2.0. -NM */
743 for (orhist_it = digestmap_iter_init(history_map);
744 !digestmap_iter_done(orhist_it);
745 orhist_it = digestmap_iter_next(history_map,orhist_it)) {
746 char dbuf[HEX_DIGEST_LEN+1];
747 const char *t = NULL;
748 digestmap_iter_get(orhist_it, &digest, &or_history_p);
749 hist = (or_history_t*) or_history_p;
751 base16_encode(dbuf, sizeof(dbuf), digest, DIGEST_LEN);
753 if (missing_means_down && hist->start_of_run &&
754 !router_get_by_digest(digest)) {
755 /* We think this relay is running, but it's not listed in our
756 * routerlist. Somehow it fell out without telling us it went
757 * down. Complain and also correct it. */
758 log_info(LD_HIST,
759 "Relay '%s' is listed as up in rephist, but it's not in "
760 "our routerlist. Correcting.", dbuf);
761 rep_hist_note_router_unreachable(digest, now);
764 PRINTF((f, "R %s\n", dbuf));
765 if (hist->start_of_run > 0) {
766 format_iso_time(time_buf, hist->start_of_run);
767 t = time_buf;
769 PRINTF((f, "+MTBF %lu %.5lf%s%s\n",
770 hist->weighted_run_length, hist->total_run_weights,
771 t ? " S=" : "", t ? t : ""));
772 t = NULL;
773 if (hist->start_of_downtime > 0) {
774 format_iso_time(time_buf, hist->start_of_downtime);
775 t = time_buf;
777 PRINTF((f, "+WFU %lu %lu%s%s\n",
778 hist->weighted_uptime, hist->total_weighted_time,
779 t ? " S=" : "", t ? t : ""));
782 PUT(".\n");
784 #undef PUT
785 #undef PRINTF
787 return finish_writing_to_file(open_file);
788 err:
789 abort_writing_to_file(open_file);
790 return -1;
793 /** Format the current tracked status of the router in <b>hist</b> at time
794 * <b>now</b> for analysis; return it in a newly allocated string. */
795 static char *
796 rep_hist_format_router_status(or_history_t *hist, time_t now)
798 char buf[1024];
799 char sor_buf[ISO_TIME_LEN+1];
800 char sod_buf[ISO_TIME_LEN+1];
801 double wfu;
802 double mtbf;
803 int up = 0, down = 0;
805 if (hist->start_of_run) {
806 format_iso_time(sor_buf, hist->start_of_run);
807 up = 1;
809 if (hist->start_of_downtime) {
810 format_iso_time(sod_buf, hist->start_of_downtime);
811 down = 1;
814 wfu = get_weighted_fractional_uptime(hist, now);
815 mtbf = get_stability(hist, now);
816 tor_snprintf(buf, sizeof(buf),
817 "%s%s%s"
818 "%s%s%s"
819 "wfu %0.3lf\n"
820 " weighted-time %lu\n"
821 " weighted-uptime %lu\n"
822 "mtbf %0.1lf\n"
823 " weighted-run-length %lu\n"
824 " total-run-weights %lf\n",
825 up?"uptime-started ":"", up?sor_buf:"", up?" UTC\n":"",
826 down?"downtime-started ":"", down?sod_buf:"", down?" UTC\n":"",
827 wfu,
828 hist->total_weighted_time,
829 hist->weighted_uptime,
830 mtbf,
831 hist->weighted_run_length,
832 hist->total_run_weights
835 return tor_strdup(buf);
838 /** The last stability analysis document that we created, or NULL if we never
839 * have created one. */
840 static char *last_stability_doc = NULL;
841 /** The last time we created a stability analysis document, or 0 if we never
842 * have created one. */
843 static time_t built_last_stability_doc_at = 0;
844 /** Shortest allowable time between building two stability documents. */
845 #define MAX_STABILITY_DOC_BUILD_RATE (3*60)
847 /** Return a pointer to a NUL-terminated document describing our view of the
848 * stability of the routers we've been tracking. Return NULL on failure. */
849 const char *
850 rep_hist_get_router_stability_doc(time_t now)
852 char *result;
853 smartlist_t *chunks;
854 if (built_last_stability_doc_at + MAX_STABILITY_DOC_BUILD_RATE > now)
855 return last_stability_doc;
857 if (!history_map)
858 return NULL;
860 tor_free(last_stability_doc);
861 chunks = smartlist_create();
863 if (rep_hist_have_measured_enough_stability()) {
864 smartlist_add(chunks, tor_strdup("we-have-enough-measurements\n"));
865 } else {
866 smartlist_add(chunks, tor_strdup("we-do-not-have-enough-measurements\n"));
869 DIGESTMAP_FOREACH(history_map, id, or_history_t *, hist) {
870 routerinfo_t *ri;
871 char dbuf[BASE64_DIGEST_LEN+1];
872 char header_buf[512];
873 char *info;
874 digest_to_base64(dbuf, id);
875 ri = router_get_by_digest(id);
876 if (ri) {
877 char *ip = tor_dup_ip(ri->addr);
878 char tbuf[ISO_TIME_LEN+1];
879 format_iso_time(tbuf, ri->cache_info.published_on);
880 tor_snprintf(header_buf, sizeof(header_buf),
881 "router %s %s %s\n"
882 "published %s\n"
883 "relevant-flags %s%s%s\n"
884 "declared-uptime %ld\n",
885 dbuf, ri->nickname, ip,
886 tbuf,
887 ri->is_running ? "Running " : "",
888 ri->is_valid ? "Valid " : "",
889 ri->is_hibernating ? "Hibernating " : "",
890 ri->uptime);
891 tor_free(ip);
892 } else {
893 tor_snprintf(header_buf, sizeof(header_buf),
894 "router %s {no descriptor}\n", dbuf);
896 smartlist_add(chunks, tor_strdup(header_buf));
897 info = rep_hist_format_router_status(hist, now);
898 if (info)
899 smartlist_add(chunks, info);
901 } DIGESTMAP_FOREACH_END;
903 result = smartlist_join_strings(chunks, "", 0, NULL);
904 SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
905 smartlist_free(chunks);
907 last_stability_doc = result;
908 built_last_stability_doc_at = time(NULL);
909 return result;
912 /** Helper: return the first j >= i such that !strcmpstart(sl[j], prefix) and
913 * such that no line sl[k] with i <= k < j starts with "R ". Return -1 if no
914 * such line exists. */
915 static int
916 find_next_with(smartlist_t *sl, int i, const char *prefix)
918 for ( ; i < smartlist_len(sl); ++i) {
919 const char *line = smartlist_get(sl, i);
920 if (!strcmpstart(line, prefix))
921 return i;
922 if (!strcmpstart(line, "R "))
923 return -1;
925 return -1;
928 /** How many bad times has parse_possibly_bad_iso_time parsed? */
929 static int n_bogus_times = 0;
930 /** Parse the ISO-formatted time in <b>s</b> into *<b>time_out</b>, but
931 * rounds any pre-1970 date to Jan 1, 1970. */
932 static int
933 parse_possibly_bad_iso_time(const char *s, time_t *time_out)
935 int year;
936 char b[5];
937 strlcpy(b, s, sizeof(b));
938 b[4] = '\0';
939 year = (int)tor_parse_long(b, 10, 0, INT_MAX, NULL, NULL);
940 if (year < 1970) {
941 *time_out = 0;
942 ++n_bogus_times;
943 return 0;
944 } else
945 return parse_iso_time(s, time_out);
948 /** We've read a time <b>t</b> from a file stored at <b>stored_at</b>, which
949 * says we started measuring at <b>started_measuring</b>. Return a new number
950 * that's about as much before <b>now</b> as <b>t</b> was before
951 * <b>stored_at</b>.
953 static INLINE time_t
954 correct_time(time_t t, time_t now, time_t stored_at, time_t started_measuring)
956 if (t < started_measuring - 24*60*60*365)
957 return 0;
958 else if (t < started_measuring)
959 return started_measuring;
960 else if (t > stored_at)
961 return 0;
962 else {
963 long run_length = stored_at - t;
964 t = now - run_length;
965 if (t < started_measuring)
966 t = started_measuring;
967 return t;
971 /** Load MTBF data from disk. Returns 0 on success or recoverable error, -1
972 * on failure. */
974 rep_hist_load_mtbf_data(time_t now)
976 /* XXXX won't handle being called while history is already populated. */
977 smartlist_t *lines;
978 const char *line = NULL;
979 int r=0, i;
980 time_t last_downrated = 0, stored_at = 0, tracked_since = 0;
981 time_t latest_possible_start = now;
982 long format = -1;
985 char *filename = get_datadir_fname("router-stability");
986 char *d = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
987 tor_free(filename);
988 if (!d)
989 return -1;
990 lines = smartlist_create();
991 smartlist_split_string(lines, d, "\n", SPLIT_SKIP_SPACE, 0);
992 tor_free(d);
996 const char *firstline;
997 if (smartlist_len(lines)>4) {
998 firstline = smartlist_get(lines, 0);
999 if (!strcmpstart(firstline, "format "))
1000 format = tor_parse_long(firstline+strlen("format "),
1001 10, -1, LONG_MAX, NULL, NULL);
1004 if (format != 1 && format != 2) {
1005 log_warn(LD_HIST,
1006 "Unrecognized format in mtbf history file. Skipping.");
1007 goto err;
1009 for (i = 1; i < smartlist_len(lines); ++i) {
1010 line = smartlist_get(lines, i);
1011 if (!strcmp(line, "data"))
1012 break;
1013 if (!strcmpstart(line, "last-downrated ")) {
1014 if (parse_iso_time(line+strlen("last-downrated "), &last_downrated)<0)
1015 log_warn(LD_HIST,"Couldn't parse downrate time in mtbf "
1016 "history file.");
1018 if (!strcmpstart(line, "stored-at ")) {
1019 if (parse_iso_time(line+strlen("stored-at "), &stored_at)<0)
1020 log_warn(LD_HIST,"Couldn't parse stored time in mtbf "
1021 "history file.");
1023 if (!strcmpstart(line, "tracked-since ")) {
1024 if (parse_iso_time(line+strlen("tracked-since "), &tracked_since)<0)
1025 log_warn(LD_HIST,"Couldn't parse started-tracking time in mtbf "
1026 "history file.");
1029 if (last_downrated > now)
1030 last_downrated = now;
1031 if (tracked_since > now)
1032 tracked_since = now;
1034 if (!stored_at) {
1035 log_warn(LD_HIST, "No stored time recorded.");
1036 goto err;
1039 if (line && !strcmp(line, "data"))
1040 ++i;
1042 n_bogus_times = 0;
1044 for (; i < smartlist_len(lines); ++i) {
1045 char digest[DIGEST_LEN];
1046 char hexbuf[HEX_DIGEST_LEN+1];
1047 char mtbf_timebuf[ISO_TIME_LEN+1];
1048 char wfu_timebuf[ISO_TIME_LEN+1];
1049 time_t start_of_run = 0;
1050 time_t start_of_downtime = 0;
1051 int have_mtbf = 0, have_wfu = 0;
1052 long wrl = 0;
1053 double trw = 0;
1054 long wt_uptime = 0, total_wt_time = 0;
1055 int n;
1056 or_history_t *hist;
1057 line = smartlist_get(lines, i);
1058 if (!strcmp(line, "."))
1059 break;
1061 mtbf_timebuf[0] = '\0';
1062 wfu_timebuf[0] = '\0';
1064 if (format == 1) {
1065 n = sscanf(line, "%40s %ld %lf S=%10s %8s",
1066 hexbuf, &wrl, &trw, mtbf_timebuf, mtbf_timebuf+11);
1067 if (n != 3 && n != 5) {
1068 log_warn(LD_HIST, "Couldn't scan line %s", escaped(line));
1069 continue;
1071 have_mtbf = 1;
1072 } else {
1073 // format == 2.
1074 int mtbf_idx, wfu_idx;
1075 if (strcmpstart(line, "R ") || strlen(line) < 2+HEX_DIGEST_LEN)
1076 continue;
1077 strlcpy(hexbuf, line+2, sizeof(hexbuf));
1078 mtbf_idx = find_next_with(lines, i+1, "+MTBF ");
1079 wfu_idx = find_next_with(lines, i+1, "+WFU ");
1080 if (mtbf_idx >= 0) {
1081 const char *mtbfline = smartlist_get(lines, mtbf_idx);
1082 n = sscanf(mtbfline, "+MTBF %lu %lf S=%10s %8s",
1083 &wrl, &trw, mtbf_timebuf, mtbf_timebuf+11);
1084 if (n == 2 || n == 4) {
1085 have_mtbf = 1;
1086 } else {
1087 log_warn(LD_HIST, "Couldn't scan +MTBF line %s",
1088 escaped(mtbfline));
1091 if (wfu_idx >= 0) {
1092 const char *wfuline = smartlist_get(lines, wfu_idx);
1093 n = sscanf(wfuline, "+WFU %lu %lu S=%10s %8s",
1094 &wt_uptime, &total_wt_time,
1095 wfu_timebuf, wfu_timebuf+11);
1096 if (n == 2 || n == 4) {
1097 have_wfu = 1;
1098 } else {
1099 log_warn(LD_HIST, "Couldn't scan +WFU line %s", escaped(wfuline));
1102 if (wfu_idx > i)
1103 i = wfu_idx;
1104 if (mtbf_idx > i)
1105 i = mtbf_idx;
1107 if (base16_decode(digest, DIGEST_LEN, hexbuf, HEX_DIGEST_LEN) < 0) {
1108 log_warn(LD_HIST, "Couldn't hex string %s", escaped(hexbuf));
1109 continue;
1111 hist = get_or_history(digest);
1112 if (!hist)
1113 continue;
1115 if (have_mtbf) {
1116 if (mtbf_timebuf[0]) {
1117 mtbf_timebuf[10] = ' ';
1118 if (parse_possibly_bad_iso_time(mtbf_timebuf, &start_of_run)<0)
1119 log_warn(LD_HIST, "Couldn't parse time %s",
1120 escaped(mtbf_timebuf));
1122 hist->start_of_run = correct_time(start_of_run, now, stored_at,
1123 tracked_since);
1124 if (hist->start_of_run < latest_possible_start + wrl)
1125 latest_possible_start = hist->start_of_run - wrl;
1127 hist->weighted_run_length = wrl;
1128 hist->total_run_weights = trw;
1130 if (have_wfu) {
1131 if (wfu_timebuf[0]) {
1132 wfu_timebuf[10] = ' ';
1133 if (parse_possibly_bad_iso_time(wfu_timebuf, &start_of_downtime)<0)
1134 log_warn(LD_HIST, "Couldn't parse time %s", escaped(wfu_timebuf));
1137 hist->start_of_downtime = correct_time(start_of_downtime, now, stored_at,
1138 tracked_since);
1139 hist->weighted_uptime = wt_uptime;
1140 hist->total_weighted_time = total_wt_time;
1142 if (strcmp(line, "."))
1143 log_warn(LD_HIST, "Truncated MTBF file.");
1145 if (tracked_since < 86400*365) /* Recover from insanely early value. */
1146 tracked_since = latest_possible_start;
1148 stability_last_downrated = last_downrated;
1149 started_tracking_stability = tracked_since;
1151 goto done;
1152 err:
1153 r = -1;
1154 done:
1155 SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
1156 smartlist_free(lines);
1157 return r;
1160 /** For how many seconds do we keep track of individual per-second bandwidth
1161 * totals? */
1162 #define NUM_SECS_ROLLING_MEASURE 10
1163 /** How large are the intervals for which we track and report bandwidth use? */
1164 #define NUM_SECS_BW_SUM_INTERVAL (15*60)
1165 /** How far in the past do we remember and publish bandwidth use? */
1166 #define NUM_SECS_BW_SUM_IS_VALID (24*60*60)
1167 /** How many bandwidth usage intervals do we remember? (derived) */
1168 #define NUM_TOTALS (NUM_SECS_BW_SUM_IS_VALID/NUM_SECS_BW_SUM_INTERVAL)
1170 /** Structure to track bandwidth use, and remember the maxima for a given
1171 * time period.
1173 typedef struct bw_array_t {
1174 /** Observation array: Total number of bytes transferred in each of the last
1175 * NUM_SECS_ROLLING_MEASURE seconds. This is used as a circular array. */
1176 uint64_t obs[NUM_SECS_ROLLING_MEASURE];
1177 int cur_obs_idx; /**< Current position in obs. */
1178 time_t cur_obs_time; /**< Time represented in obs[cur_obs_idx] */
1179 uint64_t total_obs; /**< Total for all members of obs except
1180 * obs[cur_obs_idx] */
1181 uint64_t max_total; /**< Largest value that total_obs has taken on in the
1182 * current period. */
1183 uint64_t total_in_period; /**< Total bytes transferred in the current
1184 * period. */
1186 /** When does the next period begin? */
1187 time_t next_period;
1188 /** Where in 'maxima' should the maximum bandwidth usage for the current
1189 * period be stored? */
1190 int next_max_idx;
1191 /** How many values in maxima/totals have been set ever? */
1192 int num_maxes_set;
1193 /** Circular array of the maximum
1194 * bandwidth-per-NUM_SECS_ROLLING_MEASURE usage for the last
1195 * NUM_TOTALS periods */
1196 uint64_t maxima[NUM_TOTALS];
1197 /** Circular array of the total bandwidth usage for the last NUM_TOTALS
1198 * periods */
1199 uint64_t totals[NUM_TOTALS];
1200 } bw_array_t;
1202 /** Shift the current period of b forward by one. */
1203 static void
1204 commit_max(bw_array_t *b)
1206 /* Store total from current period. */
1207 b->totals[b->next_max_idx] = b->total_in_period;
1208 /* Store maximum from current period. */
1209 b->maxima[b->next_max_idx++] = b->max_total;
1210 /* Advance next_period and next_max_idx */
1211 b->next_period += NUM_SECS_BW_SUM_INTERVAL;
1212 if (b->next_max_idx == NUM_TOTALS)
1213 b->next_max_idx = 0;
1214 if (b->num_maxes_set < NUM_TOTALS)
1215 ++b->num_maxes_set;
1216 /* Reset max_total. */
1217 b->max_total = 0;
1218 /* Reset total_in_period. */
1219 b->total_in_period = 0;
1222 /** Shift the current observation time of 'b' forward by one second. */
1223 static INLINE void
1224 advance_obs(bw_array_t *b)
1226 int nextidx;
1227 uint64_t total;
1229 /* Calculate the total bandwidth for the last NUM_SECS_ROLLING_MEASURE
1230 * seconds; adjust max_total as needed.*/
1231 total = b->total_obs + b->obs[b->cur_obs_idx];
1232 if (total > b->max_total)
1233 b->max_total = total;
1235 nextidx = b->cur_obs_idx+1;
1236 if (nextidx == NUM_SECS_ROLLING_MEASURE)
1237 nextidx = 0;
1239 b->total_obs = total - b->obs[nextidx];
1240 b->obs[nextidx]=0;
1241 b->cur_obs_idx = nextidx;
1243 if (++b->cur_obs_time >= b->next_period)
1244 commit_max(b);
1247 /** Add <b>n</b> bytes to the number of bytes in <b>b</b> for second
1248 * <b>when</b>. */
1249 static INLINE void
1250 add_obs(bw_array_t *b, time_t when, uint64_t n)
1252 /* Don't record data in the past. */
1253 if (when<b->cur_obs_time)
1254 return;
1255 /* If we're currently adding observations for an earlier second than
1256 * 'when', advance b->cur_obs_time and b->cur_obs_idx by an
1257 * appropriate number of seconds, and do all the other housekeeping */
1258 while (when>b->cur_obs_time)
1259 advance_obs(b);
1261 b->obs[b->cur_obs_idx] += n;
1262 b->total_in_period += n;
1265 /** Allocate, initialize, and return a new bw_array. */
1266 static bw_array_t *
1267 bw_array_new(void)
1269 bw_array_t *b;
1270 time_t start;
1271 b = tor_malloc_zero(sizeof(bw_array_t));
1272 rephist_total_alloc += sizeof(bw_array_t);
1273 start = time(NULL);
1274 b->cur_obs_time = start;
1275 b->next_period = start + NUM_SECS_BW_SUM_INTERVAL;
1276 return b;
1279 /** Recent history of bandwidth observations for read operations. */
1280 static bw_array_t *read_array = NULL;
1281 /** Recent history of bandwidth observations for write operations. */
1282 static bw_array_t *write_array = NULL;
1284 /** Set up read_array and write_array. */
1285 static void
1286 bw_arrays_init(void)
1288 read_array = bw_array_new();
1289 write_array = bw_array_new();
1292 /** We read <b>num_bytes</b> more bytes in second <b>when</b>.
1294 * Add num_bytes to the current running total for <b>when</b>.
1296 * <b>when</b> can go back to time, but it's safe to ignore calls
1297 * earlier than the latest <b>when</b> you've heard of.
1299 void
1300 rep_hist_note_bytes_written(size_t num_bytes, time_t when)
1302 /* Maybe a circular array for recent seconds, and step to a new point
1303 * every time a new second shows up. Or simpler is to just to have
1304 * a normal array and push down each item every second; it's short.
1306 /* When a new second has rolled over, compute the sum of the bytes we've
1307 * seen over when-1 to when-1-NUM_SECS_ROLLING_MEASURE, and stick it
1308 * somewhere. See rep_hist_bandwidth_assess() below.
1310 add_obs(write_array, when, num_bytes);
1313 /** We wrote <b>num_bytes</b> more bytes in second <b>when</b>.
1314 * (like rep_hist_note_bytes_written() above)
1316 void
1317 rep_hist_note_bytes_read(size_t num_bytes, time_t when)
1319 /* if we're smart, we can make this func and the one above share code */
1320 add_obs(read_array, when, num_bytes);
1323 /* Some constants */
1324 /** To what multiple should byte numbers be rounded up? */
1325 #define EXIT_STATS_ROUND_UP_BYTES 1024
1326 /** To what multiple should stream counts be rounded up? */
1327 #define EXIT_STATS_ROUND_UP_STREAMS 4
1328 /** Number of TCP ports */
1329 #define EXIT_STATS_NUM_PORTS 65536
1330 /** Reciprocal of threshold (= 0.01%) of total bytes that a port needs to
1331 * see in order to be included in exit stats. */
1332 #define EXIT_STATS_THRESHOLD_RECIPROCAL 10000
1334 /* The following data structures are arrays and no fancy smartlists or maps,
1335 * so that all write operations can be done in constant time. This comes at
1336 * the price of some memory (1.25 MB) and linear complexity when writing
1337 * stats for measuring relays. */
1338 /** Number of bytes read in current period by exit port */
1339 static uint64_t *exit_bytes_read = NULL;
1340 /** Number of bytes written in current period by exit port */
1341 static uint64_t *exit_bytes_written = NULL;
1342 /** Number of streams opened in current period by exit port */
1343 static uint32_t *exit_streams = NULL;
1345 /** When does the current exit stats period end? */
1346 static time_t start_of_exit_stats_interval;
1348 /** Initialize exit port stats. */
1349 void
1350 rep_hist_exit_stats_init(time_t now)
1352 start_of_exit_stats_interval = now;
1353 exit_bytes_read = tor_malloc_zero(EXIT_STATS_NUM_PORTS *
1354 sizeof(uint64_t));
1355 exit_bytes_written = tor_malloc_zero(EXIT_STATS_NUM_PORTS *
1356 sizeof(uint64_t));
1357 exit_streams = tor_malloc_zero(EXIT_STATS_NUM_PORTS *
1358 sizeof(uint32_t));
1361 /** Write exit stats to $DATADIR/stats/exit-stats and reset counters. */
1362 void
1363 rep_hist_exit_stats_write(time_t now)
1365 char t[ISO_TIME_LEN+1];
1366 int r, i, comma;
1367 uint64_t *b, total_bytes, threshold_bytes, other_bytes;
1368 uint32_t other_streams;
1370 char *statsdir = NULL, *filename = NULL;
1371 open_file_t *open_file = NULL;
1372 FILE *out = NULL;
1374 if (!exit_streams)
1375 return; /* Not initialized */
1377 statsdir = get_datadir_fname("stats");
1378 if (check_private_dir(statsdir, CPD_CREATE) < 0)
1379 goto done;
1380 filename = get_datadir_fname2("stats", "exit-stats");
1381 format_iso_time(t, now);
1382 log_info(LD_HIST, "Writing exit port statistics to disk for period "
1383 "ending at %s.", t);
1385 if (!open_file) {
1386 out = start_writing_to_stdio_file(filename, OPEN_FLAGS_APPEND,
1387 0600, &open_file);
1388 if (!out) {
1389 log_warn(LD_HIST, "Couldn't open '%s'.", filename);
1390 goto done;
1394 /* written yyyy-mm-dd HH:MM:SS (n s) */
1395 if (fprintf(out, "exit-stats-end %s (%d s)\n", t,
1396 (unsigned) (now - start_of_exit_stats_interval)) < 0)
1397 goto done;
1399 /* Count the total number of bytes, so that we can attribute all
1400 * observations below a threshold of 1 / EXIT_STATS_THRESHOLD_RECIPROCAL
1401 * of all bytes to a special port 'other'. */
1402 total_bytes = 0;
1403 for (i = 1; i < EXIT_STATS_NUM_PORTS; i++) {
1404 total_bytes += exit_bytes_read[i];
1405 total_bytes += exit_bytes_written[i];
1407 threshold_bytes = total_bytes / EXIT_STATS_THRESHOLD_RECIPROCAL;
1409 /* exit-kibibytes-(read|written) port=kibibytes,.. */
1410 for (r = 0; r < 2; r++) {
1411 b = r ? exit_bytes_read : exit_bytes_written;
1412 tor_assert(b);
1413 if (fprintf(out, "%s ",
1414 r ? "exit-kibibytes-read"
1415 : "exit-kibibytes-written") < 0)
1416 goto done;
1418 comma = 0;
1419 other_bytes = 0;
1420 for (i = 1; i < EXIT_STATS_NUM_PORTS; i++) {
1421 if (b[i] > 0) {
1422 if (exit_bytes_read[i] + exit_bytes_written[i] > threshold_bytes) {
1423 uint64_t num = round_uint64_to_next_multiple_of(b[i],
1424 EXIT_STATS_ROUND_UP_BYTES);
1425 num /= 1024;
1426 if (fprintf(out, "%s%d="U64_FORMAT,
1427 comma++ ? "," : "", i,
1428 U64_PRINTF_ARG(num)) < 0)
1429 goto done;
1430 } else
1431 other_bytes += b[i];
1434 other_bytes = round_uint64_to_next_multiple_of(other_bytes,
1435 EXIT_STATS_ROUND_UP_BYTES);
1436 other_bytes /= 1024;
1437 if (fprintf(out, "%sother="U64_FORMAT"\n",
1438 comma ? "," : "", U64_PRINTF_ARG(other_bytes))<0)
1439 goto done;
1441 /* exit-streams-opened port=num,.. */
1442 if (fprintf(out, "exit-streams-opened ") < 0)
1443 goto done;
1444 comma = 0;
1445 other_streams = 0;
1446 for (i = 1; i < EXIT_STATS_NUM_PORTS; i++) {
1447 if (exit_streams[i] > 0) {
1448 if (exit_bytes_read[i] + exit_bytes_written[i] > threshold_bytes) {
1449 uint32_t num = round_uint32_to_next_multiple_of(exit_streams[i],
1450 EXIT_STATS_ROUND_UP_STREAMS);
1451 if (fprintf(out, "%s%d=%u",
1452 comma++ ? "," : "", i, num)<0)
1453 goto done;
1454 } else
1455 other_streams += exit_streams[i];
1458 other_streams = round_uint32_to_next_multiple_of(other_streams,
1459 EXIT_STATS_ROUND_UP_STREAMS);
1460 if (fprintf(out, "%sother=%u\n",
1461 comma ? "," : "", other_streams)<0)
1462 goto done;
1463 /* Reset counters */
1464 memset(exit_bytes_read, 0, EXIT_STATS_NUM_PORTS * sizeof(uint64_t));
1465 memset(exit_bytes_written, 0, EXIT_STATS_NUM_PORTS * sizeof(uint64_t));
1466 memset(exit_streams, 0, EXIT_STATS_NUM_PORTS * sizeof(uint32_t));
1467 start_of_exit_stats_interval = now;
1469 if (open_file)
1470 finish_writing_to_file(open_file);
1471 open_file = NULL;
1472 done:
1473 if (open_file)
1474 abort_writing_to_file(open_file);
1475 tor_free(filename);
1476 tor_free(statsdir);
1479 /** Note that we wrote <b>num_bytes</b> to an exit connection to
1480 * <b>port</b>. */
1481 void
1482 rep_hist_note_exit_bytes_written(uint16_t port, size_t num_bytes)
1484 if (!get_options()->ExitPortStatistics)
1485 return;
1486 if (!exit_bytes_written)
1487 return; /* Not initialized */
1488 exit_bytes_written[port] += num_bytes;
1489 log_debug(LD_HIST, "Written %lu bytes to exit connection to port %d.",
1490 (unsigned long)num_bytes, port);
1493 /** Note that we read <b>num_bytes</b> from an exit connection to
1494 * <b>port</b>. */
1495 void
1496 rep_hist_note_exit_bytes_read(uint16_t port, size_t num_bytes)
1498 if (!get_options()->ExitPortStatistics)
1499 return;
1500 if (!exit_bytes_read)
1501 return; /* Not initialized */
1502 exit_bytes_read[port] += num_bytes;
1503 log_debug(LD_HIST, "Read %lu bytes from exit connection to port %d.",
1504 (unsigned long)num_bytes, port);
1507 /** Note that we opened an exit stream to <b>port</b>. */
1508 void
1509 rep_hist_note_exit_stream_opened(uint16_t port)
1511 if (!get_options()->ExitPortStatistics)
1512 return;
1513 if (!exit_streams)
1514 return; /* Not initialized */
1515 exit_streams[port]++;
1516 log_debug(LD_HIST, "Opened exit stream to port %d", port);
1519 /** Helper: Return the largest value in b->maxima. (This is equal to the
1520 * most bandwidth used in any NUM_SECS_ROLLING_MEASURE period for the last
1521 * NUM_SECS_BW_SUM_IS_VALID seconds.)
1523 static uint64_t
1524 find_largest_max(bw_array_t *b)
1526 int i;
1527 uint64_t max;
1528 max=0;
1529 for (i=0; i<NUM_TOTALS; ++i) {
1530 if (b->maxima[i]>max)
1531 max = b->maxima[i];
1533 return max;
1536 /** Find the largest sums in the past NUM_SECS_BW_SUM_IS_VALID (roughly)
1537 * seconds. Find one sum for reading and one for writing. They don't have
1538 * to be at the same time.
1540 * Return the smaller of these sums, divided by NUM_SECS_ROLLING_MEASURE.
1543 rep_hist_bandwidth_assess(void)
1545 uint64_t w,r;
1546 r = find_largest_max(read_array);
1547 w = find_largest_max(write_array);
1548 if (r>w)
1549 return (int)(U64_TO_DBL(w)/NUM_SECS_ROLLING_MEASURE);
1550 else
1551 return (int)(U64_TO_DBL(r)/NUM_SECS_ROLLING_MEASURE);
1554 /** Print the bandwidth history of b (either read_array or write_array)
1555 * into the buffer pointed to by buf. The format is simply comma
1556 * separated numbers, from oldest to newest.
1558 * It returns the number of bytes written.
1560 static size_t
1561 rep_hist_fill_bandwidth_history(char *buf, size_t len, bw_array_t *b)
1563 char *cp = buf;
1564 int i, n;
1565 or_options_t *options = get_options();
1566 uint64_t cutoff;
1568 if (b->num_maxes_set <= b->next_max_idx) {
1569 /* We haven't been through the circular array yet; time starts at i=0.*/
1570 i = 0;
1571 } else {
1572 /* We've been around the array at least once. The next i to be
1573 overwritten is the oldest. */
1574 i = b->next_max_idx;
1577 if (options->RelayBandwidthRate) {
1578 /* We don't want to report that we used more bandwidth than the max we're
1579 * willing to relay; otherwise everybody will know how much traffic
1580 * we used ourself. */
1581 cutoff = options->RelayBandwidthRate * NUM_SECS_BW_SUM_INTERVAL;
1582 } else {
1583 cutoff = UINT64_MAX;
1586 for (n=0; n<b->num_maxes_set; ++n,++i) {
1587 uint64_t total;
1588 if (i >= NUM_TOTALS)
1589 i -= NUM_TOTALS;
1590 tor_assert(i < NUM_TOTALS);
1591 /* Round the bandwidth used down to the nearest 1k. */
1592 total = b->totals[i] & ~0x3ff;
1593 if (total > cutoff)
1594 total = cutoff;
1596 if (n==(b->num_maxes_set-1))
1597 tor_snprintf(cp, len-(cp-buf), U64_FORMAT, U64_PRINTF_ARG(total));
1598 else
1599 tor_snprintf(cp, len-(cp-buf), U64_FORMAT",", U64_PRINTF_ARG(total));
1600 cp += strlen(cp);
1602 return cp-buf;
1605 /** Allocate and return lines for representing this server's bandwidth
1606 * history in its descriptor.
1608 char *
1609 rep_hist_get_bandwidth_lines(int for_extrainfo)
1611 char *buf, *cp;
1612 char t[ISO_TIME_LEN+1];
1613 int r;
1614 bw_array_t *b;
1615 size_t len;
1617 /* opt (read|write)-history yyyy-mm-dd HH:MM:SS (n s) n,n,n,n,n... */
1618 len = (60+20*NUM_TOTALS)*2;
1619 buf = tor_malloc_zero(len);
1620 cp = buf;
1621 for (r=0;r<2;++r) {
1622 b = r?read_array:write_array;
1623 tor_assert(b);
1624 format_iso_time(t, b->next_period-NUM_SECS_BW_SUM_INTERVAL);
1625 tor_snprintf(cp, len-(cp-buf), "%s%s %s (%d s) ",
1626 for_extrainfo ? "" : "opt ",
1627 r ? "read-history" : "write-history", t,
1628 NUM_SECS_BW_SUM_INTERVAL);
1629 cp += strlen(cp);
1630 cp += rep_hist_fill_bandwidth_history(cp, len-(cp-buf), b);
1631 strlcat(cp, "\n", len-(cp-buf));
1632 ++cp;
1634 return buf;
1637 /** Update <b>state</b> with the newest bandwidth history. */
1638 void
1639 rep_hist_update_state(or_state_t *state)
1641 int len, r;
1642 char *buf, *cp;
1643 smartlist_t **s_values;
1644 time_t *s_begins;
1645 int *s_interval;
1646 bw_array_t *b;
1648 len = 20*NUM_TOTALS+1;
1649 buf = tor_malloc_zero(len);
1651 for (r=0;r<2;++r) {
1652 b = r?read_array:write_array;
1653 s_begins = r?&state->BWHistoryReadEnds :&state->BWHistoryWriteEnds;
1654 s_interval= r?&state->BWHistoryReadInterval:&state->BWHistoryWriteInterval;
1655 s_values = r?&state->BWHistoryReadValues :&state->BWHistoryWriteValues;
1657 if (*s_values) {
1658 SMARTLIST_FOREACH(*s_values, char *, val, tor_free(val));
1659 smartlist_free(*s_values);
1661 if (! server_mode(get_options())) {
1662 /* Clients don't need to store bandwidth history persistently;
1663 * force these values to the defaults. */
1664 /* FFFF we should pull the default out of config.c's state table,
1665 * so we don't have two defaults. */
1666 if (*s_begins != 0 || *s_interval != 900) {
1667 time_t now = time(NULL);
1668 time_t save_at = get_options()->AvoidDiskWrites ? now+3600 : now+600;
1669 or_state_mark_dirty(state, save_at);
1671 *s_begins = 0;
1672 *s_interval = 900;
1673 *s_values = smartlist_create();
1674 continue;
1676 *s_begins = b->next_period;
1677 *s_interval = NUM_SECS_BW_SUM_INTERVAL;
1678 cp = buf;
1679 cp += rep_hist_fill_bandwidth_history(cp, len, b);
1680 tor_snprintf(cp, len-(cp-buf), cp == buf ? U64_FORMAT : ","U64_FORMAT,
1681 U64_PRINTF_ARG(b->total_in_period));
1682 *s_values = smartlist_create();
1683 if (server_mode(get_options()))
1684 smartlist_split_string(*s_values, buf, ",", SPLIT_SKIP_SPACE, 0);
1686 tor_free(buf);
1687 if (server_mode(get_options())) {
1688 or_state_mark_dirty(get_or_state(), time(NULL)+(2*3600));
1692 /** Set bandwidth history from our saved state. */
1694 rep_hist_load_state(or_state_t *state, char **err)
1696 time_t s_begins, start;
1697 time_t now = time(NULL);
1698 uint64_t v;
1699 int r,i,ok;
1700 int all_ok = 1;
1701 int s_interval;
1702 smartlist_t *s_values;
1703 bw_array_t *b;
1705 /* Assert they already have been malloced */
1706 tor_assert(read_array && write_array);
1708 for (r=0;r<2;++r) {
1709 b = r?read_array:write_array;
1710 s_begins = r?state->BWHistoryReadEnds:state->BWHistoryWriteEnds;
1711 s_interval = r?state->BWHistoryReadInterval:state->BWHistoryWriteInterval;
1712 s_values = r?state->BWHistoryReadValues:state->BWHistoryWriteValues;
1713 if (s_values && s_begins >= now - NUM_SECS_BW_SUM_INTERVAL*NUM_TOTALS) {
1714 start = s_begins - s_interval*(smartlist_len(s_values));
1715 if (start > now)
1716 continue;
1717 b->cur_obs_time = start;
1718 b->next_period = start + NUM_SECS_BW_SUM_INTERVAL;
1719 SMARTLIST_FOREACH(s_values, char *, cp, {
1720 v = tor_parse_uint64(cp, 10, 0, UINT64_MAX, &ok, NULL);
1721 if (!ok) {
1722 all_ok=0;
1723 log_notice(LD_HIST, "Could not parse '%s' into a number.'", cp);
1725 if (start < now) {
1726 add_obs(b, start, v);
1727 start += NUM_SECS_BW_SUM_INTERVAL;
1732 /* Clean up maxima and observed */
1733 /* Do we really want to zero this for the purpose of max capacity? */
1734 for (i=0; i<NUM_SECS_ROLLING_MEASURE; ++i) {
1735 b->obs[i] = 0;
1737 b->total_obs = 0;
1738 for (i=0; i<NUM_TOTALS; ++i) {
1739 b->maxima[i] = 0;
1741 b->max_total = 0;
1744 if (!all_ok) {
1745 *err = tor_strdup("Parsing of bandwidth history values failed");
1746 /* and create fresh arrays */
1747 tor_free(read_array);
1748 tor_free(write_array);
1749 read_array = bw_array_new();
1750 write_array = bw_array_new();
1751 return -1;
1753 return 0;
1756 /*********************************************************************/
1758 /** A list of port numbers that have been used recently. */
1759 static smartlist_t *predicted_ports_list=NULL;
1760 /** The corresponding most recently used time for each port. */
1761 static smartlist_t *predicted_ports_times=NULL;
1763 /** We just got an application request for a connection with
1764 * port <b>port</b>. Remember it for the future, so we can keep
1765 * some circuits open that will exit to this port.
1767 static void
1768 add_predicted_port(time_t now, uint16_t port)
1770 /* XXXX we could just use uintptr_t here, I think. */
1771 uint16_t *tmp_port = tor_malloc(sizeof(uint16_t));
1772 time_t *tmp_time = tor_malloc(sizeof(time_t));
1773 *tmp_port = port;
1774 *tmp_time = now;
1775 rephist_total_alloc += sizeof(uint16_t) + sizeof(time_t);
1776 smartlist_add(predicted_ports_list, tmp_port);
1777 smartlist_add(predicted_ports_times, tmp_time);
1780 /** Initialize whatever memory and structs are needed for predicting
1781 * which ports will be used. Also seed it with port 80, so we'll build
1782 * circuits on start-up.
1784 static void
1785 predicted_ports_init(void)
1787 predicted_ports_list = smartlist_create();
1788 predicted_ports_times = smartlist_create();
1789 add_predicted_port(time(NULL), 80); /* add one to kickstart us */
1792 /** Free whatever memory is needed for predicting which ports will
1793 * be used.
1795 static void
1796 predicted_ports_free(void)
1798 rephist_total_alloc -= smartlist_len(predicted_ports_list)*sizeof(uint16_t);
1799 SMARTLIST_FOREACH(predicted_ports_list, char *, cp, tor_free(cp));
1800 smartlist_free(predicted_ports_list);
1801 rephist_total_alloc -= smartlist_len(predicted_ports_times)*sizeof(time_t);
1802 SMARTLIST_FOREACH(predicted_ports_times, char *, cp, tor_free(cp));
1803 smartlist_free(predicted_ports_times);
1806 /** Remember that <b>port</b> has been asked for as of time <b>now</b>.
1807 * This is used for predicting what sorts of streams we'll make in the
1808 * future and making exit circuits to anticipate that.
1810 void
1811 rep_hist_note_used_port(time_t now, uint16_t port)
1813 int i;
1814 uint16_t *tmp_port;
1815 time_t *tmp_time;
1817 tor_assert(predicted_ports_list);
1818 tor_assert(predicted_ports_times);
1820 if (!port) /* record nothing */
1821 return;
1823 for (i = 0; i < smartlist_len(predicted_ports_list); ++i) {
1824 tmp_port = smartlist_get(predicted_ports_list, i);
1825 tmp_time = smartlist_get(predicted_ports_times, i);
1826 if (*tmp_port == port) {
1827 *tmp_time = now;
1828 return;
1831 /* it's not there yet; we need to add it */
1832 add_predicted_port(now, port);
1835 /** For this long after we've seen a request for a given port, assume that
1836 * we'll want to make connections to the same port in the future. */
1837 #define PREDICTED_CIRCS_RELEVANCE_TIME (60*60)
1839 /** Return a pointer to the list of port numbers that
1840 * are likely to be asked for in the near future.
1842 * The caller promises not to mess with it.
1844 smartlist_t *
1845 rep_hist_get_predicted_ports(time_t now)
1847 int i;
1848 uint16_t *tmp_port;
1849 time_t *tmp_time;
1851 tor_assert(predicted_ports_list);
1852 tor_assert(predicted_ports_times);
1854 /* clean out obsolete entries */
1855 for (i = 0; i < smartlist_len(predicted_ports_list); ++i) {
1856 tmp_time = smartlist_get(predicted_ports_times, i);
1857 if (*tmp_time + PREDICTED_CIRCS_RELEVANCE_TIME < now) {
1858 tmp_port = smartlist_get(predicted_ports_list, i);
1859 log_debug(LD_CIRC, "Expiring predicted port %d", *tmp_port);
1860 smartlist_del(predicted_ports_list, i);
1861 smartlist_del(predicted_ports_times, i);
1862 rephist_total_alloc -= sizeof(uint16_t)+sizeof(time_t);
1863 tor_free(tmp_port);
1864 tor_free(tmp_time);
1865 i--;
1868 return predicted_ports_list;
1871 /** The user asked us to do a resolve. Rather than keeping track of
1872 * timings and such of resolves, we fake it for now by treating
1873 * it the same way as a connection to port 80. This way we will continue
1874 * to have circuits lying around if the user only uses Tor for resolves.
1876 void
1877 rep_hist_note_used_resolve(time_t now)
1879 rep_hist_note_used_port(now, 80);
1882 /** The last time at which we needed an internal circ. */
1883 static time_t predicted_internal_time = 0;
1884 /** The last time we needed an internal circ with good uptime. */
1885 static time_t predicted_internal_uptime_time = 0;
1886 /** The last time we needed an internal circ with good capacity. */
1887 static time_t predicted_internal_capacity_time = 0;
1889 /** Remember that we used an internal circ at time <b>now</b>. */
1890 void
1891 rep_hist_note_used_internal(time_t now, int need_uptime, int need_capacity)
1893 predicted_internal_time = now;
1894 if (need_uptime)
1895 predicted_internal_uptime_time = now;
1896 if (need_capacity)
1897 predicted_internal_capacity_time = now;
1900 /** Return 1 if we've used an internal circ recently; else return 0. */
1902 rep_hist_get_predicted_internal(time_t now, int *need_uptime,
1903 int *need_capacity)
1905 if (!predicted_internal_time) { /* initialize it */
1906 predicted_internal_time = now;
1907 predicted_internal_uptime_time = now;
1908 predicted_internal_capacity_time = now;
1910 if (predicted_internal_time + PREDICTED_CIRCS_RELEVANCE_TIME < now)
1911 return 0; /* too long ago */
1912 if (predicted_internal_uptime_time + PREDICTED_CIRCS_RELEVANCE_TIME >= now)
1913 *need_uptime = 1;
1914 if (predicted_internal_capacity_time + PREDICTED_CIRCS_RELEVANCE_TIME >= now)
1915 *need_capacity = 1;
1916 return 1;
1919 /** Any ports used lately? These are pre-seeded if we just started
1920 * up or if we're running a hidden service. */
1922 any_predicted_circuits(time_t now)
1924 return smartlist_len(predicted_ports_list) ||
1925 predicted_internal_time + PREDICTED_CIRCS_RELEVANCE_TIME >= now;
1928 /** Return 1 if we have no need for circuits currently, else return 0. */
1930 rep_hist_circbuilding_dormant(time_t now)
1932 if (any_predicted_circuits(now))
1933 return 0;
1935 /* see if we'll still need to build testing circuits */
1936 if (server_mode(get_options()) &&
1937 (!check_whether_orport_reachable() || !circuit_enough_testing_circs()))
1938 return 0;
1939 if (!check_whether_dirport_reachable())
1940 return 0;
1942 return 1;
1945 /** Structure to track how many times we've done each public key operation. */
1946 static struct {
1947 /** How many directory objects have we signed? */
1948 unsigned long n_signed_dir_objs;
1949 /** How many routerdescs have we signed? */
1950 unsigned long n_signed_routerdescs;
1951 /** How many directory objects have we verified? */
1952 unsigned long n_verified_dir_objs;
1953 /** How many routerdescs have we verified */
1954 unsigned long n_verified_routerdescs;
1955 /** How many onionskins have we encrypted to build circuits? */
1956 unsigned long n_onionskins_encrypted;
1957 /** How many onionskins have we decrypted to do circuit build requests? */
1958 unsigned long n_onionskins_decrypted;
1959 /** How many times have we done the TLS handshake as a client? */
1960 unsigned long n_tls_client_handshakes;
1961 /** How many times have we done the TLS handshake as a server? */
1962 unsigned long n_tls_server_handshakes;
1963 /** How many PK operations have we done as a hidden service client? */
1964 unsigned long n_rend_client_ops;
1965 /** How many PK operations have we done as a hidden service midpoint? */
1966 unsigned long n_rend_mid_ops;
1967 /** How many PK operations have we done as a hidden service provider? */
1968 unsigned long n_rend_server_ops;
1969 } pk_op_counts = {0,0,0,0,0,0,0,0,0,0,0};
1971 /** Increment the count of the number of times we've done <b>operation</b>. */
1972 void
1973 note_crypto_pk_op(pk_op_t operation)
1975 switch (operation)
1977 case SIGN_DIR:
1978 pk_op_counts.n_signed_dir_objs++;
1979 break;
1980 case SIGN_RTR:
1981 pk_op_counts.n_signed_routerdescs++;
1982 break;
1983 case VERIFY_DIR:
1984 pk_op_counts.n_verified_dir_objs++;
1985 break;
1986 case VERIFY_RTR:
1987 pk_op_counts.n_verified_routerdescs++;
1988 break;
1989 case ENC_ONIONSKIN:
1990 pk_op_counts.n_onionskins_encrypted++;
1991 break;
1992 case DEC_ONIONSKIN:
1993 pk_op_counts.n_onionskins_decrypted++;
1994 break;
1995 case TLS_HANDSHAKE_C:
1996 pk_op_counts.n_tls_client_handshakes++;
1997 break;
1998 case TLS_HANDSHAKE_S:
1999 pk_op_counts.n_tls_server_handshakes++;
2000 break;
2001 case REND_CLIENT:
2002 pk_op_counts.n_rend_client_ops++;
2003 break;
2004 case REND_MID:
2005 pk_op_counts.n_rend_mid_ops++;
2006 break;
2007 case REND_SERVER:
2008 pk_op_counts.n_rend_server_ops++;
2009 break;
2010 default:
2011 log_warn(LD_BUG, "Unknown pk operation %d", operation);
2015 /** Log the number of times we've done each public/private-key operation. */
2016 void
2017 dump_pk_ops(int severity)
2019 log(severity, LD_HIST,
2020 "PK operations: %lu directory objects signed, "
2021 "%lu directory objects verified, "
2022 "%lu routerdescs signed, "
2023 "%lu routerdescs verified, "
2024 "%lu onionskins encrypted, "
2025 "%lu onionskins decrypted, "
2026 "%lu client-side TLS handshakes, "
2027 "%lu server-side TLS handshakes, "
2028 "%lu rendezvous client operations, "
2029 "%lu rendezvous middle operations, "
2030 "%lu rendezvous server operations.",
2031 pk_op_counts.n_signed_dir_objs,
2032 pk_op_counts.n_verified_dir_objs,
2033 pk_op_counts.n_signed_routerdescs,
2034 pk_op_counts.n_verified_routerdescs,
2035 pk_op_counts.n_onionskins_encrypted,
2036 pk_op_counts.n_onionskins_decrypted,
2037 pk_op_counts.n_tls_client_handshakes,
2038 pk_op_counts.n_tls_server_handshakes,
2039 pk_op_counts.n_rend_client_ops,
2040 pk_op_counts.n_rend_mid_ops,
2041 pk_op_counts.n_rend_server_ops);
2044 /** Free all storage held by the OR/link history caches, by the
2045 * bandwidth history arrays, or by the port history. */
2046 void
2047 rep_hist_free_all(void)
2049 digestmap_free(history_map, free_or_history);
2050 tor_free(read_array);
2051 tor_free(write_array);
2052 tor_free(last_stability_doc);
2053 tor_free(exit_bytes_read);
2054 tor_free(exit_bytes_written);
2055 tor_free(exit_streams);
2056 built_last_stability_doc_at = 0;
2057 predicted_ports_free();
2060 /****************** hidden service usage statistics ******************/
2062 /** How large are the intervals for which we track and report hidden service
2063 * use? */
2064 #define NUM_SECS_HS_USAGE_SUM_INTERVAL (15*60)
2065 /** How far in the past do we remember and publish hidden service use? */
2066 #define NUM_SECS_HS_USAGE_SUM_IS_VALID (24*60*60)
2067 /** How many hidden service usage intervals do we remember? (derived) */
2068 #define NUM_TOTALS_HS_USAGE (NUM_SECS_HS_USAGE_SUM_IS_VALID/ \
2069 NUM_SECS_HS_USAGE_SUM_INTERVAL)
2071 /** List element containing a service id and the count. */
2072 typedef struct hs_usage_list_elem_t {
2073 /** Service id of this elem. */
2074 char service_id[REND_SERVICE_ID_LEN_BASE32+1];
2075 /** Number of occurrences for the given service id. */
2076 uint32_t count;
2077 /* Pointer to next list elem */
2078 struct hs_usage_list_elem_t *next;
2079 } hs_usage_list_elem_t;
2081 /** Ordered list that stores service ids and the number of observations. It is
2082 * ordered by the number of occurrences in descending order. Its purpose is to
2083 * calculate the frequency distribution when the period is over. */
2084 typedef struct hs_usage_list_t {
2085 /* Pointer to the first element in the list. */
2086 hs_usage_list_elem_t *start;
2087 /* Number of total occurrences for all list elements. */
2088 uint32_t total_count;
2089 /* Number of service ids, i.e. number of list elements. */
2090 uint32_t total_service_ids;
2091 } hs_usage_list_t;
2093 /** Tracks service-related observations in the current period and their
2094 * history. */
2095 typedef struct hs_usage_service_related_observation_t {
2096 /** Ordered list that stores service ids and the number of observations in
2097 * the current period. It is ordered by the number of occurrences in
2098 * descending order. Its purpose is to calculate the frequency distribution
2099 * when the period is over. */
2100 hs_usage_list_t *list;
2101 /** Circular arrays that store the history of observations. totals stores all
2102 * observations, twenty (ten, five) the number of observations related to a
2103 * service id being accounted for the top 20 (10, 5) percent of all
2104 * observations. */
2105 uint32_t totals[NUM_TOTALS_HS_USAGE];
2106 uint32_t five[NUM_TOTALS_HS_USAGE];
2107 uint32_t ten[NUM_TOTALS_HS_USAGE];
2108 uint32_t twenty[NUM_TOTALS_HS_USAGE];
2109 } hs_usage_service_related_observation_t;
2111 /** Tracks the history of general period-related observations, i.e. those that
2112 * cannot be related to a specific service id. */
2113 typedef struct hs_usage_general_period_related_observations_t {
2114 /** Circular array that stores the history of observations. */
2115 uint32_t totals[NUM_TOTALS_HS_USAGE];
2116 } hs_usage_general_period_related_observations_t;
2118 /** Keeps information about the current observation period and its relation to
2119 * the histories of observations. */
2120 typedef struct hs_usage_current_observation_period_t {
2121 /** Where do we write the next history entry? */
2122 int next_idx;
2123 /** How many values in history have been set ever? (upper bound!) */
2124 int num_set;
2125 /** When did this period begin? */
2126 time_t start_of_current_period;
2127 /** When does the next period begin? */
2128 time_t start_of_next_period;
2129 } hs_usage_current_observation_period_t;
2131 /** Usage statistics for the current observation period. */
2132 static hs_usage_current_observation_period_t *current_period = NULL;
2134 /** Total number of descriptor publish requests in the current observation
2135 * period. */
2136 static hs_usage_service_related_observation_t *publish_total = NULL;
2138 /** Number of descriptor publish requests for services that have not been
2139 * seen before in the current observation period. */
2140 static hs_usage_service_related_observation_t *publish_novel = NULL;
2142 /** Total number of descriptor fetch requests in the current observation
2143 * period. */
2144 static hs_usage_service_related_observation_t *fetch_total = NULL;
2146 /** Number of successful descriptor fetch requests in the current
2147 * observation period. */
2148 static hs_usage_service_related_observation_t *fetch_successful = NULL;
2150 /** Number of descriptors stored in the current observation period. */
2151 static hs_usage_general_period_related_observations_t *descs = NULL;
2153 /** Creates an empty ordered list element. */
2154 static hs_usage_list_elem_t *
2155 hs_usage_list_elem_new(void)
2157 hs_usage_list_elem_t *e;
2158 e = tor_malloc_zero(sizeof(hs_usage_list_elem_t));
2159 rephist_total_alloc += sizeof(hs_usage_list_elem_t);
2160 e->count = 1;
2161 e->next = NULL;
2162 return e;
2165 /** Creates an empty ordered list. */
2166 static hs_usage_list_t *
2167 hs_usage_list_new(void)
2169 hs_usage_list_t *l;
2170 l = tor_malloc_zero(sizeof(hs_usage_list_t));
2171 rephist_total_alloc += sizeof(hs_usage_list_t);
2172 l->start = NULL;
2173 l->total_count = 0;
2174 l->total_service_ids = 0;
2175 return l;
2178 /** Creates an empty structure for storing service-related observations. */
2179 static hs_usage_service_related_observation_t *
2180 hs_usage_service_related_observation_new(void)
2182 hs_usage_service_related_observation_t *h;
2183 h = tor_malloc_zero(sizeof(hs_usage_service_related_observation_t));
2184 rephist_total_alloc += sizeof(hs_usage_service_related_observation_t);
2185 h->list = hs_usage_list_new();
2186 return h;
2189 /** Creates an empty structure for storing general period-related
2190 * observations. */
2191 static hs_usage_general_period_related_observations_t *
2192 hs_usage_general_period_related_observations_new(void)
2194 hs_usage_general_period_related_observations_t *p;
2195 p = tor_malloc_zero(sizeof(hs_usage_general_period_related_observations_t));
2196 rephist_total_alloc+= sizeof(hs_usage_general_period_related_observations_t);
2197 return p;
2200 /** Creates an empty structure for storing period-specific information. */
2201 static hs_usage_current_observation_period_t *
2202 hs_usage_current_observation_period_new(void)
2204 hs_usage_current_observation_period_t *c;
2205 time_t now;
2206 c = tor_malloc_zero(sizeof(hs_usage_current_observation_period_t));
2207 rephist_total_alloc += sizeof(hs_usage_current_observation_period_t);
2208 now = time(NULL);
2209 c->start_of_current_period = now;
2210 c->start_of_next_period = now + NUM_SECS_HS_USAGE_SUM_INTERVAL;
2211 return c;
2214 /** Initializes the structures for collecting hidden service usage data. */
2215 static void
2216 hs_usage_init(void)
2218 current_period = hs_usage_current_observation_period_new();
2219 publish_total = hs_usage_service_related_observation_new();
2220 publish_novel = hs_usage_service_related_observation_new();
2221 fetch_total = hs_usage_service_related_observation_new();
2222 fetch_successful = hs_usage_service_related_observation_new();
2223 descs = hs_usage_general_period_related_observations_new();
2226 /** Clears the given ordered list by resetting its attributes and releasing
2227 * the memory allocated by its elements. */
2228 static void
2229 hs_usage_list_clear(hs_usage_list_t *lst)
2231 /* walk through elements and free memory */
2232 hs_usage_list_elem_t *current = lst->start;
2233 hs_usage_list_elem_t *tmp;
2234 while (current != NULL) {
2235 tmp = current->next;
2236 rephist_total_alloc -= sizeof(hs_usage_list_elem_t);
2237 tor_free(current);
2238 current = tmp;
2240 /* reset attributes */
2241 lst->start = NULL;
2242 lst->total_count = 0;
2243 lst->total_service_ids = 0;
2244 return;
2247 /** Frees the memory used by the given list. */
2248 static void
2249 hs_usage_list_free(hs_usage_list_t *lst)
2251 if (!lst)
2252 return;
2253 hs_usage_list_clear(lst);
2254 rephist_total_alloc -= sizeof(hs_usage_list_t);
2255 tor_free(lst);
2258 /** Frees the memory used by the given service-related observations. */
2259 static void
2260 hs_usage_service_related_observation_free(
2261 hs_usage_service_related_observation_t *s)
2263 if (!s)
2264 return;
2265 hs_usage_list_free(s->list);
2266 rephist_total_alloc -= sizeof(hs_usage_service_related_observation_t);
2267 tor_free(s);
2270 /** Frees the memory used by the given period-specific observations. */
2271 static void
2272 hs_usage_general_period_related_observations_free(
2273 hs_usage_general_period_related_observations_t *s)
2275 if (!s)
2276 return;
2277 rephist_total_alloc-=sizeof(hs_usage_general_period_related_observations_t);
2278 tor_free(s);
2281 /** Frees the memory used by period-specific information. */
2282 static void
2283 hs_usage_current_observation_period_free(
2284 hs_usage_current_observation_period_t *s)
2286 if (!s)
2287 return;
2288 rephist_total_alloc -= sizeof(hs_usage_current_observation_period_t);
2289 tor_free(s);
2292 /** Frees all memory that was used for collecting hidden service usage data. */
2293 void
2294 hs_usage_free_all(void)
2296 hs_usage_general_period_related_observations_free(descs);
2297 descs = NULL;
2298 hs_usage_service_related_observation_free(fetch_successful);
2299 hs_usage_service_related_observation_free(fetch_total);
2300 hs_usage_service_related_observation_free(publish_novel);
2301 hs_usage_service_related_observation_free(publish_total);
2302 fetch_successful = fetch_total = publish_novel = publish_total = NULL;
2303 hs_usage_current_observation_period_free(current_period);
2304 current_period = NULL;
2307 /** Inserts a new occurrence for the given service id to the given ordered
2308 * list. */
2309 static void
2310 hs_usage_insert_value(hs_usage_list_t *lst, const char *service_id)
2312 /* search if there is already an elem with same service_id in list */
2313 hs_usage_list_elem_t *current = lst->start;
2314 hs_usage_list_elem_t *previous = NULL;
2315 while (current != NULL && strcasecmp(current->service_id,service_id)) {
2316 previous = current;
2317 current = current->next;
2319 /* found an element with same service_id? */
2320 if (current == NULL) {
2321 /* not found! append to end (which could also be the end of a zero-length
2322 * list), don't need to sort (1 is smallest value). */
2323 /* create elem */
2324 hs_usage_list_elem_t *e = hs_usage_list_elem_new();
2325 /* update list attributes (one new elem, one new occurrence) */
2326 lst->total_count++;
2327 lst->total_service_ids++;
2328 /* copy service id to elem */
2329 strlcpy(e->service_id,service_id,sizeof(e->service_id));
2330 /* let either l->start or previously last elem point to new elem */
2331 if (lst->start == NULL) {
2332 /* this is the first elem */
2333 lst->start = e;
2334 } else {
2335 /* there were elems in the list before */
2336 previous->next = e;
2338 } else {
2339 /* found! add occurrence to elem and consider resorting */
2340 /* update list attributes (no new elem, but one new occurrence) */
2341 lst->total_count++;
2342 /* add occurrence to elem */
2343 current->count++;
2344 /* is it another than the first list elem? and has previous elem fewer
2345 * count than current? then we need to resort */
2346 if (previous != NULL && previous->count < current->count) {
2347 /* yes! we need to resort */
2348 /* remove current elem first */
2349 previous->next = current->next;
2350 /* can we prepend elem to all other elements? */
2351 if (lst->start->count <= current->count) {
2352 /* yes! prepend elem */
2353 current->next = lst->start;
2354 lst->start = current;
2355 } else {
2356 /* no! walk through list a second time and insert at correct place */
2357 hs_usage_list_elem_t *insert_current = lst->start->next;
2358 hs_usage_list_elem_t *insert_previous = lst->start;
2359 while (insert_current != NULL &&
2360 insert_current->count > current->count) {
2361 insert_previous = insert_current;
2362 insert_current = insert_current->next;
2364 /* insert here */
2365 current->next = insert_current;
2366 insert_previous->next = current;
2372 /** Writes the current service-related observations to the history array and
2373 * clears the observations of the current period. */
2374 static void
2375 hs_usage_write_service_related_observations_to_history(
2376 hs_usage_current_observation_period_t *p,
2377 hs_usage_service_related_observation_t *h)
2379 /* walk through the first 20 % of list elements and calculate frequency
2380 * distributions */
2381 /* maximum indices for the three frequencies */
2382 int five_percent_idx = h->list->total_service_ids/20;
2383 int ten_percent_idx = h->list->total_service_ids/10;
2384 int twenty_percent_idx = h->list->total_service_ids/5;
2385 /* temp values */
2386 uint32_t five_percent = 0;
2387 uint32_t ten_percent = 0;
2388 uint32_t twenty_percent = 0;
2389 /* walk through list */
2390 hs_usage_list_elem_t *current = h->list->start;
2391 int i=0;
2392 while (current != NULL && i <= twenty_percent_idx) {
2393 twenty_percent += current->count;
2394 if (i <= ten_percent_idx)
2395 ten_percent += current->count;
2396 if (i <= five_percent_idx)
2397 five_percent += current->count;
2398 current = current->next;
2399 i++;
2401 /* copy frequencies */
2402 h->twenty[p->next_idx] = twenty_percent;
2403 h->ten[p->next_idx] = ten_percent;
2404 h->five[p->next_idx] = five_percent;
2405 /* copy total number of observations */
2406 h->totals[p->next_idx] = h->list->total_count;
2407 /* free memory of old list */
2408 hs_usage_list_clear(h->list);
2411 /** Advances to next observation period. */
2412 static void
2413 hs_usage_advance_current_observation_period(void)
2415 /* aggregate observations to history, including frequency distribution
2416 * arrays */
2417 hs_usage_write_service_related_observations_to_history(
2418 current_period, publish_total);
2419 hs_usage_write_service_related_observations_to_history(
2420 current_period, publish_novel);
2421 hs_usage_write_service_related_observations_to_history(
2422 current_period, fetch_total);
2423 hs_usage_write_service_related_observations_to_history(
2424 current_period, fetch_successful);
2425 /* write current number of descriptors to descs history */
2426 descs->totals[current_period->next_idx] = rend_cache_size();
2427 /* advance to next period */
2428 current_period->next_idx++;
2429 if (current_period->next_idx == NUM_TOTALS_HS_USAGE)
2430 current_period->next_idx = 0;
2431 if (current_period->num_set < NUM_TOTALS_HS_USAGE)
2432 ++current_period->num_set;
2433 current_period->start_of_current_period=current_period->start_of_next_period;
2434 current_period->start_of_next_period += NUM_SECS_HS_USAGE_SUM_INTERVAL;
2437 /** Checks if the current period is up to date, and if not, advances it. */
2438 static void
2439 hs_usage_check_if_current_period_is_up_to_date(time_t now)
2441 while (now > current_period->start_of_next_period) {
2442 hs_usage_advance_current_observation_period();
2446 /** Adds a service-related observation, maybe after advancing to next
2447 * observation period. */
2448 static void
2449 hs_usage_add_service_related_observation(
2450 hs_usage_service_related_observation_t *h,
2451 time_t now,
2452 const char *service_id)
2454 if (now < current_period->start_of_current_period) {
2455 /* don't record old data */
2456 return;
2458 /* check if we are up-to-date */
2459 hs_usage_check_if_current_period_is_up_to_date(now);
2460 /* add observation */
2461 hs_usage_insert_value(h->list, service_id);
2464 /** Adds the observation of storing a rendezvous service descriptor to our
2465 * cache in our role as HS authoritative directory. */
2466 void
2467 hs_usage_note_publish_total(const char *service_id, time_t now)
2469 hs_usage_add_service_related_observation(publish_total, now, service_id);
2472 /** Adds the observation of storing a novel rendezvous service descriptor to
2473 * our cache in our role as HS authoritative directory. */
2474 void
2475 hs_usage_note_publish_novel(const char *service_id, time_t now)
2477 hs_usage_add_service_related_observation(publish_novel, now, service_id);
2480 /** Adds the observation of being requested for a rendezvous service descriptor
2481 * in our role as HS authoritative directory. */
2482 void
2483 hs_usage_note_fetch_total(const char *service_id, time_t now)
2485 hs_usage_add_service_related_observation(fetch_total, now, service_id);
2488 /** Adds the observation of being requested for a rendezvous service descriptor
2489 * in our role as HS authoritative directory and being able to answer that
2490 * request successfully. */
2491 void
2492 hs_usage_note_fetch_successful(const char *service_id, time_t now)
2494 hs_usage_add_service_related_observation(fetch_successful, now, service_id);
2497 /** Writes the given circular array to a string. */
2498 static size_t
2499 hs_usage_format_history(char *buf, size_t len, uint32_t *data)
2501 char *cp = buf; /* pointer where we are in the buffer */
2502 int i, n;
2503 if (current_period->num_set <= current_period->next_idx) {
2504 i = 0; /* not been through circular array */
2505 } else {
2506 i = current_period->next_idx;
2508 for (n = 0; n < current_period->num_set; ++n,++i) {
2509 if (i >= NUM_TOTALS_HS_USAGE)
2510 i -= NUM_TOTALS_HS_USAGE;
2511 tor_assert(i < NUM_TOTALS_HS_USAGE);
2512 if (n == (current_period->num_set-1))
2513 tor_snprintf(cp, len-(cp-buf), "%d", data[i]);
2514 else
2515 tor_snprintf(cp, len-(cp-buf), "%d,", data[i]);
2516 cp += strlen(cp);
2518 return cp-buf;
2521 /** Writes the complete usage history as hidden service authoritative directory
2522 * to a string. */
2523 static char *
2524 hs_usage_format_statistics(void)
2526 char *buf, *cp, *s = NULL;
2527 char t[ISO_TIME_LEN+1];
2528 int r;
2529 uint32_t *data = NULL;
2530 size_t len;
2531 len = (70+20*NUM_TOTALS_HS_USAGE)*11;
2532 buf = tor_malloc_zero(len);
2533 cp = buf;
2534 for (r = 0; r < 11; ++r) {
2535 switch (r) {
2536 case 0:
2537 s = (char*) "publish-total-history";
2538 data = publish_total->totals;
2539 break;
2540 case 1:
2541 s = (char*) "publish-novel-history";
2542 data = publish_novel->totals;
2543 break;
2544 case 2:
2545 s = (char*) "publish-top-5-percent-history";
2546 data = publish_total->five;
2547 break;
2548 case 3:
2549 s = (char*) "publish-top-10-percent-history";
2550 data = publish_total->ten;
2551 break;
2552 case 4:
2553 s = (char*) "publish-top-20-percent-history";
2554 data = publish_total->twenty;
2555 break;
2556 case 5:
2557 s = (char*) "fetch-total-history";
2558 data = fetch_total->totals;
2559 break;
2560 case 6:
2561 s = (char*) "fetch-successful-history";
2562 data = fetch_successful->totals;
2563 break;
2564 case 7:
2565 s = (char*) "fetch-top-5-percent-history";
2566 data = fetch_total->five;
2567 break;
2568 case 8:
2569 s = (char*) "fetch-top-10-percent-history";
2570 data = fetch_total->ten;
2571 break;
2572 case 9:
2573 s = (char*) "fetch-top-20-percent-history";
2574 data = fetch_total->twenty;
2575 break;
2576 case 10:
2577 s = (char*) "desc-total-history";
2578 data = descs->totals;
2579 break;
2581 format_iso_time(t, current_period->start_of_current_period);
2582 tor_snprintf(cp, len-(cp-buf), "%s %s (%d s) ", s, t,
2583 NUM_SECS_HS_USAGE_SUM_INTERVAL);
2584 cp += strlen(cp);
2585 cp += hs_usage_format_history(cp, len-(cp-buf), data);
2586 strlcat(cp, "\n", len-(cp-buf));
2587 ++cp;
2589 return buf;
2592 /** Write current statistics about hidden service usage to file. */
2593 void
2594 hs_usage_write_statistics_to_file(time_t now)
2596 char *buf;
2597 size_t len;
2598 char *fname;
2599 or_options_t *options = get_options();
2600 /* check if we are up-to-date */
2601 hs_usage_check_if_current_period_is_up_to_date(now);
2602 buf = hs_usage_format_statistics();
2603 len = strlen(options->DataDirectory) + 16;
2604 fname = tor_malloc(len);
2605 tor_snprintf(fname, len, "%s"PATH_SEPARATOR"hsusage",
2606 options->DataDirectory);
2607 write_str_to_file(fname,buf,0);
2608 tor_free(buf);
2609 tor_free(fname);
2612 /*** cell statistics ***/
2614 /** Start of the current buffer stats interval. */
2615 static time_t start_of_buffer_stats_interval;
2617 /** Initialize buffer stats. */
2618 void
2619 rep_hist_buffer_stats_init(time_t now)
2621 start_of_buffer_stats_interval = now;
2624 typedef struct circ_buffer_stats_t {
2625 uint32_t processed_cells;
2626 double mean_num_cells_in_queue;
2627 double mean_time_cells_in_queue;
2628 uint32_t local_circ_id;
2629 } circ_buffer_stats_t;
2631 /** Holds stats. */
2632 smartlist_t *circuits_for_buffer_stats = NULL;
2634 /** Remember cell statistics for circuit <b>circ</b> at time
2635 * <b>end_of_interval</b> and reset cell counters in case the circuit
2636 * remains open in the next measurement interval. */
2637 void
2638 rep_hist_buffer_stats_add_circ(circuit_t *circ, time_t end_of_interval)
2640 circ_buffer_stats_t *stat;
2641 time_t start_of_interval;
2642 int interval_length;
2643 or_circuit_t *orcirc;
2644 if (CIRCUIT_IS_ORIGIN(circ))
2645 return;
2646 orcirc = TO_OR_CIRCUIT(circ);
2647 if (!orcirc->processed_cells)
2648 return;
2649 if (!circuits_for_buffer_stats)
2650 circuits_for_buffer_stats = smartlist_create();
2651 start_of_interval = circ->timestamp_created >
2652 start_of_buffer_stats_interval ?
2653 circ->timestamp_created :
2654 start_of_buffer_stats_interval;
2655 interval_length = (int) (end_of_interval - start_of_interval);
2656 stat = tor_malloc_zero(sizeof(circ_buffer_stats_t));
2657 stat->processed_cells = orcirc->processed_cells;
2658 /* 1000.0 for s -> ms; 2.0 because of app-ward and exit-ward queues */
2659 stat->mean_num_cells_in_queue = interval_length == 0 ? 0.0 :
2660 (double) orcirc->total_cell_waiting_time /
2661 (double) interval_length / 1000.0 / 2.0;
2662 stat->mean_time_cells_in_queue =
2663 (double) orcirc->total_cell_waiting_time /
2664 (double) orcirc->processed_cells;
2665 smartlist_add(circuits_for_buffer_stats, stat);
2666 orcirc->total_cell_waiting_time = 0;
2667 orcirc->processed_cells = 0;
2670 /** Sorting helper: return -1, 1, or 0 based on comparison of two
2671 * circ_buffer_stats_t */
2672 static int
2673 _buffer_stats_compare_entries(const void **_a, const void **_b)
2675 const circ_buffer_stats_t *a = *_a, *b = *_b;
2676 if (a->processed_cells < b->processed_cells)
2677 return 1;
2678 else if (a->processed_cells > b->processed_cells)
2679 return -1;
2680 else
2681 return 0;
2684 /** Write buffer statistics to $DATADIR/stats/buffer-stats. */
2685 void
2686 rep_hist_buffer_stats_write(time_t now)
2688 char *statsdir = NULL, *filename = NULL;
2689 char written[ISO_TIME_LEN+1];
2690 open_file_t *open_file = NULL;
2691 FILE *out;
2692 #define SHARES 10
2693 int processed_cells[SHARES], circs_in_share[SHARES],
2694 number_of_circuits, i;
2695 double queued_cells[SHARES], time_in_queue[SHARES];
2696 smartlist_t *str_build = smartlist_create();
2697 char *str = NULL;
2698 char buf[32];
2699 circuit_t *circ;
2700 /* add current circuits to stats */
2701 for (circ = _circuit_get_global_list(); circ; circ = circ->next)
2702 rep_hist_buffer_stats_add_circ(circ, now);
2703 /* calculate deciles */
2704 memset(processed_cells, 0, SHARES * sizeof(int));
2705 memset(circs_in_share, 0, SHARES * sizeof(int));
2706 memset(queued_cells, 0, SHARES * sizeof(double));
2707 memset(time_in_queue, 0, SHARES * sizeof(double));
2708 if (!circuits_for_buffer_stats)
2709 circuits_for_buffer_stats = smartlist_create();
2710 smartlist_sort(circuits_for_buffer_stats,
2711 _buffer_stats_compare_entries);
2712 number_of_circuits = smartlist_len(circuits_for_buffer_stats);
2713 if (number_of_circuits < 1) {
2714 log_info(LD_HIST, "Attempt to write cell statistics to disk failed. "
2715 "We haven't seen a single circuit to report about.");
2716 goto done;
2718 i = 0;
2719 SMARTLIST_FOREACH_BEGIN(circuits_for_buffer_stats,
2720 circ_buffer_stats_t *, stat)
2722 int share = i++ * SHARES / number_of_circuits;
2723 processed_cells[share] += stat->processed_cells;
2724 queued_cells[share] += stat->mean_num_cells_in_queue;
2725 time_in_queue[share] += stat->mean_time_cells_in_queue;
2726 circs_in_share[share]++;
2728 SMARTLIST_FOREACH_END(stat);
2729 /* clear buffer stats history */
2730 SMARTLIST_FOREACH(circuits_for_buffer_stats, circ_buffer_stats_t *,
2731 stat, tor_free(stat));
2732 smartlist_clear(circuits_for_buffer_stats);
2733 /* write to file */
2734 statsdir = get_datadir_fname("stats");
2735 if (check_private_dir(statsdir, CPD_CREATE) < 0)
2736 goto done;
2737 filename = get_datadir_fname2("stats", "buffer-stats");
2738 out = start_writing_to_stdio_file(filename, OPEN_FLAGS_APPEND,
2739 0600, &open_file);
2740 if (!out)
2741 goto done;
2742 format_iso_time(written, now);
2743 if (fprintf(out, "cell-stats-end %s (%d s)\n", written,
2744 (unsigned) (now - start_of_buffer_stats_interval)) < 0)
2745 goto done;
2746 for (i = 0; i < SHARES; i++) {
2747 tor_snprintf(buf, sizeof(buf), "%d", !circs_in_share[i] ? 0 :
2748 processed_cells[i] / circs_in_share[i]);
2749 smartlist_add(str_build, tor_strdup(buf));
2751 str = smartlist_join_strings(str_build, ",", 0, NULL);
2752 if (fprintf(out, "cell-processed-cells %s\n", str) < 0)
2753 goto done;
2754 tor_free(str);
2755 SMARTLIST_FOREACH(str_build, char *, c, tor_free(c));
2756 smartlist_clear(str_build);
2757 for (i = 0; i < SHARES; i++) {
2758 tor_snprintf(buf, sizeof(buf), "%.2f", circs_in_share[i] == 0 ? 0.0 :
2759 queued_cells[i] / (double) circs_in_share[i]);
2760 smartlist_add(str_build, tor_strdup(buf));
2762 str = smartlist_join_strings(str_build, ",", 0, NULL);
2763 if (fprintf(out, "cell-queued-cells %s\n", str) < 0)
2764 goto done;
2765 tor_free(str);
2766 SMARTLIST_FOREACH(str_build, char *, c, tor_free(c));
2767 smartlist_clear(str_build);
2768 for (i = 0; i < SHARES; i++) {
2769 tor_snprintf(buf, sizeof(buf), "%.0f", circs_in_share[i] == 0 ? 0.0 :
2770 time_in_queue[i] / (double) circs_in_share[i]);
2771 smartlist_add(str_build, tor_strdup(buf));
2773 str = smartlist_join_strings(str_build, ",", 0, NULL);
2774 if (fprintf(out, "cell-time-in-queue %s\n", str) < 0)
2775 goto done;
2776 tor_free(str);
2777 SMARTLIST_FOREACH(str_build, char *, c, tor_free(c));
2778 smartlist_free(str_build);
2779 str_build = NULL;
2780 if (fprintf(out, "cell-circuits-per-decile %d\n",
2781 (number_of_circuits + SHARES - 1) / SHARES) < 0)
2782 goto done;
2783 finish_writing_to_file(open_file);
2784 open_file = NULL;
2785 start_of_buffer_stats_interval = now;
2786 done:
2787 if (open_file)
2788 abort_writing_to_file(open_file);
2789 tor_free(filename);
2790 tor_free(statsdir);
2791 if (str_build) {
2792 SMARTLIST_FOREACH(str_build, char *, c, tor_free(c));
2793 smartlist_free(str_build);
2795 tor_free(str);
2796 #undef SHARES