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