use -Wl on -no-undefined as it is a linker option:
[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 static void
295 bandwidth_changed_cb (void *cls,
296                       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 %u Bps out / %u Bps in\n",
304               GNUNET_i2s (&address->peer),
305               address,
306               (unsigned int) ntohl (address->assigned_bw_out.value__),
307               (unsigned int) ntohl (address->assigned_bw_in.value__));
308   if (GNUNET_YES == ph.bulk_running)
309     GNUNET_break (0);
310   return;
311 }
312
313
314 const double *
315 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
316 {
317   return GAS_normalization_get_preferences_by_peer (id);
318 }
319
320
321 const double *
322 get_property_cb (void *cls, const struct ATS_Address *address)
323 {
324   return GAS_normalization_get_properties ((struct ATS_Address *) address);
325 }
326
327 static void
328 normalized_property_changed_cb (void *cls, struct ATS_Address *peer,
329     uint32_t type, double prop_rel)
330 {
331   /* TODO */
332 }
333
334 static void
335 perf_address_initial_update (void *solver,
336     struct GNUNET_CONTAINER_MultiPeerMap * addresses,
337     struct ATS_Address *address)
338 {
339   ph.env.sf.s_address_update_property (solver, address, GNUNET_ATS_QUALITY_NET_DELAY,
340       100,
341       (double) (100 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100))
342           / 100);
343
344   ph.env.sf.s_address_update_property (solver, address,
345       GNUNET_ATS_QUALITY_NET_DISTANCE, 10,
346       (double) (100 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100))
347           / 100);
348 }
349
350 static void
351 perf_update_all_addresses (unsigned int cp, unsigned int ca, unsigned int percentage_peers)
352 {
353   struct ATS_Address *cur_address;
354   int c_peer;
355   int c_select;
356   int c_cur_p;
357   int c_cur_a;
358   int r;
359   int count;
360   unsigned int m[cp];
361
362   count = cp * ((double) percentage_peers / 100);
363   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
364       "Updating %u of %u peers \n", count, cp);
365
366   for (c_peer = 0; c_peer < cp; c_peer++)
367     m[c_peer] = 0;
368
369   c_select = 0;
370
371   while (c_select < count)
372   {
373     r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, cp);
374     if (0 == m[r])
375     {
376       m[r] = 1;
377       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
378           "Updating peer [%u] \n", r);
379       c_select++;
380     }
381   }
382   for (c_cur_p = 0; c_cur_p < cp; c_cur_p++)
383   {
384     if (1 == m[c_cur_p])
385     {
386       r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, ca);
387       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
388           "Updating peer [%u] address [%u]\n", c_cur_p, r);
389
390       c_cur_a = 0;
391       for (cur_address = ph.peers[c_cur_p].head; NULL != cur_address; cur_address = cur_address->next)
392       {
393         if (c_cur_a == r)
394           perf_update_address (cur_address);
395
396         c_cur_a ++;
397       }
398     }
399   }
400 }
401
402
403 static struct ATS_Address *
404 perf_create_address (int cp, int ca)
405 {
406   struct ATS_Address *a;
407   a = create_address (&ph.peers[cp].id,
408       "Test 1", "test 1", strlen ("test 1") + 1, 0);
409   GNUNET_CONTAINER_DLL_insert (ph.peers[cp].head, ph.peers[cp].tail, a);
410   GNUNET_CONTAINER_multipeermap_put (ph.addresses, &ph.peers[cp].id, a,
411       GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
412   return a;
413 }
414
415 static void
416 solver_info_cb (void *cls,
417     enum GAS_Solver_Operation op,
418     enum GAS_Solver_Status stat,
419     enum GAS_Solver_Additional_Information add)
420 {
421   char *add_info;
422   switch (add) {
423     case GAS_INFO_NONE:
424       add_info = "GAS_INFO_NONE";
425       break;
426     case GAS_INFO_FULL:
427       add_info = "GAS_INFO_MLP_FULL";
428       break;
429     case GAS_INFO_UPDATED:
430       add_info = "GAS_INFO_MLP_UPDATED";
431       break;
432     case GAS_INFO_PROP_ALL:
433       add_info = "GAS_INFO_PROP_ALL";
434       break;
435     case GAS_INFO_PROP_SINGLE:
436       add_info = "GAS_INFO_PROP_SINGLE";
437       break;
438     default:
439       add_info = "INVALID";
440       break;
441   }
442
443   struct Result *tmp;
444   switch (op)
445   {
446     case GAS_OP_SOLVE_START:
447       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
448           "Solver notifies `%s' with result `%s' `%s'\n", "GAS_OP_SOLVE_START",
449           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
450       if (GNUNET_NO == ph.expecting_solution)
451       {
452         /* We do not expect a solution at the moment */
453         GNUNET_break (0);
454         return;
455       }
456
457       if ((GAS_STAT_SUCCESS == stat) && (NULL == ph.current_result))
458       {
459         /* Create new result */
460         tmp = GNUNET_new (struct Result);
461         ph.current_result = tmp;
462         GNUNET_CONTAINER_DLL_insert_tail(ph.head, ph.tail, tmp);
463         ph.current_result->addresses = ph.current_a;
464         ph.current_result->peers = ph.current_p;
465         ph.current_result->s_total = GNUNET_TIME_absolute_get();
466         ph.current_result->d_total = GNUNET_TIME_UNIT_FOREVER_REL;
467         ph.current_result->d_setup = GNUNET_TIME_UNIT_FOREVER_REL;
468         ph.current_result->d_lp = GNUNET_TIME_UNIT_FOREVER_REL;
469         ph.current_result->d_mlp = GNUNET_TIME_UNIT_FOREVER_REL;
470         ph.current_result->info = add;
471         if (add == GAS_INFO_UPDATED)
472           ph.current_result->update = GNUNET_YES;
473         else
474           ph.current_result->update = GNUNET_NO;
475       }
476       return;
477     case GAS_OP_SOLVE_STOP:
478       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
479           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_STOP",
480           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
481       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
482       {
483         /* We do not expect a solution at the moment */
484         GNUNET_break (0);
485         return;
486       }
487       if (NULL != ph.current_result)
488       {
489         /* Finalize result */
490         ph.current_result->e_total = GNUNET_TIME_absolute_get ();
491         ph.current_result->d_total = GNUNET_TIME_absolute_get_difference (
492             ph.current_result->s_total, ph.current_result->e_total);
493       }
494       ph.current_result = NULL;
495       return;
496
497     case GAS_OP_SOLVE_SETUP_START:
498       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
499           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
500           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
501       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
502       {
503         GNUNET_break(0);
504         return;
505       }
506       ph.current_result->s_setup = GNUNET_TIME_absolute_get ();
507       return;
508
509     case GAS_OP_SOLVE_SETUP_STOP:
510       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
511           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
512           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
513       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
514       {
515         GNUNET_break(0);
516         return;
517       }
518       ph.current_result->e_setup = GNUNET_TIME_absolute_get ();
519       ph.current_result->d_setup = GNUNET_TIME_absolute_get_difference (
520           ph.current_result->s_setup, ph.current_result->e_setup);
521       return;
522
523     case GAS_OP_SOLVE_MLP_LP_START:
524       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
525           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
526           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
527       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
528       {
529         GNUNET_break(0);
530         return;
531       }
532       ph.current_result->s_lp = GNUNET_TIME_absolute_get ();
533       return;
534     case GAS_OP_SOLVE_MLP_LP_STOP:
535       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
536           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
537           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
538       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
539       {
540         GNUNET_break(0);
541         return;
542       }
543       ph.current_result->e_lp = GNUNET_TIME_absolute_get ();
544       ph.current_result->d_lp = GNUNET_TIME_absolute_get_difference (
545           ph.current_result->s_lp, ph.current_result->e_lp);
546       return;
547
548     case GAS_OP_SOLVE_MLP_MLP_START:
549       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
550           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
551           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
552       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
553       {
554         GNUNET_break(0);
555         return;
556       }
557       ph.current_result->s_mlp = GNUNET_TIME_absolute_get ();
558       return;
559     case GAS_OP_SOLVE_MLP_MLP_STOP:
560       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
561           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
562           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
563       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
564       {
565         GNUNET_break(0);
566         return;
567       }
568       ph.current_result->e_mlp = GNUNET_TIME_absolute_get ();
569       ph.current_result->d_mlp = GNUNET_TIME_absolute_get_difference (
570       ph.current_result->s_mlp, ph.current_result->e_mlp);
571       return;
572     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
573       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
574           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
575           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
576       return;
577     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
578       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
579           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
580           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
581       return;
582     default:
583       break;
584     }
585 }
586
587 static void
588 write_gnuplot_script (char * data_fn, int full)
589 {
590   struct GNUNET_DISK_FileHandle *f;
591   char * gfn;
592   char *data;
593   char *template;
594
595   /* Write header */
596   switch (ph.ats_mode) {
597     case MODE_PROPORTIONAL:
598       if (GNUNET_YES == full)
599         template = GNUPLOT_PROP_TEMPLATE;
600       else
601         template = GNUPLOT_PROP_UPDATE_TEMPLATE;
602       break;
603     case MODE_MLP:
604       if (GNUNET_YES == full)
605         template = GNUPLOT_MLP_TEMPLATE;
606       else
607         template = GNUPLOT_MLP_UPDATE_TEMPLATE;
608       break;
609     case MODE_RIL:
610       if (GNUNET_YES == full)
611         template = GNUPLOT_RIL_TEMPLATE;
612       else
613         template = GNUPLOT_RIL_UPDATE_TEMPLATE;
614       break;
615     default:
616       GNUNET_break (0);
617       return;
618   }
619
620   if (GNUNET_YES == full)
621     GNUNET_asprintf (&gfn, "perf_%s_full_%u_%u_%u.gnuplot", ph.ats_string, ph.N_peers_start, ph.N_peers_end, ph.N_address);
622   else
623     GNUNET_asprintf (&gfn, "perf_%s_update_%u_%u_%u.gnuplot", ph.ats_string, ph.N_peers_start, ph.N_peers_end, ph.N_address);
624
625   f = GNUNET_DISK_file_open (gfn,
626       GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
627       GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
628   if (NULL == f)
629   {
630     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot open gnuplot file `%s'\n", gfn);
631     GNUNET_free (gfn);
632     return;
633   }
634
635   if (GNUNET_SYSERR == GNUNET_DISK_file_write(f, template, strlen(template)))
636     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot write data to plot file `%s'\n", gfn);
637
638   data = NULL;
639   if (MODE_PROPORTIONAL == ph.ats_mode)
640   {
641     GNUNET_asprintf (&data, "plot '%s' using 1:%u with lines title 'Total time to solve'\n" \
642                            "pause -1",
643                            data_fn, 3);
644   }
645   else if (MODE_MLP == ph.ats_mode)
646   {
647     GNUNET_asprintf (&data, "plot '%s' using 1:%u with lines title 'Total time to solve',\\\n" \
648                             "'%s' using 1:%u with lines title 'Time to setup',\\\n"
649                             "'%s' using 1:%u with lines title 'Time to solve LP',\\\n"
650                             "'%s' using 1:%u with lines title 'Total time to solve MLP'\n" \
651                             "pause -1",
652                            data_fn, 3,
653                            data_fn, 4,
654                            data_fn, 5,
655                            data_fn, 6);
656   }
657   else if (MODE_RIL == ph.ats_mode)
658   {
659     GNUNET_asprintf (&data,
660                      "plot '%s' using 1:%u with lines title 'Total time to solve'\n" \
661                      "pause -1",
662                      data_fn, 3);
663   }
664
665   if ((NULL != data) &&
666       (GNUNET_SYSERR == GNUNET_DISK_file_write (f, data, strlen(data))))
667     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
668                 "Cannot write data to plot file `%s'\n",
669                 gfn);
670   GNUNET_free_non_null (data);
671
672   if (GNUNET_SYSERR == GNUNET_DISK_file_close(f))
673     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
674                 "Cannot close gnuplot file `%s'\n",
675                 gfn);
676   else
677     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
678                 "Data successfully written to plot file `%s'\n",
679                 gfn);
680   GNUNET_free (gfn);
681
682 }
683
684
685 static void
686 evaluate ()
687 {
688   struct GNUNET_DISK_FileHandle *f_full;
689   struct GNUNET_DISK_FileHandle *f_update;
690   char * data_fn_full;
691   char * data_fn_update;
692   char * data;
693   struct Result *cur;
694   struct Result *next;
695   char * str_d_total;
696   char * str_d_setup;
697   char * str_d_lp;
698   char * str_d_mlp;
699
700   f_full = NULL;
701   f_update = NULL;
702
703   data_fn_full = NULL;
704
705   if (ph.create_plot)
706   {
707     GNUNET_asprintf (&data_fn_full,
708                      "perf_%s_full_%u_%u_%u.data",
709                      ph.ats_string,
710                      ph.N_peers_start,
711                      ph.N_peers_end,
712                      ph.N_address);
713     f_full = GNUNET_DISK_file_open (data_fn_full,
714         GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
715         GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
716     if (NULL == f_full)
717     {
718       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
719                   "Cannot open gnuplot file `%s'\n",
720                   data_fn_full);
721       GNUNET_free (data_fn_full);
722       return;
723     }
724     data = "#peers;addresses;time total in us;#time setup in us;#time lp in us;#time mlp in us;\n";
725     if (GNUNET_SYSERR == GNUNET_DISK_file_write(f_full, data, strlen(data)))
726       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
727                   "Cannot write data to log file `%s'\n",
728                   data_fn_full);
729     write_gnuplot_script (data_fn_full, GNUNET_YES);
730   }
731
732   data_fn_update = NULL;
733   if ((ph.create_plot) && (GNUNET_YES == ph.measure_updates))
734   {
735     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);
736     f_update = GNUNET_DISK_file_open (data_fn_update,
737         GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
738         GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
739     if (NULL == f_update)
740     {
741       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
742                   "Cannot open gnuplot file `%s'\n", data_fn_update);
743       GNUNET_free (data_fn_update);
744       if (NULL != f_full)
745         GNUNET_DISK_file_close (f_full);
746       GNUNET_free (data_fn_full);
747       return;
748     }
749     data = "#peers;addresses;time total in us;#time setup in us;#time lp in us;#time mlp in us;\n";
750     if (GNUNET_SYSERR == GNUNET_DISK_file_write (f_update, data, strlen(data)))
751       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
752                   "Cannot write data to log file `%s'\n",
753                   data_fn_update);
754     write_gnuplot_script (data_fn_update, GNUNET_NO);
755   }
756
757   next = ph.head;
758   while (NULL != (cur = next))
759   {
760     next = cur->next;
761     str_d_total = NULL;
762     str_d_setup = NULL;
763     str_d_lp = NULL;
764     str_d_mlp = NULL;
765
766     /* Print log */
767     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_total.rel_value_us)
768     {
769       fprintf (stderr,
770                "Total time to solve %s for %u peers %u addresses: %llu us\n",
771                (GNUNET_YES == cur->update) ? "updated" : "full",
772                cur->peers, cur->addresses, (unsigned long long) cur->d_total.rel_value_us);
773       GNUNET_asprintf(&str_d_total,
774                       "%llu",
775                       (unsigned long long) cur->d_total.rel_value_us);
776     }
777     else
778       GNUNET_asprintf(&str_d_total, "-1");
779     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_setup.rel_value_us)
780     {
781       fprintf (stderr, "Total time to setup %s %u peers %u addresses: %llu us\n",
782           (GNUNET_YES == cur->update) ? "updated" : "full",
783           cur->peers, cur->addresses, (unsigned long long )cur->d_setup.rel_value_us);
784       GNUNET_asprintf(&str_d_setup, "%llu", (unsigned long long )cur->d_setup.rel_value_us);
785     }
786     else
787       GNUNET_asprintf(&str_d_setup, "-1");
788     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_lp.rel_value_us)
789     {
790       fprintf (stderr,
791                "Total time to solve %s LP for %u peers %u addresses: %llu us\n",
792                (GNUNET_YES == cur->update) ? "updated" : "full",
793                cur->peers,
794                cur->addresses,
795                (unsigned long long )cur->d_lp.rel_value_us);
796       GNUNET_asprintf (&str_d_lp,
797                        "%llu",
798                        (unsigned long long )cur->d_lp.rel_value_us);
799     }
800     else
801       GNUNET_asprintf (&str_d_lp, "-1");
802     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_mlp.rel_value_us)
803     {
804       fprintf (stderr, "Total time to solve %s MLP for %u peers %u addresses: %llu us\n",
805           (GNUNET_YES == cur->update) ? "updated" : "full",
806           cur->peers, cur->addresses, (unsigned long long )cur->d_mlp.rel_value_us);
807       GNUNET_asprintf (&str_d_mlp,
808                        "%llu",
809                        (unsigned long long )cur->d_mlp.rel_value_us);
810     }
811     else
812       GNUNET_asprintf (&str_d_mlp, "-1");
813
814     data = NULL;
815     if (GNUNET_YES == ph.create_plot)
816     {
817
818       GNUNET_asprintf (&data,
819                        "%u;%u;%s;%s;%s;%s\n",
820                        cur->peers, cur->addresses,
821                        str_d_total,
822                        str_d_setup,
823                        str_d_lp,
824                        str_d_mlp);
825       if (cur->update == GNUNET_NO)
826       {
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       if ((cur->update == GNUNET_YES) && (NULL != f_update))
833       {
834         if (GNUNET_SYSERR == GNUNET_DISK_file_write (f_update, data, strlen(data)))
835           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
836                       "Cannot write data to log file `%s'\n",
837                       data_fn_update);
838       }
839       GNUNET_free (data);
840     }
841     GNUNET_free_non_null (str_d_total);
842     GNUNET_free_non_null (str_d_setup);
843     GNUNET_free_non_null (str_d_lp);
844     GNUNET_free_non_null (str_d_mlp);
845
846     GNUNET_CONTAINER_DLL_remove (ph.head, ph.tail, cur);
847     GNUNET_free (cur);
848   }
849
850   if ((NULL != f_full) && (GNUNET_SYSERR == GNUNET_DISK_file_close (f_full)))
851     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Cannot close log file `%s'\n",
852         data_fn_full);
853   GNUNET_free_non_null (data_fn_full);
854
855   if ((NULL != f_update) && (GNUNET_SYSERR == GNUNET_DISK_file_close (f_update)))
856     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Cannot close log file `%s'\n",
857         data_fn_update);
858   GNUNET_free_non_null (data_fn_update);
859 }
860
861
862 static void
863 perf_run ()
864 {
865   struct ATS_Address *cur;
866   struct ATS_Address *next;
867   int cp;
868   int ca;
869   int count_p = ph.N_peers_end;
870   int count_a = ph.N_address;
871   struct ATS_Address * cur_addr;
872
873
874   ph.peers = GNUNET_malloc ((count_p) * sizeof (struct PerfPeer));
875
876   for (cp = 0; cp < count_p; cp++)
877     perf_create_peer (cp);
878   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
879       "Added %u peers\n", cp);
880
881   for (cp = 0; cp < count_p; cp++)
882   {
883     if (GNUNET_NO == ph.bulk_running)
884     {
885       ph.bulk_running = GNUNET_YES;
886       ph.env.sf.s_bulk_start (ph.solver);
887     }
888     ph.current_p = cp + 1;
889     for (ca = 0; ca < count_a; ca++)
890     {
891       cur_addr = perf_create_address (cp, ca);
892       /* Add address */
893       ph.env.sf.s_add (ph.solver, cur_addr, GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_ATS_NetworkTypeCount));
894       ph.current_a = ca + 1;
895       perf_address_initial_update (ph.solver, ph.addresses, cur_addr);
896       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
897           "Adding address for peer %u address %u\n", cp, ca);
898     }
899     /* Notify solver about request */
900     ph.env.sf.s_get (ph.solver, &ph.peers[cp].id);
901
902     if (cp + 1 >= ph.N_peers_start)
903     {
904       /* Disable bulk to solve the problem */
905       if (GNUNET_YES == ph.bulk_running)
906       {
907         ph.expecting_solution = GNUNET_YES;
908         ph.bulk_running = GNUNET_NO;
909         ph.env.sf.s_bulk_stop (ph.solver);
910       }
911       else
912       {
913         GNUNET_break (0);
914       }
915
916       /* Problem is solved by the solver here due to unlocking */
917       ph.expecting_solution = GNUNET_NO;
918
919       /* Update the problem */
920       if ((0 < ph.opt_update_percent) && (GNUNET_YES == ph.measure_updates))
921       {
922         /* Update */
923         GNUNET_log(GNUNET_ERROR_TYPE_INFO,
924             "Updating problem with %u peers and %u addresses\n", cp + 1, ca);
925
926         ph.expecting_solution = GNUNET_YES;
927         if (GNUNET_NO == ph.bulk_running)
928         {
929           ph.bulk_running = GNUNET_YES;
930           ph.env.sf.s_bulk_start (ph.solver);
931         }
932         perf_update_all_addresses (cp + 1, ca, ph.opt_update_percent);
933         ph.bulk_running = GNUNET_NO;
934         ph.env.sf.s_bulk_stop (ph.solver);
935         /* Problem is solved by the solver here due to unlocking */
936         ph.expecting_solution = GNUNET_NO;
937       }
938       GNUNET_assert (GNUNET_NO == ph.bulk_running);
939     }
940   }
941
942   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
943       "Done, cleaning up addresses\n");
944   if (GNUNET_NO == ph.bulk_running)
945   {
946     ph.env.sf.s_bulk_start (ph.solver);
947     ph.bulk_running = GNUNET_YES;
948   }
949
950   for (cp = 0; cp < count_p; cp++)
951   {
952     for (cur = ph.peers[cp].head; cur != NULL ; cur = next)
953     {
954       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
955           "Deleting addresses for peer %u\n", cp);
956       GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (ph.addresses,
957           &ph.peers[cp].id, cur));
958       ph.env.sf.s_del (ph.solver, cur, GNUNET_NO);
959       next = cur->next;
960       GNUNET_CONTAINER_DLL_remove(ph.peers[cp].head, ph.peers[cp].tail, cur);
961       GNUNET_free(cur);
962     }
963
964   }
965   GNUNET_free(ph.peers);
966
967   evaluate ();
968 }
969
970
971 static void
972 run (void *cls, char * const *args, const char *cfgfile,
973     const struct GNUNET_CONFIGURATION_Handle *cfg)
974 {
975   GNUNET_log_setup ("perf-ats-solver", "WARNING", NULL);
976   char *sep;
977   char *src_filename = GNUNET_strdup (__FILE__);
978   char *test_filename = cls;
979   char *solver;
980   char *plugin;
981   struct GNUNET_CONFIGURATION_Handle *solver_cfg;
982   unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
983   unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
984   int c;
985
986   /* Extract test name */
987   if (NULL == (sep  = (strstr (src_filename,".c"))))
988   {
989     GNUNET_free (src_filename);
990     GNUNET_break (0);
991     ret = 1;
992     return ;
993   }
994   sep[0] = '\0';
995
996   if (NULL != (sep = strstr (test_filename, ".exe")))
997     sep[0] = '\0';
998
999   if (NULL == (solver = strstr (test_filename, src_filename)))
1000   {
1001     GNUNET_free (src_filename);
1002     GNUNET_break (0);
1003     ret = 1;
1004     return ;
1005   }
1006   solver += strlen (src_filename) +1;
1007
1008   if (0 == strcmp(solver, "proportional"))
1009   {
1010     ph.ats_mode = MODE_PROPORTIONAL;
1011     ph.ats_string = "proportional";
1012   }
1013   else if (0 == strcmp(solver, "mlp"))
1014   {
1015     ph.ats_mode = MODE_MLP;
1016     ph.ats_string = "mlp";
1017   }
1018   else if ((0 == strcmp(solver, "ril")))
1019   {
1020     ph.ats_mode = MODE_RIL;
1021     ph.ats_string = "ril";
1022   }
1023   else
1024   {
1025     GNUNET_free (src_filename);
1026     GNUNET_break (0);
1027     ret = 1;
1028     return ;
1029   }
1030   GNUNET_free (src_filename);
1031
1032   /* Calculcate peers */
1033   if ((0 == ph.N_peers_start) && (0 == ph.N_peers_end))
1034   {
1035     ph.N_peers_start = DEFAULT_PEERS_START;
1036     ph.N_peers_end = DEFAULT_PEERS_END;
1037   }
1038   if (0 == ph.N_address)
1039     ph.N_address = DEFAULT_ADDRESSES;
1040
1041   if (ph.N_peers_start != ph.N_peers_end)
1042     fprintf (stderr, "Benchmarking solver `%s' with %u to %u peers and %u addresses\n",
1043         ph.ats_string, ph.N_peers_start, ph.N_peers_end, ph.N_address);
1044   else
1045     fprintf (stderr, "Benchmarking solver `%s' with %u peers and %u addresses\n",
1046         ph.ats_string, ph.N_peers_end, ph.N_address);
1047
1048   if (0 == ph.opt_update_percent)
1049     ph.opt_update_percent = DEFAULT_UPDATE_PERCENTAGE;
1050
1051   /* Load quotas */
1052   solver_cfg = GNUNET_CONFIGURATION_create();
1053   if ((NULL == solver_cfg) || (GNUNET_SYSERR == (GNUNET_CONFIGURATION_load ( solver_cfg, "perf_ats_solver.conf"))))
1054   {
1055     GNUNET_break(0);
1056     end_now (1);
1057     return;
1058   }
1059   if (GNUNET_ATS_NetworkTypeCount != load_quotas (solver_cfg,
1060       quotas_out, quotas_in, GNUNET_ATS_NetworkTypeCount))
1061   {
1062     GNUNET_break(0);
1063     end_now (1);
1064     return;
1065   }
1066
1067   /* Load solver */
1068   ph.env.cfg = solver_cfg;
1069   ph.stat = GNUNET_STATISTICS_create ("ats", cfg);
1070   ph.env.stats = ph.stat;
1071   ph.addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1072   ph.env.addresses = ph.addresses;
1073   ph.env.bandwidth_changed_cb = bandwidth_changed_cb;
1074   ph.env.get_preferences = &get_preferences_cb;
1075   ph.env.get_property = &get_property_cb;
1076   ph.env.network_count = GNUNET_ATS_NetworkTypeCount;
1077   ph.env.info_cb = &solver_info_cb;
1078   ph.env.info_cb_cls = NULL;
1079
1080   int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1081   for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1082   {
1083     ph.env.networks[c] = networks[c];
1084     ph.env.out_quota[c] = quotas_out[c];
1085     ph.env.in_quota[c] = quotas_in[c];
1086     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Loading network quotas: `%s' %llu %llu \n",
1087         GNUNET_ATS_print_network_type(ph.env.networks[c]),
1088         ph.env.out_quota[c],
1089         ph.env.in_quota[c]);
1090   }
1091   GAS_normalization_start (NULL, NULL, &normalized_property_changed_cb, NULL );
1092
1093   GNUNET_asprintf (&plugin, "libgnunet_plugin_ats_%s", ph.ats_string);
1094   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initializing solver `%s'\n"), ph.ats_string);
1095   if  (NULL == (ph.solver = GNUNET_PLUGIN_load (plugin, &ph.env)))
1096   {
1097     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver `%s'!\n"), plugin);
1098     ret = 1;
1099     return;
1100   }
1101
1102   /* Do work */
1103   perf_run ();
1104
1105   /* Unload solver*/
1106   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Unloading solver `%s'\n"), ph.ats_string);
1107   GNUNET_PLUGIN_unload (plugin, ph.solver);
1108   GNUNET_free (plugin);
1109   GNUNET_CONFIGURATION_destroy (solver_cfg);
1110   ph.solver = NULL;
1111 }
1112
1113 int
1114 main (int argc, char *argv[])
1115 {
1116   /* extract command line arguments */
1117   ph.opt_update_percent = 0;
1118   ph.N_peers_start = 0;
1119   ph.N_peers_end = 0;
1120   ph.N_address = 0;
1121   ph.ats_string = NULL;
1122   ph.create_plot = GNUNET_NO;
1123   ph.measure_updates = GNUNET_NO;
1124
1125   static struct GNUNET_GETOPT_CommandLineOption options[] = {
1126       { 'a', "addresses", NULL,
1127           gettext_noop ("addresses to use"),
1128           1, &GNUNET_GETOPT_set_uint, &ph.N_address },
1129       { 's', "start", NULL,
1130           gettext_noop ("start with peer"),
1131           1, &GNUNET_GETOPT_set_uint, &ph.N_peers_start },
1132       { 'e', "end", NULL,
1133           gettext_noop ("end with peer"),
1134           1, &GNUNET_GETOPT_set_uint, &ph.N_peers_end },
1135       { 'p', "percentage", NULL,
1136           gettext_noop ("update a fix percentage of addresses"),
1137           1, &GNUNET_GETOPT_set_uint, &ph.opt_update_percent },
1138       { 'g', "gnuplot", NULL,
1139           gettext_noop ("create GNUplot file"),
1140           0, &GNUNET_GETOPT_set_one, &ph.create_plot},
1141       { 'u', "update", NULL,
1142           gettext_noop ("measure updates"),
1143           0, &GNUNET_GETOPT_set_one, &ph.measure_updates},
1144       GNUNET_GETOPT_OPTION_END
1145   };
1146
1147   GNUNET_PROGRAM_run (argc, argv, argv[0], NULL, options, &run, argv[0]);
1148
1149   return ret;
1150 }
1151
1152 /* end of file perf_ats_solver.c */