Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove gras from the main documentation
[simgrid.git] / examples / amok / bandwidth / bandwidth.c
1 /* bandwidth - bandwidth test demo of GRAS features                         */
2
3 /* Copyright (c) 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9
10 #include "gras.h"
11 #include "amok/bandwidth.h"
12 #include "amok/peermanagement.h"
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(Bandwidth,
15                              "Messages specific to this example");
16
17 /* **********************************************************************
18  * Sensor code
19  * **********************************************************************/
20
21 static xbt_socket_t try_gras_socket_client_from_string(const char *host)
22 {
23   volatile xbt_socket_t sock = NULL;
24   xbt_ex_t e;
25   TRY {
26     sock = gras_socket_client_from_string(host);
27   }
28   CATCH(e) {
29     xbt_ex_free(e);
30   }
31   return sock;
32 }
33
34 /* Function prototypes */
35 int sensor(int argc, char *argv[]);
36
37 int sensor(int argc, char *argv[])
38 {
39   xbt_socket_t mysock;
40   xbt_socket_t master = NULL;
41   int connection_try = 10;
42
43   gras_init(&argc, argv);
44   amok_bw_init();
45   amok_pm_init();
46
47   mysock = gras_socket_server_range(3000, 9999, 0, 0);
48   XBT_INFO("Sensor starting (on port %d)", gras_os_myport());
49   while (connection_try > 0 &&
50          !(master = try_gras_socket_client_from_string(argv[1]))) {
51     connection_try--;
52     gras_os_sleep(0.5);       /* let the master get ready */
53   }
54
55   amok_pm_group_join(master, "bandwidth");
56   amok_pm_mainloop(60);
57
58   gras_socket_close(mysock);
59   gras_socket_close(master);
60   gras_exit();
61   return 0;
62 }
63
64 /* **********************************************************************
65  * Maestro code
66  * **********************************************************************/
67
68 /* Function prototypes */
69 int maestro(int argc, char *argv[]);
70
71 int maestro(int argc, char *argv[])
72 {
73   double sec, bw;
74   int buf_size = 32 * 1024;
75   int msg_size = 512 * 1024;
76   int msg_amount = 1;
77   double min_duration = 1;
78
79   xbt_socket_t peer;
80   xbt_socket_t mysock;
81   xbt_peer_t h1, h2, h_temp;
82   xbt_dynar_t group;
83
84   gras_init(&argc, argv);
85   amok_bw_init();
86   amok_pm_init();
87
88   XBT_INFO("Maestro starting");
89   if (argc != 2) {
90     XBT_ERROR("Usage: maestro port\n");
91     return 1;
92   }
93   mysock = gras_socket_server(atoi(argv[1]));
94   group = amok_pm_group_new("bandwidth");
95   XBT_INFO("Wait for peers for 5 sec");
96   gras_msg_handleall(5);        /* friends, we're ready. Come and play */
97
98   if (xbt_dynar_length(group) < 2) {
99     amok_pm_group_shutdown("bandwidth");
100     xbt_die("Not enough peers arrived. Expected 2 got %ld",
101             xbt_dynar_length(group));
102   }
103   h1 = *(xbt_peer_t *) xbt_dynar_get_ptr(group, 0);
104   h2 = *(xbt_peer_t *) xbt_dynar_get_ptr(group, 1);
105   /* sort peers in right order to keep output right */
106   if (strcmp(h1->name, h2->name) < 0 || h1->port > h2->port) {
107     h_temp = h1;
108     h1 = h2;
109     h2 = h_temp;
110   }
111
112   XBT_INFO("Contact %s:%d", h1->name, h1->port);
113   peer = gras_socket_client(h1->name, h1->port);
114
115   XBT_INFO("Test the BW between me and one of the sensors");
116   amok_bw_test(peer, buf_size, msg_size, msg_amount, min_duration, &sec,
117                &bw);
118   XBT_INFO
119       ("Experience between me and %s:%d (initially %d msgs of %d bytes, maybe modified to fill the pipe at least %.1fs) took %f sec, achieving %f kb/s",
120        h1->name, h1->port, msg_amount, msg_size, min_duration, sec,
121        ((double) bw) / 1024.0);
122
123   XBT_INFO("Test the BW between %s:%d and %s:%d", h1->name, h1->port,
124         h2->name, h2->port);
125   amok_bw_request(h1->name, h1->port, h2->name, h2->port, buf_size,
126                   msg_size, msg_amount, min_duration, &sec, &bw);
127   XBT_INFO
128       ("Experience between %s:%d and %s:%d took took %f sec, achieving %f kb/s",
129        h1->name, h1->port, h2->name, h2->port, sec,
130        ((double) bw) / 1024.0);
131
132   /* Game is over, friends */
133   amok_pm_group_shutdown("bandwidth");
134
135   gras_socket_close(mysock);
136   gras_exit();
137   return 0;
138 }