gnunet-rps-profiler.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2012 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file rps/test_rps.c
  18. * @brief Testcase for the random peer sampling service. Starts
  19. * a peergroup with a given number of peers, then waits to
  20. * receive size pushes/pulls from each peer. Expects to wait
  21. * for one message from each peer.
  22. */
  23. #include "platform.h"
  24. //#include "rps_test_lib.h"
  25. #include "gnunet_util_lib.h"
  26. #include "gnunet_testbed_service.h"
  27. #include "gnunet_rps_service.h"
  28. #include "rps-test_util.h"
  29. #include "gnunet-service-rps_sampler_elem.h"
  30. #include <inttypes.h>
  31. #define BIT(n) (1 << (n))
  32. /**
  33. * How many peers do we start?
  34. */
  35. static uint32_t num_peers;
  36. /**
  37. * @brief numer of bits required to represent the largest peer id
  38. */
  39. static unsigned bits_needed;
  40. /**
  41. * How long do we run the test?
  42. */
  43. static struct GNUNET_TIME_Relative duration;
  44. /**
  45. * When do we do a hard shutdown?
  46. */
  47. static struct GNUNET_TIME_Relative timeout;
  48. /**
  49. * Portion of malicious peers
  50. */
  51. static double portion = .1;
  52. /**
  53. * Type of malicious peer to test
  54. */
  55. static unsigned int mal_type = 0;
  56. /**
  57. * Handles to all of the running peers
  58. */
  59. static struct GNUNET_TESTBED_Peer **testbed_peers;
  60. enum STAT_TYPE
  61. {
  62. STAT_TYPE_ROUNDS, /* 0 */
  63. STAT_TYPE_BLOCKS, /* 1 */
  64. STAT_TYPE_BLOCKS_MANY_PUSH, /* 2 */
  65. STAT_TYPE_BLOCKS_NO_PUSH, /* 3 */
  66. STAT_TYPE_BLOCKS_NO_PULL, /* 4 */
  67. STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL, /* 5 */
  68. STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL, /* 6 */
  69. STAT_TYPE_ISSUED_PUSH_SEND, /* 7 */
  70. STAT_TYPE_ISSUED_PUSH_SEND_MH, /* 8 */
  71. STAT_TYPE_ISSUED_PULL_REQ, /* 9 */
  72. STAT_TYPE_ISSUED_PULL_REQ_MH, /* 10 */
  73. STAT_TYPE_ISSUED_PULL_REP, /* 11 */
  74. STAT_TYPE_SENT_PUSH_SEND, /* 12 */
  75. STAT_TYPE_SENT_PULL_REQ, /* 13 */
  76. STAT_TYPE_SENT_PULL_REQ_MH, /* 14 */
  77. STAT_TYPE_SENT_PULL_REP, /* 15 */
  78. STAT_TYPE_RECV_PUSH_SEND, /* 16 */
  79. STAT_TYPE_RECV_PUSH_SEND_MH, /* 17 */
  80. STAT_TYPE_RECV_PULL_REQ, /* 18 */
  81. STAT_TYPE_RECV_PULL_REQ_MH, /* 19 */
  82. STAT_TYPE_RECV_PULL_REP, /* 20 */
  83. STAT_TYPE_RECV_PULL_REP_MH, /* 21 */
  84. STAT_TYPE_VIEW_SIZE, /* 22 */
  85. STAT_TYPE_KNOWN_PEERS, /* 23 */
  86. STAT_TYPE_VALID_PEERS, /* 24 */
  87. STAT_TYPE_LEARND_PEERS, /* 25 */
  88. STAT_TYPE_PENDING_ONLINE_CHECKS, /* 26 */
  89. STAT_TYPE_UNREQUESTED_PULL_REPLIES, /* 27 */
  90. STAT_TYPE_PEERS_IN_PUSH_MAP, /* 28 */
  91. STAT_TYPE_PEERS_IN_PULL_MAP, /* 29 */
  92. STAT_TYPE_PEERS_IN_VIEW, /* 30 */
  93. STAT_TYPE_VIEW_SIZE_AIM, /* 31 */
  94. STAT_TYPE_MAX, /* 32 */
  95. };
  96. static char* stat_type_strings[] = {
  97. "# rounds",
  98. "# rounds blocked",
  99. "# rounds blocked - too many pushes",
  100. "# rounds blocked - no pushes",
  101. "# rounds blocked - no pull replies",
  102. "# rounds blocked - too many pushes, no pull replies",
  103. "# rounds blocked - no pushes, no pull replies",
  104. "# push send issued",
  105. "# push send issued (multi-hop peer)",
  106. "# pull request send issued",
  107. "# pull request send issued (multi-hop peer)",
  108. "# pull reply send issued",
  109. "# pushes sent",
  110. "# pull requests sent",
  111. "# pull requests sent (multi-hop peer)",
  112. "# pull replys sent",
  113. "# push message received",
  114. "# push message received (multi-hop peer)",
  115. "# pull request message received",
  116. "# pull request message received (multi-hop peer)",
  117. "# pull reply messages received",
  118. "# pull reply messages received (multi-hop peer)",
  119. "view size",
  120. "# known peers",
  121. "# valid peers",
  122. "# learnd peers",
  123. "# pending online checks",
  124. "# unrequested pull replies",
  125. "# peers in push map at end of round",
  126. "# peers in pull map at end of round",
  127. "# peers in view at end of round",
  128. "view size aim",
  129. };
  130. struct STATcls
  131. {
  132. struct RPSPeer *rps_peer;
  133. enum STAT_TYPE stat_type;
  134. };
  135. /**
  136. * @brief Converts string representation to the corresponding #STAT_TYPE enum.
  137. *
  138. * @param stat_str string representation of statistics specifier
  139. *
  140. * @return corresponding enum
  141. */
  142. enum STAT_TYPE stat_str_2_type (const char *stat_str)
  143. {
  144. if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS_NO_PULL],
  145. stat_str,
  146. strlen (stat_type_strings[STAT_TYPE_BLOCKS_NO_PULL])))
  147. {
  148. return STAT_TYPE_BLOCKS_NO_PULL;
  149. }
  150. else if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL],
  151. stat_str,
  152. strlen (stat_type_strings[STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL])))
  153. {
  154. return STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL;
  155. }
  156. else if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS_MANY_PUSH],
  157. stat_str,
  158. strlen (stat_type_strings[STAT_TYPE_BLOCKS_MANY_PUSH])))
  159. {
  160. return STAT_TYPE_BLOCKS_MANY_PUSH;
  161. }
  162. else if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL],
  163. stat_str,
  164. strlen (stat_type_strings[STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL])))
  165. {
  166. return STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL;
  167. }
  168. else if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS_NO_PUSH],
  169. stat_str,
  170. strlen (stat_type_strings[STAT_TYPE_BLOCKS_NO_PUSH])))
  171. {
  172. return STAT_TYPE_BLOCKS_NO_PUSH;
  173. }
  174. else if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS],
  175. stat_str,
  176. strlen (stat_type_strings[STAT_TYPE_BLOCKS])))
  177. {
  178. return STAT_TYPE_BLOCKS;
  179. }
  180. else if (0 == strncmp (stat_type_strings[STAT_TYPE_ROUNDS],
  181. stat_str,
  182. strlen (stat_type_strings[STAT_TYPE_ROUNDS])))
  183. {
  184. return STAT_TYPE_ROUNDS;
  185. }
  186. else if (0 == strncmp (stat_type_strings[STAT_TYPE_ISSUED_PUSH_SEND],
  187. stat_str,
  188. strlen (stat_type_strings[STAT_TYPE_ISSUED_PUSH_SEND])))
  189. {
  190. return STAT_TYPE_ISSUED_PUSH_SEND;
  191. }
  192. else if (0 == strncmp (stat_type_strings[STAT_TYPE_ISSUED_PUSH_SEND_MH],
  193. stat_str,
  194. strlen (stat_type_strings[STAT_TYPE_ISSUED_PUSH_SEND_MH])))
  195. {
  196. return STAT_TYPE_ISSUED_PUSH_SEND_MH;
  197. }
  198. else if (0 == strncmp (stat_type_strings[STAT_TYPE_ISSUED_PULL_REQ],
  199. stat_str,
  200. strlen (stat_type_strings[STAT_TYPE_ISSUED_PULL_REQ])))
  201. {
  202. return STAT_TYPE_ISSUED_PULL_REQ;
  203. }
  204. else if (0 == strncmp (stat_type_strings[STAT_TYPE_ISSUED_PULL_REQ_MH],
  205. stat_str,
  206. strlen (stat_type_strings[STAT_TYPE_ISSUED_PULL_REQ_MH])))
  207. {
  208. return STAT_TYPE_ISSUED_PULL_REQ_MH;
  209. }
  210. else if (0 == strncmp (stat_type_strings[STAT_TYPE_ISSUED_PULL_REP],
  211. stat_str,
  212. strlen (stat_type_strings[STAT_TYPE_ISSUED_PULL_REP])))
  213. {
  214. return STAT_TYPE_ISSUED_PULL_REP;
  215. }
  216. else if (0 == strncmp (stat_type_strings[STAT_TYPE_SENT_PUSH_SEND],
  217. stat_str,
  218. strlen (stat_type_strings[STAT_TYPE_SENT_PUSH_SEND])))
  219. {
  220. return STAT_TYPE_SENT_PUSH_SEND;
  221. }
  222. else if (0 == strncmp (stat_type_strings[STAT_TYPE_SENT_PULL_REQ],
  223. stat_str,
  224. strlen (stat_type_strings[STAT_TYPE_SENT_PULL_REQ])))
  225. {
  226. return STAT_TYPE_SENT_PULL_REQ;
  227. }
  228. else if (0 == strncmp (stat_type_strings[STAT_TYPE_SENT_PULL_REQ_MH],
  229. stat_str,
  230. strlen (stat_type_strings[STAT_TYPE_SENT_PULL_REQ_MH])))
  231. {
  232. return STAT_TYPE_SENT_PULL_REQ_MH;
  233. }
  234. else if (0 == strncmp (stat_type_strings[STAT_TYPE_SENT_PULL_REP],
  235. stat_str,
  236. strlen (stat_type_strings[STAT_TYPE_SENT_PULL_REP])))
  237. {
  238. return STAT_TYPE_SENT_PULL_REP;
  239. }
  240. else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PUSH_SEND],
  241. stat_str,
  242. strlen (stat_type_strings[STAT_TYPE_RECV_PUSH_SEND])))
  243. {
  244. return STAT_TYPE_RECV_PUSH_SEND;
  245. }
  246. else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PUSH_SEND_MH],
  247. stat_str,
  248. strlen (stat_type_strings[STAT_TYPE_RECV_PUSH_SEND_MH])))
  249. {
  250. return STAT_TYPE_RECV_PUSH_SEND_MH;
  251. }
  252. else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PULL_REQ],
  253. stat_str,
  254. strlen (stat_type_strings[STAT_TYPE_RECV_PULL_REQ])))
  255. {
  256. return STAT_TYPE_RECV_PULL_REQ;
  257. }
  258. else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PULL_REQ_MH],
  259. stat_str,
  260. strlen (stat_type_strings[STAT_TYPE_RECV_PULL_REQ_MH])))
  261. {
  262. return STAT_TYPE_RECV_PULL_REQ_MH;
  263. }
  264. else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PULL_REP],
  265. stat_str,
  266. strlen (stat_type_strings[STAT_TYPE_RECV_PULL_REP])))
  267. {
  268. return STAT_TYPE_RECV_PULL_REP;
  269. }
  270. else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PULL_REP_MH],
  271. stat_str,
  272. strlen (stat_type_strings[STAT_TYPE_RECV_PULL_REP_MH])))
  273. {
  274. return STAT_TYPE_RECV_PULL_REP_MH;
  275. }
  276. else if (0 == strncmp (stat_type_strings[STAT_TYPE_VIEW_SIZE],
  277. stat_str,
  278. strlen (stat_type_strings[STAT_TYPE_VIEW_SIZE])))
  279. {
  280. return STAT_TYPE_VIEW_SIZE;
  281. }
  282. else if (0 == strncmp (stat_type_strings[STAT_TYPE_KNOWN_PEERS],
  283. stat_str,
  284. strlen (stat_type_strings[STAT_TYPE_KNOWN_PEERS])))
  285. {
  286. return STAT_TYPE_KNOWN_PEERS;
  287. }
  288. else if (0 == strncmp (stat_type_strings[STAT_TYPE_VALID_PEERS],
  289. stat_str,
  290. strlen (stat_type_strings[STAT_TYPE_VALID_PEERS])))
  291. {
  292. return STAT_TYPE_VALID_PEERS;
  293. }
  294. else if (0 == strncmp (stat_type_strings[STAT_TYPE_LEARND_PEERS],
  295. stat_str,
  296. strlen (stat_type_strings[STAT_TYPE_LEARND_PEERS])))
  297. {
  298. return STAT_TYPE_LEARND_PEERS;
  299. }
  300. else if (0 == strncmp (stat_type_strings[STAT_TYPE_PENDING_ONLINE_CHECKS],
  301. stat_str,
  302. strlen (stat_type_strings[STAT_TYPE_PENDING_ONLINE_CHECKS])))
  303. {
  304. return STAT_TYPE_PENDING_ONLINE_CHECKS;
  305. }
  306. else if (0 == strncmp (stat_type_strings[STAT_TYPE_UNREQUESTED_PULL_REPLIES],
  307. stat_str,
  308. strlen (stat_type_strings[STAT_TYPE_UNREQUESTED_PULL_REPLIES])))
  309. {
  310. return STAT_TYPE_UNREQUESTED_PULL_REPLIES;
  311. }
  312. else if (0 == strncmp (stat_type_strings[STAT_TYPE_PEERS_IN_PUSH_MAP],
  313. stat_str,
  314. strlen (stat_type_strings[STAT_TYPE_PEERS_IN_PUSH_MAP])))
  315. {
  316. return STAT_TYPE_PEERS_IN_PUSH_MAP;
  317. }
  318. else if (0 == strncmp (stat_type_strings[STAT_TYPE_PEERS_IN_PULL_MAP],
  319. stat_str,
  320. strlen (stat_type_strings[STAT_TYPE_PEERS_IN_PULL_MAP])))
  321. {
  322. return STAT_TYPE_PEERS_IN_PULL_MAP;
  323. }
  324. else if (0 == strncmp (stat_type_strings[STAT_TYPE_PEERS_IN_VIEW],
  325. stat_str,
  326. strlen (stat_type_strings[STAT_TYPE_PEERS_IN_VIEW])))
  327. {
  328. return STAT_TYPE_PEERS_IN_VIEW;
  329. }
  330. else if (0 == strncmp (stat_type_strings[STAT_TYPE_VIEW_SIZE_AIM],
  331. stat_str,
  332. strlen (stat_type_strings[STAT_TYPE_VIEW_SIZE_AIM])))
  333. {
  334. return STAT_TYPE_VIEW_SIZE_AIM;
  335. }
  336. return STAT_TYPE_MAX;
  337. }
  338. /**
  339. * @brief Indicates whether peer should go off- or online
  340. */
  341. enum PEER_ONLINE_DELTA {
  342. /**
  343. * @brief Indicates peer going online
  344. */
  345. PEER_GO_ONLINE = 1,
  346. /**
  347. * @brief Indicates peer going offline
  348. */
  349. PEER_GO_OFFLINE = -1,
  350. };
  351. /**
  352. * Operation map entry
  353. */
  354. struct OpListEntry
  355. {
  356. /**
  357. * DLL next ptr
  358. */
  359. struct OpListEntry *next;
  360. /**
  361. * DLL prev ptr
  362. */
  363. struct OpListEntry *prev;
  364. /**
  365. * The testbed operation
  366. */
  367. struct GNUNET_TESTBED_Operation *op;
  368. /**
  369. * Depending on whether we start or stop RPS service at the peer, set this to
  370. * #PEER_GO_ONLINE (1) or #PEER_GO_OFFLINE (-1)
  371. */
  372. enum PEER_ONLINE_DELTA delta;
  373. /**
  374. * Index of the regarding peer
  375. */
  376. unsigned int index;
  377. };
  378. /**
  379. * OpList DLL head
  380. */
  381. static struct OpListEntry *oplist_head;
  382. /**
  383. * OpList DLL tail
  384. */
  385. static struct OpListEntry *oplist_tail;
  386. /**
  387. * A pending reply: A request was sent and the reply is pending.
  388. */
  389. struct PendingReply
  390. {
  391. /**
  392. * DLL next,prev ptr
  393. */
  394. struct PendingReply *next;
  395. struct PendingReply *prev;
  396. /**
  397. * Handle to the request we are waiting for
  398. */
  399. struct GNUNET_RPS_Request_Handle *req_handle;
  400. /**
  401. * The peer that requested
  402. */
  403. struct RPSPeer *rps_peer;
  404. };
  405. /**
  406. * A pending request: A request was not made yet but is scheduled for later.
  407. */
  408. struct PendingRequest
  409. {
  410. /**
  411. * DLL next,prev ptr
  412. */
  413. struct PendingRequest *next;
  414. struct PendingRequest *prev;
  415. /**
  416. * Handle to the request we are waiting for
  417. */
  418. struct GNUNET_SCHEDULER_Task *request_task;
  419. /**
  420. * The peer that requested
  421. */
  422. struct RPSPeer *rps_peer;
  423. };
  424. /**
  425. * Information we track for each peer.
  426. */
  427. struct RPSPeer
  428. {
  429. /**
  430. * Index of the peer.
  431. */
  432. unsigned int index;
  433. /**
  434. * Handle for RPS connect operation.
  435. */
  436. struct GNUNET_TESTBED_Operation *op;
  437. /**
  438. * Handle to RPS service.
  439. */
  440. struct GNUNET_RPS_Handle *rps_handle;
  441. /**
  442. * ID of the peer.
  443. */
  444. struct GNUNET_PeerIdentity *peer_id;
  445. /**
  446. * A request handle to check for an request
  447. */
  448. //struct GNUNET_RPS_Request_Handle *req_handle;
  449. /**
  450. * Peer on- or offline?
  451. */
  452. int online;
  453. /**
  454. * Number of Peer IDs to request during the whole test
  455. */
  456. unsigned int num_ids_to_request;
  457. /**
  458. * Pending requests DLL
  459. */
  460. struct PendingRequest *pending_req_head;
  461. struct PendingRequest *pending_req_tail;
  462. /**
  463. * Number of pending requests
  464. */
  465. unsigned int num_pending_reqs;
  466. /**
  467. * Pending replies DLL
  468. */
  469. struct PendingReply *pending_rep_head;
  470. struct PendingReply *pending_rep_tail;
  471. /**
  472. * Number of pending replies
  473. */
  474. unsigned int num_pending_reps;
  475. /**
  476. * Number of received PeerIDs
  477. */
  478. unsigned int num_recv_ids;
  479. /**
  480. * Pending operation on that peer
  481. */
  482. const struct OpListEntry *entry_op_manage;
  483. /**
  484. * Testbed operation to connect to statistics service
  485. */
  486. struct GNUNET_TESTBED_Operation *stat_op;
  487. /**
  488. * Handle to the statistics service
  489. */
  490. struct GNUNET_STATISTICS_Handle *stats_h;
  491. /**
  492. * @brief flags to indicate which statistics values have been already
  493. * collected from the statistics service.
  494. * Used to check whether we are able to shutdown.
  495. */
  496. uint32_t stat_collected_flags;
  497. /**
  498. * @brief File name of the file the stats are finally written to
  499. */
  500. const char *file_name_stats;
  501. /**
  502. * @brief File name of the file the stats are finally written to
  503. */
  504. const char *file_name_probs;
  505. /**
  506. * @brief The current view
  507. */
  508. struct GNUNET_PeerIdentity *cur_view;
  509. /**
  510. * @brief Number of peers in the #cur_view.
  511. */
  512. uint32_t cur_view_count;
  513. /**
  514. * @brief Number of occurrences in other peer's view
  515. */
  516. uint32_t count_in_views;
  517. /**
  518. * @brief statistics values
  519. */
  520. uint64_t stats[STAT_TYPE_MAX];
  521. /**
  522. * @brief Handle for the statistics get request
  523. */
  524. struct GNUNET_STATISTICS_GetHandle *h_stat_get[STAT_TYPE_MAX];
  525. };
  526. /**
  527. * Information for all the peers.
  528. */
  529. static struct RPSPeer *rps_peers;
  530. /**
  531. * Peermap to get the index of a given peer ID quick.
  532. */
  533. static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
  534. /**
  535. * IDs of the peers.
  536. */
  537. static struct GNUNET_PeerIdentity *rps_peer_ids;
  538. /**
  539. * ID of the targeted peer.
  540. */
  541. static struct GNUNET_PeerIdentity *target_peer;
  542. /**
  543. * Number of online peers.
  544. */
  545. static unsigned int num_peers_online;
  546. /**
  547. * @brief The added sizes of the peer's views
  548. */
  549. static unsigned int view_sizes;
  550. /**
  551. * Return value from 'main'.
  552. */
  553. static int ok;
  554. /**
  555. * Identifier for the task that runs after the test to collect results
  556. */
  557. static struct GNUNET_SCHEDULER_Task *post_test_task;
  558. /**
  559. * Identifier for the shutdown task
  560. */
  561. static struct GNUNET_SCHEDULER_Task *shutdown_task;
  562. /**
  563. * Identifier for the churn task that runs periodically
  564. */
  565. static struct GNUNET_SCHEDULER_Task *churn_task;
  566. /**
  567. * Called to initialise the given RPSPeer
  568. */
  569. typedef void (*InitPeer) (struct RPSPeer *rps_peer);
  570. /**
  571. * @brief Called directly after connecting to the service
  572. *
  573. * @param rps_peer Specific peer the function is called on
  574. * @param h the handle to the rps service
  575. */
  576. typedef void (*PreTest) (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h);
  577. /**
  578. * @brief Executes functions to test the api/service for a given peer
  579. *
  580. * Called from within #rps_connect_complete_cb ()
  581. * Implemented by #churn_test_cb, #profiler_cb, #mal_cb, #single_req_cb,
  582. * #delay_req_cb, #seed_big_cb, #single_peer_seed_cb, #seed_cb, #req_cancel_cb
  583. *
  584. * @param rps_peer the peer the task runs on
  585. */
  586. typedef void (*MainTest) (struct RPSPeer *rps_peer);
  587. /**
  588. * Callback called once the requested random peers are available
  589. */
  590. typedef void (*ReplyHandle) (void *cls,
  591. uint64_t n,
  592. const struct GNUNET_PeerIdentity *recv_peers);
  593. /**
  594. * Called directly before disconnecting from the service
  595. */
  596. typedef void (*PostTest) (struct RPSPeer *peer);
  597. /**
  598. * Function called after disconnect to evaluate test success
  599. */
  600. typedef int (*EvaluationCallback) (void);
  601. /**
  602. * @brief Do we have Churn?
  603. */
  604. enum OPTION_CHURN {
  605. /**
  606. * @brief If we have churn this is set
  607. */
  608. HAVE_CHURN,
  609. /**
  610. * @brief If we have no churn this is set
  611. */
  612. HAVE_NO_CHURN,
  613. };
  614. /**
  615. * @brief Is it ok to quit the test before the timeout?
  616. */
  617. enum OPTION_QUICK_QUIT {
  618. /**
  619. * @brief It is ok for the test to quit before the timeout triggers
  620. */
  621. HAVE_QUICK_QUIT,
  622. /**
  623. * @brief It is NOT ok for the test to quit before the timeout triggers
  624. */
  625. HAVE_NO_QUICK_QUIT,
  626. };
  627. /**
  628. * @brief Do we collect statistics at the end?
  629. */
  630. enum OPTION_COLLECT_STATISTICS {
  631. /**
  632. * @brief We collect statistics at the end
  633. */
  634. COLLECT_STATISTICS,
  635. /**
  636. * @brief We do not collect statistics at the end
  637. */
  638. NO_COLLECT_STATISTICS,
  639. };
  640. /**
  641. * @brief Do we collect views during run?
  642. */
  643. enum OPTION_COLLECT_VIEW {
  644. /**
  645. * @brief We collect view during run
  646. */
  647. COLLECT_VIEW,
  648. /**
  649. * @brief We do not collect the view during run
  650. */
  651. NO_COLLECT_VIEW,
  652. };
  653. /**
  654. * Structure to define a single test
  655. */
  656. struct SingleTestRun
  657. {
  658. /**
  659. * Name of the test
  660. */
  661. char *name;
  662. /**
  663. * Called with a single peer in order to initialise that peer
  664. */
  665. InitPeer init_peer;
  666. /**
  667. * Called directly after connecting to the service
  668. */
  669. PreTest pre_test;
  670. /**
  671. * Main function for each peer
  672. */
  673. MainTest main_test;
  674. /**
  675. * Callback called once the requested peers are available
  676. */
  677. ReplyHandle reply_handle;
  678. /**
  679. * Called directly before disconnecting from the service
  680. */
  681. PostTest post_test;
  682. /**
  683. * Function to evaluate the test results
  684. */
  685. EvaluationCallback eval_cb;
  686. /**
  687. * Request interval
  688. */
  689. uint32_t request_interval;
  690. /**
  691. * Number of Requests to make.
  692. */
  693. uint32_t num_requests;
  694. /**
  695. * Run with (-out) churn
  696. */
  697. enum OPTION_CHURN have_churn;
  698. /**
  699. * Quit test before timeout?
  700. */
  701. enum OPTION_QUICK_QUIT have_quick_quit;
  702. /**
  703. * Collect statistics at the end?
  704. */
  705. enum OPTION_COLLECT_STATISTICS have_collect_statistics;
  706. /**
  707. * Collect view during run?
  708. */
  709. enum OPTION_COLLECT_VIEW have_collect_view;
  710. /**
  711. * @brief Mark which values from the statistics service to collect at the end
  712. * of the run
  713. */
  714. uint32_t stat_collect_flags;
  715. } cur_test_run;
  716. /**
  717. * Did we finish the test?
  718. */
  719. static int post_test;
  720. /**
  721. * Are we shutting down?
  722. */
  723. static int in_shutdown;
  724. /**
  725. * Append arguments to file
  726. */
  727. static void
  728. tofile_ (const char *file_name, const char *line)
  729. {
  730. struct GNUNET_DISK_FileHandle *f;
  731. /* char output_buffer[512]; */
  732. size_t size;
  733. /* int size; */
  734. size_t size2;
  735. if (NULL == (f = GNUNET_DISK_file_open (file_name,
  736. GNUNET_DISK_OPEN_APPEND |
  737. GNUNET_DISK_OPEN_WRITE |
  738. GNUNET_DISK_OPEN_CREATE,
  739. GNUNET_DISK_PERM_USER_READ |
  740. GNUNET_DISK_PERM_USER_WRITE |
  741. GNUNET_DISK_PERM_GROUP_READ |
  742. GNUNET_DISK_PERM_OTHER_READ)))
  743. {
  744. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  745. "Not able to open file %s\n",
  746. file_name);
  747. return;
  748. }
  749. /* size = GNUNET_snprintf (output_buffer,
  750. sizeof (output_buffer),
  751. "%llu %s\n",
  752. GNUNET_TIME_absolute_get ().abs_value_us,
  753. line);
  754. if (0 > size)
  755. {
  756. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  757. "Failed to write string to buffer (size: %i)\n",
  758. size);
  759. return;
  760. } */
  761. size = strlen (line) * sizeof (char);
  762. size2 = GNUNET_DISK_file_write (f, line, size);
  763. if (size != size2)
  764. {
  765. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  766. "Unable to write to file! (Size: %lu, size2: %lu)\n",
  767. size,
  768. size2);
  769. if (GNUNET_YES != GNUNET_DISK_file_close (f))
  770. {
  771. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  772. "Unable to close file\n");
  773. }
  774. return;
  775. }
  776. if (GNUNET_YES != GNUNET_DISK_file_close (f))
  777. {
  778. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  779. "Unable to close file\n");
  780. }
  781. }
  782. /**
  783. * This function is used to facilitate writing important information to disk
  784. */
  785. #define tofile(file_name, ...) do {\
  786. char tmp_buf[512];\
  787. int size;\
  788. size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
  789. if (0 > size)\
  790. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
  791. "Failed to create tmp_buf\n");\
  792. else\
  793. tofile_(file_name,tmp_buf);\
  794. } while (0);
  795. /**
  796. * Write the ids and their according index in the given array to a file
  797. * Unused
  798. */
  799. /* static void
  800. ids_to_file (char *file_name,
  801. struct GNUNET_PeerIdentity *peer_ids,
  802. unsigned int num_peer_ids)
  803. {
  804. unsigned int i;
  805. for (i=0 ; i < num_peer_ids ; i++)
  806. {
  807. to_file (file_name,
  808. "%u\t%s",
  809. i,
  810. GNUNET_i2s_full (&peer_ids[i]));
  811. }
  812. } */
  813. /**
  814. * Test the success of a single test
  815. */
  816. static int
  817. evaluate (void)
  818. {
  819. unsigned int i;
  820. int tmp_ok;
  821. tmp_ok = 1;
  822. for (i = 0; i < num_peers; i++)
  823. {
  824. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  825. "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
  826. i,
  827. GNUNET_i2s (rps_peers[i].peer_id),
  828. rps_peers[i].num_recv_ids,
  829. rps_peers[i].num_ids_to_request,
  830. (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids));
  831. tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids);
  832. }
  833. return tmp_ok? 0 : 1;
  834. }
  835. /**
  836. * Creates an oplist entry and adds it to the oplist DLL
  837. */
  838. static struct OpListEntry *
  839. make_oplist_entry ()
  840. {
  841. struct OpListEntry *entry;
  842. entry = GNUNET_new (struct OpListEntry);
  843. GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
  844. return entry;
  845. }
  846. /**
  847. * @brief Checks if given peer already received its statistics value from the
  848. * statistics service.
  849. *
  850. * @param rps_peer the peer to check for
  851. *
  852. * @return #GNUNET_YES if so
  853. * #GNUNET_NO otherwise
  854. */
  855. static int check_statistics_collect_completed_single_peer (
  856. const struct RPSPeer *rps_peer)
  857. {
  858. if (cur_test_run.stat_collect_flags !=
  859. (cur_test_run.stat_collect_flags &
  860. rps_peer->stat_collected_flags))
  861. {
  862. return GNUNET_NO;
  863. }
  864. return GNUNET_YES;
  865. }
  866. /**
  867. * @brief Checks if all peers already received their statistics value from the
  868. * statistics service.
  869. *
  870. * @return #GNUNET_YES if so
  871. * #GNUNET_NO otherwise
  872. */
  873. static int check_statistics_collect_completed ()
  874. {
  875. uint32_t i;
  876. for (i = 0; i < num_peers; i++)
  877. {
  878. if (GNUNET_NO == check_statistics_collect_completed_single_peer (&rps_peers[i]))
  879. {
  880. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  881. "At least Peer %" PRIu32 " did not yet receive all statistics values\n",
  882. i);
  883. return GNUNET_NO;
  884. }
  885. }
  886. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  887. "All peers received their statistics values\n");
  888. return GNUNET_YES;
  889. }
  890. static void
  891. rps_disconnect_adapter (void *cls,
  892. void *op_result);
  893. static void
  894. cancel_pending_req (struct PendingRequest *pending_req)
  895. {
  896. struct RPSPeer *rps_peer;
  897. rps_peer = pending_req->rps_peer;
  898. GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
  899. rps_peer->pending_req_tail,
  900. pending_req);
  901. rps_peer->num_pending_reqs--;
  902. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  903. "Cancelling pending rps get request\n");
  904. GNUNET_SCHEDULER_cancel (pending_req->request_task);
  905. GNUNET_free (pending_req);
  906. }
  907. static void
  908. cancel_request (struct PendingReply *pending_rep)
  909. {
  910. struct RPSPeer *rps_peer;
  911. rps_peer = pending_rep->rps_peer;
  912. GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
  913. rps_peer->pending_rep_tail,
  914. pending_rep);
  915. rps_peer->num_pending_reps--;
  916. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  917. "Cancelling rps get reply\n");
  918. GNUNET_assert (NULL != pending_rep->req_handle);
  919. GNUNET_RPS_request_cancel (pending_rep->req_handle);
  920. pending_rep->req_handle = NULL;
  921. GNUNET_free (pending_rep);
  922. pending_rep = NULL;
  923. }
  924. void
  925. clean_peer (unsigned peer_index)
  926. {
  927. struct PendingRequest *pending_req;
  928. while (NULL != (pending_req = rps_peers[peer_index].pending_req_head))
  929. {
  930. cancel_pending_req (pending_req);
  931. }
  932. pending_req = rps_peers[peer_index].pending_req_head;
  933. rps_disconnect_adapter (&rps_peers[peer_index],
  934. &rps_peers[peer_index].rps_handle);
  935. for (unsigned stat_type = STAT_TYPE_ROUNDS;
  936. stat_type < STAT_TYPE_MAX;
  937. stat_type++)
  938. {
  939. if (NULL != rps_peers[peer_index].h_stat_get[stat_type])
  940. {
  941. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  942. "(%u) did not yet receive stat value for `%s'\n",
  943. rps_peers[peer_index].index,
  944. stat_type_strings[stat_type]);
  945. GNUNET_STATISTICS_get_cancel (
  946. rps_peers[peer_index].h_stat_get[stat_type]);
  947. }
  948. }
  949. if (NULL != rps_peers[peer_index].op)
  950. {
  951. GNUNET_TESTBED_operation_done (rps_peers[peer_index].op);
  952. rps_peers[peer_index].op = NULL;
  953. }
  954. }
  955. /**
  956. * Task run on timeout to shut everything down.
  957. */
  958. static void
  959. shutdown_op (void *cls)
  960. {
  961. unsigned int i;
  962. struct OpListEntry *entry;
  963. (void) cls;
  964. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  965. "Shutdown task scheduled, going down.\n");
  966. in_shutdown = GNUNET_YES;
  967. if (NULL != shutdown_task)
  968. {
  969. GNUNET_SCHEDULER_cancel (shutdown_task);
  970. shutdown_task = NULL;
  971. }
  972. if (NULL != post_test_task)
  973. {
  974. GNUNET_SCHEDULER_cancel (post_test_task);
  975. post_test_task = NULL;
  976. }
  977. if (NULL != churn_task)
  978. {
  979. GNUNET_SCHEDULER_cancel (churn_task);
  980. churn_task = NULL;
  981. }
  982. entry = oplist_head;
  983. while (NULL != (entry = oplist_head))
  984. {
  985. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  986. "Operation still pending on shutdown (%u)\n",
  987. entry->index);
  988. GNUNET_TESTBED_operation_done (entry->op);
  989. GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
  990. GNUNET_free (entry);
  991. }
  992. for (i = 0; i < num_peers; i++)
  993. {
  994. clean_peer (i);
  995. }
  996. close_all_files();
  997. }
  998. static void
  999. trigger_shutdown (void *cls)
  1000. {
  1001. (void) cls;
  1002. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1003. "Shutdown was triggerd by timeout, going down.\n");
  1004. shutdown_task = NULL;
  1005. GNUNET_SCHEDULER_shutdown ();
  1006. }
  1007. /**
  1008. * Task run after #duration to collect statistics and potentially shut down.
  1009. */
  1010. static void
  1011. post_test_op (void *cls)
  1012. {
  1013. unsigned int i;
  1014. (void) cls;
  1015. post_test_task = NULL;
  1016. post_test = GNUNET_YES;
  1017. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1018. "Executing post test op.\n");
  1019. if (NULL != churn_task)
  1020. {
  1021. GNUNET_SCHEDULER_cancel (churn_task);
  1022. churn_task = NULL;
  1023. }
  1024. for (i = 0; i < num_peers; i++)
  1025. {
  1026. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1027. "Executing post test op. (peer %" PRIu32 ")\n",
  1028. rps_peers[i].index);
  1029. if (NULL != rps_peers[i].op)
  1030. {
  1031. GNUNET_TESTBED_operation_done (rps_peers[i].op);
  1032. rps_peers[i].op = NULL;
  1033. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1034. "Cancelled testbed operation\n");
  1035. }
  1036. if (NULL != cur_test_run.post_test)
  1037. {
  1038. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing post_test for peer %u\n", i);
  1039. cur_test_run.post_test (&rps_peers[i]);
  1040. }
  1041. }
  1042. /* If we do not collect statistics, shut down directly */
  1043. if (NO_COLLECT_STATISTICS == cur_test_run.have_collect_statistics ||
  1044. GNUNET_YES == check_statistics_collect_completed())
  1045. {
  1046. GNUNET_SCHEDULER_cancel (shutdown_task);
  1047. shutdown_task = NULL;
  1048. GNUNET_SCHEDULER_shutdown ();
  1049. }
  1050. }
  1051. /**
  1052. * Seed peers.
  1053. */
  1054. static void
  1055. seed_peers (void *cls)
  1056. {
  1057. struct RPSPeer *peer = cls;
  1058. unsigned int amount;
  1059. unsigned int i;
  1060. // TODO if malicious don't seed mal peers
  1061. amount = round (.5 * num_peers);
  1062. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
  1063. for (i = 0 ; i < amount ; i++)
  1064. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
  1065. i,
  1066. GNUNET_i2s (&rps_peer_ids[i]));
  1067. GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
  1068. }
  1069. /**
  1070. * Get the id of peer i.
  1071. */
  1072. void
  1073. info_cb (void *cb_cls,
  1074. struct GNUNET_TESTBED_Operation *op,
  1075. const struct GNUNET_TESTBED_PeerInformation *pinfo,
  1076. const char *emsg)
  1077. {
  1078. struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
  1079. (void) op;
  1080. if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
  1081. {
  1082. return;
  1083. }
  1084. if (NULL == pinfo || NULL != emsg)
  1085. {
  1086. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
  1087. GNUNET_TESTBED_operation_done (entry->op);
  1088. return;
  1089. }
  1090. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1091. "Peer %u is %s\n",
  1092. entry->index,
  1093. GNUNET_i2s (pinfo->result.id));
  1094. rps_peer_ids[entry->index] = *(pinfo->result.id);
  1095. rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
  1096. GNUNET_assert (GNUNET_OK ==
  1097. GNUNET_CONTAINER_multipeermap_put (peer_map,
  1098. &rps_peer_ids[entry->index],
  1099. &rps_peers[entry->index],
  1100. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  1101. tofile ("/tmp/rps/peer_ids",
  1102. "%u\t%s\n",
  1103. entry->index,
  1104. GNUNET_i2s_full (&rps_peer_ids[entry->index]));
  1105. GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
  1106. GNUNET_TESTBED_operation_done (entry->op);
  1107. GNUNET_free (entry);
  1108. }
  1109. /**
  1110. * Callback to be called when RPS service connect operation is completed
  1111. *
  1112. * @param cls the callback closure from functions generating an operation
  1113. * @param op the operation that has been finished
  1114. * @param ca_result the RPS service handle returned from rps_connect_adapter
  1115. * @param emsg error message in case the operation has failed; will be NULL if
  1116. * operation has executed successfully.
  1117. */
  1118. static void
  1119. rps_connect_complete_cb (void *cls,
  1120. struct GNUNET_TESTBED_Operation *op,
  1121. void *ca_result,
  1122. const char *emsg)
  1123. {
  1124. struct RPSPeer *rps_peer = cls;
  1125. struct GNUNET_RPS_Handle *rps = ca_result;
  1126. if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
  1127. {
  1128. return;
  1129. }
  1130. rps_peer->rps_handle = rps;
  1131. rps_peer->online = GNUNET_YES;
  1132. num_peers_online++;
  1133. GNUNET_assert (op == rps_peer->op);
  1134. if (NULL != emsg)
  1135. {
  1136. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1137. "Failed to connect to RPS service: %s\n",
  1138. emsg);
  1139. ok = 1;
  1140. GNUNET_SCHEDULER_shutdown ();
  1141. return;
  1142. }
  1143. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1144. "Started client successfully (%u)\n",
  1145. rps_peer->index);
  1146. cur_test_run.main_test (rps_peer);
  1147. }
  1148. /**
  1149. * Adapter function called to establish a connection to
  1150. * the RPS service.
  1151. *
  1152. * @param cls closure
  1153. * @param cfg configuration of the peer to connect to; will be available until
  1154. * GNUNET_TESTBED_operation_done() is called on the operation returned
  1155. * from GNUNET_TESTBED_service_connect()
  1156. * @return service handle to return in 'op_result', NULL on error
  1157. */
  1158. static void *
  1159. rps_connect_adapter (void *cls,
  1160. const struct GNUNET_CONFIGURATION_Handle *cfg)
  1161. {
  1162. struct GNUNET_RPS_Handle *h;
  1163. h = GNUNET_RPS_connect (cfg);
  1164. if (NULL != cur_test_run.pre_test)
  1165. cur_test_run.pre_test (cls, h);
  1166. return h;
  1167. }
  1168. /**
  1169. * Called to open a connection to the peer's statistics
  1170. *
  1171. * @param cls peer context
  1172. * @param cfg configuration of the peer to connect to; will be available until
  1173. * GNUNET_TESTBED_operation_done() is called on the operation returned
  1174. * from GNUNET_TESTBED_service_connect()
  1175. * @return service handle to return in 'op_result', NULL on error
  1176. */
  1177. static void *
  1178. stat_connect_adapter (void *cls,
  1179. const struct GNUNET_CONFIGURATION_Handle *cfg)
  1180. {
  1181. struct RPSPeer *peer = cls;
  1182. peer->stats_h = GNUNET_STATISTICS_create ("rps-profiler", cfg);
  1183. return peer->stats_h;
  1184. }
  1185. /**
  1186. * Called to disconnect from peer's statistics service
  1187. *
  1188. * @param cls peer context
  1189. * @param op_result service handle returned from the connect adapter
  1190. */
  1191. static void
  1192. stat_disconnect_adapter (void *cls, void *op_result)
  1193. {
  1194. struct RPSPeer *peer = cls;
  1195. //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
  1196. // (peer->stats_h, "core", "# peers connected",
  1197. // stat_iterator, peer));
  1198. //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
  1199. // (peer->stats_h, "nse", "# peers connected",
  1200. // stat_iterator, peer));
  1201. GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
  1202. peer->stats_h = NULL;
  1203. }
  1204. /**
  1205. * Called after successfully opening a connection to a peer's statistics
  1206. * service; we register statistics monitoring for CORE and NSE here.
  1207. *
  1208. * @param cls the callback closure from functions generating an operation
  1209. * @param op the operation that has been finished
  1210. * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
  1211. * @param emsg error message in case the operation has failed; will be NULL if
  1212. * operation has executed successfully.
  1213. */
  1214. static void
  1215. stat_complete_cb (void *cls,
  1216. struct GNUNET_TESTBED_Operation *op,
  1217. void *ca_result,
  1218. const char *emsg )
  1219. {
  1220. //struct GNUNET_STATISTICS_Handle *sh = ca_result;
  1221. //struct RPSPeer *peer = (struct RPSPeer *) cls;
  1222. (void) cls;
  1223. (void) op;
  1224. (void) ca_result;
  1225. if (NULL != emsg)
  1226. {
  1227. GNUNET_break (0);
  1228. return;
  1229. }
  1230. //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
  1231. // (sh, "core", "# peers connected",
  1232. // stat_iterator, peer));
  1233. //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
  1234. // (sh, "nse", "# peers connected",
  1235. // stat_iterator, peer));
  1236. }
  1237. /**
  1238. * Adapter function called to destroy connection to
  1239. * RPS service.
  1240. *
  1241. * @param cls closure
  1242. * @param op_result service handle returned from the connect adapter
  1243. */
  1244. static void
  1245. rps_disconnect_adapter (void *cls,
  1246. void *op_result)
  1247. {
  1248. struct RPSPeer *peer = cls;
  1249. struct GNUNET_RPS_Handle *h = op_result;
  1250. struct PendingReply *pending_rep;
  1251. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1252. "disconnect_adapter (%u)\n",
  1253. peer->index);
  1254. GNUNET_assert (NULL != peer);
  1255. if (NULL != peer->rps_handle)
  1256. {
  1257. while (NULL != (pending_rep = peer->pending_rep_head))
  1258. {
  1259. cancel_request (pending_rep);
  1260. }
  1261. GNUNET_assert (h == peer->rps_handle);
  1262. if (NULL != h)
  1263. {
  1264. GNUNET_RPS_disconnect (h);
  1265. h = NULL;
  1266. }
  1267. peer->rps_handle = NULL;
  1268. }
  1269. }
  1270. /***********************************************************************
  1271. * Definition of tests
  1272. ***********************************************************************/
  1273. /**
  1274. * Callback to call on receipt of a reply
  1275. *
  1276. * @param cls closure
  1277. * @param n number of peers
  1278. * @param recv_peers the received peers
  1279. */
  1280. static void
  1281. default_reply_handle (void *cls,
  1282. uint64_t n,
  1283. const struct GNUNET_PeerIdentity *recv_peers)
  1284. {
  1285. struct RPSPeer *rps_peer;
  1286. struct PendingReply *pending_rep = (struct PendingReply *) cls;
  1287. unsigned int i;
  1288. rps_peer = pending_rep->rps_peer;
  1289. GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
  1290. rps_peer->pending_rep_tail,
  1291. pending_rep);
  1292. rps_peer->num_pending_reps--;
  1293. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1294. "[%s] got %" PRIu64 " peers:\n",
  1295. GNUNET_i2s (rps_peer->peer_id),
  1296. n);
  1297. for (i = 0; i < n; i++)
  1298. {
  1299. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1300. "%u: %s\n",
  1301. i,
  1302. GNUNET_i2s (&recv_peers[i]));
  1303. rps_peer->num_recv_ids++;
  1304. }
  1305. if (GNUNET_YES != post_test) return;
  1306. if (HAVE_QUICK_QUIT != cur_test_run.have_quick_quit) return;
  1307. if (0 == evaluate())
  1308. {
  1309. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1310. "Test succeeded before end of duration\n");
  1311. if (NULL != post_test_task) GNUNET_SCHEDULER_cancel (post_test_task);
  1312. post_test_task = GNUNET_SCHEDULER_add_now (&post_test_op, NULL);
  1313. GNUNET_assert (NULL != post_test_task);
  1314. }
  1315. }
  1316. /**
  1317. * Request random peers.
  1318. */
  1319. static void
  1320. request_peers (void *cls)
  1321. {
  1322. struct PendingRequest *pending_req = cls;
  1323. struct RPSPeer *rps_peer;
  1324. struct PendingReply *pending_rep;
  1325. rps_peer = pending_req->rps_peer;
  1326. GNUNET_assert (1 <= rps_peer->num_pending_reqs);
  1327. GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
  1328. rps_peer->pending_req_tail,
  1329. pending_req);
  1330. rps_peer->num_pending_reqs--;
  1331. if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test) return;
  1332. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1333. "Requesting one peer\n");
  1334. pending_rep = GNUNET_new (struct PendingReply);
  1335. pending_rep->rps_peer = rps_peer;
  1336. pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
  1337. 1,
  1338. cur_test_run.reply_handle,
  1339. pending_rep);
  1340. GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
  1341. rps_peer->pending_rep_tail,
  1342. pending_rep);
  1343. rps_peer->num_pending_reps++;
  1344. }
  1345. /**
  1346. * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
  1347. * issued, nor replied
  1348. */
  1349. void
  1350. schedule_missing_requests (struct RPSPeer *rps_peer)
  1351. {
  1352. unsigned int i;
  1353. struct PendingRequest *pending_req;
  1354. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1355. "Scheduling %u - %u missing requests\n",
  1356. rps_peer->num_ids_to_request,
  1357. rps_peer->num_pending_reqs + rps_peer->num_pending_reps);
  1358. GNUNET_assert (rps_peer->num_pending_reqs + rps_peer->num_pending_reps <=
  1359. rps_peer->num_ids_to_request);
  1360. for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
  1361. i < rps_peer->num_ids_to_request; i++)
  1362. {
  1363. pending_req = GNUNET_new (struct PendingRequest);
  1364. pending_req->rps_peer = rps_peer;
  1365. pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
  1366. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
  1367. cur_test_run.request_interval * i),
  1368. request_peers,
  1369. pending_req);
  1370. GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
  1371. rps_peer->pending_req_tail,
  1372. pending_req);
  1373. rps_peer->num_pending_reqs++;
  1374. }
  1375. }
  1376. void
  1377. cancel_pending_req_rep (struct RPSPeer *rps_peer)
  1378. {
  1379. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1380. "Cancelling all (pending) requests.\n");
  1381. while (NULL != rps_peer->pending_req_head)
  1382. cancel_pending_req (rps_peer->pending_req_head);
  1383. GNUNET_assert (0 == rps_peer->num_pending_reqs);
  1384. while (NULL != rps_peer->pending_rep_head)
  1385. cancel_request (rps_peer->pending_rep_head);
  1386. GNUNET_assert (0 == rps_peer->num_pending_reps);
  1387. }
  1388. /***********************************
  1389. * MALICIOUS
  1390. ***********************************/
  1391. /**
  1392. * Initialise only non-mal RPSPeers
  1393. */
  1394. static void mal_init_peer (struct RPSPeer *rps_peer)
  1395. {
  1396. if (rps_peer->index >= round (portion * num_peers))
  1397. rps_peer->num_ids_to_request = 1;
  1398. }
  1399. /**
  1400. * @brief Set peers to (non-)malicious before execution
  1401. *
  1402. * Of signature #PreTest
  1403. *
  1404. * @param rps_peer the peer to set (non-) malicious
  1405. * @param h the handle to the service
  1406. */
  1407. static void
  1408. mal_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
  1409. {
  1410. #if ENABLE_MALICIOUS
  1411. uint32_t num_mal_peers;
  1412. GNUNET_assert ( (1 >= portion) &&
  1413. (0 < portion) );
  1414. num_mal_peers = round (portion * num_peers);
  1415. if (rps_peer->index < num_mal_peers)
  1416. {
  1417. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1418. "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
  1419. rps_peer->index,
  1420. GNUNET_i2s (rps_peer->peer_id),
  1421. num_mal_peers);
  1422. GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
  1423. rps_peer_ids, target_peer);
  1424. }
  1425. #endif /* ENABLE_MALICIOUS */
  1426. }
  1427. static void
  1428. mal_cb (struct RPSPeer *rps_peer)
  1429. {
  1430. uint32_t num_mal_peers;
  1431. if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
  1432. {
  1433. return;
  1434. }
  1435. #if ENABLE_MALICIOUS
  1436. GNUNET_assert ( (1 >= portion) &&
  1437. (0 < portion) );
  1438. num_mal_peers = round (portion * num_peers);
  1439. if (rps_peer->index >= num_mal_peers)
  1440. { /* It's useless to ask a malicious peer about a random sample -
  1441. it's not sampling */
  1442. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
  1443. seed_peers, rps_peer);
  1444. schedule_missing_requests (rps_peer);
  1445. }
  1446. #endif /* ENABLE_MALICIOUS */
  1447. }
  1448. /***********************************
  1449. * CHURN
  1450. ***********************************/
  1451. static void
  1452. churn (void *cls);
  1453. /**
  1454. * @brief Starts churn
  1455. *
  1456. * Has signature of #MainTest
  1457. *
  1458. * This is not implemented too nicely as this is called for each peer, but we
  1459. * only need to call it once. (Yes we check that we only schedule the task
  1460. * once.)
  1461. *
  1462. * @param rps_peer The peer it's called for
  1463. */
  1464. static void
  1465. churn_test_cb (struct RPSPeer *rps_peer)
  1466. {
  1467. if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
  1468. {
  1469. return;
  1470. }
  1471. /* Start churn */
  1472. if (HAVE_CHURN == cur_test_run.have_churn && NULL == churn_task)
  1473. {
  1474. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1475. "Starting churn task\n");
  1476. churn_task = GNUNET_SCHEDULER_add_delayed (
  1477. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
  1478. churn,
  1479. NULL);
  1480. } else {
  1481. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1482. "Not starting churn task\n");
  1483. }
  1484. schedule_missing_requests (rps_peer);
  1485. }
  1486. /***********************************
  1487. * PROFILER
  1488. ***********************************/
  1489. /**
  1490. * Callback to be called when RPS service is started or stopped at peers
  1491. *
  1492. * @param cls NULL
  1493. * @param op the operation handle
  1494. * @param emsg NULL on success; otherwise an error description
  1495. */
  1496. static void
  1497. churn_cb (void *cls,
  1498. struct GNUNET_TESTBED_Operation *op,
  1499. const char *emsg)
  1500. {
  1501. // FIXME
  1502. struct OpListEntry *entry = cls;
  1503. (void) op;
  1504. if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
  1505. {
  1506. return;
  1507. }
  1508. GNUNET_TESTBED_operation_done (entry->op);
  1509. if (NULL != emsg)
  1510. {
  1511. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
  1512. GNUNET_SCHEDULER_shutdown ();
  1513. return;
  1514. }
  1515. GNUNET_assert (0 != entry->delta);
  1516. num_peers_online += entry->delta;
  1517. if (PEER_GO_OFFLINE == entry->delta)
  1518. { /* Peer hopefully just went offline */
  1519. if (GNUNET_YES != rps_peers[entry->index].online)
  1520. {
  1521. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  1522. "peer %s was expected to go offline but is still marked as online\n",
  1523. GNUNET_i2s (rps_peers[entry->index].peer_id));
  1524. GNUNET_break (0);
  1525. }
  1526. else
  1527. {
  1528. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1529. "peer %s probably went offline as expected\n",
  1530. GNUNET_i2s (rps_peers[entry->index].peer_id));
  1531. }
  1532. rps_peers[entry->index].online = GNUNET_NO;
  1533. }
  1534. else if (PEER_GO_ONLINE < entry->delta)
  1535. { /* Peer hopefully just went online */
  1536. if (GNUNET_NO != rps_peers[entry->index].online)
  1537. {
  1538. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  1539. "peer %s was expected to go online but is still marked as offline\n",
  1540. GNUNET_i2s (rps_peers[entry->index].peer_id));
  1541. GNUNET_break (0);
  1542. }
  1543. else
  1544. {
  1545. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1546. "peer %s probably went online as expected\n",
  1547. GNUNET_i2s (rps_peers[entry->index].peer_id));
  1548. if (NULL != cur_test_run.pre_test)
  1549. {
  1550. cur_test_run.pre_test (&rps_peers[entry->index],
  1551. rps_peers[entry->index].rps_handle);
  1552. schedule_missing_requests (&rps_peers[entry->index]);
  1553. }
  1554. }
  1555. rps_peers[entry->index].online = GNUNET_YES;
  1556. }
  1557. else
  1558. {
  1559. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1560. "Invalid value for delta: %i\n", entry->delta);
  1561. GNUNET_break (0);
  1562. }
  1563. GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
  1564. rps_peers[entry->index].entry_op_manage = NULL;
  1565. GNUNET_free (entry);
  1566. //if (num_peers_in_round[current_round] == peers_running)
  1567. // run_round ();
  1568. }
  1569. /**
  1570. * @brief Set the rps-service up or down for a specific peer
  1571. *
  1572. * @param i index of action
  1573. * @param j index of peer
  1574. * @param delta (#PEER_ONLINE_DELTA) down (-1) or up (1)
  1575. * @param prob_go_on_off the probability of the action
  1576. */
  1577. static void
  1578. manage_service_wrapper (unsigned int i, unsigned int j,
  1579. enum PEER_ONLINE_DELTA delta,
  1580. double prob_go_on_off)
  1581. {
  1582. struct OpListEntry *entry = NULL;
  1583. uint32_t prob;
  1584. /* make sure that management operation is not already scheduled */
  1585. if (NULL != rps_peers[j].entry_op_manage)
  1586. {
  1587. return;
  1588. }
  1589. prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
  1590. UINT32_MAX);
  1591. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1592. "%u. selected peer (%u: %s) is %s.\n",
  1593. i,
  1594. j,
  1595. GNUNET_i2s (rps_peers[j].peer_id),
  1596. (PEER_GO_ONLINE == delta) ? "online" : "offline");
  1597. if (prob < prob_go_on_off * UINT32_MAX)
  1598. {
  1599. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1600. "%s goes %s\n",
  1601. GNUNET_i2s (rps_peers[j].peer_id),
  1602. (PEER_GO_OFFLINE == delta) ? "offline" : "online");
  1603. if (PEER_GO_OFFLINE == delta)
  1604. cancel_pending_req_rep (&rps_peers[j]);
  1605. entry = make_oplist_entry ();
  1606. entry->delta = delta;
  1607. entry->index = j;
  1608. entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
  1609. testbed_peers[j],
  1610. "rps",
  1611. &churn_cb,
  1612. entry,
  1613. (PEER_GO_OFFLINE == delta) ? 0 : 1);
  1614. rps_peers[j].entry_op_manage = entry;
  1615. }
  1616. }
  1617. static void
  1618. churn (void *cls)
  1619. {
  1620. unsigned int i;
  1621. unsigned int j;
  1622. double portion_online;
  1623. unsigned int *permut;
  1624. double prob_go_offline;
  1625. double portion_go_online;
  1626. double portion_go_offline;
  1627. (void) cls;
  1628. if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
  1629. {
  1630. return;
  1631. }
  1632. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1633. "Churn function executing\n");
  1634. churn_task = NULL; /* Should be invalid by now */
  1635. /* Compute the probability for an online peer to go offline
  1636. * this round */
  1637. portion_online = num_peers_online * 1.0 / num_peers;
  1638. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1639. "Portion online: %f\n",
  1640. portion_online);
  1641. portion_go_online = ((1 - portion_online) * .5 * .66);
  1642. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1643. "Portion that should go online: %f\n",
  1644. portion_go_online);
  1645. portion_go_offline = (portion_online + portion_go_online) - .75;
  1646. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1647. "Portion that probably goes offline: %f\n",
  1648. portion_go_offline);
  1649. prob_go_offline = portion_go_offline / (portion_online * .5);
  1650. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1651. "Probability of a selected online peer to go offline: %f\n",
  1652. prob_go_offline);
  1653. permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
  1654. (unsigned int) num_peers);
  1655. /* Go over 50% randomly chosen peers */
  1656. for (i = 0; i < .5 * num_peers; i++)
  1657. {
  1658. j = permut[i];
  1659. /* If online, shut down with certain probability */
  1660. if (GNUNET_YES == rps_peers[j].online)
  1661. {
  1662. manage_service_wrapper (i, j, -1, prob_go_offline);
  1663. }
  1664. /* If offline, restart with certain probability */
  1665. else if (GNUNET_NO == rps_peers[j].online)
  1666. {
  1667. manage_service_wrapper (i, j, 1, 0.66);
  1668. }
  1669. }
  1670. GNUNET_free (permut);
  1671. churn_task = GNUNET_SCHEDULER_add_delayed (
  1672. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
  1673. churn,
  1674. NULL);
  1675. }
  1676. /**
  1677. * Initialise given RPSPeer
  1678. */
  1679. static void profiler_init_peer (struct RPSPeer *rps_peer)
  1680. {
  1681. rps_peer->num_ids_to_request = cur_test_run.num_requests;
  1682. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer shall request %i peers\n",
  1683. rps_peer->num_ids_to_request);
  1684. }
  1685. /**
  1686. * Callback to call on receipt of a reply
  1687. *
  1688. * @param cls closure
  1689. * @param n number of peers
  1690. * @param recv_peers the received peers
  1691. */
  1692. static void
  1693. profiler_reply_handle (void *cls,
  1694. uint64_t n,
  1695. const struct GNUNET_PeerIdentity *recv_peers)
  1696. {
  1697. struct RPSPeer *rps_peer;
  1698. struct RPSPeer *rcv_rps_peer;
  1699. char file_name_buf[128];
  1700. char file_name_dh_buf[128];
  1701. char file_name_dhr_buf[128];
  1702. char file_name_dhru_buf[128];
  1703. char *file_name = file_name_buf;
  1704. char *file_name_dh = file_name_dh_buf;
  1705. char *file_name_dhr = file_name_dhr_buf;
  1706. char *file_name_dhru = file_name_dhru_buf;
  1707. unsigned int i;
  1708. struct PendingReply *pending_rep = (struct PendingReply *) cls;
  1709. pending_rep->req_handle = NULL;
  1710. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "profiler_reply_handle()\n");
  1711. rps_peer = pending_rep->rps_peer;
  1712. (void) GNUNET_asprintf (&file_name,
  1713. "/tmp/rps/received_ids-%u",
  1714. rps_peer->index);
  1715. (void) GNUNET_asprintf (&file_name_dh,
  1716. "/tmp/rps/diehard_input-%u",
  1717. rps_peer->index);
  1718. (void) GNUNET_asprintf (&file_name_dhr,
  1719. "/tmp/rps/diehard_input_raw-%u",
  1720. rps_peer->index);
  1721. (void) GNUNET_asprintf (&file_name_dhru,
  1722. "/tmp/rps/diehard_input_raw_aligned-%u",
  1723. rps_peer->index);
  1724. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1725. "[%s] got %" PRIu64 " peers:\n",
  1726. GNUNET_i2s (rps_peer->peer_id),
  1727. n);
  1728. for (i = 0; i < n; i++)
  1729. {
  1730. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1731. "%u: %s\n",
  1732. i,
  1733. GNUNET_i2s (&recv_peers[i]));
  1734. tofile (file_name,
  1735. "%s\n",
  1736. GNUNET_i2s_full (&recv_peers[i]));
  1737. rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
  1738. GNUNET_assert (NULL != rcv_rps_peer);
  1739. tofile (file_name_dh,
  1740. "%" PRIu32 "\n",
  1741. (uint32_t) rcv_rps_peer->index);
  1742. #ifdef TO_FILE
  1743. to_file_raw (file_name_dhr,
  1744. (char *) &rcv_rps_peer->index,
  1745. sizeof (uint32_t));
  1746. to_file_raw_unaligned (file_name_dhru,
  1747. (char *) &rcv_rps_peer->index,
  1748. sizeof (uint32_t),
  1749. bits_needed);
  1750. #endif /* TO_FILE */
  1751. }
  1752. default_reply_handle (cls, n, recv_peers);
  1753. }
  1754. static void
  1755. profiler_cb (struct RPSPeer *rps_peer)
  1756. {
  1757. if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
  1758. {
  1759. return;
  1760. }
  1761. /* Start churn */
  1762. if (HAVE_CHURN == cur_test_run.have_churn && NULL == churn_task)
  1763. {
  1764. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1765. "Starting churn task\n");
  1766. churn_task = GNUNET_SCHEDULER_add_delayed (
  1767. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
  1768. churn,
  1769. NULL);
  1770. } else {
  1771. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1772. "Not starting churn task\n");
  1773. }
  1774. /* Only request peer ids at one peer.
  1775. * (It's the before-last because last one is target of the focussed attack.)
  1776. */
  1777. if (0 < rps_peer->num_ids_to_request)
  1778. schedule_missing_requests (rps_peer);
  1779. }
  1780. /**
  1781. * Function called from #profiler_eval with a filename.
  1782. *
  1783. * @param cls closure
  1784. * @param filename complete filename (absolute path)
  1785. * @return #GNUNET_OK to continue to iterate,
  1786. * #GNUNET_NO to stop iteration with no error,
  1787. * #GNUNET_SYSERR to abort iteration with error!
  1788. */
  1789. static int
  1790. file_name_cb (void *cls, const char *filename)
  1791. {
  1792. if (NULL != strstr (filename, "sampler_el"))
  1793. {
  1794. struct RPS_SamplerElement *s_elem;
  1795. struct GNUNET_CRYPTO_AuthKey auth_key;
  1796. const char *key_char;
  1797. uint32_t i;
  1798. (void) cls;
  1799. key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
  1800. tofile (filename, "--------------------------\n");
  1801. auth_key = string_to_auth_key (key_char);
  1802. s_elem = RPS_sampler_elem_create ();
  1803. RPS_sampler_elem_set (s_elem, auth_key);
  1804. for (i = 0; i < num_peers; i++)
  1805. {
  1806. RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
  1807. }
  1808. RPS_sampler_elem_destroy (s_elem);
  1809. }
  1810. return GNUNET_OK;
  1811. }
  1812. /**
  1813. * This is run after the test finished.
  1814. *
  1815. * Compute all perfect samples.
  1816. */
  1817. static int
  1818. profiler_eval (void)
  1819. {
  1820. #ifdef TO_FILE
  1821. /* Compute perfect sample for each sampler element */
  1822. if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
  1823. {
  1824. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
  1825. }
  1826. #endif /* TO_FILE */
  1827. return evaluate ();
  1828. }
  1829. /** @brief is b in view of a?
  1830. *
  1831. * @param a
  1832. * @param b
  1833. *
  1834. * @return
  1835. */
  1836. static int is_in_view (uint32_t a, uint32_t b)
  1837. {
  1838. uint32_t i;
  1839. for (i = 0; i < rps_peers[a].cur_view_count; i++)
  1840. {
  1841. if (0 == memcmp (rps_peers[b].peer_id,
  1842. &rps_peers[a].cur_view[i],
  1843. sizeof (struct GNUNET_PeerIdentity)))
  1844. {
  1845. return GNUNET_YES;
  1846. }
  1847. }
  1848. return GNUNET_NO;
  1849. }
  1850. static uint32_t get_idx_of_pid (const struct GNUNET_PeerIdentity *pid)
  1851. {
  1852. uint32_t i;
  1853. for (i = 0; i < num_peers; i++)
  1854. {
  1855. if (0 == memcmp (pid,
  1856. rps_peers[i].peer_id,
  1857. sizeof (struct GNUNET_PeerIdentity)))
  1858. {
  1859. return i;
  1860. }
  1861. }
  1862. //return 0; /* Should not happen - make compiler happy */
  1863. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1864. "No known _PeerIdentity %s!\n",
  1865. GNUNET_i2s_full (pid));
  1866. GNUNET_assert (0);
  1867. }
  1868. /**
  1869. * @brief Counts number of peers in view of a that have b in their view
  1870. *
  1871. * @param a
  1872. * @param uint32_tb
  1873. *
  1874. * @return
  1875. */
  1876. static uint32_t count_containing_views (uint32_t a, uint32_t b)
  1877. {
  1878. uint32_t i;
  1879. uint32_t peer_idx;
  1880. uint32_t count = 0;
  1881. for (i = 0; i < rps_peers[a].cur_view_count; i++)
  1882. {
  1883. peer_idx = get_idx_of_pid (&rps_peers[a].cur_view[i]);
  1884. if (GNUNET_YES == is_in_view (peer_idx, b))
  1885. {
  1886. count++;
  1887. }
  1888. }
  1889. return count;
  1890. }
  1891. /**
  1892. * @brief Computes the probability for each other peer to be selected by the
  1893. * sampling process based on the views of all peers
  1894. *
  1895. * @param peer_idx index of the peer that is about to sample
  1896. */
  1897. static void compute_probabilities (uint32_t peer_idx)
  1898. {
  1899. //double probs[num_peers] = { 0 };
  1900. double probs[num_peers];
  1901. size_t probs_as_str_size = (num_peers * 10 + 1) * sizeof (char);
  1902. char *probs_as_str = GNUNET_malloc (probs_as_str_size);
  1903. char *probs_as_str_cpy;
  1904. uint32_t i;
  1905. double prob_push;
  1906. double prob_pull;
  1907. uint32_t view_size;
  1908. uint32_t cont_views;
  1909. uint32_t number_of_being_in_pull_events;
  1910. int tmp;
  1911. uint32_t count_non_zero_prob = 0;
  1912. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1913. "Computing probabilities for peer %" PRIu32 "\n", peer_idx);
  1914. /* Firstly without knowledge of old views */
  1915. for (i = 0; i < num_peers; i++)
  1916. {
  1917. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1918. "\tfor peer %" PRIu32 ":\n", i);
  1919. view_size = rps_peers[i].cur_view_count;
  1920. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1921. "\t\tview_size: %" PRIu32 "\n", view_size);
  1922. /* For peer i the probability of being sampled is
  1923. * evenly distributed among all possibly observed peers. */
  1924. /* We could have observed a peer in three cases:
  1925. * 1. peer sent a push
  1926. * 2. peer was contained in a pull reply
  1927. * 3. peer was in history (sampler) - ignored for now */
  1928. /* 1. Probability of having received a push from peer i */
  1929. if ((GNUNET_YES == is_in_view (i, peer_idx)) &&
  1930. (1 <= (0.45 * view_size)))
  1931. {
  1932. if (0 == binom (view_size, 0.45 * view_size)) prob_push = 0;
  1933. else
  1934. {
  1935. prob_push = 1.0 * binom (0.45 * view_size, 1)
  1936. /
  1937. binom (view_size, 0.45 * view_size);
  1938. }
  1939. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1940. "\t\t%" PRIu32 " is in %" PRIu32 "'s view, prob: %f\n",
  1941. peer_idx,
  1942. i,
  1943. prob_push);
  1944. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1945. "\t\tposs choices from view: %" PRIu32 ", containing i: %" PRIu32 "\n",
  1946. binom (view_size, 0.45 * view_size),
  1947. binom (0.45 * view_size, 1));
  1948. } else {
  1949. prob_push = 0;
  1950. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1951. "\t\t%" PRIu32 " is not in %" PRIu32 "'s view, prob: 0\n",
  1952. peer_idx,
  1953. i);
  1954. }
  1955. /* 2. Probability of peer i being contained in pulls */
  1956. view_size = rps_peers[peer_idx].cur_view_count;
  1957. cont_views = count_containing_views (peer_idx, i);
  1958. number_of_being_in_pull_events =
  1959. (binom (view_size, 0.45 * view_size) -
  1960. binom (view_size - cont_views, 0.45 * view_size));
  1961. if (0 != number_of_being_in_pull_events)
  1962. {
  1963. prob_pull = number_of_being_in_pull_events
  1964. /
  1965. (1.0 * binom (view_size, 0.45 * view_size));
  1966. } else
  1967. {
  1968. prob_pull = 0;
  1969. }
  1970. probs[i] = prob_push + prob_pull - (prob_push * prob_pull);
  1971. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1972. "\t\t%" PRIu32 " has %" PRIu32 " of %" PRIu32
  1973. " peers in its view who know %" PRIu32 " prob: %f\n",
  1974. peer_idx,
  1975. cont_views,
  1976. view_size,
  1977. i,
  1978. prob_pull);
  1979. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1980. "\t\tnumber of possible pull combinations: %" PRIu32 "\n",
  1981. binom (view_size, 0.45 * view_size));
  1982. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1983. "\t\tnumber of possible pull combinations without %" PRIu32
  1984. ": %" PRIu32 "\n",
  1985. i,
  1986. binom (view_size - cont_views, 0.45 * view_size));
  1987. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1988. "\t\tnumber of possible pull combinations with %" PRIu32
  1989. ": %" PRIu32 "\n",
  1990. i,
  1991. number_of_being_in_pull_events);
  1992. if (0 != probs[i]) count_non_zero_prob++;
  1993. }
  1994. /* normalize */
  1995. if (0 != count_non_zero_prob)
  1996. {
  1997. for (i = 0; i < num_peers; i++)
  1998. {
  1999. probs[i] = probs[i] * (1.0 / count_non_zero_prob);
  2000. }
  2001. } else {
  2002. for (i = 0; i < num_peers; i++)
  2003. {
  2004. probs[i] = 0;
  2005. }
  2006. }
  2007. /* str repr */
  2008. for (i = 0; i < num_peers; i++)
  2009. {
  2010. probs_as_str_cpy = GNUNET_strndup (probs_as_str, probs_as_str_size);
  2011. tmp = GNUNET_snprintf (probs_as_str,
  2012. probs_as_str_size,
  2013. "%s %7.6f", probs_as_str_cpy, probs[i]);
  2014. GNUNET_free (probs_as_str_cpy);
  2015. GNUNET_assert (0 <= tmp);
  2016. }
  2017. to_file_w_len (rps_peers[peer_idx].file_name_probs,
  2018. probs_as_str_size,
  2019. probs_as_str);
  2020. GNUNET_free (probs_as_str);
  2021. }
  2022. /**
  2023. * @brief This counts the number of peers in which views a given peer occurs.
  2024. *
  2025. * It also stores this value in the rps peer.
  2026. *
  2027. * @param peer_idx the index of the peer to count the representation
  2028. *
  2029. * @return the number of occurrences
  2030. */
  2031. static uint32_t count_peer_in_views_2 (uint32_t peer_idx)
  2032. {
  2033. uint32_t i, j;
  2034. uint32_t count = 0;
  2035. for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
  2036. {
  2037. for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
  2038. {
  2039. if (0 == memcmp (rps_peers[peer_idx].peer_id,
  2040. &rps_peers[i].cur_view[j],
  2041. sizeof (struct GNUNET_PeerIdentity)))
  2042. {
  2043. count++;
  2044. break;
  2045. }
  2046. }
  2047. }
  2048. rps_peers[peer_idx].count_in_views = count;
  2049. return count;
  2050. }
  2051. static uint32_t cumulated_view_sizes ()
  2052. {
  2053. uint32_t i;
  2054. view_sizes = 0;
  2055. for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
  2056. {
  2057. view_sizes += rps_peers[i].cur_view_count;
  2058. }
  2059. return view_sizes;
  2060. }
  2061. static void count_peer_in_views (uint32_t *count_peers)
  2062. {
  2063. uint32_t i, j;
  2064. for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
  2065. {
  2066. for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
  2067. {
  2068. if (0 == memcmp (rps_peers[i].peer_id,
  2069. &rps_peers[i].cur_view[j],
  2070. sizeof (struct GNUNET_PeerIdentity)))
  2071. {
  2072. count_peers[i]++;
  2073. }
  2074. }
  2075. }
  2076. }
  2077. void compute_diversity ()
  2078. {
  2079. uint32_t i;
  2080. /* ith entry represents the numer of occurrences in other peer's views */
  2081. uint32_t *count_peers = GNUNET_new_array (num_peers, uint32_t);
  2082. uint32_t views_total_size;
  2083. double expected;
  2084. /* deviation from expected number of peers */
  2085. double *deviation = GNUNET_new_array (num_peers, double);
  2086. views_total_size = 0;
  2087. expected = 0;
  2088. /* For each peer count its representation in other peer's views*/
  2089. for (i = 0; i < num_peers; i++) /* Peer to count */
  2090. {
  2091. views_total_size += rps_peers[i].cur_view_count;
  2092. count_peer_in_views (count_peers);
  2093. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2094. "Counted representation of %" PRIu32 "th peer [%s]: %" PRIu32"\n",
  2095. i,
  2096. GNUNET_i2s (rps_peers[i].peer_id),
  2097. count_peers[i]);
  2098. }
  2099. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2100. "size of all views combined: %" PRIu32 "\n",
  2101. views_total_size);
  2102. expected = ((double) 1/num_peers) * views_total_size;
  2103. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2104. "Expected number of occurrences of each peer in all views: %f\n",
  2105. expected);
  2106. for (i = 0; i < num_peers; i++) /* Peer to count */
  2107. {
  2108. deviation[i] = expected - count_peers[i];
  2109. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2110. "Deviation from expectation: %f\n", deviation[i]);
  2111. }
  2112. GNUNET_free (count_peers);
  2113. GNUNET_free (deviation);
  2114. }
  2115. void print_view_sizes()
  2116. {
  2117. uint32_t i;
  2118. for (i = 0; i < num_peers; i++) /* Peer to count */
  2119. {
  2120. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2121. "View size of %" PRIu32 ". [%s] is %" PRIu32 "\n",
  2122. i,
  2123. GNUNET_i2s (rps_peers[i].peer_id),
  2124. rps_peers[i].cur_view_count);
  2125. }
  2126. }
  2127. void all_views_updated_cb()
  2128. {
  2129. compute_diversity();
  2130. print_view_sizes();
  2131. }
  2132. void view_update_cb (void *cls,
  2133. uint64_t view_size,
  2134. const struct GNUNET_PeerIdentity *peers)
  2135. {
  2136. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2137. "View was updated (%" PRIu64 ")\n", view_size);
  2138. struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
  2139. to_file ("/tmp/rps/view_sizes.txt",
  2140. "%" PRIu64 " %" PRIu32 "",
  2141. rps_peer->index,
  2142. view_size);
  2143. for (uint64_t i = 0; i < view_size; i++)
  2144. {
  2145. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2146. "\t%s\n", GNUNET_i2s (&peers[i]));
  2147. }
  2148. GNUNET_array_grow (rps_peer->cur_view,
  2149. rps_peer->cur_view_count,
  2150. view_size);
  2151. //*rps_peer->cur_view = *peers;
  2152. GNUNET_memcpy (rps_peer->cur_view,
  2153. peers,
  2154. view_size * sizeof (struct GNUNET_PeerIdentity));
  2155. to_file ("/tmp/rps/count_in_views.txt",
  2156. "%" PRIu64 " %" PRIu32 "",
  2157. rps_peer->index,
  2158. count_peer_in_views_2 (rps_peer->index));
  2159. cumulated_view_sizes();
  2160. if (0 != view_size)
  2161. {
  2162. to_file ("/tmp/rps/repr.txt",
  2163. "%" PRIu64 /* index */
  2164. " %" PRIu32 /* occurrence in views */
  2165. " %" PRIu32 /* view sizes */
  2166. " %f" /* fraction of repr in views */
  2167. " %f" /* average view size */
  2168. " %f" /* prob of occurrence in view slot */
  2169. " %f" "", /* exp frac of repr in views */
  2170. rps_peer->index,
  2171. count_peer_in_views_2 (rps_peer->index),
  2172. view_sizes,
  2173. count_peer_in_views_2 (rps_peer->index) / (view_size * 1.0), /* fraction of representation in views */
  2174. view_sizes / (view_size * 1.0), /* average view size */
  2175. 1.0 /view_size, /* prob of occurrence in view slot */
  2176. (1.0/view_size) * (view_sizes/view_size) /* expected fraction of repr in views */
  2177. );
  2178. }
  2179. compute_probabilities (rps_peer->index);
  2180. all_views_updated_cb();
  2181. }
  2182. static void
  2183. pre_profiler (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
  2184. {
  2185. rps_peer->file_name_probs =
  2186. store_prefix_file_name (rps_peer->peer_id, "probs");
  2187. GNUNET_RPS_view_request (h, 0, view_update_cb, rps_peer);
  2188. }
  2189. void write_final_stats (void){
  2190. uint64_t sums[STAT_TYPE_MAX] = { 0 };
  2191. for (uint32_t i = 0; i < num_peers; i++)
  2192. {
  2193. to_file ("/tmp/rps/final_stats.csv",
  2194. "%" PRIu32 ", " /* index */
  2195. "%s, %" /* id */
  2196. PRIu64 ", %" /* rounds */
  2197. PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* blocking */
  2198. PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* issued */
  2199. PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* sent */
  2200. PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* recv */
  2201. PRIu64 ", %" /* view size */
  2202. PRIu64 ", %" /* known peers */
  2203. PRIu64 ", %" /* valid peers */
  2204. PRIu64 ", %" /* learned peers */
  2205. PRIu64 ", %" /* pending online checks */
  2206. PRIu64 ", %" /* unrequested pull replies */
  2207. PRIu64 ", %" /* peers in push map */
  2208. PRIu64 ", %" /* peers in pull map */
  2209. PRIu64 ", %" /* peers in view */
  2210. PRIu64 "\n"/* view size aim */,
  2211. i,
  2212. GNUNET_i2s (rps_peers[i].peer_id),
  2213. rps_peers[i].stats[STAT_TYPE_ROUNDS],
  2214. rps_peers[i].stats[STAT_TYPE_BLOCKS],
  2215. rps_peers[i].stats[STAT_TYPE_BLOCKS_MANY_PUSH],
  2216. rps_peers[i].stats[STAT_TYPE_BLOCKS_NO_PUSH],
  2217. rps_peers[i].stats[STAT_TYPE_BLOCKS_NO_PULL],
  2218. rps_peers[i].stats[STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL],
  2219. rps_peers[i].stats[STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL],
  2220. rps_peers[i].stats[STAT_TYPE_ISSUED_PUSH_SEND],
  2221. rps_peers[i].stats[STAT_TYPE_ISSUED_PUSH_SEND_MH],
  2222. rps_peers[i].stats[STAT_TYPE_ISSUED_PULL_REQ],
  2223. rps_peers[i].stats[STAT_TYPE_ISSUED_PULL_REQ_MH],
  2224. rps_peers[i].stats[STAT_TYPE_ISSUED_PULL_REP],
  2225. rps_peers[i].stats[STAT_TYPE_SENT_PUSH_SEND],
  2226. rps_peers[i].stats[STAT_TYPE_SENT_PULL_REQ],
  2227. rps_peers[i].stats[STAT_TYPE_SENT_PULL_REQ_MH],
  2228. rps_peers[i].stats[STAT_TYPE_SENT_PULL_REP],
  2229. rps_peers[i].stats[STAT_TYPE_RECV_PUSH_SEND],
  2230. rps_peers[i].stats[STAT_TYPE_RECV_PUSH_SEND_MH],
  2231. rps_peers[i].stats[STAT_TYPE_RECV_PULL_REQ],
  2232. rps_peers[i].stats[STAT_TYPE_RECV_PULL_REQ_MH],
  2233. rps_peers[i].stats[STAT_TYPE_RECV_PULL_REP_MH],
  2234. rps_peers[i].stats[STAT_TYPE_RECV_PULL_REP],
  2235. rps_peers[i].stats[STAT_TYPE_VIEW_SIZE],
  2236. rps_peers[i].stats[STAT_TYPE_KNOWN_PEERS],
  2237. rps_peers[i].stats[STAT_TYPE_VALID_PEERS],
  2238. rps_peers[i].stats[STAT_TYPE_LEARND_PEERS],
  2239. rps_peers[i].stats[STAT_TYPE_PENDING_ONLINE_CHECKS],
  2240. rps_peers[i].stats[STAT_TYPE_UNREQUESTED_PULL_REPLIES],
  2241. rps_peers[i].stats[STAT_TYPE_PEERS_IN_PUSH_MAP],
  2242. rps_peers[i].stats[STAT_TYPE_PEERS_IN_PULL_MAP],
  2243. rps_peers[i].stats[STAT_TYPE_PEERS_IN_VIEW],
  2244. rps_peers[i].stats[STAT_TYPE_VIEW_SIZE_AIM]);
  2245. for (enum STAT_TYPE stat_type = STAT_TYPE_ROUNDS;
  2246. stat_type < STAT_TYPE_MAX;
  2247. stat_type++)
  2248. {
  2249. sums[stat_type] += rps_peers[i].stats[stat_type];
  2250. }
  2251. }
  2252. to_file ("/tmp/rps/final_stats.dat",
  2253. "SUM %"
  2254. PRIu64 " %" /* rounds */
  2255. PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" /* blocking */
  2256. PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* issued */
  2257. PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* sent */
  2258. PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* recv */
  2259. PRIu64 ", %" /* view size */
  2260. PRIu64 ", %" /* known peers */
  2261. PRIu64 ", %" /* valid peers */
  2262. PRIu64 ", %" /* learned peers */
  2263. PRIu64 ", %" /* pending online checks */
  2264. PRIu64 ", %" /* unrequested pull replies */
  2265. PRIu64 ", %" /* peers in push map */
  2266. PRIu64 ", %" /* peers in pull map */
  2267. PRIu64 ", %" /* peers in view */
  2268. PRIu64 "\n"/* view size aim */,
  2269. sums[STAT_TYPE_ROUNDS],
  2270. sums[STAT_TYPE_BLOCKS],
  2271. sums[STAT_TYPE_BLOCKS_MANY_PUSH],
  2272. sums[STAT_TYPE_BLOCKS_NO_PUSH],
  2273. sums[STAT_TYPE_BLOCKS_NO_PULL],
  2274. sums[STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL],
  2275. sums[STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL],
  2276. sums[STAT_TYPE_ISSUED_PUSH_SEND],
  2277. sums[STAT_TYPE_ISSUED_PUSH_SEND_MH],
  2278. sums[STAT_TYPE_ISSUED_PULL_REQ],
  2279. sums[STAT_TYPE_ISSUED_PULL_REQ_MH],
  2280. sums[STAT_TYPE_ISSUED_PULL_REP],
  2281. sums[STAT_TYPE_SENT_PUSH_SEND],
  2282. sums[STAT_TYPE_SENT_PULL_REQ],
  2283. sums[STAT_TYPE_SENT_PULL_REQ_MH],
  2284. sums[STAT_TYPE_SENT_PULL_REP],
  2285. sums[STAT_TYPE_RECV_PUSH_SEND],
  2286. sums[STAT_TYPE_RECV_PUSH_SEND_MH],
  2287. sums[STAT_TYPE_RECV_PULL_REQ],
  2288. sums[STAT_TYPE_RECV_PULL_REQ_MH],
  2289. sums[STAT_TYPE_RECV_PULL_REP],
  2290. sums[STAT_TYPE_RECV_PULL_REP_MH],
  2291. sums[STAT_TYPE_VIEW_SIZE],
  2292. sums[STAT_TYPE_KNOWN_PEERS],
  2293. sums[STAT_TYPE_VALID_PEERS],
  2294. sums[STAT_TYPE_LEARND_PEERS],
  2295. sums[STAT_TYPE_PENDING_ONLINE_CHECKS],
  2296. sums[STAT_TYPE_UNREQUESTED_PULL_REPLIES],
  2297. sums[STAT_TYPE_PEERS_IN_PUSH_MAP],
  2298. sums[STAT_TYPE_PEERS_IN_PULL_MAP],
  2299. sums[STAT_TYPE_PEERS_IN_VIEW],
  2300. sums[STAT_TYPE_VIEW_SIZE_AIM]);
  2301. }
  2302. /**
  2303. * Continuation called by #GNUNET_STATISTICS_get() functions.
  2304. *
  2305. * Remembers that this specific statistics value was received for this peer.
  2306. * Checks whether all peers received their statistics yet.
  2307. * Issues the shutdown.
  2308. *
  2309. * @param cls closure
  2310. * @param success #GNUNET_OK if statistics were
  2311. * successfully obtained, #GNUNET_SYSERR if not.
  2312. */
  2313. void
  2314. post_test_shutdown_ready_cb (void *cls,
  2315. int success)
  2316. {
  2317. struct STATcls *stat_cls = (struct STATcls *) cls;
  2318. struct RPSPeer *rps_peer = stat_cls->rps_peer;
  2319. rps_peer->h_stat_get[stat_cls->stat_type] = NULL;
  2320. if (GNUNET_OK == success)
  2321. {
  2322. /* set flag that we we got the value */
  2323. rps_peer->stat_collected_flags |= BIT(stat_cls->stat_type);
  2324. } else {
  2325. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2326. "Peer %u did not receive statistics value\n",
  2327. rps_peer->index);
  2328. GNUNET_free (stat_cls);
  2329. GNUNET_break (0);
  2330. return;
  2331. }
  2332. if (NULL != rps_peer->stat_op &&
  2333. GNUNET_YES == check_statistics_collect_completed_single_peer (rps_peer))
  2334. {
  2335. GNUNET_TESTBED_operation_done (rps_peer->stat_op);
  2336. }
  2337. write_final_stats ();
  2338. if (GNUNET_YES == check_statistics_collect_completed())
  2339. {
  2340. //write_final_stats ();
  2341. GNUNET_free (stat_cls);
  2342. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2343. "Shutting down\n");
  2344. GNUNET_SCHEDULER_shutdown ();
  2345. } else {
  2346. GNUNET_free (stat_cls);
  2347. }
  2348. }
  2349. /**
  2350. * Callback function to process statistic values.
  2351. *
  2352. * @param cls closure
  2353. * @param subsystem name of subsystem that created the statistic
  2354. * @param name the name of the datum
  2355. * @param value the current value
  2356. * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
  2357. * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
  2358. */
  2359. int
  2360. stat_iterator (void *cls,
  2361. const char *subsystem,
  2362. const char *name,
  2363. uint64_t value,
  2364. int is_persistent)
  2365. {
  2366. const struct STATcls *stat_cls = (const struct STATcls *) cls;
  2367. struct RPSPeer *rps_peer = (struct RPSPeer *) stat_cls->rps_peer;
  2368. enum STAT_TYPE stat_type;
  2369. (void) subsystem;
  2370. (void) is_persistent;
  2371. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2372. "Got stat value: %s - %" PRIu64 " (%u)\n",
  2373. name,
  2374. value,
  2375. rps_peer->index);
  2376. to_file (rps_peer->file_name_stats,
  2377. "%s: %" PRIu64 "\n",
  2378. name,
  2379. value);
  2380. stat_type = stat_str_2_type (name);
  2381. GNUNET_assert (STAT_TYPE_ROUNDS <= stat_type &&
  2382. STAT_TYPE_MAX > stat_type);
  2383. rps_peer->stats[stat_type] = value;
  2384. return GNUNET_OK;
  2385. }
  2386. void
  2387. post_profiler (struct RPSPeer *rps_peer)
  2388. {
  2389. if (COLLECT_STATISTICS != cur_test_run.have_collect_statistics)
  2390. {
  2391. return;
  2392. }
  2393. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2394. "Going to request statistic values with mask 0x%" PRIx32 "\n",
  2395. cur_test_run.stat_collect_flags);
  2396. struct STATcls *stat_cls;
  2397. uint32_t stat_type;
  2398. for (stat_type = STAT_TYPE_ROUNDS;
  2399. stat_type < STAT_TYPE_MAX;
  2400. stat_type++)
  2401. {
  2402. if (BIT(stat_type) & cur_test_run.stat_collect_flags)
  2403. {
  2404. stat_cls = GNUNET_malloc (sizeof (struct STATcls));
  2405. stat_cls->rps_peer = rps_peer;
  2406. stat_cls->stat_type = stat_type;
  2407. rps_peer->file_name_stats =
  2408. store_prefix_file_name (rps_peer->peer_id, "stats");
  2409. rps_peer->h_stat_get[stat_type] =
  2410. GNUNET_STATISTICS_get (rps_peer->stats_h,
  2411. "rps",
  2412. stat_type_strings [stat_type],
  2413. post_test_shutdown_ready_cb,
  2414. stat_iterator,
  2415. (struct STATcls *) stat_cls);
  2416. GNUNET_assert (NULL != rps_peer->h_stat_get);
  2417. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2418. "Requested statistics for %s (peer %" PRIu32 ")\n",
  2419. stat_type_strings [stat_type],
  2420. rps_peer->index);
  2421. }
  2422. }
  2423. }
  2424. /***********************************************************************
  2425. * /Definition of tests
  2426. ***********************************************************************/
  2427. /**
  2428. * Actual "main" function for the testcase.
  2429. *
  2430. * @param cls closure
  2431. * @param h the run handle
  2432. * @param n_peers number of peers in 'peers'
  2433. * @param peers handle to peers run in the testbed
  2434. * @param links_succeeded the number of overlay link connection attempts that
  2435. * succeeded
  2436. * @param links_failed the number of overlay link connection attempts that
  2437. * failed
  2438. */
  2439. static void
  2440. test_run (void *cls,
  2441. struct GNUNET_TESTBED_RunHandle *h,
  2442. unsigned int n_peers,
  2443. struct GNUNET_TESTBED_Peer **peers,
  2444. unsigned int links_succeeded,
  2445. unsigned int links_failed)
  2446. {
  2447. unsigned int i;
  2448. struct OpListEntry *entry;
  2449. (void) cls;
  2450. (void) h;
  2451. (void) links_failed;
  2452. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "RUN was called\n");
  2453. /* Check whether we timed out */
  2454. if (n_peers != num_peers ||
  2455. NULL == peers ||
  2456. 0 == links_succeeded)
  2457. {
  2458. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Going down due to args (eg. timeout)\n");
  2459. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tn_peers: %u\n", n_peers);
  2460. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tnum_peers: %" PRIu32 "\n", num_peers);
  2461. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tpeers: %p\n", peers);
  2462. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tlinks_succeeded: %u\n", links_succeeded);
  2463. ok = 1;
  2464. GNUNET_SCHEDULER_shutdown ();
  2465. return;
  2466. }
  2467. /* Initialize peers */
  2468. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "going to initialise peers\n");
  2469. testbed_peers = peers;
  2470. num_peers_online = 0;
  2471. for (i = 0; i < num_peers; i++)
  2472. {
  2473. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "initialising %u\n", i);
  2474. entry = make_oplist_entry ();
  2475. entry->index = i;
  2476. rps_peers[i].index = i;
  2477. if (NULL != cur_test_run.init_peer)
  2478. cur_test_run.init_peer (&rps_peers[i]);
  2479. if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
  2480. {
  2481. rps_peers->cur_view_count = 0;
  2482. rps_peers->cur_view = NULL;
  2483. }
  2484. entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
  2485. GNUNET_TESTBED_PIT_IDENTITY,
  2486. &info_cb,
  2487. entry);
  2488. }
  2489. /* Bring peers up */
  2490. GNUNET_assert (num_peers == n_peers);
  2491. for (i = 0; i < n_peers; i++)
  2492. {
  2493. rps_peers[i].index = i;
  2494. rps_peers[i].op =
  2495. GNUNET_TESTBED_service_connect (&rps_peers[i],
  2496. peers[i],
  2497. "rps",
  2498. &rps_connect_complete_cb,
  2499. &rps_peers[i],
  2500. &rps_connect_adapter,
  2501. &rps_disconnect_adapter,
  2502. &rps_peers[i]);
  2503. /* Connect all peers to statistics service */
  2504. if (COLLECT_STATISTICS == cur_test_run.have_collect_statistics)
  2505. {
  2506. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2507. "Connecting to statistics service\n");
  2508. rps_peers[i].stat_op =
  2509. GNUNET_TESTBED_service_connect (NULL,
  2510. peers[i],
  2511. "statistics",
  2512. stat_complete_cb,
  2513. &rps_peers[i],
  2514. &stat_connect_adapter,
  2515. &stat_disconnect_adapter,
  2516. &rps_peers[i]);
  2517. }
  2518. }
  2519. if (NULL != churn_task)
  2520. GNUNET_SCHEDULER_cancel (churn_task);
  2521. post_test_task = GNUNET_SCHEDULER_add_delayed (duration, &post_test_op, NULL);
  2522. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout for shutdown is %lu\n", timeout.rel_value_us/1000000);
  2523. shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout,
  2524. &trigger_shutdown,
  2525. NULL);
  2526. GNUNET_SCHEDULER_add_shutdown (shutdown_op, NULL);
  2527. }
  2528. /**
  2529. * Entry point for the testcase, sets up the testbed.
  2530. *
  2531. * @param argc unused
  2532. * @param argv unused
  2533. */
  2534. static void
  2535. run (void *cls,
  2536. char *const *args,
  2537. const char *cfgfile,
  2538. const struct GNUNET_CONFIGURATION_Handle *cfg)
  2539. {
  2540. //int ret_value;
  2541. (void) cls;
  2542. (void) args;
  2543. (void) cfgfile;
  2544. /* Defaults for tests */
  2545. churn_task = NULL;
  2546. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
  2547. cur_test_run.name = "test-rps-profiler";
  2548. if (0 == num_peers)
  2549. num_peers = 10;
  2550. mal_type = 3;
  2551. cur_test_run.init_peer = profiler_init_peer;
  2552. //cur_test_run.pre_test = mal_pre;
  2553. cur_test_run.pre_test = pre_profiler;
  2554. cur_test_run.main_test = profiler_cb;
  2555. cur_test_run.reply_handle = profiler_reply_handle;
  2556. cur_test_run.eval_cb = profiler_eval;
  2557. cur_test_run.post_test = post_profiler;
  2558. cur_test_run.request_interval = 2;
  2559. if (0 == cur_test_run.num_requests) cur_test_run.num_requests = 5;
  2560. //cur_test_run.have_churn = HAVE_CHURN;
  2561. cur_test_run.have_churn = HAVE_NO_CHURN;
  2562. cur_test_run.have_quick_quit = HAVE_QUICK_QUIT;
  2563. cur_test_run.have_collect_statistics = COLLECT_STATISTICS;
  2564. cur_test_run.stat_collect_flags = BIT(STAT_TYPE_ROUNDS) |
  2565. BIT(STAT_TYPE_BLOCKS) |
  2566. BIT(STAT_TYPE_BLOCKS_MANY_PUSH) |
  2567. BIT(STAT_TYPE_BLOCKS_NO_PUSH) |
  2568. BIT(STAT_TYPE_BLOCKS_NO_PULL) |
  2569. BIT(STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL) |
  2570. BIT(STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL) |
  2571. BIT(STAT_TYPE_ISSUED_PUSH_SEND) |
  2572. BIT(STAT_TYPE_ISSUED_PUSH_SEND_MH) |
  2573. BIT(STAT_TYPE_ISSUED_PULL_REQ) |
  2574. BIT(STAT_TYPE_ISSUED_PULL_REQ_MH) |
  2575. BIT(STAT_TYPE_ISSUED_PULL_REP) |
  2576. BIT(STAT_TYPE_SENT_PUSH_SEND) |
  2577. BIT(STAT_TYPE_SENT_PULL_REQ) |
  2578. BIT(STAT_TYPE_SENT_PULL_REQ_MH) |
  2579. BIT(STAT_TYPE_SENT_PULL_REP) |
  2580. BIT(STAT_TYPE_RECV_PUSH_SEND) |
  2581. BIT(STAT_TYPE_RECV_PUSH_SEND_MH) |
  2582. BIT(STAT_TYPE_RECV_PULL_REQ) |
  2583. BIT(STAT_TYPE_RECV_PULL_REQ_MH) |
  2584. BIT(STAT_TYPE_RECV_PULL_REP) |
  2585. BIT(STAT_TYPE_RECV_PULL_REP_MH) |
  2586. BIT(STAT_TYPE_VIEW_SIZE) |
  2587. BIT(STAT_TYPE_KNOWN_PEERS) |
  2588. BIT(STAT_TYPE_VALID_PEERS) |
  2589. BIT(STAT_TYPE_LEARND_PEERS) |
  2590. BIT(STAT_TYPE_PENDING_ONLINE_CHECKS) |
  2591. BIT(STAT_TYPE_UNREQUESTED_PULL_REPLIES) |
  2592. BIT(STAT_TYPE_PEERS_IN_PUSH_MAP) |
  2593. BIT(STAT_TYPE_PEERS_IN_PULL_MAP) |
  2594. BIT(STAT_TYPE_PEERS_IN_VIEW) |
  2595. BIT(STAT_TYPE_VIEW_SIZE_AIM);
  2596. cur_test_run.have_collect_view = COLLECT_VIEW;
  2597. /* 'Clean' directory */
  2598. (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
  2599. GNUNET_DISK_directory_create ("/tmp/rps/");
  2600. if (0 == duration.rel_value_us)
  2601. {
  2602. if (0 == timeout.rel_value_us)
  2603. {
  2604. duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90);
  2605. timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
  2606. (90 * 1.2) +
  2607. (0.01 * num_peers));
  2608. }
  2609. else
  2610. {
  2611. duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
  2612. (timeout.rel_value_us/1000000)
  2613. * 0.75);
  2614. }
  2615. }
  2616. else
  2617. {
  2618. if (0 == timeout.rel_value_us)
  2619. {
  2620. timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
  2621. ((duration.rel_value_us/1000000)
  2622. * 1.2) + (0.01 * num_peers));
  2623. }
  2624. }
  2625. GNUNET_assert (duration.rel_value_us < timeout.rel_value_us);
  2626. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2627. "duration is %lus\n",
  2628. duration.rel_value_us/1000000);
  2629. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2630. "timeout is %lus\n",
  2631. timeout.rel_value_us/1000000);
  2632. /* Compute number of bits for representing largest peer id */
  2633. for (bits_needed = 1; (1 << bits_needed) < num_peers; bits_needed++)
  2634. ;
  2635. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2636. "Need %u bits to represent %" PRIu32 " peers\n",
  2637. bits_needed,
  2638. num_peers);
  2639. rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
  2640. peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
  2641. rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
  2642. if ( (2 == mal_type) ||
  2643. (3 == mal_type))
  2644. target_peer = &rps_peer_ids[num_peers - 2];
  2645. ok = 1;
  2646. GNUNET_TESTBED_run (NULL,
  2647. cfg,
  2648. num_peers,
  2649. 0, /* event mask */
  2650. NULL,
  2651. NULL,
  2652. &test_run,
  2653. NULL);
  2654. }
  2655. /**
  2656. * Entry point for the testcase, sets up the testbed.
  2657. *
  2658. * @param argc unused
  2659. * @param argv unused
  2660. * @return 0 on success
  2661. */
  2662. int
  2663. main (int argc, char *argv[])
  2664. {
  2665. int ret_value;
  2666. struct GNUNET_GETOPT_CommandLineOption options[] = {
  2667. GNUNET_GETOPT_option_uint ('n',
  2668. "num-peers",
  2669. "COUNT",
  2670. gettext_noop ("number of peers to start"),
  2671. &num_peers),
  2672. GNUNET_GETOPT_option_relative_time ('d',
  2673. "duration",
  2674. "DURATION",
  2675. gettext_noop ("duration of the profiling"),
  2676. &duration),
  2677. GNUNET_GETOPT_option_relative_time ('t',
  2678. "timeout",
  2679. "TIMEOUT",
  2680. gettext_noop ("timeout for the profiling"),
  2681. &timeout),
  2682. GNUNET_GETOPT_option_uint ('r',
  2683. "num-requests",
  2684. "COUNT",
  2685. gettext_noop ("number of PeerIDs to request"),
  2686. &cur_test_run.num_requests),
  2687. GNUNET_GETOPT_OPTION_END
  2688. };
  2689. unsetenv ("XDG_DATA_HOME");
  2690. unsetenv ("XDG_CONFIG_HOME");
  2691. //if (GNUNET_OK !=
  2692. // GNUNET_STRINGS_get_utf8_args (argc, argv,
  2693. // &argc, &argv))
  2694. // return 2;
  2695. ret_value = 0;
  2696. if (GNUNET_OK !=
  2697. GNUNET_PROGRAM_run (argc,
  2698. argv,
  2699. "gnunet-rps-profiler",
  2700. gettext_noop ("Measure quality and performance of the RPS service."),
  2701. options,
  2702. &run,
  2703. NULL))
  2704. {
  2705. ret_value = 1;
  2706. }
  2707. if (0 != ret_value)
  2708. {
  2709. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2710. "Test did not run successfully!\n");
  2711. }
  2712. else
  2713. {
  2714. ret_value = cur_test_run.eval_cb();
  2715. if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
  2716. {
  2717. GNUNET_array_grow (rps_peers->cur_view,
  2718. rps_peers->cur_view_count,
  2719. 0);
  2720. }
  2721. GNUNET_free (rps_peers);
  2722. GNUNET_free (rps_peer_ids);
  2723. GNUNET_CONTAINER_multipeermap_destroy (peer_map);
  2724. }
  2725. return ret_value;
  2726. }
  2727. /* end of test_rps.c */