Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
done reformating the doc of MSG examples. Many examples remain undocumented
[simgrid.git] / examples / msg / trace-user-variables / trace-user-variables.c
1 /* Copyright (c) 2010, 2012-2016. The SimGrid Team. All rights reserved.    */
2
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
6 #include <stdio.h>
7 #include "simgrid/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10
11 static int trace_fun(int argc, char *argv[])
12 {
13   const char *hostname = MSG_host_get_name(MSG_host_self());
14
15   //the hostname has an empty HDD with a capacity of 100000 (bytes)
16   TRACE_host_variable_set(hostname, "HDD_capacity", 100000);
17   TRACE_host_variable_set(hostname, "HDD_utilization", 0);
18
19   for (int i = 0; i < 10; i++) {
20     //create and execute a task just to make the simulated time advance
21     msg_task_t task = MSG_task_create("task", 10000, 0, NULL);
22     MSG_task_execute (task);
23     MSG_task_destroy (task);
24
25     //ADD: after the execution of this task, the HDD utilization increases by 100 (bytes)
26     TRACE_host_variable_add(hostname, "HDD_utilization", 100);
27   }
28
29   for (int i = 0; i < 10; i++) {
30     //create and execute a task just to make the simulated time advance
31     msg_task_t task = MSG_task_create("task", 10000, 0, NULL);
32     MSG_task_execute (task);
33     MSG_task_destroy (task);
34
35     //SUB: after the execution of this task, the HDD utilization decreases by 100 (bytes)
36     TRACE_host_variable_sub(hostname, "HDD_utilization", 100);
37   }
38   return 0;
39 }
40
41 int main(int argc, char *argv[])
42 {
43   MSG_init(&argc, argv);
44   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
45              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
46
47   MSG_create_environment(argv[1]);
48
49   //declaring user variables
50   TRACE_host_variable_declare("HDD_capacity");
51   TRACE_host_variable_declare("HDD_utilization");
52
53   //register functions and launch deployment
54   MSG_function_register("master", trace_fun);
55   MSG_function_register("worker", trace_fun);
56   MSG_launch_application(argv[2]);
57
58   MSG_main();
59
60   //get user declared variables
61   unsigned int cursor;
62   char *variable;
63   xbt_dynar_t host_variables = TRACE_get_host_variables ();
64   if (host_variables){
65     XBT_INFO ("Declared host variables:");
66     xbt_dynar_foreach (host_variables, cursor, variable){
67       XBT_INFO ("%s", variable);
68     }
69     xbt_dynar_free (&host_variables);
70   }
71   xbt_dynar_t link_variables = TRACE_get_link_variables ();
72   if (link_variables){
73     XBT_INFO ("Declared link variables:");
74     xbt_dynar_foreach (link_variables, cursor, variable){
75       XBT_INFO ("%s", variable);
76     }
77     xbt_dynar_free (&link_variables);
78   }
79
80   //create a customized viva graph configuration file
81   FILE *fp;
82   fp = fopen ("viva_graph.plist", "w");
83   if (!fp){
84     return 1;
85   }
86   fprintf (fp, "{\n node = (");
87   xbt_dynar_t nodes_type = TRACE_get_node_types ();
88   if (nodes_type){
89     XBT_INFO ("Node types in the trace:");
90     char *node_type;
91     xbt_dynar_foreach (nodes_type, cursor, node_type){
92       XBT_INFO ("%s", node_type);
93       fprintf (fp, "%s, ", node_type);
94     }
95     xbt_dynar_free (&nodes_type);
96   }
97   fprintf (fp, ");\n edge = (");
98   xbt_dynar_t edges_type = TRACE_get_edge_types ();
99   if (edges_type){
100     XBT_INFO ("Node types in the trace:");
101     char *edge_type;
102     xbt_dynar_foreach (edges_type, cursor, edge_type){
103       XBT_INFO ("%s", edge_type);
104       fprintf (fp, "%s, ", edge_type);
105     }
106     xbt_dynar_free (&edges_type);
107   }
108   fprintf (fp, ");\n");
109   fprintf (fp, " host = {\n  type = square;\n  size = HDD_capacity; \n  values = (HDD_utilization);\n };\n");
110   fprintf (fp, " link = {\n  type = rhombus;\n  size = bandwidth;\n };\n");
111   fprintf (fp, "}\n");
112   fclose (fp);
113
114   return 0;
115 }