rephist.c 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  2. * Copyright (c) 2007-2016, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. /**
  5. * \file rephist.c
  6. * \brief Basic history and "reputation" functionality to remember
  7. * which servers have worked in the past, how much bandwidth we've
  8. * been using, which ports we tend to want, and so on; further,
  9. * exit port statistics, cell statistics, and connection statistics.
  10. **/
  11. #include "or.h"
  12. #include "circuitlist.h"
  13. #include "circuituse.h"
  14. #include "config.h"
  15. #include "networkstatus.h"
  16. #include "nodelist.h"
  17. #include "rephist.h"
  18. #include "router.h"
  19. #include "routerlist.h"
  20. #include "ht.h"
  21. static void bw_arrays_init(void);
  22. static void predicted_ports_init(void);
  23. /** Total number of bytes currently allocated in fields used by rephist.c. */
  24. uint64_t rephist_total_alloc=0;
  25. /** Number of or_history_t objects currently allocated. */
  26. uint32_t rephist_total_num=0;
  27. /** If the total weighted run count of all runs for a router ever falls
  28. * below this amount, the router can be treated as having 0 MTBF. */
  29. #define STABILITY_EPSILON 0.0001
  30. /** Value by which to discount all old intervals for MTBF purposes. This
  31. * is compounded every STABILITY_INTERVAL. */
  32. #define STABILITY_ALPHA 0.95
  33. /** Interval at which to discount all old intervals for MTBF purposes. */
  34. #define STABILITY_INTERVAL (12*60*60)
  35. /* (This combination of ALPHA, INTERVAL, and EPSILON makes it so that an
  36. * interval that just ended counts twice as much as one that ended a week ago,
  37. * 20X as much as one that ended a month ago, and routers that have had no
  38. * uptime data for about half a year will get forgotten.) */
  39. /** History of an OR-\>OR link. */
  40. typedef struct link_history_t {
  41. /** When did we start tracking this list? */
  42. time_t since;
  43. /** When did we most recently note a change to this link */
  44. time_t changed;
  45. /** How many times did extending from OR1 to OR2 succeed? */
  46. unsigned long n_extend_ok;
  47. /** How many times did extending from OR1 to OR2 fail? */
  48. unsigned long n_extend_fail;
  49. } link_history_t;
  50. /** History of an OR. */
  51. typedef struct or_history_t {
  52. /** When did we start tracking this OR? */
  53. time_t since;
  54. /** When did we most recently note a change to this OR? */
  55. time_t changed;
  56. /** How many times did we successfully connect? */
  57. unsigned long n_conn_ok;
  58. /** How many times did we try to connect and fail?*/
  59. unsigned long n_conn_fail;
  60. /** How many seconds have we been connected to this OR before
  61. * 'up_since'? */
  62. unsigned long uptime;
  63. /** How many seconds have we been unable to connect to this OR before
  64. * 'down_since'? */
  65. unsigned long downtime;
  66. /** If nonzero, we have been connected since this time. */
  67. time_t up_since;
  68. /** If nonzero, we have been unable to connect since this time. */
  69. time_t down_since;
  70. /** The address at which we most recently connected to this OR
  71. * successfully. */
  72. tor_addr_t last_reached_addr;
  73. /** The port at which we most recently connected to this OR successfully */
  74. uint16_t last_reached_port;
  75. /* === For MTBF tracking: */
  76. /** Weighted sum total of all times that this router has been online.
  77. */
  78. unsigned long weighted_run_length;
  79. /** If the router is now online (according to stability-checking rules),
  80. * when did it come online? */
  81. time_t start_of_run;
  82. /** Sum of weights for runs in weighted_run_length. */
  83. double total_run_weights;
  84. /* === For fractional uptime tracking: */
  85. time_t start_of_downtime;
  86. unsigned long weighted_uptime;
  87. unsigned long total_weighted_time;
  88. /** Map from hex OR2 identity digest to a link_history_t for the link
  89. * from this OR to OR2. */
  90. digestmap_t *link_history_map;
  91. } or_history_t;
  92. /** When did we last multiply all routers' weighted_run_length and
  93. * total_run_weights by STABILITY_ALPHA? */
  94. static time_t stability_last_downrated = 0;
  95. /** */
  96. static time_t started_tracking_stability = 0;
  97. /** Map from hex OR identity digest to or_history_t. */
  98. static digestmap_t *history_map = NULL;
  99. /** Return the or_history_t for the OR with identity digest <b>id</b>,
  100. * creating it if necessary. */
  101. static or_history_t *
  102. get_or_history(const char* id)
  103. {
  104. or_history_t *hist;
  105. if (tor_digest_is_zero(id))
  106. return NULL;
  107. hist = digestmap_get(history_map, id);
  108. if (!hist) {
  109. hist = tor_malloc_zero(sizeof(or_history_t));
  110. rephist_total_alloc += sizeof(or_history_t);
  111. rephist_total_num++;
  112. hist->link_history_map = digestmap_new();
  113. hist->since = hist->changed = time(NULL);
  114. tor_addr_make_unspec(&hist->last_reached_addr);
  115. digestmap_set(history_map, id, hist);
  116. }
  117. return hist;
  118. }
  119. /** Return the link_history_t for the link from the first named OR to
  120. * the second, creating it if necessary. (ORs are identified by
  121. * identity digest.)
  122. */
  123. static link_history_t *
  124. get_link_history(const char *from_id, const char *to_id)
  125. {
  126. or_history_t *orhist;
  127. link_history_t *lhist;
  128. orhist = get_or_history(from_id);
  129. if (!orhist)
  130. return NULL;
  131. if (tor_digest_is_zero(to_id))
  132. return NULL;
  133. lhist = digestmap_get(orhist->link_history_map, to_id);
  134. if (!lhist) {
  135. lhist = tor_malloc_zero(sizeof(link_history_t));
  136. rephist_total_alloc += sizeof(link_history_t);
  137. lhist->since = lhist->changed = time(NULL);
  138. digestmap_set(orhist->link_history_map, to_id, lhist);
  139. }
  140. return lhist;
  141. }
  142. /** Helper: free storage held by a single link history entry. */
  143. static void
  144. free_link_history_(void *val)
  145. {
  146. rephist_total_alloc -= sizeof(link_history_t);
  147. tor_free(val);
  148. }
  149. /** Helper: free storage held by a single OR history entry. */
  150. static void
  151. free_or_history(void *_hist)
  152. {
  153. or_history_t *hist = _hist;
  154. digestmap_free(hist->link_history_map, free_link_history_);
  155. rephist_total_alloc -= sizeof(or_history_t);
  156. rephist_total_num--;
  157. tor_free(hist);
  158. }
  159. /** Update an or_history_t object <b>hist</b> so that its uptime/downtime
  160. * count is up-to-date as of <b>when</b>.
  161. */
  162. static void
  163. update_or_history(or_history_t *hist, time_t when)
  164. {
  165. tor_assert(hist);
  166. if (hist->up_since) {
  167. tor_assert(!hist->down_since);
  168. hist->uptime += (when - hist->up_since);
  169. hist->up_since = when;
  170. } else if (hist->down_since) {
  171. hist->downtime += (when - hist->down_since);
  172. hist->down_since = when;
  173. }
  174. }
  175. /** Initialize the static data structures for tracking history. */
  176. void
  177. rep_hist_init(void)
  178. {
  179. history_map = digestmap_new();
  180. bw_arrays_init();
  181. predicted_ports_init();
  182. }
  183. /** Helper: note that we are no longer connected to the router with history
  184. * <b>hist</b>. If <b>failed</b>, the connection failed; otherwise, it was
  185. * closed correctly. */
  186. static void
  187. mark_or_down(or_history_t *hist, time_t when, int failed)
  188. {
  189. if (hist->up_since) {
  190. hist->uptime += (when - hist->up_since);
  191. hist->up_since = 0;
  192. }
  193. if (failed && !hist->down_since) {
  194. hist->down_since = when;
  195. }
  196. }
  197. /** Helper: note that we are connected to the router with history
  198. * <b>hist</b>. */
  199. static void
  200. mark_or_up(or_history_t *hist, time_t when)
  201. {
  202. if (hist->down_since) {
  203. hist->downtime += (when - hist->down_since);
  204. hist->down_since = 0;
  205. }
  206. if (!hist->up_since) {
  207. hist->up_since = when;
  208. }
  209. }
  210. /** Remember that an attempt to connect to the OR with identity digest
  211. * <b>id</b> failed at <b>when</b>.
  212. */
  213. void
  214. rep_hist_note_connect_failed(const char* id, time_t when)
  215. {
  216. or_history_t *hist;
  217. hist = get_or_history(id);
  218. if (!hist)
  219. return;
  220. ++hist->n_conn_fail;
  221. mark_or_down(hist, when, 1);
  222. hist->changed = when;
  223. }
  224. /** Remember that an attempt to connect to the OR with identity digest
  225. * <b>id</b> succeeded at <b>when</b>.
  226. */
  227. void
  228. rep_hist_note_connect_succeeded(const char* id, time_t when)
  229. {
  230. or_history_t *hist;
  231. hist = get_or_history(id);
  232. if (!hist)
  233. return;
  234. ++hist->n_conn_ok;
  235. mark_or_up(hist, when);
  236. hist->changed = when;
  237. }
  238. /** Remember that we intentionally closed our connection to the OR
  239. * with identity digest <b>id</b> at <b>when</b>.
  240. */
  241. void
  242. rep_hist_note_disconnect(const char* id, time_t when)
  243. {
  244. or_history_t *hist;
  245. hist = get_or_history(id);
  246. if (!hist)
  247. return;
  248. mark_or_down(hist, when, 0);
  249. hist->changed = when;
  250. }
  251. /** Remember that our connection to the OR with identity digest
  252. * <b>id</b> had an error and stopped working at <b>when</b>.
  253. */
  254. void
  255. rep_hist_note_connection_died(const char* id, time_t when)
  256. {
  257. or_history_t *hist;
  258. if (!id) {
  259. /* If conn has no identity, it didn't complete its handshake, or something
  260. * went wrong. Ignore it.
  261. */
  262. return;
  263. }
  264. hist = get_or_history(id);
  265. if (!hist)
  266. return;
  267. mark_or_down(hist, when, 1);
  268. hist->changed = when;
  269. }
  270. /** We have just decided that this router with identity digest <b>id</b> is
  271. * reachable, meaning we will give it a "Running" flag for the next while. */
  272. void
  273. rep_hist_note_router_reachable(const char *id, const tor_addr_t *at_addr,
  274. const uint16_t at_port, time_t when)
  275. {
  276. or_history_t *hist = get_or_history(id);
  277. int was_in_run = 1;
  278. char tbuf[ISO_TIME_LEN+1];
  279. int addr_changed, port_changed;
  280. tor_assert(hist);
  281. tor_assert((!at_addr && !at_port) || (at_addr && at_port));
  282. addr_changed = at_addr && !tor_addr_is_null(&hist->last_reached_addr) &&
  283. tor_addr_compare(at_addr, &hist->last_reached_addr, CMP_EXACT) != 0;
  284. port_changed = at_port && hist->last_reached_port &&
  285. at_port != hist->last_reached_port;
  286. if (!started_tracking_stability)
  287. started_tracking_stability = time(NULL);
  288. if (!hist->start_of_run) {
  289. hist->start_of_run = when;
  290. was_in_run = 0;
  291. }
  292. if (hist->start_of_downtime) {
  293. long down_length;
  294. format_local_iso_time(tbuf, hist->start_of_downtime);
  295. log_info(LD_HIST, "Router %s is now Running; it had been down since %s.",
  296. hex_str(id, DIGEST_LEN), tbuf);
  297. if (was_in_run)
  298. log_info(LD_HIST, " (Paradoxically, it was already Running too.)");
  299. down_length = when - hist->start_of_downtime;
  300. hist->total_weighted_time += down_length;
  301. hist->start_of_downtime = 0;
  302. } else if (addr_changed || port_changed) {
  303. /* If we're reachable, but the address changed, treat this as some
  304. * downtime. */
  305. int penalty = get_options()->TestingTorNetwork ? 240 : 3600;
  306. networkstatus_t *ns;
  307. if ((ns = networkstatus_get_latest_consensus())) {
  308. int fresh_interval = (int)(ns->fresh_until - ns->valid_after);
  309. int live_interval = (int)(ns->valid_until - ns->valid_after);
  310. /* on average, a descriptor addr change takes .5 intervals to make it
  311. * into a consensus, and half a liveness period to make it to
  312. * clients. */
  313. penalty = (int)(fresh_interval + live_interval) / 2;
  314. }
  315. format_local_iso_time(tbuf, hist->start_of_run);
  316. log_info(LD_HIST,"Router %s still seems Running, but its address appears "
  317. "to have changed since the last time it was reachable. I'm "
  318. "going to treat it as having been down for %d seconds",
  319. hex_str(id, DIGEST_LEN), penalty);
  320. rep_hist_note_router_unreachable(id, when-penalty);
  321. rep_hist_note_router_reachable(id, NULL, 0, when);
  322. } else {
  323. format_local_iso_time(tbuf, hist->start_of_run);
  324. if (was_in_run)
  325. log_debug(LD_HIST, "Router %s is still Running; it has been Running "
  326. "since %s", hex_str(id, DIGEST_LEN), tbuf);
  327. else
  328. log_info(LD_HIST,"Router %s is now Running; it was previously untracked",
  329. hex_str(id, DIGEST_LEN));
  330. }
  331. if (at_addr)
  332. tor_addr_copy(&hist->last_reached_addr, at_addr);
  333. if (at_port)
  334. hist->last_reached_port = at_port;
  335. }
  336. /** We have just decided that this router is unreachable, meaning
  337. * we are taking away its "Running" flag. */
  338. void
  339. rep_hist_note_router_unreachable(const char *id, time_t when)
  340. {
  341. or_history_t *hist = get_or_history(id);
  342. char tbuf[ISO_TIME_LEN+1];
  343. int was_running = 0;
  344. if (!started_tracking_stability)
  345. started_tracking_stability = time(NULL);
  346. tor_assert(hist);
  347. if (hist->start_of_run) {
  348. /*XXXX We could treat failed connections differently from failed
  349. * connect attempts. */
  350. long run_length = when - hist->start_of_run;
  351. format_local_iso_time(tbuf, hist->start_of_run);
  352. hist->total_run_weights += 1.0;
  353. hist->start_of_run = 0;
  354. if (run_length < 0) {
  355. unsigned long penalty = -run_length;
  356. #define SUBTRACT_CLAMPED(var, penalty) \
  357. do { (var) = (var) < (penalty) ? 0 : (var) - (penalty); } while (0)
  358. SUBTRACT_CLAMPED(hist->weighted_run_length, penalty);
  359. SUBTRACT_CLAMPED(hist->weighted_uptime, penalty);
  360. } else {
  361. hist->weighted_run_length += run_length;
  362. hist->weighted_uptime += run_length;
  363. hist->total_weighted_time += run_length;
  364. }
  365. was_running = 1;
  366. log_info(LD_HIST, "Router %s is now non-Running: it had previously been "
  367. "Running since %s. Its total weighted uptime is %lu/%lu.",
  368. hex_str(id, DIGEST_LEN), tbuf, hist->weighted_uptime,
  369. hist->total_weighted_time);
  370. }
  371. if (!hist->start_of_downtime) {
  372. hist->start_of_downtime = when;
  373. if (!was_running)
  374. log_info(LD_HIST, "Router %s is now non-Running; it was previously "
  375. "untracked.", hex_str(id, DIGEST_LEN));
  376. } else {
  377. if (!was_running) {
  378. format_local_iso_time(tbuf, hist->start_of_downtime);
  379. log_info(LD_HIST, "Router %s is still non-Running; it has been "
  380. "non-Running since %s.", hex_str(id, DIGEST_LEN), tbuf);
  381. }
  382. }
  383. }
  384. /** Mark a router with ID <b>id</b> as non-Running, and retroactively declare
  385. * that it has never been running: give it no stability and no WFU. */
  386. void
  387. rep_hist_make_router_pessimal(const char *id, time_t when)
  388. {
  389. or_history_t *hist = get_or_history(id);
  390. tor_assert(hist);
  391. rep_hist_note_router_unreachable(id, when);
  392. mark_or_down(hist, when, 1);
  393. hist->weighted_run_length = 0;
  394. hist->weighted_uptime = 0;
  395. }
  396. /** Helper: Discount all old MTBF data, if it is time to do so. Return
  397. * the time at which we should next discount MTBF data. */
  398. time_t
  399. rep_hist_downrate_old_runs(time_t now)
  400. {
  401. digestmap_iter_t *orhist_it;
  402. const char *digest1;
  403. or_history_t *hist;
  404. void *hist_p;
  405. double alpha = 1.0;
  406. if (!history_map)
  407. history_map = digestmap_new();
  408. if (!stability_last_downrated)
  409. stability_last_downrated = now;
  410. if (stability_last_downrated + STABILITY_INTERVAL > now)
  411. return stability_last_downrated + STABILITY_INTERVAL;
  412. /* Okay, we should downrate the data. By how much? */
  413. while (stability_last_downrated + STABILITY_INTERVAL < now) {
  414. stability_last_downrated += STABILITY_INTERVAL;
  415. alpha *= STABILITY_ALPHA;
  416. }
  417. log_info(LD_HIST, "Discounting all old stability info by a factor of %f",
  418. alpha);
  419. /* Multiply every w_r_l, t_r_w pair by alpha. */
  420. for (orhist_it = digestmap_iter_init(history_map);
  421. !digestmap_iter_done(orhist_it);
  422. orhist_it = digestmap_iter_next(history_map,orhist_it)) {
  423. digestmap_iter_get(orhist_it, &digest1, &hist_p);
  424. hist = hist_p;
  425. hist->weighted_run_length =
  426. (unsigned long)(hist->weighted_run_length * alpha);
  427. hist->total_run_weights *= alpha;
  428. hist->weighted_uptime = (unsigned long)(hist->weighted_uptime * alpha);
  429. hist->total_weighted_time = (unsigned long)
  430. (hist->total_weighted_time * alpha);
  431. }
  432. return stability_last_downrated + STABILITY_INTERVAL;
  433. }
  434. /** Helper: Return the weighted MTBF of the router with history <b>hist</b>. */
  435. static double
  436. get_stability(or_history_t *hist, time_t when)
  437. {
  438. long total = hist->weighted_run_length;
  439. double total_weights = hist->total_run_weights;
  440. if (hist->start_of_run) {
  441. /* We're currently in a run. Let total and total_weights hold the values
  442. * they would hold if the current run were to end now. */
  443. total += (when-hist->start_of_run);
  444. total_weights += 1.0;
  445. }
  446. if (total_weights < STABILITY_EPSILON) {
  447. /* Round down to zero, and avoid divide-by-zero. */
  448. return 0.0;
  449. }
  450. return total / total_weights;
  451. }
  452. /** Return the total amount of time we've been observing, with each run of
  453. * time downrated by the appropriate factor. */
  454. static long
  455. get_total_weighted_time(or_history_t *hist, time_t when)
  456. {
  457. long total = hist->total_weighted_time;
  458. if (hist->start_of_run) {
  459. total += (when - hist->start_of_run);
  460. } else if (hist->start_of_downtime) {
  461. total += (when - hist->start_of_downtime);
  462. }
  463. return total;
  464. }
  465. /** Helper: Return the weighted percent-of-time-online of the router with
  466. * history <b>hist</b>. */
  467. static double
  468. get_weighted_fractional_uptime(or_history_t *hist, time_t when)
  469. {
  470. long total = hist->total_weighted_time;
  471. long up = hist->weighted_uptime;
  472. if (hist->start_of_run) {
  473. long run_length = (when - hist->start_of_run);
  474. up += run_length;
  475. total += run_length;
  476. } else if (hist->start_of_downtime) {
  477. total += (when - hist->start_of_downtime);
  478. }
  479. if (!total) {
  480. /* Avoid calling anybody's uptime infinity (which should be impossible if
  481. * the code is working), or NaN (which can happen for any router we haven't
  482. * observed up or down yet). */
  483. return 0.0;
  484. }
  485. return ((double) up) / total;
  486. }
  487. /** Return how long the router whose identity digest is <b>id</b> has
  488. * been reachable. Return 0 if the router is unknown or currently deemed
  489. * unreachable. */
  490. long
  491. rep_hist_get_uptime(const char *id, time_t when)
  492. {
  493. or_history_t *hist = get_or_history(id);
  494. if (!hist)
  495. return 0;
  496. if (!hist->start_of_run || when < hist->start_of_run)
  497. return 0;
  498. return when - hist->start_of_run;
  499. }
  500. /** Return an estimated MTBF for the router whose identity digest is
  501. * <b>id</b>. Return 0 if the router is unknown. */
  502. double
  503. rep_hist_get_stability(const char *id, time_t when)
  504. {
  505. or_history_t *hist = get_or_history(id);
  506. if (!hist)
  507. return 0.0;
  508. return get_stability(hist, when);
  509. }
  510. /** Return an estimated percent-of-time-online for the router whose identity
  511. * digest is <b>id</b>. Return 0 if the router is unknown. */
  512. double
  513. rep_hist_get_weighted_fractional_uptime(const char *id, time_t when)
  514. {
  515. or_history_t *hist = get_or_history(id);
  516. if (!hist)
  517. return 0.0;
  518. return get_weighted_fractional_uptime(hist, when);
  519. }
  520. /** Return a number representing how long we've known about the router whose
  521. * digest is <b>id</b>. Return 0 if the router is unknown.
  522. *
  523. * Be careful: this measure increases monotonically as we know the router for
  524. * longer and longer, but it doesn't increase linearly.
  525. */
  526. long
  527. rep_hist_get_weighted_time_known(const char *id, time_t when)
  528. {
  529. or_history_t *hist = get_or_history(id);
  530. if (!hist)
  531. return 0;
  532. return get_total_weighted_time(hist, when);
  533. }
  534. /** Return true if we've been measuring MTBFs for long enough to
  535. * pronounce on Stability. */
  536. int
  537. rep_hist_have_measured_enough_stability(void)
  538. {
  539. /* XXXX++ This doesn't do so well when we change our opinion
  540. * as to whether we're tracking router stability. */
  541. return started_tracking_stability < time(NULL) - 4*60*60;
  542. }
  543. /** Remember that we successfully extended from the OR with identity
  544. * digest <b>from_id</b> to the OR with identity digest
  545. * <b>to_name</b>.
  546. */
  547. void
  548. rep_hist_note_extend_succeeded(const char *from_id, const char *to_id)
  549. {
  550. link_history_t *hist;
  551. /* log_fn(LOG_WARN, "EXTEND SUCCEEDED: %s->%s",from_name,to_name); */
  552. hist = get_link_history(from_id, to_id);
  553. if (!hist)
  554. return;
  555. ++hist->n_extend_ok;
  556. hist->changed = time(NULL);
  557. }
  558. /** Remember that we tried to extend from the OR with identity digest
  559. * <b>from_id</b> to the OR with identity digest <b>to_name</b>, but
  560. * failed.
  561. */
  562. void
  563. rep_hist_note_extend_failed(const char *from_id, const char *to_id)
  564. {
  565. link_history_t *hist;
  566. /* log_fn(LOG_WARN, "EXTEND FAILED: %s->%s",from_name,to_name); */
  567. hist = get_link_history(from_id, to_id);
  568. if (!hist)
  569. return;
  570. ++hist->n_extend_fail;
  571. hist->changed = time(NULL);
  572. }
  573. /** Log all the reliability data we have remembered, with the chosen
  574. * severity.
  575. */
  576. void
  577. rep_hist_dump_stats(time_t now, int severity)
  578. {
  579. digestmap_iter_t *lhist_it;
  580. digestmap_iter_t *orhist_it;
  581. const char *name1, *name2, *digest1, *digest2;
  582. char hexdigest1[HEX_DIGEST_LEN+1];
  583. char hexdigest2[HEX_DIGEST_LEN+1];
  584. or_history_t *or_history;
  585. link_history_t *link_history;
  586. void *or_history_p, *link_history_p;
  587. double uptime;
  588. char buffer[2048];
  589. size_t len;
  590. int ret;
  591. unsigned long upt, downt;
  592. const node_t *node;
  593. rep_history_clean(now - get_options()->RephistTrackTime);
  594. tor_log(severity, LD_HIST, "--------------- Dumping history information:");
  595. for (orhist_it = digestmap_iter_init(history_map);
  596. !digestmap_iter_done(orhist_it);
  597. orhist_it = digestmap_iter_next(history_map,orhist_it)) {
  598. double s;
  599. long stability;
  600. digestmap_iter_get(orhist_it, &digest1, &or_history_p);
  601. or_history = (or_history_t*) or_history_p;
  602. if ((node = node_get_by_id(digest1)) && node_get_nickname(node))
  603. name1 = node_get_nickname(node);
  604. else
  605. name1 = "(unknown)";
  606. base16_encode(hexdigest1, sizeof(hexdigest1), digest1, DIGEST_LEN);
  607. update_or_history(or_history, now);
  608. upt = or_history->uptime;
  609. downt = or_history->downtime;
  610. s = get_stability(or_history, now);
  611. stability = (long)s;
  612. if (upt+downt) {
  613. uptime = ((double)upt) / (upt+downt);
  614. } else {
  615. uptime=1.0;
  616. }
  617. tor_log(severity, LD_HIST,
  618. "OR %s [%s]: %ld/%ld good connections; uptime %ld/%ld sec (%.2f%%); "
  619. "wmtbf %lu:%02lu:%02lu",
  620. name1, hexdigest1,
  621. or_history->n_conn_ok, or_history->n_conn_fail+or_history->n_conn_ok,
  622. upt, upt+downt, uptime*100.0,
  623. stability/3600, (stability/60)%60, stability%60);
  624. if (!digestmap_isempty(or_history->link_history_map)) {
  625. strlcpy(buffer, " Extend attempts: ", sizeof(buffer));
  626. len = strlen(buffer);
  627. for (lhist_it = digestmap_iter_init(or_history->link_history_map);
  628. !digestmap_iter_done(lhist_it);
  629. lhist_it = digestmap_iter_next(or_history->link_history_map,
  630. lhist_it)) {
  631. digestmap_iter_get(lhist_it, &digest2, &link_history_p);
  632. if ((node = node_get_by_id(digest2)) && node_get_nickname(node))
  633. name2 = node_get_nickname(node);
  634. else
  635. name2 = "(unknown)";
  636. link_history = (link_history_t*) link_history_p;
  637. base16_encode(hexdigest2, sizeof(hexdigest2), digest2, DIGEST_LEN);
  638. ret = tor_snprintf(buffer+len, 2048-len, "%s [%s](%ld/%ld); ",
  639. name2,
  640. hexdigest2,
  641. link_history->n_extend_ok,
  642. link_history->n_extend_ok+link_history->n_extend_fail);
  643. if (ret<0)
  644. break;
  645. else
  646. len += ret;
  647. }
  648. tor_log(severity, LD_HIST, "%s", buffer);
  649. }
  650. }
  651. }
  652. /** Remove history info for routers/links that haven't changed since
  653. * <b>before</b>.
  654. */
  655. void
  656. rep_history_clean(time_t before)
  657. {
  658. int authority = authdir_mode(get_options());
  659. or_history_t *or_history;
  660. link_history_t *link_history;
  661. void *or_history_p, *link_history_p;
  662. digestmap_iter_t *orhist_it, *lhist_it;
  663. const char *d1, *d2;
  664. orhist_it = digestmap_iter_init(history_map);
  665. while (!digestmap_iter_done(orhist_it)) {
  666. int remove;
  667. digestmap_iter_get(orhist_it, &d1, &or_history_p);
  668. or_history = or_history_p;
  669. remove = authority ? (or_history->total_run_weights < STABILITY_EPSILON &&
  670. !or_history->start_of_run)
  671. : (or_history->changed < before);
  672. if (remove) {
  673. orhist_it = digestmap_iter_next_rmv(history_map, orhist_it);
  674. free_or_history(or_history);
  675. continue;
  676. }
  677. for (lhist_it = digestmap_iter_init(or_history->link_history_map);
  678. !digestmap_iter_done(lhist_it); ) {
  679. digestmap_iter_get(lhist_it, &d2, &link_history_p);
  680. link_history = link_history_p;
  681. if (link_history->changed < before) {
  682. lhist_it = digestmap_iter_next_rmv(or_history->link_history_map,
  683. lhist_it);
  684. rephist_total_alloc -= sizeof(link_history_t);
  685. tor_free(link_history);
  686. continue;
  687. }
  688. lhist_it = digestmap_iter_next(or_history->link_history_map,lhist_it);
  689. }
  690. orhist_it = digestmap_iter_next(history_map, orhist_it);
  691. }
  692. }
  693. /** Write MTBF data to disk. Return 0 on success, negative on failure.
  694. *
  695. * If <b>missing_means_down</b>, then if we're about to write an entry
  696. * that is still considered up but isn't in our routerlist, consider it
  697. * to be down. */
  698. int
  699. rep_hist_record_mtbf_data(time_t now, int missing_means_down)
  700. {
  701. char time_buf[ISO_TIME_LEN+1];
  702. digestmap_iter_t *orhist_it;
  703. const char *digest;
  704. void *or_history_p;
  705. or_history_t *hist;
  706. open_file_t *open_file = NULL;
  707. FILE *f;
  708. {
  709. char *filename = get_datadir_fname("router-stability");
  710. f = start_writing_to_stdio_file(filename, OPEN_FLAGS_REPLACE|O_TEXT, 0600,
  711. &open_file);
  712. tor_free(filename);
  713. if (!f)
  714. return -1;
  715. }
  716. /* File format is:
  717. * FormatLine *KeywordLine Data
  718. *
  719. * FormatLine = "format 1" NL
  720. * KeywordLine = Keyword SP Arguments NL
  721. * Data = "data" NL *RouterMTBFLine "." NL
  722. * RouterMTBFLine = Fingerprint SP WeightedRunLen SP
  723. * TotalRunWeights [SP S=StartRunTime] NL
  724. */
  725. #define PUT(s) STMT_BEGIN if (fputs((s),f)<0) goto err; STMT_END
  726. #define PRINTF(args) STMT_BEGIN if (fprintf args <0) goto err; STMT_END
  727. PUT("format 2\n");
  728. format_iso_time(time_buf, time(NULL));
  729. PRINTF((f, "stored-at %s\n", time_buf));
  730. if (started_tracking_stability) {
  731. format_iso_time(time_buf, started_tracking_stability);
  732. PRINTF((f, "tracked-since %s\n", time_buf));
  733. }
  734. if (stability_last_downrated) {
  735. format_iso_time(time_buf, stability_last_downrated);
  736. PRINTF((f, "last-downrated %s\n", time_buf));
  737. }
  738. PUT("data\n");
  739. /* XXX Nick: now bridge auths record this for all routers too.
  740. * Should we make them record it only for bridge routers? -RD
  741. * Not for 0.2.0. -NM */
  742. for (orhist_it = digestmap_iter_init(history_map);
  743. !digestmap_iter_done(orhist_it);
  744. orhist_it = digestmap_iter_next(history_map,orhist_it)) {
  745. char dbuf[HEX_DIGEST_LEN+1];
  746. const char *t = NULL;
  747. digestmap_iter_get(orhist_it, &digest, &or_history_p);
  748. hist = (or_history_t*) or_history_p;
  749. base16_encode(dbuf, sizeof(dbuf), digest, DIGEST_LEN);
  750. if (missing_means_down && hist->start_of_run &&
  751. !router_get_by_id_digest(digest)) {
  752. /* We think this relay is running, but it's not listed in our
  753. * routerlist. Somehow it fell out without telling us it went
  754. * down. Complain and also correct it. */
  755. log_info(LD_HIST,
  756. "Relay '%s' is listed as up in rephist, but it's not in "
  757. "our routerlist. Correcting.", dbuf);
  758. rep_hist_note_router_unreachable(digest, now);
  759. }
  760. PRINTF((f, "R %s\n", dbuf));
  761. if (hist->start_of_run > 0) {
  762. format_iso_time(time_buf, hist->start_of_run);
  763. t = time_buf;
  764. }
  765. PRINTF((f, "+MTBF %lu %.5f%s%s\n",
  766. hist->weighted_run_length, hist->total_run_weights,
  767. t ? " S=" : "", t ? t : ""));
  768. t = NULL;
  769. if (hist->start_of_downtime > 0) {
  770. format_iso_time(time_buf, hist->start_of_downtime);
  771. t = time_buf;
  772. }
  773. PRINTF((f, "+WFU %lu %lu%s%s\n",
  774. hist->weighted_uptime, hist->total_weighted_time,
  775. t ? " S=" : "", t ? t : ""));
  776. }
  777. PUT(".\n");
  778. #undef PUT
  779. #undef PRINTF
  780. return finish_writing_to_file(open_file);
  781. err:
  782. abort_writing_to_file(open_file);
  783. return -1;
  784. }
  785. /** Helper: return the first j >= i such that !strcmpstart(sl[j], prefix) and
  786. * such that no line sl[k] with i <= k < j starts with "R ". Return -1 if no
  787. * such line exists. */
  788. static int
  789. find_next_with(smartlist_t *sl, int i, const char *prefix)
  790. {
  791. for ( ; i < smartlist_len(sl); ++i) {
  792. const char *line = smartlist_get(sl, i);
  793. if (!strcmpstart(line, prefix))
  794. return i;
  795. if (!strcmpstart(line, "R "))
  796. return -1;
  797. }
  798. return -1;
  799. }
  800. /** How many bad times has parse_possibly_bad_iso_time() parsed? */
  801. static int n_bogus_times = 0;
  802. /** Parse the ISO-formatted time in <b>s</b> into *<b>time_out</b>, but
  803. * round any pre-1970 date to Jan 1, 1970. */
  804. static int
  805. parse_possibly_bad_iso_time(const char *s, time_t *time_out)
  806. {
  807. int year;
  808. char b[5];
  809. strlcpy(b, s, sizeof(b));
  810. b[4] = '\0';
  811. year = (int)tor_parse_long(b, 10, 0, INT_MAX, NULL, NULL);
  812. if (year < 1970) {
  813. *time_out = 0;
  814. ++n_bogus_times;
  815. return 0;
  816. } else
  817. return parse_iso_time(s, time_out);
  818. }
  819. /** We've read a time <b>t</b> from a file stored at <b>stored_at</b>, which
  820. * says we started measuring at <b>started_measuring</b>. Return a new number
  821. * that's about as much before <b>now</b> as <b>t</b> was before
  822. * <b>stored_at</b>.
  823. */
  824. static inline time_t
  825. correct_time(time_t t, time_t now, time_t stored_at, time_t started_measuring)
  826. {
  827. if (t < started_measuring - 24*60*60*365)
  828. return 0;
  829. else if (t < started_measuring)
  830. return started_measuring;
  831. else if (t > stored_at)
  832. return 0;
  833. else {
  834. long run_length = stored_at - t;
  835. t = (time_t)(now - run_length);
  836. if (t < started_measuring)
  837. t = started_measuring;
  838. return t;
  839. }
  840. }
  841. /** Load MTBF data from disk. Returns 0 on success or recoverable error, -1
  842. * on failure. */
  843. int
  844. rep_hist_load_mtbf_data(time_t now)
  845. {
  846. /* XXXX won't handle being called while history is already populated. */
  847. smartlist_t *lines;
  848. const char *line = NULL;
  849. int r=0, i;
  850. time_t last_downrated = 0, stored_at = 0, tracked_since = 0;
  851. time_t latest_possible_start = now;
  852. long format = -1;
  853. {
  854. char *filename = get_datadir_fname("router-stability");
  855. char *d = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
  856. tor_free(filename);
  857. if (!d)
  858. return -1;
  859. lines = smartlist_new();
  860. smartlist_split_string(lines, d, "\n", SPLIT_SKIP_SPACE, 0);
  861. tor_free(d);
  862. }
  863. {
  864. const char *firstline;
  865. if (smartlist_len(lines)>4) {
  866. firstline = smartlist_get(lines, 0);
  867. if (!strcmpstart(firstline, "format "))
  868. format = tor_parse_long(firstline+strlen("format "),
  869. 10, -1, LONG_MAX, NULL, NULL);
  870. }
  871. }
  872. if (format != 1 && format != 2) {
  873. log_warn(LD_HIST,
  874. "Unrecognized format in mtbf history file. Skipping.");
  875. goto err;
  876. }
  877. for (i = 1; i < smartlist_len(lines); ++i) {
  878. line = smartlist_get(lines, i);
  879. if (!strcmp(line, "data"))
  880. break;
  881. if (!strcmpstart(line, "last-downrated ")) {
  882. if (parse_iso_time(line+strlen("last-downrated "), &last_downrated)<0)
  883. log_warn(LD_HIST,"Couldn't parse downrate time in mtbf "
  884. "history file.");
  885. }
  886. if (!strcmpstart(line, "stored-at ")) {
  887. if (parse_iso_time(line+strlen("stored-at "), &stored_at)<0)
  888. log_warn(LD_HIST,"Couldn't parse stored time in mtbf "
  889. "history file.");
  890. }
  891. if (!strcmpstart(line, "tracked-since ")) {
  892. if (parse_iso_time(line+strlen("tracked-since "), &tracked_since)<0)
  893. log_warn(LD_HIST,"Couldn't parse started-tracking time in mtbf "
  894. "history file.");
  895. }
  896. }
  897. if (last_downrated > now)
  898. last_downrated = now;
  899. if (tracked_since > now)
  900. tracked_since = now;
  901. if (!stored_at) {
  902. log_warn(LD_HIST, "No stored time recorded.");
  903. goto err;
  904. }
  905. if (line && !strcmp(line, "data"))
  906. ++i;
  907. n_bogus_times = 0;
  908. for (; i < smartlist_len(lines); ++i) {
  909. char digest[DIGEST_LEN];
  910. char hexbuf[HEX_DIGEST_LEN+1];
  911. char mtbf_timebuf[ISO_TIME_LEN+1];
  912. char wfu_timebuf[ISO_TIME_LEN+1];
  913. time_t start_of_run = 0;
  914. time_t start_of_downtime = 0;
  915. int have_mtbf = 0, have_wfu = 0;
  916. long wrl = 0;
  917. double trw = 0;
  918. long wt_uptime = 0, total_wt_time = 0;
  919. int n;
  920. or_history_t *hist;
  921. line = smartlist_get(lines, i);
  922. if (!strcmp(line, "."))
  923. break;
  924. mtbf_timebuf[0] = '\0';
  925. wfu_timebuf[0] = '\0';
  926. if (format == 1) {
  927. n = tor_sscanf(line, "%40s %ld %lf S=%10s %8s",
  928. hexbuf, &wrl, &trw, mtbf_timebuf, mtbf_timebuf+11);
  929. if (n != 3 && n != 5) {
  930. log_warn(LD_HIST, "Couldn't scan line %s", escaped(line));
  931. continue;
  932. }
  933. have_mtbf = 1;
  934. } else {
  935. // format == 2.
  936. int mtbf_idx, wfu_idx;
  937. if (strcmpstart(line, "R ") || strlen(line) < 2+HEX_DIGEST_LEN)
  938. continue;
  939. strlcpy(hexbuf, line+2, sizeof(hexbuf));
  940. mtbf_idx = find_next_with(lines, i+1, "+MTBF ");
  941. wfu_idx = find_next_with(lines, i+1, "+WFU ");
  942. if (mtbf_idx >= 0) {
  943. const char *mtbfline = smartlist_get(lines, mtbf_idx);
  944. n = tor_sscanf(mtbfline, "+MTBF %lu %lf S=%10s %8s",
  945. &wrl, &trw, mtbf_timebuf, mtbf_timebuf+11);
  946. if (n == 2 || n == 4) {
  947. have_mtbf = 1;
  948. } else {
  949. log_warn(LD_HIST, "Couldn't scan +MTBF line %s",
  950. escaped(mtbfline));
  951. }
  952. }
  953. if (wfu_idx >= 0) {
  954. const char *wfuline = smartlist_get(lines, wfu_idx);
  955. n = tor_sscanf(wfuline, "+WFU %lu %lu S=%10s %8s",
  956. &wt_uptime, &total_wt_time,
  957. wfu_timebuf, wfu_timebuf+11);
  958. if (n == 2 || n == 4) {
  959. have_wfu = 1;
  960. } else {
  961. log_warn(LD_HIST, "Couldn't scan +WFU line %s", escaped(wfuline));
  962. }
  963. }
  964. if (wfu_idx > i)
  965. i = wfu_idx;
  966. if (mtbf_idx > i)
  967. i = mtbf_idx;
  968. }
  969. if (base16_decode(digest, DIGEST_LEN, hexbuf, HEX_DIGEST_LEN) < 0) {
  970. log_warn(LD_HIST, "Couldn't hex string %s", escaped(hexbuf));
  971. continue;
  972. }
  973. hist = get_or_history(digest);
  974. if (!hist)
  975. continue;
  976. if (have_mtbf) {
  977. if (mtbf_timebuf[0]) {
  978. mtbf_timebuf[10] = ' ';
  979. if (parse_possibly_bad_iso_time(mtbf_timebuf, &start_of_run)<0)
  980. log_warn(LD_HIST, "Couldn't parse time %s",
  981. escaped(mtbf_timebuf));
  982. }
  983. hist->start_of_run = correct_time(start_of_run, now, stored_at,
  984. tracked_since);
  985. if (hist->start_of_run < latest_possible_start + wrl)
  986. latest_possible_start = (time_t)(hist->start_of_run - wrl);
  987. hist->weighted_run_length = wrl;
  988. hist->total_run_weights = trw;
  989. }
  990. if (have_wfu) {
  991. if (wfu_timebuf[0]) {
  992. wfu_timebuf[10] = ' ';
  993. if (parse_possibly_bad_iso_time(wfu_timebuf, &start_of_downtime)<0)
  994. log_warn(LD_HIST, "Couldn't parse time %s", escaped(wfu_timebuf));
  995. }
  996. }
  997. hist->start_of_downtime = correct_time(start_of_downtime, now, stored_at,
  998. tracked_since);
  999. hist->weighted_uptime = wt_uptime;
  1000. hist->total_weighted_time = total_wt_time;
  1001. }
  1002. if (strcmp(line, "."))
  1003. log_warn(LD_HIST, "Truncated MTBF file.");
  1004. if (tracked_since < 86400*365) /* Recover from insanely early value. */
  1005. tracked_since = latest_possible_start;
  1006. stability_last_downrated = last_downrated;
  1007. started_tracking_stability = tracked_since;
  1008. goto done;
  1009. err:
  1010. r = -1;
  1011. done:
  1012. SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
  1013. smartlist_free(lines);
  1014. return r;
  1015. }
  1016. /** For how many seconds do we keep track of individual per-second bandwidth
  1017. * totals? */
  1018. #define NUM_SECS_ROLLING_MEASURE 10
  1019. /** How large are the intervals for which we track and report bandwidth use? */
  1020. #define NUM_SECS_BW_SUM_INTERVAL (4*60*60)
  1021. /** How far in the past do we remember and publish bandwidth use? */
  1022. #define NUM_SECS_BW_SUM_IS_VALID (24*60*60)
  1023. /** How many bandwidth usage intervals do we remember? (derived) */
  1024. #define NUM_TOTALS (NUM_SECS_BW_SUM_IS_VALID/NUM_SECS_BW_SUM_INTERVAL)
  1025. /** Structure to track bandwidth use, and remember the maxima for a given
  1026. * time period.
  1027. */
  1028. typedef struct bw_array_t {
  1029. /** Observation array: Total number of bytes transferred in each of the last
  1030. * NUM_SECS_ROLLING_MEASURE seconds. This is used as a circular array. */
  1031. uint64_t obs[NUM_SECS_ROLLING_MEASURE];
  1032. int cur_obs_idx; /**< Current position in obs. */
  1033. time_t cur_obs_time; /**< Time represented in obs[cur_obs_idx] */
  1034. uint64_t total_obs; /**< Total for all members of obs except
  1035. * obs[cur_obs_idx] */
  1036. uint64_t max_total; /**< Largest value that total_obs has taken on in the
  1037. * current period. */
  1038. uint64_t total_in_period; /**< Total bytes transferred in the current
  1039. * period. */
  1040. /** When does the next period begin? */
  1041. time_t next_period;
  1042. /** Where in 'maxima' should the maximum bandwidth usage for the current
  1043. * period be stored? */
  1044. int next_max_idx;
  1045. /** How many values in maxima/totals have been set ever? */
  1046. int num_maxes_set;
  1047. /** Circular array of the maximum
  1048. * bandwidth-per-NUM_SECS_ROLLING_MEASURE usage for the last
  1049. * NUM_TOTALS periods */
  1050. uint64_t maxima[NUM_TOTALS];
  1051. /** Circular array of the total bandwidth usage for the last NUM_TOTALS
  1052. * periods */
  1053. uint64_t totals[NUM_TOTALS];
  1054. } bw_array_t;
  1055. /** Shift the current period of b forward by one. */
  1056. static void
  1057. commit_max(bw_array_t *b)
  1058. {
  1059. /* Store total from current period. */
  1060. b->totals[b->next_max_idx] = b->total_in_period;
  1061. /* Store maximum from current period. */
  1062. b->maxima[b->next_max_idx++] = b->max_total;
  1063. /* Advance next_period and next_max_idx */
  1064. b->next_period += NUM_SECS_BW_SUM_INTERVAL;
  1065. if (b->next_max_idx == NUM_TOTALS)
  1066. b->next_max_idx = 0;
  1067. if (b->num_maxes_set < NUM_TOTALS)
  1068. ++b->num_maxes_set;
  1069. /* Reset max_total. */
  1070. b->max_total = 0;
  1071. /* Reset total_in_period. */
  1072. b->total_in_period = 0;
  1073. }
  1074. /** Shift the current observation time of <b>b</b> forward by one second. */
  1075. static inline void
  1076. advance_obs(bw_array_t *b)
  1077. {
  1078. int nextidx;
  1079. uint64_t total;
  1080. /* Calculate the total bandwidth for the last NUM_SECS_ROLLING_MEASURE
  1081. * seconds; adjust max_total as needed.*/
  1082. total = b->total_obs + b->obs[b->cur_obs_idx];
  1083. if (total > b->max_total)
  1084. b->max_total = total;
  1085. nextidx = b->cur_obs_idx+1;
  1086. if (nextidx == NUM_SECS_ROLLING_MEASURE)
  1087. nextidx = 0;
  1088. b->total_obs = total - b->obs[nextidx];
  1089. b->obs[nextidx]=0;
  1090. b->cur_obs_idx = nextidx;
  1091. if (++b->cur_obs_time >= b->next_period)
  1092. commit_max(b);
  1093. }
  1094. /** Add <b>n</b> bytes to the number of bytes in <b>b</b> for second
  1095. * <b>when</b>. */
  1096. static inline void
  1097. add_obs(bw_array_t *b, time_t when, uint64_t n)
  1098. {
  1099. if (when < b->cur_obs_time)
  1100. return; /* Don't record data in the past. */
  1101. /* If we're currently adding observations for an earlier second than
  1102. * 'when', advance b->cur_obs_time and b->cur_obs_idx by an
  1103. * appropriate number of seconds, and do all the other housekeeping. */
  1104. while (when > b->cur_obs_time) {
  1105. /* Doing this one second at a time is potentially inefficient, if we start
  1106. with a state file that is very old. Fortunately, it doesn't seem to
  1107. show up in profiles, so we can just ignore it for now. */
  1108. advance_obs(b);
  1109. }
  1110. b->obs[b->cur_obs_idx] += n;
  1111. b->total_in_period += n;
  1112. }
  1113. /** Allocate, initialize, and return a new bw_array. */
  1114. static bw_array_t *
  1115. bw_array_new(void)
  1116. {
  1117. bw_array_t *b;
  1118. time_t start;
  1119. b = tor_malloc_zero(sizeof(bw_array_t));
  1120. rephist_total_alloc += sizeof(bw_array_t);
  1121. start = time(NULL);
  1122. b->cur_obs_time = start;
  1123. b->next_period = start + NUM_SECS_BW_SUM_INTERVAL;
  1124. return b;
  1125. }
  1126. /** Free storage held by bandwidth array <b>b</b>. */
  1127. static void
  1128. bw_array_free(bw_array_t *b)
  1129. {
  1130. if (!b) {
  1131. return;
  1132. }
  1133. rephist_total_alloc -= sizeof(bw_array_t);
  1134. tor_free(b);
  1135. }
  1136. /** Recent history of bandwidth observations for read operations. */
  1137. static bw_array_t *read_array = NULL;
  1138. /** Recent history of bandwidth observations for write operations. */
  1139. static bw_array_t *write_array = NULL;
  1140. /** Recent history of bandwidth observations for read operations for the
  1141. directory protocol. */
  1142. static bw_array_t *dir_read_array = NULL;
  1143. /** Recent history of bandwidth observations for write operations for the
  1144. directory protocol. */
  1145. static bw_array_t *dir_write_array = NULL;
  1146. /** Set up [dir-]read_array and [dir-]write_array, freeing them if they
  1147. * already exist. */
  1148. static void
  1149. bw_arrays_init(void)
  1150. {
  1151. bw_array_free(read_array);
  1152. bw_array_free(write_array);
  1153. bw_array_free(dir_read_array);
  1154. bw_array_free(dir_write_array);
  1155. read_array = bw_array_new();
  1156. write_array = bw_array_new();
  1157. dir_read_array = bw_array_new();
  1158. dir_write_array = bw_array_new();
  1159. }
  1160. /** Remember that we read <b>num_bytes</b> bytes in second <b>when</b>.
  1161. *
  1162. * Add num_bytes to the current running total for <b>when</b>.
  1163. *
  1164. * <b>when</b> can go back to time, but it's safe to ignore calls
  1165. * earlier than the latest <b>when</b> you've heard of.
  1166. */
  1167. void
  1168. rep_hist_note_bytes_written(size_t num_bytes, time_t when)
  1169. {
  1170. /* Maybe a circular array for recent seconds, and step to a new point
  1171. * every time a new second shows up. Or simpler is to just to have
  1172. * a normal array and push down each item every second; it's short.
  1173. */
  1174. /* When a new second has rolled over, compute the sum of the bytes we've
  1175. * seen over when-1 to when-1-NUM_SECS_ROLLING_MEASURE, and stick it
  1176. * somewhere. See rep_hist_bandwidth_assess() below.
  1177. */
  1178. add_obs(write_array, when, num_bytes);
  1179. }
  1180. /** Remember that we wrote <b>num_bytes</b> bytes in second <b>when</b>.
  1181. * (like rep_hist_note_bytes_written() above)
  1182. */
  1183. void
  1184. rep_hist_note_bytes_read(size_t num_bytes, time_t when)
  1185. {
  1186. /* if we're smart, we can make this func and the one above share code */
  1187. add_obs(read_array, when, num_bytes);
  1188. }
  1189. /** Remember that we wrote <b>num_bytes</b> directory bytes in second
  1190. * <b>when</b>. (like rep_hist_note_bytes_written() above)
  1191. */
  1192. void
  1193. rep_hist_note_dir_bytes_written(size_t num_bytes, time_t when)
  1194. {
  1195. add_obs(dir_write_array, when, num_bytes);
  1196. }
  1197. /** Remember that we read <b>num_bytes</b> directory bytes in second
  1198. * <b>when</b>. (like rep_hist_note_bytes_written() above)
  1199. */
  1200. void
  1201. rep_hist_note_dir_bytes_read(size_t num_bytes, time_t when)
  1202. {
  1203. add_obs(dir_read_array, when, num_bytes);
  1204. }
  1205. /** Helper: Return the largest value in b->maxima. (This is equal to the
  1206. * most bandwidth used in any NUM_SECS_ROLLING_MEASURE period for the last
  1207. * NUM_SECS_BW_SUM_IS_VALID seconds.)
  1208. */
  1209. static uint64_t
  1210. find_largest_max(bw_array_t *b)
  1211. {
  1212. int i;
  1213. uint64_t max;
  1214. max=0;
  1215. for (i=0; i<NUM_TOTALS; ++i) {
  1216. if (b->maxima[i]>max)
  1217. max = b->maxima[i];
  1218. }
  1219. return max;
  1220. }
  1221. /** Find the largest sums in the past NUM_SECS_BW_SUM_IS_VALID (roughly)
  1222. * seconds. Find one sum for reading and one for writing. They don't have
  1223. * to be at the same time.
  1224. *
  1225. * Return the smaller of these sums, divided by NUM_SECS_ROLLING_MEASURE.
  1226. */
  1227. int
  1228. rep_hist_bandwidth_assess(void)
  1229. {
  1230. uint64_t w,r;
  1231. r = find_largest_max(read_array);
  1232. w = find_largest_max(write_array);
  1233. if (r>w)
  1234. return (int)(U64_TO_DBL(w)/NUM_SECS_ROLLING_MEASURE);
  1235. else
  1236. return (int)(U64_TO_DBL(r)/NUM_SECS_ROLLING_MEASURE);
  1237. }
  1238. /** Print the bandwidth history of b (either [dir-]read_array or
  1239. * [dir-]write_array) into the buffer pointed to by buf. The format is
  1240. * simply comma separated numbers, from oldest to newest.
  1241. *
  1242. * It returns the number of bytes written.
  1243. */
  1244. static size_t
  1245. rep_hist_fill_bandwidth_history(char *buf, size_t len, const bw_array_t *b)
  1246. {
  1247. char *cp = buf;
  1248. int i, n;
  1249. const or_options_t *options = get_options();
  1250. uint64_t cutoff;
  1251. if (b->num_maxes_set <= b->next_max_idx) {
  1252. /* We haven't been through the circular array yet; time starts at i=0.*/
  1253. i = 0;
  1254. } else {
  1255. /* We've been around the array at least once. The next i to be
  1256. overwritten is the oldest. */
  1257. i = b->next_max_idx;
  1258. }
  1259. if (options->RelayBandwidthRate) {
  1260. /* We don't want to report that we used more bandwidth than the max we're
  1261. * willing to relay; otherwise everybody will know how much traffic
  1262. * we used ourself. */
  1263. cutoff = options->RelayBandwidthRate * NUM_SECS_BW_SUM_INTERVAL;
  1264. } else {
  1265. cutoff = UINT64_MAX;
  1266. }
  1267. for (n=0; n<b->num_maxes_set; ++n,++i) {
  1268. uint64_t total;
  1269. if (i >= NUM_TOTALS)
  1270. i -= NUM_TOTALS;
  1271. tor_assert(i < NUM_TOTALS);
  1272. /* Round the bandwidth used down to the nearest 1k. */
  1273. total = b->totals[i] & ~0x3ff;
  1274. if (total > cutoff)
  1275. total = cutoff;
  1276. if (n==(b->num_maxes_set-1))
  1277. tor_snprintf(cp, len-(cp-buf), U64_FORMAT, U64_PRINTF_ARG(total));
  1278. else
  1279. tor_snprintf(cp, len-(cp-buf), U64_FORMAT",", U64_PRINTF_ARG(total));
  1280. cp += strlen(cp);
  1281. }
  1282. return cp-buf;
  1283. }
  1284. /** Allocate and return lines for representing this server's bandwidth
  1285. * history in its descriptor. We publish these lines in our extra-info
  1286. * descriptor.
  1287. */
  1288. char *
  1289. rep_hist_get_bandwidth_lines(void)
  1290. {
  1291. char *buf, *cp;
  1292. char t[ISO_TIME_LEN+1];
  1293. int r;
  1294. bw_array_t *b = NULL;
  1295. const char *desc = NULL;
  1296. size_t len;
  1297. /* [dirreq-](read|write)-history yyyy-mm-dd HH:MM:SS (n s) n,n,n... */
  1298. /* The n,n,n part above. Largest representation of a uint64_t is 20 chars
  1299. * long, plus the comma. */
  1300. #define MAX_HIST_VALUE_LEN (21*NUM_TOTALS)
  1301. len = (67+MAX_HIST_VALUE_LEN)*4;
  1302. buf = tor_malloc_zero(len);
  1303. cp = buf;
  1304. for (r=0;r<4;++r) {
  1305. char tmp[MAX_HIST_VALUE_LEN];
  1306. size_t slen;
  1307. switch (r) {
  1308. case 0:
  1309. b = write_array;
  1310. desc = "write-history";
  1311. break;
  1312. case 1:
  1313. b = read_array;
  1314. desc = "read-history";
  1315. break;
  1316. case 2:
  1317. b = dir_write_array;
  1318. desc = "dirreq-write-history";
  1319. break;
  1320. case 3:
  1321. b = dir_read_array;
  1322. desc = "dirreq-read-history";
  1323. break;
  1324. }
  1325. tor_assert(b);
  1326. slen = rep_hist_fill_bandwidth_history(tmp, MAX_HIST_VALUE_LEN, b);
  1327. /* If we don't have anything to write, skip to the next entry. */
  1328. if (slen == 0)
  1329. continue;
  1330. format_iso_time(t, b->next_period-NUM_SECS_BW_SUM_INTERVAL);
  1331. tor_snprintf(cp, len-(cp-buf), "%s %s (%d s) ",
  1332. desc, t, NUM_SECS_BW_SUM_INTERVAL);
  1333. cp += strlen(cp);
  1334. strlcat(cp, tmp, len-(cp-buf));
  1335. cp += slen;
  1336. strlcat(cp, "\n", len-(cp-buf));
  1337. ++cp;
  1338. }
  1339. return buf;
  1340. }
  1341. /** Write a single bw_array_t into the Values, Ends, Interval, and Maximum
  1342. * entries of an or_state_t. Done before writing out a new state file. */
  1343. static void
  1344. rep_hist_update_bwhist_state_section(or_state_t *state,
  1345. const bw_array_t *b,
  1346. smartlist_t **s_values,
  1347. smartlist_t **s_maxima,
  1348. time_t *s_begins,
  1349. int *s_interval)
  1350. {
  1351. int i,j;
  1352. uint64_t maxval;
  1353. if (*s_values) {
  1354. SMARTLIST_FOREACH(*s_values, char *, val, tor_free(val));
  1355. smartlist_free(*s_values);
  1356. }
  1357. if (*s_maxima) {
  1358. SMARTLIST_FOREACH(*s_maxima, char *, val, tor_free(val));
  1359. smartlist_free(*s_maxima);
  1360. }
  1361. if (! server_mode(get_options())) {
  1362. /* Clients don't need to store bandwidth history persistently;
  1363. * force these values to the defaults. */
  1364. /* FFFF we should pull the default out of config.c's state table,
  1365. * so we don't have two defaults. */
  1366. if (*s_begins != 0 || *s_interval != 900) {
  1367. time_t now = time(NULL);
  1368. time_t save_at = get_options()->AvoidDiskWrites ? now+3600 : now+600;
  1369. or_state_mark_dirty(state, save_at);
  1370. }
  1371. *s_begins = 0;
  1372. *s_interval = 900;
  1373. *s_values = smartlist_new();
  1374. *s_maxima = smartlist_new();
  1375. return;
  1376. }
  1377. *s_begins = b->next_period;
  1378. *s_interval = NUM_SECS_BW_SUM_INTERVAL;
  1379. *s_values = smartlist_new();
  1380. *s_maxima = smartlist_new();
  1381. /* Set i to first position in circular array */
  1382. i = (b->num_maxes_set <= b->next_max_idx) ? 0 : b->next_max_idx;
  1383. for (j=0; j < b->num_maxes_set; ++j,++i) {
  1384. if (i >= NUM_TOTALS)
  1385. i = 0;
  1386. smartlist_add_asprintf(*s_values, U64_FORMAT,
  1387. U64_PRINTF_ARG(b->totals[i] & ~0x3ff));
  1388. maxval = b->maxima[i] / NUM_SECS_ROLLING_MEASURE;
  1389. smartlist_add_asprintf(*s_maxima, U64_FORMAT,
  1390. U64_PRINTF_ARG(maxval & ~0x3ff));
  1391. }
  1392. smartlist_add_asprintf(*s_values, U64_FORMAT,
  1393. U64_PRINTF_ARG(b->total_in_period & ~0x3ff));
  1394. maxval = b->max_total / NUM_SECS_ROLLING_MEASURE;
  1395. smartlist_add_asprintf(*s_maxima, U64_FORMAT,
  1396. U64_PRINTF_ARG(maxval & ~0x3ff));
  1397. }
  1398. /** Update <b>state</b> with the newest bandwidth history. Done before
  1399. * writing out a new state file. */
  1400. void
  1401. rep_hist_update_state(or_state_t *state)
  1402. {
  1403. #define UPDATE(arrname,st) \
  1404. rep_hist_update_bwhist_state_section(state,\
  1405. (arrname),\
  1406. &state->BWHistory ## st ## Values, \
  1407. &state->BWHistory ## st ## Maxima, \
  1408. &state->BWHistory ## st ## Ends, \
  1409. &state->BWHistory ## st ## Interval)
  1410. UPDATE(write_array, Write);
  1411. UPDATE(read_array, Read);
  1412. UPDATE(dir_write_array, DirWrite);
  1413. UPDATE(dir_read_array, DirRead);
  1414. if (server_mode(get_options())) {
  1415. or_state_mark_dirty(state, time(NULL)+(2*3600));
  1416. }
  1417. #undef UPDATE
  1418. }
  1419. /** Load a single bw_array_t from its Values, Ends, Maxima, and Interval
  1420. * entries in an or_state_t. Done while reading the state file. */
  1421. static int
  1422. rep_hist_load_bwhist_state_section(bw_array_t *b,
  1423. const smartlist_t *s_values,
  1424. const smartlist_t *s_maxima,
  1425. const time_t s_begins,
  1426. const int s_interval)
  1427. {
  1428. time_t now = time(NULL);
  1429. int retval = 0;
  1430. time_t start;
  1431. uint64_t v, mv;
  1432. int i,ok,ok_m = 0;
  1433. int have_maxima = s_maxima && s_values &&
  1434. (smartlist_len(s_values) == smartlist_len(s_maxima));
  1435. if (s_values && s_begins >= now - NUM_SECS_BW_SUM_INTERVAL*NUM_TOTALS) {
  1436. start = s_begins - s_interval*(smartlist_len(s_values));
  1437. if (start > now)
  1438. return 0;
  1439. b->cur_obs_time = start;
  1440. b->next_period = start + NUM_SECS_BW_SUM_INTERVAL;
  1441. SMARTLIST_FOREACH_BEGIN(s_values, const char *, cp) {
  1442. const char *maxstr = NULL;
  1443. v = tor_parse_uint64(cp, 10, 0, UINT64_MAX, &ok, NULL);
  1444. if (have_maxima) {
  1445. maxstr = smartlist_get(s_maxima, cp_sl_idx);
  1446. mv = tor_parse_uint64(maxstr, 10, 0, UINT64_MAX, &ok_m, NULL);
  1447. mv *= NUM_SECS_ROLLING_MEASURE;
  1448. } else {
  1449. /* No maxima known; guess average rate to be conservative. */
  1450. mv = (v / s_interval) * NUM_SECS_ROLLING_MEASURE;
  1451. }
  1452. if (!ok) {
  1453. retval = -1;
  1454. log_notice(LD_HIST, "Could not parse value '%s' into a number.'",cp);
  1455. }
  1456. if (maxstr && !ok_m) {
  1457. retval = -1;
  1458. log_notice(LD_HIST, "Could not parse maximum '%s' into a number.'",
  1459. maxstr);
  1460. }
  1461. if (start < now) {
  1462. time_t cur_start = start;
  1463. time_t actual_interval_len = s_interval;
  1464. uint64_t cur_val = 0;
  1465. /* Calculate the average per second. This is the best we can do
  1466. * because our state file doesn't have per-second resolution. */
  1467. if (start + s_interval > now)
  1468. actual_interval_len = now - start;
  1469. cur_val = v / actual_interval_len;
  1470. /* This is potentially inefficient, but since we don't do it very
  1471. * often it should be ok. */
  1472. while (cur_start < start + actual_interval_len) {
  1473. add_obs(b, cur_start, cur_val);
  1474. ++cur_start;
  1475. }
  1476. b->max_total = mv;
  1477. /* This will result in some fairly choppy history if s_interval
  1478. * is not the same as NUM_SECS_BW_SUM_INTERVAL. XXXX */
  1479. start += actual_interval_len;
  1480. }
  1481. } SMARTLIST_FOREACH_END(cp);
  1482. }
  1483. /* Clean up maxima and observed */
  1484. for (i=0; i<NUM_SECS_ROLLING_MEASURE; ++i) {
  1485. b->obs[i] = 0;
  1486. }
  1487. b->total_obs = 0;
  1488. return retval;
  1489. }
  1490. /** Set bandwidth history from the state file we just loaded. */
  1491. int
  1492. rep_hist_load_state(or_state_t *state, char **err)
  1493. {
  1494. int all_ok = 1;
  1495. /* Assert they already have been malloced */
  1496. tor_assert(read_array && write_array);
  1497. tor_assert(dir_read_array && dir_write_array);
  1498. #define LOAD(arrname,st) \
  1499. if (rep_hist_load_bwhist_state_section( \
  1500. (arrname), \
  1501. state->BWHistory ## st ## Values, \
  1502. state->BWHistory ## st ## Maxima, \
  1503. state->BWHistory ## st ## Ends, \
  1504. state->BWHistory ## st ## Interval)<0) \
  1505. all_ok = 0
  1506. LOAD(write_array, Write);
  1507. LOAD(read_array, Read);
  1508. LOAD(dir_write_array, DirWrite);
  1509. LOAD(dir_read_array, DirRead);
  1510. #undef LOAD
  1511. if (!all_ok) {
  1512. *err = tor_strdup("Parsing of bandwidth history values failed");
  1513. /* and create fresh arrays */
  1514. bw_arrays_init();
  1515. return -1;
  1516. }
  1517. return 0;
  1518. }
  1519. /*********************************************************************/
  1520. /** A single predicted port: used to remember which ports we've made
  1521. * connections to, so that we can try to keep making circuits that can handle
  1522. * those ports. */
  1523. typedef struct predicted_port_t {
  1524. /** The port we connected to */
  1525. uint16_t port;
  1526. /** The time at which we last used it */
  1527. time_t time;
  1528. } predicted_port_t;
  1529. /** A list of port numbers that have been used recently. */
  1530. static smartlist_t *predicted_ports_list=NULL;
  1531. /** We just got an application request for a connection with
  1532. * port <b>port</b>. Remember it for the future, so we can keep
  1533. * some circuits open that will exit to this port.
  1534. */
  1535. static void
  1536. add_predicted_port(time_t now, uint16_t port)
  1537. {
  1538. predicted_port_t *pp = tor_malloc(sizeof(predicted_port_t));
  1539. pp->port = port;
  1540. pp->time = now;
  1541. rephist_total_alloc += sizeof(*pp);
  1542. smartlist_add(predicted_ports_list, pp);
  1543. }
  1544. /** Initialize whatever memory and structs are needed for predicting
  1545. * which ports will be used. Also seed it with port 80, so we'll build
  1546. * circuits on start-up.
  1547. */
  1548. static void
  1549. predicted_ports_init(void)
  1550. {
  1551. predicted_ports_list = smartlist_new();
  1552. add_predicted_port(time(NULL), 80); /* add one to kickstart us */
  1553. }
  1554. /** Free whatever memory is needed for predicting which ports will
  1555. * be used.
  1556. */
  1557. static void
  1558. predicted_ports_free(void)
  1559. {
  1560. rephist_total_alloc -=
  1561. smartlist_len(predicted_ports_list)*sizeof(predicted_port_t);
  1562. SMARTLIST_FOREACH(predicted_ports_list, predicted_port_t *,
  1563. pp, tor_free(pp));
  1564. smartlist_free(predicted_ports_list);
  1565. }
  1566. /** Remember that <b>port</b> has been asked for as of time <b>now</b>.
  1567. * This is used for predicting what sorts of streams we'll make in the
  1568. * future and making exit circuits to anticipate that.
  1569. */
  1570. void
  1571. rep_hist_note_used_port(time_t now, uint16_t port)
  1572. {
  1573. tor_assert(predicted_ports_list);
  1574. if (!port) /* record nothing */
  1575. return;
  1576. SMARTLIST_FOREACH_BEGIN(predicted_ports_list, predicted_port_t *, pp) {
  1577. if (pp->port == port) {
  1578. pp->time = now;
  1579. return;
  1580. }
  1581. } SMARTLIST_FOREACH_END(pp);
  1582. /* it's not there yet; we need to add it */
  1583. add_predicted_port(now, port);
  1584. }
  1585. /** Return a newly allocated pointer to a list of uint16_t * for ports that
  1586. * are likely to be asked for in the near future.
  1587. */
  1588. smartlist_t *
  1589. rep_hist_get_predicted_ports(time_t now)
  1590. {
  1591. int predicted_circs_relevance_time;
  1592. smartlist_t *out = smartlist_new();
  1593. tor_assert(predicted_ports_list);
  1594. predicted_circs_relevance_time = get_options()->PredictedPortsRelevanceTime;
  1595. /* clean out obsolete entries */
  1596. SMARTLIST_FOREACH_BEGIN(predicted_ports_list, predicted_port_t *, pp) {
  1597. if (pp->time + predicted_circs_relevance_time < now) {
  1598. log_debug(LD_CIRC, "Expiring predicted port %d", pp->port);
  1599. rephist_total_alloc -= sizeof(predicted_port_t);
  1600. tor_free(pp);
  1601. SMARTLIST_DEL_CURRENT(predicted_ports_list, pp);
  1602. } else {
  1603. smartlist_add(out, tor_memdup(&pp->port, sizeof(uint16_t)));
  1604. }
  1605. } SMARTLIST_FOREACH_END(pp);
  1606. return out;
  1607. }
  1608. /**
  1609. * Take a list of uint16_t *, and remove every port in the list from the
  1610. * current list of predicted ports.
  1611. */
  1612. void
  1613. rep_hist_remove_predicted_ports(const smartlist_t *rmv_ports)
  1614. {
  1615. /* Let's do this on O(N), not O(N^2). */
  1616. bitarray_t *remove_ports = bitarray_init_zero(UINT16_MAX);
  1617. SMARTLIST_FOREACH(rmv_ports, const uint16_t *, p,
  1618. bitarray_set(remove_ports, *p));
  1619. SMARTLIST_FOREACH_BEGIN(predicted_ports_list, predicted_port_t *, pp) {
  1620. if (bitarray_is_set(remove_ports, pp->port)) {
  1621. tor_free(pp);
  1622. rephist_total_alloc -= sizeof(*pp);
  1623. SMARTLIST_DEL_CURRENT(predicted_ports_list, pp);
  1624. }
  1625. } SMARTLIST_FOREACH_END(pp);
  1626. bitarray_free(remove_ports);
  1627. }
  1628. /** The user asked us to do a resolve. Rather than keeping track of
  1629. * timings and such of resolves, we fake it for now by treating
  1630. * it the same way as a connection to port 80. This way we will continue
  1631. * to have circuits lying around if the user only uses Tor for resolves.
  1632. */
  1633. void
  1634. rep_hist_note_used_resolve(time_t now)
  1635. {
  1636. rep_hist_note_used_port(now, 80);
  1637. }
  1638. /** The last time at which we needed an internal circ. */
  1639. static time_t predicted_internal_time = 0;
  1640. /** The last time we needed an internal circ with good uptime. */
  1641. static time_t predicted_internal_uptime_time = 0;
  1642. /** The last time we needed an internal circ with good capacity. */
  1643. static time_t predicted_internal_capacity_time = 0;
  1644. /** Remember that we used an internal circ at time <b>now</b>. */
  1645. void
  1646. rep_hist_note_used_internal(time_t now, int need_uptime, int need_capacity)
  1647. {
  1648. predicted_internal_time = now;
  1649. if (need_uptime)
  1650. predicted_internal_uptime_time = now;
  1651. if (need_capacity)
  1652. predicted_internal_capacity_time = now;
  1653. }
  1654. /** Return 1 if we've used an internal circ recently; else return 0. */
  1655. int
  1656. rep_hist_get_predicted_internal(time_t now, int *need_uptime,
  1657. int *need_capacity)
  1658. {
  1659. int predicted_circs_relevance_time;
  1660. predicted_circs_relevance_time = get_options()->PredictedPortsRelevanceTime;
  1661. if (!predicted_internal_time) { /* initialize it */
  1662. predicted_internal_time = now;
  1663. predicted_internal_uptime_time = now;
  1664. predicted_internal_capacity_time = now;
  1665. }
  1666. if (predicted_internal_time + predicted_circs_relevance_time < now)
  1667. return 0; /* too long ago */
  1668. if (predicted_internal_uptime_time + predicted_circs_relevance_time >= now)
  1669. *need_uptime = 1;
  1670. // Always predict that we need capacity.
  1671. *need_capacity = 1;
  1672. return 1;
  1673. }
  1674. /** Any ports used lately? These are pre-seeded if we just started
  1675. * up or if we're running a hidden service. */
  1676. int
  1677. any_predicted_circuits(time_t now)
  1678. {
  1679. int predicted_circs_relevance_time;
  1680. predicted_circs_relevance_time = get_options()->PredictedPortsRelevanceTime;
  1681. return smartlist_len(predicted_ports_list) ||
  1682. predicted_internal_time + predicted_circs_relevance_time >= now;
  1683. }
  1684. /** Return 1 if we have no need for circuits currently, else return 0. */
  1685. int
  1686. rep_hist_circbuilding_dormant(time_t now)
  1687. {
  1688. const or_options_t *options = get_options();
  1689. if (any_predicted_circuits(now))
  1690. return 0;
  1691. /* see if we'll still need to build testing circuits */
  1692. if (server_mode(options) &&
  1693. (!check_whether_orport_reachable(options) ||
  1694. !circuit_enough_testing_circs()))
  1695. return 0;
  1696. if (!check_whether_dirport_reachable(options))
  1697. return 0;
  1698. return 1;
  1699. }
  1700. /** Structure to track how many times we've done each public key operation. */
  1701. static struct {
  1702. /** How many directory objects have we signed? */
  1703. unsigned long n_signed_dir_objs;
  1704. /** How many routerdescs have we signed? */
  1705. unsigned long n_signed_routerdescs;
  1706. /** How many directory objects have we verified? */
  1707. unsigned long n_verified_dir_objs;
  1708. /** How many routerdescs have we verified */
  1709. unsigned long n_verified_routerdescs;
  1710. /** How many onionskins have we encrypted to build circuits? */
  1711. unsigned long n_onionskins_encrypted;
  1712. /** How many onionskins have we decrypted to do circuit build requests? */
  1713. unsigned long n_onionskins_decrypted;
  1714. /** How many times have we done the TLS handshake as a client? */
  1715. unsigned long n_tls_client_handshakes;
  1716. /** How many times have we done the TLS handshake as a server? */
  1717. unsigned long n_tls_server_handshakes;
  1718. /** How many PK operations have we done as a hidden service client? */
  1719. unsigned long n_rend_client_ops;
  1720. /** How many PK operations have we done as a hidden service midpoint? */
  1721. unsigned long n_rend_mid_ops;
  1722. /** How many PK operations have we done as a hidden service provider? */
  1723. unsigned long n_rend_server_ops;
  1724. } pk_op_counts = {0,0,0,0,0,0,0,0,0,0,0};
  1725. /** Increment the count of the number of times we've done <b>operation</b>. */
  1726. void
  1727. note_crypto_pk_op(pk_op_t operation)
  1728. {
  1729. switch (operation)
  1730. {
  1731. case SIGN_DIR:
  1732. pk_op_counts.n_signed_dir_objs++;
  1733. break;
  1734. case SIGN_RTR:
  1735. pk_op_counts.n_signed_routerdescs++;
  1736. break;
  1737. case VERIFY_DIR:
  1738. pk_op_counts.n_verified_dir_objs++;
  1739. break;
  1740. case VERIFY_RTR:
  1741. pk_op_counts.n_verified_routerdescs++;
  1742. break;
  1743. case ENC_ONIONSKIN:
  1744. pk_op_counts.n_onionskins_encrypted++;
  1745. break;
  1746. case DEC_ONIONSKIN:
  1747. pk_op_counts.n_onionskins_decrypted++;
  1748. break;
  1749. case TLS_HANDSHAKE_C:
  1750. pk_op_counts.n_tls_client_handshakes++;
  1751. break;
  1752. case TLS_HANDSHAKE_S:
  1753. pk_op_counts.n_tls_server_handshakes++;
  1754. break;
  1755. case REND_CLIENT:
  1756. pk_op_counts.n_rend_client_ops++;
  1757. break;
  1758. case REND_MID:
  1759. pk_op_counts.n_rend_mid_ops++;
  1760. break;
  1761. case REND_SERVER:
  1762. pk_op_counts.n_rend_server_ops++;
  1763. break;
  1764. default:
  1765. log_warn(LD_BUG, "Unknown pk operation %d", operation);
  1766. }
  1767. }
  1768. /** Log the number of times we've done each public/private-key operation. */
  1769. void
  1770. dump_pk_ops(int severity)
  1771. {
  1772. tor_log(severity, LD_HIST,
  1773. "PK operations: %lu directory objects signed, "
  1774. "%lu directory objects verified, "
  1775. "%lu routerdescs signed, "
  1776. "%lu routerdescs verified, "
  1777. "%lu onionskins encrypted, "
  1778. "%lu onionskins decrypted, "
  1779. "%lu client-side TLS handshakes, "
  1780. "%lu server-side TLS handshakes, "
  1781. "%lu rendezvous client operations, "
  1782. "%lu rendezvous middle operations, "
  1783. "%lu rendezvous server operations.",
  1784. pk_op_counts.n_signed_dir_objs,
  1785. pk_op_counts.n_verified_dir_objs,
  1786. pk_op_counts.n_signed_routerdescs,
  1787. pk_op_counts.n_verified_routerdescs,
  1788. pk_op_counts.n_onionskins_encrypted,
  1789. pk_op_counts.n_onionskins_decrypted,
  1790. pk_op_counts.n_tls_client_handshakes,
  1791. pk_op_counts.n_tls_server_handshakes,
  1792. pk_op_counts.n_rend_client_ops,
  1793. pk_op_counts.n_rend_mid_ops,
  1794. pk_op_counts.n_rend_server_ops);
  1795. }
  1796. /*** Exit port statistics ***/
  1797. /* Some constants */
  1798. /** To what multiple should byte numbers be rounded up? */
  1799. #define EXIT_STATS_ROUND_UP_BYTES 1024
  1800. /** To what multiple should stream counts be rounded up? */
  1801. #define EXIT_STATS_ROUND_UP_STREAMS 4
  1802. /** Number of TCP ports */
  1803. #define EXIT_STATS_NUM_PORTS 65536
  1804. /** Top n ports that will be included in exit stats. */
  1805. #define EXIT_STATS_TOP_N_PORTS 10
  1806. /* The following data structures are arrays and no fancy smartlists or maps,
  1807. * so that all write operations can be done in constant time. This comes at
  1808. * the price of some memory (1.25 MB) and linear complexity when writing
  1809. * stats for measuring relays. */
  1810. /** Number of bytes read in current period by exit port */
  1811. static uint64_t *exit_bytes_read = NULL;
  1812. /** Number of bytes written in current period by exit port */
  1813. static uint64_t *exit_bytes_written = NULL;
  1814. /** Number of streams opened in current period by exit port */
  1815. static uint32_t *exit_streams = NULL;
  1816. /** Start time of exit stats or 0 if we're not collecting exit stats. */
  1817. static time_t start_of_exit_stats_interval;
  1818. /** Initialize exit port stats. */
  1819. void
  1820. rep_hist_exit_stats_init(time_t now)
  1821. {
  1822. start_of_exit_stats_interval = now;
  1823. exit_bytes_read = tor_calloc(EXIT_STATS_NUM_PORTS, sizeof(uint64_t));
  1824. exit_bytes_written = tor_calloc(EXIT_STATS_NUM_PORTS, sizeof(uint64_t));
  1825. exit_streams = tor_calloc(EXIT_STATS_NUM_PORTS, sizeof(uint32_t));
  1826. }
  1827. /** Reset counters for exit port statistics. */
  1828. void
  1829. rep_hist_reset_exit_stats(time_t now)
  1830. {
  1831. start_of_exit_stats_interval = now;
  1832. memset(exit_bytes_read, 0, EXIT_STATS_NUM_PORTS * sizeof(uint64_t));
  1833. memset(exit_bytes_written, 0, EXIT_STATS_NUM_PORTS * sizeof(uint64_t));
  1834. memset(exit_streams, 0, EXIT_STATS_NUM_PORTS * sizeof(uint32_t));
  1835. }
  1836. /** Stop collecting exit port stats in a way that we can re-start doing
  1837. * so in rep_hist_exit_stats_init(). */
  1838. void
  1839. rep_hist_exit_stats_term(void)
  1840. {
  1841. start_of_exit_stats_interval = 0;
  1842. tor_free(exit_bytes_read);
  1843. tor_free(exit_bytes_written);
  1844. tor_free(exit_streams);
  1845. }
  1846. /** Helper for qsort: compare two ints. Does not handle overflow properly,
  1847. * but works fine for sorting an array of port numbers, which is what we use
  1848. * it for. */
  1849. static int
  1850. compare_int_(const void *x, const void *y)
  1851. {
  1852. return (*(int*)x - *(int*)y);
  1853. }
  1854. /** Return a newly allocated string containing the exit port statistics
  1855. * until <b>now</b>, or NULL if we're not collecting exit stats. Caller
  1856. * must ensure start_of_exit_stats_interval is in the past. */
  1857. char *
  1858. rep_hist_format_exit_stats(time_t now)
  1859. {
  1860. int i, j, top_elements = 0, cur_min_idx = 0, cur_port;
  1861. uint64_t top_bytes[EXIT_STATS_TOP_N_PORTS];
  1862. int top_ports[EXIT_STATS_TOP_N_PORTS];
  1863. uint64_t cur_bytes = 0, other_read = 0, other_written = 0,
  1864. total_read = 0, total_written = 0;
  1865. uint32_t total_streams = 0, other_streams = 0;
  1866. smartlist_t *written_strings, *read_strings, *streams_strings;
  1867. char *written_string, *read_string, *streams_string;
  1868. char t[ISO_TIME_LEN+1];
  1869. char *result;
  1870. if (!start_of_exit_stats_interval)
  1871. return NULL; /* Not initialized. */
  1872. tor_assert(now >= start_of_exit_stats_interval);
  1873. /* Go through all ports to find the n ports that saw most written and
  1874. * read bytes.
  1875. *
  1876. * Invariant: at the end of the loop for iteration i,
  1877. * total_read is the sum of all exit_bytes_read[0..i]
  1878. * total_written is the sum of all exit_bytes_written[0..i]
  1879. * total_stream is the sum of all exit_streams[0..i]
  1880. *
  1881. * top_elements = MAX(EXIT_STATS_TOP_N_PORTS,
  1882. * #{j | 0 <= j <= i && volume(i) > 0})
  1883. *
  1884. * For all 0 <= j < top_elements,
  1885. * top_bytes[j] > 0
  1886. * 0 <= top_ports[j] <= 65535
  1887. * top_bytes[j] = volume(top_ports[j])
  1888. *
  1889. * There is no j in 0..i and k in 0..top_elements such that:
  1890. * volume(j) > top_bytes[k] AND j is not in top_ports[0..top_elements]
  1891. *
  1892. * There is no j!=cur_min_idx in 0..top_elements such that:
  1893. * top_bytes[j] < top_bytes[cur_min_idx]
  1894. *
  1895. * where volume(x) == exit_bytes_read[x]+exit_bytes_written[x]
  1896. *
  1897. * Worst case: O(EXIT_STATS_NUM_PORTS * EXIT_STATS_TOP_N_PORTS)
  1898. */
  1899. for (i = 1; i < EXIT_STATS_NUM_PORTS; i++) {
  1900. total_read += exit_bytes_read[i];
  1901. total_written += exit_bytes_written[i];
  1902. total_streams += exit_streams[i];
  1903. cur_bytes = exit_bytes_read[i] + exit_bytes_written[i];
  1904. if (cur_bytes == 0) {
  1905. continue;
  1906. }
  1907. if (top_elements < EXIT_STATS_TOP_N_PORTS) {
  1908. top_bytes[top_elements] = cur_bytes;
  1909. top_ports[top_elements++] = i;
  1910. } else if (cur_bytes > top_bytes[cur_min_idx]) {
  1911. top_bytes[cur_min_idx] = cur_bytes;
  1912. top_ports[cur_min_idx] = i;
  1913. } else {
  1914. continue;
  1915. }
  1916. cur_min_idx = 0;
  1917. for (j = 1; j < top_elements; j++) {
  1918. if (top_bytes[j] < top_bytes[cur_min_idx]) {
  1919. cur_min_idx = j;
  1920. }
  1921. }
  1922. }
  1923. /* Add observations of top ports to smartlists. */
  1924. written_strings = smartlist_new();
  1925. read_strings = smartlist_new();
  1926. streams_strings = smartlist_new();
  1927. other_read = total_read;
  1928. other_written = total_written;
  1929. other_streams = total_streams;
  1930. /* Sort the ports; this puts them out of sync with top_bytes, but we
  1931. * won't be using top_bytes again anyway */
  1932. qsort(top_ports, top_elements, sizeof(int), compare_int_);
  1933. for (j = 0; j < top_elements; j++) {
  1934. cur_port = top_ports[j];
  1935. if (exit_bytes_written[cur_port] > 0) {
  1936. uint64_t num = round_uint64_to_next_multiple_of(
  1937. exit_bytes_written[cur_port],
  1938. EXIT_STATS_ROUND_UP_BYTES);
  1939. num /= 1024;
  1940. smartlist_add_asprintf(written_strings, "%d="U64_FORMAT,
  1941. cur_port, U64_PRINTF_ARG(num));
  1942. other_written -= exit_bytes_written[cur_port];
  1943. }
  1944. if (exit_bytes_read[cur_port] > 0) {
  1945. uint64_t num = round_uint64_to_next_multiple_of(
  1946. exit_bytes_read[cur_port],
  1947. EXIT_STATS_ROUND_UP_BYTES);
  1948. num /= 1024;
  1949. smartlist_add_asprintf(read_strings, "%d="U64_FORMAT,
  1950. cur_port, U64_PRINTF_ARG(num));
  1951. other_read -= exit_bytes_read[cur_port];
  1952. }
  1953. if (exit_streams[cur_port] > 0) {
  1954. uint32_t num = round_uint32_to_next_multiple_of(
  1955. exit_streams[cur_port],
  1956. EXIT_STATS_ROUND_UP_STREAMS);
  1957. smartlist_add_asprintf(streams_strings, "%d=%u", cur_port, num);
  1958. other_streams -= exit_streams[cur_port];
  1959. }
  1960. }
  1961. /* Add observations of other ports in a single element. */
  1962. other_written = round_uint64_to_next_multiple_of(other_written,
  1963. EXIT_STATS_ROUND_UP_BYTES);
  1964. other_written /= 1024;
  1965. smartlist_add_asprintf(written_strings, "other="U64_FORMAT,
  1966. U64_PRINTF_ARG(other_written));
  1967. other_read = round_uint64_to_next_multiple_of(other_read,
  1968. EXIT_STATS_ROUND_UP_BYTES);
  1969. other_read /= 1024;
  1970. smartlist_add_asprintf(read_strings, "other="U64_FORMAT,
  1971. U64_PRINTF_ARG(other_read));
  1972. other_streams = round_uint32_to_next_multiple_of(other_streams,
  1973. EXIT_STATS_ROUND_UP_STREAMS);
  1974. smartlist_add_asprintf(streams_strings, "other=%u", other_streams);
  1975. /* Join all observations in single strings. */
  1976. written_string = smartlist_join_strings(written_strings, ",", 0, NULL);
  1977. read_string = smartlist_join_strings(read_strings, ",", 0, NULL);
  1978. streams_string = smartlist_join_strings(streams_strings, ",", 0, NULL);
  1979. SMARTLIST_FOREACH(written_strings, char *, cp, tor_free(cp));
  1980. SMARTLIST_FOREACH(read_strings, char *, cp, tor_free(cp));
  1981. SMARTLIST_FOREACH(streams_strings, char *, cp, tor_free(cp));
  1982. smartlist_free(written_strings);
  1983. smartlist_free(read_strings);
  1984. smartlist_free(streams_strings);
  1985. /* Put everything together. */
  1986. format_iso_time(t, now);
  1987. tor_asprintf(&result, "exit-stats-end %s (%d s)\n"
  1988. "exit-kibibytes-written %s\n"
  1989. "exit-kibibytes-read %s\n"
  1990. "exit-streams-opened %s\n",
  1991. t, (unsigned) (now - start_of_exit_stats_interval),
  1992. written_string,
  1993. read_string,
  1994. streams_string);
  1995. tor_free(written_string);
  1996. tor_free(read_string);
  1997. tor_free(streams_string);
  1998. return result;
  1999. }
  2000. /** If 24 hours have passed since the beginning of the current exit port
  2001. * stats period, write exit stats to $DATADIR/stats/exit-stats (possibly
  2002. * overwriting an existing file) and reset counters. Return when we would
  2003. * next want to write exit stats or 0 if we never want to write. */
  2004. time_t
  2005. rep_hist_exit_stats_write(time_t now)
  2006. {
  2007. char *str = NULL;
  2008. if (!start_of_exit_stats_interval)
  2009. return 0; /* Not initialized. */
  2010. if (start_of_exit_stats_interval + WRITE_STATS_INTERVAL > now)
  2011. goto done; /* Not ready to write. */
  2012. log_info(LD_HIST, "Writing exit port statistics to disk.");
  2013. /* Generate history string. */
  2014. str = rep_hist_format_exit_stats(now);
  2015. /* Reset counters. */
  2016. rep_hist_reset_exit_stats(now);
  2017. /* Try to write to disk. */
  2018. if (!check_or_create_data_subdir("stats")) {
  2019. write_to_data_subdir("stats", "exit-stats", str, "exit port statistics");
  2020. }
  2021. done:
  2022. tor_free(str);
  2023. return start_of_exit_stats_interval + WRITE_STATS_INTERVAL;
  2024. }
  2025. /** Note that we wrote <b>num_written</b> bytes and read <b>num_read</b>
  2026. * bytes to/from an exit connection to <b>port</b>. */
  2027. void
  2028. rep_hist_note_exit_bytes(uint16_t port, size_t num_written,
  2029. size_t num_read)
  2030. {
  2031. if (!start_of_exit_stats_interval)
  2032. return; /* Not initialized. */
  2033. exit_bytes_written[port] += num_written;
  2034. exit_bytes_read[port] += num_read;
  2035. log_debug(LD_HIST, "Written %lu bytes and read %lu bytes to/from an "
  2036. "exit connection to port %d.",
  2037. (unsigned long)num_written, (unsigned long)num_read, port);
  2038. }
  2039. /** Note that we opened an exit stream to <b>port</b>. */
  2040. void
  2041. rep_hist_note_exit_stream_opened(uint16_t port)
  2042. {
  2043. if (!start_of_exit_stats_interval)
  2044. return; /* Not initialized. */
  2045. exit_streams[port]++;
  2046. log_debug(LD_HIST, "Opened exit stream to port %d", port);
  2047. }
  2048. /*** cell statistics ***/
  2049. /** Start of the current buffer stats interval or 0 if we're not
  2050. * collecting buffer statistics. */
  2051. static time_t start_of_buffer_stats_interval;
  2052. /** Initialize buffer stats. */
  2053. void
  2054. rep_hist_buffer_stats_init(time_t now)
  2055. {
  2056. start_of_buffer_stats_interval = now;
  2057. }
  2058. /** Statistics from a single circuit. Collected when the circuit closes, or
  2059. * when we flush statistics to disk. */
  2060. typedef struct circ_buffer_stats_t {
  2061. /** Average number of cells in the circuit's queue */
  2062. double mean_num_cells_in_queue;
  2063. /** Average time a cell waits in the queue. */
  2064. double mean_time_cells_in_queue;
  2065. /** Total number of cells sent over this circuit */
  2066. uint32_t processed_cells;
  2067. } circ_buffer_stats_t;
  2068. /** List of circ_buffer_stats_t. */
  2069. static smartlist_t *circuits_for_buffer_stats = NULL;
  2070. /** Remember cell statistics <b>mean_num_cells_in_queue</b>,
  2071. * <b>mean_time_cells_in_queue</b>, and <b>processed_cells</b> of a
  2072. * circuit. */
  2073. void
  2074. rep_hist_add_buffer_stats(double mean_num_cells_in_queue,
  2075. double mean_time_cells_in_queue, uint32_t processed_cells)
  2076. {
  2077. circ_buffer_stats_t *stat;
  2078. if (!start_of_buffer_stats_interval)
  2079. return; /* Not initialized. */
  2080. stat = tor_malloc_zero(sizeof(circ_buffer_stats_t));
  2081. stat->mean_num_cells_in_queue = mean_num_cells_in_queue;
  2082. stat->mean_time_cells_in_queue = mean_time_cells_in_queue;
  2083. stat->processed_cells = processed_cells;
  2084. if (!circuits_for_buffer_stats)
  2085. circuits_for_buffer_stats = smartlist_new();
  2086. smartlist_add(circuits_for_buffer_stats, stat);
  2087. }
  2088. /** Remember cell statistics for circuit <b>circ</b> at time
  2089. * <b>end_of_interval</b> and reset cell counters in case the circuit
  2090. * remains open in the next measurement interval. */
  2091. void
  2092. rep_hist_buffer_stats_add_circ(circuit_t *circ, time_t end_of_interval)
  2093. {
  2094. time_t start_of_interval;
  2095. int interval_length;
  2096. or_circuit_t *orcirc;
  2097. double mean_num_cells_in_queue, mean_time_cells_in_queue;
  2098. uint32_t processed_cells;
  2099. if (CIRCUIT_IS_ORIGIN(circ))
  2100. return;
  2101. orcirc = TO_OR_CIRCUIT(circ);
  2102. if (!orcirc->processed_cells)
  2103. return;
  2104. start_of_interval = (circ->timestamp_created.tv_sec >
  2105. start_of_buffer_stats_interval) ?
  2106. (time_t)circ->timestamp_created.tv_sec :
  2107. start_of_buffer_stats_interval;
  2108. interval_length = (int) (end_of_interval - start_of_interval);
  2109. if (interval_length <= 0)
  2110. return;
  2111. processed_cells = orcirc->processed_cells;
  2112. /* 1000.0 for s -> ms; 2.0 because of app-ward and exit-ward queues */
  2113. mean_num_cells_in_queue = (double) orcirc->total_cell_waiting_time /
  2114. (double) interval_length / 1000.0 / 2.0;
  2115. mean_time_cells_in_queue =
  2116. (double) orcirc->total_cell_waiting_time /
  2117. (double) orcirc->processed_cells;
  2118. orcirc->total_cell_waiting_time = 0;
  2119. orcirc->processed_cells = 0;
  2120. rep_hist_add_buffer_stats(mean_num_cells_in_queue,
  2121. mean_time_cells_in_queue,
  2122. processed_cells);
  2123. }
  2124. /** Sorting helper: return -1, 1, or 0 based on comparison of two
  2125. * circ_buffer_stats_t */
  2126. static int
  2127. buffer_stats_compare_entries_(const void **_a, const void **_b)
  2128. {
  2129. const circ_buffer_stats_t *a = *_a, *b = *_b;
  2130. if (a->processed_cells < b->processed_cells)
  2131. return 1;
  2132. else if (a->processed_cells > b->processed_cells)
  2133. return -1;
  2134. else
  2135. return 0;
  2136. }
  2137. /** Stop collecting cell stats in a way that we can re-start doing so in
  2138. * rep_hist_buffer_stats_init(). */
  2139. void
  2140. rep_hist_buffer_stats_term(void)
  2141. {
  2142. rep_hist_reset_buffer_stats(0);
  2143. }
  2144. /** Clear history of circuit statistics and set the measurement interval
  2145. * start to <b>now</b>. */
  2146. void
  2147. rep_hist_reset_buffer_stats(time_t now)
  2148. {
  2149. if (!circuits_for_buffer_stats)
  2150. circuits_for_buffer_stats = smartlist_new();
  2151. SMARTLIST_FOREACH(circuits_for_buffer_stats, circ_buffer_stats_t *,
  2152. stat, tor_free(stat));
  2153. smartlist_clear(circuits_for_buffer_stats);
  2154. start_of_buffer_stats_interval = now;
  2155. }
  2156. /** Return a newly allocated string containing the buffer statistics until
  2157. * <b>now</b>, or NULL if we're not collecting buffer stats. Caller must
  2158. * ensure start_of_buffer_stats_interval is in the past. */
  2159. char *
  2160. rep_hist_format_buffer_stats(time_t now)
  2161. {
  2162. #define SHARES 10
  2163. uint64_t processed_cells[SHARES];
  2164. uint32_t circs_in_share[SHARES];
  2165. int number_of_circuits, i;
  2166. double queued_cells[SHARES], time_in_queue[SHARES];
  2167. smartlist_t *processed_cells_strings, *queued_cells_strings,
  2168. *time_in_queue_strings;
  2169. char *processed_cells_string, *queued_cells_string,
  2170. *time_in_queue_string;
  2171. char t[ISO_TIME_LEN+1];
  2172. char *result;
  2173. if (!start_of_buffer_stats_interval)
  2174. return NULL; /* Not initialized. */
  2175. tor_assert(now >= start_of_buffer_stats_interval);
  2176. /* Calculate deciles if we saw at least one circuit. */
  2177. memset(processed_cells, 0, SHARES * sizeof(uint64_t));
  2178. memset(circs_in_share, 0, SHARES * sizeof(uint32_t));
  2179. memset(queued_cells, 0, SHARES * sizeof(double));
  2180. memset(time_in_queue, 0, SHARES * sizeof(double));
  2181. if (!circuits_for_buffer_stats)
  2182. circuits_for_buffer_stats = smartlist_new();
  2183. number_of_circuits = smartlist_len(circuits_for_buffer_stats);
  2184. if (number_of_circuits > 0) {
  2185. smartlist_sort(circuits_for_buffer_stats,
  2186. buffer_stats_compare_entries_);
  2187. i = 0;
  2188. SMARTLIST_FOREACH_BEGIN(circuits_for_buffer_stats,
  2189. circ_buffer_stats_t *, stat)
  2190. {
  2191. int share = i++ * SHARES / number_of_circuits;
  2192. processed_cells[share] += stat->processed_cells;
  2193. queued_cells[share] += stat->mean_num_cells_in_queue;
  2194. time_in_queue[share] += stat->mean_time_cells_in_queue;
  2195. circs_in_share[share]++;
  2196. }
  2197. SMARTLIST_FOREACH_END(stat);
  2198. }
  2199. /* Write deciles to strings. */
  2200. processed_cells_strings = smartlist_new();
  2201. queued_cells_strings = smartlist_new();
  2202. time_in_queue_strings = smartlist_new();
  2203. for (i = 0; i < SHARES; i++) {
  2204. smartlist_add_asprintf(processed_cells_strings,
  2205. U64_FORMAT, !circs_in_share[i] ? 0 :
  2206. U64_PRINTF_ARG(processed_cells[i] /
  2207. circs_in_share[i]));
  2208. }
  2209. for (i = 0; i < SHARES; i++) {
  2210. smartlist_add_asprintf(queued_cells_strings, "%.2f",
  2211. circs_in_share[i] == 0 ? 0.0 :
  2212. queued_cells[i] / (double) circs_in_share[i]);
  2213. }
  2214. for (i = 0; i < SHARES; i++) {
  2215. smartlist_add_asprintf(time_in_queue_strings, "%.0f",
  2216. circs_in_share[i] == 0 ? 0.0 :
  2217. time_in_queue[i] / (double) circs_in_share[i]);
  2218. }
  2219. /* Join all observations in single strings. */
  2220. processed_cells_string = smartlist_join_strings(processed_cells_strings,
  2221. ",", 0, NULL);
  2222. queued_cells_string = smartlist_join_strings(queued_cells_strings,
  2223. ",", 0, NULL);
  2224. time_in_queue_string = smartlist_join_strings(time_in_queue_strings,
  2225. ",", 0, NULL);
  2226. SMARTLIST_FOREACH(processed_cells_strings, char *, cp, tor_free(cp));
  2227. SMARTLIST_FOREACH(queued_cells_strings, char *, cp, tor_free(cp));
  2228. SMARTLIST_FOREACH(time_in_queue_strings, char *, cp, tor_free(cp));
  2229. smartlist_free(processed_cells_strings);
  2230. smartlist_free(queued_cells_strings);
  2231. smartlist_free(time_in_queue_strings);
  2232. /* Put everything together. */
  2233. format_iso_time(t, now);
  2234. tor_asprintf(&result, "cell-stats-end %s (%d s)\n"
  2235. "cell-processed-cells %s\n"
  2236. "cell-queued-cells %s\n"
  2237. "cell-time-in-queue %s\n"
  2238. "cell-circuits-per-decile %d\n",
  2239. t, (unsigned) (now - start_of_buffer_stats_interval),
  2240. processed_cells_string,
  2241. queued_cells_string,
  2242. time_in_queue_string,
  2243. (number_of_circuits + SHARES - 1) / SHARES);
  2244. tor_free(processed_cells_string);
  2245. tor_free(queued_cells_string);
  2246. tor_free(time_in_queue_string);
  2247. return result;
  2248. #undef SHARES
  2249. }
  2250. /** If 24 hours have passed since the beginning of the current buffer
  2251. * stats period, write buffer stats to $DATADIR/stats/buffer-stats
  2252. * (possibly overwriting an existing file) and reset counters. Return
  2253. * when we would next want to write buffer stats or 0 if we never want to
  2254. * write. */
  2255. time_t
  2256. rep_hist_buffer_stats_write(time_t now)
  2257. {
  2258. char *str = NULL;
  2259. if (!start_of_buffer_stats_interval)
  2260. return 0; /* Not initialized. */
  2261. if (start_of_buffer_stats_interval + WRITE_STATS_INTERVAL > now)
  2262. goto done; /* Not ready to write */
  2263. /* Add open circuits to the history. */
  2264. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  2265. rep_hist_buffer_stats_add_circ(circ, now);
  2266. }
  2267. SMARTLIST_FOREACH_END(circ);
  2268. /* Generate history string. */
  2269. str = rep_hist_format_buffer_stats(now);
  2270. /* Reset both buffer history and counters of open circuits. */
  2271. rep_hist_reset_buffer_stats(now);
  2272. /* Try to write to disk. */
  2273. if (!check_or_create_data_subdir("stats")) {
  2274. write_to_data_subdir("stats", "buffer-stats", str, "buffer statistics");
  2275. }
  2276. done:
  2277. tor_free(str);
  2278. return start_of_buffer_stats_interval + WRITE_STATS_INTERVAL;
  2279. }
  2280. /*** Descriptor serving statistics ***/
  2281. /** Digestmap to track which descriptors were downloaded this stats
  2282. * collection interval. It maps descriptor digest to pointers to 1,
  2283. * effectively turning this into a list. */
  2284. static digestmap_t *served_descs = NULL;
  2285. /** Number of how many descriptors were downloaded in total during this
  2286. * interval. */
  2287. static unsigned long total_descriptor_downloads;
  2288. /** Start time of served descs stats or 0 if we're not collecting those. */
  2289. static time_t start_of_served_descs_stats_interval;
  2290. /** Initialize descriptor stats. */
  2291. void
  2292. rep_hist_desc_stats_init(time_t now)
  2293. {
  2294. if (served_descs) {
  2295. log_warn(LD_BUG, "Called rep_hist_desc_stats_init() when desc stats were "
  2296. "already initialized. This is probably harmless.");
  2297. return; // Already initialized
  2298. }
  2299. served_descs = digestmap_new();
  2300. total_descriptor_downloads = 0;
  2301. start_of_served_descs_stats_interval = now;
  2302. }
  2303. /** Reset served descs stats to empty, starting a new interval <b>now</b>. */
  2304. static void
  2305. rep_hist_reset_desc_stats(time_t now)
  2306. {
  2307. rep_hist_desc_stats_term();
  2308. rep_hist_desc_stats_init(now);
  2309. }
  2310. /** Stop collecting served descs stats, so that rep_hist_desc_stats_init() is
  2311. * safe to be called again. */
  2312. void
  2313. rep_hist_desc_stats_term(void)
  2314. {
  2315. digestmap_free(served_descs, NULL);
  2316. served_descs = NULL;
  2317. start_of_served_descs_stats_interval = 0;
  2318. total_descriptor_downloads = 0;
  2319. }
  2320. /** Helper for rep_hist_desc_stats_write(). Return a newly allocated string
  2321. * containing the served desc statistics until now, or NULL if we're not
  2322. * collecting served desc stats. Caller must ensure that now is not before
  2323. * start_of_served_descs_stats_interval. */
  2324. static char *
  2325. rep_hist_format_desc_stats(time_t now)
  2326. {
  2327. char t[ISO_TIME_LEN+1];
  2328. char *result;
  2329. digestmap_iter_t *iter;
  2330. const char *key;
  2331. void *val;
  2332. unsigned size;
  2333. int *vals, max = 0, q3 = 0, md = 0, q1 = 0, min = 0;
  2334. int n = 0;
  2335. if (!start_of_served_descs_stats_interval)
  2336. return NULL;
  2337. size = digestmap_size(served_descs);
  2338. if (size > 0) {
  2339. vals = tor_calloc(size, sizeof(int));
  2340. for (iter = digestmap_iter_init(served_descs);
  2341. !digestmap_iter_done(iter);
  2342. iter = digestmap_iter_next(served_descs, iter)) {
  2343. uintptr_t count;
  2344. digestmap_iter_get(iter, &key, &val);
  2345. count = (uintptr_t)val;
  2346. vals[n++] = (int)count;
  2347. (void)key;
  2348. }
  2349. max = find_nth_int(vals, size, size-1);
  2350. q3 = find_nth_int(vals, size, (3*size-1)/4);
  2351. md = find_nth_int(vals, size, (size-1)/2);
  2352. q1 = find_nth_int(vals, size, (size-1)/4);
  2353. min = find_nth_int(vals, size, 0);
  2354. tor_free(vals);
  2355. }
  2356. format_iso_time(t, now);
  2357. tor_asprintf(&result,
  2358. "served-descs-stats-end %s (%d s) total=%lu unique=%u "
  2359. "max=%d q3=%d md=%d q1=%d min=%d\n",
  2360. t,
  2361. (unsigned) (now - start_of_served_descs_stats_interval),
  2362. total_descriptor_downloads,
  2363. size, max, q3, md, q1, min);
  2364. return result;
  2365. }
  2366. /** If WRITE_STATS_INTERVAL seconds have passed since the beginning of
  2367. * the current served desc stats interval, write the stats to
  2368. * $DATADIR/stats/served-desc-stats (possibly appending to an existing file)
  2369. * and reset the state for the next interval. Return when we would next want
  2370. * to write served desc stats or 0 if we won't want to write. */
  2371. time_t
  2372. rep_hist_desc_stats_write(time_t now)
  2373. {
  2374. char *filename = NULL, *str = NULL;
  2375. if (!start_of_served_descs_stats_interval)
  2376. return 0; /* We're not collecting stats. */
  2377. if (start_of_served_descs_stats_interval + WRITE_STATS_INTERVAL > now)
  2378. return start_of_served_descs_stats_interval + WRITE_STATS_INTERVAL;
  2379. str = rep_hist_format_desc_stats(now);
  2380. tor_assert(str != NULL);
  2381. if (check_or_create_data_subdir("stats") < 0) {
  2382. goto done;
  2383. }
  2384. filename = get_datadir_fname2("stats", "served-desc-stats");
  2385. if (append_bytes_to_file(filename, str, strlen(str), 0) < 0)
  2386. log_warn(LD_HIST, "Unable to write served descs statistics to disk!");
  2387. rep_hist_reset_desc_stats(now);
  2388. done:
  2389. tor_free(filename);
  2390. tor_free(str);
  2391. return start_of_served_descs_stats_interval + WRITE_STATS_INTERVAL;
  2392. }
  2393. /* DOCDOC rep_hist_note_desc_served */
  2394. void
  2395. rep_hist_note_desc_served(const char * desc)
  2396. {
  2397. void *val;
  2398. uintptr_t count;
  2399. if (!served_descs)
  2400. return; // We're not collecting stats
  2401. val = digestmap_get(served_descs, desc);
  2402. count = (uintptr_t)val;
  2403. if (count != INT_MAX)
  2404. ++count;
  2405. digestmap_set(served_descs, desc, (void*)count);
  2406. total_descriptor_downloads++;
  2407. }
  2408. /*** Connection statistics ***/
  2409. /** Start of the current connection stats interval or 0 if we're not
  2410. * collecting connection statistics. */
  2411. static time_t start_of_conn_stats_interval;
  2412. /** Initialize connection stats. */
  2413. void
  2414. rep_hist_conn_stats_init(time_t now)
  2415. {
  2416. start_of_conn_stats_interval = now;
  2417. }
  2418. /* Count connections that we read and wrote less than these many bytes
  2419. * from/to as below threshold. */
  2420. #define BIDI_THRESHOLD 20480
  2421. /* Count connections that we read or wrote at least this factor as many
  2422. * bytes from/to than we wrote or read to/from as mostly reading or
  2423. * writing. */
  2424. #define BIDI_FACTOR 10
  2425. /* Interval length in seconds for considering read and written bytes for
  2426. * connection stats. */
  2427. #define BIDI_INTERVAL 10
  2428. /** Start of next BIDI_INTERVAL second interval. */
  2429. static time_t bidi_next_interval = 0;
  2430. /** Number of connections that we read and wrote less than BIDI_THRESHOLD
  2431. * bytes from/to in BIDI_INTERVAL seconds. */
  2432. static uint32_t below_threshold = 0;
  2433. /** Number of connections that we read at least BIDI_FACTOR times more
  2434. * bytes from than we wrote to in BIDI_INTERVAL seconds. */
  2435. static uint32_t mostly_read = 0;
  2436. /** Number of connections that we wrote at least BIDI_FACTOR times more
  2437. * bytes to than we read from in BIDI_INTERVAL seconds. */
  2438. static uint32_t mostly_written = 0;
  2439. /** Number of connections that we read and wrote at least BIDI_THRESHOLD
  2440. * bytes from/to, but not BIDI_FACTOR times more in either direction in
  2441. * BIDI_INTERVAL seconds. */
  2442. static uint32_t both_read_and_written = 0;
  2443. /** Entry in a map from connection ID to the number of read and written
  2444. * bytes on this connection in a BIDI_INTERVAL second interval. */
  2445. typedef struct bidi_map_entry_t {
  2446. HT_ENTRY(bidi_map_entry_t) node;
  2447. uint64_t conn_id; /**< Connection ID */
  2448. size_t read; /**< Number of read bytes */
  2449. size_t written; /**< Number of written bytes */
  2450. } bidi_map_entry_t;
  2451. /** Map of OR connections together with the number of read and written
  2452. * bytes in the current BIDI_INTERVAL second interval. */
  2453. static HT_HEAD(bidimap, bidi_map_entry_t) bidi_map =
  2454. HT_INITIALIZER();
  2455. static int
  2456. bidi_map_ent_eq(const bidi_map_entry_t *a, const bidi_map_entry_t *b)
  2457. {
  2458. return a->conn_id == b->conn_id;
  2459. }
  2460. /* DOCDOC bidi_map_ent_hash */
  2461. static unsigned
  2462. bidi_map_ent_hash(const bidi_map_entry_t *entry)
  2463. {
  2464. return (unsigned) entry->conn_id;
  2465. }
  2466. HT_PROTOTYPE(bidimap, bidi_map_entry_t, node, bidi_map_ent_hash,
  2467. bidi_map_ent_eq)
  2468. HT_GENERATE2(bidimap, bidi_map_entry_t, node, bidi_map_ent_hash,
  2469. bidi_map_ent_eq, 0.6, tor_reallocarray_, tor_free_)
  2470. /* DOCDOC bidi_map_free */
  2471. static void
  2472. bidi_map_free(void)
  2473. {
  2474. bidi_map_entry_t **ptr, **next, *ent;
  2475. for (ptr = HT_START(bidimap, &bidi_map); ptr; ptr = next) {
  2476. ent = *ptr;
  2477. next = HT_NEXT_RMV(bidimap, &bidi_map, ptr);
  2478. tor_free(ent);
  2479. }
  2480. HT_CLEAR(bidimap, &bidi_map);
  2481. }
  2482. /** Reset counters for conn statistics. */
  2483. void
  2484. rep_hist_reset_conn_stats(time_t now)
  2485. {
  2486. start_of_conn_stats_interval = now;
  2487. below_threshold = 0;
  2488. mostly_read = 0;
  2489. mostly_written = 0;
  2490. both_read_and_written = 0;
  2491. bidi_map_free();
  2492. }
  2493. /** Stop collecting connection stats in a way that we can re-start doing
  2494. * so in rep_hist_conn_stats_init(). */
  2495. void
  2496. rep_hist_conn_stats_term(void)
  2497. {
  2498. rep_hist_reset_conn_stats(0);
  2499. }
  2500. /** We read <b>num_read</b> bytes and wrote <b>num_written</b> from/to OR
  2501. * connection <b>conn_id</b> in second <b>when</b>. If this is the first
  2502. * observation in a new interval, sum up the last observations. Add bytes
  2503. * for this connection. */
  2504. void
  2505. rep_hist_note_or_conn_bytes(uint64_t conn_id, size_t num_read,
  2506. size_t num_written, time_t when)
  2507. {
  2508. if (!start_of_conn_stats_interval)
  2509. return;
  2510. /* Initialize */
  2511. if (bidi_next_interval == 0)
  2512. bidi_next_interval = when + BIDI_INTERVAL;
  2513. /* Sum up last period's statistics */
  2514. if (when >= bidi_next_interval) {
  2515. bidi_map_entry_t **ptr, **next, *ent;
  2516. for (ptr = HT_START(bidimap, &bidi_map); ptr; ptr = next) {
  2517. ent = *ptr;
  2518. if (ent->read + ent->written < BIDI_THRESHOLD)
  2519. below_threshold++;
  2520. else if (ent->read >= ent->written * BIDI_FACTOR)
  2521. mostly_read++;
  2522. else if (ent->written >= ent->read * BIDI_FACTOR)
  2523. mostly_written++;
  2524. else
  2525. both_read_and_written++;
  2526. next = HT_NEXT_RMV(bidimap, &bidi_map, ptr);
  2527. tor_free(ent);
  2528. }
  2529. while (when >= bidi_next_interval)
  2530. bidi_next_interval += BIDI_INTERVAL;
  2531. log_info(LD_GENERAL, "%d below threshold, %d mostly read, "
  2532. "%d mostly written, %d both read and written.",
  2533. below_threshold, mostly_read, mostly_written,
  2534. both_read_and_written);
  2535. }
  2536. /* Add this connection's bytes. */
  2537. if (num_read > 0 || num_written > 0) {
  2538. bidi_map_entry_t *entry, lookup;
  2539. lookup.conn_id = conn_id;
  2540. entry = HT_FIND(bidimap, &bidi_map, &lookup);
  2541. if (entry) {
  2542. entry->written += num_written;
  2543. entry->read += num_read;
  2544. } else {
  2545. entry = tor_malloc_zero(sizeof(bidi_map_entry_t));
  2546. entry->conn_id = conn_id;
  2547. entry->written = num_written;
  2548. entry->read = num_read;
  2549. HT_INSERT(bidimap, &bidi_map, entry);
  2550. }
  2551. }
  2552. }
  2553. /** Return a newly allocated string containing the connection statistics
  2554. * until <b>now</b>, or NULL if we're not collecting conn stats. Caller must
  2555. * ensure start_of_conn_stats_interval is in the past. */
  2556. char *
  2557. rep_hist_format_conn_stats(time_t now)
  2558. {
  2559. char *result, written[ISO_TIME_LEN+1];
  2560. if (!start_of_conn_stats_interval)
  2561. return NULL; /* Not initialized. */
  2562. tor_assert(now >= start_of_conn_stats_interval);
  2563. format_iso_time(written, now);
  2564. tor_asprintf(&result, "conn-bi-direct %s (%d s) %d,%d,%d,%d\n",
  2565. written,
  2566. (unsigned) (now - start_of_conn_stats_interval),
  2567. below_threshold,
  2568. mostly_read,
  2569. mostly_written,
  2570. both_read_and_written);
  2571. return result;
  2572. }
  2573. /** If 24 hours have passed since the beginning of the current conn stats
  2574. * period, write conn stats to $DATADIR/stats/conn-stats (possibly
  2575. * overwriting an existing file) and reset counters. Return when we would
  2576. * next want to write conn stats or 0 if we never want to write. */
  2577. time_t
  2578. rep_hist_conn_stats_write(time_t now)
  2579. {
  2580. char *str = NULL;
  2581. if (!start_of_conn_stats_interval)
  2582. return 0; /* Not initialized. */
  2583. if (start_of_conn_stats_interval + WRITE_STATS_INTERVAL > now)
  2584. goto done; /* Not ready to write */
  2585. /* Generate history string. */
  2586. str = rep_hist_format_conn_stats(now);
  2587. /* Reset counters. */
  2588. rep_hist_reset_conn_stats(now);
  2589. /* Try to write to disk. */
  2590. if (!check_or_create_data_subdir("stats")) {
  2591. write_to_data_subdir("stats", "conn-stats", str, "connection statistics");
  2592. }
  2593. done:
  2594. tor_free(str);
  2595. return start_of_conn_stats_interval + WRITE_STATS_INTERVAL;
  2596. }
  2597. /** Internal statistics to track how many requests of each type of
  2598. * handshake we've received, and how many we've assigned to cpuworkers.
  2599. * Useful for seeing trends in cpu load.
  2600. * @{ */
  2601. STATIC int onion_handshakes_requested[MAX_ONION_HANDSHAKE_TYPE+1] = {0};
  2602. STATIC int onion_handshakes_assigned[MAX_ONION_HANDSHAKE_TYPE+1] = {0};
  2603. /**@}*/
  2604. /** A new onionskin (using the <b>type</b> handshake) has arrived. */
  2605. void
  2606. rep_hist_note_circuit_handshake_requested(uint16_t type)
  2607. {
  2608. if (type <= MAX_ONION_HANDSHAKE_TYPE)
  2609. onion_handshakes_requested[type]++;
  2610. }
  2611. /** We've sent an onionskin (using the <b>type</b> handshake) to a
  2612. * cpuworker. */
  2613. void
  2614. rep_hist_note_circuit_handshake_assigned(uint16_t type)
  2615. {
  2616. if (type <= MAX_ONION_HANDSHAKE_TYPE)
  2617. onion_handshakes_assigned[type]++;
  2618. }
  2619. /** Log our onionskin statistics since the last time we were called. */
  2620. void
  2621. rep_hist_log_circuit_handshake_stats(time_t now)
  2622. {
  2623. (void)now;
  2624. log_notice(LD_HEARTBEAT, "Circuit handshake stats since last time: "
  2625. "%d/%d TAP, %d/%d NTor.",
  2626. onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_TAP],
  2627. onion_handshakes_requested[ONION_HANDSHAKE_TYPE_TAP],
  2628. onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_NTOR],
  2629. onion_handshakes_requested[ONION_HANDSHAKE_TYPE_NTOR]);
  2630. memset(onion_handshakes_assigned, 0, sizeof(onion_handshakes_assigned));
  2631. memset(onion_handshakes_requested, 0, sizeof(onion_handshakes_requested));
  2632. }
  2633. /* Hidden service statistics section */
  2634. /** Start of the current hidden service stats interval or 0 if we're
  2635. * not collecting hidden service statistics. */
  2636. static time_t start_of_hs_stats_interval;
  2637. /** Carries the various hidden service statistics, and any other
  2638. * information needed. */
  2639. typedef struct hs_stats_t {
  2640. /** How many relay cells have we seen as rendezvous points? */
  2641. uint64_t rp_relay_cells_seen;
  2642. /** Set of unique public key digests we've seen this stat period
  2643. * (could also be implemented as sorted smartlist). */
  2644. digestmap_t *onions_seen_this_period;
  2645. } hs_stats_t;
  2646. /** Our statistics structure singleton. */
  2647. static hs_stats_t *hs_stats = NULL;
  2648. /** Allocate, initialize and return an hs_stats_t structure. */
  2649. static hs_stats_t *
  2650. hs_stats_new(void)
  2651. {
  2652. hs_stats_t * hs_stats = tor_malloc_zero(sizeof(hs_stats_t));
  2653. hs_stats->onions_seen_this_period = digestmap_new();
  2654. return hs_stats;
  2655. }
  2656. /** Free an hs_stats_t structure. */
  2657. static void
  2658. hs_stats_free(hs_stats_t *hs_stats)
  2659. {
  2660. if (!hs_stats) {
  2661. return;
  2662. }
  2663. digestmap_free(hs_stats->onions_seen_this_period, NULL);
  2664. tor_free(hs_stats);
  2665. }
  2666. /** Initialize hidden service statistics. */
  2667. void
  2668. rep_hist_hs_stats_init(time_t now)
  2669. {
  2670. if (!hs_stats) {
  2671. hs_stats = hs_stats_new();
  2672. }
  2673. start_of_hs_stats_interval = now;
  2674. }
  2675. /** Clear history of hidden service statistics and set the measurement
  2676. * interval start to <b>now</b>. */
  2677. static void
  2678. rep_hist_reset_hs_stats(time_t now)
  2679. {
  2680. if (!hs_stats) {
  2681. hs_stats = hs_stats_new();
  2682. }
  2683. hs_stats->rp_relay_cells_seen = 0;
  2684. digestmap_free(hs_stats->onions_seen_this_period, NULL);
  2685. hs_stats->onions_seen_this_period = digestmap_new();
  2686. start_of_hs_stats_interval = now;
  2687. }
  2688. /** Stop collecting hidden service stats in a way that we can re-start
  2689. * doing so in rep_hist_buffer_stats_init(). */
  2690. void
  2691. rep_hist_hs_stats_term(void)
  2692. {
  2693. rep_hist_reset_hs_stats(0);
  2694. }
  2695. /** We saw a new HS relay cell, Count it! */
  2696. void
  2697. rep_hist_seen_new_rp_cell(void)
  2698. {
  2699. if (!hs_stats) {
  2700. return; // We're not collecting stats
  2701. }
  2702. hs_stats->rp_relay_cells_seen++;
  2703. }
  2704. /** As HSDirs, we saw another hidden service with public key
  2705. * <b>pubkey</b>. Check whether we have counted it before, if not
  2706. * count it now! */
  2707. void
  2708. rep_hist_stored_maybe_new_hs(const crypto_pk_t *pubkey)
  2709. {
  2710. char pubkey_hash[DIGEST_LEN];
  2711. if (!hs_stats) {
  2712. return; // We're not collecting stats
  2713. }
  2714. /* Get the digest of the pubkey which will be used to detect whether
  2715. we've seen this hidden service before or not. */
  2716. if (crypto_pk_get_digest(pubkey, pubkey_hash) < 0) {
  2717. /* This fail should not happen; key has been validated by
  2718. descriptor parsing code first. */
  2719. return;
  2720. }
  2721. /* Check if this is the first time we've seen this hidden
  2722. service. If it is, count it as new. */
  2723. if (!digestmap_get(hs_stats->onions_seen_this_period,
  2724. pubkey_hash)) {
  2725. digestmap_set(hs_stats->onions_seen_this_period,
  2726. pubkey_hash, (void*)(uintptr_t)1);
  2727. }
  2728. }
  2729. /* The number of cells that are supposed to be hidden from the adversary
  2730. * by adding noise from the Laplace distribution. This value, divided by
  2731. * EPSILON, is Laplace parameter b. It must be greather than 0. */
  2732. #define REND_CELLS_DELTA_F 2048
  2733. /* Security parameter for obfuscating number of cells with a value between
  2734. * ]0.0, 1.0]. Smaller values obfuscate observations more, but at the same
  2735. * time make statistics less usable. */
  2736. #define REND_CELLS_EPSILON 0.3
  2737. /* The number of cells that are supposed to be hidden from the adversary
  2738. * by rounding up to the next multiple of this number. */
  2739. #define REND_CELLS_BIN_SIZE 1024
  2740. /* The number of service identities that are supposed to be hidden from the
  2741. * adversary by adding noise from the Laplace distribution. This value,
  2742. * divided by EPSILON, is Laplace parameter b. It must be greater than 0. */
  2743. #define ONIONS_SEEN_DELTA_F 8
  2744. /* Security parameter for obfuscating number of service identities with a
  2745. * value between ]0.0, 1.0]. Smaller values obfuscate observations more, but
  2746. * at the same time make statistics less usable. */
  2747. #define ONIONS_SEEN_EPSILON 0.3
  2748. /* The number of service identities that are supposed to be hidden from
  2749. * the adversary by rounding up to the next multiple of this number. */
  2750. #define ONIONS_SEEN_BIN_SIZE 8
  2751. /** Allocate and return a string containing hidden service stats that
  2752. * are meant to be placed in the extra-info descriptor. */
  2753. static char *
  2754. rep_hist_format_hs_stats(time_t now)
  2755. {
  2756. char t[ISO_TIME_LEN+1];
  2757. char *hs_stats_string;
  2758. int64_t obfuscated_cells_seen;
  2759. int64_t obfuscated_onions_seen;
  2760. uint64_t rounded_cells_seen
  2761. = round_uint64_to_next_multiple_of(hs_stats->rp_relay_cells_seen,
  2762. REND_CELLS_BIN_SIZE);
  2763. rounded_cells_seen = MIN(rounded_cells_seen, INT64_MAX);
  2764. obfuscated_cells_seen = add_laplace_noise((int64_t)rounded_cells_seen,
  2765. crypto_rand_double(),
  2766. REND_CELLS_DELTA_F, REND_CELLS_EPSILON);
  2767. uint64_t rounded_onions_seen =
  2768. round_uint64_to_next_multiple_of((size_t)digestmap_size(
  2769. hs_stats->onions_seen_this_period),
  2770. ONIONS_SEEN_BIN_SIZE);
  2771. rounded_onions_seen = MIN(rounded_onions_seen, INT64_MAX);
  2772. obfuscated_onions_seen = add_laplace_noise((int64_t)rounded_onions_seen,
  2773. crypto_rand_double(), ONIONS_SEEN_DELTA_F,
  2774. ONIONS_SEEN_EPSILON);
  2775. format_iso_time(t, now);
  2776. tor_asprintf(&hs_stats_string, "hidserv-stats-end %s (%d s)\n"
  2777. "hidserv-rend-relayed-cells "I64_FORMAT" delta_f=%d "
  2778. "epsilon=%.2f bin_size=%d\n"
  2779. "hidserv-dir-onions-seen "I64_FORMAT" delta_f=%d "
  2780. "epsilon=%.2f bin_size=%d\n",
  2781. t, (unsigned) (now - start_of_hs_stats_interval),
  2782. I64_PRINTF_ARG(obfuscated_cells_seen), REND_CELLS_DELTA_F,
  2783. REND_CELLS_EPSILON, REND_CELLS_BIN_SIZE,
  2784. I64_PRINTF_ARG(obfuscated_onions_seen),
  2785. ONIONS_SEEN_DELTA_F,
  2786. ONIONS_SEEN_EPSILON, ONIONS_SEEN_BIN_SIZE);
  2787. return hs_stats_string;
  2788. }
  2789. /** If 24 hours have passed since the beginning of the current HS
  2790. * stats period, write buffer stats to $DATADIR/stats/hidserv-stats
  2791. * (possibly overwriting an existing file) and reset counters. Return
  2792. * when we would next want to write buffer stats or 0 if we never want to
  2793. * write. */
  2794. time_t
  2795. rep_hist_hs_stats_write(time_t now)
  2796. {
  2797. char *str = NULL;
  2798. if (!start_of_hs_stats_interval) {
  2799. return 0; /* Not initialized. */
  2800. }
  2801. if (start_of_hs_stats_interval + WRITE_STATS_INTERVAL > now) {
  2802. goto done; /* Not ready to write */
  2803. }
  2804. /* Generate history string. */
  2805. str = rep_hist_format_hs_stats(now);
  2806. /* Reset HS history. */
  2807. rep_hist_reset_hs_stats(now);
  2808. /* Try to write to disk. */
  2809. if (!check_or_create_data_subdir("stats")) {
  2810. write_to_data_subdir("stats", "hidserv-stats", str,
  2811. "hidden service stats");
  2812. }
  2813. done:
  2814. tor_free(str);
  2815. return start_of_hs_stats_interval + WRITE_STATS_INTERVAL;
  2816. }
  2817. #define MAX_LINK_PROTO_TO_LOG 4
  2818. static uint64_t link_proto_count[MAX_LINK_PROTO_TO_LOG+1][2];
  2819. /** Note that we negotiated link protocol version <b>link_proto</b>, on
  2820. * a connection that started here iff <b>started_here</b> is true.
  2821. */
  2822. void
  2823. rep_hist_note_negotiated_link_proto(unsigned link_proto, int started_here)
  2824. {
  2825. started_here = !!started_here; /* force to 0 or 1 */
  2826. if (link_proto > MAX_LINK_PROTO_TO_LOG) {
  2827. log_warn(LD_BUG, "Can't log link protocol %u", link_proto);
  2828. return;
  2829. }
  2830. link_proto_count[link_proto][started_here]++;
  2831. }
  2832. /** Log a heartbeat message explaining how many connections of each link
  2833. * protocol version we have used.
  2834. */
  2835. void
  2836. rep_hist_log_link_protocol_counts(void)
  2837. {
  2838. log_notice(LD_HEARTBEAT,
  2839. "Since startup, we have initiated "
  2840. U64_FORMAT" v1 connections, "
  2841. U64_FORMAT" v2 connections, "
  2842. U64_FORMAT" v3 connections, and "
  2843. U64_FORMAT" v4 connections; and received "
  2844. U64_FORMAT" v1 connections, "
  2845. U64_FORMAT" v2 connections, "
  2846. U64_FORMAT" v3 connections, and "
  2847. U64_FORMAT" v4 connections.",
  2848. U64_PRINTF_ARG(link_proto_count[1][1]),
  2849. U64_PRINTF_ARG(link_proto_count[2][1]),
  2850. U64_PRINTF_ARG(link_proto_count[3][1]),
  2851. U64_PRINTF_ARG(link_proto_count[4][1]),
  2852. U64_PRINTF_ARG(link_proto_count[1][0]),
  2853. U64_PRINTF_ARG(link_proto_count[2][0]),
  2854. U64_PRINTF_ARG(link_proto_count[3][0]),
  2855. U64_PRINTF_ARG(link_proto_count[4][0]));
  2856. }
  2857. /** Free all storage held by the OR/link history caches, by the
  2858. * bandwidth history arrays, by the port history, or by statistics . */
  2859. void
  2860. rep_hist_free_all(void)
  2861. {
  2862. hs_stats_free(hs_stats);
  2863. digestmap_free(history_map, free_or_history);
  2864. bw_array_free(read_array);
  2865. read_array = NULL;
  2866. bw_array_free(write_array);
  2867. write_array = NULL;
  2868. bw_array_free(dir_read_array);
  2869. dir_read_array = NULL;
  2870. bw_array_free(dir_write_array);
  2871. dir_write_array = NULL;
  2872. tor_free(exit_bytes_read);
  2873. tor_free(exit_bytes_written);
  2874. tor_free(exit_streams);
  2875. predicted_ports_free();
  2876. bidi_map_free();
  2877. if (circuits_for_buffer_stats) {
  2878. SMARTLIST_FOREACH(circuits_for_buffer_stats, circ_buffer_stats_t *, s,
  2879. tor_free(s));
  2880. smartlist_free(circuits_for_buffer_stats);
  2881. circuits_for_buffer_stats = NULL;
  2882. }
  2883. rep_hist_desc_stats_term();
  2884. total_descriptor_downloads = 0;
  2885. tor_assert_nonfatal(rephist_total_alloc == 0);
  2886. tor_assert_nonfatal_once(rephist_total_num == 0);
  2887. }