Logo AND Algorithmique Numérique Distribuée

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