Back to index

tor  0.2.3.18-rc
rephist.c
Go to the documentation of this file.
00001 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
00002  * Copyright (c) 2007-2012, The Tor Project, Inc. */
00003 /* See LICENSE for licensing information */
00004 
00013 #include "or.h"
00014 #include "circuitlist.h"
00015 #include "circuituse.h"
00016 #include "config.h"
00017 #include "networkstatus.h"
00018 #include "nodelist.h"
00019 #include "rephist.h"
00020 #include "router.h"
00021 #include "routerlist.h"
00022 #include "ht.h"
00023 
00024 static void bw_arrays_init(void);
00025 static void predicted_ports_init(void);
00026 
00028 uint64_t rephist_total_alloc=0;
00030 uint32_t rephist_total_num=0;
00031 
00034 #define STABILITY_EPSILON   0.0001
00035 
00037 #define STABILITY_ALPHA     0.95
00038 
00039 #define STABILITY_INTERVAL  (12*60*60)
00040 /* (This combination of ALPHA, INTERVAL, and EPSILON makes it so that an
00041  * interval that just ended counts twice as much as one that ended a week ago,
00042  * 20X as much as one that ended a month ago, and routers that have had no
00043  * uptime data for about half a year will get forgotten.) */
00044 
00046 typedef struct link_history_t {
00048   time_t since;
00050   time_t changed;
00052   unsigned long n_extend_ok;
00054   unsigned long n_extend_fail;
00055 } link_history_t;
00056 
00058 typedef struct or_history_t {
00060   time_t since;
00062   time_t changed;
00064   unsigned long n_conn_ok;
00066   unsigned long n_conn_fail;
00069   unsigned long uptime;
00072   unsigned long downtime;
00074   time_t up_since;
00076   time_t down_since;
00077 
00080   tor_addr_t last_reached_addr;
00081 
00083   uint16_t last_reached_port;
00084 
00085   /* === For MTBF tracking: */
00088   unsigned long weighted_run_length;
00091   time_t start_of_run;
00093   double total_run_weights;
00094   /* === For fractional uptime tracking: */
00095   time_t start_of_downtime;
00096   unsigned long weighted_uptime;
00097   unsigned long total_weighted_time;
00098 
00101   digestmap_t *link_history_map;
00102 } or_history_t;
00103 
00106 static time_t stability_last_downrated = 0;
00107 
00109 static time_t started_tracking_stability = 0;
00110 
00112 static digestmap_t *history_map = NULL;
00113 
00116 static or_history_t *
00117 get_or_history(const char* id)
00118 {
00119   or_history_t *hist;
00120 
00121   if (tor_digest_is_zero(id))
00122     return NULL;
00123 
00124   hist = digestmap_get(history_map, id);
00125   if (!hist) {
00126     hist = tor_malloc_zero(sizeof(or_history_t));
00127     rephist_total_alloc += sizeof(or_history_t);
00128     rephist_total_num++;
00129     hist->link_history_map = digestmap_new();
00130     hist->since = hist->changed = time(NULL);
00131     tor_addr_make_unspec(&hist->last_reached_addr);
00132     digestmap_set(history_map, id, hist);
00133   }
00134   return hist;
00135 }
00136 
00141 static link_history_t *
00142 get_link_history(const char *from_id, const char *to_id)
00143 {
00144   or_history_t *orhist;
00145   link_history_t *lhist;
00146   orhist = get_or_history(from_id);
00147   if (!orhist)
00148     return NULL;
00149   if (tor_digest_is_zero(to_id))
00150     return NULL;
00151   lhist = (link_history_t*) digestmap_get(orhist->link_history_map, to_id);
00152   if (!lhist) {
00153     lhist = tor_malloc_zero(sizeof(link_history_t));
00154     rephist_total_alloc += sizeof(link_history_t);
00155     lhist->since = lhist->changed = time(NULL);
00156     digestmap_set(orhist->link_history_map, to_id, lhist);
00157   }
00158   return lhist;
00159 }
00160 
00162 static void
00163 _free_link_history(void *val)
00164 {
00165   rephist_total_alloc -= sizeof(link_history_t);
00166   tor_free(val);
00167 }
00168 
00170 static void
00171 free_or_history(void *_hist)
00172 {
00173   or_history_t *hist = _hist;
00174   digestmap_free(hist->link_history_map, _free_link_history);
00175   rephist_total_alloc -= sizeof(or_history_t);
00176   rephist_total_num--;
00177   tor_free(hist);
00178 }
00179 
00183 static void
00184 update_or_history(or_history_t *hist, time_t when)
00185 {
00186   tor_assert(hist);
00187   if (hist->up_since) {
00188     tor_assert(!hist->down_since);
00189     hist->uptime += (when - hist->up_since);
00190     hist->up_since = when;
00191   } else if (hist->down_since) {
00192     hist->downtime += (when - hist->down_since);
00193     hist->down_since = when;
00194   }
00195 }
00196 
00198 void
00199 rep_hist_init(void)
00200 {
00201   history_map = digestmap_new();
00202   bw_arrays_init();
00203   predicted_ports_init();
00204 }
00205 
00209 static void
00210 mark_or_down(or_history_t *hist, time_t when, int failed)
00211 {
00212   if (hist->up_since) {
00213     hist->uptime += (when - hist->up_since);
00214     hist->up_since = 0;
00215   }
00216   if (failed && !hist->down_since) {
00217     hist->down_since = when;
00218   }
00219 }
00220 
00223 static void
00224 mark_or_up(or_history_t *hist, time_t when)
00225 {
00226   if (hist->down_since) {
00227     hist->downtime += (when - hist->down_since);
00228     hist->down_since = 0;
00229   }
00230   if (!hist->up_since) {
00231     hist->up_since = when;
00232   }
00233 }
00234 
00238 void
00239 rep_hist_note_connect_failed(const char* id, time_t when)
00240 {
00241   or_history_t *hist;
00242   hist = get_or_history(id);
00243   if (!hist)
00244     return;
00245   ++hist->n_conn_fail;
00246   mark_or_down(hist, when, 1);
00247   hist->changed = when;
00248 }
00249 
00253 void
00254 rep_hist_note_connect_succeeded(const char* id, time_t when)
00255 {
00256   or_history_t *hist;
00257   hist = get_or_history(id);
00258   if (!hist)
00259     return;
00260   ++hist->n_conn_ok;
00261   mark_or_up(hist, when);
00262   hist->changed = when;
00263 }
00264 
00268 void
00269 rep_hist_note_disconnect(const char* id, time_t when)
00270 {
00271   or_history_t *hist;
00272   hist = get_or_history(id);
00273   if (!hist)
00274     return;
00275   mark_or_down(hist, when, 0);
00276   hist->changed = when;
00277 }
00278 
00282 void
00283 rep_hist_note_connection_died(const char* id, time_t when)
00284 {
00285   or_history_t *hist;
00286   if (!id) {
00287     /* If conn has no identity, it didn't complete its handshake, or something
00288      * went wrong.  Ignore it.
00289      */
00290     return;
00291   }
00292   hist = get_or_history(id);
00293   if (!hist)
00294     return;
00295   mark_or_down(hist, when, 1);
00296   hist->changed = when;
00297 }
00298 
00301 void
00302 rep_hist_note_router_reachable(const char *id, const tor_addr_t *at_addr,
00303                                const uint16_t at_port, time_t when)
00304 {
00305   or_history_t *hist = get_or_history(id);
00306   int was_in_run = 1;
00307   char tbuf[ISO_TIME_LEN+1];
00308   int addr_changed, port_changed;
00309 
00310   tor_assert(hist);
00311   tor_assert((!at_addr && !at_port) || (at_addr && at_port));
00312 
00313   addr_changed = at_addr &&
00314     tor_addr_compare(at_addr, &hist->last_reached_addr, CMP_EXACT) != 0;
00315   port_changed = at_port && at_port != hist->last_reached_port;
00316 
00317   if (!started_tracking_stability)
00318     started_tracking_stability = time(NULL);
00319   if (!hist->start_of_run) {
00320     hist->start_of_run = when;
00321     was_in_run = 0;
00322   }
00323   if (hist->start_of_downtime) {
00324     long down_length;
00325 
00326     format_local_iso_time(tbuf, hist->start_of_downtime);
00327     log_info(LD_HIST, "Router %s is now Running; it had been down since %s.",
00328              hex_str(id, DIGEST_LEN), tbuf);
00329     if (was_in_run)
00330       log_info(LD_HIST, "  (Paradoxically, it was already Running too.)");
00331 
00332     down_length = when - hist->start_of_downtime;
00333     hist->total_weighted_time += down_length;
00334     hist->start_of_downtime = 0;
00335   } else if (addr_changed || port_changed) {
00336     /* If we're reachable, but the address changed, treat this as some
00337      * downtime. */
00338     int penalty = get_options()->TestingTorNetwork ? 240 : 3600;
00339     networkstatus_t *ns;
00340 
00341     if ((ns = networkstatus_get_latest_consensus())) {
00342       int fresh_interval = (int)(ns->fresh_until - ns->valid_after);
00343       int live_interval = (int)(ns->valid_until - ns->valid_after);
00344       /* on average, a descriptor addr change takes .5 intervals to make it
00345        * into a consensus, and half a liveness period to make it to
00346        * clients. */
00347       penalty = (int)(fresh_interval + live_interval) / 2;
00348     }
00349     format_local_iso_time(tbuf, hist->start_of_run);
00350     log_info(LD_HIST,"Router %s still seems Running, but its address appears "
00351              "to have changed since the last time it was reachable.  I'm "
00352              "going to treat it as having been down for %d seconds",
00353              hex_str(id, DIGEST_LEN), penalty);
00354     rep_hist_note_router_unreachable(id, when-penalty);
00355     rep_hist_note_router_reachable(id, NULL, 0, when);
00356   } else {
00357     format_local_iso_time(tbuf, hist->start_of_run);
00358     if (was_in_run)
00359       log_debug(LD_HIST, "Router %s is still Running; it has been Running "
00360                 "since %s", hex_str(id, DIGEST_LEN), tbuf);
00361     else
00362       log_info(LD_HIST,"Router %s is now Running; it was previously untracked",
00363                hex_str(id, DIGEST_LEN));
00364   }
00365   if (at_addr)
00366     tor_addr_copy(&hist->last_reached_addr, at_addr);
00367   if (at_port)
00368     hist->last_reached_port = at_port;
00369 }
00370 
00373 void
00374 rep_hist_note_router_unreachable(const char *id, time_t when)
00375 {
00376   or_history_t *hist = get_or_history(id);
00377   char tbuf[ISO_TIME_LEN+1];
00378   int was_running = 0;
00379   if (!started_tracking_stability)
00380     started_tracking_stability = time(NULL);
00381 
00382   tor_assert(hist);
00383   if (hist->start_of_run) {
00384     /*XXXX We could treat failed connections differently from failed
00385      * connect attempts. */
00386     long run_length = when - hist->start_of_run;
00387     format_local_iso_time(tbuf, hist->start_of_run);
00388 
00389     hist->total_run_weights += 1.0;
00390     hist->start_of_run = 0;
00391     if (run_length < 0) {
00392       unsigned long penalty = -run_length;
00393 #define SUBTRACT_CLAMPED(var, penalty) \
00394       do { (var) = (var) < (penalty) ? 0 : (var) - (penalty); } while (0)
00395 
00396       SUBTRACT_CLAMPED(hist->weighted_run_length, penalty);
00397       SUBTRACT_CLAMPED(hist->weighted_uptime, penalty);
00398     } else {
00399       hist->weighted_run_length += run_length;
00400       hist->weighted_uptime += run_length;
00401       hist->total_weighted_time += run_length;
00402     }
00403     was_running = 1;
00404     log_info(LD_HIST, "Router %s is now non-Running: it had previously been "
00405              "Running since %s.  Its total weighted uptime is %lu/%lu.",
00406              hex_str(id, DIGEST_LEN), tbuf, hist->weighted_uptime,
00407              hist->total_weighted_time);
00408   }
00409   if (!hist->start_of_downtime) {
00410     hist->start_of_downtime = when;
00411 
00412     if (!was_running)
00413       log_info(LD_HIST, "Router %s is now non-Running; it was previously "
00414                "untracked.", hex_str(id, DIGEST_LEN));
00415   } else {
00416     if (!was_running) {
00417       format_local_iso_time(tbuf, hist->start_of_downtime);
00418 
00419       log_info(LD_HIST, "Router %s is still non-Running; it has been "
00420                "non-Running since %s.", hex_str(id, DIGEST_LEN), tbuf);
00421     }
00422   }
00423 }
00424 
00427 time_t
00428 rep_hist_downrate_old_runs(time_t now)
00429 {
00430   digestmap_iter_t *orhist_it;
00431   const char *digest1;
00432   or_history_t *hist;
00433   void *hist_p;
00434   double alpha = 1.0;
00435 
00436   if (!history_map)
00437     history_map = digestmap_new();
00438   if (!stability_last_downrated)
00439     stability_last_downrated = now;
00440   if (stability_last_downrated + STABILITY_INTERVAL > now)
00441     return stability_last_downrated + STABILITY_INTERVAL;
00442 
00443   /* Okay, we should downrate the data.  By how much? */
00444   while (stability_last_downrated + STABILITY_INTERVAL < now) {
00445     stability_last_downrated += STABILITY_INTERVAL;
00446     alpha *= STABILITY_ALPHA;
00447   }
00448 
00449   log_info(LD_HIST, "Discounting all old stability info by a factor of %f",
00450            alpha);
00451 
00452   /* Multiply every w_r_l, t_r_w pair by alpha. */
00453   for (orhist_it = digestmap_iter_init(history_map);
00454        !digestmap_iter_done(orhist_it);
00455        orhist_it = digestmap_iter_next(history_map,orhist_it)) {
00456     digestmap_iter_get(orhist_it, &digest1, &hist_p);
00457     hist = hist_p;
00458 
00459     hist->weighted_run_length =
00460       (unsigned long)(hist->weighted_run_length * alpha);
00461     hist->total_run_weights *= alpha;
00462 
00463     hist->weighted_uptime = (unsigned long)(hist->weighted_uptime * alpha);
00464     hist->total_weighted_time = (unsigned long)
00465       (hist->total_weighted_time * alpha);
00466   }
00467 
00468   return stability_last_downrated + STABILITY_INTERVAL;
00469 }
00470 
00472 static double
00473 get_stability(or_history_t *hist, time_t when)
00474 {
00475   long total = hist->weighted_run_length;
00476   double total_weights = hist->total_run_weights;
00477 
00478   if (hist->start_of_run) {
00479     /* We're currently in a run.  Let total and total_weights hold the values
00480      * they would hold if the current run were to end now. */
00481     total += (when-hist->start_of_run);
00482     total_weights += 1.0;
00483   }
00484   if (total_weights < STABILITY_EPSILON) {
00485     /* Round down to zero, and avoid divide-by-zero. */
00486     return 0.0;
00487   }
00488 
00489   return total / total_weights;
00490 }
00491 
00494 static long
00495 get_total_weighted_time(or_history_t *hist, time_t when)
00496 {
00497   long total = hist->total_weighted_time;
00498   if (hist->start_of_run) {
00499     total += (when - hist->start_of_run);
00500   } else if (hist->start_of_downtime) {
00501     total += (when - hist->start_of_downtime);
00502   }
00503   return total;
00504 }
00505 
00508 static double
00509 get_weighted_fractional_uptime(or_history_t *hist, time_t when)
00510 {
00511   long total = hist->total_weighted_time;
00512   long up = hist->weighted_uptime;
00513 
00514   if (hist->start_of_run) {
00515     long run_length = (when - hist->start_of_run);
00516     up += run_length;
00517     total += run_length;
00518   } else if (hist->start_of_downtime) {
00519     total += (when - hist->start_of_downtime);
00520   }
00521 
00522   if (!total) {
00523     /* Avoid calling anybody's uptime infinity (which should be impossible if
00524      * the code is working), or NaN (which can happen for any router we haven't
00525      * observed up or down yet). */
00526     return 0.0;
00527   }
00528 
00529   return ((double) up) / total;
00530 }
00531 
00535 long
00536 rep_hist_get_uptime(const char *id, time_t when)
00537 {
00538   or_history_t *hist = get_or_history(id);
00539   if (!hist)
00540     return 0;
00541   if (!hist->start_of_run || when < hist->start_of_run)
00542     return 0;
00543   return when - hist->start_of_run;
00544 }
00545 
00548 double
00549 rep_hist_get_stability(const char *id, time_t when)
00550 {
00551   or_history_t *hist = get_or_history(id);
00552   if (!hist)
00553     return 0.0;
00554 
00555   return get_stability(hist, when);
00556 }
00557 
00560 double
00561 rep_hist_get_weighted_fractional_uptime(const char *id, time_t when)
00562 {
00563   or_history_t *hist = get_or_history(id);
00564   if (!hist)
00565     return 0.0;
00566 
00567   return get_weighted_fractional_uptime(hist, when);
00568 }
00569 
00576 long
00577 rep_hist_get_weighted_time_known(const char *id, time_t when)
00578 {
00579   or_history_t *hist = get_or_history(id);
00580   if (!hist)
00581     return 0;
00582 
00583   return get_total_weighted_time(hist, when);
00584 }
00585 
00588 int
00589 rep_hist_have_measured_enough_stability(void)
00590 {
00591   /* XXXX023 This doesn't do so well when we change our opinion
00592    * as to whether we're tracking router stability. */
00593   return started_tracking_stability < time(NULL) - 4*60*60;
00594 }
00595 
00600 void
00601 rep_hist_note_extend_succeeded(const char *from_id, const char *to_id)
00602 {
00603   link_history_t *hist;
00604   /* log_fn(LOG_WARN, "EXTEND SUCCEEDED: %s->%s",from_name,to_name); */
00605   hist = get_link_history(from_id, to_id);
00606   if (!hist)
00607     return;
00608   ++hist->n_extend_ok;
00609   hist->changed = time(NULL);
00610 }
00611 
00616 void
00617 rep_hist_note_extend_failed(const char *from_id, const char *to_id)
00618 {
00619   link_history_t *hist;
00620   /* log_fn(LOG_WARN, "EXTEND FAILED: %s->%s",from_name,to_name); */
00621   hist = get_link_history(from_id, to_id);
00622   if (!hist)
00623     return;
00624   ++hist->n_extend_fail;
00625   hist->changed = time(NULL);
00626 }
00627 
00631 void
00632 rep_hist_dump_stats(time_t now, int severity)
00633 {
00634   digestmap_iter_t *lhist_it;
00635   digestmap_iter_t *orhist_it;
00636   const char *name1, *name2, *digest1, *digest2;
00637   char hexdigest1[HEX_DIGEST_LEN+1];
00638   char hexdigest2[HEX_DIGEST_LEN+1];
00639   or_history_t *or_history;
00640   link_history_t *link_history;
00641   void *or_history_p, *link_history_p;
00642   double uptime;
00643   char buffer[2048];
00644   size_t len;
00645   int ret;
00646   unsigned long upt, downt;
00647   const node_t *node;
00648 
00649   rep_history_clean(now - get_options()->RephistTrackTime);
00650 
00651   log(severity, LD_HIST, "--------------- Dumping history information:");
00652 
00653   for (orhist_it = digestmap_iter_init(history_map);
00654        !digestmap_iter_done(orhist_it);
00655        orhist_it = digestmap_iter_next(history_map,orhist_it)) {
00656     double s;
00657     long stability;
00658     digestmap_iter_get(orhist_it, &digest1, &or_history_p);
00659     or_history = (or_history_t*) or_history_p;
00660 
00661     if ((node = node_get_by_id(digest1)) && node_get_nickname(node))
00662       name1 = node_get_nickname(node);
00663     else
00664       name1 = "(unknown)";
00665     base16_encode(hexdigest1, sizeof(hexdigest1), digest1, DIGEST_LEN);
00666     update_or_history(or_history, now);
00667     upt = or_history->uptime;
00668     downt = or_history->downtime;
00669     s = get_stability(or_history, now);
00670     stability = (long)s;
00671     if (upt+downt) {
00672       uptime = ((double)upt) / (upt+downt);
00673     } else {
00674       uptime=1.0;
00675     }
00676     log(severity, LD_HIST,
00677         "OR %s [%s]: %ld/%ld good connections; uptime %ld/%ld sec (%.2f%%); "
00678         "wmtbf %lu:%02lu:%02lu",
00679         name1, hexdigest1,
00680         or_history->n_conn_ok, or_history->n_conn_fail+or_history->n_conn_ok,
00681         upt, upt+downt, uptime*100.0,
00682         stability/3600, (stability/60)%60, stability%60);
00683 
00684     if (!digestmap_isempty(or_history->link_history_map)) {
00685       strlcpy(buffer, "    Extend attempts: ", sizeof(buffer));
00686       len = strlen(buffer);
00687       for (lhist_it = digestmap_iter_init(or_history->link_history_map);
00688            !digestmap_iter_done(lhist_it);
00689            lhist_it = digestmap_iter_next(or_history->link_history_map,
00690                                           lhist_it)) {
00691         digestmap_iter_get(lhist_it, &digest2, &link_history_p);
00692         if ((node = node_get_by_id(digest2)) && node_get_nickname(node))
00693           name2 = node_get_nickname(node);
00694         else
00695           name2 = "(unknown)";
00696 
00697         link_history = (link_history_t*) link_history_p;
00698 
00699         base16_encode(hexdigest2, sizeof(hexdigest2), digest2, DIGEST_LEN);
00700         ret = tor_snprintf(buffer+len, 2048-len, "%s [%s](%ld/%ld); ",
00701                         name2,
00702                         hexdigest2,
00703                         link_history->n_extend_ok,
00704                         link_history->n_extend_ok+link_history->n_extend_fail);
00705         if (ret<0)
00706           break;
00707         else
00708           len += ret;
00709       }
00710       log(severity, LD_HIST, "%s", buffer);
00711     }
00712   }
00713 }
00714 
00718 void
00719 rep_history_clean(time_t before)
00720 {
00721   int authority = authdir_mode(get_options());
00722   or_history_t *or_history;
00723   link_history_t *link_history;
00724   void *or_history_p, *link_history_p;
00725   digestmap_iter_t *orhist_it, *lhist_it;
00726   const char *d1, *d2;
00727 
00728   orhist_it = digestmap_iter_init(history_map);
00729   while (!digestmap_iter_done(orhist_it)) {
00730     int remove;
00731     digestmap_iter_get(orhist_it, &d1, &or_history_p);
00732     or_history = or_history_p;
00733 
00734     remove = authority ? (or_history->total_run_weights < STABILITY_EPSILON &&
00735                           !or_history->start_of_run)
00736                        : (or_history->changed < before);
00737     if (remove) {
00738       orhist_it = digestmap_iter_next_rmv(history_map, orhist_it);
00739       free_or_history(or_history);
00740       continue;
00741     }
00742     for (lhist_it = digestmap_iter_init(or_history->link_history_map);
00743          !digestmap_iter_done(lhist_it); ) {
00744       digestmap_iter_get(lhist_it, &d2, &link_history_p);
00745       link_history = link_history_p;
00746       if (link_history->changed < before) {
00747         lhist_it = digestmap_iter_next_rmv(or_history->link_history_map,
00748                                            lhist_it);
00749         rephist_total_alloc -= sizeof(link_history_t);
00750         tor_free(link_history);
00751         continue;
00752       }
00753       lhist_it = digestmap_iter_next(or_history->link_history_map,lhist_it);
00754     }
00755     orhist_it = digestmap_iter_next(history_map, orhist_it);
00756   }
00757 }
00758 
00764 int
00765 rep_hist_record_mtbf_data(time_t now, int missing_means_down)
00766 {
00767   char time_buf[ISO_TIME_LEN+1];
00768 
00769   digestmap_iter_t *orhist_it;
00770   const char *digest;
00771   void *or_history_p;
00772   or_history_t *hist;
00773   open_file_t *open_file = NULL;
00774   FILE *f;
00775 
00776   {
00777     char *filename = get_datadir_fname("router-stability");
00778     f = start_writing_to_stdio_file(filename, OPEN_FLAGS_REPLACE|O_TEXT, 0600,
00779                                     &open_file);
00780     tor_free(filename);
00781     if (!f)
00782       return -1;
00783   }
00784 
00785   /* File format is:
00786    *   FormatLine *KeywordLine Data
00787    *
00788    *   FormatLine = "format 1" NL
00789    *   KeywordLine = Keyword SP Arguments NL
00790    *   Data = "data" NL *RouterMTBFLine "." NL
00791    *   RouterMTBFLine = Fingerprint SP WeightedRunLen SP
00792    *           TotalRunWeights [SP S=StartRunTime] NL
00793    */
00794 #define PUT(s) STMT_BEGIN if (fputs((s),f)<0) goto err; STMT_END
00795 #define PRINTF(args) STMT_BEGIN if (fprintf args <0) goto err; STMT_END
00796 
00797   PUT("format 2\n");
00798 
00799   format_iso_time(time_buf, time(NULL));
00800   PRINTF((f, "stored-at %s\n", time_buf));
00801 
00802   if (started_tracking_stability) {
00803     format_iso_time(time_buf, started_tracking_stability);
00804     PRINTF((f, "tracked-since %s\n", time_buf));
00805   }
00806   if (stability_last_downrated) {
00807     format_iso_time(time_buf, stability_last_downrated);
00808     PRINTF((f, "last-downrated %s\n", time_buf));
00809   }
00810 
00811   PUT("data\n");
00812 
00813   /* XXX Nick: now bridge auths record this for all routers too.
00814    * Should we make them record it only for bridge routers? -RD
00815    * Not for 0.2.0. -NM */
00816   for (orhist_it = digestmap_iter_init(history_map);
00817        !digestmap_iter_done(orhist_it);
00818        orhist_it = digestmap_iter_next(history_map,orhist_it)) {
00819     char dbuf[HEX_DIGEST_LEN+1];
00820     const char *t = NULL;
00821     digestmap_iter_get(orhist_it, &digest, &or_history_p);
00822     hist = (or_history_t*) or_history_p;
00823 
00824     base16_encode(dbuf, sizeof(dbuf), digest, DIGEST_LEN);
00825 
00826     if (missing_means_down && hist->start_of_run &&
00827         !router_get_by_id_digest(digest)) {
00828       /* We think this relay is running, but it's not listed in our
00829        * routerlist. Somehow it fell out without telling us it went
00830        * down. Complain and also correct it. */
00831       log_info(LD_HIST,
00832                "Relay '%s' is listed as up in rephist, but it's not in "
00833                "our routerlist. Correcting.", dbuf);
00834       rep_hist_note_router_unreachable(digest, now);
00835     }
00836 
00837     PRINTF((f, "R %s\n", dbuf));
00838     if (hist->start_of_run > 0) {
00839       format_iso_time(time_buf, hist->start_of_run);
00840       t = time_buf;
00841     }
00842     PRINTF((f, "+MTBF %lu %.5f%s%s\n",
00843             hist->weighted_run_length, hist->total_run_weights,
00844             t ? " S=" : "", t ? t : ""));
00845     t = NULL;
00846     if (hist->start_of_downtime > 0) {
00847       format_iso_time(time_buf, hist->start_of_downtime);
00848       t = time_buf;
00849     }
00850     PRINTF((f, "+WFU %lu %lu%s%s\n",
00851             hist->weighted_uptime, hist->total_weighted_time,
00852             t ? " S=" : "", t ? t : ""));
00853   }
00854 
00855   PUT(".\n");
00856 
00857 #undef PUT
00858 #undef PRINTF
00859 
00860   return finish_writing_to_file(open_file);
00861  err:
00862   abort_writing_to_file(open_file);
00863   return -1;
00864 }
00865 
00868 static char *
00869 rep_hist_format_router_status(or_history_t *hist, time_t now)
00870 {
00871   char sor_buf[ISO_TIME_LEN+1];
00872   char sod_buf[ISO_TIME_LEN+1];
00873   double wfu;
00874   double mtbf;
00875   int up = 0, down = 0;
00876   char *cp = NULL;
00877 
00878   if (hist->start_of_run) {
00879     format_iso_time(sor_buf, hist->start_of_run);
00880     up = 1;
00881   }
00882   if (hist->start_of_downtime) {
00883     format_iso_time(sod_buf, hist->start_of_downtime);
00884     down = 1;
00885   }
00886 
00887   wfu = get_weighted_fractional_uptime(hist, now);
00888   mtbf = get_stability(hist, now);
00889   tor_asprintf(&cp,
00890                "%s%s%s"
00891                "%s%s%s"
00892                "wfu %0.3f\n"
00893                " weighted-time %lu\n"
00894                " weighted-uptime %lu\n"
00895                "mtbf %0.1f\n"
00896                " weighted-run-length %lu\n"
00897                " total-run-weights %f\n",
00898                up?"uptime-started ":"", up?sor_buf:"", up?" UTC\n":"",
00899                down?"downtime-started ":"", down?sod_buf:"", down?" UTC\n":"",
00900                wfu,
00901                hist->total_weighted_time,
00902                hist->weighted_uptime,
00903                mtbf,
00904                hist->weighted_run_length,
00905                hist->total_run_weights
00906                );
00907   return cp;
00908 }
00909 
00912 static char *last_stability_doc = NULL;
00915 static time_t built_last_stability_doc_at = 0;
00917 #define MAX_STABILITY_DOC_BUILD_RATE (3*60)
00918 
00921 const char *
00922 rep_hist_get_router_stability_doc(time_t now)
00923 {
00924   char *result;
00925   smartlist_t *chunks;
00926   if (built_last_stability_doc_at + MAX_STABILITY_DOC_BUILD_RATE > now)
00927     return last_stability_doc;
00928 
00929   if (!history_map)
00930     return NULL;
00931 
00932   tor_free(last_stability_doc);
00933   chunks = smartlist_new();
00934 
00935   if (rep_hist_have_measured_enough_stability()) {
00936     smartlist_add(chunks, tor_strdup("we-have-enough-measurements\n"));
00937   } else {
00938     smartlist_add(chunks, tor_strdup("we-do-not-have-enough-measurements\n"));
00939   }
00940 
00941   DIGESTMAP_FOREACH(history_map, id, or_history_t *, hist) {
00942     const node_t *node;
00943     char dbuf[BASE64_DIGEST_LEN+1];
00944     char *info;
00945     digest_to_base64(dbuf, id);
00946     node = node_get_by_id(id);
00947     if (node) {
00948       char ip[INET_NTOA_BUF_LEN+1];
00949       char tbuf[ISO_TIME_LEN+1];
00950       time_t published = node_get_published_on(node);
00951       node_get_address_string(node,ip,sizeof(ip));
00952       if (published > 0)
00953         format_iso_time(tbuf, published);
00954       else
00955         strlcpy(tbuf, "???", sizeof(tbuf));
00956       smartlist_add_asprintf(chunks,
00957                    "router %s %s %s\n"
00958                    "published %s\n"
00959                    "relevant-flags %s%s%s\n"
00960                    "declared-uptime %ld\n",
00961                    dbuf, node_get_nickname(node), ip,
00962                    tbuf,
00963                    node->is_running ? "Running " : "",
00964                    node->is_valid ? "Valid " : "",
00965                    node->ri && node->ri->is_hibernating ? "Hibernating " : "",
00966                    node_get_declared_uptime(node));
00967     } else {
00968       smartlist_add_asprintf(chunks,
00969                    "router %s {no descriptor}\n", dbuf);
00970     }
00971     info = rep_hist_format_router_status(hist, now);
00972     if (info)
00973       smartlist_add(chunks, info);
00974 
00975   } DIGESTMAP_FOREACH_END;
00976 
00977   result = smartlist_join_strings(chunks, "", 0, NULL);
00978   SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
00979   smartlist_free(chunks);
00980 
00981   last_stability_doc = result;
00982   built_last_stability_doc_at = time(NULL);
00983   return result;
00984 }
00985 
00989 static int
00990 find_next_with(smartlist_t *sl, int i, const char *prefix)
00991 {
00992   for ( ; i < smartlist_len(sl); ++i) {
00993     const char *line = smartlist_get(sl, i);
00994     if (!strcmpstart(line, prefix))
00995       return i;
00996     if (!strcmpstart(line, "R "))
00997       return -1;
00998   }
00999   return -1;
01000 }
01001 
01003 static int n_bogus_times = 0;
01006 static int
01007 parse_possibly_bad_iso_time(const char *s, time_t *time_out)
01008 {
01009   int year;
01010   char b[5];
01011   strlcpy(b, s, sizeof(b));
01012   b[4] = '\0';
01013   year = (int)tor_parse_long(b, 10, 0, INT_MAX, NULL, NULL);
01014   if (year < 1970) {
01015     *time_out = 0;
01016     ++n_bogus_times;
01017     return 0;
01018   } else
01019     return parse_iso_time(s, time_out);
01020 }
01021 
01027 static INLINE time_t
01028 correct_time(time_t t, time_t now, time_t stored_at, time_t started_measuring)
01029 {
01030   if (t < started_measuring - 24*60*60*365)
01031     return 0;
01032   else if (t < started_measuring)
01033     return started_measuring;
01034   else if (t > stored_at)
01035     return 0;
01036   else {
01037     long run_length = stored_at - t;
01038     t = now - run_length;
01039     if (t < started_measuring)
01040       t = started_measuring;
01041     return t;
01042   }
01043 }
01044 
01047 int
01048 rep_hist_load_mtbf_data(time_t now)
01049 {
01050   /* XXXX won't handle being called while history is already populated. */
01051   smartlist_t *lines;
01052   const char *line = NULL;
01053   int r=0, i;
01054   time_t last_downrated = 0, stored_at = 0, tracked_since = 0;
01055   time_t latest_possible_start = now;
01056   long format = -1;
01057 
01058   {
01059     char *filename = get_datadir_fname("router-stability");
01060     char *d = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
01061     tor_free(filename);
01062     if (!d)
01063       return -1;
01064     lines = smartlist_new();
01065     smartlist_split_string(lines, d, "\n", SPLIT_SKIP_SPACE, 0);
01066     tor_free(d);
01067   }
01068 
01069   {
01070     const char *firstline;
01071     if (smartlist_len(lines)>4) {
01072       firstline = smartlist_get(lines, 0);
01073       if (!strcmpstart(firstline, "format "))
01074         format = tor_parse_long(firstline+strlen("format "),
01075                                 10, -1, LONG_MAX, NULL, NULL);
01076     }
01077   }
01078   if (format != 1 && format != 2) {
01079     log_warn(LD_HIST,
01080              "Unrecognized format in mtbf history file. Skipping.");
01081     goto err;
01082   }
01083   for (i = 1; i < smartlist_len(lines); ++i) {
01084     line = smartlist_get(lines, i);
01085     if (!strcmp(line, "data"))
01086       break;
01087     if (!strcmpstart(line, "last-downrated ")) {
01088       if (parse_iso_time(line+strlen("last-downrated "), &last_downrated)<0)
01089         log_warn(LD_HIST,"Couldn't parse downrate time in mtbf "
01090                  "history file.");
01091     }
01092     if (!strcmpstart(line, "stored-at ")) {
01093       if (parse_iso_time(line+strlen("stored-at "), &stored_at)<0)
01094         log_warn(LD_HIST,"Couldn't parse stored time in mtbf "
01095                  "history file.");
01096     }
01097     if (!strcmpstart(line, "tracked-since ")) {
01098       if (parse_iso_time(line+strlen("tracked-since "), &tracked_since)<0)
01099         log_warn(LD_HIST,"Couldn't parse started-tracking time in mtbf "
01100                  "history file.");
01101     }
01102   }
01103   if (last_downrated > now)
01104     last_downrated = now;
01105   if (tracked_since > now)
01106     tracked_since = now;
01107 
01108   if (!stored_at) {
01109     log_warn(LD_HIST, "No stored time recorded.");
01110     goto err;
01111   }
01112 
01113   if (line && !strcmp(line, "data"))
01114     ++i;
01115 
01116   n_bogus_times = 0;
01117 
01118   for (; i < smartlist_len(lines); ++i) {
01119     char digest[DIGEST_LEN];
01120     char hexbuf[HEX_DIGEST_LEN+1];
01121     char mtbf_timebuf[ISO_TIME_LEN+1];
01122     char wfu_timebuf[ISO_TIME_LEN+1];
01123     time_t start_of_run = 0;
01124     time_t start_of_downtime = 0;
01125     int have_mtbf = 0, have_wfu = 0;
01126     long wrl = 0;
01127     double trw = 0;
01128     long wt_uptime = 0, total_wt_time = 0;
01129     int n;
01130     or_history_t *hist;
01131     line = smartlist_get(lines, i);
01132     if (!strcmp(line, "."))
01133       break;
01134 
01135     mtbf_timebuf[0] = '\0';
01136     wfu_timebuf[0] = '\0';
01137 
01138     if (format == 1) {
01139       n = sscanf(line, "%40s %ld %lf S=%10s %8s",
01140                  hexbuf, &wrl, &trw, mtbf_timebuf, mtbf_timebuf+11);
01141       if (n != 3 && n != 5) {
01142         log_warn(LD_HIST, "Couldn't scan line %s", escaped(line));
01143         continue;
01144       }
01145       have_mtbf = 1;
01146     } else {
01147       // format == 2.
01148       int mtbf_idx, wfu_idx;
01149       if (strcmpstart(line, "R ") || strlen(line) < 2+HEX_DIGEST_LEN)
01150         continue;
01151       strlcpy(hexbuf, line+2, sizeof(hexbuf));
01152       mtbf_idx = find_next_with(lines, i+1, "+MTBF ");
01153       wfu_idx = find_next_with(lines, i+1, "+WFU ");
01154       if (mtbf_idx >= 0) {
01155         const char *mtbfline = smartlist_get(lines, mtbf_idx);
01156         n = sscanf(mtbfline, "+MTBF %lu %lf S=%10s %8s",
01157                    &wrl, &trw, mtbf_timebuf, mtbf_timebuf+11);
01158         if (n == 2 || n == 4) {
01159           have_mtbf = 1;
01160         } else {
01161           log_warn(LD_HIST, "Couldn't scan +MTBF line %s",
01162                    escaped(mtbfline));
01163         }
01164       }
01165       if (wfu_idx >= 0) {
01166         const char *wfuline = smartlist_get(lines, wfu_idx);
01167         n = sscanf(wfuline, "+WFU %lu %lu S=%10s %8s",
01168                    &wt_uptime, &total_wt_time,
01169                    wfu_timebuf, wfu_timebuf+11);
01170         if (n == 2 || n == 4) {
01171           have_wfu = 1;
01172         } else {
01173           log_warn(LD_HIST, "Couldn't scan +WFU line %s", escaped(wfuline));
01174         }
01175       }
01176       if (wfu_idx > i)
01177         i = wfu_idx;
01178       if (mtbf_idx > i)
01179         i = mtbf_idx;
01180     }
01181     if (base16_decode(digest, DIGEST_LEN, hexbuf, HEX_DIGEST_LEN) < 0) {
01182       log_warn(LD_HIST, "Couldn't hex string %s", escaped(hexbuf));
01183       continue;
01184     }
01185     hist = get_or_history(digest);
01186     if (!hist)
01187       continue;
01188 
01189     if (have_mtbf) {
01190       if (mtbf_timebuf[0]) {
01191         mtbf_timebuf[10] = ' ';
01192         if (parse_possibly_bad_iso_time(mtbf_timebuf, &start_of_run)<0)
01193           log_warn(LD_HIST, "Couldn't parse time %s",
01194                    escaped(mtbf_timebuf));
01195       }
01196       hist->start_of_run = correct_time(start_of_run, now, stored_at,
01197                                         tracked_since);
01198       if (hist->start_of_run < latest_possible_start + wrl)
01199         latest_possible_start = hist->start_of_run - wrl;
01200 
01201       hist->weighted_run_length = wrl;
01202       hist->total_run_weights = trw;
01203     }
01204     if (have_wfu) {
01205       if (wfu_timebuf[0]) {
01206         wfu_timebuf[10] = ' ';
01207         if (parse_possibly_bad_iso_time(wfu_timebuf, &start_of_downtime)<0)
01208           log_warn(LD_HIST, "Couldn't parse time %s", escaped(wfu_timebuf));
01209       }
01210     }
01211     hist->start_of_downtime = correct_time(start_of_downtime, now, stored_at,
01212                                            tracked_since);
01213     hist->weighted_uptime = wt_uptime;
01214     hist->total_weighted_time = total_wt_time;
01215   }
01216   if (strcmp(line, "."))
01217     log_warn(LD_HIST, "Truncated MTBF file.");
01218 
01219   if (tracked_since < 86400*365) /* Recover from insanely early value. */
01220     tracked_since = latest_possible_start;
01221 
01222   stability_last_downrated = last_downrated;
01223   started_tracking_stability = tracked_since;
01224 
01225   goto done;
01226  err:
01227   r = -1;
01228  done:
01229   SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
01230   smartlist_free(lines);
01231   return r;
01232 }
01233 
01236 #define NUM_SECS_ROLLING_MEASURE 10
01237 
01238 /* XXXX Watch out! Before Tor 0.2.2.21-alpha, using any other value here would
01239  * generate an unparseable state file. */
01240 #define NUM_SECS_BW_SUM_INTERVAL (15*60)
01241 
01242 #define NUM_SECS_BW_SUM_IS_VALID (24*60*60)
01243 
01244 #define NUM_TOTALS (NUM_SECS_BW_SUM_IS_VALID/NUM_SECS_BW_SUM_INTERVAL)
01245 
01249 typedef struct bw_array_t {
01252   uint64_t obs[NUM_SECS_ROLLING_MEASURE];
01253   int cur_obs_idx; 
01254   time_t cur_obs_time; 
01255   uint64_t total_obs; 
01257   uint64_t max_total; 
01259   uint64_t total_in_period; 
01263   time_t next_period;
01266   int next_max_idx;
01268   int num_maxes_set;
01272   uint64_t maxima[NUM_TOTALS];
01275   uint64_t totals[NUM_TOTALS];
01276 } bw_array_t;
01277 
01279 static void
01280 commit_max(bw_array_t *b)
01281 {
01282   /* Store total from current period. */
01283   b->totals[b->next_max_idx] = b->total_in_period;
01284   /* Store maximum from current period. */
01285   b->maxima[b->next_max_idx++] = b->max_total;
01286   /* Advance next_period and next_max_idx */
01287   b->next_period += NUM_SECS_BW_SUM_INTERVAL;
01288   if (b->next_max_idx == NUM_TOTALS)
01289     b->next_max_idx = 0;
01290   if (b->num_maxes_set < NUM_TOTALS)
01291     ++b->num_maxes_set;
01292   /* Reset max_total. */
01293   b->max_total = 0;
01294   /* Reset total_in_period. */
01295   b->total_in_period = 0;
01296 }
01297 
01299 static INLINE void
01300 advance_obs(bw_array_t *b)
01301 {
01302   int nextidx;
01303   uint64_t total;
01304 
01305   /* Calculate the total bandwidth for the last NUM_SECS_ROLLING_MEASURE
01306    * seconds; adjust max_total as needed.*/
01307   total = b->total_obs + b->obs[b->cur_obs_idx];
01308   if (total > b->max_total)
01309     b->max_total = total;
01310 
01311   nextidx = b->cur_obs_idx+1;
01312   if (nextidx == NUM_SECS_ROLLING_MEASURE)
01313     nextidx = 0;
01314 
01315   b->total_obs = total - b->obs[nextidx];
01316   b->obs[nextidx]=0;
01317   b->cur_obs_idx = nextidx;
01318 
01319   if (++b->cur_obs_time >= b->next_period)
01320     commit_max(b);
01321 }
01322 
01325 static INLINE void
01326 add_obs(bw_array_t *b, time_t when, uint64_t n)
01327 {
01328   if (when < b->cur_obs_time)
01329     return; /* Don't record data in the past. */
01330 
01331   /* If we're currently adding observations for an earlier second than
01332    * 'when', advance b->cur_obs_time and b->cur_obs_idx by an
01333    * appropriate number of seconds, and do all the other housekeeping. */
01334   while (when > b->cur_obs_time) {
01335     /* Doing this one second at a time is potentially inefficient, if we start
01336        with a state file that is very old.  Fortunately, it doesn't seem to
01337        show up in profiles, so we can just ignore it for now. */
01338     advance_obs(b);
01339   }
01340 
01341   b->obs[b->cur_obs_idx] += n;
01342   b->total_in_period += n;
01343 }
01344 
01346 static bw_array_t *
01347 bw_array_new(void)
01348 {
01349   bw_array_t *b;
01350   time_t start;
01351   b = tor_malloc_zero(sizeof(bw_array_t));
01352   rephist_total_alloc += sizeof(bw_array_t);
01353   start = time(NULL);
01354   b->cur_obs_time = start;
01355   b->next_period = start + NUM_SECS_BW_SUM_INTERVAL;
01356   return b;
01357 }
01358 
01360 static bw_array_t *read_array = NULL;
01362 static bw_array_t *write_array = NULL;
01365 static bw_array_t *dir_read_array = NULL;
01368 static bw_array_t *dir_write_array = NULL;
01369 
01372 static void
01373 bw_arrays_init(void)
01374 {
01375   tor_free(read_array);
01376   tor_free(write_array);
01377   tor_free(dir_read_array);
01378   tor_free(dir_write_array);
01379   read_array = bw_array_new();
01380   write_array = bw_array_new();
01381   dir_read_array = bw_array_new();
01382   dir_write_array = bw_array_new();
01383 }
01384 
01392 void
01393 rep_hist_note_bytes_written(size_t num_bytes, time_t when)
01394 {
01395 /* Maybe a circular array for recent seconds, and step to a new point
01396  * every time a new second shows up. Or simpler is to just to have
01397  * a normal array and push down each item every second; it's short.
01398  */
01399 /* When a new second has rolled over, compute the sum of the bytes we've
01400  * seen over when-1 to when-1-NUM_SECS_ROLLING_MEASURE, and stick it
01401  * somewhere. See rep_hist_bandwidth_assess() below.
01402  */
01403   add_obs(write_array, when, num_bytes);
01404 }
01405 
01409 void
01410 rep_hist_note_bytes_read(size_t num_bytes, time_t when)
01411 {
01412 /* if we're smart, we can make this func and the one above share code */
01413   add_obs(read_array, when, num_bytes);
01414 }
01415 
01419 void
01420 rep_hist_note_dir_bytes_written(size_t num_bytes, time_t when)
01421 {
01422   add_obs(dir_write_array, when, num_bytes);
01423 }
01424 
01428 void
01429 rep_hist_note_dir_bytes_read(size_t num_bytes, time_t when)
01430 {
01431   add_obs(dir_read_array, when, num_bytes);
01432 }
01433 
01438 static uint64_t
01439 find_largest_max(bw_array_t *b)
01440 {
01441   int i;
01442   uint64_t max;
01443   max=0;
01444   for (i=0; i<NUM_TOTALS; ++i) {
01445     if (b->maxima[i]>max)
01446       max = b->maxima[i];
01447   }
01448   return max;
01449 }
01450 
01457 int
01458 rep_hist_bandwidth_assess(void)
01459 {
01460   uint64_t w,r;
01461   r = find_largest_max(read_array);
01462   w = find_largest_max(write_array);
01463   if (r>w)
01464     return (int)(U64_TO_DBL(w)/NUM_SECS_ROLLING_MEASURE);
01465   else
01466     return (int)(U64_TO_DBL(r)/NUM_SECS_ROLLING_MEASURE);
01467 }
01468 
01475 static size_t
01476 rep_hist_fill_bandwidth_history(char *buf, size_t len, const bw_array_t *b)
01477 {
01478   char *cp = buf;
01479   int i, n;
01480   const or_options_t *options = get_options();
01481   uint64_t cutoff;
01482 
01483   if (b->num_maxes_set <= b->next_max_idx) {
01484     /* We haven't been through the circular array yet; time starts at i=0.*/
01485     i = 0;
01486   } else {
01487     /* We've been around the array at least once.  The next i to be
01488        overwritten is the oldest. */
01489     i = b->next_max_idx;
01490   }
01491 
01492   if (options->RelayBandwidthRate) {
01493     /* We don't want to report that we used more bandwidth than the max we're
01494      * willing to relay; otherwise everybody will know how much traffic
01495      * we used ourself. */
01496     cutoff = options->RelayBandwidthRate * NUM_SECS_BW_SUM_INTERVAL;
01497   } else {
01498     cutoff = UINT64_MAX;
01499   }
01500 
01501   for (n=0; n<b->num_maxes_set; ++n,++i) {
01502     uint64_t total;
01503     if (i >= NUM_TOTALS)
01504       i -= NUM_TOTALS;
01505     tor_assert(i < NUM_TOTALS);
01506     /* Round the bandwidth used down to the nearest 1k. */
01507     total = b->totals[i] & ~0x3ff;
01508     if (total > cutoff)
01509       total = cutoff;
01510 
01511     if (n==(b->num_maxes_set-1))
01512       tor_snprintf(cp, len-(cp-buf), U64_FORMAT, U64_PRINTF_ARG(total));
01513     else
01514       tor_snprintf(cp, len-(cp-buf), U64_FORMAT",", U64_PRINTF_ARG(total));
01515     cp += strlen(cp);
01516   }
01517   return cp-buf;
01518 }
01519 
01524 char *
01525 rep_hist_get_bandwidth_lines(void)
01526 {
01527   char *buf, *cp;
01528   char t[ISO_TIME_LEN+1];
01529   int r;
01530   bw_array_t *b = NULL;
01531   const char *desc = NULL;
01532   size_t len;
01533 
01534   /* opt [dirreq-](read|write)-history yyyy-mm-dd HH:MM:SS (n s) n,n,n... */
01535 /* The n,n,n part above. Largest representation of a uint64_t is 20 chars
01536  * long, plus the comma. */
01537 #define MAX_HIST_VALUE_LEN 21*NUM_TOTALS
01538   len = (67+MAX_HIST_VALUE_LEN)*4;
01539   buf = tor_malloc_zero(len);
01540   cp = buf;
01541   for (r=0;r<4;++r) {
01542     char tmp[MAX_HIST_VALUE_LEN];
01543     size_t slen;
01544     switch (r) {
01545       case 0:
01546         b = write_array;
01547         desc = "write-history";
01548         break;
01549       case 1:
01550         b = read_array;
01551         desc = "read-history";
01552         break;
01553       case 2:
01554         b = dir_write_array;
01555         desc = "dirreq-write-history";
01556         break;
01557       case 3:
01558         b = dir_read_array;
01559         desc = "dirreq-read-history";
01560         break;
01561     }
01562     tor_assert(b);
01563     slen = rep_hist_fill_bandwidth_history(tmp, MAX_HIST_VALUE_LEN, b);
01564     /* If we don't have anything to write, skip to the next entry. */
01565     if (slen == 0)
01566       continue;
01567     format_iso_time(t, b->next_period-NUM_SECS_BW_SUM_INTERVAL);
01568     tor_snprintf(cp, len-(cp-buf), "%s %s (%d s) ",
01569                  desc, t, NUM_SECS_BW_SUM_INTERVAL);
01570     cp += strlen(cp);
01571     strlcat(cp, tmp, len-(cp-buf));
01572     cp += slen;
01573     strlcat(cp, "\n", len-(cp-buf));
01574     ++cp;
01575   }
01576   return buf;
01577 }
01578 
01581 static void
01582 rep_hist_update_bwhist_state_section(or_state_t *state,
01583                                      const bw_array_t *b,
01584                                      smartlist_t **s_values,
01585                                      smartlist_t **s_maxima,
01586                                      time_t *s_begins,
01587                                      int *s_interval)
01588 {
01589   int i,j;
01590   uint64_t maxval;
01591 
01592   if (*s_values) {
01593     SMARTLIST_FOREACH(*s_values, char *, val, tor_free(val));
01594     smartlist_free(*s_values);
01595   }
01596   if (*s_maxima) {
01597     SMARTLIST_FOREACH(*s_maxima, char *, val, tor_free(val));
01598     smartlist_free(*s_maxima);
01599   }
01600   if (! server_mode(get_options())) {
01601     /* Clients don't need to store bandwidth history persistently;
01602      * force these values to the defaults. */
01603     /* FFFF we should pull the default out of config.c's state table,
01604      * so we don't have two defaults. */
01605     if (*s_begins != 0 || *s_interval != 900) {
01606       time_t now = time(NULL);
01607       time_t save_at = get_options()->AvoidDiskWrites ? now+3600 : now+600;
01608       or_state_mark_dirty(state, save_at);
01609     }
01610     *s_begins = 0;
01611     *s_interval = 900;
01612     *s_values = smartlist_new();
01613     *s_maxima = smartlist_new();
01614     return;
01615   }
01616   *s_begins = b->next_period;
01617   *s_interval = NUM_SECS_BW_SUM_INTERVAL;
01618 
01619   *s_values = smartlist_new();
01620   *s_maxima = smartlist_new();
01621   /* Set i to first position in circular array */
01622   i = (b->num_maxes_set <= b->next_max_idx) ? 0 : b->next_max_idx;
01623   for (j=0; j < b->num_maxes_set; ++j,++i) {
01624     if (i >= NUM_TOTALS)
01625       i = 0;
01626     smartlist_add_asprintf(*s_values, U64_FORMAT,
01627                            U64_PRINTF_ARG(b->totals[i] & ~0x3ff));
01628     maxval = b->maxima[i] / NUM_SECS_ROLLING_MEASURE;
01629     smartlist_add_asprintf(*s_maxima, U64_FORMAT,
01630                            U64_PRINTF_ARG(maxval & ~0x3ff));
01631   }
01632   smartlist_add_asprintf(*s_values, U64_FORMAT,
01633                          U64_PRINTF_ARG(b->total_in_period & ~0x3ff));
01634   maxval = b->max_total / NUM_SECS_ROLLING_MEASURE;
01635   smartlist_add_asprintf(*s_maxima, U64_FORMAT,
01636                          U64_PRINTF_ARG(maxval & ~0x3ff));
01637 }
01638 
01641 void
01642 rep_hist_update_state(or_state_t *state)
01643 {
01644 #define UPDATE(arrname,st) \
01645   rep_hist_update_bwhist_state_section(state,\
01646                                        (arrname),\
01647                                        &state->BWHistory ## st ## Values, \
01648                                        &state->BWHistory ## st ## Maxima, \
01649                                        &state->BWHistory ## st ## Ends, \
01650                                        &state->BWHistory ## st ## Interval)
01651 
01652   UPDATE(write_array, Write);
01653   UPDATE(read_array, Read);
01654   UPDATE(dir_write_array, DirWrite);
01655   UPDATE(dir_read_array, DirRead);
01656 
01657   if (server_mode(get_options())) {
01658     or_state_mark_dirty(state, time(NULL)+(2*3600));
01659   }
01660 #undef UPDATE
01661 }
01662 
01665 static int
01666 rep_hist_load_bwhist_state_section(bw_array_t *b,
01667                                    const smartlist_t *s_values,
01668                                    const smartlist_t *s_maxima,
01669                                    const time_t s_begins,
01670                                    const int s_interval)
01671 {
01672   time_t now = time(NULL);
01673   int retval = 0;
01674   time_t start;
01675 
01676   uint64_t v, mv;
01677   int i,ok,ok_m;
01678   int have_maxima = (smartlist_len(s_values) == smartlist_len(s_maxima));
01679 
01680   if (s_values && s_begins >= now - NUM_SECS_BW_SUM_INTERVAL*NUM_TOTALS) {
01681     start = s_begins - s_interval*(smartlist_len(s_values));
01682     if (start > now)
01683       return 0;
01684     b->cur_obs_time = start;
01685     b->next_period = start + NUM_SECS_BW_SUM_INTERVAL;
01686     SMARTLIST_FOREACH_BEGIN(s_values, const char *, cp) {
01687         const char *maxstr = NULL;
01688         v = tor_parse_uint64(cp, 10, 0, UINT64_MAX, &ok, NULL);
01689         if (have_maxima) {
01690           maxstr = smartlist_get(s_maxima, cp_sl_idx);
01691           mv = tor_parse_uint64(maxstr, 10, 0, UINT64_MAX, &ok_m, NULL);
01692           mv *= NUM_SECS_ROLLING_MEASURE;
01693         } else {
01694           /* No maxima known; guess average rate to be conservative. */
01695           mv = (v / s_interval) * NUM_SECS_ROLLING_MEASURE;
01696         }
01697         if (!ok) {
01698           retval = -1;
01699           log_notice(LD_HIST, "Could not parse value '%s' into a number.'",cp);
01700         }
01701         if (maxstr && !ok_m) {
01702           retval = -1;
01703           log_notice(LD_HIST, "Could not parse maximum '%s' into a number.'",
01704                      maxstr);
01705         }
01706 
01707         if (start < now) {
01708           time_t cur_start = start;
01709           time_t actual_interval_len = s_interval;
01710           uint64_t cur_val = 0;
01711           /* Calculate the average per second. This is the best we can do
01712            * because our state file doesn't have per-second resolution. */
01713           if (start + s_interval > now)
01714             actual_interval_len = now - start;
01715           cur_val = v / actual_interval_len;
01716           /* This is potentially inefficient, but since we don't do it very
01717            * often it should be ok. */
01718           while (cur_start < start + actual_interval_len) {
01719             add_obs(b, cur_start, cur_val);
01720             ++cur_start;
01721           }
01722           b->max_total = mv;
01723           /* This will result in some fairly choppy history if s_interval
01724            * is not the same as NUM_SECS_BW_SUM_INTERVAL. XXXX */
01725           start += actual_interval_len;
01726         }
01727     } SMARTLIST_FOREACH_END(cp);
01728   }
01729 
01730   /* Clean up maxima and observed */
01731   for (i=0; i<NUM_SECS_ROLLING_MEASURE; ++i) {
01732     b->obs[i] = 0;
01733   }
01734   b->total_obs = 0;
01735 
01736   return retval;
01737 }
01738 
01740 int
01741 rep_hist_load_state(or_state_t *state, char **err)
01742 {
01743   int all_ok = 1;
01744 
01745   /* Assert they already have been malloced */
01746   tor_assert(read_array && write_array);
01747   tor_assert(dir_read_array && dir_write_array);
01748 
01749 #define LOAD(arrname,st)                                                \
01750   if (rep_hist_load_bwhist_state_section(                               \
01751                                 (arrname),                              \
01752                                 state->BWHistory ## st ## Values,       \
01753                                 state->BWHistory ## st ## Maxima,       \
01754                                 state->BWHistory ## st ## Ends,         \
01755                                 state->BWHistory ## st ## Interval)<0)  \
01756     all_ok = 0
01757 
01758   LOAD(write_array, Write);
01759   LOAD(read_array, Read);
01760   LOAD(dir_write_array, DirWrite);
01761   LOAD(dir_read_array, DirRead);
01762 
01763 #undef LOAD
01764   if (!all_ok) {
01765     *err = tor_strdup("Parsing of bandwidth history values failed");
01766     /* and create fresh arrays */
01767     bw_arrays_init();
01768     return -1;
01769   }
01770   return 0;
01771 }
01772 
01773 /*********************************************************************/
01774 
01778 typedef struct predicted_port_t {
01780   uint16_t port;
01782   time_t time;
01783 } predicted_port_t;
01784 
01786 static smartlist_t *predicted_ports_list=NULL;
01787 
01792 static void
01793 add_predicted_port(time_t now, uint16_t port)
01794 {
01795   predicted_port_t *pp = tor_malloc(sizeof(predicted_port_t));
01796   pp->port = port;
01797   pp->time = now;
01798   rephist_total_alloc += sizeof(*pp);
01799   smartlist_add(predicted_ports_list, pp);
01800 }
01801 
01806 static void
01807 predicted_ports_init(void)
01808 {
01809   predicted_ports_list = smartlist_new();
01810   add_predicted_port(time(NULL), 80); /* add one to kickstart us */
01811 }
01812 
01816 static void
01817 predicted_ports_free(void)
01818 {
01819   rephist_total_alloc -=
01820     smartlist_len(predicted_ports_list)*sizeof(predicted_port_t);
01821   SMARTLIST_FOREACH(predicted_ports_list, predicted_port_t *,
01822                     pp, tor_free(pp));
01823   smartlist_free(predicted_ports_list);
01824 }
01825 
01830 void
01831 rep_hist_note_used_port(time_t now, uint16_t port)
01832 {
01833   tor_assert(predicted_ports_list);
01834 
01835   if (!port) /* record nothing */
01836     return;
01837 
01838   SMARTLIST_FOREACH_BEGIN(predicted_ports_list, predicted_port_t *, pp) {
01839     if (pp->port == port) {
01840       pp->time = now;
01841       return;
01842     }
01843   } SMARTLIST_FOREACH_END(pp);
01844   /* it's not there yet; we need to add it */
01845   add_predicted_port(now, port);
01846 }
01847 
01850 #define PREDICTED_CIRCS_RELEVANCE_TIME (60*60)
01851 
01855 smartlist_t *
01856 rep_hist_get_predicted_ports(time_t now)
01857 {
01858   smartlist_t *out = smartlist_new();
01859   tor_assert(predicted_ports_list);
01860 
01861   /* clean out obsolete entries */
01862   SMARTLIST_FOREACH_BEGIN(predicted_ports_list, predicted_port_t *, pp) {
01863     if (pp->time + PREDICTED_CIRCS_RELEVANCE_TIME < now) {
01864       log_debug(LD_CIRC, "Expiring predicted port %d", pp->port);
01865 
01866       rephist_total_alloc -= sizeof(predicted_port_t);
01867       tor_free(pp);
01868       SMARTLIST_DEL_CURRENT(predicted_ports_list, pp);
01869     } else {
01870       smartlist_add(out, tor_memdup(&pp->port, sizeof(uint16_t)));
01871     }
01872   } SMARTLIST_FOREACH_END(pp);
01873   return out;
01874 }
01875 
01880 void
01881 rep_hist_remove_predicted_ports(const smartlist_t *rmv_ports)
01882 {
01883   /* Let's do this on O(N), not O(N^2). */
01884   bitarray_t *remove_ports = bitarray_init_zero(UINT16_MAX);
01885   SMARTLIST_FOREACH(rmv_ports, const uint16_t *, p,
01886                     bitarray_set(remove_ports, *p));
01887   SMARTLIST_FOREACH_BEGIN(predicted_ports_list, predicted_port_t *, pp) {
01888     if (bitarray_is_set(remove_ports, pp->port)) {
01889       tor_free(pp);
01890       SMARTLIST_DEL_CURRENT(predicted_ports_list, pp);
01891     }
01892   } SMARTLIST_FOREACH_END(pp);
01893   bitarray_free(remove_ports);
01894 }
01895 
01901 void
01902 rep_hist_note_used_resolve(time_t now)
01903 {
01904   rep_hist_note_used_port(now, 80);
01905 }
01906 
01908 static time_t predicted_internal_time = 0;
01910 static time_t predicted_internal_uptime_time = 0;
01912 static time_t predicted_internal_capacity_time = 0;
01913 
01915 void
01916 rep_hist_note_used_internal(time_t now, int need_uptime, int need_capacity)
01917 {
01918   predicted_internal_time = now;
01919   if (need_uptime)
01920     predicted_internal_uptime_time = now;
01921   if (need_capacity)
01922     predicted_internal_capacity_time = now;
01923 }
01924 
01926 int
01927 rep_hist_get_predicted_internal(time_t now, int *need_uptime,
01928                                 int *need_capacity)
01929 {
01930   if (!predicted_internal_time) { /* initialize it */
01931     predicted_internal_time = now;
01932     predicted_internal_uptime_time = now;
01933     predicted_internal_capacity_time = now;
01934   }
01935   if (predicted_internal_time + PREDICTED_CIRCS_RELEVANCE_TIME < now)
01936     return 0; /* too long ago */
01937   if (predicted_internal_uptime_time + PREDICTED_CIRCS_RELEVANCE_TIME >= now)
01938     *need_uptime = 1;
01939   // Always predict that we need capacity.
01940   *need_capacity = 1;
01941   return 1;
01942 }
01943 
01946 int
01947 any_predicted_circuits(time_t now)
01948 {
01949   return smartlist_len(predicted_ports_list) ||
01950          predicted_internal_time + PREDICTED_CIRCS_RELEVANCE_TIME >= now;
01951 }
01952 
01954 int
01955 rep_hist_circbuilding_dormant(time_t now)
01956 {
01957   if (any_predicted_circuits(now))
01958     return 0;
01959 
01960   /* see if we'll still need to build testing circuits */
01961   if (server_mode(get_options()) &&
01962       (!check_whether_orport_reachable() || !circuit_enough_testing_circs()))
01963     return 0;
01964   if (!check_whether_dirport_reachable())
01965     return 0;
01966 
01967   return 1;
01968 }
01969 
01971 static struct {
01973   unsigned long n_signed_dir_objs;
01975   unsigned long n_signed_routerdescs;
01977   unsigned long n_verified_dir_objs;
01979   unsigned long n_verified_routerdescs;
01981   unsigned long n_onionskins_encrypted;
01983   unsigned long n_onionskins_decrypted;
01985   unsigned long n_tls_client_handshakes;
01987   unsigned long n_tls_server_handshakes;
01989   unsigned long n_rend_client_ops;
01991   unsigned long n_rend_mid_ops;
01993   unsigned long n_rend_server_ops;
01994 } pk_op_counts = {0,0,0,0,0,0,0,0,0,0,0};
01995 
01997 void
01998 note_crypto_pk_op(pk_op_t operation)
01999 {
02000   switch (operation)
02001     {
02002     case SIGN_DIR:
02003       pk_op_counts.n_signed_dir_objs++;
02004       break;
02005     case SIGN_RTR:
02006       pk_op_counts.n_signed_routerdescs++;
02007       break;
02008     case VERIFY_DIR:
02009       pk_op_counts.n_verified_dir_objs++;
02010       break;
02011     case VERIFY_RTR:
02012       pk_op_counts.n_verified_routerdescs++;
02013       break;
02014     case ENC_ONIONSKIN:
02015       pk_op_counts.n_onionskins_encrypted++;
02016       break;
02017     case DEC_ONIONSKIN:
02018       pk_op_counts.n_onionskins_decrypted++;
02019       break;
02020     case TLS_HANDSHAKE_C:
02021       pk_op_counts.n_tls_client_handshakes++;
02022       break;
02023     case TLS_HANDSHAKE_S:
02024       pk_op_counts.n_tls_server_handshakes++;
02025       break;
02026     case REND_CLIENT:
02027       pk_op_counts.n_rend_client_ops++;
02028       break;
02029     case REND_MID:
02030       pk_op_counts.n_rend_mid_ops++;
02031       break;
02032     case REND_SERVER:
02033       pk_op_counts.n_rend_server_ops++;
02034       break;
02035     default:
02036       log_warn(LD_BUG, "Unknown pk operation %d", operation);
02037   }
02038 }
02039 
02041 void
02042 dump_pk_ops(int severity)
02043 {
02044   log(severity, LD_HIST,
02045       "PK operations: %lu directory objects signed, "
02046       "%lu directory objects verified, "
02047       "%lu routerdescs signed, "
02048       "%lu routerdescs verified, "
02049       "%lu onionskins encrypted, "
02050       "%lu onionskins decrypted, "
02051       "%lu client-side TLS handshakes, "
02052       "%lu server-side TLS handshakes, "
02053       "%lu rendezvous client operations, "
02054       "%lu rendezvous middle operations, "
02055       "%lu rendezvous server operations.",
02056       pk_op_counts.n_signed_dir_objs,
02057       pk_op_counts.n_verified_dir_objs,
02058       pk_op_counts.n_signed_routerdescs,
02059       pk_op_counts.n_verified_routerdescs,
02060       pk_op_counts.n_onionskins_encrypted,
02061       pk_op_counts.n_onionskins_decrypted,
02062       pk_op_counts.n_tls_client_handshakes,
02063       pk_op_counts.n_tls_server_handshakes,
02064       pk_op_counts.n_rend_client_ops,
02065       pk_op_counts.n_rend_mid_ops,
02066       pk_op_counts.n_rend_server_ops);
02067 }
02068 
02069 /*** Exit port statistics ***/
02070 
02071 /* Some constants */
02073 #define EXIT_STATS_ROUND_UP_BYTES 1024
02074 
02075 #define EXIT_STATS_ROUND_UP_STREAMS 4
02076 
02077 #define EXIT_STATS_NUM_PORTS 65536
02078 
02079 #define EXIT_STATS_TOP_N_PORTS 10
02080 
02081 /* The following data structures are arrays and no fancy smartlists or maps,
02082  * so that all write operations can be done in constant time. This comes at
02083  * the price of some memory (1.25 MB) and linear complexity when writing
02084  * stats for measuring relays. */
02086 static uint64_t *exit_bytes_read = NULL;
02088 static uint64_t *exit_bytes_written = NULL;
02090 static uint32_t *exit_streams = NULL;
02091 
02093 static time_t start_of_exit_stats_interval;
02094 
02096 void
02097 rep_hist_exit_stats_init(time_t now)
02098 {
02099   start_of_exit_stats_interval = now;
02100   exit_bytes_read = tor_malloc_zero(EXIT_STATS_NUM_PORTS *
02101                                     sizeof(uint64_t));
02102   exit_bytes_written = tor_malloc_zero(EXIT_STATS_NUM_PORTS *
02103                                        sizeof(uint64_t));
02104   exit_streams = tor_malloc_zero(EXIT_STATS_NUM_PORTS *
02105                                  sizeof(uint32_t));
02106 }
02107 
02109 void
02110 rep_hist_reset_exit_stats(time_t now)
02111 {
02112   start_of_exit_stats_interval = now;
02113   memset(exit_bytes_read, 0, EXIT_STATS_NUM_PORTS * sizeof(uint64_t));
02114   memset(exit_bytes_written, 0, EXIT_STATS_NUM_PORTS * sizeof(uint64_t));
02115   memset(exit_streams, 0, EXIT_STATS_NUM_PORTS * sizeof(uint32_t));
02116 }
02117 
02120 void
02121 rep_hist_exit_stats_term(void)
02122 {
02123   start_of_exit_stats_interval = 0;
02124   tor_free(exit_bytes_read);
02125   tor_free(exit_bytes_written);
02126   tor_free(exit_streams);
02127 }
02128 
02132 static int
02133 _compare_int(const void *x, const void *y)
02134 {
02135   return (*(int*)x - *(int*)y);
02136 }
02137 
02141 char *
02142 rep_hist_format_exit_stats(time_t now)
02143 {
02144   int i, j, top_elements = 0, cur_min_idx = 0, cur_port;
02145   uint64_t top_bytes[EXIT_STATS_TOP_N_PORTS];
02146   int top_ports[EXIT_STATS_TOP_N_PORTS];
02147   uint64_t cur_bytes = 0, other_read = 0, other_written = 0,
02148            total_read = 0, total_written = 0;
02149   uint32_t total_streams = 0, other_streams = 0;
02150   smartlist_t *written_strings, *read_strings, *streams_strings;
02151   char *written_string, *read_string, *streams_string;
02152   char t[ISO_TIME_LEN+1];
02153   char *result;
02154 
02155   if (!start_of_exit_stats_interval)
02156     return NULL; /* Not initialized. */
02157 
02158   tor_assert(now >= start_of_exit_stats_interval);
02159 
02160   /* Go through all ports to find the n ports that saw most written and
02161    * read bytes.
02162    *
02163    * Invariant: at the end of the loop for iteration i,
02164    *    total_read is the sum of all exit_bytes_read[0..i]
02165    *    total_written is the sum of all exit_bytes_written[0..i]
02166    *    total_stream is the sum of all exit_streams[0..i]
02167    *
02168    *    top_elements = MAX(EXIT_STATS_TOP_N_PORTS,
02169    *                  #{j | 0 <= j <= i && volume(i) > 0})
02170    *
02171    *    For all 0 <= j < top_elements,
02172    *        top_bytes[j] > 0
02173    *        0 <= top_ports[j] <= 65535
02174    *        top_bytes[j] = volume(top_ports[j])
02175    *
02176    *    There is no j in 0..i and k in 0..top_elements such that:
02177    *        volume(j) > top_bytes[k] AND j is not in top_ports[0..top_elements]
02178    *
02179    *    There is no j!=cur_min_idx in 0..top_elements such that:
02180    *        top_bytes[j] < top_bytes[cur_min_idx]
02181    *
02182    * where volume(x) == exit_bytes_read[x]+exit_bytes_written[x]
02183    *
02184    * Worst case: O(EXIT_STATS_NUM_PORTS * EXIT_STATS_TOP_N_PORTS)
02185    */
02186   for (i = 1; i < EXIT_STATS_NUM_PORTS; i++) {
02187     total_read += exit_bytes_read[i];
02188     total_written += exit_bytes_written[i];
02189     total_streams += exit_streams[i];
02190     cur_bytes = exit_bytes_read[i] + exit_bytes_written[i];
02191     if (cur_bytes == 0) {
02192       continue;
02193     }
02194     if (top_elements < EXIT_STATS_TOP_N_PORTS) {
02195       top_bytes[top_elements] = cur_bytes;
02196       top_ports[top_elements++] = i;
02197     } else if (cur_bytes > top_bytes[cur_min_idx]) {
02198       top_bytes[cur_min_idx] = cur_bytes;
02199       top_ports[cur_min_idx] = i;
02200     } else {
02201       continue;
02202     }
02203     cur_min_idx = 0;
02204     for (j = 1; j < top_elements; j++) {
02205       if (top_bytes[j] < top_bytes[cur_min_idx]) {
02206         cur_min_idx = j;
02207       }
02208     }
02209   }
02210 
02211   /* Add observations of top ports to smartlists. */
02212   written_strings = smartlist_new();
02213   read_strings = smartlist_new();
02214   streams_strings = smartlist_new();
02215   other_read = total_read;
02216   other_written = total_written;
02217   other_streams = total_streams;
02218   /* Sort the ports; this puts them out of sync with top_bytes, but we
02219    * won't be using top_bytes again anyway */
02220   qsort(top_ports, top_elements, sizeof(int), _compare_int);
02221   for (j = 0; j < top_elements; j++) {
02222     cur_port = top_ports[j];
02223     if (exit_bytes_written[cur_port] > 0) {
02224       uint64_t num = round_uint64_to_next_multiple_of(
02225                      exit_bytes_written[cur_port],
02226                      EXIT_STATS_ROUND_UP_BYTES);
02227       num /= 1024;
02228       smartlist_add_asprintf(written_strings, "%d="U64_FORMAT,
02229                              cur_port, U64_PRINTF_ARG(num));
02230       other_written -= exit_bytes_written[cur_port];
02231     }
02232     if (exit_bytes_read[cur_port] > 0) {
02233       uint64_t num = round_uint64_to_next_multiple_of(
02234                      exit_bytes_read[cur_port],
02235                      EXIT_STATS_ROUND_UP_BYTES);
02236       num /= 1024;
02237       smartlist_add_asprintf(read_strings, "%d="U64_FORMAT,
02238                              cur_port, U64_PRINTF_ARG(num));
02239       other_read -= exit_bytes_read[cur_port];
02240     }
02241     if (exit_streams[cur_port] > 0) {
02242       uint32_t num = round_uint32_to_next_multiple_of(
02243                      exit_streams[cur_port],
02244                      EXIT_STATS_ROUND_UP_STREAMS);
02245       smartlist_add_asprintf(streams_strings, "%d=%u", cur_port, num);
02246       other_streams -= exit_streams[cur_port];
02247     }
02248   }
02249 
02250   /* Add observations of other ports in a single element. */
02251   other_written = round_uint64_to_next_multiple_of(other_written,
02252                   EXIT_STATS_ROUND_UP_BYTES);
02253   other_written /= 1024;
02254   smartlist_add_asprintf(written_strings, "other="U64_FORMAT,
02255                          U64_PRINTF_ARG(other_written));
02256   other_read = round_uint64_to_next_multiple_of(other_read,
02257                EXIT_STATS_ROUND_UP_BYTES);
02258   other_read /= 1024;
02259   smartlist_add_asprintf(read_strings, "other="U64_FORMAT,
02260                          U64_PRINTF_ARG(other_read));
02261   other_streams = round_uint32_to_next_multiple_of(other_streams,
02262                   EXIT_STATS_ROUND_UP_STREAMS);
02263   smartlist_add_asprintf(streams_strings, "other=%u", other_streams);
02264 
02265   /* Join all observations in single strings. */
02266   written_string = smartlist_join_strings(written_strings, ",", 0, NULL);
02267   read_string = smartlist_join_strings(read_strings, ",", 0, NULL);
02268   streams_string = smartlist_join_strings(streams_strings, ",", 0, NULL);
02269   SMARTLIST_FOREACH(written_strings, char *, cp, tor_free(cp));
02270   SMARTLIST_FOREACH(read_strings, char *, cp, tor_free(cp));
02271   SMARTLIST_FOREACH(streams_strings, char *, cp, tor_free(cp));
02272   smartlist_free(written_strings);
02273   smartlist_free(read_strings);
02274   smartlist_free(streams_strings);
02275 
02276   /* Put everything together. */
02277   format_iso_time(t, now);
02278   tor_asprintf(&result, "exit-stats-end %s (%d s)\n"
02279                "exit-kibibytes-written %s\n"
02280                "exit-kibibytes-read %s\n"
02281                "exit-streams-opened %s\n",
02282                t, (unsigned) (now - start_of_exit_stats_interval),
02283                written_string,
02284                read_string,
02285                streams_string);
02286   tor_free(written_string);
02287   tor_free(read_string);
02288   tor_free(streams_string);
02289   return result;
02290 }
02291 
02296 time_t
02297 rep_hist_exit_stats_write(time_t now)
02298 {
02299   char *statsdir = NULL, *filename = NULL, *str = NULL;
02300 
02301   if (!start_of_exit_stats_interval)
02302     return 0; /* Not initialized. */
02303   if (start_of_exit_stats_interval + WRITE_STATS_INTERVAL > now)
02304     goto done; /* Not ready to write. */
02305 
02306   log_info(LD_HIST, "Writing exit port statistics to disk.");
02307 
02308   /* Generate history string. */
02309   str = rep_hist_format_exit_stats(now);
02310 
02311   /* Reset counters. */
02312   rep_hist_reset_exit_stats(now);
02313 
02314   /* Try to write to disk. */
02315   statsdir = get_datadir_fname("stats");
02316   if (check_private_dir(statsdir, CPD_CREATE, get_options()->User) < 0) {
02317     log_warn(LD_HIST, "Unable to create stats/ directory!");
02318     goto done;
02319   }
02320   filename = get_datadir_fname2("stats", "exit-stats");
02321   if (write_str_to_file(filename, str, 0) < 0)
02322     log_warn(LD_HIST, "Unable to write exit port statistics to disk!");
02323 
02324  done:
02325   tor_free(str);
02326   tor_free(statsdir);
02327   tor_free(filename);
02328   return start_of_exit_stats_interval + WRITE_STATS_INTERVAL;
02329 }
02330 
02333 void
02334 rep_hist_note_exit_bytes(uint16_t port, size_t num_written,
02335                          size_t num_read)
02336 {
02337   if (!start_of_exit_stats_interval)
02338     return; /* Not initialized. */
02339   exit_bytes_written[port] += num_written;
02340   exit_bytes_read[port] += num_read;
02341   log_debug(LD_HIST, "Written %lu bytes and read %lu bytes to/from an "
02342             "exit connection to port %d.",
02343             (unsigned long)num_written, (unsigned long)num_read, port);
02344 }
02345 
02347 void
02348 rep_hist_note_exit_stream_opened(uint16_t port)
02349 {
02350   if (!start_of_exit_stats_interval)
02351     return; /* Not initialized. */
02352   exit_streams[port]++;
02353   log_debug(LD_HIST, "Opened exit stream to port %d", port);
02354 }
02355 
02356 /*** cell statistics ***/
02357 
02360 static time_t start_of_buffer_stats_interval;
02361 
02363 void
02364 rep_hist_buffer_stats_init(time_t now)
02365 {
02366   start_of_buffer_stats_interval = now;
02367 }
02368 
02371 typedef struct circ_buffer_stats_t {
02373   double mean_num_cells_in_queue;
02375   double mean_time_cells_in_queue;
02377   uint32_t processed_cells;
02378 } circ_buffer_stats_t;
02379 
02381 static smartlist_t *circuits_for_buffer_stats = NULL;
02382 
02386 void
02387 rep_hist_add_buffer_stats(double mean_num_cells_in_queue,
02388     double mean_time_cells_in_queue, uint32_t processed_cells)
02389 {
02390   circ_buffer_stats_t *stat;
02391   if (!start_of_buffer_stats_interval)
02392     return; /* Not initialized. */
02393   stat = tor_malloc_zero(sizeof(circ_buffer_stats_t));
02394   stat->mean_num_cells_in_queue = mean_num_cells_in_queue;
02395   stat->mean_time_cells_in_queue = mean_time_cells_in_queue;
02396   stat->processed_cells = processed_cells;
02397   if (!circuits_for_buffer_stats)
02398     circuits_for_buffer_stats = smartlist_new();
02399   smartlist_add(circuits_for_buffer_stats, stat);
02400 }
02401 
02405 void
02406 rep_hist_buffer_stats_add_circ(circuit_t *circ, time_t end_of_interval)
02407 {
02408   time_t start_of_interval;
02409   int interval_length;
02410   or_circuit_t *orcirc;
02411   double mean_num_cells_in_queue, mean_time_cells_in_queue;
02412   uint32_t processed_cells;
02413   if (CIRCUIT_IS_ORIGIN(circ))
02414     return;
02415   orcirc = TO_OR_CIRCUIT(circ);
02416   if (!orcirc->processed_cells)
02417     return;
02418   start_of_interval = (circ->timestamp_created.tv_sec >
02419                        start_of_buffer_stats_interval) ?
02420         circ->timestamp_created.tv_sec :
02421         start_of_buffer_stats_interval;
02422   interval_length = (int) (end_of_interval - start_of_interval);
02423   if (interval_length <= 0)
02424     return;
02425   processed_cells = orcirc->processed_cells;
02426   /* 1000.0 for s -> ms; 2.0 because of app-ward and exit-ward queues */
02427   mean_num_cells_in_queue = (double) orcirc->total_cell_waiting_time /
02428       (double) interval_length / 1000.0 / 2.0;
02429   mean_time_cells_in_queue =
02430       (double) orcirc->total_cell_waiting_time /
02431       (double) orcirc->processed_cells;
02432   orcirc->total_cell_waiting_time = 0;
02433   orcirc->processed_cells = 0;
02434   rep_hist_add_buffer_stats(mean_num_cells_in_queue,
02435                             mean_time_cells_in_queue,
02436                             processed_cells);
02437 }
02438 
02441 static int
02442 _buffer_stats_compare_entries(const void **_a, const void **_b)
02443 {
02444   const circ_buffer_stats_t *a = *_a, *b = *_b;
02445   if (a->processed_cells < b->processed_cells)
02446     return 1;
02447   else if (a->processed_cells > b->processed_cells)
02448     return -1;
02449   else
02450     return 0;
02451 }
02452 
02455 void
02456 rep_hist_buffer_stats_term(void)
02457 {
02458   rep_hist_reset_buffer_stats(0);
02459 }
02460 
02463 void
02464 rep_hist_reset_buffer_stats(time_t now)
02465 {
02466   if (!circuits_for_buffer_stats)
02467     circuits_for_buffer_stats = smartlist_new();
02468   SMARTLIST_FOREACH(circuits_for_buffer_stats, circ_buffer_stats_t *,
02469       stat, tor_free(stat));
02470   smartlist_clear(circuits_for_buffer_stats);
02471   start_of_buffer_stats_interval = now;
02472 }
02473 
02477 char *
02478 rep_hist_format_buffer_stats(time_t now)
02479 {
02480 #define SHARES 10
02481   uint64_t processed_cells[SHARES];
02482   uint32_t circs_in_share[SHARES];
02483   int number_of_circuits, i;
02484   double queued_cells[SHARES], time_in_queue[SHARES];
02485   smartlist_t *processed_cells_strings, *queued_cells_strings,
02486               *time_in_queue_strings;
02487   char *processed_cells_string, *queued_cells_string,
02488        *time_in_queue_string;
02489   char t[ISO_TIME_LEN+1];
02490   char *result;
02491 
02492   if (!start_of_buffer_stats_interval)
02493     return NULL; /* Not initialized. */
02494 
02495   tor_assert(now >= start_of_buffer_stats_interval);
02496 
02497   /* Calculate deciles if we saw at least one circuit. */
02498   memset(processed_cells, 0, SHARES * sizeof(uint64_t));
02499   memset(circs_in_share, 0, SHARES * sizeof(uint32_t));
02500   memset(queued_cells, 0, SHARES * sizeof(double));
02501   memset(time_in_queue, 0, SHARES * sizeof(double));
02502   if (!circuits_for_buffer_stats)
02503     circuits_for_buffer_stats = smartlist_new();
02504   number_of_circuits = smartlist_len(circuits_for_buffer_stats);
02505   if (number_of_circuits > 0) {
02506     smartlist_sort(circuits_for_buffer_stats,
02507                    _buffer_stats_compare_entries);
02508     i = 0;
02509     SMARTLIST_FOREACH_BEGIN(circuits_for_buffer_stats,
02510                             circ_buffer_stats_t *, stat)
02511     {
02512       int share = i++ * SHARES / number_of_circuits;
02513       processed_cells[share] += stat->processed_cells;
02514       queued_cells[share] += stat->mean_num_cells_in_queue;
02515       time_in_queue[share] += stat->mean_time_cells_in_queue;
02516       circs_in_share[share]++;
02517     }
02518     SMARTLIST_FOREACH_END(stat);
02519   }
02520 
02521   /* Write deciles to strings. */
02522   processed_cells_strings = smartlist_new();
02523   queued_cells_strings = smartlist_new();
02524   time_in_queue_strings = smartlist_new();
02525   for (i = 0; i < SHARES; i++) {
02526     smartlist_add_asprintf(processed_cells_strings,
02527                            U64_FORMAT, !circs_in_share[i] ? 0 :
02528                            U64_PRINTF_ARG(processed_cells[i] /
02529                            circs_in_share[i]));
02530   }
02531   for (i = 0; i < SHARES; i++) {
02532     smartlist_add_asprintf(queued_cells_strings, "%.2f",
02533                            circs_in_share[i] == 0 ? 0.0 :
02534                              queued_cells[i] / (double) circs_in_share[i]);
02535   }
02536   for (i = 0; i < SHARES; i++) {
02537     smartlist_add_asprintf(time_in_queue_strings, "%.0f",
02538                            circs_in_share[i] == 0 ? 0.0 :
02539                              time_in_queue[i] / (double) circs_in_share[i]);
02540   }
02541 
02542   /* Join all observations in single strings. */
02543   processed_cells_string = smartlist_join_strings(processed_cells_strings,
02544                                                   ",", 0, NULL);
02545   queued_cells_string = smartlist_join_strings(queued_cells_strings,
02546                                                ",", 0, NULL);
02547   time_in_queue_string = smartlist_join_strings(time_in_queue_strings,
02548                                                 ",", 0, NULL);
02549   SMARTLIST_FOREACH(processed_cells_strings, char *, cp, tor_free(cp));
02550   SMARTLIST_FOREACH(queued_cells_strings, char *, cp, tor_free(cp));
02551   SMARTLIST_FOREACH(time_in_queue_strings, char *, cp, tor_free(cp));
02552   smartlist_free(processed_cells_strings);
02553   smartlist_free(queued_cells_strings);
02554   smartlist_free(time_in_queue_strings);
02555 
02556   /* Put everything together. */
02557   format_iso_time(t, now);
02558   tor_asprintf(&result, "cell-stats-end %s (%d s)\n"
02559                "cell-processed-cells %s\n"
02560                "cell-queued-cells %s\n"
02561                "cell-time-in-queue %s\n"
02562                "cell-circuits-per-decile %d\n",
02563                t, (unsigned) (now - start_of_buffer_stats_interval),
02564                processed_cells_string,
02565                queued_cells_string,
02566                time_in_queue_string,
02567                (number_of_circuits + SHARES - 1) / SHARES);
02568   tor_free(processed_cells_string);
02569   tor_free(queued_cells_string);
02570   tor_free(time_in_queue_string);
02571   return result;
02572 #undef SHARES
02573 }
02574 
02580 time_t
02581 rep_hist_buffer_stats_write(time_t now)
02582 {
02583   circuit_t *circ;
02584   char *statsdir = NULL, *filename = NULL, *str = NULL;
02585 
02586   if (!start_of_buffer_stats_interval)
02587     return 0; /* Not initialized. */
02588   if (start_of_buffer_stats_interval + WRITE_STATS_INTERVAL > now)
02589     goto done; /* Not ready to write */
02590 
02591   /* Add open circuits to the history. */
02592   for (circ = _circuit_get_global_list(); circ; circ = circ->next) {
02593     rep_hist_buffer_stats_add_circ(circ, now);
02594   }
02595 
02596   /* Generate history string. */
02597   str = rep_hist_format_buffer_stats(now);
02598 
02599   /* Reset both buffer history and counters of open circuits. */
02600   rep_hist_reset_buffer_stats(now);
02601 
02602   /* Try to write to disk. */
02603   statsdir = get_datadir_fname("stats");
02604   if (check_private_dir(statsdir, CPD_CREATE, get_options()->User) < 0) {
02605     log_warn(LD_HIST, "Unable to create stats/ directory!");
02606     goto done;
02607   }
02608   filename = get_datadir_fname2("stats", "buffer-stats");
02609   if (write_str_to_file(filename, str, 0) < 0)
02610     log_warn(LD_HIST, "Unable to write buffer stats to disk!");
02611 
02612  done:
02613   tor_free(str);
02614   tor_free(filename);
02615   tor_free(statsdir);
02616   return start_of_buffer_stats_interval + WRITE_STATS_INTERVAL;
02617 }
02618 
02619 /*** Descriptor serving statistics ***/
02620 
02624 static digestmap_t *served_descs = NULL;
02625 
02628 static unsigned long total_descriptor_downloads;
02629 
02631 static time_t start_of_served_descs_stats_interval;
02632 
02634 void
02635 rep_hist_desc_stats_init(time_t now)
02636 {
02637   if (served_descs) {
02638     log_warn(LD_BUG, "Called rep_hist_desc_stats_init() when desc stats were "
02639              "already initialized. This is probably harmless.");
02640     return; // Already initialized
02641   }
02642   served_descs = digestmap_new();
02643   total_descriptor_downloads = 0;
02644   start_of_served_descs_stats_interval = now;
02645 }
02646 
02648 static void
02649 rep_hist_reset_desc_stats(time_t now)
02650 {
02651   rep_hist_desc_stats_term();
02652   rep_hist_desc_stats_init(now);
02653 }
02654 
02657 void
02658 rep_hist_desc_stats_term(void)
02659 {
02660   digestmap_free(served_descs, NULL);
02661   served_descs = NULL;
02662   start_of_served_descs_stats_interval = 0;
02663   total_descriptor_downloads = 0;
02664 }
02665 
02670 static char *
02671 rep_hist_format_desc_stats(time_t now)
02672 {
02673   char t[ISO_TIME_LEN+1];
02674   char *result;
02675 
02676   digestmap_iter_t *iter;
02677   const char *key;
02678   void *val;
02679   unsigned size;
02680   int *vals, max = 0, q3 = 0, md = 0, q1 = 0, min = 0;
02681   int n = 0;
02682 
02683   if (!start_of_served_descs_stats_interval)
02684     return NULL;
02685 
02686   size = digestmap_size(served_descs);
02687   if (size > 0) {
02688     vals = tor_malloc(size * sizeof(int));
02689     for (iter = digestmap_iter_init(served_descs);
02690          !digestmap_iter_done(iter);
02691          iter = digestmap_iter_next(served_descs, iter)) {
02692       uintptr_t count;
02693       digestmap_iter_get(iter, &key, &val);
02694       count = (uintptr_t)val;
02695       vals[n++] = (int)count;
02696       (void)key;
02697     }
02698     max = find_nth_int(vals, size, size-1);
02699     q3 = find_nth_int(vals, size, (3*size-1)/4);
02700     md = find_nth_int(vals, size, (size-1)/2);
02701     q1 = find_nth_int(vals, size, (size-1)/4);
02702     min = find_nth_int(vals, size, 0);
02703     tor_free(vals);
02704   }
02705 
02706   format_iso_time(t, now);
02707 
02708   tor_asprintf(&result,
02709                "served-descs-stats-end %s (%d s) total=%lu unique=%u "
02710                "max=%d q3=%d md=%d q1=%d min=%d\n",
02711                t,
02712                (unsigned) (now - start_of_served_descs_stats_interval),
02713                total_descriptor_downloads,
02714                size, max, q3, md, q1, min);
02715 
02716   return result;
02717 }
02718 
02724 time_t
02725 rep_hist_desc_stats_write(time_t now)
02726 {
02727   char *statsdir = NULL, *filename = NULL, *str = NULL;
02728 
02729   if (!start_of_served_descs_stats_interval)
02730     return 0; /* We're not collecting stats. */
02731   if (start_of_served_descs_stats_interval + WRITE_STATS_INTERVAL > now)
02732     return start_of_served_descs_stats_interval + WRITE_STATS_INTERVAL;
02733 
02734   str = rep_hist_format_desc_stats(now);
02735   tor_assert(str != NULL);
02736 
02737   statsdir = get_datadir_fname("stats");
02738   if (check_private_dir(statsdir, CPD_CREATE, get_options()->User) < 0) {
02739     log_warn(LD_HIST, "Unable to create stats/ directory!");
02740       goto done;
02741   }
02742   filename = get_datadir_fname2("stats", "served-desc-stats");
02743   if (append_bytes_to_file(filename, str, strlen(str), 0) < 0)
02744     log_warn(LD_HIST, "Unable to write served descs statistics to disk!");
02745 
02746   rep_hist_reset_desc_stats(now);
02747 
02748  done:
02749   tor_free(statsdir);
02750   tor_free(filename);
02751   tor_free(str);
02752   return start_of_served_descs_stats_interval + WRITE_STATS_INTERVAL;
02753 }
02754 
02755 /* DOCDOC rep_hist_note_desc_served */
02756 void
02757 rep_hist_note_desc_served(const char * desc)
02758 {
02759   void *val;
02760   uintptr_t count;
02761   if (!served_descs)
02762     return; // We're not collecting stats
02763   val = digestmap_get(served_descs, desc);
02764   count = (uintptr_t)val;
02765   if (count != INT_MAX)
02766     ++count;
02767   digestmap_set(served_descs, desc, (void*)count);
02768   total_descriptor_downloads++;
02769 }
02770 
02771 /*** Connection statistics ***/
02772 
02775 static time_t start_of_conn_stats_interval;
02776 
02778 void
02779 rep_hist_conn_stats_init(time_t now)
02780 {
02781   start_of_conn_stats_interval = now;
02782 }
02783 
02784 /* Count connections that we read and wrote less than these many bytes
02785  * from/to as below threshold. */
02786 #define BIDI_THRESHOLD 20480
02787 
02788 /* Count connections that we read or wrote at least this factor as many
02789  * bytes from/to than we wrote or read to/from as mostly reading or
02790  * writing. */
02791 #define BIDI_FACTOR 10
02792 
02793 /* Interval length in seconds for considering read and written bytes for
02794  * connection stats. */
02795 #define BIDI_INTERVAL 10
02796 
02798 static time_t bidi_next_interval = 0;
02799 
02802 static uint32_t below_threshold = 0;
02803 
02806 static uint32_t mostly_read = 0;
02807 
02810 static uint32_t mostly_written = 0;
02811 
02815 static uint32_t both_read_and_written = 0;
02816 
02819 typedef struct bidi_map_entry_t {
02820   HT_ENTRY(bidi_map_entry_t) node;
02821   uint64_t conn_id; 
02822   size_t read; 
02823   size_t written; 
02824 } bidi_map_entry_t;
02825 
02828 static HT_HEAD(bidimap, bidi_map_entry_t) bidi_map =
02829      HT_INITIALIZER();
02830 
02831 static int
02832 bidi_map_ent_eq(const bidi_map_entry_t *a, const bidi_map_entry_t *b)
02833 {
02834   return a->conn_id == b->conn_id;
02835 }
02836 
02837 /* DOCDOC bidi_map_ent_hash */
02838 static unsigned
02839 bidi_map_ent_hash(const bidi_map_entry_t *entry)
02840 {
02841   return (unsigned) entry->conn_id;
02842 }
02843 
02844 HT_PROTOTYPE(bidimap, bidi_map_entry_t, node, bidi_map_ent_hash,
02845              bidi_map_ent_eq);
02846 HT_GENERATE(bidimap, bidi_map_entry_t, node, bidi_map_ent_hash,
02847             bidi_map_ent_eq, 0.6, malloc, realloc, free);
02848 
02849 /* DOCDOC bidi_map_free */
02850 static void
02851 bidi_map_free(void)
02852 {
02853   bidi_map_entry_t **ptr, **next, *ent;
02854   for (ptr = HT_START(bidimap, &bidi_map); ptr; ptr = next) {
02855     ent = *ptr;
02856     next = HT_NEXT_RMV(bidimap, &bidi_map, ptr);
02857     tor_free(ent);
02858   }
02859   HT_CLEAR(bidimap, &bidi_map);
02860 }
02861 
02863 void
02864 rep_hist_reset_conn_stats(time_t now)
02865 {
02866   start_of_conn_stats_interval = now;
02867   below_threshold = 0;
02868   mostly_read = 0;
02869   mostly_written = 0;
02870   both_read_and_written = 0;
02871   bidi_map_free();
02872 }
02873 
02876 void
02877 rep_hist_conn_stats_term(void)
02878 {
02879   rep_hist_reset_conn_stats(0);
02880 }
02881 
02886 void
02887 rep_hist_note_or_conn_bytes(uint64_t conn_id, size_t num_read,
02888                             size_t num_written, time_t when)
02889 {
02890   if (!start_of_conn_stats_interval)
02891     return;
02892   /* Initialize */
02893   if (bidi_next_interval == 0)
02894     bidi_next_interval = when + BIDI_INTERVAL;
02895   /* Sum up last period's statistics */
02896   if (when >= bidi_next_interval) {
02897     bidi_map_entry_t **ptr, **next, *ent;
02898     for (ptr = HT_START(bidimap, &bidi_map); ptr; ptr = next) {
02899       ent = *ptr;
02900       if (ent->read + ent->written < BIDI_THRESHOLD)
02901         below_threshold++;
02902       else if (ent->read >= ent->written * BIDI_FACTOR)
02903         mostly_read++;
02904       else if (ent->written >= ent->read * BIDI_FACTOR)
02905         mostly_written++;
02906       else
02907         both_read_and_written++;
02908       next = HT_NEXT_RMV(bidimap, &bidi_map, ptr);
02909       tor_free(ent);
02910     }
02911     while (when >= bidi_next_interval)
02912       bidi_next_interval += BIDI_INTERVAL;
02913     log_info(LD_GENERAL, "%d below threshold, %d mostly read, "
02914              "%d mostly written, %d both read and written.",
02915              below_threshold, mostly_read, mostly_written,
02916              both_read_and_written);
02917   }
02918   /* Add this connection's bytes. */
02919   if (num_read > 0 || num_written > 0) {
02920     bidi_map_entry_t *entry, lookup;
02921     lookup.conn_id = conn_id;
02922     entry = HT_FIND(bidimap, &bidi_map, &lookup);
02923     if (entry) {
02924       entry->written += num_written;
02925       entry->read += num_read;
02926     } else {
02927       entry = tor_malloc_zero(sizeof(bidi_map_entry_t));
02928       entry->conn_id = conn_id;
02929       entry->written = num_written;
02930       entry->read = num_read;
02931       HT_INSERT(bidimap, &bidi_map, entry);
02932     }
02933   }
02934 }
02935 
02939 char *
02940 rep_hist_format_conn_stats(time_t now)
02941 {
02942   char *result, written[ISO_TIME_LEN+1];
02943 
02944   if (!start_of_conn_stats_interval)
02945     return NULL; /* Not initialized. */
02946 
02947   tor_assert(now >= start_of_conn_stats_interval);
02948 
02949   format_iso_time(written, now);
02950   tor_asprintf(&result, "conn-bi-direct %s (%d s) %d,%d,%d,%d\n",
02951                written,
02952                (unsigned) (now - start_of_conn_stats_interval),
02953                below_threshold,
02954                mostly_read,
02955                mostly_written,
02956                both_read_and_written);
02957   return result;
02958 }
02959 
02964 time_t
02965 rep_hist_conn_stats_write(time_t now)
02966 {
02967   char *statsdir = NULL, *filename = NULL, *str = NULL;
02968 
02969   if (!start_of_conn_stats_interval)
02970     return 0; /* Not initialized. */
02971   if (start_of_conn_stats_interval + WRITE_STATS_INTERVAL > now)
02972     goto done; /* Not ready to write */
02973 
02974   /* Generate history string. */
02975   str = rep_hist_format_conn_stats(now);
02976 
02977   /* Reset counters. */
02978   rep_hist_reset_conn_stats(now);
02979 
02980   /* Try to write to disk. */
02981   statsdir = get_datadir_fname("stats");
02982   if (check_private_dir(statsdir, CPD_CREATE, get_options()->User) < 0) {
02983     log_warn(LD_HIST, "Unable to create stats/ directory!");
02984     goto done;
02985   }
02986   filename = get_datadir_fname2("stats", "conn-stats");
02987   if (write_str_to_file(filename, str, 0) < 0)
02988     log_warn(LD_HIST, "Unable to write conn stats to disk!");
02989 
02990  done:
02991   tor_free(str);
02992   tor_free(filename);
02993   tor_free(statsdir);
02994   return start_of_conn_stats_interval + WRITE_STATS_INTERVAL;
02995 }
02996 
02999 void
03000 rep_hist_free_all(void)
03001 {
03002   digestmap_free(history_map, free_or_history);
03003   tor_free(read_array);
03004   tor_free(write_array);
03005   tor_free(last_stability_doc);
03006   tor_free(exit_bytes_read);
03007   tor_free(exit_bytes_written);
03008   tor_free(exit_streams);
03009   built_last_stability_doc_at = 0;
03010   predicted_ports_free();
03011   bidi_map_free();
03012 
03013   if (circuits_for_buffer_stats) {
03014     SMARTLIST_FOREACH(circuits_for_buffer_stats, circ_buffer_stats_t *, s,
03015                       tor_free(s));
03016     smartlist_free(circuits_for_buffer_stats);
03017     circuits_for_buffer_stats = NULL;
03018   }
03019   rep_hist_desc_stats_term();
03020   total_descriptor_downloads = 0;
03021 }
03022