Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Oups, I changed the prototype of these functions
[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   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",-1);
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   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("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",
114         h1->name, h1->port,
115         msg_amount,msg_size,min_duration,
116         sec,((double)bw)/1024.0);
117
118   INFO4("Test the BW between %s:%d and %s:%d",  h1->name, h1->port,     h2->name, h2->port);
119   amok_bw_request(h1->name, h1->port,   h2->name, h2->port,
120                   buf_size,msg_size,msg_amount,min_duration,&sec,&bw);
121   INFO6("Experience between %s:%d and %s:%d took took %f sec, achieving %f kb/s",
122         h1->name, h1->port,     h2->name, h2->port,
123         sec,((double)bw)/1024.0);
124
125   /* Game is over, friends */
126   amok_pm_group_shutdown ("bandwidth");
127
128   gras_socket_close(mysock);
129   gras_exit();
130   return 0;
131 }