pass bucket sizes num_read and num_written around as size_ts
[tor.git] / src / or / rephist.c
blob76b14247aaf625d9b0cea166fc38bc54f678aeae
1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2008, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
4 /* $Id$ */
5 const char rephist_c_id[] =
6 "$Id$";
8 /**
9 * \file rephist.c
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.
13 **/
15 #include "or.h"
16 #include "ht.h"
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? */
43 time_t since;
44 /** When did we most recently note a change to this link */
45 time_t changed;
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;
50 } link_history_t;
52 /** History of an OR. */
53 typedef struct or_history_t {
54 /** When did we start tracking this OR? */
55 time_t since;
56 /** When did we most recently note a change to this OR? */
57 time_t changed;
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
63 * 'up_since'? */
64 unsigned long uptime;
65 /** How many seconds have we been unable to connect to this OR before
66 * 'down_since'? */
67 unsigned long downtime;
68 /** If nonzero, we have been connected since this time. */
69 time_t up_since;
70 /** If nonzero, we have been unable to connect since this time. */
71 time_t down_since;
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? */
79 time_t start_of_run;
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;
90 } or_history_t;
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;
96 /** */
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)
107 or_history_t *hist;
109 if (tor_mem_is_zero(id, DIGEST_LEN))
110 return NULL;
112 hist = digestmap_get(history_map, id);
113 if (!hist) {
114 hist = tor_malloc_zero(sizeof(or_history_t));
115 rephist_total_alloc += sizeof(or_history_t);
116 rephist_total_num++;
117 hist->link_history_map = digestmap_new();
118 hist->since = hist->changed = time(NULL);
119 digestmap_set(history_map, id, hist);
121 return 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
126 * identity digest.)
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);
134 if (!orhist)
135 return NULL;
136 if (tor_mem_is_zero(to_id, DIGEST_LEN))
137 return NULL;
138 lhist = (link_history_t*) digestmap_get(orhist->link_history_map, to_id);
139 if (!lhist) {
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);
145 return lhist;
148 /** Helper: free storage held by a single link history entry. */
149 static void
150 _free_link_history(void *val)
152 rephist_total_alloc -= sizeof(link_history_t);
153 tor_free(val);
156 /** Helper: free storage held by a single OR history entry. */
157 static void
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);
163 rephist_total_num--;
164 tor_free(hist);
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>.
170 static void
171 update_or_history(or_history_t *hist, time_t when)
173 tor_assert(hist);
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. */
185 void
186 rep_hist_init(void)
188 history_map = digestmap_new();
189 bw_arrays_init();
190 predicted_ports_init();
191 hs_usage_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. */
197 static void
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);
202 hist->up_since = 0;
204 if (failed && !hist->down_since) {
205 hist->down_since = when;
209 /** Helper: note that we are connected to the router with history
210 * <b>hist</b>. */
211 static void
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>.
226 void
227 rep_hist_note_connect_failed(const char* id, time_t when)
229 or_history_t *hist;
230 hist = get_or_history(id);
231 if (!hist)
232 return;
233 ++hist->n_conn_fail;
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>.
241 void
242 rep_hist_note_connect_succeeded(const char* id, time_t when)
244 or_history_t *hist;
245 hist = get_or_history(id);
246 if (!hist)
247 return;
248 ++hist->n_conn_ok;
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>.
256 void
257 rep_hist_note_disconnect(const char* id, time_t when)
259 or_history_t *hist;
260 hist = get_or_history(id);
261 if (!hist)
262 return;
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>.
270 void
271 rep_hist_note_connection_died(const char* id, time_t when)
273 or_history_t *hist;
274 if (!id) {
275 /* If conn has no identity, it didn't complete its handshake, or something
276 * went wrong. Ignore it.
278 return;
280 hist = get_or_history(id);
281 if (!hist)
282 return;
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. */
289 void
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. */
307 void
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. */
331 time_t
332 rep_hist_downrate_old_runs(time_t now)
334 digestmap_iter_t *orhist_it;
335 const char *digest1;
336 or_history_t *hist;
337 void *hist_p;
338 double alpha = 1.0;
340 if (!history_map)
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);
358 hist = 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>. */
372 static double
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. */
386 return 0.0;
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. */
394 static long
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);
403 return total;
406 /** Helper: Return the weighted percent-of-time-online of the router with
407 * history <b>hist</b>. */
408 static double
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);
416 up += run_length;
417 total += run_length;
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. */
426 double
427 rep_hist_get_stability(const char *id, time_t when)
429 or_history_t *hist = get_or_history(id);
430 if (!hist)
431 return 0.0;
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. */
438 double
439 rep_hist_get_weighted_fractional_uptime(const char *id, time_t when)
441 or_history_t *hist = get_or_history(id);
442 if (!hist)
443 return 0.0;
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.
454 long
455 rep_hist_get_weighted_time_known(const char *id, time_t when)
457 or_history_t *hist = get_or_history(id);
458 if (!hist)
459 return 0;
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
476 * <b>to_name</b>.
478 void
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);
484 if (!hist)
485 return;
486 ++hist->n_extend_ok;
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
492 * failed.
494 void
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);
500 if (!hist)
501 return;
502 ++hist->n_extend_fail;
503 hist->changed = time(NULL);
506 /** Log all the reliability data we have remembered, with the chosen
507 * severity.
509 void
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;
519 double uptime;
520 char buffer[2048];
521 size_t len;
522 int ret;
523 unsigned long upt, downt;
524 routerinfo_t *r;
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)) {
533 double s;
534 long stability;
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)))
539 name1 = r->nickname;
540 else
541 name1 = "(unknown)";
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);
547 stability = (long)s;
548 if (upt+downt) {
549 uptime = ((double)upt) / (upt+downt);
550 } else {
551 uptime=1.0;
553 log(severity, LD_GENERAL,
554 "OR %s [%s]: %ld/%ld good connections; uptime %ld/%ld sec (%.2f%%); "
555 "wmtbf %lu:%02lu:%02lu",
556 name1, hexdigest1,
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,
567 lhist_it)) {
568 digestmap_iter_get(lhist_it, &digest2, &link_history_p);
569 if ((r = router_get_by_digest(digest2)))
570 name2 = r->nickname;
571 else
572 name2 = "(unknown)";
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);
579 if (ret<0)
580 break;
581 else
582 len += ret;
584 log(severity, LD_GENERAL, "%s", buffer);
589 /** Remove history info for routers/links that haven't changed since
590 * <b>before</b>.
592 void
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;
600 const char *d1, *d2;
602 orhist_it = digestmap_iter_init(history_map);
603 while (!digestmap_iter_done(orhist_it)) {
604 int remove;
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);
611 if (remove) {
612 orhist_it = digestmap_iter_next_rmv(history_map, orhist_it);
613 free_or_history(or_history);
614 continue;
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,
622 lhist_it);
623 rephist_total_alloc -= sizeof(link_history_t);
624 tor_free(link_history);
625 continue;
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;
640 const char *digest;
641 void *or_history_p;
642 or_history_t *hist;
643 open_file_t *open_file = NULL;
644 FILE *f;
647 char *filename = get_datadir_fname("router-stability");
648 f = start_writing_to_stdio_file(filename, OPEN_FLAGS_REPLACE|O_TEXT, 0600,
649 &open_file);
650 tor_free(filename);
651 if (!f)
652 return -1;
655 /* File format is:
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
667 PUT("format 2\n");
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));
681 PUT("data\n");
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);
698 t = time_buf;
700 PRINTF((f, "+MTBF %lu %.5lf%s%s\n",
701 hist->weighted_run_length, hist->total_run_weights,
702 t ? " S=" : "", t ? t : ""));
703 t = NULL;
704 if (hist->start_of_downtime > 0) {
705 format_iso_time(time_buf, hist->start_of_downtime);
706 t = time_buf;
708 PRINTF((f, "+WFU %lu %lu%s%s\n",
709 hist->weighted_uptime, hist->total_weighted_time,
710 t ? " S=" : "", t ? t : ""));
713 PUT(".\n");
715 #undef PUT
716 #undef PRINTF
718 return finish_writing_to_file(open_file);
719 err:
720 abort_writing_to_file(open_file);
721 return -1;
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. */
727 static int
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))
733 return i;
734 if (!strcmpstart(line, "R "))
735 return -1;
737 return -1;
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. */
744 static int
745 parse_possibly_bad_iso_time(const char *s, time_t *time_out)
747 int year;
748 char b[5];
749 strlcpy(b, s, sizeof(b));
750 b[4] = '\0';
751 year = (int)tor_parse_long(b, 10, 0, INT_MAX, NULL, NULL);
752 if (year < 1970) {
753 *time_out = 0;
754 ++n_bogus_times;
755 return 0;
756 } else
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
763 * <b>stored_at</b>.
765 static INLINE time_t
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)
769 return 0;
770 else if (t < started_measuring)
771 return started_measuring;
772 else if (t > stored_at)
773 return 0;
774 else {
775 long run_length = stored_at - t;
776 t = now - run_length;
777 if (t < started_measuring)
778 t = started_measuring;
779 return t;
783 /** Load MTBF data from disk. Returns 0 on success or recoverable error, -1
784 * on failure. */
786 rep_hist_load_mtbf_data(time_t now)
788 /* XXXX won't handle being called while history is already populated. */
789 smartlist_t *lines;
790 const char *line = NULL;
791 int r=0, i;
792 time_t last_downrated = 0, stored_at = 0, tracked_since = 0;
793 time_t latest_possible_start = now;
794 long format = -1;
797 char *filename = get_datadir_fname("router-stability");
798 char *d = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
799 tor_free(filename);
800 if (!d)
801 return -1;
802 lines = smartlist_create();
803 smartlist_split_string(lines, d, "\n", SPLIT_SKIP_SPACE, 0);
804 tor_free(d);
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) {
817 log_warn(LD_GENERAL,
818 "Unrecognized format in mtbf history file. Skipping.");
819 goto err;
821 for (i = 1; i < smartlist_len(lines); ++i) {
822 line = smartlist_get(lines, i);
823 if (!strcmp(line, "data"))
824 break;
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 "
828 "history file.");
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 "
833 "history file.");
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 "
838 "history file.");
841 if (last_downrated > now)
842 last_downrated = now;
843 if (tracked_since > now)
844 tracked_since = now;
846 if (!stored_at) {
847 log_warn(LD_GENERAL, "No stored time recorded.");
848 goto err;
851 if (line && !strcmp(line, "data"))
852 ++i;
854 n_bogus_times = 0;
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;
864 long wrl = 0;
865 double trw = 0;
866 long wt_uptime = 0, total_wt_time = 0;
867 int n;
868 or_history_t *hist;
869 line = smartlist_get(lines, i);
870 if (!strcmp(line, "."))
871 break;
873 mtbf_timebuf[0] = '\0';
874 wfu_timebuf[0] = '\0';
876 if (format == 1) {
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));
881 continue;
883 have_mtbf = 1;
884 } else {
885 // format == 2.
886 int mtbf_idx, wfu_idx;
887 if (strcmpstart(line, "R ") || strlen(line) < 2+HEX_DIGEST_LEN)
888 continue;
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 ");
892 if (mtbf_idx >= 0) {
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) {
897 have_mtbf = 1;
898 } else {
899 log_warn(LD_GENERAL, "Couldn't scan +MTBF line %s",
900 escaped(mtbfline));
903 if (wfu_idx >= 0) {
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) {
909 have_wfu = 1;
910 } else {
911 log_warn(LD_GENERAL, "Couldn't scan +WFU line %s", escaped(wfuline));
914 if (wfu_idx > i)
915 i = wfu_idx;
916 if (mtbf_idx > i)
917 i = mtbf_idx;
919 if (base16_decode(digest, DIGEST_LEN, hexbuf, HEX_DIGEST_LEN) < 0) {
920 log_warn(LD_GENERAL, "Couldn't hex string %s", escaped(hexbuf));
921 continue;
923 hist = get_or_history(digest);
924 if (!hist)
925 continue;
927 if (have_mtbf) {
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,
935 tracked_since);
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;
942 if (have_wfu) {
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,
950 tracked_since);
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.");
957 if (!tracked_since)
958 tracked_since = latest_possible_start;
960 stability_last_downrated = last_downrated;
961 started_tracking_stability = tracked_since;
963 goto done;
964 err:
965 r = -1;
966 done:
967 SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
968 smartlist_free(lines);
969 return r;
972 /** For how many seconds do we keep track of individual per-second bandwidth
973 * totals? */
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
983 * time period.
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
994 * current period. */
995 uint64_t total_in_period; /**< Total bytes transferred in the current
996 * period. */
998 /** When does the next period begin? */
999 time_t next_period;
1000 /** Where in 'maxima' should the maximum bandwidth usage for the current
1001 * period be stored? */
1002 int next_max_idx;
1003 /** How many values in maxima/totals have been set ever? */
1004 int num_maxes_set;
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
1010 * periods */
1011 uint64_t totals[NUM_TOTALS];
1012 } bw_array_t;
1014 /** Shift the current period of b forward by one. */
1015 static void
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)
1027 ++b->num_maxes_set;
1028 /* Reset max_total. */
1029 b->max_total = 0;
1030 /* Reset total_in_period. */
1031 b->total_in_period = 0;
1034 /** Shift the current observation time of 'b' forward by one second. */
1035 static INLINE void
1036 advance_obs(bw_array_t *b)
1038 int nextidx;
1039 uint64_t total;
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)
1049 nextidx = 0;
1051 b->total_obs = total - b->obs[nextidx];
1052 b->obs[nextidx]=0;
1053 b->cur_obs_idx = nextidx;
1055 if (++b->cur_obs_time >= b->next_period)
1056 commit_max(b);
1059 /** Add <b>n</b> bytes to the number of bytes in <b>b</b> for second
1060 * <b>when</b>. */
1061 static INLINE void
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)
1066 return;
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)
1071 advance_obs(b);
1073 b->obs[b->cur_obs_idx] += n;
1074 b->total_in_period += n;
1077 /** Allocate, initialize, and return a new bw_array. */
1078 static bw_array_t *
1079 bw_array_new(void)
1081 bw_array_t *b;
1082 time_t start;
1083 b = tor_malloc_zero(sizeof(bw_array_t));
1084 rephist_total_alloc += sizeof(bw_array_t);
1085 start = time(NULL);
1086 b->cur_obs_time = start;
1087 b->next_period = start + NUM_SECS_BW_SUM_INTERVAL;
1088 return b;
1091 static bw_array_t *read_array = NULL;
1092 static bw_array_t *write_array = NULL;
1094 /** Set up read_array and write_array. */
1095 static void
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.
1109 void
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)
1126 void
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.)
1137 static uint64_t
1138 find_largest_max(bw_array_t *b)
1140 int i;
1141 uint64_t max;
1142 max=0;
1143 for (i=0; i<NUM_TOTALS; ++i) {
1144 if (b->maxima[i]>max)
1145 max = b->maxima[i];
1147 return 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)
1159 uint64_t w,r;
1160 r = find_largest_max(read_array);
1161 w = find_largest_max(write_array);
1162 if (r>w)
1163 return (int)(U64_TO_DBL(w)/NUM_SECS_ROLLING_MEASURE);
1164 else
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.
1174 static size_t
1175 rep_hist_fill_bandwidth_history(char *buf, size_t len, bw_array_t *b)
1177 char *cp = buf;
1178 int i, n;
1179 or_options_t *options = get_options();
1180 uint64_t cutoff;
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.*/
1184 i = 0;
1185 } else {
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;
1196 } else {
1197 cutoff = UINT64_MAX;
1200 for (n=0; n<b->num_maxes_set; ++n,++i) {
1201 uint64_t total;
1202 if (i >= NUM_TOTALS)
1203 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;
1207 if (total > cutoff)
1208 total = cutoff;
1210 if (n==(b->num_maxes_set-1))
1211 tor_snprintf(cp, len-(cp-buf), U64_FORMAT, U64_PRINTF_ARG(total));
1212 else
1213 tor_snprintf(cp, len-(cp-buf), U64_FORMAT",", U64_PRINTF_ARG(total));
1214 cp += strlen(cp);
1216 return cp-buf;
1219 /** Allocate and return lines for representing this server's bandwidth
1220 * history in its descriptor.
1222 char *
1223 rep_hist_get_bandwidth_lines(int for_extrainfo)
1225 char *buf, *cp;
1226 char t[ISO_TIME_LEN+1];
1227 int r;
1228 bw_array_t *b;
1229 size_t len;
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);
1234 cp = buf;
1235 for (r=0;r<2;++r) {
1236 b = r?read_array:write_array;
1237 tor_assert(b);
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);
1243 cp += strlen(cp);
1244 cp += rep_hist_fill_bandwidth_history(cp, len-(cp-buf), b);
1245 strlcat(cp, "\n", len-(cp-buf));
1246 ++cp;
1248 return buf;
1251 /** Update <b>state</b> with the newest bandwidth history. */
1252 void
1253 rep_hist_update_state(or_state_t *state)
1255 int len, r;
1256 char *buf, *cp;
1257 smartlist_t **s_values;
1258 time_t *s_begins;
1259 int *s_interval;
1260 bw_array_t *b;
1262 len = 20*NUM_TOTALS+1;
1263 buf = tor_malloc_zero(len);
1265 for (r=0;r<2;++r) {
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;
1271 if (*s_values) {
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);
1285 *s_begins = 0;
1286 *s_interval = 900;
1287 *s_values = smartlist_create();
1288 continue;
1290 *s_begins = b->next_period;
1291 *s_interval = NUM_SECS_BW_SUM_INTERVAL;
1292 cp = buf;
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);
1300 tor_free(buf);
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);
1312 uint64_t v;
1313 int r,i,ok;
1314 int all_ok = 1;
1315 int s_interval;
1316 smartlist_t *s_values;
1317 bw_array_t *b;
1319 /* Assert they already have been malloced */
1320 tor_assert(read_array && write_array);
1322 for (r=0;r<2;++r) {
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));
1329 if (start > now)
1330 continue;
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);
1335 if (!ok) {
1336 all_ok=0;
1337 log_notice(LD_GENERAL, "Could not parse '%s' into a number.'", cp);
1339 if (start < now) {
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) {
1349 b->obs[i] = 0;
1351 b->total_obs = 0;
1352 for (i=0; i<NUM_TOTALS; ++i) {
1353 b->maxima[i] = 0;
1355 b->max_total = 0;
1358 if (!all_ok) {
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();
1365 return -1;
1367 return 0;
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.
1381 static void
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));
1387 *tmp_port = port;
1388 *tmp_time = now;
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.
1398 static void
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
1407 * be used.
1409 static void
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.
1424 void
1425 rep_hist_note_used_port(uint16_t port, time_t now)
1427 int i;
1428 uint16_t *tmp_port;
1429 time_t *tmp_time;
1431 tor_assert(predicted_ports_list);
1432 tor_assert(predicted_ports_times);
1434 if (!port) /* record nothing */
1435 return;
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) {
1441 *tmp_time = now;
1442 return;
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.
1458 smartlist_t *
1459 rep_hist_get_predicted_ports(time_t now)
1461 int i;
1462 uint16_t *tmp_port;
1463 time_t *tmp_time;
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);
1477 tor_free(tmp_port);
1478 tor_free(tmp_time);
1479 i--;
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.
1490 void
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>. */
1504 void
1505 rep_hist_note_used_internal(time_t now, int need_uptime, int need_capacity)
1507 predicted_internal_time = now;
1508 if (need_uptime)
1509 predicted_internal_uptime_time = now;
1510 if (need_capacity)
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,
1517 int *need_capacity)
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)
1527 *need_uptime = 1;
1528 if (predicted_internal_capacity_time + PREDICTED_CIRCS_RELEVANCE_TIME >= now)
1529 *need_capacity = 1;
1530 return 1;
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))
1547 return 0;
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()))
1552 return 0;
1553 if (!check_whether_dirport_reachable())
1554 return 0;
1556 return 1;
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>. */
1572 void
1573 note_crypto_pk_op(pk_op_t operation)
1575 switch (operation)
1577 case SIGN_DIR:
1578 n_signed_dir_objs++;
1579 break;
1580 case SIGN_RTR:
1581 n_signed_routerdescs++;
1582 break;
1583 case VERIFY_DIR:
1584 n_verified_dir_objs++;
1585 break;
1586 case VERIFY_RTR:
1587 n_verified_routerdescs++;
1588 break;
1589 case ENC_ONIONSKIN:
1590 n_onionskins_encrypted++;
1591 break;
1592 case DEC_ONIONSKIN:
1593 n_onionskins_decrypted++;
1594 break;
1595 case TLS_HANDSHAKE_C:
1596 n_tls_client_handshakes++;
1597 break;
1598 case TLS_HANDSHAKE_S:
1599 n_tls_server_handshakes++;
1600 break;
1601 case REND_CLIENT:
1602 n_rend_client_ops++;
1603 break;
1604 case REND_MID:
1605 n_rend_mid_ops++;
1606 break;
1607 case REND_SERVER:
1608 n_rend_server_ops++;
1609 break;
1610 default:
1611 log_warn(LD_BUG, "Unknown pk operation %d", operation);
1615 /** Log the number of times we've done each public/private-key operation. */
1616 void
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. */
1646 void
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
1658 * use? */
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. */
1671 uint32_t count;
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;
1686 } hs_usage_list_t;
1688 /** Tracks service-related observations in the current period and their
1689 * history. */
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
1699 * observations. */
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? */
1717 int next_idx;
1718 /** How many values in history have been set ever? (upper bound!) */
1719 int num_set;
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);
1740 e->count = 1;
1741 e->next = NULL;
1742 return e;
1745 /** Creates an empty ordered list. */
1746 static hs_usage_list_t *
1747 hs_usage_list_new(void)
1749 hs_usage_list_t *l;
1750 l = tor_malloc_zero(sizeof(hs_usage_list_t));
1751 rephist_total_alloc += sizeof(hs_usage_list_t);
1752 l->start = NULL;
1753 l->total_count = 0;
1754 l->total_service_ids = 0;
1755 return l;
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();
1766 return h;
1769 /** Creates an empty structure for storing general period-related
1770 * observations. */
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);
1777 return p;
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;
1785 time_t now;
1786 c = tor_malloc_zero(sizeof(hs_usage_current_observation_period_t));
1787 rephist_total_alloc += sizeof(hs_usage_current_observation_period_t);
1788 now = time(NULL);
1789 c->start_of_current_period = now;
1790 c->start_of_next_period = now + NUM_SECS_HS_USAGE_SUM_INTERVAL;
1791 return c;
1794 /** Initializes the structures for collecting hidden service usage data. */
1795 static void
1796 hs_usage_init(void)
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. */
1808 static void
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);
1817 tor_free(current);
1818 current = tmp;
1820 /* reset attributes */
1821 lst->start = NULL;
1822 lst->total_count = 0;
1823 lst->total_service_ids = 0;
1824 return;
1827 /** Frees the memory used by the given list. */
1828 static void
1829 hs_usage_list_free(hs_usage_list_t *lst)
1831 if (!lst)
1832 return;
1833 hs_usage_list_clear(lst);
1834 rephist_total_alloc -= sizeof(hs_usage_list_t);
1835 tor_free(lst);
1838 /** Frees the memory used by the given service-related observations. */
1839 static void
1840 hs_usage_service_related_observation_free(
1841 hs_usage_service_related_observation_t *s)
1843 if (!s)
1844 return;
1845 hs_usage_list_free(s->list);
1846 rephist_total_alloc -= sizeof(hs_usage_service_related_observation_t);
1847 tor_free(s);
1850 /** Frees the memory used by the given period-specific observations. */
1851 static void
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);
1856 tor_free(s);
1859 /** Frees the memory used by period-specific information. */
1860 static void
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);
1865 tor_free(s);
1868 /** Frees all memory that was used for collecting hidden service usage data. */
1869 void
1870 hs_usage_free_all(void)
1872 hs_usage_general_period_related_observations_free(descs);
1873 descs = NULL;
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
1884 * list. */
1885 static void
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)) {
1892 previous = current;
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). */
1899 /* create elem */
1900 hs_usage_list_elem_t *e = hs_usage_list_elem_new();
1901 /* update list attributes (one new elem, one new occurrence) */
1902 lst->total_count++;
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 */
1909 lst->start = e;
1910 } else {
1911 /* there were elems in the list before */
1912 previous->next = e;
1914 } else {
1915 /* found! add occurrence to elem and consider resorting */
1916 /* update list attributes (no new elem, but one new occurrence) */
1917 lst->total_count++;
1918 /* add occurrence to elem */
1919 current->count++;
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;
1931 } else {
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;
1940 /* insert here */
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. */
1950 static void
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
1956 * distributions */
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;
1961 /* temp values */
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;
1967 int i=0;
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;
1975 i++;
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. */
1988 static void
1989 hs_usage_advance_current_observation_period(void)
1991 /* aggregate observations to history, including frequency distribution
1992 * arrays */
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. */
2014 static void
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. */
2024 static void
2025 hs_usage_add_service_related_observation(
2026 hs_usage_service_related_observation_t *h,
2027 time_t now,
2028 const char *service_id)
2030 if (now < current_period->start_of_current_period) {
2031 /* don't record old data */
2032 return;
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. */
2042 void
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. */
2050 void
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. */
2058 void
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. */
2067 void
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. */
2074 static size_t
2075 hs_usage_format_history(char *buf, size_t len, uint32_t *data)
2077 char *cp = buf; /* pointer where we are in the buffer */
2078 int i, n;
2079 if (current_period->num_set <= current_period->next_idx) {
2080 i = 0; /* not been through circular array */
2081 } else {
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]);
2090 else
2091 tor_snprintf(cp, len-(cp-buf), "%d,", data[i]);
2092 cp += strlen(cp);
2094 return cp-buf;
2097 /** Writes the complete usage history as hidden service authoritative directory
2098 * to a string. */
2099 static char *
2100 hs_usage_format_statistics(void)
2102 char *buf, *cp, *s = NULL;
2103 char t[ISO_TIME_LEN+1];
2104 int r;
2105 uint32_t *data = NULL;
2106 size_t len;
2107 len = (70+20*NUM_TOTALS_HS_USAGE)*11;
2108 buf = tor_malloc_zero(len);
2109 cp = buf;
2110 for (r = 0; r < 11; ++r) {
2111 switch (r) {
2112 case 0:
2113 s = (char*) "publish-total-history";
2114 data = publish_total->totals;
2115 break;
2116 case 1:
2117 s = (char*) "publish-novel-history";
2118 data = publish_novel->totals;
2119 break;
2120 case 2:
2121 s = (char*) "publish-top-5-percent-history";
2122 data = publish_total->five;
2123 break;
2124 case 3:
2125 s = (char*) "publish-top-10-percent-history";
2126 data = publish_total->ten;
2127 break;
2128 case 4:
2129 s = (char*) "publish-top-20-percent-history";
2130 data = publish_total->twenty;
2131 break;
2132 case 5:
2133 s = (char*) "fetch-total-history";
2134 data = fetch_total->totals;
2135 break;
2136 case 6:
2137 s = (char*) "fetch-successful-history";
2138 data = fetch_successful->totals;
2139 break;
2140 case 7:
2141 s = (char*) "fetch-top-5-percent-history";
2142 data = fetch_total->five;
2143 break;
2144 case 8:
2145 s = (char*) "fetch-top-10-percent-history";
2146 data = fetch_total->ten;
2147 break;
2148 case 9:
2149 s = (char*) "fetch-top-20-percent-history";
2150 data = fetch_total->twenty;
2151 break;
2152 case 10:
2153 s = (char*) "desc-total-history";
2154 data = descs->totals;
2155 break;
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);
2160 cp += strlen(cp);
2161 cp += hs_usage_format_history(cp, len-(cp-buf), data);
2162 strlcat(cp, "\n", len-(cp-buf));
2163 ++cp;
2165 return buf;
2168 /** Write current statistics about hidden service usage to file. */
2169 void
2170 hs_usage_write_statistics_to_file(time_t now)
2172 char *buf;
2173 size_t len;
2174 char *fname;
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);
2184 tor_free(buf);
2185 tor_free(fname);