document options provided
[oweals/gnunet.git] / src / ats / perf_ats_solver.c
1 /*
2  This file is part of GNUnet.
3  (C) 2010,2011 Christian Grothoff (and other contributing authors)
4
5  GNUnet is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published
7  by the Free Software Foundation; either version 3, or (at your
8  option) any later version.
9
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  General Public License for more details.
14
15  You should have received a copy of the GNU General Public License
16  along with GNUnet; see the file COPYING.  If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  Boston, MA 02111-1307, USA.
19  */
20 /**
21  * @file ats/perf_ats_solver.c
22  * @brief generic performance test for ATS solvers
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_statistics_service.h"
29 #include "gnunet-service-ats_addresses.h"
30 #include "gnunet-service-ats_normalization.h"
31 #include "gnunet_ats_service.h"
32 #include "gnunet_ats_plugin.h"
33 #include "test_ats_api_common.h"
34
35 #define DEFAULT_UPDATE_PERCENTAGE       20
36 #define DEFAULT_PEERS_START     10
37 #define DEFAULT_PEERS_END       10
38 #define DEFAULT_ADDRESSES       10
39 #define DEFAULT_ATS_COUNT       2
40
41 #define GNUPLOT_PROP_TEMPLATE "#!/usr/bin/gnuplot \n" \
42 "set datafile separator ';' \n" \
43 "set title \"Execution time Proportional solver  \" \n" \
44 "set xlabel \"Number of peers\" \n" \
45 "set ylabel \"Execution time in us\" \n" \
46 "set grid \n"
47
48 #define GNUPLOT_PROP_UPDATE_TEMPLATE "#!/usr/bin/gnuplot \n" \
49 "set datafile separator ';' \n" \
50 "set title \"Execution time Proportional solver with updated problem\" \n" \
51 "set xlabel \"Number of peers\" \n" \
52 "set ylabel \"Execution time in us\" \n" \
53 "set grid \n"
54
55 #define GNUPLOT_MLP_TEMPLATE "#!/usr/bin/gnuplot \n" \
56 "set datafile separator ';' \n" \
57 "set title \"Execution time MLP solver \" \n" \
58 "set xlabel \"Number of peers\" \n" \
59 "set ylabel \"Execution time in us\" \n" \
60 "set grid \n"
61
62 #define GNUPLOT_MLP_UPDATE_TEMPLATE "#!/usr/bin/gnuplot \n" \
63 "set datafile separator ';' \n" \
64 "set title \"Execution time MLP solver with updated problem\" \n" \
65 "set xlabel \"Number of peers\" \n" \
66 "set ylabel \"Execution time in us\" \n" \
67 "set grid \n"
68
69 #define GNUPLOT_RIL_TEMPLATE "#!/usr/bin/gnuplot \n" \
70 "set datafile separator ';' \n" \
71 "set title \"Execution time RIL solver \" \n" \
72 "set xlabel \"Number of peers\" \n" \
73 "set ylabel \"Execution time in us\" \n" \
74 "set grid \n"
75
76 #define GNUPLOT_RIL_UPDATE_TEMPLATE "#!/usr/bin/gnuplot \n" \
77 "set datafile separator ';' \n" \
78 "set title \"Execution time RIL solver with updated problem\" \n" \
79 "set xlabel \"Number of peers\" \n" \
80 "set ylabel \"Execution time in us\" \n" \
81 "set grid \n"
82
83 /**
84  * Handle for ATS address component
85  */
86 struct PerfHandle
87 {
88   /**
89    * Performance peers
90    */
91   struct PerfPeer *peers;
92
93   /**
94    *  Solver handle
95    */
96   void *solver;
97
98   /**
99    * Statistics stat;
100    */
101   struct GNUNET_STATISTICS_Handle *stat;
102
103   /**
104    * A multihashmap to store all addresses
105    */
106   struct GNUNET_CONTAINER_MultiPeerMap *addresses;
107
108   /**
109    * Solver functions
110    * */
111   struct GNUNET_ATS_PluginEnvironment env;
112
113   struct Result *head;
114
115   struct Result *tail;
116
117   struct Result *current_result;
118
119   int current_p;
120   int current_a;
121
122   /**
123    * Solver description as string
124    */
125   char *ats_string;
126
127   /**
128    * Configured ATS solver
129    */
130   int ats_mode;
131
132   /**
133    * #peers to start benchmarking with
134    */
135   int N_peers_start;
136
137   /**
138    * #peers to end benchmarking with
139    */
140   int N_peers_end;
141
142   /**
143    * #addresses to benchmarking with
144    */
145   int N_address;
146
147   /**
148    * Percentage of peers to update
149    */
150   int opt_update_percent;
151
152   /**
153    * Create gnuplot file
154    */
155   int create_plot;
156
157   /**
158    * Measure updates
159    */
160   int measure_updates;
161
162   /**
163    * Is a bulk operation running?
164    */
165   int bulk_running;
166
167   /**
168    * Is a bulk operation running?
169    */
170   int expecting_solution;
171 };
172
173 struct Result
174 {
175   struct Result *prev;
176   struct Result *next;
177
178   int peers;
179   int addresses;
180   int update;
181
182   enum GAS_Solver_Additional_Information info;
183
184   struct GNUNET_TIME_Relative d_setup;
185   struct GNUNET_TIME_Relative d_lp;
186   struct GNUNET_TIME_Relative d_mlp;
187   struct GNUNET_TIME_Relative d_total;
188
189   struct GNUNET_TIME_Absolute s_setup;
190   struct GNUNET_TIME_Absolute s_lp;
191   struct GNUNET_TIME_Absolute s_mlp;
192   struct GNUNET_TIME_Absolute s_total;
193
194   struct GNUNET_TIME_Absolute e_setup;
195   struct GNUNET_TIME_Absolute e_lp;
196   struct GNUNET_TIME_Absolute e_mlp;
197   struct GNUNET_TIME_Absolute e_total;
198 };
199
200 struct PerfPeer
201 {
202   struct GNUNET_PeerIdentity id;
203
204   struct ATS_Address *head;
205   struct ATS_Address *tail;
206 };
207
208 static struct PerfHandle ph;
209
210 /**
211  * Return value
212  */
213 static int ret;
214
215
216 /**
217  * ATS information
218  */
219 //static struct GNUNET_ATS_Information ats[2];
220
221
222 static void
223 end_now (int res)
224 {
225   if (NULL != ph.stat)
226   {
227     GNUNET_STATISTICS_destroy (ph.stat, GNUNET_NO);
228     ph.stat = NULL;
229   }
230   /*
231    if (NULL != addresses)
232    {
233    GNUNET_CONTAINER_multihashmap_iterate (addresses, &addr_it, NULL);
234    GNUNET_CONTAINER_multihashmap_destroy (addresses);
235    addresses = NULL ;
236    }*/
237   if (NULL != ph.peers)
238   {
239     GNUNET_free(ph.peers);
240   }
241
242   GAS_normalization_stop ();
243   ret = res;
244 }
245
246
247 static void
248 perf_create_peer (int cp)
249 {
250
251   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
252       &ph.peers[cp].id, sizeof (struct GNUNET_PeerIdentity));
253   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating peer #%u: %s \n", cp,
254       GNUNET_i2s (&ph.peers[cp].id));
255 }
256
257
258
259 static void
260 perf_update_address (struct ATS_Address *cur)
261 {
262   int r_type;
263   int r_val;
264
265   r_type = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 2);
266   switch (r_type)
267   {
268   case 0:
269     r_val = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
270     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
271         "Updating peer `%s' address %p type %s val %u\n",
272         GNUNET_i2s (&cur->peer), cur, "GNUNET_ATS_QUALITY_NET_DELAY", r_val);
273     ph.env.sf.s_address_update_property (ph.solver, cur,
274         GNUNET_ATS_QUALITY_NET_DELAY,
275         r_val, (double) (100 + r_val / 100));
276     break;
277   case 1:
278     r_val = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 10);
279
280     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
281         "Updating peer `%s' address %p type %s val %u\n",
282         GNUNET_i2s (&cur->peer), cur, "GNUNET_ATS_QUALITY_NET_DISTANCE", r_val);
283     ph.env.sf.s_address_update_property (ph.solver, cur,
284         GNUNET_ATS_QUALITY_NET_DISTANCE,
285         r_val, (double) (100 + r_val) / 100);
286     break;
287   default:
288     break;
289   }
290   ph.env.sf.s_address_update_inuse (ph.solver, cur, GNUNET_YES);
291 }
292
293
294
295 static void
296 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
297 {
298   if (0 == ntohl(address->assigned_bw_out.value__) &&
299       0 == ntohl(address->assigned_bw_in.value__))
300     return;
301
302   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
303       "Bandwidth changed addresses %s %p to %llu Bps out / %llu Bps in\n",
304       GNUNET_i2s (&address->peer),
305       address,
306       ntohl(address->assigned_bw_out.value__),
307       ntohl(address->assigned_bw_in.value__));
308   if (GNUNET_YES == ph.bulk_running)
309     GNUNET_break (0);
310   return;
311 }
312
313 const double *
314 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
315 {
316   return GAS_normalization_get_preferences_by_peer (id);
317 }
318
319
320 const double *
321 get_property_cb (void *cls, const struct ATS_Address *address)
322 {
323   return GAS_normalization_get_properties ((struct ATS_Address *) address);
324 }
325
326 static void
327 normalized_property_changed_cb (void *cls, struct ATS_Address *peer,
328     uint32_t type, double prop_rel)
329 {
330   /* TODO */
331 }
332
333 static void
334 perf_address_initial_update (void *solver,
335     struct GNUNET_CONTAINER_MultiPeerMap * addresses,
336     struct ATS_Address *address)
337 {
338   ph.env.sf.s_address_update_property (solver, address, GNUNET_ATS_QUALITY_NET_DELAY,
339       100,
340       (double) (100 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100))
341           / 100);
342
343   ph.env.sf.s_address_update_property (solver, address,
344       GNUNET_ATS_QUALITY_NET_DISTANCE, 10,
345       (double) (100 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100))
346           / 100);
347 }
348
349 static void
350 perf_update_all_addresses (unsigned int cp, unsigned int ca, unsigned int percentage_peers)
351 {
352   struct ATS_Address *cur_address;
353   int c_peer;
354   int c_select;
355   int c_cur_p;
356   int c_cur_a;
357   int r;
358   int count;
359   unsigned int m[cp];
360
361   count = cp * ((double) percentage_peers / 100);
362   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
363       "Updating %u of %u peers \n", count, cp);
364
365   for (c_peer = 0; c_peer < cp; c_peer++)
366     m[c_peer] = 0;
367
368   c_select = 0;
369
370   while (c_select < count)
371   {
372     r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, cp);
373     if (0 == m[r])
374     {
375       m[r] = 1;
376       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
377           "Updating peer [%u] \n", r);
378       c_select++;
379     }
380   }
381   for (c_cur_p = 0; c_cur_p < cp; c_cur_p++)
382   {
383     if (1 == m[c_cur_p])
384     {
385       r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, ca);
386       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
387           "Updating peer [%u] address [%u]\n", c_cur_p, r);
388
389       c_cur_a = 0;
390       for (cur_address = ph.peers[c_cur_p].head; NULL != cur_address; cur_address = cur_address->next)
391       {
392         if (c_cur_a == r)
393           perf_update_address (cur_address);
394
395         c_cur_a ++;
396       }
397     }
398   }
399 }
400
401
402 static struct ATS_Address *
403 perf_create_address (int cp, int ca)
404 {
405   struct ATS_Address *a;
406   a = create_address (&ph.peers[cp].id,
407       "Test 1", "test 1", strlen ("test 1") + 1, 0);
408   GNUNET_CONTAINER_DLL_insert (ph.peers[cp].head, ph.peers[cp].tail, a);
409   GNUNET_CONTAINER_multipeermap_put (ph.addresses, &ph.peers[cp].id, a,
410       GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
411   return a;
412 }
413
414 static void
415 solver_info_cb (void *cls,
416     enum GAS_Solver_Operation op,
417     enum GAS_Solver_Status stat,
418     enum GAS_Solver_Additional_Information add)
419 {
420   char *add_info;
421   switch (add) {
422     case GAS_INFO_NONE:
423       add_info = "GAS_INFO_NONE";
424       break;
425     case GAS_INFO_FULL:
426       add_info = "GAS_INFO_MLP_FULL";
427       break;
428     case GAS_INFO_UPDATED:
429       add_info = "GAS_INFO_MLP_UPDATED";
430       break;
431     case GAS_INFO_PROP_ALL:
432       add_info = "GAS_INFO_PROP_ALL";
433       break;
434     case GAS_INFO_PROP_SINGLE:
435       add_info = "GAS_INFO_PROP_SINGLE";
436       break;
437     default:
438       add_info = "INVALID";
439       break;
440   }
441
442   struct Result *tmp;
443   switch (op)
444   {
445     case GAS_OP_SOLVE_START:
446       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
447           "Solver notifies `%s' with result `%s' `%s'\n", "GAS_OP_SOLVE_START",
448           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
449       if (GNUNET_NO == ph.expecting_solution)
450       {
451         /* We do not expect a solution at the moment */
452         GNUNET_break (0);
453         return;
454       }
455
456       if ((GAS_STAT_SUCCESS == stat) && (NULL == ph.current_result))
457       {
458         /* Create new result */
459         tmp = GNUNET_malloc (sizeof (struct Result));
460         ph.current_result = tmp;
461         GNUNET_CONTAINER_DLL_insert_tail(ph.head, ph.tail, tmp);
462         ph.current_result->addresses = ph.current_a;
463         ph.current_result->peers = ph.current_p;
464         ph.current_result->s_total = GNUNET_TIME_absolute_get();
465         ph.current_result->d_total = GNUNET_TIME_UNIT_FOREVER_REL;
466         ph.current_result->d_setup = GNUNET_TIME_UNIT_FOREVER_REL;
467         ph.current_result->d_lp = GNUNET_TIME_UNIT_FOREVER_REL;
468         ph.current_result->d_mlp = GNUNET_TIME_UNIT_FOREVER_REL;
469         ph.current_result->info = add;
470         if (add == GAS_INFO_UPDATED)
471           ph.current_result->update = GNUNET_YES;
472         else
473           ph.current_result->update = GNUNET_NO;
474       }
475       return;
476     case GAS_OP_SOLVE_STOP:
477       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
478           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_STOP",
479           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
480       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
481       {
482         /* We do not expect a solution at the moment */
483         GNUNET_break (0);
484         return;
485       }
486       if (NULL != ph.current_result)
487       {
488         /* Finalize result */
489         ph.current_result->e_total = GNUNET_TIME_absolute_get ();
490         ph.current_result->d_total = GNUNET_TIME_absolute_get_difference (
491             ph.current_result->s_total, ph.current_result->e_total);
492       }
493       ph.current_result = NULL;
494       return;
495
496     case GAS_OP_SOLVE_SETUP_START:
497       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
498           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
499           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
500       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
501       {
502         GNUNET_break(0);
503         return;
504       }
505       ph.current_result->s_setup = GNUNET_TIME_absolute_get ();
506       return;
507
508     case GAS_OP_SOLVE_SETUP_STOP:
509       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
510           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
511           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
512       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
513       {
514         GNUNET_break(0);
515         return;
516       }
517       ph.current_result->e_setup = GNUNET_TIME_absolute_get ();
518       ph.current_result->d_setup = GNUNET_TIME_absolute_get_difference (
519           ph.current_result->s_setup, ph.current_result->e_setup);
520       return;
521
522     case GAS_OP_SOLVE_MLP_LP_START:
523       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
524           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
525           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
526       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
527       {
528         GNUNET_break(0);
529         return;
530       }
531       ph.current_result->s_lp = GNUNET_TIME_absolute_get ();
532       return;
533     case GAS_OP_SOLVE_MLP_LP_STOP:
534       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
535           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
536           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
537       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
538       {
539         GNUNET_break(0);
540         return;
541       }
542       ph.current_result->e_lp = GNUNET_TIME_absolute_get ();
543       ph.current_result->d_lp = GNUNET_TIME_absolute_get_difference (
544           ph.current_result->s_lp, ph.current_result->e_lp);
545       return;
546
547     case GAS_OP_SOLVE_MLP_MLP_START:
548       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
549           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
550           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
551       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
552       {
553         GNUNET_break(0);
554         return;
555       }
556       ph.current_result->s_mlp = GNUNET_TIME_absolute_get ();
557       return;
558     case GAS_OP_SOLVE_MLP_MLP_STOP:
559       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
560           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
561           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
562       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
563       {
564         GNUNET_break(0);
565         return;
566       }
567       ph.current_result->e_mlp = GNUNET_TIME_absolute_get ();
568       ph.current_result->d_mlp = GNUNET_TIME_absolute_get_difference (
569       ph.current_result->s_mlp, ph.current_result->e_mlp);
570       return;
571     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
572       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
573           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
574           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
575       return;
576     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
577       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
578           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
579           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
580       return;
581     default:
582       break;
583     }
584 }
585
586 static void
587 write_gnuplot_script (char * data_fn, int full)
588 {
589   struct GNUNET_DISK_FileHandle *f;
590   char * gfn;
591   char *data;
592   char *template;
593
594   /* Write header */
595   switch (ph.ats_mode) {
596     case MODE_PROPORTIONAL:
597       if (GNUNET_YES == full)
598         template = GNUPLOT_PROP_TEMPLATE;
599       else
600         template = GNUPLOT_PROP_UPDATE_TEMPLATE;
601       break;
602     case MODE_MLP:
603       if (GNUNET_YES == full)
604         template = GNUPLOT_MLP_TEMPLATE;
605       else
606         template = GNUPLOT_MLP_UPDATE_TEMPLATE;
607       break;
608     case MODE_RIL:
609       if (GNUNET_YES == full)
610         template = GNUPLOT_RIL_TEMPLATE;
611       else
612         template = GNUPLOT_RIL_UPDATE_TEMPLATE;
613       break;
614     default:
615       GNUNET_break (0);
616       return;
617   }
618
619   if (GNUNET_YES == full)
620     GNUNET_asprintf (&gfn, "perf_%s_full_%u_%u_%u.gnuplot", ph.ats_string, ph.N_peers_start, ph.N_peers_end, ph.N_address);
621   else
622     GNUNET_asprintf (&gfn, "perf_%s_update_%u_%u_%u.gnuplot", ph.ats_string, ph.N_peers_start, ph.N_peers_end, ph.N_address);
623
624   f = GNUNET_DISK_file_open (gfn,
625       GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
626       GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
627   if (NULL == f)
628   {
629     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot open gnuplot file `%s'\n", gfn);
630     GNUNET_free (gfn);
631     return;
632   }
633
634   if (GNUNET_SYSERR == GNUNET_DISK_file_write(f, template, strlen(template)))
635     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot write data to plot file `%s'\n", gfn);
636
637   data = NULL;
638   if (MODE_PROPORTIONAL == ph.ats_mode)
639   {
640     GNUNET_asprintf (&data, "plot '%s' using 1:%u with lines title 'Total time to solve'\n" \
641                            "pause -1",
642                            data_fn, 3);
643   }
644   else if (MODE_MLP == ph.ats_mode)
645   {
646     GNUNET_asprintf (&data, "plot '%s' using 1:%u with lines title 'Total time to solve',\\\n" \
647                             "'%s' using 1:%u with lines title 'Time to setup',\\\n"
648                             "'%s' using 1:%u with lines title 'Time to solve LP',\\\n"
649                             "'%s' using 1:%u with lines title 'Total time to solve MLP'\n" \
650                             "pause -1",
651                            data_fn, 3,
652                            data_fn, 4,
653                            data_fn, 5,
654                            data_fn, 6);
655   }
656   else if (MODE_RIL == ph.ats_mode)
657   {
658     GNUNET_asprintf (&data,
659                      "plot '%s' using 1:%u with lines title 'Total time to solve'\n" \
660                      "pause -1",
661                      data_fn, 3);
662   }
663
664   if ((NULL != data) &&
665       (GNUNET_SYSERR == GNUNET_DISK_file_write (f, data, strlen(data))))
666     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
667                 "Cannot write data to plot file `%s'\n",
668                 gfn);
669   GNUNET_free_non_null (data);
670
671   if (GNUNET_SYSERR == GNUNET_DISK_file_close(f))
672     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
673                 "Cannot close gnuplot file `%s'\n",
674                 gfn);
675   else
676     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
677                 "Data successfully written to plot file `%s'\n",
678                 gfn);
679   GNUNET_free (gfn);
680
681 }
682
683
684 static void
685 evaluate ()
686 {
687   struct GNUNET_DISK_FileHandle *f_full;
688   struct GNUNET_DISK_FileHandle *f_update;
689   char * data_fn_full;
690   char * data_fn_update;
691   char * data;
692   struct Result *cur;
693   struct Result *next;
694   char * str_d_total;
695   char * str_d_setup;
696   char * str_d_lp;
697   char * str_d_mlp;
698
699   f_full = NULL;
700   f_update = NULL;
701
702   data_fn_full = NULL;
703
704   if (ph.create_plot)
705   {
706     GNUNET_asprintf (&data_fn_full,
707                      "perf_%s_full_%u_%u_%u.data",
708                      ph.ats_string,
709                      ph.N_peers_start,
710                      ph.N_peers_end,
711                      ph.N_address);
712     f_full = GNUNET_DISK_file_open (data_fn_full,
713         GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
714         GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
715     if (NULL == f_full)
716     {
717       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
718                   "Cannot open gnuplot file `%s'\n",
719                   data_fn_full);
720       GNUNET_free (data_fn_full);
721       return;
722     }
723     data = "#peers;addresses;time total in us;#time setup in us;#time lp in us;#time mlp in us;\n";
724     if (GNUNET_SYSERR == GNUNET_DISK_file_write(f_full, data, strlen(data)))
725       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
726                   "Cannot write data to log file `%s'\n",
727                   data_fn_full);
728     write_gnuplot_script (data_fn_full, GNUNET_YES);
729   }
730
731   data_fn_update = NULL;
732   if ((ph.create_plot) && (GNUNET_YES == ph.measure_updates))
733   {
734     GNUNET_asprintf (&data_fn_update, "perf_%s_update_%u_%u_%u.data", ph.ats_string, ph.N_peers_start, ph.N_peers_end, ph.N_address);
735     f_update = GNUNET_DISK_file_open (data_fn_update,
736         GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
737         GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
738     if (NULL == f_update)
739     {
740       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
741                   "Cannot open gnuplot file `%s'\n", data_fn_update);
742       GNUNET_free (data_fn_update);
743       if (NULL != f_full)
744         GNUNET_DISK_file_close (f_full);
745       GNUNET_free (data_fn_full);
746       return;
747     }
748     data = "#peers;addresses;time total in us;#time setup in us;#time lp in us;#time mlp in us;\n";
749     if (GNUNET_SYSERR == GNUNET_DISK_file_write (f_update, data, strlen(data)))
750       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
751                   "Cannot write data to log file `%s'\n",
752                   data_fn_update);
753     write_gnuplot_script (data_fn_update, GNUNET_NO);
754   }
755
756   next = ph.head;
757   while (NULL != (cur = next))
758   {
759     next = cur->next;
760     str_d_total = NULL;
761     str_d_setup = NULL;
762     str_d_lp = NULL;
763     str_d_mlp = NULL;
764
765     /* Print log */
766     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_total.rel_value_us)
767     {
768       fprintf (stderr,
769                "Total time to solve %s for %u peers %u addresses: %llu us\n",
770                (GNUNET_YES == cur->update) ? "updated" : "full",
771                cur->peers, cur->addresses, (unsigned long long) cur->d_total.rel_value_us);
772       GNUNET_asprintf(&str_d_total,
773                       "%llu",
774                       (unsigned long long) cur->d_total.rel_value_us);
775     }
776     else
777       GNUNET_asprintf(&str_d_total, "-1");
778     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_setup.rel_value_us)
779     {
780       fprintf (stderr, "Total time to setup %s %u peers %u addresses: %llu us\n",
781           (GNUNET_YES == cur->update) ? "updated" : "full",
782           cur->peers, cur->addresses, (unsigned long long )cur->d_setup.rel_value_us);
783       GNUNET_asprintf(&str_d_setup, "%llu", (unsigned long long )cur->d_setup.rel_value_us);
784     }
785     else
786       GNUNET_asprintf(&str_d_setup, "-1");
787     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_lp.rel_value_us)
788     {
789       fprintf (stderr,
790                "Total time to solve %s LP for %u peers %u addresses: %llu us\n",
791                (GNUNET_YES == cur->update) ? "updated" : "full",
792                cur->peers,
793                cur->addresses,
794                (unsigned long long )cur->d_lp.rel_value_us);
795       GNUNET_asprintf (&str_d_lp,
796                        "%llu",
797                        (unsigned long long )cur->d_lp.rel_value_us);
798     }
799     else
800       GNUNET_asprintf (&str_d_lp, "-1");
801     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_mlp.rel_value_us)
802     {
803       fprintf (stderr, "Total time to solve %s MLP for %u peers %u addresses: %llu us\n",
804           (GNUNET_YES == cur->update) ? "updated" : "full",
805           cur->peers, cur->addresses, (unsigned long long )cur->d_mlp.rel_value_us);
806       GNUNET_asprintf (&str_d_mlp,
807                        "%llu",
808                        (unsigned long long )cur->d_mlp.rel_value_us);
809     }
810     else
811       GNUNET_asprintf (&str_d_mlp, "-1");
812
813     data = NULL;
814     if (GNUNET_YES == ph.create_plot)
815     {
816
817       GNUNET_asprintf (&data,
818                        "%u;%u;%s;%s;%s;%s\n",
819                        cur->peers, cur->addresses,
820                        str_d_total,
821                        str_d_setup,
822                        str_d_lp,
823                        str_d_mlp);
824       if (cur->update == GNUNET_NO)
825       {
826         if (GNUNET_SYSERR == GNUNET_DISK_file_write (f_full, data, strlen(data)))
827           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
828                       "Cannot write data to log file `%s'\n",
829                       data_fn_full);
830       }
831       if ((cur->update == GNUNET_YES) && (NULL != f_update))
832       {
833         if (GNUNET_SYSERR == GNUNET_DISK_file_write (f_update, data, strlen(data)))
834           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
835                       "Cannot write data to log file `%s'\n",
836                       data_fn_update);
837       }
838       GNUNET_free (data);
839     }
840     GNUNET_free_non_null (str_d_total);
841     GNUNET_free_non_null (str_d_setup);
842     GNUNET_free_non_null (str_d_lp);
843     GNUNET_free_non_null (str_d_mlp);
844
845     GNUNET_CONTAINER_DLL_remove (ph.head, ph.tail, cur);
846     GNUNET_free (cur);
847   }
848
849   if ((NULL != f_full) && (GNUNET_SYSERR == GNUNET_DISK_file_close (f_full)))
850     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Cannot close log file `%s'\n",
851         data_fn_full);
852   GNUNET_free_non_null (data_fn_full);
853
854   if ((NULL != f_update) && (GNUNET_SYSERR == GNUNET_DISK_file_close (f_update)))
855     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Cannot close log file `%s'\n",
856         data_fn_update);
857   GNUNET_free_non_null (data_fn_update);
858 }
859
860
861 static void
862 perf_run ()
863 {
864   struct ATS_Address *cur;
865   struct ATS_Address *next;
866   int cp;
867   int ca;
868   int count_p = ph.N_peers_end;
869   int count_a = ph.N_address;
870   struct ATS_Address * cur_addr;
871
872
873   ph.peers = GNUNET_malloc ((count_p) * sizeof (struct PerfPeer));
874
875   for (cp = 0; cp < count_p; cp++)
876     perf_create_peer (cp);
877   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
878       "Added %u peers\n", cp);
879
880   for (cp = 0; cp < count_p; cp++)
881   {
882     if (GNUNET_NO == ph.bulk_running)
883     {
884       ph.bulk_running = GNUNET_YES;
885       ph.env.sf.s_bulk_start (ph.solver);
886     }
887     ph.current_p = cp + 1;
888     for (ca = 0; ca < count_a; ca++)
889     {
890       cur_addr = perf_create_address (cp, ca);
891       /* Add address */
892       ph.env.sf.s_add (ph.solver, cur_addr, GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_ATS_NetworkTypeCount));
893       ph.current_a = ca + 1;
894       perf_address_initial_update (ph.solver, ph.addresses, cur_addr);
895       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
896           "Adding address for peer %u address %u\n", cp, ca);
897     }
898     /* Notify solver about request */
899     ph.env.sf.s_get (ph.solver, &ph.peers[cp].id);
900
901     if (cp + 1 >= ph.N_peers_start)
902     {
903       /* Disable bulk to solve the problem */
904       if (GNUNET_YES == ph.bulk_running)
905       {
906         ph.expecting_solution = GNUNET_YES;
907         ph.bulk_running = GNUNET_NO;
908         ph.env.sf.s_bulk_stop (ph.solver);
909       }
910       else
911       {
912         GNUNET_break (0);
913       }
914
915       /* Problem is solved by the solver here due to unlocking */
916       ph.expecting_solution = GNUNET_NO;
917
918       /* Update the problem */
919       if ((0 < ph.opt_update_percent) && (GNUNET_YES == ph.measure_updates))
920       {
921         /* Update */
922         GNUNET_log(GNUNET_ERROR_TYPE_INFO,
923             "Updating problem with %u peers and %u addresses\n", cp + 1, ca);
924
925         ph.expecting_solution = GNUNET_YES;
926         if (GNUNET_NO == ph.bulk_running)
927         {
928           ph.bulk_running = GNUNET_YES;
929           ph.env.sf.s_bulk_start (ph.solver);
930         }
931         perf_update_all_addresses (cp + 1, ca, ph.opt_update_percent);
932         ph.bulk_running = GNUNET_NO;
933         ph.env.sf.s_bulk_stop (ph.solver);
934         /* Problem is solved by the solver here due to unlocking */
935         ph.expecting_solution = GNUNET_NO;
936       }
937       GNUNET_assert (GNUNET_NO == ph.bulk_running);
938     }
939   }
940
941   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
942       "Done, cleaning up addresses\n");
943   if (GNUNET_NO == ph.bulk_running)
944   {
945     ph.env.sf.s_bulk_start (ph.solver);
946     ph.bulk_running = GNUNET_YES;
947   }
948
949   for (cp = 0; cp < count_p; cp++)
950   {
951     for (cur = ph.peers[cp].head; cur != NULL ; cur = next)
952     {
953       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
954           "Deleting addresses for peer %u\n", cp);
955       GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (ph.addresses,
956           &ph.peers[cp].id, cur));
957       ph.env.sf.s_del (ph.solver, cur, GNUNET_NO);
958       next = cur->next;
959       GNUNET_CONTAINER_DLL_remove(ph.peers[cp].head, ph.peers[cp].tail, cur);
960       GNUNET_free(cur);
961     }
962
963   }
964   GNUNET_free(ph.peers);
965
966   evaluate ();
967 }
968
969
970 static void
971 run (void *cls, char * const *args, const char *cfgfile,
972     const struct GNUNET_CONFIGURATION_Handle *cfg)
973 {
974   GNUNET_log_setup ("perf-ats-solver", "WARNING", NULL);
975   char *sep;
976   char *src_filename = GNUNET_strdup (__FILE__);
977   char *test_filename = cls;
978   char *solver;
979   char *plugin;
980   struct GNUNET_CONFIGURATION_Handle *solver_cfg;
981   unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
982   unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
983   int c;
984
985   /* Extract test name */
986   if (NULL == (sep  = (strstr (src_filename,".c"))))
987   {
988     GNUNET_free (src_filename);
989     GNUNET_break (0);
990     ret = 1;
991     return ;
992   }
993   sep[0] = '\0';
994
995   if (NULL != (sep = strstr (test_filename, ".exe")))
996     sep[0] = '\0';
997
998   if (NULL == (solver = strstr (test_filename, src_filename)))
999   {
1000     GNUNET_free (src_filename);
1001     GNUNET_break (0);
1002     ret = 1;
1003     return ;
1004   }
1005   solver += strlen (src_filename) +1;
1006
1007   if (0 == strcmp(solver, "proportional"))
1008   {
1009     ph.ats_mode = MODE_PROPORTIONAL;
1010     ph.ats_string = "proportional";
1011   }
1012   else if (0 == strcmp(solver, "mlp"))
1013   {
1014     ph.ats_mode = MODE_MLP;
1015     ph.ats_string = "mlp";
1016   }
1017   else if ((0 == strcmp(solver, "ril")))
1018   {
1019     ph.ats_mode = MODE_RIL;
1020     ph.ats_string = "ril";
1021   }
1022   else
1023   {
1024     GNUNET_free (src_filename);
1025     GNUNET_break (0);
1026     ret = 1;
1027     return ;
1028   }
1029   GNUNET_free (src_filename);
1030
1031   /* Calculcate peers */
1032   if ((0 == ph.N_peers_start) && (0 == ph.N_peers_end))
1033   {
1034     ph.N_peers_start = DEFAULT_PEERS_START;
1035     ph.N_peers_end = DEFAULT_PEERS_END;
1036   }
1037   if (0 == ph.N_address)
1038     ph.N_address = DEFAULT_ADDRESSES;
1039
1040   if (ph.N_peers_start != ph.N_peers_end)
1041     fprintf (stderr, "Benchmarking solver `%s' with %u to %u peers and %u addresses\n",
1042         ph.ats_string, ph.N_peers_start, ph.N_peers_end, ph.N_address);
1043   else
1044     fprintf (stderr, "Benchmarking solver `%s' with %u peers and %u addresses\n",
1045         ph.ats_string, ph.N_peers_end, ph.N_address);
1046
1047   if (0 == ph.opt_update_percent)
1048     ph.opt_update_percent = DEFAULT_UPDATE_PERCENTAGE;
1049
1050   /* Load quotas */
1051   solver_cfg = GNUNET_CONFIGURATION_create();
1052   if ((NULL == solver_cfg) || (GNUNET_SYSERR == (GNUNET_CONFIGURATION_load ( solver_cfg, "perf_ats_solver.conf"))))
1053   {
1054     GNUNET_break(0);
1055     end_now (1);
1056     return;
1057   }
1058   if (GNUNET_ATS_NetworkTypeCount != load_quotas (solver_cfg,
1059       quotas_out, quotas_in, GNUNET_ATS_NetworkTypeCount))
1060   {
1061     GNUNET_break(0);
1062     end_now (1);
1063     return;
1064   }
1065
1066   /* Load solver */
1067   ph.env.cfg = solver_cfg;
1068   ph.stat = GNUNET_STATISTICS_create ("ats", cfg);
1069   ph.env.stats = ph.stat;
1070   ph.addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1071   ph.env.addresses = ph.addresses;
1072   ph.env.bandwidth_changed_cb = bandwidth_changed_cb;
1073   ph.env.get_preferences = &get_preferences_cb;
1074   ph.env.get_property = &get_property_cb;
1075   ph.env.network_count = GNUNET_ATS_NetworkTypeCount;
1076   ph.env.info_cb = &solver_info_cb;
1077   ph.env.info_cb_cls = NULL;
1078
1079   int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1080   for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1081   {
1082     ph.env.networks[c] = networks[c];
1083     ph.env.out_quota[c] = quotas_out[c];
1084     ph.env.in_quota[c] = quotas_in[c];
1085     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Loading network quotas: `%s' %llu %llu \n",
1086         GNUNET_ATS_print_network_type(ph.env.networks[c]),
1087         ph.env.out_quota[c],
1088         ph.env.in_quota[c]);
1089   }
1090   GAS_normalization_start (NULL, NULL, &normalized_property_changed_cb, NULL );
1091
1092   GNUNET_asprintf (&plugin, "libgnunet_plugin_ats_%s", ph.ats_string);
1093   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initializing solver `%s'\n"), ph.ats_string);
1094   if  (NULL == (ph.solver = GNUNET_PLUGIN_load (plugin, &ph.env)))
1095   {
1096     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver `%s'!\n"), plugin);
1097     ret = 1;
1098     return;
1099   }
1100
1101   /* Do work */
1102   perf_run ();
1103
1104   /* Unload solver*/
1105   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Unloading solver `%s'\n"), ph.ats_string);
1106   GNUNET_PLUGIN_unload (plugin, ph.solver);
1107   GNUNET_free (plugin);
1108   GNUNET_CONFIGURATION_destroy (solver_cfg);
1109   ph.solver = NULL;
1110 }
1111
1112 int
1113 main (int argc, char *argv[])
1114 {
1115   /* extract command line arguments */
1116   ph.opt_update_percent = 0;
1117   ph.N_peers_start = 0;
1118   ph.N_peers_end = 0;
1119   ph.N_address = 0;
1120   ph.ats_string = NULL;
1121   ph.create_plot = GNUNET_NO;
1122   ph.measure_updates = GNUNET_NO;
1123
1124   static struct GNUNET_GETOPT_CommandLineOption options[] = {
1125       { 'a', "addresses", NULL,
1126           gettext_noop ("addresses to use"),
1127           1, &GNUNET_GETOPT_set_uint, &ph.N_address },
1128       { 's', "start", NULL,
1129           gettext_noop ("start with peer"),
1130           1, &GNUNET_GETOPT_set_uint, &ph.N_peers_start },
1131       { 'e', "end", NULL,
1132           gettext_noop ("end with peer"),
1133           1, &GNUNET_GETOPT_set_uint, &ph.N_peers_end },
1134       { 'p', "percentage", NULL,
1135           gettext_noop ("update a fix percentage of addresses"),
1136           1, &GNUNET_GETOPT_set_uint, &ph.opt_update_percent },
1137       { 'g', "gnuplot", NULL,
1138           gettext_noop ("create GNUplot file"),
1139           0, &GNUNET_GETOPT_set_one, &ph.create_plot},
1140       { 'u', "update", NULL,
1141           gettext_noop ("measure updates"),
1142           0, &GNUNET_GETOPT_set_one, &ph.measure_updates},
1143       GNUNET_GETOPT_OPTION_END
1144   };
1145
1146   GNUNET_PROGRAM_run (argc, argv, argv[0], NULL, options, &run, argv[0]);
1147
1148   return ret;
1149 }
1150
1151 /* end of file perf_ats_solver.c */