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