Logo AND Algorithmique Numérique Distribuée

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