Logo AND Algorithmique Numérique Distribuée

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