Merge branch 'master' of gnunet.org:gnunet
[oweals/gnunet.git] / src / ats / perf_ats_solver.c
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2010,2011 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, 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_plugins.h"
31 #include "gnunet-service-ats_normalization.h"
32 #include "gnunet-service-ats_preferences.h"
33 #include "gnunet_ats_service.h"
34 #include "gnunet_ats_plugin.h"
35 #include "test_ats_api_common.h"
36
37 #define DEFAULT_UPDATE_PERCENTAGE       20
38 #define DEFAULT_PEERS_START     10
39 #define DEFAULT_PEERS_END       10
40 #define DEFAULT_ADDRESSES       10
41 #define DEFAULT_ATS_COUNT       2
42
43
44 /**
45  * Handle for statistics.
46  */
47 struct GNUNET_STATISTICS_Handle *GSA_stats;
48
49 /**
50  * Handle for ATS address component
51  */
52 struct PerfHandle
53 {
54   /**
55    * Performance peers
56    */
57   struct PerfPeer *peers;
58
59   /**
60    *  Solver handle
61    */
62   struct GNUNET_ATS_SolverFunctions *sf;
63
64   /**
65    * Statistics stat;
66    */
67   struct GNUNET_STATISTICS_Handle *stat;
68
69   /**
70    * A multihashmap to store all addresses
71    */
72   struct GNUNET_CONTAINER_MultiPeerMap *addresses;
73
74   /**
75    * Solver functions
76    * */
77   struct GNUNET_ATS_PluginEnvironment env;
78
79   /**
80    * Array for results for each iteration with length iterations
81    */
82   struct Iteration *iterations_results;
83
84   /**
85    * The current result
86    */
87   struct Result *current_result;
88
89   /**
90    * Current number of peers benchmarked
91    */
92   int current_p;
93
94   /**
95    * Current number of addresses benchmarked
96    */
97   int current_a;
98
99   /**
100    * Solver description as string
101    */
102   char *ats_string;
103
104   /**
105    * Configured ATS solver
106    */
107   int ats_mode;
108
109   /**
110    * #peers to start benchmarking with
111    */
112   int N_peers_start;
113
114   /**
115    * #peers to end benchmarking with
116    */
117   int N_peers_end;
118
119   /**
120    * #addresses to benchmarking with
121    */
122   int N_address;
123
124   /**
125    * Percentage of peers to update
126    */
127   int opt_update_percent;
128
129   /**
130    * Create gnuplot file
131    */
132   int create_datafile;
133
134   /**
135    * Measure updates
136    */
137   int measure_updates;
138
139   /**
140    * Number of iterations
141    */
142   int total_iterations;
143
144   /**
145    * Current iteration
146    */
147   int current_iteration;
148
149   /**
150    * Is a bulk operation running?
151    */
152   int bulk_running;
153
154   /**
155    * Is a bulk operation running?
156    */
157   int expecting_solution;
158
159   /**
160    * Was the problem just updates?
161    */
162   int performed_update;
163 };
164
165 /**
166  * Data structure to store results for a single iteration
167  */
168 struct Iteration
169 {
170   struct Result **results_array;
171
172   struct Result **update_results_array;
173 };
174
175
176 /**
177  * Result for a solver calculcation
178  */
179 struct Result
180 {
181   /**
182    * Previous element in the linked list
183    */
184   struct Result *prev;
185
186   /**
187    * Next element in the linked list
188    */
189   struct Result *next;
190
191   /**
192    * Number of peers this solution included
193    */
194   int peers;
195
196   /**
197    * Number of addresses per peer this solution included
198    */
199   int addresses;
200
201   /**
202    * Is this an update or a full solution
203    */
204   int update;
205
206   /**
207    * Was the solution valid or did the solver fail
208    */
209   int valid;
210
211   /**
212    * Result of the solver
213    */
214   enum GAS_Solver_Additional_Information info;
215
216   /**
217    * Duration of setting up the problem in the solver
218    */
219   struct GNUNET_TIME_Relative d_setup_full;
220
221   /**
222    * Duration of solving the LP problem in the solver
223    * MLP solver only
224    */
225   struct GNUNET_TIME_Relative d_lp_full;
226
227   /**
228    * Duration of solving the MLP problem in the solver
229    * MLP solver only
230    */
231   struct GNUNET_TIME_Relative d_mlp_full;
232
233   /**
234    * Duration of solving whole problem in the solver
235    */
236   struct GNUNET_TIME_Relative d_total_full;
237
238   /**
239    * Start time of setting up the problem in the solver
240    */
241   struct GNUNET_TIME_Absolute s_setup;
242
243   /**
244    * Start time of solving the LP problem in the solver
245    * MLP solver only
246    */
247   struct GNUNET_TIME_Absolute s_lp;
248
249   /**
250    * Start time of solving the MLP problem in the solver
251    * MLP solver only
252    */
253   struct GNUNET_TIME_Absolute s_mlp;
254
255   /**
256    * Start time of solving whole problem in the solver
257    */
258   struct GNUNET_TIME_Absolute s_total;
259
260   /**
261    * End time of setting up the problem in the solver
262    */
263   struct GNUNET_TIME_Absolute e_setup;
264
265   /**
266    * End time of solving the LP problem in the solver
267    * MLP solver only
268    */
269   struct GNUNET_TIME_Absolute e_lp;
270
271   /**
272    * End time of solving the MLP problem in the solver
273    * MLP solver only
274    */
275   struct GNUNET_TIME_Absolute e_mlp;
276
277   /**
278    * End time of solving whole problem in the solver
279    */
280   struct GNUNET_TIME_Absolute e_total;
281 };
282
283 /**
284  * Peer used for the benchmarking
285  */
286 struct PerfPeer
287 {
288   /**
289    * Peer identitity
290    */
291   struct GNUNET_PeerIdentity id;
292
293   /**
294    * Head of linked list of addresses used with this peer
295    */
296   struct ATS_Address *head;
297
298   /**
299    * Head of linked list of addresses used with this peer
300    */
301   struct ATS_Address *tail;
302 };
303
304
305 /**
306  * ATS performance handle
307  */
308 static struct PerfHandle ph;
309
310 /**
311  * Return value
312  */
313 static int ret;
314
315
316 /**
317  * Do shutdown
318  */
319 static void
320 end_now (int res)
321 {
322   if (NULL != ph.stat)
323   {
324     GNUNET_STATISTICS_destroy (ph.stat, GNUNET_NO);
325     ph.stat = NULL;
326   }
327
328   GNUNET_free_non_null (ph.peers);
329   GNUNET_free_non_null (ph.iterations_results);
330
331   GAS_normalization_stop ();
332   GAS_preference_done ();
333   ret = res;
334 }
335
336
337 /**
338  * Create a peer used for benchmarking
339  *
340  * @param cp the number of the peer
341  */
342 static void
343 perf_create_peer (int cp)
344 {
345
346   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
347       &ph.peers[cp].id, sizeof (struct GNUNET_PeerIdentity));
348   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating peer #%u: %s \n", cp,
349       GNUNET_i2s (&ph.peers[cp].id));
350 }
351
352
353 /**
354  * Perform an update for an address
355  *
356  * @param cur the address to update
357  */
358 static void
359 perf_update_address (struct ATS_Address *cur)
360 {
361   int r_type;
362   int abs_val;
363   double rel_val;
364
365   r_type = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 2);
366   switch (r_type)
367   {
368   case 0:
369     abs_val = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
370     rel_val = (100 + (double) abs_val) / 100;
371
372     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
373         "Updating peer `%s' address %p type %s abs val %u rel val %.3f\n",
374         GNUNET_i2s (&cur->peer), cur,
375         "GNUNET_ATS_QUALITY_NET_DELAY",
376         abs_val, rel_val);
377     ph.sf->s_address_update_property (ph.sf->cls, cur,
378         GNUNET_ATS_QUALITY_NET_DELAY,
379         abs_val, rel_val);
380     break;
381   case 1:
382     abs_val = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 10);
383     rel_val = (100 + (double) abs_val) / 100;
384
385     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
386         "Updating peer `%s' address %p type %s abs val %u rel val %.3f\n",
387         GNUNET_i2s (&cur->peer), cur, "GNUNET_ATS_QUALITY_NET_DISTANCE",
388         abs_val, rel_val);
389     ph.sf->s_address_update_property (ph.sf->cls, cur,
390         GNUNET_ATS_QUALITY_NET_DISTANCE,
391         abs_val, rel_val);
392     break;
393   default:
394     break;
395   }
396 }
397
398
399 static void
400 bandwidth_changed_cb (void *cls,
401                       struct ATS_Address *address)
402 {
403   if ( (0 == address->assigned_bw_out) && (0 == address->assigned_bw_in) )
404     return;
405
406   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407               "Bandwidth changed addresses %s %p to %u Bps out / %u Bps in\n",
408               GNUNET_i2s (&address->peer),
409               address,
410               address->assigned_bw_out,
411               address->assigned_bw_in);
412   if (GNUNET_YES == ph.bulk_running)
413     GNUNET_break (0);
414   return;
415 }
416
417
418 static const double *
419 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
420 {
421   return GAS_preference_get_by_peer (NULL, id);
422 }
423
424
425 static void
426 perf_address_initial_update (void *dead,
427     struct GNUNET_CONTAINER_MultiPeerMap * addresses,
428     struct ATS_Address *address)
429 {
430   double delay;
431   double distance;
432   uint32_t random = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
433   delay = (100 + (double) random) / 100;
434   ph.sf->s_address_update_property (ph.sf->cls,
435                                     address, GNUNET_ATS_QUALITY_NET_DELAY,
436       100,  delay);
437
438   random = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
439   distance = (100 + (double) random) / 100;
440
441   ph.sf->s_address_update_property (ph.sf->cls, address,
442                                     GNUNET_ATS_QUALITY_NET_DISTANCE,
443                                     10, distance);
444
445   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
446              "Initial update address %p : %.2f  %.2f\n",
447              address, delay, distance);
448 }
449
450
451 struct DUA_Ctx
452 {
453   int r;
454   int c_cur_a;
455 };
456
457
458 static int
459 do_update_address (void *cls,
460                    const struct GNUNET_PeerIdentity *pid,
461                    void *value)
462 {
463   struct DUA_Ctx *ctx = cls;
464   struct ATS_Address *addr = value;
465
466   if (ctx->c_cur_a == ctx->r)
467     perf_update_address (addr);
468   ctx->c_cur_a++;
469   return GNUNET_OK;
470 }
471
472
473 /**
474  * Update a certain percentage of peers
475  *
476  * @param cp the current number of peers
477  * @param ca the current number of addresses
478  * @param percentage_peers the percentage of peers to update
479  */
480 static void
481 perf_update_all_addresses (unsigned int cp, unsigned int ca, unsigned int percentage_peers)
482 {
483   int c_peer;
484   int c_select;
485   int c_cur_p;
486   int r;
487   int count;
488   unsigned int m[cp];
489   struct DUA_Ctx dua_ctx;
490
491   count = cp * ((double) percentage_peers / 100);
492   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
493       "Updating %u of %u peers \n", count, cp);
494
495   for (c_peer = 0; c_peer < cp; c_peer++)
496     m[c_peer] = 0;
497
498   c_select = 0;
499
500   while (c_select < count)
501   {
502     r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, cp);
503     if (0 == m[r])
504     {
505       m[r] = 1;
506       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
507           "Updating peer [%u] \n", r);
508       c_select++;
509     }
510   }
511   for (c_cur_p = 0; c_cur_p < cp; c_cur_p++)
512   {
513     if (1 == m[c_cur_p])
514     {
515       r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, ca);
516       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
517                  "Updating peer [%u] address [%u]\n", c_cur_p, r);
518
519       dua_ctx.c_cur_a = 0;
520       dua_ctx.r = r;
521       GNUNET_CONTAINER_multipeermap_get_multiple (ph.addresses,
522                                                   &ph.peers[c_cur_p].id,
523                                                   &do_update_address,
524                                                   &dua_ctx);
525     }
526   }
527 }
528
529 /**
530  * Create an address for a peer
531  *
532  * @param cp index of the peer
533  * @param ca index of the address
534  * @return the address
535  */
536 static struct ATS_Address *
537 perf_create_address (int cp, int ca)
538 {
539   struct ATS_Address *a;
540
541   a = create_address (&ph.peers[cp].id,
542       "Test 1", "test 1", strlen ("test 1") + 1, 0);
543   GNUNET_CONTAINER_multipeermap_put (ph.addresses, &ph.peers[cp].id, a,
544       GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
545   return a;
546 }
547
548
549 /**
550  * Information callback for the solver
551  *
552  * @param op the solver operation
553  * @param stat status of the solver operation
554  * @param add additional solver information
555  */
556 static void
557 solver_info_cb (void *cls,
558     enum GAS_Solver_Operation op,
559     enum GAS_Solver_Status stat,
560     enum GAS_Solver_Additional_Information add)
561 {
562   char *add_info;
563   switch (add) {
564     case GAS_INFO_NONE:
565       add_info = "GAS_INFO_NONE";
566       break;
567     case GAS_INFO_FULL:
568       add_info = "GAS_INFO_MLP_FULL";
569       break;
570     case GAS_INFO_UPDATED:
571       add_info = "GAS_INFO_MLP_UPDATED";
572       break;
573     case GAS_INFO_PROP_ALL:
574       add_info = "GAS_INFO_PROP_ALL";
575       break;
576     case GAS_INFO_PROP_SINGLE:
577       add_info = "GAS_INFO_PROP_SINGLE";
578       break;
579     default:
580       add_info = "INVALID";
581       break;
582   }
583
584   struct Result *tmp;
585   switch (op)
586   {
587     case GAS_OP_SOLVE_START:
588       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
589           "Solver notifies `%s' with result `%s' `%s'\n", "GAS_OP_SOLVE_START",
590           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
591       if (GNUNET_NO == ph.expecting_solution)
592       {
593         /* We do not expect a solution at the moment */
594         GNUNET_break (0);
595         return;
596       }
597
598       if ((GAS_STAT_SUCCESS == stat) && (NULL == ph.current_result))
599       {
600         tmp = GNUNET_new (struct Result);
601         /* Create new result */
602         if ((add == GAS_INFO_UPDATED) || (GNUNET_YES == ph.performed_update))
603         {
604           ph.current_result = tmp;
605           //fprintf (stderr,"UPDATE %u %u\n",ph.current_iteration-1, ph.current_p);
606           ph.iterations_results[ph.current_iteration-1].update_results_array[ph.current_p] = tmp;
607         }
608         else
609         {
610           ph.current_result = tmp;
611           //fprintf (stderr,"FULL %u %u\n",ph.current_iteration-1, ph.current_p);
612           ph.iterations_results[ph.current_iteration-1].results_array[ph.current_p] = tmp;
613         }
614
615         ph.current_result->addresses = ph.current_a;
616         ph.current_result->peers = ph.current_p;
617         ph.current_result->s_total = GNUNET_TIME_absolute_get();
618         ph.current_result->d_total_full = GNUNET_TIME_UNIT_FOREVER_REL;
619         ph.current_result->d_setup_full = GNUNET_TIME_UNIT_FOREVER_REL;
620         ph.current_result->d_lp_full = GNUNET_TIME_UNIT_FOREVER_REL;
621         ph.current_result->d_mlp_full = GNUNET_TIME_UNIT_FOREVER_REL;
622         ph.current_result->info = add;
623         if ((add == GAS_INFO_UPDATED) || (GNUNET_YES == ph.performed_update))
624         {
625           ph.current_result->update = GNUNET_YES;
626         }
627         else
628         {
629           ph.current_result->update = GNUNET_NO;
630         }
631
632       }
633       return;
634     case GAS_OP_SOLVE_STOP:
635       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
636           "Solver notifies `%s' with result `%s', `%s'\n", "GAS_OP_SOLVE_STOP",
637           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
638       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
639       {
640         /* We do not expect a solution at the moment */
641         GNUNET_break (0);
642         return;
643       }
644
645       if (GAS_STAT_SUCCESS == stat)
646         ph.current_result->valid = GNUNET_YES;
647       else
648         ph.current_result->valid = GNUNET_NO;
649
650       if (NULL != ph.current_result)
651       {
652         /* Finalize result */
653         ph.current_result->e_total = GNUNET_TIME_absolute_get ();
654         ph.current_result->d_total_full = GNUNET_TIME_absolute_get_difference (
655             ph.current_result->s_total, ph.current_result->e_total);
656       }
657       ph.current_result = NULL;
658       return;
659
660     case GAS_OP_SOLVE_SETUP_START:
661       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
662           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
663           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
664       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
665       {
666         GNUNET_break(0);
667         return;
668       }
669
670       if (GAS_STAT_SUCCESS == stat)
671         ph.current_result->valid = GNUNET_YES;
672       else
673         ph.current_result->valid = GNUNET_NO;
674
675       ph.current_result->s_setup = GNUNET_TIME_absolute_get ();
676       return;
677
678     case GAS_OP_SOLVE_SETUP_STOP:
679       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
680           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
681           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
682       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
683       {
684         GNUNET_break(0);
685         return;
686       }
687
688       if (GAS_STAT_SUCCESS == stat)
689         ph.current_result->valid = GNUNET_YES;
690       else
691         ph.current_result->valid = GNUNET_NO;
692
693       ph.current_result->e_setup = GNUNET_TIME_absolute_get ();
694       ph.current_result->d_setup_full = GNUNET_TIME_absolute_get_difference (
695           ph.current_result->s_setup, ph.current_result->e_setup);
696       return;
697
698     case GAS_OP_SOLVE_MLP_LP_START:
699       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
700           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
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->s_lp = GNUNET_TIME_absolute_get ();
714       return;
715     case GAS_OP_SOLVE_MLP_LP_STOP:
716       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
717           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
718           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
719       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
720       {
721         GNUNET_break(0);
722         return;
723       }
724
725       if (GAS_STAT_SUCCESS == stat)
726         ph.current_result->valid = GNUNET_YES;
727       else
728         ph.current_result->valid = GNUNET_NO;
729
730       ph.current_result->e_lp = GNUNET_TIME_absolute_get ();
731       ph.current_result->d_lp_full = GNUNET_TIME_absolute_get_difference (
732           ph.current_result->s_lp, ph.current_result->e_lp);
733       return;
734
735     case GAS_OP_SOLVE_MLP_MLP_START:
736       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
737           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
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->s_mlp = GNUNET_TIME_absolute_get ();
751       return;
752     case GAS_OP_SOLVE_MLP_MLP_STOP:
753       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
754           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
755           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
756       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
757       {
758         GNUNET_break(0);
759         return;
760       }
761
762       if (GAS_STAT_SUCCESS == stat)
763         ph.current_result->valid = GNUNET_YES;
764       else
765         ph.current_result->valid = GNUNET_NO;
766
767       ph.current_result->e_mlp = GNUNET_TIME_absolute_get ();
768       ph.current_result->d_mlp_full = GNUNET_TIME_absolute_get_difference (
769       ph.current_result->s_mlp, ph.current_result->e_mlp);
770       return;
771     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
772       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
773           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
774           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
775       return;
776     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
777       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
778           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
779           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
780       if (GAS_STAT_SUCCESS != stat)
781       {
782         GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
783             "Solver `%s' failed to update problem with %u peers and %u address!\n",
784             ph.ats_string, ph.current_p, ph.current_a);
785       }
786
787       return;
788     default:
789       break;
790     }
791 }
792
793 /**
794  * Evaluate results for a specific iteration
795  *
796  * @param iteration the iteration to evaluate
797  */
798 static void
799 evaluate (int iteration)
800 {
801   struct Result *cur;
802   int cp;
803
804   for (cp = ph.N_peers_start; cp <= ph.N_peers_end; cp ++)
805   {
806     cur = ph.iterations_results[ph.current_iteration-1].results_array[cp];
807     if (0 == cp)
808       continue;
809     if (NULL == cur)
810     {
811       GNUNET_break (0);
812       fprintf (stderr,
813                "Missing result for %u peers\n", cp);
814       continue;
815     }
816
817
818     if (GNUNET_NO == cur->valid)
819     {
820       fprintf (stderr,
821                "Total time to solve %s for %u peers %u addresses: %s\n",
822                (GNUNET_YES == cur->update) ? "updated" : "full",
823                cur->peers, cur->addresses, "Failed to solve!");
824       continue;
825     }
826
827
828     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_total_full.rel_value_us)
829     {
830       fprintf (stderr,
831          "Total time to solve %s for %u peers %u addresses: %llu us\n",
832          (GNUNET_YES == cur->update) ? "updated" : "full",
833          cur->peers, cur->addresses,
834          (unsigned long long) cur->d_total_full.rel_value_us);
835     }
836
837
838     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_setup_full.rel_value_us)
839     {
840       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
841           "Total time to setup %s %u peers %u addresses: %llu us\n",
842           (GNUNET_YES == cur->update) ? "updated" : "full",
843           cur->peers, cur->addresses,
844           (unsigned long long) cur->d_setup_full.rel_value_us);
845     }
846
847     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_lp_full.rel_value_us)
848     {
849       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
850          "Total time to solve %s LP for %u peers %u addresses: %llu us\n",
851          (GNUNET_YES == cur->update) ? "updated" : "full",
852          cur->peers,
853          cur->addresses,
854          (unsigned long long )cur->d_lp_full.rel_value_us);
855     }
856
857     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_mlp_full.rel_value_us)
858     {
859       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
860           "Total time to solve %s MLP for %u peers %u addresses: %llu us\n",
861           (GNUNET_YES == cur->update) ? "updated" : "full",
862           cur->peers, cur->addresses,
863           (unsigned long long )cur->d_mlp_full.rel_value_us);
864     }
865   }
866 }
867
868
869 static unsigned int
870 get_connectivity_cb (void *cls,
871                      const struct GNUNET_PeerIdentity *peer)
872 {
873   return GNUNET_CONTAINER_multipeermap_contains (ph.addresses,
874                                                  peer);
875 }
876
877
878 /**
879  * Evaluate average results for all iterations
880  */
881 static void
882 write_all_iterations (void)
883 {
884   int c_iteration;
885   int c_peer;
886
887   struct GNUNET_DISK_FileHandle *f_full;
888   struct GNUNET_DISK_FileHandle *f_update;
889   char * data_fn_full;
890   char * data_fn_update;
891   char * data;
892
893   f_full = NULL;
894   f_update = NULL;
895
896   data_fn_full = NULL;
897
898   if (GNUNET_NO == ph.create_datafile)
899     return;
900
901   GNUNET_asprintf (&data_fn_full,
902                    "perf_%s_full_%u-%u_%u_%u.data",
903                    ph.ats_string,
904                    ph.total_iterations,
905                    ph.N_peers_start,
906                    ph.N_peers_end,
907                    ph.N_address);
908   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
909               "Using data file `%s'\n",
910               data_fn_full);
911
912   f_full = GNUNET_DISK_file_open (data_fn_full,
913       GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
914       GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
915   if (NULL == f_full)
916   {
917     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
918                 "Cannot open data file `%s'\n",
919                 data_fn_full);
920     GNUNET_free (data_fn_full);
921     return;
922   }
923
924   data = "#peers;addresses;time total in us;#time setup in us;#time lp in us;#time mlp in us;\n";
925   if (GNUNET_SYSERR == GNUNET_DISK_file_write(f_full, data, strlen(data)))
926     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
927                 "Cannot write data to log file `%s'\n",
928                 data_fn_full);
929
930   data_fn_update = NULL;
931   if (GNUNET_YES == ph.measure_updates)
932   {
933     GNUNET_asprintf (&data_fn_update, "perf_%s_update_%u-%u_%u_%u.data",
934         ph.ats_string,
935         ph.total_iterations,
936         ph.N_peers_start,
937         ph.N_peers_end,
938         ph.N_address);
939     f_update = GNUNET_DISK_file_open (data_fn_update,
940         GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
941         GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
942     if (NULL == f_update)
943     {
944       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
945                   "Cannot open gnuplot file `%s'\n", data_fn_update);
946       GNUNET_free (data_fn_update);
947       if (NULL != f_full)
948         GNUNET_DISK_file_close (f_full);
949       GNUNET_free (data_fn_full);
950       return;
951     }
952
953     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
954                 "Using update data file `%s'\n",
955                 data_fn_update);
956
957     data = "#peers;addresses;time total in us;#time setup in us;#time lp in us;#time mlp in us;\n";
958     if (GNUNET_SYSERR == GNUNET_DISK_file_write (f_update, data, strlen(data)))
959       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
960                   "Cannot write data to log file `%s'\n",
961                   data_fn_update);
962   }
963
964   for (c_peer = ph.N_peers_start; c_peer <= ph.N_peers_end; c_peer ++)
965   {
966     char * data_str;
967     char * data_tmp;
968     char * data_upd_str;
969     char * data_upd_tmp;
970     GNUNET_asprintf(&data_str, "%u;%u",c_peer, ph.N_address);
971     if (ph.measure_updates)
972       GNUNET_asprintf(&data_upd_str, "%u;%u",c_peer, ph.N_address);
973     for (c_iteration = 0; c_iteration < ph.total_iterations; c_iteration ++)
974     {
975       struct Result *cur_full_res;
976       struct Result *cur_upd_res;
977
978
979
980       //fprintf (stderr, "P: %u I: %u  == %p \n", c_peer, c_iteration, cur_res);
981       cur_full_res = ph.iterations_results[c_iteration].results_array[c_peer];
982       if (c_peer == 0)
983         continue;
984       if (NULL == cur_full_res)
985         continue;
986
987       if (ph.measure_updates)
988       {
989         cur_upd_res = ph.iterations_results[c_iteration].update_results_array[c_peer];
990         data_upd_tmp = GNUNET_strdup (data_upd_str);
991         GNUNET_free (data_upd_str);
992         if (GNUNET_YES == cur_full_res->valid)
993         {
994           GNUNET_asprintf (&data_upd_str, "%s;%llu", data_upd_tmp,
995             (NULL == cur_upd_res) ? 0 : cur_upd_res->d_total_full.rel_value_us);
996         }
997         else
998         {
999             GNUNET_asprintf (&data_upd_str, "%s;", data_upd_tmp);
1000         }
1001         GNUNET_free (data_upd_tmp);
1002
1003       }
1004
1005       //fprintf (stderr, "P: %u I: %u: P %i  A %i\n", c_peer, c_iteration, cur_res->peers, cur_res->addresses);
1006       //fprintf (stderr, "D total: %llu\n", (long long unsigned int) cur_res->d_total.rel_value_us);
1007
1008       data_tmp = GNUNET_strdup (data_str);
1009       GNUNET_free (data_str);
1010       if (GNUNET_YES == cur_full_res->valid)
1011       {
1012           GNUNET_asprintf (&data_str, "%s;%llu", data_tmp,
1013               cur_full_res->d_total_full.rel_value_us);
1014       }
1015       else
1016       {
1017           GNUNET_asprintf (&data_str, "%s;", data_tmp);
1018       }
1019
1020       GNUNET_free (data_tmp);
1021     }
1022     data_tmp = GNUNET_strdup (data_str);
1023     GNUNET_free (data_str);
1024     GNUNET_asprintf (&data_str, "%s\n", data_tmp);
1025     GNUNET_free (data_tmp);
1026
1027     fprintf (stderr, "Result full solution: %s\n", data_str);
1028     if (GNUNET_SYSERR == GNUNET_DISK_file_write (f_full, data_str, strlen(data_str)))
1029       GNUNET_break (0);
1030     GNUNET_free (data_str);
1031
1032     if (ph.measure_updates)
1033     {
1034       data_upd_tmp = GNUNET_strdup (data_upd_str);
1035       GNUNET_free (data_upd_str);
1036       GNUNET_asprintf (&data_upd_str, "%s\n", data_upd_tmp);
1037       GNUNET_free (data_upd_tmp);
1038
1039       fprintf (stderr, "Result updated solution: `%s'\n", data_upd_str);
1040       if (GNUNET_SYSERR == GNUNET_DISK_file_write (f_update, data_upd_str, strlen(data_upd_str)))
1041         GNUNET_break (0);
1042       GNUNET_free (data_upd_str);
1043     }
1044   }
1045
1046   if ((NULL != f_full) && (GNUNET_SYSERR == GNUNET_DISK_file_close (f_full)))
1047     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Cannot close log file `%s'\n",
1048         data_fn_full);
1049   GNUNET_free_non_null (data_fn_full);
1050
1051   if ((NULL != f_update) && (GNUNET_SYSERR == GNUNET_DISK_file_close (f_update)))
1052     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Cannot close log file `%s'\n",
1053         data_fn_update);
1054   GNUNET_free_non_null (data_fn_update);
1055 }
1056
1057
1058 static int
1059 do_delete_address (void *cls,
1060                    const struct GNUNET_PeerIdentity *pid,
1061                    void *value)
1062 {
1063   struct ATS_Address *cur = value;
1064
1065   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1066              "Deleting addresses for peer %u\n",
1067              pid);
1068   GNUNET_assert (GNUNET_OK ==
1069                  GNUNET_CONTAINER_multipeermap_remove (ph.addresses,
1070                                                        pid,
1071                                                        cur));
1072   ph.sf->s_del (ph.sf->cls, cur);
1073   GNUNET_free_non_null (cur->atsi);
1074   GNUNET_free (cur);
1075   return GNUNET_OK;
1076 }
1077
1078
1079 /**
1080  * Run a performance iteration
1081  */
1082 static void
1083 perf_run_iteration (void)
1084 {
1085   int cp;
1086   int ca;
1087   int count_p = ph.N_peers_end;
1088   int count_a = ph.N_address;
1089   struct ATS_Address * cur_addr;
1090   uint32_t net;
1091
1092   ph.iterations_results[ph.current_iteration-1].results_array = GNUNET_malloc ((count_p + 1) * sizeof (struct Result *));
1093   if (ph.measure_updates)
1094     ph.iterations_results[ph.current_iteration-1].update_results_array = GNUNET_malloc ((count_p + 1) * sizeof (struct Result *));
1095   ph.peers = GNUNET_malloc ((count_p) * sizeof (struct PerfPeer));
1096   for (cp = 0; cp < count_p; cp++)
1097     perf_create_peer (cp);
1098   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1099       "Iteration %u of %u, added %u peers\n", ph.current_iteration, ph.total_iterations, cp);
1100
1101   for (cp = 0; cp < count_p; cp++)
1102   {
1103     fprintf (stderr,"%u..", cp);
1104     if (GNUNET_NO == ph.bulk_running)
1105     {
1106       ph.bulk_running = GNUNET_YES;
1107       ph.sf->s_bulk_start (ph.sf->cls);
1108     }
1109     ph.current_p = cp + 1;
1110     for (ca = 0; ca < count_a; ca++)
1111     {
1112       cur_addr = perf_create_address (cp, ca);
1113       /* Add address */
1114
1115       /* Random network selection */
1116       //net = 1 + GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_ATS_NetworkTypeCount - 1);
1117       /* Random equally distributed network selection */
1118       net = 1 + (ca %  (GNUNET_ATS_NetworkTypeCount - 1));
1119       /* fprintf (stderr, "Network: %u `%s'\n",
1120        * mod_net , GNUNET_ATS_print_network_type(mod_net)); */
1121
1122       cur_addr->atsi = GNUNET_new (struct GNUNET_ATS_Information);
1123       cur_addr->atsi_count = 1;
1124       cur_addr->atsi[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
1125       cur_addr->atsi[0].value = htonl (net);
1126       ph.sf->s_add (ph.sf->cls, cur_addr, net);
1127
1128       ph.current_a = ca + 1;
1129       perf_address_initial_update (NULL, ph.addresses, cur_addr);
1130       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1131           "Adding address for peer %u address %u in network %s\n", cp, ca,
1132           GNUNET_ATS_print_network_type(net));
1133     }
1134     /* Notify solver about request */
1135     ph.sf->s_get (ph.sf->cls, &ph.peers[cp].id);
1136
1137     if (cp + 1 >= ph.N_peers_start)
1138     {
1139       /* Disable bulk to solve the problem */
1140       if (GNUNET_YES == ph.bulk_running)
1141       {
1142         ph.expecting_solution = GNUNET_YES;
1143         ph.bulk_running = GNUNET_NO;
1144         ph.sf->s_bulk_stop (ph.sf->cls);
1145       }
1146       else
1147         GNUNET_break (0);
1148
1149       /* Problem is solved by the solver here due to unlocking */
1150       ph.expecting_solution = GNUNET_NO;
1151
1152       /* Update the problem */
1153       if ((0 < ph.opt_update_percent) && (GNUNET_YES == ph.measure_updates))
1154       {
1155         /* Update */
1156         GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1157             "Updating problem with %u peers and %u addresses\n", cp + 1, ca);
1158
1159         ph.expecting_solution = GNUNET_YES;
1160         ph.performed_update = GNUNET_YES;
1161         if (GNUNET_NO == ph.bulk_running)
1162         {
1163           ph.bulk_running = GNUNET_YES;
1164           ph.sf->s_bulk_start (ph.sf->cls);
1165         }
1166         perf_update_all_addresses (cp + 1, ca, ph.opt_update_percent);
1167         ph.bulk_running = GNUNET_NO;
1168         ph.sf->s_bulk_stop (ph.sf->cls);
1169         /* Problem is solved by the solver here due to unlocking */
1170         ph.performed_update = GNUNET_NO;
1171         ph.expecting_solution = GNUNET_NO;
1172       }
1173       GNUNET_assert (GNUNET_NO == ph.bulk_running);
1174     }
1175   }
1176   fprintf (stderr,"\n");
1177   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1178       "Done, cleaning up addresses\n");
1179   if (GNUNET_NO == ph.bulk_running)
1180   {
1181     ph.sf->s_bulk_start (ph.sf->cls);
1182     ph.bulk_running = GNUNET_YES;
1183   }
1184
1185   for (cp = 0; cp < count_p; cp++)
1186   {
1187     GNUNET_CONTAINER_multipeermap_get_multiple (ph.addresses,
1188                                                 &ph.peers[cp].id,
1189                                                 &do_delete_address,
1190                                                 NULL);
1191   }
1192   if (GNUNET_NO == ph.bulk_running)
1193   {
1194     ph.sf->s_bulk_stop (ph.sf->cls);
1195     ph.bulk_running = GNUNET_NO;
1196   }
1197
1198   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1199       "Iteration done\n");
1200   GNUNET_free(ph.peers);
1201 }
1202
1203
1204 static void
1205 run (void *cls, char * const *args, const char *cfgfile,
1206     const struct GNUNET_CONFIGURATION_Handle *cfg)
1207 {
1208   GNUNET_log_setup ("perf-ats-solver", "WARNING", NULL);
1209   char *sep;
1210   char *src_filename = GNUNET_strdup (__FILE__);
1211   char *test_filename = cls;
1212   char *solver;
1213   char *plugin;
1214   struct GNUNET_CONFIGURATION_Handle *solver_cfg;
1215   unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1216   unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1217   int c;
1218   int c2;
1219
1220   /* Extract test name */
1221   if (NULL == (sep  = (strstr (src_filename,".c"))))
1222   {
1223     GNUNET_free (src_filename);
1224     GNUNET_break (0);
1225     ret = 1;
1226     return ;
1227   }
1228   sep[0] = '\0';
1229
1230   if (NULL != (sep = strstr (test_filename, ".exe")))
1231     sep[0] = '\0';
1232
1233   if (NULL == (solver = strstr (test_filename, src_filename)))
1234   {
1235     GNUNET_free (src_filename);
1236     GNUNET_break (0);
1237     ret = 1;
1238     return ;
1239   }
1240   solver += strlen (src_filename) +1;
1241
1242   if (0 == strcmp(solver, "proportional"))
1243   {
1244     ph.ats_string = "proportional";
1245   }
1246   else if (0 == strcmp(solver, "mlp"))
1247   {
1248     ph.ats_string = "mlp";
1249   }
1250   else if ((0 == strcmp(solver, "ril")))
1251   {
1252     ph.ats_string = "ril";
1253   }
1254   else
1255   {
1256     GNUNET_free (src_filename);
1257     GNUNET_break (0);
1258     ret = 1;
1259     return ;
1260   }
1261   GNUNET_free (src_filename);
1262
1263   /* Calculcate peers */
1264   if ((0 == ph.N_peers_start) && (0 == ph.N_peers_end))
1265   {
1266     ph.N_peers_start = DEFAULT_PEERS_START;
1267     ph.N_peers_end = DEFAULT_PEERS_END;
1268   }
1269   if (0 == ph.N_address)
1270     ph.N_address = DEFAULT_ADDRESSES;
1271
1272
1273   if (ph.N_peers_start != ph.N_peers_end)
1274     fprintf (stderr, "Benchmarking solver `%s' with %u to %u peers and %u addresses in %u iterations\n",
1275         ph.ats_string, ph.N_peers_start, ph.N_peers_end, ph.N_address, ph.total_iterations);
1276   else
1277     fprintf (stderr, "Benchmarking solver `%s' with %u peers and %u addresses in %u iterations\n",
1278         ph.ats_string, ph.N_peers_end, ph.N_address, ph.total_iterations);
1279
1280   if (0 == ph.opt_update_percent)
1281     ph.opt_update_percent = DEFAULT_UPDATE_PERCENTAGE;
1282
1283   /* Load quotas */
1284   solver_cfg = GNUNET_CONFIGURATION_create();
1285   if ((NULL == solver_cfg) || (GNUNET_SYSERR == (GNUNET_CONFIGURATION_load ( solver_cfg, "perf_ats_solver.conf"))))
1286   {
1287     GNUNET_break(0);
1288     end_now (1);
1289     return;
1290   }
1291   if (GNUNET_ATS_NetworkTypeCount != load_quotas (solver_cfg,
1292       quotas_out, quotas_in, GNUNET_ATS_NetworkTypeCount))
1293   {
1294     GNUNET_break(0);
1295     end_now (1);
1296     return;
1297   }
1298
1299   /* Create array of DLL to store results for iterations */
1300   ph.iterations_results = GNUNET_malloc (sizeof (struct Iteration) * ph.total_iterations);
1301
1302   /* Load solver */
1303   ph.env.cfg = solver_cfg;
1304   ph.stat = GNUNET_STATISTICS_create ("ats", cfg);
1305   ph.env.stats = ph.stat;
1306   ph.addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1307   ph.env.addresses = ph.addresses;
1308   ph.env.bandwidth_changed_cb = bandwidth_changed_cb;
1309   ph.env.get_connectivity = &get_connectivity_cb;
1310   ph.env.get_preferences = &get_preferences_cb;
1311   ph.env.network_count = GNUNET_ATS_NetworkTypeCount;
1312   ph.env.info_cb = &solver_info_cb;
1313
1314   for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1315   {
1316     ph.env.out_quota[c] = quotas_out[c];
1317     ph.env.in_quota[c] = quotas_in[c];
1318     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1319                 "Loading network quotas: `%s' %llu %llu \n",
1320                 GNUNET_ATS_print_network_type (c),
1321                 ph.env.out_quota[c],
1322                 ph.env.in_quota[c]);
1323   }
1324   GAS_normalization_start ();
1325   GAS_preference_init ();
1326
1327   GNUNET_asprintf (&plugin,
1328                    "libgnunet_plugin_ats_%s",
1329                    ph.ats_string);
1330   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1331              _("Initializing solver `%s'\n"),
1332              ph.ats_string);
1333   if  (NULL == (ph.sf = GNUNET_PLUGIN_load (plugin, &ph.env)))
1334   {
1335     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1336                 _("Failed to initialize solver `%s'!\n"),
1337                 plugin);
1338     ret = 1;
1339     return;
1340   }
1341
1342   /* Do the benchmark */
1343   for (ph.current_iteration = 1; ph.current_iteration <= ph.total_iterations; ph.current_iteration++)
1344   {
1345     fprintf (stderr,
1346              "Iteration %u of %u starting\n",
1347              ph.current_iteration,
1348              ph.total_iterations);
1349     perf_run_iteration ();
1350     evaluate (ph.current_iteration);
1351     fprintf (stderr,
1352              "Iteration %u of %u done\n",
1353              ph.current_iteration,
1354              ph.total_iterations);
1355   }
1356   if (ph.create_datafile)
1357     write_all_iterations ();
1358
1359   /* Unload solver*/
1360   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1361              _("Unloading solver `%s'\n"),
1362              ph.ats_string);
1363   GNUNET_PLUGIN_unload (plugin, ph.sf);
1364   ph.sf = NULL;
1365   GNUNET_free (plugin);
1366   for (c = 0; c < ph.total_iterations; c++ )
1367   {
1368     for (c2 = ph.N_peers_start; c2 < ph.N_peers_end; c2++ )
1369     {
1370       if (0 == c2)
1371         continue;
1372       if (ph.measure_updates)
1373         GNUNET_free_non_null (ph.iterations_results[c].update_results_array[c2]);
1374       GNUNET_free (ph.iterations_results[c].results_array[c2]);
1375     }
1376     if (ph.measure_updates)
1377       GNUNET_free (ph.iterations_results[c].update_results_array);
1378     GNUNET_free(ph.iterations_results[c].results_array);
1379   }
1380   GNUNET_free (ph.iterations_results);
1381
1382   GNUNET_CONFIGURATION_destroy (solver_cfg);
1383   GNUNET_STATISTICS_destroy (ph.stat, GNUNET_NO);
1384 }
1385
1386
1387 /**
1388  * Main function of the benchmark
1389  *
1390  * @param argc argument count
1391  * @param argv argument values
1392  */
1393 int
1394 main (int argc, char *argv[])
1395 {
1396   /* extract command line arguments */
1397   ph.opt_update_percent = 0;
1398   ph.N_peers_start = 0;
1399   ph.N_peers_end = 0;
1400   ph.N_address = 0;
1401   ph.ats_string = NULL;
1402   ph.create_datafile = GNUNET_NO;
1403   ph.measure_updates = GNUNET_NO;
1404   ph.total_iterations = 1;
1405
1406   static struct GNUNET_GETOPT_CommandLineOption options[] = {
1407
1408       GNUNET_GETOPT_option_uint ('a',
1409                                      "addresses",
1410                                      gettext_noop ("addresses to use"),
1411                                      &ph.N_address),
1412
1413       GNUNET_GETOPT_option_uint ('s',
1414                                      "start",
1415                                      gettext_noop ("start with peer"),
1416                                      &ph.N_peers_start),
1417
1418       GNUNET_GETOPT_option_uint ('e',
1419                                      "end",
1420                                      gettext_noop ("end with peer"),
1421                                      &ph.N_peers_end),
1422
1423       GNUNET_GETOPT_option_uint ('i',
1424                                      "iterations",
1425                                      gettext_noop ("number of iterations used for averaging (default: 1)"),
1426                                      &ph.total_iterations),
1427
1428       GNUNET_GETOPT_option_uint ('p',
1429                                      "percentage",
1430                                      gettext_noop ("update a fix percentage of addresses"),
1431                                      &ph.opt_update_percent),
1432
1433       GNUNET_GETOPT_option_flag ('d',
1434                                     "data",
1435                                     gettext_noop ("create data file"),
1436                                     &ph.create_datafile),
1437
1438       GNUNET_GETOPT_option_flag ('u',
1439                                     "update",
1440                                     gettext_noop ("measure updates"),
1441                                     &ph.measure_updates),
1442
1443       GNUNET_GETOPT_OPTION_END
1444   };
1445
1446   GNUNET_PROGRAM_run (argc, argv, argv[0], NULL, options, &run, argv[0]);
1447   return ret;
1448 }
1449
1450 /* end of file perf_ats_solver.c */