fixing resource leaks
[oweals/gnunet.git] / src / ats / perf_ats_solver.c
1 /*
2  This file is part of GNUnet.
3  (C) 2010,2011 Christian Grothoff (and other contributing authors)
4
5  GNUnet is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published
7  by the Free Software Foundation; either version 3, or (at your
8  option) any later version.
9
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  General Public License for more details.
14
15  You should have received a copy of the GNU General Public License
16  along with GNUnet; see the file COPYING.  If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  Boston, MA 02111-1307, USA.
19  */
20 /**
21  * @file ats/perf_ats_solver.c
22  * @brief generic performance test for ATS solvers
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_statistics_service.h"
29 #include "gnunet-service-ats_addresses.h"
30 #include "gnunet-service-ats_normalization.h"
31 #include "gnunet_ats_service.h"
32 #include "gnunet_ats_plugin.h"
33 #include "test_ats_api_common.h"
34
35 #define DEFAULT_UPDATE_PERCENTAGE       20
36 #define DEFAULT_PEERS_START     10
37 #define DEFAULT_PEERS_END       10
38 #define DEFAULT_ADDRESSES       10
39 #define DEFAULT_ATS_COUNT       2
40
41 #define GNUPLOT_PROP_TEMPLATE "#!/usr/bin/gnuplot \n" \
42 "set datafile separator ';' \n" \
43 "set title \"Execution time Proportional solver  \" \n" \
44 "set xlabel \"Number of peers\" \n" \
45 "set ylabel \"Execution time in us\" \n" \
46 "set grid \n"
47
48 #define GNUPLOT_PROP_UPDATE_TEMPLATE "#!/usr/bin/gnuplot \n" \
49 "set datafile separator ';' \n" \
50 "set title \"Execution time Proportional solver with updated problem\" \n" \
51 "set xlabel \"Number of peers\" \n" \
52 "set ylabel \"Execution time in us\" \n" \
53 "set grid \n"
54
55 #define GNUPLOT_MLP_TEMPLATE "#!/usr/bin/gnuplot \n" \
56 "set datafile separator ';' \n" \
57 "set title \"Execution time MLP solver \" \n" \
58 "set xlabel \"Number of peers\" \n" \
59 "set ylabel \"Execution time in us\" \n" \
60 "set grid \n"
61
62 #define GNUPLOT_MLP_UPDATE_TEMPLATE "#!/usr/bin/gnuplot \n" \
63 "set datafile separator ';' \n" \
64 "set title \"Execution time MLP solver with updated problem\" \n" \
65 "set xlabel \"Number of peers\" \n" \
66 "set ylabel \"Execution time in us\" \n" \
67 "set grid \n"
68
69 #define GNUPLOT_RIL_TEMPLATE "#!/usr/bin/gnuplot \n" \
70 "set datafile separator ';' \n" \
71 "set title \"Execution time RIL solver \" \n" \
72 "set xlabel \"Number of peers\" \n" \
73 "set ylabel \"Execution time in us\" \n" \
74 "set grid \n"
75
76 #define GNUPLOT_RIL_UPDATE_TEMPLATE "#!/usr/bin/gnuplot \n" \
77 "set datafile separator ';' \n" \
78 "set title \"Execution time RIL solver with updated problem\" \n" \
79 "set xlabel \"Number of peers\" \n" \
80 "set ylabel \"Execution time in us\" \n" \
81 "set grid \n"
82
83 /**
84  * Handle for ATS address component
85  */
86 struct PerfHandle
87 {
88   /**
89    * Performance peers
90    */
91   struct PerfPeer *peers;
92
93   /**
94    *  Solver handle
95    */
96   void *solver;
97
98   /**
99    * Statistics stat;
100    */
101   struct GNUNET_STATISTICS_Handle *stat;
102
103   /**
104    * A multihashmap to store all addresses
105    */
106   struct GNUNET_CONTAINER_MultiPeerMap *addresses;
107
108   /**
109    * Solver functions
110    * */
111   struct GNUNET_ATS_PluginEnvironment env;
112
113   /**
114    * Array for results for each iteration with length iterations
115    */
116   struct Iteration *iterations_results;
117
118   /**
119    * Array to store averaged full solution result with length #peers
120    */
121   struct Result *averaged_full_result;
122
123   /**
124    * Array to store averaged updated solution result with length #peers
125    */
126   struct Result *averaged_update_result;
127
128   /**
129    * The current result
130    */
131   struct Result *current_result;
132
133   /**
134    * Current number of peers benchmarked
135    */
136   int current_p;
137
138   /**
139    * Current number of addresses benchmarked
140    */
141   int current_a;
142
143   /**
144    * Solver description as string
145    */
146   char *ats_string;
147
148   /**
149    * Configured ATS solver
150    */
151   int ats_mode;
152
153   /**
154    * #peers to start benchmarking with
155    */
156   int N_peers_start;
157
158   /**
159    * #peers to end benchmarking with
160    */
161   int N_peers_end;
162
163   /**
164    * #addresses to benchmarking with
165    */
166   int N_address;
167
168   /**
169    * Percentage of peers to update
170    */
171   int opt_update_percent;
172
173   /**
174    * Create gnuplot file
175    */
176   int create_plot;
177
178   /**
179    * Measure updates
180    */
181   int measure_updates;
182
183   /**
184    * Number of iterations
185    */
186   int total_iterations;
187
188   /**
189    * Current iteration
190    */
191   int current_iteration;
192
193   /**
194    * Is a bulk operation running?
195    */
196   int bulk_running;
197
198   /**
199    * Is a bulk operation running?
200    */
201   int expecting_solution;
202
203   /**
204    * Was the problem just updates?
205    */
206   int performed_update;
207 };
208
209 /**
210  * Data structure to store results for a single iteration
211  */
212 struct Iteration
213 {
214   /**
215    * Head of the linked list
216    */
217   struct Result *result_head;
218
219   /**
220    * Tail of the linked list
221    */
222   struct Result *result_tail;
223 };
224
225
226 /**
227  * Result for a solver calculcation
228  */
229 struct Result
230 {
231   /**
232    * Previous element in the linked list
233    */
234   struct Result *prev;
235
236   /**
237    * Next element in the linked list
238    */
239   struct Result *next;
240
241   /**
242    * Number of peers this solution included
243    */
244   int peers;
245
246   /**
247    * Number of addresses per peer this solution included
248    */
249   int addresses;
250
251   /**
252    * Is this an update or a full solution
253    */
254   int update;
255
256   /**
257    * Was the solution valid or did the solver fail
258    */
259   int valid;
260
261   /**
262    * Result of the solver
263    */
264   enum GAS_Solver_Additional_Information info;
265
266   /**
267    * Duration of setting up the problem in the solver
268    */
269   struct GNUNET_TIME_Relative d_setup;
270
271   /**
272    * Duration of solving the LP problem in the solver
273    * MLP solver only
274    */
275   struct GNUNET_TIME_Relative d_lp;
276
277   /**
278    * Duration of solving the MLP problem in the solver
279    * MLP solver only
280    */
281   struct GNUNET_TIME_Relative d_mlp;
282
283   /**
284    * Duration of solving whole problem in the solver
285    */
286   struct GNUNET_TIME_Relative d_total;
287
288   /**
289    * Start time of setting up the problem in the solver
290    */
291   struct GNUNET_TIME_Absolute s_setup;
292
293   /**
294    * Start time of solving the LP problem in the solver
295    * MLP solver only
296    */
297   struct GNUNET_TIME_Absolute s_lp;
298
299   /**
300    * Start time of solving the MLP problem in the solver
301    * MLP solver only
302    */
303   struct GNUNET_TIME_Absolute s_mlp;
304
305   /**
306    * Start time of solving whole problem in the solver
307    */
308   struct GNUNET_TIME_Absolute s_total;
309
310   /**
311    * End time of setting up the problem in the solver
312    */
313   struct GNUNET_TIME_Absolute e_setup;
314
315   /**
316    * End time of solving the LP problem in the solver
317    * MLP solver only
318    */
319   struct GNUNET_TIME_Absolute e_lp;
320
321   /**
322    * End time of solving the MLP problem in the solver
323    * MLP solver only
324    */
325   struct GNUNET_TIME_Absolute e_mlp;
326
327   /**
328    * End time of solving whole problem in the solver
329    */
330   struct GNUNET_TIME_Absolute e_total;
331 };
332
333 /**
334  * Peer used for the benchmarking
335  */
336 struct PerfPeer
337 {
338   /**
339    * Peer identitity
340    */
341   struct GNUNET_PeerIdentity id;
342
343   /**
344    * Head of linked list of addresses used with this peer
345    */
346   struct ATS_Address *head;
347
348   /**
349    * Head of linked list of addresses used with this peer
350    */
351   struct ATS_Address *tail;
352 };
353
354
355 /**
356  * ATS performance handle
357  */
358 static struct PerfHandle ph;
359
360 /**
361  * Return value
362  */
363 static int ret;
364
365
366 /**
367  * Do shutdown
368  */
369 static void
370 end_now (int res)
371 {
372   if (NULL != ph.stat)
373   {
374     GNUNET_STATISTICS_destroy (ph.stat, GNUNET_NO);
375     ph.stat = NULL;
376   }
377
378   GNUNET_free_non_null (ph.peers);
379   GNUNET_free_non_null (ph.iterations_results);
380   GNUNET_free_non_null (ph.averaged_full_result);
381   GNUNET_free_non_null (ph.averaged_update_result);
382
383   GAS_normalization_stop ();
384   ret = res;
385 }
386
387
388 /**
389  * Create a peer used for benchmarking
390  *
391  * @param cp the number of the peer
392  */
393 static void
394 perf_create_peer (int cp)
395 {
396
397   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
398       &ph.peers[cp].id, sizeof (struct GNUNET_PeerIdentity));
399   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating peer #%u: %s \n", cp,
400       GNUNET_i2s (&ph.peers[cp].id));
401 }
402
403
404 /**
405  * Perform an update for an address
406  *
407  * @param cur the address to update
408  */
409 static void
410 perf_update_address (struct ATS_Address *cur)
411 {
412   int r_type;
413   int r_val;
414
415   r_type = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 2);
416   switch (r_type)
417   {
418   case 0:
419     r_val = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
420     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
421         "Updating peer `%s' address %p type %s val %u\n",
422         GNUNET_i2s (&cur->peer), cur, "GNUNET_ATS_QUALITY_NET_DELAY", r_val);
423     ph.env.sf.s_address_update_property (ph.solver, cur,
424         GNUNET_ATS_QUALITY_NET_DELAY,
425         r_val, (double) (100 + r_val / 100));
426     break;
427   case 1:
428     r_val = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 10);
429
430     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
431         "Updating peer `%s' address %p type %s val %u\n",
432         GNUNET_i2s (&cur->peer), cur, "GNUNET_ATS_QUALITY_NET_DISTANCE", r_val);
433     ph.env.sf.s_address_update_property (ph.solver, cur,
434         GNUNET_ATS_QUALITY_NET_DISTANCE,
435         r_val, (double) (100 + r_val) / 100);
436     break;
437   default:
438     break;
439   }
440   ph.env.sf.s_address_update_inuse (ph.solver, cur, GNUNET_YES);
441 }
442
443
444 static void
445 bandwidth_changed_cb (void *cls,
446                       struct ATS_Address *address)
447 {
448   if ( (0 == ntohl (address->assigned_bw_out.value__)) &&
449        (0 == ntohl (address->assigned_bw_in.value__)) )
450     return;
451
452   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453               "Bandwidth changed addresses %s %p to %u Bps out / %u Bps in\n",
454               GNUNET_i2s (&address->peer),
455               address,
456               (unsigned int) ntohl (address->assigned_bw_out.value__),
457               (unsigned int) ntohl (address->assigned_bw_in.value__));
458   if (GNUNET_YES == ph.bulk_running)
459     GNUNET_break (0);
460   return;
461 }
462
463
464 const double *
465 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
466 {
467   return GAS_normalization_get_preferences_by_peer (id);
468 }
469
470
471 const double *
472 get_property_cb (void *cls, const struct ATS_Address *address)
473 {
474   return GAS_normalization_get_properties ((struct ATS_Address *) address);
475 }
476
477 static void
478 normalized_property_changed_cb (void *cls, struct ATS_Address *peer,
479     uint32_t type, double prop_rel)
480 {
481   /* TODO */
482 }
483
484 static void
485 perf_address_initial_update (void *solver,
486     struct GNUNET_CONTAINER_MultiPeerMap * addresses,
487     struct ATS_Address *address)
488 {
489   ph.env.sf.s_address_update_property (solver, address, GNUNET_ATS_QUALITY_NET_DELAY,
490       100,
491       (double) (100 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100))
492           / 100);
493
494   ph.env.sf.s_address_update_property (solver, address,
495       GNUNET_ATS_QUALITY_NET_DISTANCE, 10,
496       (double) (100 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100))
497           / 100);
498 }
499
500 /**
501  * Update a certain percentage of peers
502  *
503  * @param cp the current number of peers
504  * @param ca the current number of addresses
505  * @param percentage_peers the percentage of peers to update
506  */
507
508 static void
509 perf_update_all_addresses (unsigned int cp, unsigned int ca, unsigned int percentage_peers)
510 {
511   struct ATS_Address *cur_address;
512   int c_peer;
513   int c_select;
514   int c_cur_p;
515   int c_cur_a;
516   int r;
517   int count;
518   unsigned int m[cp];
519
520   count = cp * ((double) percentage_peers / 100);
521   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
522       "Updating %u of %u peers \n", count, cp);
523
524   for (c_peer = 0; c_peer < cp; c_peer++)
525     m[c_peer] = 0;
526
527   c_select = 0;
528
529   while (c_select < count)
530   {
531     r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, cp);
532     if (0 == m[r])
533     {
534       m[r] = 1;
535       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
536           "Updating peer [%u] \n", r);
537       c_select++;
538     }
539   }
540   for (c_cur_p = 0; c_cur_p < cp; c_cur_p++)
541   {
542     if (1 == m[c_cur_p])
543     {
544       r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, ca);
545       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
546           "Updating peer [%u] address [%u]\n", c_cur_p, r);
547
548       c_cur_a = 0;
549       for (cur_address = ph.peers[c_cur_p].head; NULL != cur_address; cur_address = cur_address->next)
550       {
551         if (c_cur_a == r)
552           perf_update_address (cur_address);
553         c_cur_a ++;
554       }
555     }
556   }
557 }
558
559 /**
560  * Create an address for a peer
561  *
562  * @param cp index of the peer
563  * @param ca index of the address
564  * @return the address
565  */
566 static struct ATS_Address *
567 perf_create_address (int cp, int ca)
568 {
569   struct ATS_Address *a;
570   a = create_address (&ph.peers[cp].id,
571       "Test 1", "test 1", strlen ("test 1") + 1, 0);
572   GNUNET_CONTAINER_DLL_insert (ph.peers[cp].head, ph.peers[cp].tail, a);
573   GNUNET_CONTAINER_multipeermap_put (ph.addresses, &ph.peers[cp].id, a,
574       GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
575   return a;
576 }
577
578
579 /**
580  * Information callback for the solver
581  *
582  * @param op the solver operation
583  * @param stat status of the solver operation
584  * @param add additional solver information
585  */
586 static void
587 solver_info_cb (void *cls,
588     enum GAS_Solver_Operation op,
589     enum GAS_Solver_Status stat,
590     enum GAS_Solver_Additional_Information add)
591 {
592   char *add_info;
593   switch (add) {
594     case GAS_INFO_NONE:
595       add_info = "GAS_INFO_NONE";
596       break;
597     case GAS_INFO_FULL:
598       add_info = "GAS_INFO_MLP_FULL";
599       break;
600     case GAS_INFO_UPDATED:
601       add_info = "GAS_INFO_MLP_UPDATED";
602       break;
603     case GAS_INFO_PROP_ALL:
604       add_info = "GAS_INFO_PROP_ALL";
605       break;
606     case GAS_INFO_PROP_SINGLE:
607       add_info = "GAS_INFO_PROP_SINGLE";
608       break;
609     default:
610       add_info = "INVALID";
611       break;
612   }
613
614   struct Result *tmp;
615   switch (op)
616   {
617     case GAS_OP_SOLVE_START:
618       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
619           "Solver notifies `%s' with result `%s' `%s'\n", "GAS_OP_SOLVE_START",
620           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
621       if (GNUNET_NO == ph.expecting_solution)
622       {
623         /* We do not expect a solution at the moment */
624         GNUNET_break (0);
625         return;
626       }
627
628       if ((GAS_STAT_SUCCESS == stat) && (NULL == ph.current_result))
629       {
630         /* Create new result */
631         tmp = GNUNET_new (struct Result);
632         ph.current_result = tmp;
633         GNUNET_CONTAINER_DLL_insert_tail(ph.iterations_results[ph.current_iteration-1].result_head,
634             ph.iterations_results[ph.current_iteration-1].result_tail, tmp);
635         ph.current_result->addresses = ph.current_a;
636         ph.current_result->peers = ph.current_p;
637         ph.current_result->s_total = GNUNET_TIME_absolute_get();
638         ph.current_result->d_total = GNUNET_TIME_UNIT_FOREVER_REL;
639         ph.current_result->d_setup = GNUNET_TIME_UNIT_FOREVER_REL;
640         ph.current_result->d_lp = GNUNET_TIME_UNIT_FOREVER_REL;
641         ph.current_result->d_mlp = GNUNET_TIME_UNIT_FOREVER_REL;
642         ph.current_result->info = add;
643         if ((add == GAS_INFO_UPDATED) || (GNUNET_YES == ph.performed_update))
644         {
645           ph.current_result->update = GNUNET_YES;
646         }
647         else
648         {
649           ph.current_result->update = GNUNET_NO;
650         }
651
652       }
653       return;
654     case GAS_OP_SOLVE_STOP:
655       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
656           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_STOP",
657           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
658       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
659       {
660         /* We do not expect a solution at the moment */
661         GNUNET_break (0);
662         return;
663       }
664
665       if (GAS_STAT_SUCCESS == stat)
666         ph.current_result->valid = GNUNET_YES;
667       else
668         ph.current_result->valid = GNUNET_NO;
669
670       if (NULL != ph.current_result)
671       {
672         /* Finalize result */
673         ph.current_result->e_total = GNUNET_TIME_absolute_get ();
674         ph.current_result->d_total = GNUNET_TIME_absolute_get_difference (
675             ph.current_result->s_total, ph.current_result->e_total);
676       }
677       ph.current_result = NULL;
678       return;
679
680     case GAS_OP_SOLVE_SETUP_START:
681       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
682           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
683           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
684       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
685       {
686         GNUNET_break(0);
687         return;
688       }
689
690       if (GAS_STAT_SUCCESS == stat)
691         ph.current_result->valid = GNUNET_YES;
692       else
693         ph.current_result->valid = GNUNET_NO;
694
695       ph.current_result->s_setup = GNUNET_TIME_absolute_get ();
696       return;
697
698     case GAS_OP_SOLVE_SETUP_STOP:
699       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
700           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
701           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
702       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
703       {
704         GNUNET_break(0);
705         return;
706       }
707
708       if (GAS_STAT_SUCCESS == stat)
709         ph.current_result->valid = GNUNET_YES;
710       else
711         ph.current_result->valid = GNUNET_NO;
712
713       ph.current_result->e_setup = GNUNET_TIME_absolute_get ();
714       ph.current_result->d_setup = GNUNET_TIME_absolute_get_difference (
715           ph.current_result->s_setup, ph.current_result->e_setup);
716       return;
717
718     case GAS_OP_SOLVE_MLP_LP_START:
719       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
720           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
721           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
722       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
723       {
724         GNUNET_break(0);
725         return;
726       }
727
728       if (GAS_STAT_SUCCESS == stat)
729         ph.current_result->valid = GNUNET_YES;
730       else
731         ph.current_result->valid = GNUNET_NO;
732
733       ph.current_result->s_lp = GNUNET_TIME_absolute_get ();
734       return;
735     case GAS_OP_SOLVE_MLP_LP_STOP:
736       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
737           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
738           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
739       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
740       {
741         GNUNET_break(0);
742         return;
743       }
744
745       if (GAS_STAT_SUCCESS == stat)
746         ph.current_result->valid = GNUNET_YES;
747       else
748         ph.current_result->valid = GNUNET_NO;
749
750       ph.current_result->e_lp = GNUNET_TIME_absolute_get ();
751       ph.current_result->d_lp = GNUNET_TIME_absolute_get_difference (
752           ph.current_result->s_lp, ph.current_result->e_lp);
753       return;
754
755     case GAS_OP_SOLVE_MLP_MLP_START:
756       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
757           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
758           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
759       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
760       {
761         GNUNET_break(0);
762         return;
763       }
764
765       if (GAS_STAT_SUCCESS == stat)
766         ph.current_result->valid = GNUNET_YES;
767       else
768         ph.current_result->valid = GNUNET_NO;
769
770       ph.current_result->s_mlp = GNUNET_TIME_absolute_get ();
771       return;
772     case GAS_OP_SOLVE_MLP_MLP_STOP:
773       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
774           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
775           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
776       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
777       {
778         GNUNET_break(0);
779         return;
780       }
781
782       if (GAS_STAT_SUCCESS == stat)
783         ph.current_result->valid = GNUNET_YES;
784       else
785         ph.current_result->valid = GNUNET_NO;
786
787       ph.current_result->e_mlp = GNUNET_TIME_absolute_get ();
788       ph.current_result->d_mlp = GNUNET_TIME_absolute_get_difference (
789       ph.current_result->s_mlp, ph.current_result->e_mlp);
790       return;
791     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
792       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
793           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
794           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
795       return;
796     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
797       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
798           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
799           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
800       if (GAS_STAT_SUCCESS != stat)
801       {
802         GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
803             "Solver `%s' failed to update problem with %u peers and %u address!\n",
804             ph.ats_string, ph.current_p, ph.current_a);
805       }
806
807       return;
808     default:
809       break;
810     }
811 }
812
813 static void
814 write_gnuplot_script (char * data_fn, int iteration, int full)
815 {
816   struct GNUNET_DISK_FileHandle *f;
817   char * gfn;
818   char *data;
819   char *iter_text;
820   char *template;
821
822   /* Write header */
823   switch (ph.ats_mode) {
824     case MODE_PROPORTIONAL:
825       if (GNUNET_YES == full)
826         template = GNUPLOT_PROP_TEMPLATE;
827       else
828         template = GNUPLOT_PROP_UPDATE_TEMPLATE;
829       break;
830     case MODE_MLP:
831       if (GNUNET_YES == full)
832         template = GNUPLOT_MLP_TEMPLATE;
833       else
834         template = GNUPLOT_MLP_UPDATE_TEMPLATE;
835       break;
836     case MODE_RIL:
837       if (GNUNET_YES == full)
838         template = GNUPLOT_RIL_TEMPLATE;
839       else
840         template = GNUPLOT_RIL_UPDATE_TEMPLATE;
841       break;
842     default:
843       GNUNET_break (0);
844       return;
845   }
846   if (-1 == iteration)
847     GNUNET_asprintf (&iter_text, "%s_%u", "avg",ph.total_iterations);
848   else
849     GNUNET_asprintf (&iter_text, "%u", iteration);
850   if (GNUNET_YES == full)
851   {
852     GNUNET_asprintf (&gfn, "perf_%s_full_%s-%u_%u_%u.gnuplot",
853         ph.ats_string,
854         iter_text,
855         ph.N_peers_start,
856         ph.N_peers_end,
857         ph.N_address);
858   }
859   else
860   {
861     GNUNET_asprintf (&gfn, "perf_%s_updat_%s-%u_%u_%u.gnuplot",
862         ph.ats_string,
863         iter_text,
864         ph.N_peers_start,
865         ph.N_peers_end,
866         ph.N_address);
867   }
868   GNUNET_free (iter_text);
869
870   f = GNUNET_DISK_file_open (gfn,
871       GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
872       GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
873   if (NULL == f)
874   {
875     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot open gnuplot file `%s'\n", gfn);
876     GNUNET_free (gfn);
877     return;
878   }
879
880   if (GNUNET_SYSERR == GNUNET_DISK_file_write(f, template, strlen(template)))
881     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot write data to plot file `%s'\n", gfn);
882
883   data = NULL;
884   if (MODE_PROPORTIONAL == ph.ats_mode)
885   {
886     GNUNET_asprintf (&data, "plot '%s' using 1:%u with lines title 'Total time to solve'\n" \
887                            "pause -1",
888                            data_fn, 3);
889   }
890   else if (MODE_MLP == ph.ats_mode)
891   {
892     GNUNET_asprintf (&data, "plot '%s' using 1:%u with lines title 'Total time to solve',\\\n" \
893                             "'%s' using 1:%u with lines title 'Time to setup',\\\n"
894                             "'%s' using 1:%u with lines title 'Time to solve LP',\\\n"
895                             "'%s' using 1:%u with lines title 'Total time to solve MLP'\n" \
896                             "pause -1",
897                            data_fn, 3,
898                            data_fn, 4,
899                            data_fn, 5,
900                            data_fn, 6);
901   }
902   else if (MODE_RIL == ph.ats_mode)
903   {
904     GNUNET_asprintf (&data,
905                      "plot '%s' using 1:%u with lines title 'Total time to solve'\n" \
906                      "pause -1",
907                      data_fn, 3);
908   }
909
910   if ((NULL != data) &&
911       (GNUNET_SYSERR == GNUNET_DISK_file_write (f, data, strlen(data))))
912     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
913                 "Cannot write data to plot file `%s'\n",
914                 gfn);
915   GNUNET_free_non_null (data);
916
917   if (GNUNET_SYSERR == GNUNET_DISK_file_close(f))
918     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
919                 "Cannot close gnuplot file `%s'\n",
920                 gfn);
921   else
922     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
923                 "Data successfully written to plot file `%s'\n",
924                 gfn);
925   GNUNET_free (gfn);
926
927 }
928
929 /**
930  * Evaluate results for a specific iteration
931  *
932  * @param iteration the iteration to evaluate
933  */
934 static void
935 evaluate (int iteration)
936 {
937   struct GNUNET_DISK_FileHandle *f_full;
938   struct GNUNET_DISK_FileHandle *f_update;
939   char * data_fn_full;
940   char * data_fn_update;
941   char * data;
942   struct Result *cur;
943   struct Result *next;
944   struct Result *cur_res;
945   char * str_d_total;
946   char * str_d_setup;
947   char * str_d_lp;
948   char * str_d_mlp;
949   char * iter_text;
950
951   f_full = NULL;
952   f_update = NULL;
953
954   data_fn_full = NULL;
955
956   if (ph.create_plot)
957   {
958     if (-1 == iteration)
959       GNUNET_asprintf (&iter_text, "%s", "avg");
960     else
961       GNUNET_asprintf (&iter_text, "%u", iteration);
962     GNUNET_asprintf (&data_fn_full,
963                      "perf_%s_full_%s_%u_%u_%u.data",
964                      ph.ats_string,
965                      iter_text,
966                      ph.N_peers_start,
967                      ph.N_peers_end,
968                      ph.N_address);
969     GNUNET_free (iter_text);
970     f_full = GNUNET_DISK_file_open (data_fn_full,
971         GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
972         GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
973     if (NULL == f_full)
974     {
975       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
976                   "Cannot open gnuplot file `%s'\n",
977                   data_fn_full);
978       GNUNET_free (data_fn_full);
979       return;
980     }
981     data = "#peers;addresses;time total in us;#time setup in us;#time lp in us;#time mlp in us;\n";
982     if (GNUNET_SYSERR == GNUNET_DISK_file_write(f_full, data, strlen(data)))
983       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
984                   "Cannot write data to log file `%s'\n",
985                   data_fn_full);
986     write_gnuplot_script (data_fn_full, iteration, GNUNET_YES);
987   }
988
989   data_fn_update = NULL;
990   if ((ph.create_plot) && (GNUNET_YES == ph.measure_updates))
991   {
992     if (-1 == iteration)
993       GNUNET_asprintf (&iter_text, "%s", "avg");
994     else
995       GNUNET_asprintf (&iter_text, "%u", iteration);
996     GNUNET_asprintf (&data_fn_update, "perf_%s_update_i%u_%u_%u_%u.data",
997         ph.ats_string,
998         iter_text,
999         ph.N_peers_start,
1000         ph.N_peers_end,
1001         ph.N_address);
1002     GNUNET_free (iter_text);
1003     f_update = GNUNET_DISK_file_open (data_fn_update,
1004         GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
1005         GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
1006     if (NULL == f_update)
1007     {
1008       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1009                   "Cannot open gnuplot file `%s'\n", data_fn_update);
1010       GNUNET_free (data_fn_update);
1011       if (NULL != f_full)
1012         GNUNET_DISK_file_close (f_full);
1013       GNUNET_free (data_fn_full);
1014       return;
1015     }
1016     data = "#peers;addresses;time total in us;#time setup in us;#time lp in us;#time mlp in us;\n";
1017     if (GNUNET_SYSERR == GNUNET_DISK_file_write (f_update, data, strlen(data)))
1018       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1019                   "Cannot write data to log file `%s'\n",
1020                   data_fn_update);
1021     write_gnuplot_script (data_fn_update, iteration, GNUNET_NO);
1022   }
1023
1024   next = ph.iterations_results[ph.current_iteration -1].result_head;
1025   while (NULL != (cur = next))
1026   {
1027     next = cur->next;
1028     str_d_total = NULL;
1029     str_d_setup = NULL;
1030     str_d_lp = NULL;
1031     str_d_mlp = NULL;
1032
1033     /* Print log */
1034     if (GNUNET_NO == cur->update)
1035     {
1036       cur_res = &ph.averaged_full_result[cur->peers - ph.N_peers_start];
1037     }
1038     else
1039     {
1040       cur_res = &ph.averaged_update_result[cur->peers - ph.N_peers_start];
1041     }
1042
1043     cur_res->peers = cur->peers;
1044     cur_res->addresses = cur->addresses;
1045     cur_res->update = cur->update;
1046
1047     if (GNUNET_NO == cur->valid)
1048     {
1049       fprintf (stderr,
1050                "Total time to solve %s for %u peers %u addresses: %s\n",
1051                (GNUNET_YES == cur->update) ? "updated" : "full",
1052                cur->peers, cur->addresses, "Failed to solve!");
1053       continue;
1054     }
1055     else
1056       cur_res->valid ++;
1057
1058     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_total.rel_value_us)
1059     {
1060       if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == cur_res->d_total.rel_value_us)
1061         cur_res->d_total.rel_value_us = 0;
1062       if (GNUNET_YES == cur->valid)
1063         cur_res->d_total.rel_value_us += cur->d_total.rel_value_us;
1064       fprintf (stderr,
1065          "Total time to solve %s for %u peers %u addresses: %llu us\n",
1066          (GNUNET_YES == cur->update) ? "updated" : "full",
1067          cur->peers, cur->addresses,
1068          (unsigned long long) cur->d_total.rel_value_us);
1069       GNUNET_asprintf(&str_d_total,
1070          "%llu", (unsigned long long) cur->d_total.rel_value_us);
1071     }
1072     else
1073       GNUNET_asprintf(&str_d_total, "-1");
1074     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_setup.rel_value_us)
1075     {
1076       if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == cur_res->d_setup.rel_value_us)
1077         cur_res->d_setup.rel_value_us = 0;
1078       if (GNUNET_YES == cur->valid)
1079         cur_res->d_setup.rel_value_us += cur->d_setup.rel_value_us;
1080       fprintf (stderr, "Total time to setup %s %u peers %u addresses: %llu us\n",
1081           (GNUNET_YES == cur->update) ? "updated" : "full",
1082           cur->peers, cur->addresses,
1083           (unsigned long long) cur->d_setup.rel_value_us);
1084       GNUNET_asprintf(&str_d_setup, "%llu",
1085           (unsigned long long )cur->d_setup.rel_value_us);
1086     }
1087     else
1088       GNUNET_asprintf(&str_d_setup, "-1");
1089
1090     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_lp.rel_value_us)
1091     {
1092       if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == cur_res->d_lp.rel_value_us)
1093         cur_res->d_lp.rel_value_us = 0;
1094       if (GNUNET_YES == cur->valid)
1095         cur_res->d_lp.rel_value_us += cur->d_lp.rel_value_us;
1096       fprintf (stderr,
1097          "Total time to solve %s LP for %u peers %u addresses: %llu us\n",
1098          (GNUNET_YES == cur->update) ? "updated" : "full",
1099          cur->peers,
1100          cur->addresses,
1101          (unsigned long long )cur->d_lp.rel_value_us);
1102       GNUNET_asprintf (&str_d_lp,
1103           "%llu", (unsigned long long )cur->d_lp.rel_value_us);
1104     }
1105     else
1106       GNUNET_asprintf (&str_d_lp, "-1");
1107
1108     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_mlp.rel_value_us)
1109     {
1110       if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == cur_res->d_mlp.rel_value_us)
1111         cur_res->d_mlp.rel_value_us = 0;
1112       if (GNUNET_YES == cur->valid)
1113         cur_res->d_mlp.rel_value_us += cur->d_mlp.rel_value_us;
1114
1115       fprintf (stderr, "Total time to solve %s MLP for %u peers %u addresses: %llu us\n",
1116           (GNUNET_YES == cur->update) ? "updated" : "full",
1117           cur->peers, cur->addresses,
1118           (unsigned long long )cur->d_mlp.rel_value_us);
1119       GNUNET_asprintf (&str_d_mlp,
1120           "%llu", (unsigned long long )cur->d_mlp.rel_value_us);
1121     }
1122     else
1123       GNUNET_asprintf (&str_d_mlp, "-1");
1124
1125     data = NULL;
1126     if (GNUNET_YES == ph.create_plot)
1127     {
1128
1129       GNUNET_asprintf (&data,
1130                        "%u;%u;%s;%s;%s;%s\n",
1131                        cur->peers, cur->addresses,
1132                        str_d_total,
1133                        str_d_setup,
1134                        str_d_lp,
1135                        str_d_mlp);
1136       if (cur->update == GNUNET_NO)
1137       {
1138         if (GNUNET_SYSERR == GNUNET_DISK_file_write (f_full, data, strlen(data)))
1139           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1140                       "Cannot write data to log file `%s'\n",
1141                       data_fn_full);
1142       }
1143       if ((cur->update == GNUNET_YES) && (NULL != f_update))
1144       {
1145         if (GNUNET_SYSERR == GNUNET_DISK_file_write (f_update, data, strlen(data)))
1146           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1147                       "Cannot write data to log file `%s'\n",
1148                       data_fn_update);
1149       }
1150       GNUNET_free (data);
1151     }
1152     GNUNET_free_non_null (str_d_total);
1153     GNUNET_free_non_null (str_d_setup);
1154     GNUNET_free_non_null (str_d_lp);
1155     GNUNET_free_non_null (str_d_mlp);
1156
1157     GNUNET_CONTAINER_DLL_remove (ph.iterations_results[ph.current_iteration-1].result_head,
1158         ph.iterations_results[ph.current_iteration-1].result_tail, cur);
1159     GNUNET_free (cur);
1160   }
1161
1162   if ((NULL != f_full) && (GNUNET_SYSERR == GNUNET_DISK_file_close (f_full)))
1163     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Cannot close log file `%s'\n",
1164         data_fn_full);
1165   GNUNET_free_non_null (data_fn_full);
1166
1167   if ((NULL != f_update) && (GNUNET_SYSERR == GNUNET_DISK_file_close (f_update)))
1168     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Cannot close log file `%s'\n",
1169         data_fn_update);
1170   GNUNET_free_non_null (data_fn_update);
1171 }
1172
1173 /**
1174  * Evaluate average results for all iterations
1175  */
1176 static void
1177 evaluate_average (void)
1178 {
1179   int c_o;
1180   int c_i;
1181
1182   struct GNUNET_DISK_FileHandle *f_full;
1183   struct GNUNET_DISK_FileHandle *f_update;
1184   struct Result *cur;
1185   char * data_fn_full;
1186   char * data_fn_update;
1187   char * data;
1188   char * str_d_total;
1189   char * str_d_setup;
1190   char * str_d_lp;
1191   char * str_d_mlp;
1192
1193   f_full = NULL;
1194   f_update = NULL;
1195
1196   data_fn_full = NULL;
1197
1198   if (ph.create_plot)
1199   {
1200     GNUNET_asprintf (&data_fn_full,
1201                      "perf_%s_full_avg_%u-%u_%u_%u.data",
1202                      ph.ats_string,
1203                      ph.total_iterations,
1204                      ph.N_peers_start,
1205                      ph.N_peers_end,
1206                      ph.N_address);
1207     f_full = GNUNET_DISK_file_open (data_fn_full,
1208         GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
1209         GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
1210     if (NULL == f_full)
1211     {
1212       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1213                   "Cannot open gnuplot file `%s'\n",
1214                   data_fn_full);
1215       GNUNET_free (data_fn_full);
1216       return;
1217     }
1218     data = "#peers;addresses;time total in us;#time setup in us;#time lp in us;#time mlp in us;\n";
1219     if (GNUNET_SYSERR == GNUNET_DISK_file_write(f_full, data, strlen(data)))
1220       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1221                   "Cannot write data to log file `%s'\n",
1222                   data_fn_full);
1223     write_gnuplot_script (data_fn_full, -1, GNUNET_YES);
1224   }
1225
1226   data_fn_update = NULL;
1227   if ((ph.create_plot) && (GNUNET_YES == ph.measure_updates))
1228   {
1229     GNUNET_asprintf (&data_fn_update, "perf_%s_update_avg_%u-%u_%u_%u.data",
1230         ph.ats_string,
1231         ph.total_iterations,
1232         ph.N_peers_start,
1233         ph.N_peers_end,
1234         ph.N_address);
1235     f_update = GNUNET_DISK_file_open (data_fn_update,
1236         GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
1237         GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
1238     if (NULL == f_update)
1239     {
1240       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1241                   "Cannot open gnuplot file `%s'\n", data_fn_update);
1242       GNUNET_free (data_fn_update);
1243       if (NULL != f_full)
1244         GNUNET_DISK_file_close (f_full);
1245       GNUNET_free (data_fn_full);
1246       return;
1247     }
1248     data = "#peers;addresses;time total in us;#time setup in us;#time lp in us;#time mlp in us;\n";
1249     if (GNUNET_SYSERR == GNUNET_DISK_file_write (f_update, data, strlen(data)))
1250       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1251                   "Cannot write data to log file `%s'\n",
1252                   data_fn_update);
1253     write_gnuplot_script (data_fn_update, -1, GNUNET_NO);
1254   }
1255
1256   for (c_o = 0; c_o < 2; c_o++)
1257   {
1258     if (0 == c_o)
1259       fprintf (stderr,
1260           "Duration for a full solution averaged over %i iterations\n",
1261           ph.total_iterations);
1262     if (1 == c_o)
1263       fprintf (stderr,
1264           "Duration for a full solution averaged over %i iterations\n",
1265           ph.total_iterations);
1266
1267     for (c_i = 0; c_i <= ph.N_peers_end - ph.N_peers_start; c_i++)
1268     {
1269       if (0 == c_o)
1270       {
1271         cur = &ph.averaged_full_result[c_i];
1272       }
1273       else if ((GNUNET_YES == ph.measure_updates) && (1 == c_o))
1274       {
1275         cur = &ph.averaged_update_result[c_i];
1276       }
1277       else
1278          break;
1279
1280       if (0 == cur->peers)
1281         continue;
1282
1283       str_d_total = NULL;
1284       str_d_setup = NULL;
1285       str_d_lp = NULL;
1286       str_d_mlp = NULL;
1287
1288       if (0 >= cur->valid)
1289       {
1290         fprintf (stderr,
1291            "No valid results for %s for %u peers %u addresses!\n",
1292            (GNUNET_YES == cur->update) ? "updated" : "full",
1293                cur->peers, cur->addresses);
1294         continue;
1295       }
1296
1297       if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_total.rel_value_us)
1298       {
1299         fprintf (stderr,
1300            "Average total time from %u iterations to solve %s for %u peers %u addresses: %llu us\n",
1301            cur->valid,
1302            (GNUNET_YES == cur->update) ? "updated" : "full",
1303                cur->peers, cur->addresses,
1304            (unsigned long long) cur->d_total.rel_value_us / cur->valid);
1305         GNUNET_asprintf(&str_d_total, "%llu",
1306            (unsigned long long) cur->d_total.rel_value_us / cur->valid);
1307       }
1308       else
1309         GNUNET_asprintf (&str_d_total, "-1");
1310
1311       if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_setup.rel_value_us)
1312       {
1313         fprintf (stderr,
1314            "Average total time from %u iterations to setup for %u peers %u addresses: %llu us\n",
1315            cur->valid, cur->peers, cur->addresses,
1316            (unsigned long long) cur->d_setup.rel_value_us / cur->valid);
1317         GNUNET_asprintf(&str_d_setup, "%llu",
1318            (unsigned long long) cur->d_setup.rel_value_us / cur->valid);
1319
1320       }
1321       else
1322         GNUNET_asprintf (&str_d_setup, "-1");
1323
1324       if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_lp.rel_value_us)
1325       {
1326         fprintf (stderr,
1327            "Average total time from %u iterations to solve lp %s for %u peers %u addresses: %llu us\n",
1328            cur->valid,
1329            (GNUNET_YES == cur->update) ? "updated" : "full",
1330            cur->peers, cur->addresses,
1331            (unsigned long long) cur->d_lp.rel_value_us / cur->valid);
1332         GNUNET_asprintf(&str_d_lp, "%llu",
1333            (unsigned long long) cur->d_lp.rel_value_us / ph.total_iterations);
1334       }
1335       else
1336         GNUNET_asprintf (&str_d_lp, "-1");
1337
1338       if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_mlp.rel_value_us)
1339       {
1340         fprintf (stderr,
1341            "Average total time from %u iterations to solve mlp %s for %u peers %u addresses: %llu us\n",
1342            cur->valid,
1343            (GNUNET_YES == cur->update) ? "updated" : "full",
1344                cur->peers, cur->addresses,
1345            (unsigned long long) cur->d_mlp.rel_value_us / cur->valid);
1346         GNUNET_asprintf(&str_d_mlp, "%llu",
1347            (unsigned long long) cur->d_mlp.rel_value_us / cur->valid);
1348       }
1349       else
1350         GNUNET_asprintf (&str_d_mlp, "-1");
1351
1352       data = NULL;
1353       if (GNUNET_YES == ph.create_plot)
1354       {
1355         GNUNET_asprintf (&data,
1356                          "%u;%u;%s;%s;%s;%s\n",
1357                          cur->peers, cur->addresses,
1358                          str_d_total,
1359                          str_d_setup,
1360                          str_d_lp,
1361                          str_d_mlp);
1362         if (cur->update == GNUNET_NO)
1363         {
1364           if (GNUNET_SYSERR == GNUNET_DISK_file_write (f_full, data, strlen(data)))
1365             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1366                         "Cannot write data to log file `%s'\n",
1367                         data_fn_full);
1368         }
1369         if ((cur->update == GNUNET_YES) && (NULL != f_update))
1370         {
1371           if (GNUNET_SYSERR == GNUNET_DISK_file_write (f_update, data, strlen(data)))
1372             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1373                         "Cannot write data to log file `%s'\n",
1374                         data_fn_update);
1375         }
1376         GNUNET_free (data);
1377       }
1378
1379       GNUNET_free_non_null (str_d_total);
1380       GNUNET_free_non_null (str_d_setup);
1381       GNUNET_free_non_null (str_d_lp);
1382       GNUNET_free_non_null (str_d_mlp);
1383     }
1384   }
1385
1386   if ((NULL != f_full) && (GNUNET_SYSERR == GNUNET_DISK_file_close (f_full)))
1387     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Cannot close log file `%s'\n",
1388         data_fn_full);
1389   GNUNET_free_non_null (data_fn_full);
1390
1391   if ((NULL != f_update) && (GNUNET_SYSERR == GNUNET_DISK_file_close (f_update)))
1392     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Cannot close log file `%s'\n",
1393         data_fn_update);
1394   GNUNET_free_non_null (data_fn_update);
1395 }
1396
1397 /**
1398  * Run a performance iteration
1399  */
1400
1401 static void
1402 perf_run_iteration (void)
1403 {
1404   struct ATS_Address *cur;
1405   struct ATS_Address *next;
1406   int cp;
1407   int ca;
1408   int count_p = ph.N_peers_end;
1409   int count_a = ph.N_address;
1410   struct ATS_Address * cur_addr;
1411
1412
1413   ph.peers = GNUNET_malloc ((count_p) * sizeof (struct PerfPeer));
1414   for (cp = 0; cp < count_p; cp++)
1415     perf_create_peer (cp);
1416   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1417       "Iteration %u of %u, added %u peers\n", ph.current_iteration, ph.total_iterations, cp);
1418
1419   for (cp = 0; cp < count_p; cp++)
1420   {
1421     if (GNUNET_NO == ph.bulk_running)
1422     {
1423       ph.bulk_running = GNUNET_YES;
1424       ph.env.sf.s_bulk_start (ph.solver);
1425     }
1426     ph.current_p = cp + 1;
1427     for (ca = 0; ca < count_a; ca++)
1428     {
1429       cur_addr = perf_create_address (cp, ca);
1430       /* Add address */
1431       ph.env.sf.s_add (ph.solver, cur_addr, GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_ATS_NetworkTypeCount));
1432       ph.current_a = ca + 1;
1433       perf_address_initial_update (ph.solver, ph.addresses, cur_addr);
1434       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1435           "Adding address for peer %u address %u\n", cp, ca);
1436     }
1437     /* Notify solver about request */
1438     ph.env.sf.s_get (ph.solver, &ph.peers[cp].id);
1439
1440     if (cp + 1 >= ph.N_peers_start)
1441     {
1442       /* Disable bulk to solve the problem */
1443       if (GNUNET_YES == ph.bulk_running)
1444       {
1445         ph.expecting_solution = GNUNET_YES;
1446         ph.bulk_running = GNUNET_NO;
1447         ph.env.sf.s_bulk_stop (ph.solver);
1448       }
1449       else
1450         GNUNET_break (0);
1451
1452       /* Problem is solved by the solver here due to unlocking */
1453       ph.expecting_solution = GNUNET_NO;
1454
1455       /* Update the problem */
1456       if ((0 < ph.opt_update_percent) && (GNUNET_YES == ph.measure_updates))
1457       {
1458         /* Update */
1459         GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1460             "Updating problem with %u peers and %u addresses\n", cp + 1, ca);
1461
1462         ph.expecting_solution = GNUNET_YES;
1463         ph.performed_update = GNUNET_YES;
1464         if (GNUNET_NO == ph.bulk_running)
1465         {
1466           ph.bulk_running = GNUNET_YES;
1467           ph.env.sf.s_bulk_start (ph.solver);
1468         }
1469         perf_update_all_addresses (cp + 1, ca, ph.opt_update_percent);
1470         ph.bulk_running = GNUNET_NO;
1471         ph.env.sf.s_bulk_stop (ph.solver);
1472         /* Problem is solved by the solver here due to unlocking */
1473         ph.performed_update = GNUNET_NO;
1474         ph.expecting_solution = GNUNET_NO;
1475       }
1476       GNUNET_assert (GNUNET_NO == ph.bulk_running);
1477     }
1478   }
1479
1480   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1481       "Done, cleaning up addresses\n");
1482   if (GNUNET_NO == ph.bulk_running)
1483   {
1484     ph.env.sf.s_bulk_start (ph.solver);
1485     ph.bulk_running = GNUNET_YES;
1486   }
1487
1488   for (cp = 0; cp < count_p; cp++)
1489   {
1490     for (cur = ph.peers[cp].head; cur != NULL ; cur = next)
1491     {
1492       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1493           "Deleting addresses for peer %u\n", cp);
1494       GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (ph.addresses,
1495           &ph.peers[cp].id, cur));
1496       ph.env.sf.s_del (ph.solver, cur, GNUNET_NO);
1497       next = cur->next;
1498       GNUNET_CONTAINER_DLL_remove(ph.peers[cp].head, ph.peers[cp].tail, cur);
1499       GNUNET_free(cur);
1500     }
1501   }
1502
1503   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1504       "Iteration done\n");
1505   GNUNET_free(ph.peers);
1506 }
1507
1508
1509 static void
1510 run (void *cls, char * const *args, const char *cfgfile,
1511     const struct GNUNET_CONFIGURATION_Handle *cfg)
1512 {
1513   GNUNET_log_setup ("perf-ats-solver", "WARNING", NULL);
1514   char *sep;
1515   char *src_filename = GNUNET_strdup (__FILE__);
1516   char *test_filename = cls;
1517   char *solver;
1518   char *plugin;
1519   struct GNUNET_CONFIGURATION_Handle *solver_cfg;
1520   unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1521   unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1522   int c;
1523
1524   /* Extract test name */
1525   if (NULL == (sep  = (strstr (src_filename,".c"))))
1526   {
1527     GNUNET_free (src_filename);
1528     GNUNET_break (0);
1529     ret = 1;
1530     return ;
1531   }
1532   sep[0] = '\0';
1533
1534   if (NULL != (sep = strstr (test_filename, ".exe")))
1535     sep[0] = '\0';
1536
1537   if (NULL == (solver = strstr (test_filename, src_filename)))
1538   {
1539     GNUNET_free (src_filename);
1540     GNUNET_break (0);
1541     ret = 1;
1542     return ;
1543   }
1544   solver += strlen (src_filename) +1;
1545
1546   if (0 == strcmp(solver, "proportional"))
1547   {
1548     ph.ats_mode = MODE_PROPORTIONAL;
1549     ph.ats_string = "proportional";
1550   }
1551   else if (0 == strcmp(solver, "mlp"))
1552   {
1553     ph.ats_mode = MODE_MLP;
1554     ph.ats_string = "mlp";
1555   }
1556   else if ((0 == strcmp(solver, "ril")))
1557   {
1558     ph.ats_mode = MODE_RIL;
1559     ph.ats_string = "ril";
1560   }
1561   else
1562   {
1563     GNUNET_free (src_filename);
1564     GNUNET_break (0);
1565     ret = 1;
1566     return ;
1567   }
1568   GNUNET_free (src_filename);
1569
1570   /* Calculcate peers */
1571   if ((0 == ph.N_peers_start) && (0 == ph.N_peers_end))
1572   {
1573     ph.N_peers_start = DEFAULT_PEERS_START;
1574     ph.N_peers_end = DEFAULT_PEERS_END;
1575   }
1576   if (0 == ph.N_address)
1577     ph.N_address = DEFAULT_ADDRESSES;
1578
1579   if (ph.N_peers_start != ph.N_peers_end)
1580     fprintf (stderr, "Benchmarking solver `%s' with %u to %u peers and %u addresses in %u iterations\n",
1581         ph.ats_string, ph.N_peers_start, ph.N_peers_end, ph.N_address, ph.total_iterations);
1582   else
1583     fprintf (stderr, "Benchmarking solver `%s' with %u peers and %u addresses in %u iterations\n",
1584         ph.ats_string, ph.N_peers_end, ph.N_address, ph.total_iterations);
1585
1586   if (0 == ph.opt_update_percent)
1587     ph.opt_update_percent = DEFAULT_UPDATE_PERCENTAGE;
1588
1589   /* Load quotas */
1590   solver_cfg = GNUNET_CONFIGURATION_create();
1591   if ((NULL == solver_cfg) || (GNUNET_SYSERR == (GNUNET_CONFIGURATION_load ( solver_cfg, "perf_ats_solver.conf"))))
1592   {
1593     GNUNET_break(0);
1594     end_now (1);
1595     return;
1596   }
1597   if (GNUNET_ATS_NetworkTypeCount != load_quotas (solver_cfg,
1598       quotas_out, quotas_in, GNUNET_ATS_NetworkTypeCount))
1599   {
1600     GNUNET_break(0);
1601     end_now (1);
1602     return;
1603   }
1604
1605   /* Create array of DLL to store results for iterations */
1606   ph.iterations_results = GNUNET_malloc (sizeof (struct Iteration) * ph.total_iterations);
1607   ph.averaged_full_result = GNUNET_malloc (sizeof (struct Result) * ((ph.N_peers_end + 1) - ph.N_peers_start));
1608   for (c = 0; c <= ph.N_peers_end - ph.N_peers_start; c++)
1609   {
1610     ph.averaged_full_result[c].d_setup = GNUNET_TIME_UNIT_FOREVER_REL;
1611     ph.averaged_full_result[c].d_total = GNUNET_TIME_UNIT_FOREVER_REL;
1612     ph.averaged_full_result[c].d_lp = GNUNET_TIME_UNIT_FOREVER_REL;
1613     ph.averaged_full_result[c].d_mlp = GNUNET_TIME_UNIT_FOREVER_REL;
1614   }
1615   ph.averaged_update_result = GNUNET_malloc (sizeof (struct Result) * ((ph.N_peers_end + 1) - ph.N_peers_start));
1616   for (c = 0; c <= ph.N_peers_end - ph.N_peers_start; c++)
1617   {
1618     ph.averaged_update_result[c].d_setup = GNUNET_TIME_UNIT_FOREVER_REL;
1619     ph.averaged_update_result[c].d_total = GNUNET_TIME_UNIT_FOREVER_REL;
1620     ph.averaged_update_result[c].d_lp = GNUNET_TIME_UNIT_FOREVER_REL;
1621     ph.averaged_update_result[c].d_mlp = GNUNET_TIME_UNIT_FOREVER_REL;
1622   }
1623
1624   /* Load solver */
1625   ph.env.cfg = solver_cfg;
1626   ph.stat = GNUNET_STATISTICS_create ("ats", cfg);
1627   ph.env.stats = ph.stat;
1628   ph.addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1629   ph.env.addresses = ph.addresses;
1630   ph.env.bandwidth_changed_cb = bandwidth_changed_cb;
1631   ph.env.get_preferences = &get_preferences_cb;
1632   ph.env.get_property = &get_property_cb;
1633   ph.env.network_count = GNUNET_ATS_NetworkTypeCount;
1634   ph.env.info_cb = &solver_info_cb;
1635   ph.env.info_cb_cls = NULL;
1636
1637   int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1638   for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1639   {
1640     ph.env.networks[c] = networks[c];
1641     ph.env.out_quota[c] = quotas_out[c];
1642     ph.env.in_quota[c] = quotas_in[c];
1643     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Loading network quotas: `%s' %llu %llu \n",
1644         GNUNET_ATS_print_network_type(ph.env.networks[c]),
1645         ph.env.out_quota[c],
1646         ph.env.in_quota[c]);
1647   }
1648   GAS_normalization_start (NULL, NULL, &normalized_property_changed_cb, NULL );
1649
1650   GNUNET_asprintf (&plugin, "libgnunet_plugin_ats_%s", ph.ats_string);
1651   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initializing solver `%s'\n"), ph.ats_string);
1652   if  (NULL == (ph.solver = GNUNET_PLUGIN_load (plugin, &ph.env)))
1653   {
1654     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver `%s'!\n"), plugin);
1655     ret = 1;
1656     return;
1657   }
1658
1659   /* Do the benchmark */
1660   for (ph.current_iteration = 1; ph.current_iteration <= ph.total_iterations; ph.current_iteration++)
1661   {
1662     perf_run_iteration ();
1663     evaluate (ph.current_iteration);
1664   }
1665   evaluate_average ();
1666
1667   /* Unload solver*/
1668   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Unloading solver `%s'\n"), ph.ats_string);
1669   GNUNET_PLUGIN_unload (plugin, ph.solver);
1670   GNUNET_free (plugin);
1671   GNUNET_free (ph.iterations_results);
1672   GNUNET_free (ph.averaged_full_result);
1673   GNUNET_free (ph.averaged_update_result);
1674   GNUNET_CONFIGURATION_destroy (solver_cfg);
1675   GNUNET_STATISTICS_destroy (ph.stat, GNUNET_NO);
1676   ph.solver = NULL;
1677 }
1678
1679 /**
1680  * Main function of the benchmark
1681  *
1682  * @param argc argument count
1683  * @param argv argument values
1684  */
1685 int
1686 main (int argc, char *argv[])
1687 {
1688   /* extract command line arguments */
1689   ph.opt_update_percent = 0;
1690   ph.N_peers_start = 0;
1691   ph.N_peers_end = 0;
1692   ph.N_address = 0;
1693   ph.ats_string = NULL;
1694   ph.create_plot = GNUNET_NO;
1695   ph.measure_updates = GNUNET_NO;
1696   ph.total_iterations = 1;
1697
1698   static struct GNUNET_GETOPT_CommandLineOption options[] = {
1699       { 'a', "addresses", NULL,
1700           gettext_noop ("addresses to use"),
1701           1, &GNUNET_GETOPT_set_uint, &ph.N_address },
1702       { 's', "start", NULL,
1703           gettext_noop ("start with peer"),
1704           1, &GNUNET_GETOPT_set_uint, &ph.N_peers_start },
1705       { 'e', "end", NULL,
1706           gettext_noop ("end with peer"),
1707           1, &GNUNET_GETOPT_set_uint, &ph.N_peers_end },
1708       { 'i', "iterations", NULL,
1709           gettext_noop ("number of iterations used for averaging (default: 1)"),
1710           1, &GNUNET_GETOPT_set_uint, &ph.total_iterations },
1711       { 'p', "percentage", NULL,
1712           gettext_noop ("update a fix percentage of addresses"),
1713           1, &GNUNET_GETOPT_set_uint, &ph.opt_update_percent },
1714       { 'g', "gnuplot", NULL,
1715           gettext_noop ("create GNUplot file"),
1716           0, &GNUNET_GETOPT_set_one, &ph.create_plot},
1717       { 'u', "update", NULL,
1718           gettext_noop ("measure updates"),
1719           0, &GNUNET_GETOPT_set_one, &ph.measure_updates},
1720       GNUNET_GETOPT_OPTION_END
1721   };
1722
1723   GNUNET_PROGRAM_run (argc, argv, argv[0], NULL, options, &run, argv[0]);
1724   return ret;
1725 }
1726
1727 /* end of file perf_ats_solver.c */