Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
59bed1fcd53429c367ed4764ea15ab07a1c428a2
[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/hostmanagement.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;
27
28   gras_init(&argc, argv);
29   amok_bw_init();
30   amok_hm_init();
31
32   mysock = gras_socket_server_range(3000,9999,0,0);
33   gras_os_sleep(1); /* let the master get ready */
34   master = gras_socket_client_from_string(argv[1]);
35                                               
36   amok_hm_group_join(master,"bandwidth");
37   amok_hm_mainloop(60);
38
39   gras_socket_close(mysock);
40   gras_socket_close(master);
41   gras_exit();
42   return 0;
43 }
44
45 /* **********************************************************************
46  * Maestro code
47  * **********************************************************************/
48
49 /* Function prototypes */
50 int maestro (int argc,char *argv[]);
51
52 int maestro(int argc,char *argv[]) {
53   double sec, bw;
54   int buf_size=32  *1024;
55   int exp_size=512 *1024;
56   int msg_size=512 *1024;
57   double min_duration = 1;
58    
59   gras_socket_t peer;
60   gras_socket_t mysock;
61   xbt_host_t h1,h2;
62   xbt_dynar_t group;
63
64   gras_init(&argc, argv);
65   amok_bw_init();
66   amok_hm_init();
67
68   if (argc != 2) {
69      ERROR0("Usage: maestro port\n");
70      return 1;
71   }
72   mysock=gras_socket_server(atoi(argv[1]));
73   group=amok_hm_group_new("bandwidth");
74   gras_msg_handleall(10); /* friends, we're ready. Come and play */
75    
76   if (xbt_dynar_length(group) < 2) {
77      char *msg;
78      asprintf(&msg,"Not enough peers arrived. Expected 2 got %ld",
79               xbt_dynar_length(group));
80      xbt_die(msg);
81   }
82   h1 = *(xbt_host_t*) xbt_dynar_get_ptr(group, 0);
83   h2 = *(xbt_host_t*)xbt_dynar_get_ptr(group, 1);
84    
85   peer = gras_socket_client(h1->name, h1->port);
86
87   INFO0("Test the BW between me and one of the sensors");  
88   amok_bw_test(peer,buf_size,exp_size,msg_size,min_duration,&sec,&bw);
89   INFO6("Experience between me and %s:%d (%d bytes in msgs of %d bytes) took %f sec, achieving %f kb/s",
90         h1->name, h1->port,
91         exp_size,msg_size,
92         sec,((double)bw)/1024.0);
93
94   INFO4("Test the BW between %s:%d and %s:%d",  h1->name, h1->port,     h2->name, h2->port);
95   amok_bw_request(h1->name, h1->port,   h2->name, h2->port,
96                   buf_size,exp_size,msg_size,min_duration,&sec,&bw);
97   INFO6("Experience between %s:%d and %s:%d took took %f sec, achieving %f kb/s",
98         h1->name, h1->port,     h2->name, h2->port,
99         sec,((double)bw)/1024.0);
100
101   /* Game is over, friends */
102   amok_hm_group_shutdown ("bandwidth");
103
104   gras_socket_close(mysock);
105   gras_exit();
106   return 0;
107 }