Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
59033b68fb8fb677e7c97acd8cec8aaeaa3f3e3f
[simgrid.git] / examples / 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
14 XBT_LOG_NEW_DEFAULT_CATEGORY(Bandwidth,"Messages specific to this example");
15
16 /* **********************************************************************
17  * Sensor code
18  * **********************************************************************/
19
20 /* Global private data */
21 typedef struct {
22   gras_socket_t sock;
23   int done;
24 } s_sensor_data_t,*sensor_data_t;
25
26 static int sensor_cb_quit(gras_socket_t  expeditor,
27                           void          *payload_data) {
28   sensor_data_t globals=(sensor_data_t)gras_userdata_get();
29                           
30   globals->done = 1;                  
31   return 1;         
32 }
33
34 /* Function prototypes */
35 int sensor (int argc,char *argv[]);
36
37 int sensor (int argc,char *argv[]) {
38   xbt_error_t errcode;
39   sensor_data_t g;
40
41   gras_init(&argc, argv, NULL);
42   g=gras_userdata_new(s_sensor_data_t);  
43
44   amok_bw_init();
45    
46   if ((errcode=gras_socket_server(atoi(argv[1]),&(g->sock)))) { 
47     ERROR1("Sensor: Error %s encountered while opening the server socket",xbt_error_name(errcode));
48     return 1;
49   }
50   g->done = 0;
51   
52   gras_msgtype_declare("quit",NULL);
53   gras_cb_register(gras_msgtype_by_name("quit"),&sensor_cb_quit);
54   
55   while (! g->done ) {
56     errcode=gras_msg_handle(60.0);
57     if (errcode != no_error) {
58        ERROR1("Sensor: Error '%s' while handling message",xbt_error_name(errcode));
59        gras_socket_close(g->sock);
60        return errcode;
61     }   
62   }     
63
64   gras_socket_close(g->sock);
65   return 0;
66 }
67
68 /* **********************************************************************
69  * Maestro code
70  * **********************************************************************/
71
72 /* Global private data */
73 typedef struct {
74   gras_socket_t sock;
75 } s_maestro_data_t,*maestro_data_t;
76
77 /* Function prototypes */
78 int maestro (int argc,char *argv[]);
79
80 int maestro(int argc,char *argv[]) {
81   xbt_error_t errcode;
82   maestro_data_t g;
83   double sec, bw;
84   int buf_size=32;
85   int exp_size=1024*50;
86   int msg_size=1024;
87   gras_socket_t peer;
88
89   gras_init(&argc, argv, NULL);
90   g=gras_userdata_new(s_maestro_data_t);
91   amok_bw_init();
92    
93   if ((errcode=gras_socket_server(6000,&(g->sock)))) { 
94     ERROR1("Maestro: Error %s encountered while opening the server socket",xbt_error_name(errcode));
95     return 1;
96   }
97       
98    
99   if (argc != 5) {
100      ERROR0("Usage: maestro host port host port\n");
101      return 1;
102   }
103
104   /* wait to ensure that all server sockets are there before starting the experiment */ 
105   gras_os_sleep(1.0);
106   
107   if ((errcode=gras_socket_client(argv[1],atoi(argv[2]),&peer))) {
108      ERROR3("Client: Unable to connect to my peer on %s:%s. Got %s",
109             argv[1],argv[2],xbt_error_name(errcode));
110      return 1;
111   }
112
113   INFO0("Test the BW between me and one of the sensors");  
114   TRY(amok_bw_test(peer,buf_size,exp_size,msg_size,&sec,&bw));
115   INFO6("maestro: Experience between me and %s:%d (%d kb in msgs of %d kb) took %f sec, achieving %f kb/s",
116         argv[1],atoi(argv[2]),
117         exp_size,msg_size,
118         sec,bw);
119
120   INFO0("Test the BW between the two sensors");  
121   TRY(amok_bw_request(argv[1],atoi(argv[2]),argv[3],atoi(argv[4]),
122                       buf_size,exp_size,msg_size,&sec,&bw));    
123
124   /* ask sensors to quit */                    
125   gras_msgtype_declare("quit",NULL);
126   TRY(gras_msg_send(peer,gras_msgtype_by_name("quit"), NULL));
127   gras_socket_close(peer);
128   TRY(gras_socket_client(argv[3],atoi(argv[4]),&peer));
129   TRY(gras_msg_send(peer,gras_msgtype_by_name("quit"), NULL));
130   gras_socket_close(peer);
131
132   gras_socket_close(g->sock);
133   return 0;
134 }