d4723534d2c635ea147b700c3e3fb3b0aa1b537f
[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 (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, "plot '%s' using 1:%u with lines title 'Total time to solve'\n" \
659                            "pause -1",
660                            data_fn, 3);
661   }
662
663   if ((NULL != data) && (GNUNET_SYSERR == GNUNET_DISK_file_write(f, data, strlen(data))))
664   {
665     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot write data to plot file `%s'\n", gfn);
666     GNUNET_free (data);
667   }
668
669   if (GNUNET_SYSERR == GNUNET_DISK_file_close(f))
670     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot close gnuplot file `%s'\n", gfn);
671   else
672     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Data successfully written to plot file `%s'\n", gfn);
673   GNUNET_free (gfn);
674
675 }
676
677
678 static void
679 evaluate ()
680 {
681   struct GNUNET_DISK_FileHandle *f_full;
682   struct GNUNET_DISK_FileHandle *f_update;
683   char * data_fn_full;
684   char * data_fn_update;
685   char * data;
686   struct Result *cur;
687   struct Result *next;
688   char * str_d_total;
689   char * str_d_setup;
690   char * str_d_lp;
691   char * str_d_mlp;
692
693   f_full = NULL;
694   f_update = NULL;
695
696   data_fn_full = NULL;
697
698   if (ph.create_plot)
699   {
700     GNUNET_asprintf (&data_fn_full, "perf_%s_full_%u_%u_%u.data", ph.ats_string, ph.N_peers_start, ph.N_peers_end, ph.N_address);
701     f_full = GNUNET_DISK_file_open (data_fn_full,
702         GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
703         GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
704     if (NULL == f_full)
705     {
706       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot open gnuplot file `%s'\n", data_fn_full);
707       GNUNET_free (data_fn_full);
708       return;
709     }
710     data = "#peers;addresses;time total in us;#time setup in us;#time lp in us;#time mlp in us;\n";
711     if (GNUNET_SYSERR == GNUNET_DISK_file_write(f_full, data, strlen(data)))
712             GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot write data to log file `%s'\n", data_fn_full);
713     write_gnuplot_script (data_fn_full, GNUNET_YES);
714   }
715
716   data_fn_update = NULL;
717   if ((ph.create_plot) && (GNUNET_YES == ph.measure_updates))
718   {
719     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);
720     f_update = GNUNET_DISK_file_open (data_fn_update,
721         GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
722         GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
723     if (NULL == f_update)
724     {
725       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot open gnuplot file `%s'\n", data_fn_update);
726       GNUNET_free (data_fn_update);
727       return;
728     }
729     data = "#peers;addresses;time total in us;#time setup in us;#time lp in us;#time mlp in us;\n";
730     if (GNUNET_SYSERR == GNUNET_DISK_file_write(f_update, data, strlen(data)))
731             GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot write data to log file `%s'\n", data_fn_update);
732     write_gnuplot_script (data_fn_update, GNUNET_NO);
733   }
734
735   next = ph.head;
736   while (NULL != (cur = next))
737   {
738     next = cur->next;
739     str_d_total = NULL;
740     str_d_setup = NULL;
741     str_d_lp = NULL;
742     str_d_mlp = NULL;
743
744     /* Print log */
745     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_total.rel_value_us)
746     {
747       fprintf (stderr, "Total time to solve %s for %u peers %u addresses: %llu us\n",
748           (GNUNET_YES == cur->update) ? "updated" : "full",
749           cur->peers, cur->addresses, (unsigned long long )cur->d_total.rel_value_us);
750       GNUNET_asprintf(&str_d_total, "%llu", (unsigned long long )cur->d_total.rel_value_us);
751     }
752     else
753       GNUNET_asprintf(&str_d_total, "-1");
754     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_setup.rel_value_us)
755     {
756       fprintf (stderr, "Total time to setup %s %u peers %u addresses: %llu us\n",
757           (GNUNET_YES == cur->update) ? "updated" : "full",
758           cur->peers, cur->addresses, (unsigned long long )cur->d_setup.rel_value_us);
759       GNUNET_asprintf(&str_d_setup, "%llu", (unsigned long long )cur->d_setup.rel_value_us);
760     }
761     else
762       GNUNET_asprintf(&str_d_setup, "-1");
763     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_lp.rel_value_us)
764     {
765       fprintf (stderr, "Total time to solve %s LP for %u peers %u addresses: %llu us\n",
766           (GNUNET_YES == cur->update) ? "updated" : "full",
767           cur->peers, cur->addresses, (unsigned long long )cur->d_lp.rel_value_us);
768       GNUNET_asprintf(&str_d_lp, "%llu", (unsigned long long )cur->d_lp.rel_value_us);
769     }
770     else
771       GNUNET_asprintf(&str_d_lp, "-1");
772     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_mlp.rel_value_us)
773     {
774       fprintf (stderr, "Total time to solve %s MLP for %u peers %u addresses: %llu us\n",
775           (GNUNET_YES == cur->update) ? "updated" : "full",
776           cur->peers, cur->addresses, (unsigned long long )cur->d_mlp.rel_value_us);
777       GNUNET_asprintf(&str_d_mlp, "%llu", (unsigned long long )cur->d_mlp.rel_value_us);
778     }
779     else
780       GNUNET_asprintf(&str_d_mlp, "-1");
781
782     data = NULL;
783     if (GNUNET_YES == ph.create_plot)
784     {
785
786       GNUNET_asprintf(&data,"%u;%u;%s;%s;%s;%s\n",
787           cur->peers, cur->addresses,
788           str_d_total,
789           str_d_setup,
790           str_d_lp,
791           str_d_mlp);
792       if (cur->update == GNUNET_NO)
793       {
794         if (GNUNET_SYSERR == GNUNET_DISK_file_write(f_full, data, strlen(data)))
795           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot write data to log file `%s'\n", data_fn_full);
796       }
797       if ((cur->update == GNUNET_YES) && (NULL != f_update))
798       {
799         if (GNUNET_SYSERR == GNUNET_DISK_file_write(f_update, data, strlen(data)))
800           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot write data to log file `%s'\n", data_fn_update);
801       }
802       GNUNET_free (data);
803     }
804     GNUNET_free_non_null (str_d_total);
805     GNUNET_free_non_null (str_d_setup);
806     GNUNET_free_non_null (str_d_lp);
807     GNUNET_free_non_null (str_d_mlp);
808
809     GNUNET_CONTAINER_DLL_remove (ph.head, ph.tail, cur);
810     GNUNET_free (cur);
811   }
812
813   if ((NULL != f_full) && (GNUNET_SYSERR == GNUNET_DISK_file_close (f_full)))
814     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Cannot close log file `%s'\n",
815         data_fn_full);
816   GNUNET_free_non_null (data_fn_full);
817
818   if ((NULL != f_update) && (GNUNET_SYSERR == GNUNET_DISK_file_close (f_update)))
819     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Cannot close log file `%s'\n",
820         data_fn_update);
821   GNUNET_free_non_null (data_fn_update);
822 }
823
824 static void
825 perf_run ()
826 {
827   struct ATS_Address *cur;
828   struct ATS_Address *next;
829   int cp;
830   int ca;
831   int count_p = ph.N_peers_end;
832   int count_a = ph.N_address;
833   struct ATS_Address * cur_addr;
834
835
836   ph.peers = GNUNET_malloc ((count_p) * sizeof (struct PerfPeer));
837
838   for (cp = 0; cp < count_p; cp++)
839     perf_create_peer (cp);
840   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
841       "Added %u peers\n", cp);
842
843   for (cp = 0; cp < count_p; cp++)
844   {
845     if (GNUNET_NO == ph.bulk_running)
846     {
847       ph.bulk_running = GNUNET_YES;
848       ph.env.sf.s_bulk_start (ph.solver);
849     }
850     ph.current_p = cp + 1;
851     for (ca = 0; ca < count_a; ca++)
852     {
853       cur_addr = perf_create_address (cp, ca);
854       /* Add address */
855       ph.env.sf.s_add (ph.solver, cur_addr, GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_ATS_NetworkTypeCount));
856       ph.current_a = ca + 1;
857       perf_address_initial_update (ph.solver, ph.addresses, cur_addr);
858       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
859           "Adding address for peer %u address %u\n", cp, ca);
860     }
861     /* Notify solver about request */
862     ph.env.sf.s_get (ph.solver, &ph.peers[cp].id);
863
864     if (cp + 1 >= ph.N_peers_start)
865     {
866       /* Disable bulk to solve the problem */
867       if (GNUNET_YES == ph.bulk_running)
868       {
869         ph.expecting_solution = GNUNET_YES;
870         ph.bulk_running = GNUNET_NO;
871         ph.env.sf.s_bulk_stop (ph.solver);
872       }
873       else
874       {
875         GNUNET_break (0);
876       }
877
878       /* Problem is solved by the solver here due to unlocking */
879       ph.expecting_solution = GNUNET_NO;
880
881       /* Update the problem */
882       if ((0 < ph.opt_update_percent) && (GNUNET_YES == ph.measure_updates))
883       {
884         /* Update */
885         GNUNET_log(GNUNET_ERROR_TYPE_INFO,
886             "Updating problem with %u peers and %u addresses\n", cp + 1, ca);
887
888         ph.expecting_solution = GNUNET_YES;
889         if (GNUNET_NO == ph.bulk_running)
890         {
891           ph.bulk_running = GNUNET_YES;
892           ph.env.sf.s_bulk_start (ph.solver);
893         }
894         perf_update_all_addresses (cp + 1, ca, ph.opt_update_percent);
895         ph.bulk_running = GNUNET_NO;
896         ph.env.sf.s_bulk_stop (ph.solver);
897         /* Problem is solved by the solver here due to unlocking */
898         ph.expecting_solution = GNUNET_NO;
899       }
900       GNUNET_assert (GNUNET_NO == ph.bulk_running);
901     }
902   }
903
904   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
905       "Done, cleaning up addresses\n");
906   if (GNUNET_NO == ph.bulk_running)
907   {
908     ph.env.sf.s_bulk_start (ph.solver);
909     ph.bulk_running = GNUNET_YES;
910   }
911
912   for (cp = 0; cp < count_p; cp++)
913   {
914     for (cur = ph.peers[cp].head; cur != NULL ; cur = next)
915     {
916       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
917           "Deleting addresses for peer %u\n", cp);
918       GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (ph.addresses,
919           &ph.peers[cp].id, cur));
920       ph.env.sf.s_del (ph.solver, cur, GNUNET_NO);
921       next = cur->next;
922       GNUNET_CONTAINER_DLL_remove(ph.peers[cp].head, ph.peers[cp].tail, cur);
923       GNUNET_free(cur);
924     }
925
926   }
927   GNUNET_free(ph.peers);
928
929   evaluate ();
930 }
931
932
933 static void
934 run (void *cls, char * const *args, const char *cfgfile,
935     const struct GNUNET_CONFIGURATION_Handle *cfg)
936 {
937   GNUNET_log_setup ("perf-ats-solver", "WARNING", NULL);
938   char *sep;
939   char *src_filename = GNUNET_strdup (__FILE__);
940   char *test_filename = cls;
941   char *solver;
942   char *plugin;
943   struct GNUNET_CONFIGURATION_Handle *solver_cfg;
944   unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
945   unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
946   int c;
947
948   /* Extract test name */
949   if (NULL == (sep  = (strstr (src_filename,".c"))))
950   {
951     GNUNET_free (src_filename);
952     GNUNET_break (0);
953     ret = 1;
954     return ;
955   }
956   sep[0] = '\0';
957
958   if (NULL != (sep = strstr (test_filename, ".exe")))
959     sep[0] = '\0';
960
961   if (NULL == (solver = strstr (test_filename, src_filename)))
962   {
963     GNUNET_free (src_filename);
964     GNUNET_break (0);
965     ret = 1;
966     return ;
967   }
968   solver += strlen (src_filename) +1;
969
970   if (0 == strcmp(solver, "proportional"))
971   {
972     ph.ats_mode = MODE_PROPORTIONAL;
973     ph.ats_string = "proportional";
974   }
975   else if (0 == strcmp(solver, "mlp"))
976   {
977     ph.ats_mode = MODE_MLP;
978     ph.ats_string = "mlp";
979   }
980   else if ((0 == strcmp(solver, "ril")))
981   {
982     ph.ats_mode = MODE_RIL;
983     ph.ats_string = "ril";
984   }
985   else
986   {
987     GNUNET_free (src_filename);
988     GNUNET_break (0);
989     ret = 1;
990     return ;
991   }
992   GNUNET_free (src_filename);
993
994   /* Calculcate peers */
995   if ((0 == ph.N_peers_start) && (0 == ph.N_peers_end))
996   {
997     ph.N_peers_start = DEFAULT_PEERS_START;
998     ph.N_peers_end = DEFAULT_PEERS_END;
999   }
1000   if (0 == ph.N_address)
1001     ph.N_address = DEFAULT_ADDRESSES;
1002
1003   if (ph.N_peers_start != ph.N_peers_end)
1004     fprintf (stderr, "Benchmarking solver `%s' with %u to %u peers and %u addresses\n",
1005         ph.ats_string, ph.N_peers_start, ph.N_peers_end, ph.N_address);
1006   else
1007     fprintf (stderr, "Benchmarking solver `%s' with %u peers and %u addresses\n",
1008         ph.ats_string, ph.N_peers_end, ph.N_address);
1009
1010   if (0 == ph.opt_update_percent)
1011     ph.opt_update_percent = DEFAULT_UPDATE_PERCENTAGE;
1012
1013   /* Load quotas */
1014   solver_cfg = GNUNET_CONFIGURATION_create();
1015   if ((NULL == solver_cfg) || (GNUNET_SYSERR == (GNUNET_CONFIGURATION_load ( solver_cfg, "perf_ats_solver.conf"))))
1016   {
1017     GNUNET_break(0);
1018     end_now (1);
1019     return;
1020   }
1021   if (GNUNET_ATS_NetworkTypeCount != load_quotas (solver_cfg,
1022       quotas_out, quotas_in, GNUNET_ATS_NetworkTypeCount))
1023   {
1024     GNUNET_break(0);
1025     end_now (1);
1026     return;
1027   }
1028
1029   /* Load solver */
1030   ph.env.cfg = solver_cfg;
1031   ph.stat = GNUNET_STATISTICS_create ("ats", cfg);
1032   ph.env.stats = ph.stat;
1033   ph.addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1034   ph.env.addresses = ph.addresses;
1035   ph.env.bandwidth_changed_cb = bandwidth_changed_cb;
1036   ph.env.get_preferences = &get_preferences_cb;
1037   ph.env.get_property = &get_property_cb;
1038   ph.env.network_count = GNUNET_ATS_NetworkTypeCount;
1039   ph.env.info_cb = &solver_info_cb;
1040   ph.env.info_cb_cls = NULL;
1041
1042   int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1043   for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1044   {
1045     ph.env.networks[c] = networks[c];
1046     ph.env.out_quota[c] = quotas_out[c];
1047     ph.env.in_quota[c] = quotas_in[c];
1048     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Loading network quotas: `%s' %llu %llu \n",
1049         GNUNET_ATS_print_network_type(ph.env.networks[c]),
1050         ph.env.out_quota[c],
1051         ph.env.in_quota[c]);
1052   }
1053   GAS_normalization_start (NULL, NULL, &normalized_property_changed_cb, NULL );
1054
1055   GNUNET_asprintf (&plugin, "libgnunet_plugin_ats_%s", ph.ats_string);
1056   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initializing solver `%s'\n"), ph.ats_string);
1057   if  (NULL == (ph.solver = GNUNET_PLUGIN_load (plugin, &ph.env)))
1058   {
1059     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver `%s'!\n"), plugin);
1060     ret = 1;
1061     return;
1062   }
1063
1064   /* Do work */
1065   perf_run ();
1066
1067   /* Unload solver*/
1068   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Unloading solver `%s'\n"), ph.ats_string);
1069   GNUNET_PLUGIN_unload (plugin, ph.solver);
1070   GNUNET_free (plugin);
1071   GNUNET_CONFIGURATION_destroy (solver_cfg);
1072   ph.solver = NULL;
1073 }
1074
1075 int
1076 main (int argc, char *argv[])
1077 {
1078   /* extract command line arguments */
1079   ph.opt_update_percent = 0;
1080   ph.N_peers_start = 0;
1081   ph.N_peers_end = 0;
1082   ph.N_address = 0;
1083   ph.ats_string = NULL;
1084   ph.create_plot = GNUNET_NO;
1085   ph.measure_updates = GNUNET_NO;
1086
1087   static struct GNUNET_GETOPT_CommandLineOption options[] = {
1088       { 'a', "addresses", NULL,
1089           gettext_noop ("addresses to use"),
1090           1, &GNUNET_GETOPT_set_uint, &ph.N_address },
1091       { 's', "start", NULL,
1092           gettext_noop ("start with peer"),
1093           1, &GNUNET_GETOPT_set_uint, &ph.N_peers_start },
1094       { 'e', "end", NULL,
1095           gettext_noop ("end with peer"),
1096           1, &GNUNET_GETOPT_set_uint, &ph.N_peers_end },
1097       { 'p', "percentage", NULL,
1098           gettext_noop ("update a fix percentage of addresses"),
1099           1, &GNUNET_GETOPT_set_uint, &ph.opt_update_percent },
1100       { 'g', "gnuplot", NULL,
1101           gettext_noop ("create GNUplot file"),
1102           0, &GNUNET_GETOPT_set_one, &ph.create_plot},
1103       { 'u', "update", NULL,
1104           gettext_noop ("measure updates"),
1105           0, &GNUNET_GETOPT_set_one, &ph.measure_updates},
1106       GNUNET_GETOPT_OPTION_END
1107   };
1108
1109   GNUNET_PROGRAM_run (argc, argv, argv[0], NULL, options, &run, argv[0]);
1110
1111   return ret;
1112 }
1113
1114 /* end of file perf_ats_solver.c */