Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ONGOING work on exceptions plus minor cleanups.
[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
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   sensor_data_t g;
39
40   gras_init(&argc, argv);
41   g=gras_userdata_new(s_sensor_data_t);  
42   amok_bw_init();
43    
44   g->sock=gras_socket_server(atoi(argv[1]));
45   g->done = 0;
46   
47   gras_msgtype_declare("quit",NULL);
48   gras_cb_register(gras_msgtype_by_name("quit"),&sensor_cb_quit);
49   
50   while (! g->done )
51     gras_msg_handle(60.0);
52
53   gras_socket_close(g->sock);
54   return 0;
55 }
56
57 /* **********************************************************************
58  * Maestro code
59  * **********************************************************************/
60
61 /* Global private data */
62 typedef struct {
63   gras_socket_t sock;
64 } s_maestro_data_t,*maestro_data_t;
65
66 /* Function prototypes */
67 int maestro (int argc,char *argv[]);
68
69 int maestro(int argc,char *argv[]) {
70   maestro_data_t g;
71   double sec, bw;
72   int buf_size=32;
73   int exp_size=1024*50;
74   int msg_size=512;
75   gras_socket_t peer;
76
77   gras_init(&argc, argv);
78   g=gras_userdata_new(s_maestro_data_t);
79   amok_bw_init();
80
81   if (argc != 5) {
82      ERROR0("Usage: maestro host port host port\n");
83      return 1;
84   }
85
86   /* wait to ensure that all server sockets are there before starting the experiment */ 
87   gras_os_sleep(0.5);
88   
89   peer = gras_socket_client(argv[1],atoi(argv[2]));
90
91   INFO0("Test the BW between me and one of the sensors");  
92   amok_bw_test(peer,buf_size,exp_size,msg_size,&sec,&bw);
93   INFO6("Experience between me and %s:%d (%d kb in msgs of %d kb) took %f sec, achieving %f kb/s",
94         argv[1],atoi(argv[2]),
95         exp_size,msg_size,
96         sec,bw);
97
98   INFO0("Test the BW between the two sensors");  
99   amok_bw_request(argv[1],atoi(argv[2]),argv[3],atoi(argv[4]),
100                   buf_size,exp_size,msg_size,&sec,&bw); 
101   INFO2("Experience took took %f sec, achieving %f kb/s",
102         sec,bw);
103
104   /* ask sensors to quit */                    
105   gras_msgtype_declare("quit",NULL);
106   gras_msg_send(peer,gras_msgtype_by_name("quit"), NULL);
107   gras_socket_close(peer);
108
109   peer = gras_socket_client(argv[3],atoi(argv[4]));
110   gras_msg_send(peer,gras_msgtype_by_name("quit"), NULL);
111   gras_socket_close(peer);
112
113   gras_socket_close(g->sock);
114   return 0;
115 }