- doxygen
[oweals/gnunet.git] / src / ats / perf_ats_mlp.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_mlp
22  * @brief performance test for the MLP solver
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_statistics_service.h"
30 #include "gnunet-service-ats_addresses_mlp.h"
31
32 #define VERBOSE GNUNET_YES
33 #define VERBOSE_ARM GNUNET_NO
34
35 #define MLP_MAX_EXEC_DURATION   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3)
36 #define MLP_MAX_ITERATIONS      INT_MAX
37
38 #define DEF_PEERS 10
39 #define DEF_ADDRESSES_PER_PEER 5
40 #define DEF_ATS_VALUES 2
41 #define DEF_ATS_MAX_DELAY 30
42 #define DEF_ATS_MAX_DISTANCE 3
43
44 static unsigned int peers;
45 static unsigned int addresses;
46 static unsigned int numeric;
47 static unsigned int update_percentage;
48
49 static int start;
50 static int end;
51
52 struct PeerContext *p;
53 struct ATS_Address *a;
54
55 static int ret;
56
57 struct GNUNET_CONTAINER_MultiHashMap * amap;
58
59 struct GAS_MLP_Handle *mlp;
60
61
62
63
64 GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
65
66 struct PeerContext
67 {
68   struct GNUNET_PeerIdentity id;
69
70   struct Address *addr;
71 };
72
73 struct Address
74 {
75   char *plugin;
76   size_t plugin_len;
77
78   void *addr;
79   size_t addr_len;
80
81   struct GNUNET_ATS_Information *ats;
82   int ats_count;
83
84   void *session;
85 };
86
87 void
88 do_shutdown (void *cls,
89              const struct GNUNET_SCHEDULER_TaskContext *tc)
90 {
91   unsigned int ca;
92
93   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
94
95   if (NULL != mlp)
96   {
97     GAS_mlp_done (mlp);
98     mlp = NULL;
99   }
100
101   if (NULL != a)
102   {
103     for (ca=0; ca < (peers * addresses); ca++)
104     {
105       GNUNET_free (a[ca].plugin);
106       GNUNET_free (a[ca].ats);
107     }
108   }
109
110   if (NULL != amap)
111     GNUNET_CONTAINER_multihashmap_destroy(amap);
112   GNUNET_free_non_null (a);
113   GNUNET_free_non_null (p);
114
115 }
116
117 static void
118 update_addresses (struct ATS_Address * a, unsigned int addrs, unsigned int percentage)
119 {
120   if (percentage == 0)
121     return;
122
123   unsigned int ua = (addrs) * ((float) percentage / 100);
124   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Updating %u of %u addresses per peer\n", ua, addrs);
125
126   unsigned int updated[addrs];
127   unsigned int u_types[DEF_ATS_VALUES];
128   unsigned int updates = 0;
129   unsigned int u_type = 0;
130   unsigned int u_val = 0;
131   unsigned int cur = 0;
132
133   u_types[0] = 0;
134   u_types[1] = 0;
135
136   for (cur = 0; cur < addrs; cur ++)
137   {
138     updated[cur] = 0;
139   }
140   cur = 0;
141
142   while (updates < ua)
143   {
144     cur = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, addrs);
145     if (0 == updated[cur])
146     {
147       u_type = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DEF_ATS_VALUES);
148       switch (u_type) {
149         case 0:
150             do
151             {
152             u_val = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DEF_ATS_MAX_DELAY);
153             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating DELAY from %u to %u\n",a[cur].ats[u_type].value, u_val);
154             }
155             while (a[cur].ats[u_type].value == u_val);
156           break;
157         case 1:
158           do
159           {
160             u_val = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DEF_ATS_MAX_DISTANCE);
161             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating DISTANCE from %u to %u\n",a[cur].ats[u_type].value, u_val);
162           }
163           while (a[cur].ats[u_type].value == u_val);
164           break;
165         default:
166           GNUNET_break (0);
167           break;
168       }
169       u_types[u_type]++;
170
171       a[cur].ats[u_type].value = u_val;
172       updated[cur] = 1;
173       GAS_mlp_address_update(mlp, amap, &a[cur]);
174       updates++;
175     }
176   }
177   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Updated %u delay and %u distance values\n", u_types[0], u_types[1]);
178
179 }
180
181
182 static void
183 check (void *cls, char *const *args, const char *cfgfile,
184        const struct GNUNET_CONFIGURATION_Handle *cfg)
185 {
186   unsigned int c = 0;
187   unsigned int c2 = 0;
188   unsigned int ca = 0;
189   int update = GNUNET_NO;
190   int range = GNUNET_NO;
191   int res;
192
193 #if !HAVE_LIBGLPK
194   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
195   ret = 1;
196   return;
197 #endif
198
199   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up %u peers with %u addresses per peer\n", peers, addresses);
200
201   mlp = GAS_mlp_init (cfg, NULL, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
202   if (NULL == mlp)
203   {
204     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to init MLP\n");
205     ret = 1;
206     if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
207       GNUNET_SCHEDULER_cancel(shutdown_task);
208     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
209   }
210
211   if (peers == 0)
212     peers = DEF_PEERS;
213   if (addresses == 0)
214     addresses = DEF_ADDRESSES_PER_PEER;
215
216   p = GNUNET_malloc (peers * sizeof (struct ATS_Peer));
217   a = GNUNET_malloc (peers * addresses * sizeof (struct ATS_Address));
218
219   amap = GNUNET_CONTAINER_multihashmap_create(addresses * peers);
220
221   mlp->auto_solve = GNUNET_NO;
222   if (start == 0)
223       start = 0;
224   if (end == 0)
225       end = -1;
226   if ((start != -1) && (end != -1))
227   {
228     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Solving problem starting from %u to %u\n", start , end);
229     range = GNUNET_YES;
230   }
231   else
232     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Solving problem for %u peers\n", peers);
233
234   if ((update_percentage >= 0) && (update_percentage <= 100))
235   {
236     update = GNUNET_YES;
237     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Benchmarking with existing presolution and %u \% updated addresses\n", update_percentage);
238   }
239   else if ((update_percentage > 100) && (update_percentage != UINT_MAX))
240   {
241     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Invalid percentage: %u\n", update_percentage);
242     ret = 1;
243     return;
244   }
245
246   for (c=0; c < peers; c++)
247   {
248     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up peer %u\n", c);
249     GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_NONCE, &p[c].id.hashPubKey);
250
251     for (c2=0; c2 < addresses; c2++)
252     {
253       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up address %u for peer %u\n", c2, c);
254       /* Setting required information */
255       a[ca].mlp_information = NULL;
256       a[ca].prev = NULL;
257       a[ca].next = NULL;
258
259       /* Setting address */
260       a[ca].peer = p[c].id;
261       a[ca].plugin = strdup("test");
262       a[ca].atsp_network_type = GNUNET_ATS_NET_LOOPBACK;
263
264       a[ca].ats = GNUNET_malloc (DEF_ATS_VALUES * sizeof (struct GNUNET_ATS_Information));
265       a[ca].ats[0].type = GNUNET_ATS_QUALITY_NET_DELAY;
266       a[ca].ats[0].value = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, DEF_ATS_MAX_DELAY);
267       a[ca].ats[1].type = GNUNET_ATS_QUALITY_NET_DISTANCE;
268       a[ca].ats[1].value = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, DEF_ATS_MAX_DISTANCE);
269       a[ca].ats_count = 2;
270       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up address %u\n", ca);
271       GNUNET_CONTAINER_multihashmap_put (amap, &a[ca].peer.hashPubKey, &a[ca], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
272       GAS_mlp_address_update(mlp, amap, &a[ca]);
273       ca++;
274     }
275
276     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Problem contains %u peers and %u adresses\n", mlp->c_p, mlp->addr_in_problem);
277
278     if (((GNUNET_YES == range) && (((start >= 0) && ((c+1) >= start)) && (c <= end))) || ((c+1) == peers))
279     {
280       GNUNET_assert ((c+1) == mlp->c_p);
281       GNUNET_assert ((c+1) * addresses == mlp->addr_in_problem);
282
283       /* Solving the problem */
284       struct GAS_MLP_SolutionContext ctx;
285
286       res = GAS_mlp_solve_problem(mlp, &ctx);
287
288       if (GNUNET_NO == update)
289       {
290         if (GNUNET_OK == res)
291         {
292           GNUNET_assert (GNUNET_OK == ctx.lp_result);
293           GNUNET_assert (GNUNET_OK == ctx.mlp_result);
294           if (GNUNET_YES == numeric)
295             printf ("%u;%u;%llu;%llu\n",mlp->c_p, mlp->addr_in_problem, (long long unsigned int) ctx.lp_duration.rel_value, (long long unsigned int) ctx.mlp_duration.rel_value);
296           else
297             GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Problem solved for %u peers with %u address successfully (LP: %llu ms / MLP: %llu ms)\n",
298                 mlp->c_p, mlp->addr_in_problem, ctx.lp_duration.rel_value, ctx.mlp_duration.rel_value);
299         }
300         else
301           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Solving problem with %u peers and %u addresses failed\n", c, c2);
302       }
303       else
304       {
305         struct GAS_MLP_SolutionContext uctx;
306         /* Update addresses */
307         update_addresses (a, (c+1) * c2, update_percentage);
308
309         /* Solve again */
310         res = GAS_mlp_solve_problem(mlp, &uctx);
311
312         if (GNUNET_OK == res)
313         {
314           GNUNET_assert (GNUNET_OK == uctx.lp_result);
315           GNUNET_assert (GNUNET_OK == uctx.mlp_result);
316           if (GNUNET_YES == numeric)
317             printf ("%u;%u;%llu;%llu;%llu;%llu\n",mlp->c_p, mlp->addr_in_problem,
318                 (long long unsigned int) ctx.lp_duration.rel_value, (long long unsigned int) ctx.mlp_duration.rel_value,
319                 (long long unsigned int) uctx.lp_duration.rel_value, (long long unsigned int) uctx.mlp_duration.rel_value);
320           else
321             GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Updated problem solved for %u peers with %u address successfully (Initial: LP/MLP: %llu/%llu ms, Update: %llu/%llu ms)\n",
322                 mlp->c_p, mlp->addr_in_problem,
323                 (long long unsigned int) ctx.lp_duration.rel_value, (long long unsigned int) ctx.mlp_duration.rel_value,
324                 (long long unsigned int) uctx.lp_duration.rel_value, (long long unsigned int) uctx.mlp_duration.rel_value);
325         }
326         else
327           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Solving updated problem with %u peers and %u addresses failed\n", c, c2);
328       }
329     }
330   }
331
332   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
333     GNUNET_SCHEDULER_cancel(shutdown_task);
334   shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
335
336 }
337
338
339 int
340 main (int argc, char *argv[])
341 {
342   /* Init invalid */
343   update_percentage = UINT_MAX;
344   static struct GNUNET_GETOPT_CommandLineOption options[] = {
345     {'a', "addresses", NULL,
346      gettext_noop ("addresses per peer"), 1,
347      &GNUNET_GETOPT_set_uint, &addresses},
348     {'p', "peers", NULL,
349      gettext_noop ("peers"), 1,
350      &GNUNET_GETOPT_set_uint, &peers},
351      {'n', "numeric", NULL,
352       gettext_noop ("numeric output only"), 0,
353       &GNUNET_GETOPT_set_one, &numeric},
354     {'e', "end", NULL,
355      gettext_noop ("end solving problem"), 1,
356      &GNUNET_GETOPT_set_uint, &end},
357    {'s', "start", NULL,
358     gettext_noop ("start solving problem"), 1,
359     &GNUNET_GETOPT_set_uint, &start},
360     {'u', "update", NULL,
361      gettext_noop ("benchmark with existing solution (address updates)"), 1,
362      &GNUNET_GETOPT_set_uint, &update_percentage},
363     GNUNET_GETOPT_OPTION_END
364   };
365
366
367   GNUNET_PROGRAM_run (argc, argv,
368                       "perf_ats_mlp", "nohelp", options,
369                       &check, NULL);
370
371
372   return ret;
373 }
374
375 /* end of file perf_ats_mlp.c */