Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix copyright headers
[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, "Messages specific to this example");
15
16 /* **********************************************************************
17  * Sensor code
18  * **********************************************************************/
19
20 /* Function prototypes */
21 int sensor(int argc, char *argv[]);
22
23 int sensor(int argc, char *argv[])
24 {
25   gras_socket_t mysock;
26   gras_socket_t master = NULL;
27   int connection_try = 10;
28   xbt_ex_t e;
29
30   gras_init(&argc, argv);
31   amok_bw_init();
32   amok_pm_init();
33
34   mysock = gras_socket_server_range(3000, 9999, 0, 0);
35   INFO1("Sensor starting (on port %d)", gras_os_myport());
36   while (connection_try > 0 && master == NULL) {
37     int connected = 0;
38     TRY {
39       master = gras_socket_client_from_string(argv[1]);
40       connected = 1;
41     } CATCH(e) {
42       xbt_ex_free(e);
43     }
44     if (!connected) {
45       connection_try--;
46       gras_os_sleep(0.5);       /* let the master get ready */
47     }
48   }
49
50   amok_pm_group_join(master, "bandwidth");
51   amok_pm_mainloop(60);
52
53   gras_socket_close(mysock);
54   gras_socket_close(master);
55   gras_exit();
56   return 0;
57 }
58
59 /* **********************************************************************
60  * Maestro code
61  * **********************************************************************/
62
63 /* Function prototypes */
64 int maestro(int argc, char *argv[]);
65
66 int maestro(int argc, char *argv[])
67 {
68   double sec, bw;
69   int buf_size = 32 * 1024;
70   int msg_size = 512 * 1024;
71   int msg_amount = 1;
72   double min_duration = 1;
73
74   gras_socket_t peer;
75   gras_socket_t mysock;
76   xbt_peer_t h1, h2, h_temp;
77   xbt_dynar_t group;
78
79   gras_init(&argc, argv);
80   amok_bw_init();
81   amok_pm_init();
82
83   INFO0("Maestro starting");
84   if (argc != 2) {
85     ERROR0("Usage: maestro port\n");
86     return 1;
87   }
88   mysock = gras_socket_server(atoi(argv[1]));
89   group = amok_pm_group_new("bandwidth");
90   INFO0("Wait for peers for 5 sec");
91   gras_msg_handleall(5);        /* friends, we're ready. Come and play */
92
93   if (xbt_dynar_length(group) < 2) {
94     char *msg;
95     asprintf(&msg, "Not enough peers arrived. Expected 2 got %ld",
96              xbt_dynar_length(group));
97     amok_pm_group_shutdown("bandwidth");
98     xbt_die(msg);
99   }
100   h1 = *(xbt_peer_t *) xbt_dynar_get_ptr(group, 0);
101   h2 = *(xbt_peer_t *) xbt_dynar_get_ptr(group, 1);
102   /* sort peers in right order to keep output right */
103   if (strcmp(h1->name, h2->name) < 0 || h1->port > h2->port) {
104     h_temp = h1;
105     h1 = h2;
106     h2 = h_temp;
107   }
108
109   INFO2("Contact %s:%d", h1->name, h1->port);
110   peer = gras_socket_client(h1->name, h1->port);
111
112   INFO0("Test the BW between me and one of the sensors");
113   amok_bw_test(peer, buf_size, msg_size, msg_amount, min_duration, &sec, &bw);
114   INFO7
115     ("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",
116      h1->name, h1->port, msg_amount, msg_size, min_duration, sec,
117      ((double) bw) / 1024.0);
118
119   INFO4("Test the BW between %s:%d and %s:%d", h1->name, h1->port, h2->name,
120         h2->port);
121   amok_bw_request(h1->name, h1->port, h2->name, h2->port, buf_size, msg_size,
122                   msg_amount, min_duration, &sec, &bw);
123   INFO6
124     ("Experience between %s:%d and %s:%d took took %f sec, achieving %f kb/s",
125      h1->name, h1->port, h2->name, h2->port, sec, ((double) bw) / 1024.0);
126
127   /* Game is over, friends */
128   amok_pm_group_shutdown("bandwidth");
129
130   gras_socket_close(mysock);
131   gras_exit();
132   return 0;
133 }