623f187eac0f6eae1f418db6b6a0bd1d12335a85
[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_STATISTICS_Handle * stats;
51
52 struct GNUNET_CONTAINER_MultiHashMap * amap;
53
54 struct GAS_MLP_Handle *mlp;
55
56 struct GNUNET_STATISTICS_Handle * stats;
57
58 struct GNUNET_OS_Process *stats_proc;
59
60 GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
61
62 struct PeerContext
63 {
64   struct GNUNET_PeerIdentity id;
65
66   struct Address *addr;
67 };
68
69 struct Address
70 {
71   char *plugin;
72   size_t plugin_len;
73
74   void *addr;
75   size_t addr_len;
76
77   struct GNUNET_ATS_Information *ats;
78   int ats_count;
79
80   void *session;
81 };
82
83 void
84 do_shutdown (void *cls,
85              const struct GNUNET_SCHEDULER_TaskContext *tc)
86 {
87   unsigned int ca;
88
89   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
90
91   if (NULL != mlp)
92   {
93     GAS_mlp_done (mlp);
94     mlp = NULL;
95   }
96
97   if (NULL != a)
98   {
99     for (ca=0; ca < (peers * addresses); ca++)
100     {
101       GNUNET_free (a[ca].plugin);
102       GNUNET_free (a[ca].ats);
103     }
104   }
105
106   if (NULL != amap)
107     GNUNET_CONTAINER_multihashmap_destroy(amap);
108   GNUNET_free_non_null (a);
109   GNUNET_free_non_null (p);
110   if (stats != NULL)
111     GNUNET_STATISTICS_destroy (stats,GNUNET_NO);
112
113   if (NULL != stats_proc)
114   {
115     if (0 != GNUNET_OS_process_kill (stats_proc, SIGTERM))
116       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
117     if (GNUNET_OS_process_wait (stats_proc) != GNUNET_OK)
118       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
119     GNUNET_OS_process_close (stats_proc);
120     stats_proc = NULL;
121   }
122 }
123
124 int stat_lp_it (void *cls, const char *subsystem,
125                                            const char *name, uint64_t value,
126                                            int is_persistent)
127 {
128   static long long unsigned lp_time;
129   static long long unsigned mlp_time;
130   static long long unsigned lp_time_set;
131   static long long unsigned mlp_time_set;
132
133   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received: `%s' : %u \n", name, value);
134
135   if (0 == strcmp (name, "# LP execution time (ms)"))
136   {
137     lp_time = value;
138     lp_time_set = GNUNET_YES;
139   }
140   if (0 == strcmp (name, "# MLP execution time (ms)"))
141   {
142     mlp_time = value;
143     mlp_time_set = GNUNET_YES;
144   }
145
146   if ((GNUNET_YES == lp_time_set) && (GNUNET_YES == mlp_time_set))
147   {
148     if (GNUNET_YES == numeric)
149       printf ("%u;%u;%llu;%llu\n",peers, addresses, lp_time, mlp_time);
150     if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
151       GNUNET_SCHEDULER_cancel(shutdown_task);
152     shutdown_task = GNUNET_SCHEDULER_add_now(&do_shutdown, NULL);
153   }
154   return GNUNET_OK;
155 }
156
157 int stat_ready_it (void *cls, const char *subsystem,
158                                            const char *name, uint64_t value,
159                                            int is_persistent)
160 {
161   unsigned int c = 0;
162   unsigned int c2 = 0;
163   unsigned int ca = 0;
164   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
165
166   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Statistics service ready\n");
167
168   GNUNET_STATISTICS_watch (stats, "ats", "# LP execution time (ms)", &stat_lp_it, NULL);
169   GNUNET_STATISTICS_watch (stats, "ats", "# MLP execution time (ms)", &stat_lp_it, NULL);
170
171   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up %u peers with %u addresses per peer\n", peers, addresses);
172
173   mlp = GAS_mlp_init (cfg, stats, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
174   if (NULL == mlp)
175   {
176     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to init MLP\n");
177     ret = 1;
178     if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
179       GNUNET_SCHEDULER_cancel(shutdown_task);
180     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
181     return GNUNET_NO;
182   }
183
184   if (peers == 0)
185     peers = DEF_PEERS;
186   if (addresses == 0)
187     addresses = DEF_ADDRESSES_PER_PEER;
188
189   p = GNUNET_malloc (peers * sizeof (struct ATS_Peer));
190   a = GNUNET_malloc (peers * addresses * sizeof (struct ATS_Address));
191
192   amap = GNUNET_CONTAINER_multihashmap_create(addresses * peers);
193
194   mlp->auto_solve = GNUNET_NO;
195   for (c=0; c < peers; c++)
196   {
197     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up peer %u\n", c);
198     GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p[c].id.hashPubKey);
199
200     for (c2=0; c2 < addresses; c2++)
201     {
202       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up address %u for peer %u\n", c2, c);
203       /* Setting required information */
204       a[ca].mlp_information = NULL;
205       a[ca].prev = NULL;
206       a[ca].next = NULL;
207
208       /* Setting address */
209       a[ca].peer = p[c].id;
210       a[ca].plugin = strdup("test");
211       a[ca].atsp_network_type = GNUNET_ATS_NET_LOOPBACK;
212
213       a[ca].ats = GNUNET_malloc (2 * sizeof (struct GNUNET_ATS_Information));
214       a[ca].ats[0].type = GNUNET_ATS_QUALITY_NET_DELAY;
215       a[ca].ats[0].value = 20;
216       a[ca].ats[1].type = GNUNET_ATS_QUALITY_NET_DISTANCE;
217       a[ca].ats[1].value = 2;
218       a[ca].ats_count = 2;
219       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up address %u\n", ca);
220       GNUNET_CONTAINER_multihashmap_put (amap, &a[ca].peer.hashPubKey, &a[ca], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
221       GAS_mlp_address_update(mlp, amap, &a[ca]);
222       ca++;
223     }
224   }
225   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Problem contains %u peers and %u adresses\n", mlp->c_p, mlp->addr_in_problem);
226
227   GNUNET_assert (peers == mlp->c_p);
228   GNUNET_assert (peers * addresses == mlp->addr_in_problem);
229
230   /* Solving the problem */
231   if (GNUNET_OK == GAS_mlp_solve_problem(mlp))
232     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Problem solved successfully \n");
233   else
234     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Solving problem with %u peers and %u addresses failed\n", peers, addresses);
235
236   GNUNET_log ( GNUNET_ERROR_TYPE_WARNING, "Waiting for statistics\n");
237
238
239   return GNUNET_OK;
240 }
241
242 static void
243 check (void *cls, char *const *args, const char *cfgfile,
244        const struct GNUNET_CONFIGURATION_Handle *cfg)
245 {
246 #if !HAVE_LIBGLPK
247   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
248   ret = 1;
249   return;
250 #endif
251
252   stats_proc = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-statistics",
253         "gnunet-service-statistics", NULL);
254
255   if (NULL == stats_proc)
256   {
257     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start statistics service \n");
258     ret = 1;
259     if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
260       GNUNET_SCHEDULER_cancel(shutdown_task);
261     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
262     return;
263   }
264
265   stats = GNUNET_STATISTICS_create("ats", cfg);
266
267   GNUNET_STATISTICS_watch (stats, "ats", "watch", &stat_ready_it, (void *) cfg);
268   GNUNET_STATISTICS_set(stats, "watch", 1, GNUNET_NO);
269   return;
270 }
271
272
273 int
274 main (int argc, char *argv[])
275 {
276
277   static struct GNUNET_GETOPT_CommandLineOption options[] = {
278     {'a', "addresses", NULL,
279      gettext_noop ("addresses per peer"), 1,
280      &GNUNET_GETOPT_set_uint, &addresses},
281     {'p', "peers", NULL,
282      gettext_noop ("peers"), 1,
283      &GNUNET_GETOPT_set_uint, &peers},
284      {'n', "numeric", NULL,
285       gettext_noop ("numeric output only"), 0,
286       &GNUNET_GETOPT_set_one, &numeric},
287     GNUNET_GETOPT_OPTION_END
288   };
289
290
291   GNUNET_PROGRAM_run (argc, argv,
292                       "perf_ats_mlp", "nohelp", options,
293                       &check, NULL);
294
295
296   return ret;
297 }
298
299 /* end of file perf_ats_mlp.c */