- remove statistics use return values
[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
45 struct PeerContext *p;
46 struct ATS_Address *a;
47
48 static int ret;
49
50 struct GNUNET_CONTAINER_MultiHashMap * amap;
51
52 struct GAS_MLP_Handle *mlp;
53
54
55
56
57 GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
58
59 struct PeerContext
60 {
61   struct GNUNET_PeerIdentity id;
62
63   struct Address *addr;
64 };
65
66 struct Address
67 {
68   char *plugin;
69   size_t plugin_len;
70
71   void *addr;
72   size_t addr_len;
73
74   struct GNUNET_ATS_Information *ats;
75   int ats_count;
76
77   void *session;
78 };
79
80 void
81 do_shutdown (void *cls,
82              const struct GNUNET_SCHEDULER_TaskContext *tc)
83 {
84   unsigned int ca;
85
86   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
87
88   if (NULL != mlp)
89   {
90     GAS_mlp_done (mlp);
91     mlp = NULL;
92   }
93
94   if (NULL != a)
95   {
96     for (ca=0; ca < (peers * addresses); ca++)
97     {
98       GNUNET_free (a[ca].plugin);
99       GNUNET_free (a[ca].ats);
100     }
101   }
102
103   if (NULL != amap)
104     GNUNET_CONTAINER_multihashmap_destroy(amap);
105   GNUNET_free_non_null (a);
106   GNUNET_free_non_null (p);
107
108 }
109
110
111 static void
112 check (void *cls, char *const *args, const char *cfgfile,
113        const struct GNUNET_CONFIGURATION_Handle *cfg)
114 {
115   unsigned int c = 0;
116   unsigned int c2 = 0;
117   unsigned int ca = 0;
118
119 #if !HAVE_LIBGLPK
120   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
121   ret = 1;
122   return;
123 #endif
124
125   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up %u peers with %u addresses per peer\n", peers, addresses);
126
127   mlp = GAS_mlp_init (cfg, NULL, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
128   if (NULL == mlp)
129   {
130     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to init MLP\n");
131     ret = 1;
132     if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
133       GNUNET_SCHEDULER_cancel(shutdown_task);
134     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
135   }
136
137   if (peers == 0)
138     peers = DEF_PEERS;
139   if (addresses == 0)
140     addresses = DEF_ADDRESSES_PER_PEER;
141
142   p = GNUNET_malloc (peers * sizeof (struct ATS_Peer));
143   a = GNUNET_malloc (peers * addresses * sizeof (struct ATS_Address));
144
145   amap = GNUNET_CONTAINER_multihashmap_create(addresses * peers);
146
147   mlp->auto_solve = GNUNET_NO;
148   for (c=0; c < peers; c++)
149   {
150     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up peer %u\n", c);
151     GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_NONCE, &p[c].id.hashPubKey);
152
153     for (c2=0; c2 < addresses; c2++)
154     {
155       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up address %u for peer %u\n", c2, c);
156       /* Setting required information */
157       a[ca].mlp_information = NULL;
158       a[ca].prev = NULL;
159       a[ca].next = NULL;
160
161       /* Setting address */
162       a[ca].peer = p[c].id;
163       a[ca].plugin = strdup("test");
164       a[ca].atsp_network_type = GNUNET_ATS_NET_LOOPBACK;
165
166       a[ca].ats = GNUNET_malloc (2 * sizeof (struct GNUNET_ATS_Information));
167       a[ca].ats[0].type = GNUNET_ATS_QUALITY_NET_DELAY;
168       a[ca].ats[0].value = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 10);
169       a[ca].ats[1].type = GNUNET_ATS_QUALITY_NET_DISTANCE;
170       a[ca].ats[1].value = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 2);
171       a[ca].ats_count = 2;
172       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up address %u\n", ca);
173       GNUNET_CONTAINER_multihashmap_put (amap, &a[ca].peer.hashPubKey, &a[ca], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
174       GAS_mlp_address_update(mlp, amap, &a[ca]);
175       ca++;
176     }
177   }
178   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Problem contains %u peers and %u adresses\n", mlp->c_p, mlp->addr_in_problem);
179
180   GNUNET_assert (peers == mlp->c_p);
181   GNUNET_assert (peers * addresses == mlp->addr_in_problem);
182
183   /* Solving the problem */
184   struct GAS_MLP_SolutionContext ctx;
185
186   if (GNUNET_OK == GAS_mlp_solve_problem(mlp, &ctx))
187   {
188     GNUNET_assert (GNUNET_OK == ctx.lp_result);
189     GNUNET_assert (GNUNET_OK == ctx.mlp_result);
190     if (GNUNET_YES == numeric)
191       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);
192     else
193       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Problem solved successfully (LP: %llu ms / MLP: %llu ms)\n", ctx.lp_duration.rel_value, ctx.mlp_duration.rel_value);
194
195   }
196   else
197     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Solving problem with %u peers and %u addresses failed\n", peers, addresses);
198   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
199     GNUNET_SCHEDULER_cancel(shutdown_task);
200   shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
201
202 }
203
204
205 int
206 main (int argc, char *argv[])
207 {
208
209   static struct GNUNET_GETOPT_CommandLineOption options[] = {
210     {'a', "addresses", NULL,
211      gettext_noop ("addresses per peer"), 1,
212      &GNUNET_GETOPT_set_uint, &addresses},
213     {'p', "peers", NULL,
214      gettext_noop ("peers"), 1,
215      &GNUNET_GETOPT_set_uint, &peers},
216      {'n', "numeric", NULL,
217       gettext_noop ("numeric output only"), 0,
218       &GNUNET_GETOPT_set_one, &numeric},
219     GNUNET_GETOPT_OPTION_END
220   };
221
222
223   GNUNET_PROGRAM_run (argc, argv,
224                       "perf_ats_mlp", "nohelp", options,
225                       &check, NULL);
226
227
228   return ret;
229 }
230
231 /* end of file perf_ats_mlp.c */