gnunet-ats-solver-eval.c 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010-2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file ats-tests/ats-testing-experiment.c
  19. * @brief ats benchmark: controlled experiment execution
  20. * @author Christian Grothoff
  21. * @author Matthias Wachs
  22. */
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet-ats-solver-eval.h"
  26. #include "gnunet-service-ats_normalization.h"
  27. #include "gnunet-service-ats_preferences.c"
  28. #define BIG_M_STRING "unlimited"
  29. /**
  30. * Handle for statistics.
  31. */
  32. struct GNUNET_STATISTICS_Handle *GSA_stats;
  33. static struct Experiment *e;
  34. static struct LoggingHandle *l;
  35. static struct SolverHandle *sh;
  36. static struct TestPeer *peer_head;
  37. static struct TestPeer *peer_tail;
  38. static double default_properties[GNUNET_ATS_PropertyCount];
  39. static double default_preferences[GNUNET_ATS_PreferenceCount];
  40. /**
  41. * cmd option -e: experiment file
  42. */
  43. static char *opt_exp_file;
  44. static char *opt_solver;
  45. /**
  46. * cmd option -l: enable logging
  47. */
  48. static int opt_log;
  49. /**
  50. * cmd option -p: enable plots
  51. */
  52. static int opt_save;
  53. /**
  54. * cmd option -v: verbose logs
  55. */
  56. static int opt_verbose;
  57. /**
  58. * cmd option -p: print logs
  59. */
  60. static int opt_print;
  61. /**
  62. * cmd option -d: disable normalization
  63. */
  64. static int opt_disable_normalization;
  65. static int res;
  66. static void
  67. end_now ();
  68. const double *
  69. get_property_cb (void *cls, const struct ATS_Address *address);
  70. static char *
  71. print_generator_type (enum GeneratorType g)
  72. {
  73. switch (g) {
  74. case GNUNET_ATS_TEST_TG_CONSTANT:
  75. return "CONSTANT";
  76. case GNUNET_ATS_TEST_TG_LINEAR:
  77. return "LINEAR";
  78. case GNUNET_ATS_TEST_TG_RANDOM:
  79. return "RANDOM";
  80. case GNUNET_ATS_TEST_TG_SINUS:
  81. return "SINUS";
  82. default:
  83. return "INVALID";
  84. break;
  85. }
  86. }
  87. static struct TestPeer *
  88. find_peer_by_id (int id)
  89. {
  90. struct TestPeer *cur;
  91. for (cur = peer_head; NULL != cur; cur = cur->next)
  92. if (cur->id == id)
  93. return cur;
  94. return NULL;
  95. }
  96. static struct TestPeer *
  97. find_peer_by_pid (const struct GNUNET_PeerIdentity *pid)
  98. {
  99. struct TestPeer *cur;
  100. for (cur = peer_head; NULL != cur; cur = cur->next)
  101. if (0 == memcmp (&cur->peer_id, pid, sizeof (struct GNUNET_PeerIdentity)))
  102. return cur;
  103. return NULL;
  104. }
  105. static struct TestAddress *
  106. find_address_by_id (struct TestPeer *peer, int aid)
  107. {
  108. struct TestAddress *cur;
  109. for (cur = peer->addr_head; NULL != cur; cur = cur->next)
  110. if (cur->aid == aid)
  111. return cur;
  112. return NULL;
  113. }
  114. static struct TestAddress *
  115. find_address_by_ats_address (struct TestPeer *p, const struct ATS_Address *addr)
  116. {
  117. struct TestAddress *cur;
  118. for (cur = p->addr_head; NULL != cur; cur = cur->next)
  119. if ((0 == strcmp(cur->ats_addr->plugin, addr->plugin)) &&
  120. (cur->ats_addr->addr_len == addr->addr_len) &&
  121. (0 == memcmp (cur->ats_addr->addr, addr->addr, addr->addr_len)))
  122. return cur;
  123. return NULL;
  124. }
  125. /**
  126. * Logging
  127. */
  128. void
  129. GNUNET_ATS_solver_logging_now (struct LoggingHandle *l)
  130. {
  131. struct LoggingTimeStep *lts;
  132. struct TestPeer *cur;
  133. struct TestAddress *cur_addr;
  134. struct LoggingPeer *log_p;
  135. struct LoggingAddress *log_a;
  136. int c;
  137. lts = GNUNET_new (struct LoggingTimeStep);
  138. GNUNET_CONTAINER_DLL_insert_tail(l->head, l->tail, lts);
  139. lts->timestamp = GNUNET_TIME_absolute_get();
  140. if (NULL == lts->prev)
  141. lts->delta = GNUNET_TIME_UNIT_ZERO;
  142. else
  143. lts->delta = GNUNET_TIME_absolute_get_duration(lts->prev->timestamp);
  144. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Logging %llu, delta %llu\n",
  145. lts->timestamp.abs_value_us, lts->delta.rel_value_us);
  146. /* Store logging data here */
  147. for (cur = peer_head; NULL != cur; cur = cur->next)
  148. {
  149. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Logging peer id %llu\n", cur->id);
  150. log_p = GNUNET_new (struct LoggingPeer);
  151. log_p->id = cur->id;
  152. log_p->peer_id = cur->peer_id;
  153. log_p->is_requested = cur->is_requested;
  154. for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
  155. {
  156. log_p->pref_abs[c] = cur->pref_abs[c];
  157. log_p->pref_norm[c] = cur->pref_norm[c];
  158. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\t %s = %.2f %.2f [abs/rel]\n",
  159. GNUNET_ATS_print_preference_type(c),
  160. log_p->pref_abs[c], log_p->pref_norm[c]);
  161. }
  162. GNUNET_CONTAINER_DLL_insert_tail(lts->head, lts->tail, log_p);
  163. for (cur_addr = cur->addr_head; NULL != cur_addr; cur_addr = cur_addr->next)
  164. {
  165. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Logging peer id %llu address %llu \n",
  166. cur->id, cur_addr->aid);
  167. log_a = GNUNET_new (struct LoggingAddress);
  168. log_a->aid = cur_addr->aid;
  169. log_a->active = cur_addr->ats_addr->active;
  170. log_a->network = cur_addr->network;
  171. log_a->assigned_bw_in = cur_addr->ats_addr->assigned_bw_in;
  172. log_a->assigned_bw_out = cur_addr->ats_addr->assigned_bw_out;
  173. for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
  174. {
  175. log_a->prop_abs[c] = cur_addr->prop_abs[c];
  176. log_a->prop_norm[c] = cur_addr->prop_norm[c];
  177. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\t %s = %.2f %.2f [abs/rel]\n",
  178. GNUNET_ATS_print_property_type(c),
  179. log_a->prop_abs[c], log_a->prop_norm[c]);
  180. }
  181. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\t Active = %i\n", log_a->active);
  182. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\t BW in = %llu\n", log_a->assigned_bw_in);
  183. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "\t BW out = %llu\n", log_a->assigned_bw_out);
  184. GNUNET_CONTAINER_DLL_insert_tail (log_p->addr_head, log_p->addr_tail, log_a);
  185. }
  186. }
  187. }
  188. static void
  189. logging_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  190. {
  191. struct LoggingHandle *l = cls;
  192. l->logging_task = NULL;
  193. GNUNET_ATS_solver_logging_now (l);
  194. l->logging_task = GNUNET_SCHEDULER_add_delayed (l->log_freq, &logging_task, l);
  195. }
  196. struct LoggingHandle *
  197. GNUNET_ATS_solver_logging_start (struct GNUNET_TIME_Relative freq)
  198. {
  199. struct LoggingHandle *l;
  200. l = GNUNET_new (struct LoggingHandle);
  201. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start logging every %s\n",
  202. GNUNET_STRINGS_relative_time_to_string(freq, GNUNET_NO));
  203. l->log_freq = freq;
  204. l->logging_task = GNUNET_SCHEDULER_add_now (&logging_task, l);
  205. return l;
  206. }
  207. void
  208. GNUNET_ATS_solver_logging_stop (struct LoggingHandle *l)
  209. {
  210. if (NULL != l->logging_task)
  211. GNUNET_SCHEDULER_cancel (l->logging_task);
  212. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stop logging\n");
  213. l->logging_task = NULL;
  214. }
  215. static struct LoggingFileHandle *
  216. find_logging_file_handle (struct LoggingFileHandle *lf_head,
  217. struct LoggingFileHandle *lf_tail,
  218. int peer_id, int address_id)
  219. {
  220. struct LoggingFileHandle *res;
  221. for (res = lf_head; NULL != res; res = res->next)
  222. if ((res->pid == peer_id) && (res->aid == address_id))
  223. return res;
  224. return NULL;
  225. }
  226. void
  227. GNUNET_ATS_solver_logging_write_to_disk (struct LoggingHandle *l, int add_time_stamp,
  228. char *output_dir)
  229. {
  230. struct LoggingTimeStep *lts;
  231. struct LoggingPeer *log_p;
  232. struct LoggingAddress *log_a;
  233. struct LoggingFileHandle *lf_head;
  234. struct LoggingFileHandle *lf_tail;
  235. struct LoggingFileHandle *cur;
  236. struct LoggingFileHandle *next;
  237. char * filename;
  238. char * datastring;
  239. char * propstring;
  240. char * propstring_tmp;
  241. char * prefstring;
  242. char * prefstring_tmp;
  243. int c;
  244. int use_dir;
  245. use_dir = GNUNET_NO;
  246. if (NULL != output_dir)
  247. {
  248. if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (output_dir))
  249. {
  250. fprintf (stderr, "Failed to create directory `%s'\n", output_dir);
  251. return;
  252. }
  253. else
  254. {
  255. fprintf (stderr, "Created directory `%s'\n", output_dir);
  256. use_dir = GNUNET_YES;
  257. }
  258. }
  259. lf_head = NULL;
  260. lf_tail = NULL;
  261. for (lts = l->head; NULL != lts; lts = lts->next)
  262. {
  263. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Writing log step %llu\n",
  264. (long long unsigned int) lts->timestamp.abs_value_us);
  265. for (log_p = lts->head; NULL != log_p; log_p = log_p->next)
  266. {
  267. for (log_a = log_p->addr_head; NULL != log_a; log_a = log_a->next)
  268. {
  269. cur = find_logging_file_handle (lf_head, lf_tail, log_p->id,
  270. log_a->aid);
  271. if (NULL == cur)
  272. {
  273. cur = GNUNET_new (struct LoggingFileHandle);
  274. cur->aid = log_a->aid;
  275. cur->pid = log_p->id;
  276. if (GNUNET_YES == add_time_stamp)
  277. GNUNET_asprintf (&filename, "%s%s%s_%s_p%u_a%u_%llu.log",
  278. (GNUNET_YES == use_dir) ? output_dir : "",
  279. (GNUNET_YES == use_dir) ? DIR_SEPARATOR_STR : "",
  280. e->log_prefix,
  281. opt_solver,
  282. cur->pid,
  283. cur->aid,
  284. l->head->timestamp.abs_value_us);
  285. else
  286. GNUNET_asprintf (&filename, "%s%s%s_%s_p%u_a%u.log",
  287. (GNUNET_YES == use_dir) ? output_dir : "",
  288. (GNUNET_YES == use_dir) ? DIR_SEPARATOR_STR : "",
  289. e->log_prefix,
  290. opt_solver,
  291. cur->pid,
  292. cur->aid);
  293. fprintf (stderr, "Add writing log data for peer %llu address %llu to file `%s'\n",
  294. cur->pid, cur->aid, filename);
  295. cur->f_hd = GNUNET_DISK_file_open (filename,
  296. GNUNET_DISK_OPEN_READWRITE |
  297. GNUNET_DISK_OPEN_CREATE |
  298. GNUNET_DISK_OPEN_TRUNCATE,
  299. GNUNET_DISK_PERM_USER_READ |
  300. GNUNET_DISK_PERM_USER_WRITE |
  301. GNUNET_DISK_PERM_GROUP_READ |
  302. GNUNET_DISK_PERM_OTHER_READ);
  303. if (NULL == cur->f_hd)
  304. {
  305. fprintf (stderr, "Cannot open `%s' to write log data!\n", filename);
  306. GNUNET_free (filename);
  307. GNUNET_free (cur);
  308. goto cleanup;
  309. }
  310. GNUNET_free (filename);
  311. GNUNET_CONTAINER_DLL_insert (lf_head, lf_tail, cur);
  312. GNUNET_asprintf(&datastring,"#time delta;log duration;peer_requested;addr net; addr_active; bw in; bw out; " \
  313. "UTILIZATION_UP [abs/rel]; UTILIZATION_UP; UTILIZATION_DOWN; UTILIZATION_DOWN; " \
  314. "UTILIZATION_PAYLOAD_UP; UTILIZATION_PAYLOAD_UP; UTILIZATION_PAYLOAD_DOWN; UTILIZATION_PAYLOAD_DOWN;"\
  315. "DELAY; DELAY; " \
  316. "DISTANCE ;DISTANCE ; COST_WAN; COST_WAN; COST_LAN; COST_LAN; " \
  317. "COST_WLAN; COST_WLAN;COST_BT; COST_BT; PREF BW abs; PREF BW rel; PREF LATENCY abs; PREF LATENCY rel;\n");
  318. GNUNET_DISK_file_write (cur->f_hd, datastring, strlen(datastring));
  319. GNUNET_free (datastring);
  320. }
  321. prefstring = GNUNET_strdup("");
  322. for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
  323. {
  324. /*
  325. fprintf(stderr,"\t %s = %.2f %.2f [abs/rel]\n",
  326. GNUNET_ATS_print_preference_type(c),
  327. log_p->pref_abs[c], log_p->pref_norm[c]);
  328. */
  329. GNUNET_asprintf(&prefstring_tmp,"%s;%.3f;%.3f",
  330. prefstring, log_p->pref_abs[c], log_p->pref_norm[c]);
  331. GNUNET_free (prefstring);
  332. prefstring = GNUNET_strdup(prefstring_tmp);
  333. GNUNET_free (prefstring_tmp);
  334. }
  335. propstring = GNUNET_strdup("");
  336. for (c = 1; c < GNUNET_ATS_PropertyCount; c++)
  337. {
  338. if (GNUNET_ATS_NETWORK_TYPE == c)
  339. continue;
  340. /*
  341. fprintf(stderr, "\t %s = %.2f %.2f [abs/rel]\n",
  342. GNUNET_ATS_print_property_type(c),
  343. log_a->prop_abs[c], log_a->prop_norm[c]);*/
  344. GNUNET_asprintf(&propstring_tmp,"%s%.3f;%.3f;",
  345. propstring, log_a->prop_abs[c], log_a->prop_norm[c]);
  346. GNUNET_free (propstring);
  347. propstring = GNUNET_strdup(propstring_tmp);
  348. GNUNET_free (propstring_tmp);
  349. }
  350. GNUNET_asprintf (&datastring, "%llu;%llu;%u;%u;%i;%u;%u;%s;%s\n",
  351. GNUNET_TIME_absolute_get_difference (l->head->timestamp,
  352. lts->timestamp).rel_value_us / 1000, lts->delta,
  353. log_p->is_requested, log_a->network, log_a->active,
  354. log_a->assigned_bw_in, log_a->assigned_bw_out, propstring,
  355. prefstring);
  356. GNUNET_DISK_file_write (cur->f_hd, datastring, strlen(datastring));
  357. GNUNET_free (datastring);
  358. GNUNET_free (prefstring);
  359. GNUNET_free (propstring);
  360. }
  361. }
  362. }
  363. cleanup:
  364. next = lf_head;
  365. for (cur = next; NULL != cur; cur = next)
  366. {
  367. next = cur->next;
  368. GNUNET_CONTAINER_DLL_remove (lf_head, lf_tail, cur);
  369. if (NULL != cur->f_hd)
  370. GNUNET_DISK_file_close (cur->f_hd);
  371. GNUNET_free (cur);
  372. }
  373. }
  374. void
  375. GNUNET_ATS_solver_logging_eval (struct LoggingHandle *l)
  376. {
  377. struct LoggingTimeStep *lts;
  378. struct LoggingPeer *log_p;
  379. struct LoggingAddress *log_a;
  380. int c;
  381. for (lts = l->head; NULL != lts; lts = lts->next)
  382. {
  383. fprintf (stderr, "Log step %llu %llu: \n",
  384. (long long unsigned int) lts->timestamp.abs_value_us,
  385. (long long unsigned int) lts->delta.rel_value_us);
  386. for (log_p = lts->head; NULL != log_p; log_p = log_p->next)
  387. {
  388. fprintf (stderr,"\tLogging peer pid %llu\n", log_p->id);
  389. for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
  390. {
  391. fprintf(stderr,"\t %s = %.2f %.2f [abs/rel]\n",
  392. GNUNET_ATS_print_preference_type(c),
  393. log_p->pref_abs[c], log_p->pref_norm[c]);
  394. }
  395. for (log_a = log_p->addr_head; NULL != log_a; log_a = log_a->next)
  396. {
  397. fprintf (stderr, "\tPeer pid %llu address %llu: %u %u %u\n",
  398. log_p->id, log_a->aid, log_a->active,
  399. log_a->assigned_bw_in,
  400. log_a->assigned_bw_out);
  401. for (c = 1; c < GNUNET_ATS_PropertyCount; c++)
  402. {
  403. if (GNUNET_ATS_NETWORK_TYPE == c)
  404. continue;
  405. fprintf(stderr, "\t %s = %.2f %.2f [abs/rel]\n",
  406. GNUNET_ATS_print_property_type(c),
  407. log_a->prop_abs[c], log_a->prop_norm[c]);
  408. }
  409. }
  410. }
  411. }
  412. }
  413. void
  414. GNUNET_ATS_solver_logging_free (struct LoggingHandle *l)
  415. {
  416. struct LoggingTimeStep *lts_cur;
  417. struct LoggingTimeStep *lts_next;
  418. struct LoggingPeer *log_p_cur;
  419. struct LoggingPeer *log_p_next;
  420. struct LoggingAddress *log_a_cur;
  421. struct LoggingAddress *log_a_next;
  422. if (NULL != l->logging_task)
  423. GNUNET_SCHEDULER_cancel (l->logging_task);
  424. l->logging_task = NULL;
  425. lts_next = l->head;
  426. while (NULL != (lts_cur = lts_next))
  427. {
  428. lts_next = lts_cur->next;
  429. log_p_next = lts_cur->head;
  430. while (NULL != (log_p_cur = log_p_next))
  431. {
  432. log_p_next = log_p_cur->next;
  433. log_a_next = log_p_cur->addr_head;
  434. while (NULL != (log_a_cur = log_a_next))
  435. {
  436. log_a_next = log_a_cur->next;
  437. GNUNET_CONTAINER_DLL_remove (log_p_cur->addr_head, log_p_cur->addr_tail, log_a_cur);
  438. GNUNET_free (log_a_cur);
  439. }
  440. GNUNET_CONTAINER_DLL_remove (lts_cur->head, lts_cur->tail, log_p_cur);
  441. GNUNET_free (log_p_cur);
  442. }
  443. GNUNET_CONTAINER_DLL_remove (l->head, l->tail, lts_cur);
  444. GNUNET_free (lts_cur);
  445. }
  446. GNUNET_free (l);
  447. }
  448. /**
  449. * Property Generators
  450. */
  451. static struct PropertyGenerator *prop_gen_head;
  452. static struct PropertyGenerator *prop_gen_tail;
  453. static double
  454. get_property (struct PropertyGenerator *pg)
  455. {
  456. struct GNUNET_TIME_Relative time_delta;
  457. double delta_value;
  458. double pref_value;
  459. /* Calculate the current preference value */
  460. switch (pg->type) {
  461. case GNUNET_ATS_TEST_TG_CONSTANT:
  462. pref_value = pg->base_value;
  463. break;
  464. case GNUNET_ATS_TEST_TG_LINEAR:
  465. time_delta = GNUNET_TIME_absolute_get_duration(pg->time_start);
  466. /* Calculate point of time in the current period */
  467. time_delta.rel_value_us = time_delta.rel_value_us %
  468. pg->duration_period.rel_value_us;
  469. delta_value = ((double) time_delta.rel_value_us /
  470. pg->duration_period.rel_value_us) * (pg->max_value - pg->base_value);
  471. if ((pg->max_value < pg->base_value) &&
  472. ((pg->max_value - pg->base_value) > pg->base_value))
  473. {
  474. /* This will cause an underflow */
  475. GNUNET_break (0);
  476. }
  477. pref_value = pg->base_value + delta_value;
  478. break;
  479. case GNUNET_ATS_TEST_TG_RANDOM:
  480. delta_value = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
  481. 10000 * (pg->max_value - pg->base_value)) / 10000;
  482. pref_value = pg->base_value + delta_value;
  483. break;
  484. case GNUNET_ATS_TEST_TG_SINUS:
  485. time_delta = GNUNET_TIME_absolute_get_duration(pg->time_start);
  486. /* Calculate point of time in the current period */
  487. time_delta.rel_value_us = time_delta.rel_value_us %
  488. pg->duration_period.rel_value_us;
  489. if ((pg->max_value - pg->base_value) > pg->base_value)
  490. {
  491. /* This will cause an underflow for second half of sinus period,
  492. * will be detected in general when experiments are loaded */
  493. GNUNET_break (0);
  494. }
  495. delta_value = (pg->max_value - pg->base_value) *
  496. sin ( (2 * M_PI) / ((double) pg->duration_period.rel_value_us) *
  497. time_delta.rel_value_us);
  498. pref_value = pg->base_value + delta_value;
  499. break;
  500. default:
  501. pref_value = 0.0;
  502. break;
  503. }
  504. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Current property value is %f\n",
  505. pref_value);
  506. return pref_value;
  507. }
  508. static void
  509. set_prop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  510. {
  511. struct PropertyGenerator *pg = cls;
  512. struct TestPeer *p;
  513. struct TestAddress *a;
  514. double prop_value;
  515. struct GNUNET_ATS_Information atsi;
  516. pg->set_task = NULL;
  517. if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains_value (sh->addresses,
  518. &pg->test_peer->peer_id, pg->test_address->ats_addr))
  519. {
  520. GNUNET_break (0);
  521. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  522. "Setting property generation for unknown address [%u:%u]\n",
  523. pg->peer, pg->address_id);
  524. return;
  525. }
  526. if (NULL == (p = find_peer_by_id (pg->peer)))
  527. {
  528. GNUNET_break (0);
  529. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  530. "Setting property generation for unknown peer %u\n",
  531. pg->peer);
  532. return;
  533. }
  534. if (NULL == (a = find_address_by_id (p, pg->address_id)))
  535. {
  536. GNUNET_break (0);
  537. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  538. "Setting property generation for unknown peer %u\n",
  539. pg->peer);
  540. return;
  541. }
  542. prop_value = get_property (pg);
  543. a->prop_abs[pg->ats_property] = prop_value;
  544. GNUNET_log(GNUNET_ERROR_TYPE_INFO,
  545. "Setting property for peer [%u] address [%u] for %s to %f\n",
  546. pg->peer, pg->address_id,
  547. GNUNET_ATS_print_property_type (pg->ats_property), prop_value);
  548. atsi.type = htonl (pg->ats_property);
  549. atsi.value = htonl ((uint32_t) prop_value);
  550. /* set performance here! */
  551. sh->sf->s_bulk_start (sh->sf->cls);
  552. if (GNUNET_YES == opt_disable_normalization)
  553. {
  554. a->prop_abs[pg->ats_property] = prop_value;
  555. a->prop_norm[pg->ats_property] = prop_value;
  556. sh->sf->s_address_update_property (sh->sf->cls, a->ats_addr,
  557. pg->ats_property, prop_value, prop_value);
  558. }
  559. else
  560. GAS_normalization_normalize_property (pg->test_address->ats_addr, &atsi, 1);
  561. sh->sf->s_bulk_stop (sh->sf->cls);
  562. pg->set_task = GNUNET_SCHEDULER_add_delayed (pg->frequency,
  563. &set_prop_task, pg);
  564. }
  565. /**
  566. * Set ats_property to 0 to find all pgs
  567. */
  568. static struct PropertyGenerator *
  569. find_prop_gen (unsigned int peer, unsigned int address,
  570. uint32_t ats_property)
  571. {
  572. struct PropertyGenerator *cur;
  573. for (cur = prop_gen_head; NULL != cur; cur = cur->next)
  574. if ((cur->peer == peer) && (cur->address_id == address))
  575. {
  576. if ((cur->ats_property == ats_property) || (0 == ats_property))
  577. return cur;
  578. }
  579. return NULL;
  580. }
  581. void
  582. GNUNET_ATS_solver_generate_property_stop (struct PropertyGenerator *pg)
  583. {
  584. GNUNET_CONTAINER_DLL_remove (prop_gen_head, prop_gen_tail, pg);
  585. if (NULL != pg->set_task)
  586. {
  587. GNUNET_SCHEDULER_cancel (pg->set_task);
  588. pg->set_task = NULL;
  589. }
  590. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  591. "Removing old up preference generator peer [%u] address [%u] `%s'\n",
  592. pg->peer, pg->address_id,
  593. GNUNET_ATS_print_property_type(pg->ats_property));
  594. GNUNET_free (pg);
  595. }
  596. /**
  597. * Generate between the source master and the partner and set property with a
  598. * value depending on the generator.
  599. *
  600. * @param peer source
  601. * @param address_id partner
  602. * @param test_peer the peer
  603. * @param test_address the address
  604. * @param type type of generator
  605. * @param base_value base value
  606. * @param value_rate maximum value
  607. * @param period duration of a period of generation (~ 1/frequency)
  608. * @param frequency how long to generate property
  609. * @param ats_property ATS property to generate
  610. * @return the property generator
  611. */
  612. struct PropertyGenerator *
  613. GNUNET_ATS_solver_generate_property_start (unsigned int peer,
  614. unsigned int address_id,
  615. struct TestPeer *test_peer,
  616. struct TestAddress *test_address,
  617. enum GeneratorType type,
  618. long int base_value,
  619. long int value_rate,
  620. struct GNUNET_TIME_Relative period,
  621. struct GNUNET_TIME_Relative frequency,
  622. uint32_t ats_property)
  623. {
  624. struct PropertyGenerator *pg;
  625. pg = GNUNET_new (struct PropertyGenerator);
  626. GNUNET_CONTAINER_DLL_insert (prop_gen_head, prop_gen_tail, pg);
  627. pg->type = type;
  628. pg->peer = peer;
  629. pg->test_address = test_address;
  630. pg->test_peer = test_peer;
  631. pg->address_id = address_id;
  632. pg->ats_property = ats_property;
  633. pg->base_value = base_value;
  634. pg->max_value = value_rate;
  635. pg->duration_period = period;
  636. pg->frequency = frequency;
  637. pg->time_start = GNUNET_TIME_absolute_get();
  638. switch (type) {
  639. case GNUNET_ATS_TEST_TG_CONSTANT:
  640. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  641. "Setting up %s property generator peer [%u] address [%u] `%s'"\
  642. "max %u Bips\n",
  643. print_generator_type(type), pg->peer, pg->address_id,
  644. GNUNET_ATS_print_property_type (ats_property),
  645. base_value);
  646. break;
  647. case GNUNET_ATS_TEST_TG_LINEAR:
  648. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  649. "Setting up %s property generator peer [%u] address [%u] `%s' " \
  650. "min %u Bips max %u Bips\n",
  651. print_generator_type(type), pg->peer, pg->address_id,
  652. GNUNET_ATS_print_property_type(ats_property),
  653. base_value, value_rate);
  654. break;
  655. case GNUNET_ATS_TEST_TG_SINUS:
  656. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  657. "Setting up %s property generator peer [%u] address [%u] `%s' "\
  658. "baserate %u Bips, amplitude %u Bps\n",
  659. print_generator_type(type), pg->peer, pg->address_id,
  660. GNUNET_ATS_print_property_type(ats_property),
  661. base_value, value_rate);
  662. break;
  663. case GNUNET_ATS_TEST_TG_RANDOM:
  664. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  665. "Setting up %s property generator peer [%u] address [%u] `%s' "\
  666. "min %u Bips max %u Bps\n",
  667. print_generator_type(type), pg->peer, pg->address_id,
  668. GNUNET_ATS_print_property_type(ats_property),
  669. base_value, value_rate);
  670. break;
  671. default:
  672. break;
  673. }
  674. pg->set_task = GNUNET_SCHEDULER_add_now (&set_prop_task, pg);
  675. return pg;
  676. }
  677. /**
  678. * Stop all preferences generators
  679. */
  680. void
  681. GNUNET_ATS_solver_generate_property_stop_all ()
  682. {
  683. struct PropertyGenerator *cur;
  684. struct PropertyGenerator *next;
  685. next = prop_gen_head;
  686. for (cur = next; NULL != cur; cur = next)
  687. {
  688. next = cur->next;
  689. GNUNET_ATS_solver_generate_property_stop (cur);
  690. }
  691. }
  692. /**
  693. * Preference Generators
  694. */
  695. static struct PreferenceGenerator *pref_gen_head;
  696. static struct PreferenceGenerator *pref_gen_tail;
  697. static double
  698. get_preference (struct PreferenceGenerator *pg)
  699. {
  700. struct GNUNET_TIME_Relative time_delta;
  701. double delta_value;
  702. double pref_value;
  703. /* Calculate the current preference value */
  704. switch (pg->type) {
  705. case GNUNET_ATS_TEST_TG_CONSTANT:
  706. pref_value = pg->base_value;
  707. break;
  708. case GNUNET_ATS_TEST_TG_LINEAR:
  709. time_delta = GNUNET_TIME_absolute_get_duration(pg->time_start);
  710. /* Calculate point of time in the current period */
  711. time_delta.rel_value_us = time_delta.rel_value_us %
  712. pg->duration_period.rel_value_us;
  713. delta_value = ((double) time_delta.rel_value_us /
  714. pg->duration_period.rel_value_us) * (pg->max_value - pg->base_value);
  715. if ((pg->max_value < pg->base_value) &&
  716. ((pg->max_value - pg->base_value) > pg->base_value))
  717. {
  718. /* This will cause an underflow */
  719. GNUNET_break (0);
  720. }
  721. pref_value = pg->base_value + delta_value;
  722. break;
  723. case GNUNET_ATS_TEST_TG_RANDOM:
  724. delta_value = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
  725. 10000 * (pg->max_value - pg->base_value)) / 10000;
  726. pref_value = pg->base_value + delta_value;
  727. break;
  728. case GNUNET_ATS_TEST_TG_SINUS:
  729. time_delta = GNUNET_TIME_absolute_get_duration(pg->time_start);
  730. /* Calculate point of time in the current period */
  731. time_delta.rel_value_us = time_delta.rel_value_us %
  732. pg->duration_period.rel_value_us;
  733. if ((pg->max_value - pg->base_value) > pg->base_value)
  734. {
  735. /* This will cause an underflow for second half of sinus period,
  736. * will be detected in general when experiments are loaded */
  737. GNUNET_break (0);
  738. }
  739. delta_value = (pg->max_value - pg->base_value) *
  740. sin ( (2 * M_PI) / ((double) pg->duration_period.rel_value_us) *
  741. time_delta.rel_value_us);
  742. pref_value = pg->base_value + delta_value;
  743. break;
  744. default:
  745. pref_value = 0.0;
  746. break;
  747. }
  748. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Current preference value is %f\n",
  749. pref_value);
  750. return pref_value;
  751. }
  752. static void
  753. set_feedback_task (void *cls,
  754. const struct GNUNET_SCHEDULER_TaskContext *tc)
  755. {
  756. struct PreferenceGenerator *pg = cls;
  757. struct TestPeer *p;
  758. double feedback;
  759. uint32_t bw_acc_out;
  760. uint32_t bw_acc_in;
  761. uint32_t delay_acc_in;
  762. struct GNUNET_TIME_Relative dur;
  763. double p_new;
  764. pg->feedback_task = NULL;
  765. if (NULL == (p = find_peer_by_id (pg->peer)))
  766. {
  767. GNUNET_break (0);
  768. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  769. "Setting feedback for unknown peer %u\n", pg->peer);
  770. return;
  771. }
  772. switch (pg->kind)
  773. {
  774. case GNUNET_ATS_PREFERENCE_BANDWIDTH:
  775. dur = GNUNET_TIME_absolute_get_duration(pg->feedback_last_bw_update);
  776. bw_acc_in = dur.rel_value_us *pg->last_assigned_bw_in + pg->feedback_bw_in_acc;
  777. pg->feedback_bw_in_acc = 0;
  778. bw_acc_out = dur.rel_value_us *pg->last_assigned_bw_out + pg->feedback_bw_out_acc;
  779. p_new = get_preference (pg);
  780. feedback = (p_new / pg->pref_bw_old) * (bw_acc_in + bw_acc_out) /
  781. (2 *GNUNET_TIME_absolute_get_duration(pg->feedback_last).rel_value_us);
  782. break;
  783. case GNUNET_ATS_PREFERENCE_LATENCY:
  784. dur = GNUNET_TIME_absolute_get_duration(pg->feedback_last_delay_update);
  785. delay_acc_in =dur.rel_value_us *pg->last_delay_value + pg->feedback_delay_acc;
  786. pg->feedback_delay_acc = 0;
  787. p_new = get_preference (pg);
  788. feedback = (p_new / pg->pref_latency_old) * (delay_acc_in) /
  789. (GNUNET_TIME_absolute_get_duration(pg->feedback_last).rel_value_us);
  790. break;
  791. default:
  792. GNUNET_break (0);
  793. feedback = 0.0;
  794. break;
  795. }
  796. GNUNET_log(GNUNET_ERROR_TYPE_INFO,
  797. "Giving feedback for peer [%u] for client %p pref %s of %.3f\n",
  798. pg->peer, NULL + (pg->client_id),
  799. GNUNET_ATS_print_preference_type (pg->kind),
  800. feedback);
  801. sh->sf->s_feedback (sh->sf->cls, NULL + (pg->client_id), &p->peer_id,
  802. pg->feedback_frequency, pg->kind, feedback);
  803. pg->feedback_last = GNUNET_TIME_absolute_get();
  804. pg->feedback_bw_out_acc = 0;
  805. pg->feedback_bw_in_acc = 0;
  806. pg->feedback_last_bw_update = GNUNET_TIME_absolute_get();
  807. pg->feedback_delay_acc = 0;
  808. pg->feedback_last_delay_update = GNUNET_TIME_absolute_get();
  809. pg->feedback_task = GNUNET_SCHEDULER_add_delayed (pg->feedback_frequency,
  810. &set_feedback_task, pg);
  811. }
  812. static void
  813. set_pref_task (void *cls,
  814. const struct GNUNET_SCHEDULER_TaskContext *tc)
  815. {
  816. struct PreferenceGenerator *pg = cls;
  817. struct TestPeer *p;
  818. double pref_value;
  819. pg->set_task = NULL;
  820. if (NULL == (p = find_peer_by_id (pg->peer)))
  821. {
  822. GNUNET_break (0);
  823. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  824. "Setting preference for unknown peer %u\n", pg->peer);
  825. return;
  826. }
  827. pref_value = get_preference (pg);
  828. switch (pg->kind) {
  829. case GNUNET_ATS_PREFERENCE_BANDWIDTH:
  830. pg->pref_bw_old = pref_value;
  831. break;
  832. case GNUNET_ATS_PREFERENCE_LATENCY:
  833. pg->pref_latency_old = pref_value;
  834. break;
  835. default:
  836. break;
  837. }
  838. p->pref_abs[pg->kind] = pref_value;
  839. GNUNET_log(GNUNET_ERROR_TYPE_INFO,
  840. "Setting preference for peer [%u] for client %p pref %s to %f\n",
  841. pg->peer, NULL + (pg->client_id),
  842. GNUNET_ATS_print_preference_type (pg->kind), pref_value);
  843. if (GNUNET_YES == opt_disable_normalization)
  844. {
  845. p->pref_abs[pg->kind] = pref_value;
  846. p->pref_norm[pg->kind] = pref_value;
  847. sh->sf->s_pref (sh->sf->cls, &p->peer_id, pg->kind, pref_value);
  848. }
  849. else
  850. normalize_preference (NULL + (pg->client_id),
  851. &p->peer_id,
  852. pg->kind,
  853. pref_value);
  854. pg->set_task = GNUNET_SCHEDULER_add_delayed (pg->frequency,
  855. &set_pref_task,
  856. pg);
  857. }
  858. static struct PreferenceGenerator *
  859. find_pref_gen (unsigned int peer, enum GNUNET_ATS_PreferenceKind kind)
  860. {
  861. struct PreferenceGenerator *cur;
  862. for (cur = pref_gen_head; NULL != cur; cur = cur->next)
  863. if (cur->peer == peer)
  864. {
  865. if ((cur->kind == kind) || (GNUNET_ATS_PREFERENCE_END == kind))
  866. return cur;
  867. }
  868. return NULL;
  869. }
  870. void
  871. GNUNET_ATS_solver_generate_preferences_stop (struct PreferenceGenerator *pg)
  872. {
  873. GNUNET_CONTAINER_DLL_remove (pref_gen_head, pref_gen_tail, pg);
  874. if (NULL != pg->feedback_task)
  875. {
  876. GNUNET_SCHEDULER_cancel (pg->feedback_task);
  877. pg->feedback_task = NULL;
  878. }
  879. if (NULL != pg->set_task)
  880. {
  881. GNUNET_SCHEDULER_cancel (pg->set_task);
  882. pg->set_task = NULL;
  883. }
  884. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  885. "Removing old up preference generator peer [%u] `%s'\n",
  886. pg->peer, GNUNET_ATS_print_preference_type(pg->kind));
  887. GNUNET_free (pg);
  888. }
  889. struct TestAddress*
  890. find_active_address (struct TestPeer *p)
  891. {
  892. struct TestAddress *cur;
  893. for (cur = p->addr_head; NULL != cur; cur = cur->next)
  894. if (GNUNET_YES == cur->ats_addr->active)
  895. return cur;
  896. return NULL;
  897. }
  898. /**
  899. * Generate between the source master and the partner and set property with a
  900. * value depending on the generator.
  901. *
  902. * @param peer source
  903. * @param address_id partner
  904. * @param client_id the client
  905. * @param type type of generator
  906. * @param base_value base value
  907. * @param value_rate maximum value
  908. * @param period duration of a period of generation (~ 1/frequency)
  909. * @param frequency how long to generate property
  910. * @param kind ATS preference to generate
  911. * @param feedback_frequency how often to give feedback
  912. * @return the preference generator
  913. */
  914. struct PreferenceGenerator *
  915. GNUNET_ATS_solver_generate_preferences_start (unsigned int peer,
  916. unsigned int address_id,
  917. unsigned int client_id,
  918. enum GeneratorType type,
  919. long int base_value,
  920. long int value_rate,
  921. struct GNUNET_TIME_Relative period,
  922. struct GNUNET_TIME_Relative frequency,
  923. enum GNUNET_ATS_PreferenceKind kind,
  924. struct GNUNET_TIME_Relative feedback_frequency)
  925. {
  926. struct PreferenceGenerator *pg;
  927. struct TestPeer *p;
  928. if (NULL == (p = find_peer_by_id (peer)))
  929. {
  930. GNUNET_break (0);
  931. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  932. "Starting preference for unknown peer %u\n", peer);
  933. return NULL;
  934. }
  935. pg = GNUNET_new (struct PreferenceGenerator);
  936. GNUNET_CONTAINER_DLL_insert (pref_gen_head, pref_gen_tail, pg);
  937. pg->type = type;
  938. pg->peer = peer;
  939. pg->client_id = client_id;
  940. pg->kind = kind;
  941. pg->base_value = base_value;
  942. pg->max_value = value_rate;
  943. pg->duration_period = period;
  944. pg->frequency = frequency;
  945. pg->time_start = GNUNET_TIME_absolute_get();
  946. pg->feedback_frequency = feedback_frequency;
  947. switch (type) {
  948. case GNUNET_ATS_TEST_TG_CONSTANT:
  949. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  950. "Setting up %s preference generator peer [%u] `%s' max %u Bips\n",
  951. print_generator_type (type), pg->peer,
  952. GNUNET_ATS_print_preference_type(kind),
  953. base_value);
  954. break;
  955. case GNUNET_ATS_TEST_TG_LINEAR:
  956. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  957. "Setting up %s preference generator peer [%u] `%s' min %u Bips max %u Bips\n",
  958. print_generator_type (type), pg->peer, GNUNET_ATS_print_preference_type(kind),
  959. base_value, value_rate);
  960. break;
  961. case GNUNET_ATS_TEST_TG_SINUS:
  962. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  963. "Setting up %s preference generator peer [%u] `%s' baserate %u Bips, amplitude %u Bps\n",
  964. print_generator_type (type), pg->peer, GNUNET_ATS_print_preference_type(kind),
  965. base_value, value_rate);
  966. break;
  967. case GNUNET_ATS_TEST_TG_RANDOM:
  968. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  969. "Setting up %s preference generator peer [%u] `%s' min %u Bips max %u Bps\n",
  970. print_generator_type (type), pg->peer, GNUNET_ATS_print_preference_type(kind),
  971. base_value, value_rate);
  972. break;
  973. default:
  974. break;
  975. }
  976. pg->set_task = GNUNET_SCHEDULER_add_now (&set_pref_task, pg);
  977. if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != feedback_frequency.rel_value_us)
  978. {
  979. struct TestAddress * addr = find_active_address(p);
  980. const double *properties = get_property_cb (NULL, addr->ats_addr);
  981. pg->last_assigned_bw_in = p->assigned_bw_in;
  982. pg->last_assigned_bw_out = p->assigned_bw_out;
  983. pg->feedback_bw_in_acc = 0;
  984. pg->feedback_bw_out_acc = 0;
  985. pg->last_delay_value = properties[GNUNET_ATS_QUALITY_NET_DELAY];
  986. pg->feedback_delay_acc = 0;
  987. pg->feedback_last_bw_update = GNUNET_TIME_absolute_get();
  988. pg->feedback_last_delay_update = GNUNET_TIME_absolute_get();
  989. pg->feedback_last = GNUNET_TIME_absolute_get();
  990. pg->feedback_task = GNUNET_SCHEDULER_add_delayed (feedback_frequency,
  991. &set_feedback_task, pg);
  992. }
  993. return pg;
  994. }
  995. /**
  996. * Stop all preferences generators
  997. */
  998. void
  999. GNUNET_ATS_solver_generate_preferences_stop_all ()
  1000. {
  1001. struct PreferenceGenerator *cur;
  1002. struct PreferenceGenerator *next;
  1003. next = pref_gen_head;
  1004. for (cur = next; NULL != cur; cur = next)
  1005. {
  1006. next = cur->next;
  1007. GNUNET_ATS_solver_generate_preferences_stop(cur);
  1008. }
  1009. }
  1010. /**
  1011. * Experiments
  1012. */
  1013. const char *
  1014. print_op (enum OperationType op)
  1015. {
  1016. switch (op) {
  1017. case SOLVER_OP_ADD_ADDRESS:
  1018. return "ADD_ADDRESS";
  1019. case SOLVER_OP_DEL_ADDRESS:
  1020. return "DEL_ADDRESS";
  1021. case SOLVER_OP_START_SET_PREFERENCE:
  1022. return "START_SET_PREFERENCE";
  1023. case SOLVER_OP_STOP_SET_PREFERENCE:
  1024. return "STOP_STOP_PREFERENCE";
  1025. case SOLVER_OP_START_SET_PROPERTY:
  1026. return "START_SET_PROPERTY";
  1027. case SOLVER_OP_STOP_SET_PROPERTY:
  1028. return "STOP_SET_PROPERTY";
  1029. case SOLVER_OP_START_REQUEST:
  1030. return "START_REQUEST";
  1031. case SOLVER_OP_STOP_REQUEST:
  1032. return "STOP_REQUEST";
  1033. default:
  1034. break;
  1035. }
  1036. return "";
  1037. }
  1038. static struct Experiment *
  1039. create_experiment ()
  1040. {
  1041. struct Experiment *e;
  1042. e = GNUNET_new (struct Experiment);
  1043. e->name = NULL;
  1044. e->start = NULL;
  1045. e->total_duration = GNUNET_TIME_UNIT_ZERO;
  1046. return e;
  1047. }
  1048. static void
  1049. free_experiment (struct Experiment *e)
  1050. {
  1051. struct Episode *cur;
  1052. struct Episode *next;
  1053. struct GNUNET_ATS_TEST_Operation *cur_o;
  1054. struct GNUNET_ATS_TEST_Operation *next_o;
  1055. next = e->start;
  1056. for (cur = next; NULL != cur; cur = next)
  1057. {
  1058. next = cur->next;
  1059. next_o = cur->head;
  1060. for (cur_o = next_o; NULL != cur_o; cur_o = next_o)
  1061. {
  1062. next_o = cur_o->next;
  1063. GNUNET_free_non_null (cur_o->address);
  1064. GNUNET_free_non_null (cur_o->plugin);
  1065. GNUNET_free (cur_o);
  1066. }
  1067. GNUNET_free (cur);
  1068. }
  1069. GNUNET_free_non_null (e->name);
  1070. GNUNET_free_non_null (e->log_prefix);
  1071. GNUNET_free_non_null (e->log_output_dir);
  1072. GNUNET_free_non_null (e->cfg_file);
  1073. GNUNET_free (e);
  1074. }
  1075. static int
  1076. load_op_add_address (struct GNUNET_ATS_TEST_Operation *o,
  1077. struct Episode *e,
  1078. int op_counter,
  1079. char *sec_name,
  1080. const struct GNUNET_CONFIGURATION_Handle *cfg)
  1081. {
  1082. char *op_name;
  1083. char *op_network;
  1084. /* peer pid */
  1085. GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
  1086. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
  1087. sec_name, op_name, &o->peer_id))
  1088. {
  1089. fprintf (stderr, "Missing peer-id in operation %u `%s' in episode `%s'\n",
  1090. op_counter, "ADD_ADDRESS", op_name);
  1091. GNUNET_free (op_name);
  1092. return GNUNET_SYSERR;
  1093. }
  1094. GNUNET_free (op_name);
  1095. /* address pid */
  1096. GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
  1097. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
  1098. sec_name, op_name, &o->address_id))
  1099. {
  1100. fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
  1101. op_counter, "ADD_ADDRESS", op_name);
  1102. GNUNET_free (op_name);
  1103. return GNUNET_SYSERR;
  1104. }
  1105. GNUNET_free (op_name);
  1106. /* plugin */
  1107. GNUNET_asprintf(&op_name, "op-%u-plugin", op_counter);
  1108. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
  1109. sec_name, op_name, &o->plugin))
  1110. {
  1111. fprintf (stderr, "Missing plugin in operation %u `%s' in episode `%s'\n",
  1112. op_counter, "ADD_ADDRESS", op_name);
  1113. GNUNET_free (op_name);
  1114. return GNUNET_SYSERR;
  1115. }
  1116. GNUNET_free (op_name);
  1117. /* address */
  1118. GNUNET_asprintf(&op_name, "op-%u-address", op_counter);
  1119. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
  1120. sec_name, op_name, &o->address))
  1121. {
  1122. fprintf (stderr, "Missing address in operation %u `%s' in episode `%s'\n",
  1123. op_counter, "ADD_ADDRESS", op_name);
  1124. GNUNET_free (op_name);
  1125. return GNUNET_SYSERR;
  1126. }
  1127. GNUNET_free (op_name);
  1128. /* session */
  1129. GNUNET_asprintf(&op_name, "op-%u-address-session", op_counter);
  1130. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
  1131. sec_name, op_name, &o->address_session))
  1132. {
  1133. fprintf (stderr, "Missing address-session in operation %u `%s' in episode `%s'\n",
  1134. op_counter, "ADD_ADDRESS", op_name);
  1135. GNUNET_free (op_name);
  1136. return GNUNET_SYSERR;
  1137. }
  1138. GNUNET_free (op_name);
  1139. /* network */
  1140. GNUNET_asprintf(&op_name, "op-%u-address-network", op_counter);
  1141. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
  1142. sec_name, op_name, &op_network))
  1143. {
  1144. fprintf (stderr, "Missing address-network in operation %u `%s' in episode `%s'\n",
  1145. op_counter, "ADD_ADDRESS", op_name);
  1146. GNUNET_free (op_name);
  1147. return GNUNET_SYSERR;
  1148. }
  1149. else
  1150. {
  1151. GNUNET_STRINGS_utf8_toupper (op_network,op_network);
  1152. if (0 == strcmp(op_network, "UNSPECIFIED"))
  1153. {
  1154. o->address_network = GNUNET_ATS_NET_UNSPECIFIED;
  1155. }
  1156. else if (0 == strcmp(op_network, "LOOPBACK"))
  1157. {
  1158. o->address_network = GNUNET_ATS_NET_LOOPBACK;
  1159. }
  1160. else if (0 == strcmp(op_network, "LAN"))
  1161. {
  1162. o->address_network = GNUNET_ATS_NET_LAN;
  1163. }
  1164. else if (0 == strcmp(op_network, "WAN"))
  1165. {
  1166. o->address_network = GNUNET_ATS_NET_WAN;
  1167. }
  1168. else if (0 == strcmp(op_network, "WLAN"))
  1169. {
  1170. o->address_network = GNUNET_ATS_NET_WLAN;
  1171. }
  1172. else if (0 == strcmp(op_network, "BT"))
  1173. {
  1174. o->address_network = GNUNET_ATS_NET_BT;
  1175. }
  1176. else
  1177. {
  1178. fprintf (stderr, "Invalid address-network in operation %u `%s' in episode `%s': `%s'\n",
  1179. op_counter, "ADD_ADDRESS", op_name, op_network);
  1180. GNUNET_free (op_network);
  1181. GNUNET_free (op_name);
  1182. return GNUNET_SYSERR;
  1183. }
  1184. }
  1185. GNUNET_free (op_network);
  1186. GNUNET_free (op_name);
  1187. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1188. "Found operation %s: [%llu:%llu] address `%s' plugin `%s' \n",
  1189. "ADD_ADDRESS", o->peer_id, o->address_id, o->address, o->plugin);
  1190. return GNUNET_OK;
  1191. }
  1192. static int
  1193. load_op_del_address (struct GNUNET_ATS_TEST_Operation *o,
  1194. struct Episode *e,
  1195. int op_counter,
  1196. char *sec_name,
  1197. const struct GNUNET_CONFIGURATION_Handle *cfg)
  1198. {
  1199. char *op_name;
  1200. //char *op_network;
  1201. /* peer pid */
  1202. GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
  1203. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
  1204. sec_name, op_name, &o->peer_id))
  1205. {
  1206. fprintf (stderr, "Missing peer-id in operation %u `%s' in episode `%s'\n",
  1207. op_counter, "DEL_ADDRESS", op_name);
  1208. GNUNET_free (op_name);
  1209. return GNUNET_SYSERR;
  1210. }
  1211. GNUNET_free (op_name);
  1212. /* address pid */
  1213. GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
  1214. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
  1215. sec_name, op_name, &o->address_id))
  1216. {
  1217. fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
  1218. op_counter, "DEL_ADDRESS", op_name);
  1219. GNUNET_free (op_name);
  1220. return GNUNET_SYSERR;
  1221. }
  1222. GNUNET_free (op_name);
  1223. #if 0
  1224. /* plugin */
  1225. GNUNET_asprintf(&op_name, "op-%u-plugin", op_counter);
  1226. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
  1227. sec_name, op_name, &o->plugin))
  1228. {
  1229. fprintf (stderr, "Missing plugin in operation %u `%s' in episode `%s'\n",
  1230. op_counter, "DEL_ADDRESS", op_name);
  1231. GNUNET_free (op_name);
  1232. return GNUNET_SYSERR;
  1233. }
  1234. GNUNET_free (op_name);
  1235. /* address */
  1236. GNUNET_asprintf(&op_name, "op-%u-address", op_counter);
  1237. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
  1238. sec_name, op_name, &o->address))
  1239. {
  1240. fprintf (stderr, "Missing address in operation %u `%s' in episode `%s'\n",
  1241. op_counter, "DEL_ADDRESS", op_name);
  1242. GNUNET_free (op_name);
  1243. return GNUNET_SYSERR;
  1244. }
  1245. GNUNET_free (op_name);
  1246. /* session */
  1247. GNUNET_asprintf(&op_name, "op-%u-address-session", op_counter);
  1248. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
  1249. sec_name, op_name, &o->address_session))
  1250. {
  1251. fprintf (stderr, "Missing address-session in operation %u `%s' in episode `%s'\n",
  1252. op_counter, "DEL_ADDRESS", op_name);
  1253. GNUNET_free (op_name);
  1254. return GNUNET_SYSERR;
  1255. }
  1256. GNUNET_free (op_name);
  1257. #endif
  1258. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1259. "Found operation %s: [%llu:%llu] address `%s' plugin `%s' \n",
  1260. "DEL_ADDRESS", o->peer_id, o->address_id, o->address, o->plugin);
  1261. return GNUNET_OK;
  1262. }
  1263. static enum GNUNET_ATS_Property
  1264. parse_preference_string (const char * str)
  1265. {
  1266. int c = 0;
  1267. char *props[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
  1268. for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
  1269. if (0 == strcmp(str, props[c]))
  1270. return c;
  1271. return 0;
  1272. };
  1273. static int
  1274. load_op_start_set_preference (struct GNUNET_ATS_TEST_Operation *o,
  1275. struct Episode *e,
  1276. int op_counter,
  1277. char *sec_name,
  1278. const struct GNUNET_CONFIGURATION_Handle *cfg)
  1279. {
  1280. char *op_name;
  1281. char *type;
  1282. char *pref;
  1283. /* peer pid */
  1284. GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
  1285. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
  1286. sec_name, op_name, &o->peer_id))
  1287. {
  1288. fprintf (stderr, "Missing peer-id in operation %u `%s' in episode `%s'\n",
  1289. op_counter, "START_SET_PREFERENCE", op_name);
  1290. GNUNET_free (op_name);
  1291. return GNUNET_SYSERR;
  1292. }
  1293. GNUNET_free (op_name);
  1294. /* address pid */
  1295. GNUNET_asprintf(&op_name, "op-%u-client-id", op_counter);
  1296. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
  1297. sec_name, op_name, &o->client_id))
  1298. {
  1299. fprintf (stderr, "Missing client-id in operation %u `%s' in episode `%s'\n",
  1300. op_counter, "START_SET_PREFERENCE", op_name);
  1301. GNUNET_free (op_name);
  1302. return GNUNET_SYSERR;
  1303. }
  1304. GNUNET_free (op_name);
  1305. /* generator */
  1306. GNUNET_asprintf(&op_name, "op-%u-gen-type", op_counter);
  1307. if ( (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
  1308. sec_name, op_name, &type)) )
  1309. {
  1310. fprintf (stderr, "Missing type in operation %u `%s' in episode `%s'\n",
  1311. op_counter, "START_SET_PREFERENCE", op_name);
  1312. GNUNET_free (op_name);
  1313. return GNUNET_SYSERR;
  1314. }
  1315. /* Load arguments for set_rate, start_send, set_preference */
  1316. if (0 == strcmp (type, "constant"))
  1317. {
  1318. o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
  1319. }
  1320. else if (0 == strcmp (type, "linear"))
  1321. {
  1322. o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
  1323. }
  1324. else if (0 == strcmp (type, "sinus"))
  1325. {
  1326. o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
  1327. }
  1328. else if (0 == strcmp (type, "random"))
  1329. {
  1330. o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
  1331. }
  1332. else
  1333. {
  1334. fprintf (stderr, "Invalid generator type %u `%s' in episode %u\n",
  1335. op_counter, op_name, e->id);
  1336. GNUNET_free (type);
  1337. GNUNET_free (op_name);
  1338. return GNUNET_SYSERR;
  1339. }
  1340. GNUNET_free (type);
  1341. GNUNET_free (op_name);
  1342. /* Get base rate */
  1343. GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
  1344. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
  1345. sec_name, op_name, &o->base_rate))
  1346. {
  1347. fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
  1348. op_counter, op_name, e->id);
  1349. GNUNET_free (op_name);
  1350. return GNUNET_SYSERR;
  1351. }
  1352. GNUNET_free (op_name);
  1353. /* Get max rate */
  1354. GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
  1355. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
  1356. sec_name, op_name, &o->max_rate))
  1357. {
  1358. if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
  1359. (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
  1360. (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
  1361. {
  1362. fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
  1363. op_counter, op_name, e->id);
  1364. GNUNET_free (op_name);
  1365. return GNUNET_SYSERR;
  1366. }
  1367. }
  1368. GNUNET_free (op_name);
  1369. /* Get period */
  1370. GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
  1371. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
  1372. sec_name, op_name, &o->period))
  1373. {
  1374. o->period = e->duration;
  1375. }
  1376. GNUNET_free (op_name);
  1377. /* Get frequency */
  1378. GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
  1379. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
  1380. sec_name, op_name, &o->frequency))
  1381. {
  1382. fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
  1383. op_counter, op_name, e->id);
  1384. GNUNET_free (op_name);
  1385. return GNUNET_SYSERR;
  1386. }
  1387. GNUNET_free (op_name);
  1388. /* Get preference */
  1389. GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
  1390. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
  1391. sec_name, op_name, &pref))
  1392. {
  1393. fprintf (stderr, "Missing preference in operation %u `%s' in episode %u\n",
  1394. op_counter, op_name, e->id);
  1395. GNUNET_free (op_name);
  1396. return GNUNET_SYSERR;
  1397. }
  1398. if (0 == (o->pref_type = parse_preference_string(pref)))
  1399. {
  1400. fprintf (stderr, "Invalid preference in operation %u `%s' in episode %u\n",
  1401. op_counter, op_name, e->id);
  1402. GNUNET_free (op_name);
  1403. GNUNET_free (pref);
  1404. return GNUNET_SYSERR;
  1405. }
  1406. GNUNET_free (pref);
  1407. GNUNET_free (op_name);
  1408. /* Get feedback delay */
  1409. GNUNET_asprintf(&op_name, "op-%u-feedback_delay", op_counter);
  1410. if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_time (cfg,
  1411. sec_name, op_name, &o->feedback_delay))
  1412. {
  1413. fprintf (stderr, "Using feedback delay %llu in operation %u `%s' in episode %u\n",
  1414. (long long unsigned int) o->feedback_delay.rel_value_us,
  1415. op_counter, op_name, e->id);
  1416. }
  1417. else
  1418. o->feedback_delay = GNUNET_TIME_UNIT_FOREVER_REL;
  1419. GNUNET_free (op_name);
  1420. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1421. "Found operation %s: [%llu:%llu]: %s = %llu\n",
  1422. "START_SET_PREFERENCE", o->peer_id, o->address_id,
  1423. GNUNET_ATS_print_preference_type(o->pref_type), o->base_rate);
  1424. return GNUNET_OK;
  1425. }
  1426. static int
  1427. load_op_stop_set_preference (struct GNUNET_ATS_TEST_Operation *o,
  1428. struct Episode *e,
  1429. int op_counter,
  1430. char *sec_name,
  1431. const struct GNUNET_CONFIGURATION_Handle *cfg)
  1432. {
  1433. char *op_name;
  1434. char *pref;
  1435. /* peer pid */
  1436. GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
  1437. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
  1438. sec_name, op_name, &o->peer_id))
  1439. {
  1440. fprintf (stderr, "Missing peer-id in operation %u `%s' in episode `%s'\n",
  1441. op_counter, "STOP_SET_PREFERENCE", op_name);
  1442. GNUNET_free (op_name);
  1443. return GNUNET_SYSERR;
  1444. }
  1445. GNUNET_free (op_name);
  1446. /* address pid */
  1447. GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
  1448. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
  1449. sec_name, op_name, &o->address_id))
  1450. {
  1451. fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
  1452. op_counter, "STOP_SET_PREFERENCE", op_name);
  1453. GNUNET_free (op_name);
  1454. return GNUNET_SYSERR;
  1455. }
  1456. GNUNET_free (op_name);
  1457. /* Get preference */
  1458. GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
  1459. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
  1460. sec_name, op_name, &pref))
  1461. {
  1462. fprintf (stderr, "Missing preference in operation %u `%s' in episode `%s'\n",
  1463. op_counter, "STOP_SET_PREFERENCE", op_name);
  1464. GNUNET_free (op_name);
  1465. return GNUNET_SYSERR;
  1466. }
  1467. if (0 == (o->pref_type = parse_preference_string(pref)))
  1468. {
  1469. fprintf (stderr, "Invalid preference in operation %u `%s' in episode %u\n",
  1470. op_counter, op_name, e->id);
  1471. GNUNET_free (op_name);
  1472. GNUNET_free (pref);
  1473. return GNUNET_SYSERR;
  1474. }
  1475. GNUNET_free (pref);
  1476. GNUNET_free (op_name);
  1477. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1478. "Found operation %s: [%llu:%llu]: %s\n",
  1479. "STOP_SET_PREFERENCE", o->peer_id, o->address_id,
  1480. GNUNET_ATS_print_preference_type(o->pref_type));
  1481. return GNUNET_OK;
  1482. }
  1483. static enum GNUNET_ATS_Property
  1484. parse_property_string (const char *str)
  1485. {
  1486. enum GNUNET_ATS_Property c;
  1487. for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
  1488. if (0 == strcmp(str,
  1489. GNUNET_ATS_print_property_type (c)))
  1490. return c;
  1491. return 0;
  1492. }
  1493. static int
  1494. load_op_start_set_property(struct GNUNET_ATS_TEST_Operation *o,
  1495. struct Episode *e,
  1496. int op_counter,
  1497. char *sec_name,
  1498. const struct GNUNET_CONFIGURATION_Handle *cfg)
  1499. {
  1500. char *op_name;
  1501. char *type;
  1502. char *prop;
  1503. /* peer pid */
  1504. GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
  1505. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
  1506. sec_name, op_name, &o->peer_id))
  1507. {
  1508. fprintf (stderr, "Missing peer-id in operation %u `%s' in episode `%s'\n",
  1509. op_counter, "START_SET_PROPERTY", op_name);
  1510. GNUNET_free (op_name);
  1511. return GNUNET_SYSERR;
  1512. }
  1513. GNUNET_free (op_name);
  1514. /* address pid */
  1515. GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
  1516. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
  1517. sec_name, op_name, &o->address_id))
  1518. {
  1519. fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
  1520. op_counter, "START_SET_PROPERTY", op_name);
  1521. GNUNET_free (op_name);
  1522. return GNUNET_SYSERR;
  1523. }
  1524. GNUNET_free (op_name);
  1525. /* generator */
  1526. GNUNET_asprintf(&op_name, "op-%u-gen-type", op_counter);
  1527. if ( (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
  1528. sec_name, op_name, &type)) )
  1529. {
  1530. fprintf (stderr, "Missing type in operation %u `%s' in episode `%s'\n",
  1531. op_counter, "START_SET_PROPERTY", op_name);
  1532. GNUNET_free (op_name);
  1533. return GNUNET_SYSERR;
  1534. }
  1535. /* Load arguments for set_rate, start_send, set_preference */
  1536. if (0 == strcmp (type, "constant"))
  1537. {
  1538. o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
  1539. }
  1540. else if (0 == strcmp (type, "linear"))
  1541. {
  1542. o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
  1543. }
  1544. else if (0 == strcmp (type, "sinus"))
  1545. {
  1546. o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
  1547. }
  1548. else if (0 == strcmp (type, "random"))
  1549. {
  1550. o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
  1551. }
  1552. else
  1553. {
  1554. fprintf (stderr, "Invalid generator type %u `%s' in episode %u\n",
  1555. op_counter, op_name, e->id);
  1556. GNUNET_free (type);
  1557. GNUNET_free (op_name);
  1558. return GNUNET_SYSERR;
  1559. }
  1560. GNUNET_free (type);
  1561. GNUNET_free (op_name);
  1562. /* Get base rate */
  1563. GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
  1564. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
  1565. sec_name, op_name, &o->base_rate))
  1566. {
  1567. fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
  1568. op_counter, op_name, e->id);
  1569. GNUNET_free (op_name);
  1570. return GNUNET_SYSERR;
  1571. }
  1572. GNUNET_free (op_name);
  1573. /* Get max rate */
  1574. GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
  1575. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
  1576. sec_name, op_name, &o->max_rate))
  1577. {
  1578. if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
  1579. (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
  1580. (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
  1581. {
  1582. fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
  1583. op_counter, op_name, e->id);
  1584. GNUNET_free (op_name);
  1585. return GNUNET_SYSERR;
  1586. }
  1587. }
  1588. GNUNET_free (op_name);
  1589. /* Get period */
  1590. GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
  1591. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
  1592. sec_name, op_name, &o->period))
  1593. {
  1594. o->period = e->duration;
  1595. }
  1596. GNUNET_free (op_name);
  1597. /* Get frequency */
  1598. GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
  1599. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
  1600. sec_name, op_name, &o->frequency))
  1601. {
  1602. fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
  1603. op_counter, op_name, e->id);
  1604. GNUNET_free (op_name);
  1605. return GNUNET_SYSERR;
  1606. }
  1607. GNUNET_free (op_name);
  1608. /* Get preference */
  1609. GNUNET_asprintf(&op_name, "op-%u-property", op_counter);
  1610. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
  1611. sec_name, op_name, &prop))
  1612. {
  1613. fprintf (stderr, "Missing property in operation %u `%s' in episode %u\n",
  1614. op_counter, op_name, e->id);
  1615. GNUNET_free (op_name);
  1616. GNUNET_free_non_null (prop);
  1617. return GNUNET_SYSERR;
  1618. }
  1619. if (0 == (o->prop_type = parse_property_string(prop)))
  1620. {
  1621. fprintf (stderr, "Invalid property in operation %u `%s' in episode %u\n",
  1622. op_counter, op_name, e->id);
  1623. GNUNET_free (op_name);
  1624. GNUNET_free (prop);
  1625. return GNUNET_SYSERR;
  1626. }
  1627. GNUNET_free (prop);
  1628. GNUNET_free (op_name);
  1629. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1630. "Found operation %s: [%llu:%llu] %s = %llu\n",
  1631. "START_SET_PROPERTY", o->peer_id, o->address_id,
  1632. GNUNET_ATS_print_property_type (o->prop_type), o->base_rate);
  1633. return GNUNET_OK;
  1634. }
  1635. static int
  1636. load_op_stop_set_property (struct GNUNET_ATS_TEST_Operation *o,
  1637. struct Episode *e,
  1638. int op_counter,
  1639. char *sec_name,
  1640. const struct GNUNET_CONFIGURATION_Handle *cfg)
  1641. {
  1642. char *op_name;
  1643. char *pref;
  1644. /* peer pid */
  1645. GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
  1646. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
  1647. sec_name, op_name, &o->peer_id))
  1648. {
  1649. fprintf (stderr, "Missing peer-id in operation %u `%s' in episode `%s'\n",
  1650. op_counter, "STOP_SET_PROPERTY", op_name);
  1651. GNUNET_free (op_name);
  1652. return GNUNET_SYSERR;
  1653. }
  1654. GNUNET_free (op_name);
  1655. /* address pid */
  1656. GNUNET_asprintf(&op_name, "op-%u-address-id", op_counter);
  1657. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
  1658. sec_name, op_name, &o->address_id))
  1659. {
  1660. fprintf (stderr, "Missing address-id in operation %u `%s' in episode `%s'\n",
  1661. op_counter, "STOP_SET_PROPERTY", op_name);
  1662. GNUNET_free (op_name);
  1663. return GNUNET_SYSERR;
  1664. }
  1665. GNUNET_free (op_name);
  1666. /* Get property */
  1667. GNUNET_asprintf(&op_name, "op-%u-property", op_counter);
  1668. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
  1669. sec_name, op_name, &pref))
  1670. {
  1671. fprintf (stderr, "Missing property in operation %u `%s' in episode `%s'\n",
  1672. op_counter, "STOP_SET_PROPERTY", op_name);
  1673. GNUNET_free (op_name);
  1674. GNUNET_free_non_null (pref);
  1675. return GNUNET_SYSERR;
  1676. }
  1677. if (0 == (o->prop_type = parse_property_string(pref)))
  1678. {
  1679. fprintf (stderr, "Invalid property in operation %u `%s' in episode %u\n",
  1680. op_counter, op_name, e->id);
  1681. GNUNET_free (op_name);
  1682. GNUNET_free_non_null (pref);
  1683. return GNUNET_SYSERR;
  1684. }
  1685. GNUNET_free (pref);
  1686. GNUNET_free (op_name);
  1687. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1688. "Found operation %s: [%llu:%llu] %s\n",
  1689. "STOP_SET_PROPERTY", o->peer_id, o->address_id,
  1690. GNUNET_ATS_print_property_type (o->prop_type));
  1691. return GNUNET_OK;
  1692. }
  1693. static int
  1694. load_op_start_request (struct GNUNET_ATS_TEST_Operation *o,
  1695. struct Episode *e,
  1696. int op_counter,
  1697. char *sec_name,
  1698. const struct GNUNET_CONFIGURATION_Handle *cfg)
  1699. {
  1700. char *op_name;
  1701. /* peer pid */
  1702. GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
  1703. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
  1704. sec_name, op_name, &o->peer_id))
  1705. {
  1706. fprintf (stderr, "Missing peer-id in operation %u `%s' in episode `%s'\n",
  1707. op_counter, "START_REQUEST", op_name);
  1708. GNUNET_free (op_name);
  1709. return GNUNET_SYSERR;
  1710. }
  1711. GNUNET_free (op_name);
  1712. return GNUNET_OK;
  1713. }
  1714. static int
  1715. load_op_stop_request (struct GNUNET_ATS_TEST_Operation *o,
  1716. struct Episode *e,
  1717. int op_counter,
  1718. char *sec_name,
  1719. const struct GNUNET_CONFIGURATION_Handle *cfg)
  1720. {
  1721. char *op_name;
  1722. /* peer pid */
  1723. GNUNET_asprintf(&op_name, "op-%u-peer-id", op_counter);
  1724. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
  1725. sec_name, op_name, &o->peer_id))
  1726. {
  1727. fprintf (stderr, "Missing peer-id in operation %u `%s' in episode `%s'\n",
  1728. op_counter, "STOP_REQUEST", op_name);
  1729. GNUNET_free (op_name);
  1730. return GNUNET_SYSERR;
  1731. }
  1732. GNUNET_free (op_name);
  1733. return GNUNET_OK;
  1734. }
  1735. static int
  1736. load_episode (struct Experiment *e, struct Episode *cur,
  1737. struct GNUNET_CONFIGURATION_Handle *cfg)
  1738. {
  1739. struct GNUNET_ATS_TEST_Operation *o;
  1740. char *sec_name;
  1741. char *op_name;
  1742. char *op;
  1743. int op_counter = 0;
  1744. int res;
  1745. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Parsing episode %u\n",cur->id);
  1746. GNUNET_asprintf(&sec_name, "episode-%u", cur->id);
  1747. while (1)
  1748. {
  1749. /* Load operation */
  1750. GNUNET_asprintf(&op_name, "op-%u-operation", op_counter);
  1751. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
  1752. sec_name, op_name, &op))
  1753. {
  1754. GNUNET_free (op_name);
  1755. break;
  1756. }
  1757. o = GNUNET_new (struct GNUNET_ATS_TEST_Operation);
  1758. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "==== Parsing operation %u: `%s'\n",
  1759. cur->id, op_name);
  1760. /* operations = set_rate, start_send, stop_send, set_preference */
  1761. if (0 == strcmp (op, "address_add"))
  1762. {
  1763. o->type = SOLVER_OP_ADD_ADDRESS;
  1764. res = load_op_add_address (o, cur,
  1765. op_counter, sec_name, cfg);
  1766. }
  1767. else if (0 == strcmp (op, "address_del"))
  1768. {
  1769. o->type = SOLVER_OP_DEL_ADDRESS;
  1770. res = load_op_del_address (o, cur,
  1771. op_counter, sec_name, cfg);
  1772. }
  1773. else if (0 == strcmp (op, "start_set_property"))
  1774. {
  1775. o->type = SOLVER_OP_START_SET_PROPERTY;
  1776. res = load_op_start_set_property (o, cur,
  1777. op_counter, sec_name, cfg);
  1778. }
  1779. else if (0 == strcmp (op, "stop_set_property"))
  1780. {
  1781. o->type = SOLVER_OP_STOP_SET_PROPERTY;
  1782. res = load_op_stop_set_property (o, cur,
  1783. op_counter, sec_name, cfg);
  1784. }
  1785. else if (0 == strcmp (op, "start_set_preference"))
  1786. {
  1787. o->type = SOLVER_OP_START_SET_PREFERENCE;
  1788. res = load_op_start_set_preference (o, cur,
  1789. op_counter, sec_name, cfg);
  1790. }
  1791. else if (0 == strcmp (op, "stop_set_preference"))
  1792. {
  1793. o->type = SOLVER_OP_STOP_SET_PREFERENCE;
  1794. res = load_op_stop_set_preference (o, cur,
  1795. op_counter, sec_name, cfg);
  1796. }
  1797. else if (0 == strcmp (op, "start_request"))
  1798. {
  1799. o->type = SOLVER_OP_START_REQUEST;
  1800. res = load_op_start_request (o, cur,
  1801. op_counter, sec_name, cfg);
  1802. }
  1803. else if (0 == strcmp (op, "stop_request"))
  1804. {
  1805. o->type = SOLVER_OP_STOP_REQUEST;
  1806. res = load_op_stop_request(o, cur,
  1807. op_counter, sec_name, cfg);
  1808. }
  1809. else
  1810. {
  1811. fprintf (stderr, "Invalid operation %u `%s' in episode %u\n",
  1812. op_counter, op, cur->id);
  1813. res = GNUNET_SYSERR;
  1814. }
  1815. GNUNET_free (op);
  1816. GNUNET_free (op_name);
  1817. if (GNUNET_SYSERR == res)
  1818. {
  1819. GNUNET_free (o);
  1820. GNUNET_free (sec_name);
  1821. return GNUNET_SYSERR;
  1822. }
  1823. GNUNET_CONTAINER_DLL_insert_tail (cur->head,cur->tail, o);
  1824. op_counter++;
  1825. }
  1826. GNUNET_free (sec_name);
  1827. return GNUNET_OK;
  1828. }
  1829. static int
  1830. load_episodes (struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
  1831. {
  1832. int e_counter = 0;
  1833. char *sec_name;
  1834. struct GNUNET_TIME_Relative e_duration;
  1835. struct Episode *cur;
  1836. struct Episode *last;
  1837. e_counter = 0;
  1838. last = NULL;
  1839. while (1)
  1840. {
  1841. GNUNET_asprintf(&sec_name, "episode-%u", e_counter);
  1842. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg,
  1843. sec_name, "duration", &e_duration))
  1844. {
  1845. GNUNET_free (sec_name);
  1846. break;
  1847. }
  1848. cur = GNUNET_new (struct Episode);
  1849. cur->duration = e_duration;
  1850. cur->id = e_counter;
  1851. if (GNUNET_OK != load_episode (e, cur, cfg))
  1852. {
  1853. GNUNET_free (sec_name);
  1854. GNUNET_free (cur);
  1855. return GNUNET_SYSERR;
  1856. }
  1857. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Found episode %u with duration %s \n",
  1858. e_counter,
  1859. GNUNET_STRINGS_relative_time_to_string(cur->duration, GNUNET_YES));
  1860. /* Update experiment */
  1861. e->num_episodes ++;
  1862. e->total_duration = GNUNET_TIME_relative_add(e->total_duration, cur->duration);
  1863. /* Put in linked list */
  1864. if (NULL == last)
  1865. e->start = cur;
  1866. else
  1867. last->next = cur;
  1868. GNUNET_free (sec_name);
  1869. e_counter ++;
  1870. last = cur;
  1871. }
  1872. return e_counter;
  1873. }
  1874. static void
  1875. timeout_experiment (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
  1876. {
  1877. struct Experiment *e = cls;
  1878. e->experiment_timeout_task = NULL;
  1879. fprintf (stderr, "Experiment timeout!\n");
  1880. if (NULL != e->episode_timeout_task)
  1881. {
  1882. GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
  1883. e->episode_timeout_task = NULL;
  1884. }
  1885. e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time),
  1886. GNUNET_SYSERR);
  1887. }
  1888. struct ATS_Address *
  1889. create_ats_address (const struct GNUNET_PeerIdentity *peer,
  1890. const char *plugin_name,
  1891. const void *plugin_addr,
  1892. size_t plugin_addr_len,
  1893. uint32_t session_id,
  1894. uint32_t network)
  1895. {
  1896. struct ATS_Address *aa = NULL;
  1897. aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len + strlen (plugin_name) + 1);
  1898. aa->atsi = GNUNET_new (struct GNUNET_ATS_Information);
  1899. aa->atsi[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
  1900. aa->atsi[0].value = htonl (network);
  1901. aa->atsi_count = 1;
  1902. aa->peer = *peer;
  1903. aa->addr_len = plugin_addr_len;
  1904. aa->addr = &aa[1];
  1905. aa->plugin = (char *) &aa[1] + plugin_addr_len;
  1906. memcpy (&aa[1], plugin_addr, plugin_addr_len);
  1907. memcpy (aa->plugin, plugin_name, strlen (plugin_name) + 1);
  1908. aa->session_id = session_id;
  1909. return aa;
  1910. }
  1911. static void
  1912. enforce_add_address (struct GNUNET_ATS_TEST_Operation *op)
  1913. {
  1914. struct TestPeer *p;
  1915. struct TestAddress *a;
  1916. int c;
  1917. if (NULL == (p = find_peer_by_id (op->peer_id)))
  1918. {
  1919. p = GNUNET_new (struct TestPeer);
  1920. p->id = op->peer_id;
  1921. p->assigned_bw_in = 0;
  1922. p->assigned_bw_out = 0;
  1923. memset (&p->peer_id, op->peer_id, sizeof (p->peer_id));
  1924. for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
  1925. {
  1926. p->pref_abs[c] = DEFAULT_ABS_PREFERENCE;
  1927. p->pref_norm[c] = DEFAULT_REL_PREFERENCE;
  1928. }
  1929. GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, p);
  1930. }
  1931. if (NULL != (find_address_by_id (p, op->address_id)))
  1932. {
  1933. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Duplicate address %u for peer %u\n",
  1934. op->address_id, op->peer_id);
  1935. return;
  1936. }
  1937. a = GNUNET_new (struct TestAddress);
  1938. a->aid = op->address_id;
  1939. a->network = op->address_network;
  1940. a->ats_addr = create_ats_address (&p->peer_id, op->plugin, op->address,
  1941. strlen (op->address) + 1, op->address_session, op->address_network);
  1942. memset (&p->peer_id, op->peer_id, sizeof (p->peer_id));
  1943. GNUNET_CONTAINER_DLL_insert_tail (p->addr_head, p->addr_tail, a);
  1944. for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
  1945. a->prop_norm[c] = DEFAULT_REL_QUALITY;
  1946. GNUNET_CONTAINER_multipeermap_put (sh->addresses, &p->peer_id, a->ats_addr,
  1947. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  1948. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Adding address %u for peer %u in network `%s'\n",
  1949. op->address_id, op->peer_id, GNUNET_ATS_print_network_type(a->network));
  1950. sh->sf->s_add (sh->sf->cls, a->ats_addr, op->address_network);
  1951. }
  1952. static void
  1953. enforce_del_address (struct GNUNET_ATS_TEST_Operation *op)
  1954. {
  1955. struct TestPeer *p;
  1956. struct TestAddress *a;
  1957. struct PropertyGenerator *pg;
  1958. if (NULL == (p = find_peer_by_id (op->peer_id)))
  1959. {
  1960. GNUNET_break (0);
  1961. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1962. "Deleting address for unknown peer %u\n", op->peer_id);
  1963. return;
  1964. }
  1965. if (NULL == (a =find_address_by_id (p, op->address_id)))
  1966. {
  1967. GNUNET_break (0);
  1968. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1969. "Deleting address for unknown peer %u\n", op->peer_id);
  1970. return;
  1971. }
  1972. while (NULL != (pg = find_prop_gen (p->id, a->aid, 0)))
  1973. {
  1974. GNUNET_ATS_solver_generate_property_stop (pg);
  1975. }
  1976. GNUNET_assert (GNUNET_YES ==
  1977. GNUNET_CONTAINER_multipeermap_remove (sh->addresses,
  1978. &p->peer_id,
  1979. a->ats_addr));
  1980. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1981. "Removing address %u for peer %u\n",
  1982. op->address_id,
  1983. op->peer_id);
  1984. sh->sf->s_del (sh->sf->cls, a->ats_addr, GNUNET_NO);
  1985. if (NULL != l)
  1986. {
  1987. GNUNET_ATS_solver_logging_now (l);
  1988. }
  1989. GNUNET_CONTAINER_DLL_remove(p->addr_head, p->addr_tail, a);
  1990. GNUNET_free_non_null(a->ats_addr->atsi);
  1991. GNUNET_free (a->ats_addr);
  1992. GNUNET_free (a);
  1993. }
  1994. static void
  1995. enforce_start_property (struct GNUNET_ATS_TEST_Operation *op)
  1996. {
  1997. struct PropertyGenerator *pg;
  1998. struct TestPeer *p;
  1999. struct TestAddress *a;
  2000. if (NULL != (pg = find_prop_gen (op->peer_id, op->address_id, op->prop_type)))
  2001. {
  2002. GNUNET_ATS_solver_generate_property_stop (pg);
  2003. GNUNET_free (pg);
  2004. }
  2005. if (NULL == (p = find_peer_by_id (op->peer_id)))
  2006. {
  2007. GNUNET_break (0);
  2008. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2009. "Starting property generation for unknown peer %u\n", op->peer_id);
  2010. return;
  2011. }
  2012. if (NULL == (a = find_address_by_id (p, op->address_id)))
  2013. {
  2014. GNUNET_break (0);
  2015. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2016. "Setting property for unknown address %u\n", op->peer_id);
  2017. return;
  2018. }
  2019. GNUNET_ATS_solver_generate_property_start (op->peer_id,
  2020. op->address_id,
  2021. p, a,
  2022. op->gen_type,
  2023. op->base_rate,
  2024. op->max_rate,
  2025. op->period,
  2026. op->frequency,
  2027. op->prop_type);
  2028. }
  2029. static void
  2030. enforce_stop_property (struct GNUNET_ATS_TEST_Operation *op)
  2031. {
  2032. struct PropertyGenerator *pg = find_prop_gen(op->peer_id, op->address_id,
  2033. op->prop_type);
  2034. if (NULL != pg)
  2035. {
  2036. GNUNET_log(GNUNET_ERROR_TYPE_INFO,
  2037. "Stopping preference generation for peer %u address %u\n", op->peer_id,
  2038. op->address_id);
  2039. GNUNET_ATS_solver_generate_property_stop (pg);
  2040. }
  2041. else
  2042. {
  2043. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2044. "Cannot find preference generator for peer %u address %u\n",
  2045. op->peer_id, op->address_id);
  2046. GNUNET_break (0);
  2047. }
  2048. }
  2049. static void
  2050. enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
  2051. {
  2052. struct PreferenceGenerator *pg;
  2053. if (NULL != (pg = find_pref_gen (op->peer_id, op->pref_type)))
  2054. {
  2055. GNUNET_ATS_solver_generate_preferences_stop (pg);
  2056. GNUNET_free (pg);
  2057. }
  2058. if (NULL == (find_peer_by_id (op->peer_id)))
  2059. {
  2060. GNUNET_break (0);
  2061. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2062. "Starting preference generation for unknown peer %u\n", op->peer_id);
  2063. return;
  2064. }
  2065. GNUNET_ATS_solver_generate_preferences_start (op->peer_id,
  2066. op->address_id,
  2067. op->client_id,
  2068. op->gen_type,
  2069. op->base_rate,
  2070. op->max_rate,
  2071. op->period,
  2072. op->frequency,
  2073. op->pref_type,
  2074. op->frequency);
  2075. }
  2076. static void
  2077. enforce_stop_preference (struct GNUNET_ATS_TEST_Operation *op)
  2078. {
  2079. struct PreferenceGenerator *pg = find_pref_gen(op->peer_id,
  2080. op->pref_type);
  2081. if (NULL != pg)
  2082. {
  2083. GNUNET_log(GNUNET_ERROR_TYPE_INFO,
  2084. "Stopping property generation for peer %u address %u\n", op->peer_id,
  2085. op->address_id);
  2086. GNUNET_ATS_solver_generate_preferences_stop (pg);
  2087. }
  2088. else
  2089. {
  2090. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2091. "Cannot find preference generator for peer %u address %u\n",
  2092. op->peer_id, op->address_id);
  2093. GNUNET_break (0);
  2094. }
  2095. }
  2096. static void
  2097. enforce_start_request (struct GNUNET_ATS_TEST_Operation *op)
  2098. {
  2099. struct TestPeer *p;
  2100. const struct ATS_Address *res;
  2101. if (NULL == (p = find_peer_by_id (op->peer_id)))
  2102. {
  2103. GNUNET_break (0);
  2104. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2105. "Requesting address for unknown peer %u\n", op->peer_id);
  2106. return;
  2107. }
  2108. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Requesting address for peer %u\n",
  2109. op->peer_id);
  2110. p->is_requested = GNUNET_YES;
  2111. res = sh->sf->s_get (sh->sf->cls, &p->peer_id);
  2112. if (NULL != res)
  2113. {
  2114. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Suggested address for peer %u: %llu %llu\n",
  2115. op->peer_id,
  2116. res->assigned_bw_in,
  2117. res->assigned_bw_out);
  2118. if (NULL != l)
  2119. GNUNET_ATS_solver_logging_now (l);
  2120. }
  2121. }
  2122. static void
  2123. enforce_stop_request (struct GNUNET_ATS_TEST_Operation *op)
  2124. {
  2125. struct TestPeer *p;
  2126. if (NULL == (p = find_peer_by_id (op->peer_id)))
  2127. {
  2128. GNUNET_break (0);
  2129. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2130. "Requesting address for unknown peer %u\n", op->peer_id);
  2131. return;
  2132. }
  2133. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stop requesting address for peer %u\n",
  2134. op->peer_id);
  2135. p->is_requested = GNUNET_NO;
  2136. p->assigned_bw_in = 0;
  2137. p->assigned_bw_out = 0;
  2138. sh->sf->s_get_stop (sh->sf->cls, &p->peer_id);
  2139. if (NULL != l)
  2140. {
  2141. GNUNET_ATS_solver_logging_now (l);
  2142. }
  2143. }
  2144. static void enforce_episode (struct Episode *ep)
  2145. {
  2146. struct GNUNET_ATS_TEST_Operation *cur;
  2147. for (cur = ep->head; NULL != cur; cur = cur->next)
  2148. {
  2149. switch (cur->type) {
  2150. case SOLVER_OP_ADD_ADDRESS:
  2151. fprintf (stderr, "Enforcing operation: %s [%llu:%llu]\n",
  2152. print_op (cur->type), cur->peer_id, cur->address_id);
  2153. enforce_add_address (cur);
  2154. break;
  2155. case SOLVER_OP_DEL_ADDRESS:
  2156. fprintf (stderr, "Enforcing operation: %s [%llu:%llu]\n",
  2157. print_op (cur->type), cur->peer_id, cur->address_id);
  2158. enforce_del_address (cur);
  2159. break;
  2160. case SOLVER_OP_START_SET_PROPERTY:
  2161. fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
  2162. print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
  2163. enforce_start_property (cur);
  2164. break;
  2165. case SOLVER_OP_STOP_SET_PROPERTY:
  2166. fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
  2167. print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
  2168. enforce_stop_property (cur);
  2169. break;
  2170. case SOLVER_OP_START_SET_PREFERENCE:
  2171. fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
  2172. print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
  2173. enforce_start_preference (cur);
  2174. break;
  2175. case SOLVER_OP_STOP_SET_PREFERENCE:
  2176. fprintf (stderr, "Enforcing operation: %s [%llu:%llu] == %llu\n",
  2177. print_op (cur->type), cur->peer_id, cur->address_id, cur->base_rate);
  2178. enforce_stop_preference (cur);
  2179. break;
  2180. case SOLVER_OP_START_REQUEST:
  2181. fprintf (stderr, "Enforcing operation: %s [%llu]\n",
  2182. print_op (cur->type), cur->peer_id);
  2183. enforce_start_request (cur);
  2184. break;
  2185. case SOLVER_OP_STOP_REQUEST:
  2186. fprintf (stderr, "Enforcing operation: %s [%llu]\n",
  2187. print_op (cur->type), cur->peer_id);
  2188. enforce_stop_request (cur);
  2189. break;
  2190. default:
  2191. break;
  2192. }
  2193. }
  2194. }
  2195. static void
  2196. timeout_episode (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
  2197. {
  2198. struct Experiment *e = cls;
  2199. e->episode_timeout_task = NULL;
  2200. if (NULL != e->ep_done_cb)
  2201. e->ep_done_cb (e->cur);
  2202. /* Scheduling next */
  2203. e->cur = e->cur->next;
  2204. if (NULL == e->cur)
  2205. {
  2206. /* done */
  2207. fprintf (stderr, "Last episode done!\n");
  2208. if (NULL != e->experiment_timeout_task)
  2209. {
  2210. GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
  2211. e->experiment_timeout_task = NULL;
  2212. }
  2213. e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
  2214. return;
  2215. }
  2216. fprintf (stderr, "Running episode %u with timeout %s\n",
  2217. e->cur->id,
  2218. GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
  2219. e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
  2220. &timeout_episode, e);
  2221. enforce_episode(e->cur);
  2222. }
  2223. void
  2224. GNUNET_ATS_solvers_experimentation_run (struct Experiment *e,
  2225. GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
  2226. GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
  2227. {
  2228. fprintf (stderr, "Running experiment `%s' with timeout %s\n", e->name,
  2229. GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
  2230. e->e_done_cb = e_done_cb;
  2231. e->ep_done_cb = ep_done_cb;
  2232. e->start_time = GNUNET_TIME_absolute_get();
  2233. /* Start total time out */
  2234. e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
  2235. &timeout_experiment, e);
  2236. /* Start */
  2237. if (NULL == e->start)
  2238. {
  2239. GNUNET_break (0);
  2240. return;
  2241. }
  2242. e->cur = e->start;
  2243. fprintf (stderr, "Running episode %u with timeout %s\n",
  2244. e->cur->id,
  2245. GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
  2246. e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
  2247. &timeout_episode, e);
  2248. enforce_episode(e->cur);
  2249. }
  2250. void
  2251. GNUNET_ATS_solvers_experimentation_stop (struct Experiment *e)
  2252. {
  2253. if (NULL != e->experiment_timeout_task)
  2254. {
  2255. GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
  2256. e->experiment_timeout_task = NULL;
  2257. }
  2258. if (NULL != e->episode_timeout_task)
  2259. {
  2260. GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
  2261. e->episode_timeout_task = NULL;
  2262. }
  2263. if (NULL != e->cfg)
  2264. {
  2265. GNUNET_CONFIGURATION_destroy(e->cfg);
  2266. e->cfg = NULL;
  2267. }
  2268. free_experiment (e);
  2269. }
  2270. struct Experiment *
  2271. GNUNET_ATS_solvers_experimentation_load (char *filename)
  2272. {
  2273. struct Experiment *e;
  2274. struct GNUNET_CONFIGURATION_Handle *cfg;
  2275. e = NULL;
  2276. cfg = GNUNET_CONFIGURATION_create();
  2277. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
  2278. {
  2279. fprintf (stderr, "Failed to load `%s'\n", filename);
  2280. GNUNET_CONFIGURATION_destroy (cfg);
  2281. return NULL;
  2282. }
  2283. e = create_experiment ();
  2284. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
  2285. "name", &e->name))
  2286. {
  2287. fprintf (stderr, "Invalid %s \n", "name");
  2288. free_experiment (e);
  2289. return NULL;
  2290. }
  2291. else
  2292. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment name: `%s'\n", e->name);
  2293. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
  2294. "log_prefix", &e->log_prefix))
  2295. {
  2296. fprintf (stderr, "Invalid %s \n", "log_prefix");
  2297. free_experiment (e);
  2298. return NULL;
  2299. }
  2300. else
  2301. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging prefix: `%s'\n",
  2302. e->log_prefix);
  2303. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
  2304. "log_output_dir", &e->log_output_dir))
  2305. {
  2306. e->log_output_dir = NULL;
  2307. }
  2308. else
  2309. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging output directory: `%s'\n",
  2310. e->log_output_dir);
  2311. if (GNUNET_SYSERR == (e->log_append_time_stamp = GNUNET_CONFIGURATION_get_value_yesno(cfg,
  2312. "experiment", "log_append_time_stamp")))
  2313. e->log_append_time_stamp = GNUNET_YES;
  2314. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging append timestamp: `%s'\n",
  2315. (GNUNET_YES == e->log_append_time_stamp) ? "yes" : "no");
  2316. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
  2317. "cfg_file", &e->cfg_file))
  2318. {
  2319. fprintf (stderr, "Invalid %s \n", "cfg_file");
  2320. free_experiment (e);
  2321. return NULL;
  2322. }
  2323. else
  2324. {
  2325. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment configuration: `%s'\n", e->cfg_file);
  2326. e->cfg = GNUNET_CONFIGURATION_create();
  2327. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (e->cfg, e->cfg_file))
  2328. {
  2329. fprintf (stderr, "Invalid configuration %s \n", "cfg_file");
  2330. free_experiment (e);
  2331. return NULL;
  2332. }
  2333. }
  2334. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
  2335. "log_freq", &e->log_freq))
  2336. {
  2337. fprintf (stderr, "Invalid %s \n", "log_freq");
  2338. free_experiment (e);
  2339. return NULL;
  2340. }
  2341. else
  2342. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment logging frequency: `%s'\n",
  2343. GNUNET_STRINGS_relative_time_to_string (e->log_freq, GNUNET_YES));
  2344. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
  2345. "max_duration", &e->max_duration))
  2346. {
  2347. fprintf (stderr, "Invalid %s", "max_duration");
  2348. free_experiment (e);
  2349. return NULL;
  2350. }
  2351. else
  2352. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment duration: `%s'\n",
  2353. GNUNET_STRINGS_relative_time_to_string (e->max_duration, GNUNET_YES));
  2354. if (GNUNET_SYSERR == load_episodes (e, cfg))
  2355. {
  2356. GNUNET_ATS_solvers_experimentation_stop (e);
  2357. GNUNET_CONFIGURATION_destroy (cfg);
  2358. e = NULL;
  2359. fprintf (stderr, "Failed to load experiment\n");
  2360. return NULL;
  2361. }
  2362. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Loaded %u episodes with total duration %s\n",
  2363. e->num_episodes,
  2364. GNUNET_STRINGS_relative_time_to_string (e->total_duration, GNUNET_YES));
  2365. GNUNET_CONFIGURATION_destroy (cfg);
  2366. return e;
  2367. }
  2368. /**
  2369. * Solver
  2370. */
  2371. static int
  2372. free_all_it (void *cls,
  2373. const struct GNUNET_PeerIdentity *key,
  2374. void *value)
  2375. {
  2376. struct ATS_Address *address = value;
  2377. GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (sh->env.addresses,
  2378. key, value));
  2379. GNUNET_free (address);
  2380. return GNUNET_OK;
  2381. }
  2382. void
  2383. GNUNET_ATS_solvers_solver_stop (struct SolverHandle *sh)
  2384. {
  2385. GNUNET_STATISTICS_destroy ((struct GNUNET_STATISTICS_Handle *) sh->env.stats,
  2386. GNUNET_NO);
  2387. GNUNET_PLUGIN_unload (sh->plugin, sh->sf);
  2388. sh->sf = NULL;
  2389. GAS_normalization_stop();
  2390. GNUNET_CONTAINER_multipeermap_iterate (sh->addresses,
  2391. &free_all_it,
  2392. NULL);
  2393. GNUNET_CONTAINER_multipeermap_destroy(sh->addresses);
  2394. GNUNET_free (sh->plugin);
  2395. GNUNET_free (sh);
  2396. }
  2397. /**
  2398. * Load quotas for networks from configuration
  2399. *
  2400. * @param cfg configuration handle
  2401. * @param out_dest where to write outbound quotas
  2402. * @param in_dest where to write inbound quotas
  2403. * @param dest_length length of inbound and outbound arrays
  2404. * @return number of networks loaded
  2405. */
  2406. unsigned int
  2407. GNUNET_ATS_solvers_load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
  2408. unsigned long long *out_dest,
  2409. unsigned long long *in_dest,
  2410. int dest_length)
  2411. {
  2412. char * entry_in = NULL;
  2413. char * entry_out = NULL;
  2414. char * quota_out_str;
  2415. char * quota_in_str;
  2416. int c;
  2417. int res;
  2418. for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
  2419. {
  2420. in_dest[c] = 0;
  2421. out_dest[c] = 0;
  2422. GNUNET_asprintf (&entry_out,
  2423. "%s_QUOTA_OUT",
  2424. GNUNET_ATS_print_network_type (c));
  2425. GNUNET_asprintf (&entry_in,
  2426. "%s_QUOTA_IN",
  2427. GNUNET_ATS_print_network_type (c));
  2428. /* quota out */
  2429. if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
  2430. {
  2431. res = GNUNET_NO;
  2432. if (0 == strcmp(quota_out_str, BIG_M_STRING))
  2433. {
  2434. out_dest[c] = GNUNET_ATS_MaxBandwidth;
  2435. res = GNUNET_YES;
  2436. }
  2437. if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
  2438. res = GNUNET_YES;
  2439. if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out, &out_dest[c])))
  2440. res = GNUNET_YES;
  2441. if (GNUNET_NO == res)
  2442. {
  2443. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2444. _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
  2445. GNUNET_ATS_print_network_type (c),
  2446. quota_out_str,
  2447. GNUNET_ATS_DefaultBandwidth);
  2448. out_dest[c] = GNUNET_ATS_DefaultBandwidth;
  2449. }
  2450. else
  2451. {
  2452. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2453. "Outbound quota configure for network `%s' is %llu\n",
  2454. GNUNET_ATS_print_network_type (c),
  2455. out_dest[c]);
  2456. }
  2457. GNUNET_free (quota_out_str);
  2458. }
  2459. else
  2460. {
  2461. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2462. _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
  2463. GNUNET_ATS_print_network_type (c),
  2464. GNUNET_ATS_DefaultBandwidth);
  2465. out_dest[c] = GNUNET_ATS_DefaultBandwidth;
  2466. }
  2467. /* quota in */
  2468. if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
  2469. {
  2470. res = GNUNET_NO;
  2471. if (0 == strcmp(quota_in_str, BIG_M_STRING))
  2472. {
  2473. in_dest[c] = GNUNET_ATS_MaxBandwidth;
  2474. res = GNUNET_YES;
  2475. }
  2476. if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
  2477. res = GNUNET_YES;
  2478. if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in, &in_dest[c])))
  2479. res = GNUNET_YES;
  2480. if (GNUNET_NO == res)
  2481. {
  2482. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2483. _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
  2484. GNUNET_ATS_print_network_type (c),
  2485. quota_in_str,
  2486. GNUNET_ATS_DefaultBandwidth);
  2487. in_dest[c] = GNUNET_ATS_DefaultBandwidth;
  2488. }
  2489. else
  2490. {
  2491. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2492. "Inbound quota configured for network `%s' is %llu\n",
  2493. GNUNET_ATS_print_network_type (c),
  2494. in_dest[c]);
  2495. }
  2496. GNUNET_free (quota_in_str);
  2497. }
  2498. else
  2499. {
  2500. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2501. _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
  2502. GNUNET_ATS_print_network_type (c),
  2503. GNUNET_ATS_DefaultBandwidth);
  2504. out_dest[c] = GNUNET_ATS_DefaultBandwidth;
  2505. }
  2506. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  2507. "Loaded quota for network `%s' (in/out): %llu %llu\n",
  2508. GNUNET_ATS_print_network_type (c),
  2509. in_dest[c],
  2510. out_dest[c]);
  2511. GNUNET_free (entry_out);
  2512. GNUNET_free (entry_in);
  2513. }
  2514. return GNUNET_ATS_NetworkTypeCount;
  2515. }
  2516. /**
  2517. * Information callback for the solver
  2518. *
  2519. * @param cls the closure
  2520. * @param op the solver operation
  2521. * @param stat status of the solver operation
  2522. * @param add additional solver information
  2523. */
  2524. static void
  2525. solver_info_cb (void *cls,
  2526. enum GAS_Solver_Operation op,
  2527. enum GAS_Solver_Status stat,
  2528. enum GAS_Solver_Additional_Information add)
  2529. {
  2530. char *add_info;
  2531. switch (add) {
  2532. case GAS_INFO_NONE:
  2533. add_info = "GAS_INFO_NONE";
  2534. break;
  2535. case GAS_INFO_FULL:
  2536. add_info = "GAS_INFO_MLP_FULL";
  2537. break;
  2538. case GAS_INFO_UPDATED:
  2539. add_info = "GAS_INFO_MLP_UPDATED";
  2540. break;
  2541. case GAS_INFO_PROP_ALL:
  2542. add_info = "GAS_INFO_PROP_ALL";
  2543. break;
  2544. case GAS_INFO_PROP_SINGLE:
  2545. add_info = "GAS_INFO_PROP_SINGLE";
  2546. break;
  2547. default:
  2548. add_info = "INVALID";
  2549. break;
  2550. }
  2551. switch (op)
  2552. {
  2553. case GAS_OP_SOLVE_START:
  2554. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
  2555. "Solver notifies `%s' with result `%s' `%s'\n", "GAS_OP_SOLVE_START",
  2556. (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
  2557. return;
  2558. case GAS_OP_SOLVE_STOP:
  2559. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
  2560. "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_STOP",
  2561. (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
  2562. return;
  2563. case GAS_OP_SOLVE_SETUP_START:
  2564. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
  2565. "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
  2566. (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
  2567. return;
  2568. case GAS_OP_SOLVE_SETUP_STOP:
  2569. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
  2570. "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
  2571. (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
  2572. return;
  2573. case GAS_OP_SOLVE_MLP_LP_START:
  2574. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
  2575. "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
  2576. (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
  2577. return;
  2578. case GAS_OP_SOLVE_MLP_LP_STOP:
  2579. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
  2580. "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
  2581. (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
  2582. return;
  2583. case GAS_OP_SOLVE_MLP_MLP_START:
  2584. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
  2585. "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
  2586. (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
  2587. return;
  2588. case GAS_OP_SOLVE_MLP_MLP_STOP:
  2589. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
  2590. "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
  2591. (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
  2592. return;
  2593. case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
  2594. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
  2595. "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
  2596. (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
  2597. return;
  2598. case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
  2599. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
  2600. "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
  2601. (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
  2602. return;
  2603. default:
  2604. break;
  2605. }
  2606. }
  2607. static void
  2608. solver_bandwidth_changed_cb (void *cls, struct ATS_Address *address)
  2609. {
  2610. struct GNUNET_TIME_Relative duration;
  2611. struct TestPeer *p;
  2612. static struct PreferenceGenerator *pg;
  2613. uint32_t delta;
  2614. if ( (0 == address->assigned_bw_out) && (0 == address->assigned_bw_in) )
  2615. {
  2616. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  2617. "Solver notified to disconnect peer `%s'\n",
  2618. GNUNET_i2s (&address->peer));
  2619. }
  2620. p = find_peer_by_pid(&address->peer);
  2621. if (NULL == p)
  2622. return;
  2623. p->assigned_bw_out = address->assigned_bw_out;
  2624. p->assigned_bw_in = address->assigned_bw_in;
  2625. for (pg = pref_gen_head; NULL != pg; pg = pg->next)
  2626. {
  2627. if (pg->peer == p->id)
  2628. {
  2629. duration = GNUNET_TIME_absolute_get_duration(pg->feedback_last_bw_update);
  2630. delta = duration.rel_value_us * pg->last_assigned_bw_out;
  2631. pg->feedback_bw_out_acc += delta;
  2632. delta = duration.rel_value_us * pg->last_assigned_bw_in;
  2633. pg->feedback_bw_in_acc += delta;
  2634. pg->last_assigned_bw_in = address->assigned_bw_in;
  2635. pg->last_assigned_bw_out = address->assigned_bw_out;
  2636. pg->feedback_last_bw_update = GNUNET_TIME_absolute_get();
  2637. }
  2638. }
  2639. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  2640. "Bandwidth changed addresses %s %p to %u Bps out / %u Bps in\n",
  2641. GNUNET_i2s (&address->peer),
  2642. address,
  2643. address->assigned_bw_out,
  2644. address->assigned_bw_in);
  2645. if (NULL != l)
  2646. GNUNET_ATS_solver_logging_now (l);
  2647. return;
  2648. }
  2649. const double *
  2650. get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
  2651. {
  2652. struct TestPeer *p;
  2653. if (GNUNET_YES == opt_disable_normalization)
  2654. {
  2655. if (NULL == (p = find_peer_by_pid (id)))
  2656. return NULL;
  2657. return p->pref_abs;
  2658. }
  2659. else
  2660. return GAS_normalization_get_preferences_by_peer (NULL,
  2661. id);
  2662. }
  2663. const double *
  2664. get_property_cb (void *cls, const struct ATS_Address *address)
  2665. {
  2666. struct TestPeer *p;
  2667. struct TestAddress *a;
  2668. if (GNUNET_YES == opt_disable_normalization)
  2669. {
  2670. p = find_peer_by_pid (&address->peer);
  2671. if (NULL == p)
  2672. return NULL;
  2673. a = find_address_by_ats_address (p, address);
  2674. return a->prop_abs;
  2675. }
  2676. return GAS_normalization_get_properties (NULL,
  2677. address);
  2678. }
  2679. struct SolverHandle *
  2680. GNUNET_ATS_solvers_solver_start (enum GNUNET_ATS_Solvers type)
  2681. {
  2682. struct SolverHandle *sh;
  2683. char * solver_str;
  2684. switch (type) {
  2685. case GNUNET_ATS_SOLVER_PROPORTIONAL:
  2686. solver_str = "proportional";
  2687. break;
  2688. case GNUNET_ATS_SOLVER_MLP:
  2689. solver_str = "mlp";
  2690. break;
  2691. case GNUNET_ATS_SOLVER_RIL:
  2692. solver_str = "ril";
  2693. break;
  2694. default:
  2695. GNUNET_break (0);
  2696. return NULL;
  2697. break;
  2698. }
  2699. sh = GNUNET_new (struct SolverHandle);
  2700. GNUNET_asprintf (&sh->plugin,
  2701. "libgnunet_plugin_ats_%s",
  2702. solver_str);
  2703. sh->addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
  2704. /* setup environment */
  2705. sh->env.cfg = e->cfg;
  2706. sh->env.stats = GNUNET_STATISTICS_create ("ats", e->cfg);
  2707. sh->env.addresses = sh->addresses;
  2708. sh->env.bandwidth_changed_cb = &solver_bandwidth_changed_cb;
  2709. sh->env.get_preferences = &get_preferences_cb;
  2710. sh->env.get_property = &get_property_cb;
  2711. sh->env.network_count = GNUNET_ATS_NetworkTypeCount;
  2712. sh->env.info_cb = &solver_info_cb;
  2713. sh->env.network_count = GNUNET_ATS_NetworkTypeCount;
  2714. /* start normalization */
  2715. GAS_normalization_start ();
  2716. /* load quotas */
  2717. if (GNUNET_ATS_NetworkTypeCount != GNUNET_ATS_solvers_load_quotas (e->cfg,
  2718. sh->env.out_quota, sh->env.in_quota, GNUNET_ATS_NetworkTypeCount))
  2719. {
  2720. GNUNET_break(0);
  2721. GNUNET_free (sh->plugin);
  2722. GNUNET_free (sh);
  2723. end_now ();
  2724. return NULL;
  2725. }
  2726. sh->sf = GNUNET_PLUGIN_load (sh->plugin, &sh->env);
  2727. if (NULL == sh->sf)
  2728. {
  2729. fprintf (stderr, "Failed to load solver `%s'\n", sh->plugin);
  2730. GNUNET_break(0);
  2731. GNUNET_free (sh->plugin);
  2732. GNUNET_free (sh);
  2733. end_now ();
  2734. return NULL;
  2735. }
  2736. return sh;
  2737. }
  2738. static void
  2739. done ()
  2740. {
  2741. struct TestPeer *cur;
  2742. struct TestPeer *next;
  2743. struct TestAddress *cur_a;
  2744. struct TestAddress *next_a;
  2745. /* Stop logging */
  2746. GNUNET_ATS_solver_logging_stop (l);
  2747. /* Stop all preference generation */
  2748. GNUNET_ATS_solver_generate_preferences_stop_all ();
  2749. /* Stop all property generation */
  2750. GNUNET_ATS_solver_generate_property_stop_all ();
  2751. if (opt_print)
  2752. {
  2753. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "== Printing log information \n");
  2754. GNUNET_ATS_solver_logging_eval (l);
  2755. }
  2756. if (opt_save)
  2757. {
  2758. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "== Saving log information \n");
  2759. GNUNET_ATS_solver_logging_write_to_disk (l, e->log_append_time_stamp,
  2760. e->log_output_dir);
  2761. }
  2762. if (NULL != l)
  2763. {
  2764. GNUNET_ATS_solver_logging_free (l);
  2765. l = NULL;
  2766. }
  2767. /* Clean up experiment */
  2768. if (NULL != e)
  2769. {
  2770. GNUNET_ATS_solvers_experimentation_stop (e);
  2771. e = NULL;
  2772. }
  2773. next = peer_head;
  2774. while (NULL != (cur = next))
  2775. {
  2776. next = cur->next;
  2777. GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, cur);
  2778. next_a = cur->addr_head;
  2779. while (NULL != (cur_a = next_a))
  2780. {
  2781. next_a = cur_a->next;
  2782. GNUNET_CONTAINER_DLL_remove (cur->addr_head, cur->addr_tail, cur_a);
  2783. GNUNET_free (cur_a);
  2784. }
  2785. GNUNET_free (cur);
  2786. }
  2787. if (NULL != sh)
  2788. {
  2789. GNUNET_ATS_solvers_solver_stop (sh);
  2790. sh = NULL;
  2791. }
  2792. /* Shutdown */
  2793. end_now();
  2794. }
  2795. static void
  2796. experiment_done_cb (struct Experiment *e, struct GNUNET_TIME_Relative duration,int success)
  2797. {
  2798. if (GNUNET_OK == success)
  2799. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment done successful in %s\n",
  2800. GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
  2801. else
  2802. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment failed \n");
  2803. GNUNET_SCHEDULER_add_now (&done, NULL);
  2804. }
  2805. static void
  2806. episode_done_cb (struct Episode *ep)
  2807. {
  2808. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Episode %u done\n", ep->id);
  2809. }
  2810. /**
  2811. * Do shutdown
  2812. */
  2813. static void
  2814. end_now ()
  2815. {
  2816. if (NULL != e)
  2817. {
  2818. GNUNET_ATS_solvers_experimentation_stop (e);
  2819. e = NULL;
  2820. }
  2821. if (NULL != sh)
  2822. {
  2823. GNUNET_ATS_solvers_solver_stop (sh);
  2824. sh = NULL;
  2825. }
  2826. }
  2827. static void
  2828. run (void *cls, char * const *args, const char *cfgfile,
  2829. const struct GNUNET_CONFIGURATION_Handle *cfg)
  2830. {
  2831. enum GNUNET_ATS_Solvers solver;
  2832. int c;
  2833. if (NULL == opt_exp_file)
  2834. {
  2835. fprintf (stderr, "No experiment given ...\n");
  2836. res = 1;
  2837. end_now ();
  2838. return;
  2839. }
  2840. if (NULL == opt_solver)
  2841. {
  2842. fprintf (stderr, "No solver given ...\n");
  2843. res = 1;
  2844. end_now ();
  2845. return;
  2846. }
  2847. if (0 == strcmp(opt_solver, "mlp"))
  2848. {
  2849. solver = GNUNET_ATS_SOLVER_MLP;
  2850. }
  2851. else if (0 == strcmp(opt_solver, "proportional"))
  2852. {
  2853. solver = GNUNET_ATS_SOLVER_PROPORTIONAL;
  2854. }
  2855. else if (0 == strcmp(opt_solver, "ril"))
  2856. {
  2857. solver = GNUNET_ATS_SOLVER_RIL;
  2858. }
  2859. else
  2860. {
  2861. fprintf (stderr, "No solver given ...");
  2862. res = 1;
  2863. end_now ();
  2864. return;
  2865. }
  2866. for (c = 0; c < GNUNET_ATS_PropertyCount; c++)
  2867. default_properties[c] = DEFAULT_REL_QUALITY;
  2868. for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
  2869. default_preferences[c] = DEFAULT_REL_PREFERENCE;
  2870. /* load experiment */
  2871. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Loading experiment\n");
  2872. e = GNUNET_ATS_solvers_experimentation_load (opt_exp_file);
  2873. if (NULL == e)
  2874. {
  2875. fprintf (stderr, "Failed to load experiment ...\n");
  2876. res = 1;
  2877. end_now ();
  2878. return;
  2879. }
  2880. /* load solver */
  2881. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Loading solver\n");
  2882. sh = GNUNET_ATS_solvers_solver_start (solver);
  2883. if (NULL == sh)
  2884. {
  2885. fprintf (stderr, "Failed to start solver ...\n");
  2886. end_now ();
  2887. res = 1;
  2888. return;
  2889. }
  2890. /* start logging */
  2891. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Start logging \n");
  2892. l = GNUNET_ATS_solver_logging_start (e->log_freq);
  2893. /* run experiment */
  2894. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "=== Running experiment \n");
  2895. GNUNET_ATS_solvers_experimentation_run (e, episode_done_cb,
  2896. experiment_done_cb);
  2897. /* WAIT */
  2898. }
  2899. /**
  2900. * Main function of the benchmark
  2901. *
  2902. * @param argc argument count
  2903. * @param argv argument values
  2904. */
  2905. int
  2906. main (int argc, char *argv[])
  2907. {
  2908. opt_exp_file = NULL;
  2909. opt_solver = NULL;
  2910. opt_log = GNUNET_NO;
  2911. opt_save = GNUNET_NO;
  2912. res = 0;
  2913. static struct GNUNET_GETOPT_CommandLineOption options[] =
  2914. {
  2915. { 's', "solver", NULL,
  2916. gettext_noop ("solver to use"),
  2917. 1, &GNUNET_GETOPT_set_string, &opt_solver},
  2918. { 'e', "experiment", NULL,
  2919. gettext_noop ("experiment to use"),
  2920. 1, &GNUNET_GETOPT_set_string, &opt_exp_file},
  2921. { 'V', "verbose", NULL,
  2922. gettext_noop ("be verbose"),
  2923. 0, &GNUNET_GETOPT_set_one, &opt_verbose},
  2924. { 'p', "print", NULL,
  2925. gettext_noop ("print logging"),
  2926. 0, &GNUNET_GETOPT_set_one, &opt_print},
  2927. { 'f', "file", NULL,
  2928. gettext_noop ("save logging to disk"),
  2929. 0, &GNUNET_GETOPT_set_one, &opt_save},
  2930. { 'd', "dn", NULL,
  2931. gettext_noop ("disable normalization"),
  2932. 0, &GNUNET_GETOPT_set_one, &opt_disable_normalization},
  2933. GNUNET_GETOPT_OPTION_END
  2934. };
  2935. GNUNET_PROGRAM_run (argc, argv, "gnunet-ats-solver-eval",
  2936. NULL, options, &run, argv[0]);
  2937. return res;
  2938. }
  2939. /* end of file ats-testing-experiment.c*/