- fixes
[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
41 static unsigned int peers;
42 static unsigned int addresses;
43 static unsigned int numeric;
44 static unsigned int updates;
45
46 static int start;
47 static int end;
48
49 struct PeerContext *p;
50 struct ATS_Address *a;
51
52 static int ret;
53
54 struct GNUNET_CONTAINER_MultiHashMap * amap;
55
56 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
115 static void
116 check (void *cls, char *const *args, const char *cfgfile,
117        const struct GNUNET_CONFIGURATION_Handle *cfg)
118 {
119   unsigned int c = 0;
120   unsigned int c2 = 0;
121   unsigned int ca = 0;
122
123 #if !HAVE_LIBGLPK
124   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
125   ret = 1;
126   return;
127 #endif
128
129   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up %u peers with %u addresses per peer\n", peers, addresses);
130
131   mlp = GAS_mlp_init (cfg, NULL, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
132   if (NULL == mlp)
133   {
134     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to init MLP\n");
135     ret = 1;
136     if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
137       GNUNET_SCHEDULER_cancel(shutdown_task);
138     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
139   }
140
141   if (peers == 0)
142     peers = DEF_PEERS;
143   if (addresses == 0)
144     addresses = DEF_ADDRESSES_PER_PEER;
145
146   p = GNUNET_malloc (peers * sizeof (struct ATS_Peer));
147   a = GNUNET_malloc (peers * addresses * sizeof (struct ATS_Address));
148
149   amap = GNUNET_CONTAINER_multihashmap_create(addresses * peers);
150
151   mlp->auto_solve = GNUNET_NO;
152   if (start == 0)
153       start = 0;
154   if (end == 0)
155       end = -1;
156   if ((start != -1) && (end != -1))
157     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Solving problem starting from %u to %u\n", start , end);
158   else
159     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Solving problem for %u peers\n", peers);
160   for (c=0; c < peers; c++)
161   {
162     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up peer %u\n", c);
163     GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_NONCE, &p[c].id.hashPubKey);
164
165     for (c2=0; c2 < addresses; c2++)
166     {
167       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up address %u for peer %u\n", c2, c);
168       /* Setting required information */
169       a[ca].mlp_information = NULL;
170       a[ca].prev = NULL;
171       a[ca].next = NULL;
172
173       /* Setting address */
174       a[ca].peer = p[c].id;
175       a[ca].plugin = strdup("test");
176       a[ca].atsp_network_type = GNUNET_ATS_NET_LOOPBACK;
177
178       a[ca].ats = GNUNET_malloc (2 * sizeof (struct GNUNET_ATS_Information));
179       a[ca].ats[0].type = GNUNET_ATS_QUALITY_NET_DELAY;
180       a[ca].ats[0].value = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 10);
181       a[ca].ats[1].type = GNUNET_ATS_QUALITY_NET_DISTANCE;
182       a[ca].ats[1].value = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 2);
183       a[ca].ats_count = 2;
184       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up address %u\n", ca);
185       GNUNET_CONTAINER_multihashmap_put (amap, &a[ca].peer.hashPubKey, &a[ca], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
186       GAS_mlp_address_update(mlp, amap, &a[ca]);
187       ca++;
188     }
189
190     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Problem contains %u peers and %u adresses\n", mlp->c_p, mlp->addr_in_problem);
191
192     if ((((start >= 0) && ((c+1) >= start)) && (c <= end)) || ((c+1) == peers))
193     {
194       GNUNET_assert ((c+1) == mlp->c_p);
195       GNUNET_assert ((c+1) * addresses == mlp->addr_in_problem);
196
197       /* Solving the problem */
198       struct GAS_MLP_SolutionContext ctx;
199
200       if (GNUNET_OK == GAS_mlp_solve_problem(mlp, &ctx))
201       {
202         GNUNET_assert (GNUNET_OK == ctx.lp_result);
203         GNUNET_assert (GNUNET_OK == ctx.mlp_result);
204         if (GNUNET_YES == numeric)
205           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);
206         else
207           GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Problem solved for %u peers with %u address successfully (LP: %llu ms / MLP: %llu ms)\n",
208               mlp->c_p, mlp->addr_in_problem, ctx.lp_duration.rel_value, ctx.mlp_duration.rel_value);
209
210       }
211       else
212         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Solving problem with %u peers and %u addresses failed\n", peers, addresses);
213     }
214   }
215
216   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
217     GNUNET_SCHEDULER_cancel(shutdown_task);
218   shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
219
220 }
221
222
223 int
224 main (int argc, char *argv[])
225 {
226
227   static struct GNUNET_GETOPT_CommandLineOption options[] = {
228     {'a', "addresses", NULL,
229      gettext_noop ("addresses per peer"), 1,
230      &GNUNET_GETOPT_set_uint, &addresses},
231     {'p', "peers", NULL,
232      gettext_noop ("peers"), 1,
233      &GNUNET_GETOPT_set_uint, &peers},
234      {'n', "numeric", NULL,
235       gettext_noop ("numeric output only"), 0,
236       &GNUNET_GETOPT_set_one, &numeric},
237     {'e', "end", NULL,
238      gettext_noop ("end solving problem"), 1,
239      &GNUNET_GETOPT_set_uint, &end},
240      {'s', "start", NULL,
241       gettext_noop ("start solving problem"), 1,
242       &GNUNET_GETOPT_set_uint, &start},
243     GNUNET_GETOPT_OPTION_END
244   };
245
246
247   GNUNET_PROGRAM_run (argc, argv,
248                       "perf_ats_mlp", "nohelp", options,
249                       &check, NULL);
250
251
252   return ret;
253 }
254
255 /* end of file perf_ats_mlp.c */