Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c2d75732298d0fdb57c8389baf5c8cd188199f05
[simgrid.git] / examples / msg / tracing / user_variables.c
1 /* Copyright (c) 2010. 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 /** @addtogroup MSG_examples
8  * 
9  * - <b>tracing/user_variables.c</b>: This program demonstrates how to
10  * trace user variables associated to the hosts of the platform file.
11  * You might want to run this program with the following parameters:
12  * --cfg=tracing:1
13  * --cfg=tracing/platform:1
14  * (See \ref tracing_tracing_options for details)
15  */
16
17 #include <stdio.h>
18 #include "msg/msg.h"
19 #include "xbt/sysdep.h"         /* calloc, printf */
20
21 /* Create a log channel to have nice outputs. */
22 #include "xbt/log.h"
23 #include "xbt/asserts.h"
24 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
25
26 int master(int argc, char *argv[]);
27
28 int master(int argc, char *argv[])
29 {
30   const char *hostname = MSG_host_get_name(MSG_host_self());
31   int i;
32
33   //the hostname has an empty HDD with a capacity of 100000 (bytes)
34   TRACE_host_variable_set(hostname, "HDD_capacity", 100000);
35   TRACE_host_variable_set(hostname, "HDD_utilization", 0);
36
37   for (i = 0; i < 10; i++) {
38     //create and execute a task just to make the simulated time advance
39     m_task_t task = MSG_task_create("task", 10000, 0, NULL);
40     MSG_task_execute (task);
41     MSG_task_destroy (task);
42
43     //ADD: after the execution of this task, the HDD utilization increases by 100 (bytes)
44     TRACE_host_variable_add(hostname, "HDD_utilization", 100);
45   }
46
47   for (i = 0; i < 10; i++) {
48     //create and execute a task just to make the simulated time advance
49     m_task_t task = MSG_task_create("task", 10000, 0, NULL);
50     MSG_task_execute (task);
51     MSG_task_destroy (task);
52
53     //SUB: after the execution of this task, the HDD utilization decreases by 100 (bytes)
54     TRACE_host_variable_sub(hostname, "HDD_utilization", 100);
55   }
56   return 0;
57 }
58
59 /** Main function */
60 int main(int argc, char *argv[])
61 {
62   MSG_init(&argc, argv);
63   if (argc < 3) {
64     printf("Usage: %s platform_file deployment_file\n", argv[0]);
65     exit(1);
66   }
67
68   char *platform_file = argv[1];
69   char *deployment_file = argv[2];
70   MSG_create_environment(platform_file);
71
72   //declaring user variables
73   TRACE_host_variable_declare("HDD_capacity");
74   TRACE_host_variable_declare("HDD_utilization");
75
76   //register functions and launch deployment
77   MSG_function_register("master", master);
78   MSG_function_register("slave", master);
79   MSG_launch_application(deployment_file);
80
81   MSG_main();
82
83   //get user declared variables
84   unsigned int cursor;
85   char *variable;
86   xbt_dynar_t host_variables = TRACE_get_host_variables ();
87   if (host_variables){
88     XBT_INFO ("Declared host variables:");
89     xbt_dynar_foreach (host_variables, cursor, variable){
90       XBT_INFO ("%s", variable);
91     }
92     xbt_dynar_free (&host_variables);
93   }
94   xbt_dynar_t link_variables = TRACE_get_link_variables ();
95   if (link_variables){
96     XBT_INFO ("Declared link variables:");
97     xbt_dynar_foreach (link_variables, cursor, variable){
98       XBT_INFO ("%s", variable);
99     }
100     xbt_dynar_free (&link_variables);
101   }
102
103   //create a customized triva graph configuration file
104   FILE *fp;
105   fp = fopen ("triva_graph.plist", "w");
106   if (!fp){
107     MSG_clean();
108     return 1;
109   }
110   fprintf (fp, "{\n node = (");
111   xbt_dynar_t nodes_type = TRACE_get_node_types ();
112   if (nodes_type){
113     XBT_INFO ("Node types in the trace:");
114     char *node_type;
115     xbt_dynar_foreach (nodes_type, cursor, node_type){
116       XBT_INFO ("%s", node_type);
117       fprintf (fp, "%s, ", node_type);
118     }
119     xbt_dynar_free (&nodes_type);
120   }
121   fprintf (fp, ");\n edge = (");
122   xbt_dynar_t edges_type = TRACE_get_edge_types ();
123   if (edges_type){
124     XBT_INFO ("Node types in the trace:");
125     char *edge_type;
126     xbt_dynar_foreach (edges_type, cursor, edge_type){
127       XBT_INFO ("%s", edge_type);
128       fprintf (fp, "%s, ", edge_type);
129     }
130     xbt_dynar_free (&edges_type);
131   }
132   fprintf (fp, ");\n");
133   fprintf (fp, " host = {\n  type = square;\n  size = HDD_capacity; \n  values = (HDD_utilization);\n };\n");
134   fprintf (fp, " link = {\n  type = rhombus;\n  size = bandwidth;\n };\n");
135   fprintf (fp, "}\n");
136   fclose (fp);
137
138   MSG_clean();
139   return 0;
140 }