rephist.c 112 KB

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