aboutsummaryrefslogtreecommitdiff
path: root/src/ats-tests/gnunet-ats-sim.c
blob: c7febecc9b508c56f029707cceb42474ca9b1c4b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 This file is part of GNUnet.
 (C) 2010-2013 Christian Grothoff (and other contributing authors)

 GNUnet is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published
 by the Free Software Foundation; either version 3, or (at your
 option) any later version.

 GNUnet is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with GNUnet; see the file COPYING.  If not, write to the
 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 Boston, MA 02111-1307, USA.
 */
/**
 * @file ats-tests/gnunet-ats-sim.c
 * @brief ats traffic simulator: this tool uses the ats-test library to setup a
 * topology and generate traffic between these peers. The traffic description
 * is loaded from a experiment description file
 * @author Christian Grothoff
 * @author Matthias Wachs
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_testbed_service.h"
#include "gnunet_ats_service.h"
#include "gnunet_core_service.h"
#include "ats-testing.h"

#define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)

static struct BenchmarkPeer *masters_p;
static struct BenchmarkPeer *slaves_p;

GNUNET_SCHEDULER_TaskIdentifier timeout_task;

struct Experiment *e;
struct LoggingHandle *l;

static void
evaluate (struct GNUNET_TIME_Relative duration_total)
{
  int c_m;
  int c_s;
  unsigned int duration;
  struct BenchmarkPeer *mp;
  struct BenchmarkPartner *p;

  unsigned int kb_sent_sec;
  double kb_sent_percent;
  unsigned int kb_recv_sec;
  double kb_recv_percent;
  unsigned int rtt;


  duration = (duration_total.rel_value_us / (1000 * 1000));
  for (c_m = 0; c_m < e->num_masters; c_m++)
  {
    mp = &masters_p[c_m];
    fprintf (stderr,
        _("Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
        mp->no, mp->total_bytes_sent / 1024, duration,
        (mp->total_bytes_sent / 1024) / duration,
        mp->total_bytes_received / 1024, duration,
        (mp->total_bytes_received / 1024) / duration);

    for (c_s = 0; c_s < e->num_slaves; c_s++)
    {
      p = &mp->partners[c_s];

      kb_sent_sec = 0;
      kb_recv_sec = 0;
      kb_sent_percent = 0.0;
      kb_recv_percent = 0.0;
      rtt = 0;

      if (duration > 0)
      {
          kb_sent_sec = (p->bytes_sent / 1024) / duration;
          kb_recv_sec = (p->bytes_received / 1024) / duration;
      }

      if (mp->total_bytes_sent > 0)
          kb_sent_percent = ((double) p->bytes_sent * 100) / mp->total_bytes_sent;
      if (mp->total_bytes_received > 0)
          kb_recv_percent = ((double) p->bytes_received * 100) / mp->total_bytes_received;
      if (1000 * p->messages_sent > 0)
          rtt = p->total_app_rtt / (1000 * p->messages_sent);
      fprintf (stderr,
          "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f %%), received %u KiB/s (%.2f %%)\n",
          (mp->pref_partner == p->dest) ? '*' : ' ',
          mp->no, p->dest->no,
          kb_sent_sec, kb_sent_percent,
                  kb_recv_sec, kb_recv_percent);
      fprintf (stderr,
          "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
          (mp->pref_partner == p->dest) ? '*' : ' ',
          mp->no, p->dest->no, rtt);
    }
  }
}

static void
do_shutdown ()
{
  /* timeout */
  if (NULL != e)
  {
    GNUNET_ATS_TEST_experimentation_stop (e);
    e = NULL;
  }
  GNUNET_ATS_TEST_shutdown_topology ();
}

static void
transport_recv_cb (void *cls,
                   const struct GNUNET_PeerIdentity * peer,
                   const struct GNUNET_MessageHeader * message)
{

}

static void
log_request__cb (void *cls, const struct GNUNET_HELLO_Address *address,
    int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
    struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
    const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
{
  if (NULL != l)
    GNUNET_ATS_TEST_logging_now (l);
}

static void
experiment_done_cb (struct Experiment *e, struct GNUNET_TIME_Relative duration,int success)
{
  if (GNUNET_OK == success)
    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment done successful in %s\n",
        GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
  else
    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment failed \n");
  if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
  {
    GNUNET_SCHEDULER_cancel (timeout_task);
    timeout_task = GNUNET_SCHEDULER_NO_TASK;
  }
  /* Stop logging */
  GNUNET_ATS_TEST_logging_stop (l);
  evaluate (duration);

  /* Stop traffic generation */
  GNUNET_ATS_TEST_generate_traffic_stop_all();
  /* Clean up experiment */
  GNUNET_ATS_TEST_experimentation_stop (e);
  e = NULL;

  /* Shutdown topology */
  GNUNET_ATS_TEST_shutdown_topology ();
}

static void
episode_done_cb (struct Episode *ep)
{
  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Episode %u done\n", ep->id);
}

static void topology_setup_done (void *cls,
    struct BenchmarkPeer *masters,
    struct BenchmarkPeer *slaves)
{
  int c_m;
  int c_s;
  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Topology setup complete!\n");

  masters_p = masters;
  slaves_p = slaves;

  l = GNUNET_ATS_TEST_logging_start (GNUNET_TIME_UNIT_SECONDS, e->name,
      masters_p, e->num_masters);
  GNUNET_ATS_TEST_experimentation_run (e, &episode_done_cb, &experiment_done_cb);

  for (c_m = 0; c_m < e->num_masters; c_m++)
  {
      for (c_s = 0; c_s < e->num_slaves; c_s++)
      {
        /* Generate maximum traffic to all peers */
        GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m],
            &masters[c_m].partners[c_s],
            10000,
            GNUNET_TIME_UNIT_FOREVER_REL);
      }
  }

  timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_add (GNUNET_TIME_UNIT_MINUTES,
      e->max_duration), &do_shutdown, NULL);
}

static char *opt_exp_file;
static int opt_log;

static void
parse_args (int argc, char *argv[])
{
  int c;
  opt_exp_file = NULL;
  opt_log = GNUNET_NO;

  for (c = 0; c < argc; c++)
  {
    if ((c < (argc - 1)) && (0 == strcmp (argv[c], "-e")))
    {
      opt_exp_file = GNUNET_strdup ( argv[c + 1]);
    }
    if (0 == strcmp (argv[c], "-l"))
    {
      opt_log = GNUNET_YES;
    }
  }
}

int
main (int argc, char *argv[])
{
  GNUNET_log_setup("gnunet-ats-sim", "INFO", NULL);

  parse_args (argc, argv);
  if (NULL == opt_exp_file )
  {
    fprintf (stderr, "No experiment given...\n");
    return 1;
  }

  fprintf (stderr, "Loading experiment `%s' \n", opt_exp_file );
  e = GNUNET_ATS_TEST_experimentation_load (opt_exp_file);
  GNUNET_free (opt_exp_file);
  if (NULL == e)
  {
    fprintf (stderr, "Invalid experiment\n");
    return 1;
  }
  if (0 == e->num_episodes)
  {
    fprintf (stderr, "No episodes included\n");
    return 1;
  }

  /* Setup a topology with */
  GNUNET_ATS_TEST_create_topology ("gnunet-ats-sim", e->cfg_file,
      e->num_slaves,
      e->num_masters,
      GNUNET_NO,
      &topology_setup_done,
      NULL,
      &transport_recv_cb,
      &log_request__cb);
  return 0;
}
/* end of file gnunet-ats-sim.c */