2 This file is part of GNUnet.
3 (C) 2010,2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file ats/perf_ats_mlp
22 * @brief performance test for the MLP solver
23 * @author Christian Grothoff
24 * @author Matthias Wachs
28 #include "gnunet_util_lib.h"
29 #include "gnunet_statistics_service.h"
30 #include "gnunet-service-ats_addresses_mlp.h"
32 #define MLP_MAX_EXEC_DURATION GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3)
33 #define MLP_MAX_ITERATIONS INT_MAX
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
41 static unsigned int peers;
42 static unsigned int addresses;
43 static unsigned int numeric;
44 static unsigned int update_percentage;
50 struct ATS_Address *a;
54 static struct GNUNET_CONTAINER_MultiHashMap * amap;
56 static struct GAS_MLP_Handle *mlp;
61 GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
65 struct GNUNET_PeerIdentity id;
78 struct GNUNET_ATS_Information *ats;
85 do_shutdown (void *cls,
86 const struct GNUNET_SCHEDULER_TaskContext *tc)
90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
100 for (ca=0; ca < (peers * addresses); ca++)
102 GNUNET_free (a[ca].plugin);
103 GNUNET_free (a[ca].ats);
108 GNUNET_CONTAINER_multihashmap_destroy(amap);
109 GNUNET_free_non_null (a);
110 GNUNET_free_non_null (p);
115 update_addresses (struct ATS_Address * a, unsigned int addrs, unsigned int percentage)
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);
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;
133 for (cur = 0; cur < addrs; cur ++)
141 cur = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, addrs);
142 if (0 == updated[cur])
144 u_type = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DEF_ATS_VALUES);
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);
152 while (a[cur].ats[u_type].value == u_val);
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);
160 while (a[cur].ats[u_type].value == u_val);
168 a[cur].ats[u_type].value = u_val;
170 GAS_mlp_address_update(mlp, amap, &a[cur]);
174 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Updated %u delay and %u distance values\n", u_types[0], u_types[1]);
179 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
181 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
182 char * entry_in = NULL;
183 char * entry_out = NULL;
184 char * quota_out_str;
188 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
193 case GNUNET_ATS_NET_UNSPECIFIED:
194 entry_out = "UNSPECIFIED_QUOTA_OUT";
195 entry_in = "UNSPECIFIED_QUOTA_IN";
197 case GNUNET_ATS_NET_LOOPBACK:
198 entry_out = "LOOPBACK_QUOTA_OUT";
199 entry_in = "LOOPBACK_QUOTA_IN";
201 case GNUNET_ATS_NET_LAN:
202 entry_out = "LAN_QUOTA_OUT";
203 entry_in = "LAN_QUOTA_IN";
205 case GNUNET_ATS_NET_WAN:
206 entry_out = "WAN_QUOTA_OUT";
207 entry_in = "WAN_QUOTA_IN";
209 case GNUNET_ATS_NET_WLAN:
210 entry_out = "WLAN_QUOTA_OUT";
211 entry_in = "WLAN_QUOTA_IN";
217 if ((entry_in == NULL) || (entry_out == NULL))
221 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, "a_out_str))
223 if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
224 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
225 out_dest[c] = UINT32_MAX;
227 GNUNET_free (quota_out_str);
228 quota_out_str = NULL;
230 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
231 out_dest[c] = UINT32_MAX;
233 out_dest[c] = UINT32_MAX;
236 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, "a_in_str))
238 if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
239 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
240 in_dest[c] = UINT32_MAX;
242 GNUNET_free (quota_in_str);
245 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
247 in_dest[c] = UINT32_MAX;
251 in_dest[c] = UINT32_MAX;
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota: %s %u, %s %u\n", entry_in, in_dest[c], entry_out, out_dest[c]);
256 return GNUNET_ATS_NetworkTypeCount;
261 check (void *cls, char *const *args, const char *cfgfile,
262 const struct GNUNET_CONFIGURATION_Handle *cfg)
267 int update = GNUNET_NO;
268 int range = GNUNET_NO;
270 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
271 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
272 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
276 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up %u peers with %u addresses per peer\n", peers, addresses);
283 quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
284 mlp = GAS_mlp_init (cfg, NULL, quotas, quotas_in, quotas_out, quota_count);
287 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to init MLP\n");
289 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
290 GNUNET_SCHEDULER_cancel(shutdown_task);
291 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
297 addresses = DEF_ADDRESSES_PER_PEER;
299 p = GNUNET_malloc (peers * sizeof (struct ATS_Peer));
300 a = GNUNET_malloc (peers * addresses * sizeof (struct ATS_Address));
302 amap = GNUNET_CONTAINER_multihashmap_create(addresses * peers, GNUNET_NO);
304 mlp->auto_solve = GNUNET_NO;
309 if ((start != -1) && (end != -1))
311 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Solving problem starting from %u to %u\n", start , end);
315 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Solving problem for %u peers\n", peers);
317 if ((update_percentage >= 0) && (update_percentage <= 100))
320 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Benchmarking with existing presolution and %u%% updated addresses\n", update_percentage);
322 else if ((update_percentage > 100) && (update_percentage != UINT_MAX))
324 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Invalid percentage: %u\n", update_percentage);
329 for (c=0; c < peers; c++)
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up peer %u\n", c);
332 GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_NONCE, &p[c].id.hashPubKey);
334 for (c2=0; c2 < addresses; c2++)
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up address %u for peer %u\n", c2, c);
337 /* Setting required information */
338 a[ca].solver_information = NULL;
342 /* Setting address */
343 a[ca].peer = p[c].id;
344 a[ca].plugin = GNUNET_strdup("test");
345 a[ca].atsp_network_type = GNUNET_ATS_NET_LOOPBACK;
347 a[ca].ats = GNUNET_malloc (DEF_ATS_VALUES * sizeof (struct GNUNET_ATS_Information));
348 a[ca].ats[0].type = GNUNET_ATS_QUALITY_NET_DELAY;
349 a[ca].ats[0].value = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, DEF_ATS_MAX_DELAY);
350 a[ca].ats[1].type = GNUNET_ATS_QUALITY_NET_DISTANCE;
351 a[ca].ats[1].value = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, DEF_ATS_MAX_DISTANCE);
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up address %u\n", ca);
354 GNUNET_CONTAINER_multihashmap_put (amap, &a[ca].peer.hashPubKey, &a[ca], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
355 GAS_mlp_address_update(mlp, amap, &a[ca]);
359 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Problem contains %u peers and %u adresses\n", mlp->c_p, mlp->addresses_in_problem);
361 if (((GNUNET_YES == range) && (((start >= 0) && ((c+1) >= start)) && (c <= end))) || ((c+1) == peers))
363 GNUNET_assert ((c+1) == mlp->c_p);
364 GNUNET_assert ((c+1) * addresses == mlp->addresses_in_problem);
366 /* Solving the problem */
367 struct GAS_MLP_SolutionContext ctx;
369 res = GAS_mlp_solve_problem(mlp, &ctx);
371 if (GNUNET_NO == update)
373 if (GNUNET_OK == res)
375 GNUNET_assert (GNUNET_OK == ctx.lp_result);
376 GNUNET_assert (GNUNET_OK == ctx.mlp_result);
377 if (GNUNET_YES == numeric)
378 printf ("%u;%u;%llu;%llu\n",mlp->c_p, mlp->addresses_in_problem, (unsigned long long) ctx.lp_duration.rel_value, (unsigned long long) ctx.mlp_duration.rel_value);
380 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Problem solved for %u peers with %u address successfully (LP: %llu ms / MLP: %llu ms)\n",
381 mlp->c_p, mlp->addresses_in_problem, ctx.lp_duration.rel_value, ctx.mlp_duration.rel_value);
384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Solving problem with %u peers and %u addresses failed\n", c, c2);
388 struct GAS_MLP_SolutionContext uctx;
389 /* Update addresses */
390 update_addresses (a, (c+1) * c2, update_percentage);
393 res = GAS_mlp_solve_problem(mlp, &uctx);
395 if (GNUNET_OK == res)
397 GNUNET_assert (GNUNET_OK == uctx.lp_result);
398 GNUNET_assert (GNUNET_OK == uctx.mlp_result);
399 if (GNUNET_YES == numeric)
400 printf ("%u;%u;%llu;%llu;%llu;%llu\n",mlp->c_p, mlp->addresses_in_problem,
401 (unsigned long long) ctx.lp_duration.rel_value, (unsigned long long) ctx.mlp_duration.rel_value,
402 (unsigned long long) uctx.lp_duration.rel_value, (unsigned long long) uctx.mlp_duration.rel_value);
404 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",
405 mlp->c_p, mlp->addresses_in_problem,
406 (unsigned long long) ctx.lp_duration.rel_value, (unsigned long long) ctx.mlp_duration.rel_value,
407 (unsigned long long) uctx.lp_duration.rel_value, (unsigned long long) uctx.mlp_duration.rel_value);
410 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Solving updated problem with %u peers and %u addresses failed\n", c, c2);
415 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
416 GNUNET_SCHEDULER_cancel(shutdown_task);
417 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
423 main (int argc, char *const *argv)
426 update_percentage = UINT_MAX;
427 static struct GNUNET_GETOPT_CommandLineOption options[] = {
428 {'a', "addresses", NULL,
429 gettext_noop ("addresses per peer"), 1,
430 &GNUNET_GETOPT_set_uint, &addresses},
432 gettext_noop ("peers"), 1,
433 &GNUNET_GETOPT_set_uint, &peers},
434 {'n', "numeric", NULL,
435 gettext_noop ("numeric output only"), 0,
436 &GNUNET_GETOPT_set_one, &numeric},
438 gettext_noop ("end solving problem"), 1,
439 &GNUNET_GETOPT_set_uint, &end},
441 gettext_noop ("start solving problem"), 1,
442 &GNUNET_GETOPT_set_uint, &start},
443 {'u', "update", NULL,
444 gettext_noop ("benchmark with existing solution (address updates)"), 1,
445 &GNUNET_GETOPT_set_uint, &update_percentage},
446 GNUNET_GETOPT_OPTION_END
449 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
452 GNUNET_PROGRAM_run (argc, argv,
453 "perf_ats_mlp", "nohelp", options,
455 GNUNET_free ((void *) argv);
459 /* end of file perf_ats_mlp.c */