gnunet-rps-profiler.c 93 KB

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