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