Logo AND Algorithmique Numérique Distribuée

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