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