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