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