changing solver api: remove address hashmap from functions and pass instead with...
[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
28 /*
29      This file is part of GNUnet.
30      (C) 2010,2011 Christian Grothoff (and other contributing authors)
31
32      GNUnet is free software; you can redistribute it and/or modify
33      it under the terms of the GNU General Public License as published
34      by the Free Software Foundation; either version 3, or (at your
35      option) any later version.
36
37      GNUnet is distributed in the hope that it will be useful, but
38      WITHOUT ANY WARRANTY; without even the implied warranty of
39      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
40      General Public License for more details.
41
42      You should have received a copy of the GNU General Public License
43      along with GNUnet; see the file COPYING.  If not, write to the
44      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
45      Boston, MA 02111-1307, USA.
46 */
47 /**
48  * @file ats/test_ats_mlp.c
49  * @brief basic test for the MLP solver
50  * @author Christian Grothoff
51  * @author Matthias Wachs
52
53  */
54 #include "platform.h"
55 #include "gnunet_util_lib.h"
56 #include "gnunet_statistics_service.h"
57 #include "gnunet_ats_service.h"
58 #include "gnunet-service-ats-solver_mlp.h"
59 #include "gnunet-service-ats_normalization.h"
60 #include "test_ats_api_common.h"
61
62 #define PEERS_START 100
63 #define PEERS_END       100
64
65 #define ADDRESSES 10
66
67 int count_p;
68 int count_a;
69
70
71 struct PerfPeer
72 {
73         struct GNUNET_PeerIdentity id;
74
75         struct ATS_Address *head;
76         struct ATS_Address *tail;
77 };
78
79 static int ret;
80 static int opt_numeric;
81 static int opt_dump;
82 static int opt_update_percent;
83 static int opt_update_quantity;
84
85 static int N_peers_start;
86 static int N_peers_end;
87 static int N_address;
88
89 /**
90  * Statistics handle
91  */
92 struct GNUNET_STATISTICS_Handle * stats;
93
94 /**
95  * MLP solver handle
96  */
97 struct GAS_MLP_Handle *mlp;
98
99 /**
100  * Hashmap containing addresses
101  */
102 struct GNUNET_CONTAINER_MultiHashMap * addresses;
103
104 #define ATS_COUNT 2
105 struct GNUNET_ATS_Information ats[2];
106
107 struct PerfPeer *peers;
108
109 static void
110 end_now (int res)
111 {
112   if (NULL != stats)
113   {
114           GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
115           stats = NULL;
116   }
117   /*
118   if (NULL != addresses)
119   {
120                 GNUNET_CONTAINER_multihashmap_iterate (addresses, &addr_it, NULL);
121                 GNUNET_CONTAINER_multihashmap_destroy (addresses);
122                 addresses = NULL ;
123   }*/
124   if (NULL != peers)
125         {
126                 GNUNET_free (peers);
127         }
128   if (NULL != mlp)
129   {
130                 GAS_mlp_done (mlp);
131                 mlp = NULL;
132   }
133   GAS_normalization_stop();
134         ret = res;
135 }
136
137
138 static void
139 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
140 {
141
142 }
143
144 static void
145 normalized_property_changed_cb (void *cls,
146                                                                                                                  const struct ATS_Address *peer,
147                                                                                                                  uint32_t type,
148                                                                                                                  double prop_rel)
149 {
150          /* TODO */
151 }
152
153 const double *
154 get_property_cb (void *cls, const struct ATS_Address *address)
155 {
156         return GAS_normalization_get_properties ((struct ATS_Address *) address);
157 }
158
159
160 static const double *
161 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
162 {
163         return GAS_normalization_get_preferences (id);
164 }
165
166
167 static void
168 perf_create_peer (int cp)
169 {
170         GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &peers[cp].id.hashPubKey);
171         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating peer #%u: %s \n", cp, GNUNET_i2s (&peers[cp].id));
172 }
173
174 static struct ATS_Address *
175 perf_create_address (int cp, int ca)
176 {
177         struct ATS_Address *a;
178         a = create_address (&peers[cp].id, "Test 1", "test 1", strlen ("test 1") + 1, 0);
179         GNUNET_CONTAINER_DLL_insert (peers[cp].head, peers[cp].tail, a);
180         GNUNET_CONTAINER_multihashmap_put (addresses, &peers[cp].id.hashPubKey, a, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
181         return a;
182 }
183
184 static void
185 address_initial_update (void *solver, struct GNUNET_CONTAINER_MultiHashMap * addresses, struct ATS_Address *address)
186 {
187         ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
188         ats[0].value = htonl (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 100));
189
190         ats[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
191         ats[1].value = htonl (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 10));
192
193         GAS_mlp_address_update (mlp, address, 0, GNUNET_YES, ats, 2);
194 }
195
196
197 static void
198 update_single_addresses (struct ATS_Address *cur)
199 {
200         int r_type;
201         int r_val;
202
203         r_type = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 2);
204         switch (r_type) {
205                 case 0:
206                         r_val = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 100);
207                         ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
208                         ats[0].value = htonl (r_val);
209                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s' address %p type %s val %u\n",
210                                         GNUNET_i2s (&cur->peer), cur,
211                                         "GNUNET_ATS_QUALITY_NET_DELAY", r_val);
212                         break;
213                 case 1:
214                         r_val = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 10);
215                         ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
216                         ats[0].value = htonl (r_val);
217                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s' address %p type %s val %u\n",
218                                         GNUNET_i2s (&cur->peer), cur,
219                                         "GNUNET_ATS_QUALITY_NET_DISTANCE", r_val);
220                         break;
221                 default:
222                         break;
223         }
224         GAS_mlp_address_update (mlp, cur, 0, GNUNET_YES, ats, 1);
225 }
226
227 static void
228 update_addresses (unsigned int cp, unsigned int ca, unsigned int up_q)
229 {
230         struct ATS_Address *cur;
231         int c_peer;
232         int c_select;
233         int c_addr;
234         int r;
235
236         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating addresses %u addresses per peer \n", up_q);
237         unsigned int m [ca];
238
239         for (c_peer = 0; c_peer < cp; c_peer++)
240         {
241                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s'\n", GNUNET_i2s (&peers[c_peer].id));
242                         for (c_select = 0; c_select < ca; c_select++)
243                                 m[c_select] = 0;
244                         c_select = 0;
245                         while (c_select < opt_update_quantity)
246                         {
247                                         r = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, ca);
248                                         if (0 == m[r])
249                                         {
250                                                 m[r] = 1;
251                                                 c_select++;
252                                         }
253                         }
254
255                         c_addr = 0;
256                         for (cur = peers[c_peer].head; NULL != cur; cur = cur->next)
257                         {
258                                         if (1 == m[c_addr])
259                                                         update_single_addresses (cur);
260                                         c_addr ++;
261                         }
262         }
263 }
264
265
266 static void
267 check (void *cls, char *const *args, const char *cfgfile,
268        const struct GNUNET_CONFIGURATION_Handle *cfg)
269 {
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];
273         int cp;
274         int ca;
275         struct ATS_Address * cur_addr;
276
277         int full_lp_res;
278         int full_mip_res;
279         int full_lp_presolv;
280         int full_mip_presolv;
281         struct GNUNET_TIME_Relative full_build_dur;
282         struct GNUNET_TIME_Relative full_lp_dur;
283         struct GNUNET_TIME_Relative full_mip_dur;
284
285         int update_lp_res;
286         int update_mip_res;
287         int update_lp_presolv;
288         int update_mip_presolv;
289         struct GNUNET_TIME_Relative update_build_dur;
290         struct GNUNET_TIME_Relative update_lp_dur;
291         struct GNUNET_TIME_Relative update_mip_dur;
292
293   stats = GNUNET_STATISTICS_create("ats", cfg);
294   if (NULL == stats)
295   {
296         GNUNET_break (0);
297     end_now (1);
298     return;
299   }
300   GAS_normalization_start (NULL, NULL, &normalized_property_changed_cb, NULL);
301   /* Load quotas */
302   if (GNUNET_ATS_NetworkTypeCount != load_quotas (cfg, quotas_out, quotas_in,
303                         GNUNET_ATS_NetworkTypeCount))
304   {
305         GNUNET_break (0);
306       end_now (1);
307       return;
308   }
309
310   GNUNET_assert (N_peers_end >= N_peers_start);
311   GNUNET_assert (N_address >= 0);
312
313   fprintf (stderr, "Solving problem for %u..%u peers with %u addresses\n",
314                 N_peers_start, N_peers_end, N_address);
315
316   count_p = N_peers_end;
317   count_a = N_address;
318   peers = GNUNET_malloc ((count_p) * sizeof (struct PerfPeer));
319   /* Setup address hashmap */
320   addresses = GNUNET_CONTAINER_multihashmap_create (N_address, GNUNET_NO);
321
322   /* Init MLP solver */
323   mlp  = GAS_mlp_init (cfg, stats, addresses,
324                 quotas, quotas_out, quotas_in,
325                 GNUNET_ATS_NetworkTypeCount, &bandwidth_changed_cb, NULL,
326                 &get_preferences_cb, NULL,
327                 &get_property_cb, NULL);
328   if (NULL == mlp)
329   {
330         GNUNET_break (0);
331       end_now (1);
332       return;
333   }
334   mlp->mlp_auto_solve = GNUNET_NO;
335   mlp->write_mip_mps = opt_dump;
336   mlp->write_mip_sol = opt_dump;
337
338         for (cp = 0; cp < count_p; cp++)
339                         perf_create_peer (cp);
340
341         if (GNUNET_YES == opt_numeric)
342                 fprintf (stderr, "#peers;#addresses per peer;LP/MIP state;presolv;exec build in ms;exec LP in ms; exec MIP in ms;#cols;#rows;#nonzero elements\n");
343
344         for (cp = 0; cp < count_p; cp++)
345         {
346                         for (ca = 0; ca < count_a; ca++)
347                         {
348                                         cur_addr = perf_create_address(cp, ca);
349                                         /* add address */
350                                         GAS_mlp_address_add (mlp, cur_addr, GNUNET_ATS_NET_UNSPECIFIED);
351                                         address_initial_update (mlp, addresses, cur_addr);
352                                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding address for peer %u address %u: \n", cp, ca);
353                         }
354                         GAS_mlp_get_preferred_address( mlp, &peers[cp].id);
355                         /* solve */
356                         if (cp + 1 >= N_peers_start)
357                         {
358                                 /* Solve the full problem */
359                                 GAS_mlp_solve_problem (mlp);
360                                 full_lp_res = mlp->ps.lp_res;
361                                 full_mip_res = mlp->ps.mip_res;
362                                 full_lp_presolv = mlp->ps.lp_presolv;
363                                 full_mip_presolv = mlp->ps.mip_presolv;
364                                 full_build_dur = mlp->ps.build_dur;
365                                 full_lp_dur = mlp->ps.lp_dur;
366                                 full_mip_dur = mlp->ps.mip_dur;
367
368                                 /* Update and solve the problem */
369                                 if ((0 < opt_update_quantity) || (0 < opt_update_percent))
370                                 {
371                                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating problem with %u peers and %u addresses\n", cp + 1, ca);
372                                         update_addresses (cp + 1, ca, opt_update_quantity);
373                                         GAS_mlp_solve_problem (mlp);
374                                         GAS_mlp_solve_problem (mlp);
375                                         update_lp_res = mlp->ps.lp_res;
376                                         update_mip_res = mlp->ps.mip_res;
377                                         update_lp_presolv = mlp->ps.lp_presolv;
378                                         update_mip_presolv = mlp->ps.mip_presolv;
379                                         update_build_dur = mlp->ps.build_dur;
380                                         update_lp_dur = mlp->ps.lp_dur;
381                                         update_mip_dur = mlp->ps.mip_dur;
382
383                                 }
384                                 if (GNUNET_NO == opt_numeric)
385                                 {
386                                 fprintf (stderr, "Rebuild: %03u peers each %02u addresses; rebuild: LP/MIP state [%3s/%3s] presolv [%3s/%3s], (build/LP/MIP in ms) %04llu / %04llu / %04llu\n",
387                                                 cp + 1, ca,
388                                                 (GNUNET_OK == full_lp_res) ? "OK" : "FAIL",
389                                                 (GNUNET_OK == full_mip_res) ? "OK" : "FAIL",
390                                                 (GLP_YES == full_lp_presolv) ? "YES" : "NO",
391                                                 (GNUNET_OK == full_mip_presolv) ? "YES" : "NO",
392                                                 (unsigned long long) full_build_dur.rel_value, (unsigned long long) full_lp_dur.rel_value, (unsigned long long) full_mip_dur.rel_value);
393                                 if ((0 < opt_update_quantity) || (0 < opt_update_percent))
394                                         fprintf (stderr, "Update : %03u peers each %02u addresses; rebuild: LP/MIP state [%3s/%3s] presolv [%3s/%3s], (build/LP/MIP in ms) %04llu / %04llu / %04llu\n",
395                                                 cp + 1, ca,
396                                                 (GNUNET_OK == update_lp_res) ? "OK" : "FAIL",
397                                                 (GNUNET_OK == update_mip_res) ? "OK" : "FAIL",
398                                                 (GLP_YES == update_lp_presolv) ? "YES" : "NO",
399                                                 (GNUNET_OK == update_mip_presolv) ? "YES" : "NO",
400                                                 (unsigned long long) update_build_dur.rel_value, (unsigned long long) update_lp_dur.rel_value, (unsigned long long) update_mip_dur.rel_value);
401                                 }
402                                 else
403                                 {
404                                                 fprintf (stderr, "Rebuild;%u;%u;%s;%s;%s;%s;%04llu;%04llu;%04llu\n",
405                                                                 cp + 1, ca,
406                                                                 (GNUNET_OK == full_lp_res) ? "OK" : "FAIL",
407                                                                 (GNUNET_OK == full_mip_res) ? "OK" : "FAIL",
408                                                                 (GLP_YES == full_lp_presolv) ? "YES" : "NO",
409                                                                 (GNUNET_OK == full_mip_presolv) ? "YES" : "NO",
410                                                                 (unsigned long long) full_build_dur.rel_value, (unsigned long long) full_lp_dur.rel_value, (unsigned long long) full_mip_dur.rel_value);
411                                                 if ((0 < opt_update_quantity) || (0 < opt_update_percent))
412                                                         fprintf (stderr, "Update;%u;%u;%s;%s;%s;%s;%04llu;%04llu;%04llu\n",
413                                                                         cp + 1, ca,
414                                                                         (GNUNET_OK == update_lp_res) ? "OK" : "FAIL",
415                                                                         (GNUNET_OK == update_mip_res) ? "OK" : "FAIL",
416                                                                         (GLP_YES == update_lp_presolv) ? "YES" : "NO",
417                                                                         (GNUNET_OK == update_mip_presolv) ? "YES" : "NO",
418                                                                         (unsigned long long) update_build_dur.rel_value, (unsigned long long) update_lp_dur.rel_value, (unsigned long long) update_mip_dur.rel_value);
419                                 }
420                         }
421         }
422
423
424         struct ATS_Address *cur;
425         struct ATS_Address *next;
426         for (cp = 0; cp < count_p; cp++)
427         {
428                         for (cur = peers[cp].head; cur != NULL; cur = next)
429                         {
430                                         GAS_mlp_address_delete (mlp, cur, GNUNET_NO);
431                                         next = cur->next;
432                                         GNUNET_CONTAINER_DLL_remove (peers[cp].head, peers[cp].tail, cur);
433                                         GNUNET_free (cur);
434                         }
435
436         }
437         GNUNET_free (peers);
438
439 }
440
441
442 int
443 main (int argc, char *argv[])
444 {
445
446   static char *const argv2[] = { "perf_ats_mlp",
447     "-c",
448     "test_ats_mlp.conf",
449     "-L", "WARNING",
450     NULL
451   };
452
453   opt_dump = GNUNET_NO;
454   opt_update_quantity = 0;
455   opt_update_percent = 0;
456
457   N_peers_start = 0;
458   N_peers_end = 0;
459   N_address = 0;
460   int c;
461   for (c = 0; c < argc; c++)
462   {
463                 if ((0 == strcmp (argv[c], "-z")) && (c < (argc - 1)))
464                 {
465                                 if (0 != atoi(argv[c+1]))
466                                 {
467                                                 N_peers_start = atoi(argv[c+1]);
468                                 }
469                 }
470                 if ((0 == strcmp (argv[c], "-x")) && (c < (argc - 1)))
471                 {
472                                 if (0 != atoi(argv[c+1]))
473                                 {
474                                                 N_peers_end = atoi(argv[c+1]);
475                                 }
476                 }
477                 if ((0 == strcmp (argv[c], "-c")) && (c < (argc - 1)))
478                 {
479                                 if (0 != atoi(argv[c+1]))
480                                 {
481                                                 N_address = atoi(argv[c+1]);
482                                 }
483                 }
484                 if ((0 == strcmp (argv[c], "-n")))
485                 {
486                                 opt_numeric = GNUNET_YES;
487                 }
488                 if ((0 == strcmp (argv[c], "-d")))
489                 {
490                                 opt_dump = GNUNET_YES;
491                 }
492                 if ((0 == strcmp (argv[c], "-p")) && (c < (argc - 1)))
493                 {
494                                 if (0 != atoi(argv[c+1]))
495                                 {
496                                         /* Update a fix "p"ercentage of addresses */
497                                                 opt_update_percent = atoi(argv[c+1]);
498                                                 if ((0 <= opt_update_percent) && (100 <= opt_update_percent))
499                                                 {
500                                                         fprintf (stderr, _("Percentage has to be: 0 <= p <= 100 "));
501                                                         exit (1);
502                                                 }
503                                 }
504                 }
505                 if ((0 == strcmp (argv[c], "-q")) && (c < (argc - 1)))
506                 {
507                                 if (0 != atoi(argv[c+1]))
508                                 {
509                                         /* Update a fix "q"uantity of addresses */
510                                                 opt_update_quantity = atoi(argv[c+1]);
511                                                 if (0 >= opt_update_quantity)
512                                                 {
513                                                         fprintf (stderr, _("Quantity has to be:  p => 0 "));
514                                                         exit (1);
515                                                 }
516                                 }
517                 }
518   }
519
520   if ((0 == N_peers_start) && (0 == N_peers_end))
521   {
522                 N_peers_start = PEERS_START;
523                 N_peers_end = PEERS_END;
524   }
525   if (0 == N_address)
526                 N_address = ADDRESSES;
527
528   if (opt_update_quantity > N_address)
529   {
530                 fprintf (stderr, _("Trying to update more addresses than we have per peer! (%u vs %u)"), opt_update_quantity, N_address);
531                 exit (1);
532   }
533
534   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
535     GNUNET_GETOPT_OPTION_END
536   };
537
538
539   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
540                       "perf_ats_mlp", "nohelp", options,
541                       &check, NULL);
542
543
544   return ret;
545 }
546
547 /* end of file perf_ats_mlp.c */