Logo AND Algorithmique Numérique Distribuée

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