Logo AND Algorithmique Numérique Distribuée

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