Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Platform generation: add a bittorent example using a generated platform
[simgrid.git] / examples / msg / bittorrent / bittorrent_platfgen.c
1 /* Copyright (c) 2012. The SimGrid Team.
2  * All rights reserved.                                                     */
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5 #include "bittorrent.h"
6 #include "peer.h"
7 #include "tracker.h"
8 #include <msg/msg.h>
9 #include <simgrid/platf_generator.h>
10 /**
11  * Bittorrent example launcher, using a generated platform
12  */
13
14 static RngStream rng_stream;
15
16 void promoter(context_node_t node);
17 void labeler(context_edge_t edge);
18 void create_environment(int node_count);
19 void dispatch_jobs(double tracker_deadline, double peer_deadline, double seed_percentage);
20
21 void promoter(context_node_t node) {
22   s_sg_platf_host_cbarg_t host_parameters;
23
24   if(node->degree == 1) {
25     //We promote only the leaf; as we use a star topology, all the nodes
26     //will be promoted except the first one, which will be a router with
27     //every hosts connected on.
28     host_parameters.id = NULL;
29
30     //Power from 3,000,000 to 10,000,000
31     host_parameters.power_peak = 7000000 * RngStream_RandU01(rng_stream) + 3000000;
32     host_parameters.core_amount = 1;
33     host_parameters.power_scale = 1;
34     host_parameters.power_trace = NULL;
35     host_parameters.initial_state = SURF_RESOURCE_ON;
36     host_parameters.state_trace = NULL;
37     host_parameters.coord = NULL;
38     host_parameters.properties = NULL;
39
40     platf_graph_promote_to_host(node, &host_parameters);
41   }
42 }
43
44 void labeler(context_edge_t edge) {
45
46   s_sg_platf_link_cbarg_t link_parameters;
47   link_parameters.id = NULL;
48
49   //bandwidth from 3,000,000 to 10,000,000
50   link_parameters.bandwidth = 7000000 * RngStream_RandU01(rng_stream) + 3000000;
51   link_parameters.bandwidth_trace = NULL;
52
53   //Latency from 0ms to 100ms
54   link_parameters.latency = RngStream_RandU01(rng_stream) / 10.0;
55   link_parameters.latency_trace = NULL;
56   link_parameters.state = SURF_RESOURCE_ON;
57   link_parameters.state_trace = NULL;
58   link_parameters.policy = SURF_LINK_SHARED;
59   link_parameters.properties = NULL;
60
61   platf_graph_link_label(edge, &link_parameters);
62 }
63
64 void create_environment(int node_count) {
65
66   platf_graph_uniform(node_count);
67
68   //every nodes are connected to the first one
69   platf_graph_interconnect_star();
70   //No need to check if the graph is connected, the star topology implies it.
71
72   //register promoter and labeler
73   platf_graph_promoter(promoter);
74   platf_graph_labeler(labeler);
75
76   //promoting and labeling
77   platf_do_promote();
78   platf_do_label();
79
80   //Put the platform into the simulator
81   platf_generate();
82 }
83
84 void dispatch_jobs(double tracker_deadline, double peer_deadline, double seed_percentage) {
85
86   xbt_dynar_t available_nodes = MSG_hosts_as_dynar();
87   msg_host_t host;
88   unsigned int i;
89
90   char** arguments_tracker;
91   char** arguments_peer;
92
93   unsigned int seed_count = (seed_percentage/100.0) * xbt_dynar_length(available_nodes);
94
95   xbt_dynar_foreach(available_nodes, i, host) {
96     if(i==0) {
97       //The fisrt node is the tracker
98       arguments_tracker = xbt_malloc0(sizeof(char*) * 2);
99       arguments_tracker[0] = bprintf("tracker");
100       arguments_tracker[1] = bprintf("%f", tracker_deadline);
101       MSG_process_create_with_arguments("tracker", tracker, NULL, host, 2, arguments_tracker);
102     } else {
103       //Other nodes are peers
104       int argument_size;
105       arguments_peer = xbt_malloc0(sizeof(char*) * 4);
106       arguments_peer[0] = bprintf("peer");
107       arguments_peer[1] = bprintf("%d", i);
108       arguments_peer[2] = bprintf("%f", peer_deadline);
109
110       //The first peers will be seeders
111       if(seed_count > 0) {
112         seed_count--;
113         arguments_peer[3] = bprintf("1");
114         argument_size = 4;
115       } else {
116         //Other ars leechers
117         arguments_peer[3] = NULL;
118         argument_size = 3;
119       }
120       MSG_process_create_with_arguments("peer", peer, NULL, host, argument_size, arguments_peer);
121     }
122   }
123 }
124
125 int main(int argc, char *argv[])
126 {
127   MSG_init(&argc, argv);
128
129   rng_stream = RngStream_CreateStream(NULL);
130
131   //Maybe these parameters should be set from the command line...
132   //create_environment(<node_count>)
133   create_environment(20);
134
135   //dispatch_jobs(<tracker_deadline>, <peer_deadline>, <seed_percentage>)
136   dispatch_jobs(2000, 2000, 10);
137
138   MSG_main();
139
140   return 0;
141 }