Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4 functions and a callback for one dict... seriously?!
[simgrid.git] / src / instr / instr_resource_utilization.cpp
1 /* Copyright (c) 2010-2015. 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 "src/instr/instr_private.h"
8 #include <unordered_map>
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_resource, instr, "tracing (un)-categorized resource utilization");
11
12 //to check if variables were previously set to 0, otherwise paje won't simulate them
13 static std::unordered_map<std::string, std::string> platform_variables;
14
15 //used by all methods
16 static void __TRACE_surf_check_variable_set_to_zero(double now, const char *variable, const char *resource)
17 {
18   /* To trace resource utilization, we use pajeAddVariable and pajeSubVariable only.
19    * The Paje simulator needs a pajeSetVariable in the first place so it knows the initial value of all variables for
20    * subsequent adds/subs. If we don't do so, the first pajeAddVariable is added to a non-determined value within
21    * the Paje simulator, causing analysis problems.
22    */
23
24   // create a key considering the resource and variable
25   std::string key = std::string(resource) + variable;
26
27   // check if key exists: if it doesn't, set the variable to zero and mark this in the dict
28   if (platform_variables.find(key) == platform_variables.end()) {
29     container_t container = PJ_container_get (resource);
30     type_t type = PJ_type_get (variable, container->type);
31     new SetVariableEvent (now, container, type, 0);
32     platform_variables[key] = std::string("");
33   }
34 }
35
36 static void instr_event (double now, double delta, type_t variable, container_t resource, double value)
37 {
38   __TRACE_surf_check_variable_set_to_zero(now, variable->name, resource->name);
39   new AddVariableEvent(now, resource, variable, value);
40   new SubVariableEvent(now + delta, resource, variable, value);
41 }
42
43 /* TRACE_surf_link_set_utilization: entry point from SimGrid */
44 void TRACE_surf_link_set_utilization(const char *resource, const char *category, double value, double now, double delta)
45 {
46   //only trace link utilization if link is known by tracing mechanism
47   if (not PJ_container_get_or_null(resource))
48     return;
49   if (not value)
50     return;
51
52   //trace uncategorized link utilization
53   if (TRACE_uncategorized()){
54     XBT_DEBUG("UNCAT LINK [%f - %f] %s bandwidth_used %f", now, now+delta, resource, value);
55     container_t container = PJ_container_get (resource);
56     type_t type = PJ_type_get ("bandwidth_used", container->type);
57     instr_event (now, delta, type, container, value);
58   }
59
60   //trace categorized utilization
61   if (TRACE_categorized()){
62     if (not category)
63       return;
64     //variable of this category starts by 'b', because we have a link here
65     char category_type[INSTR_DEFAULT_STR_SIZE];
66     snprintf (category_type, INSTR_DEFAULT_STR_SIZE, "b%s", category);
67     XBT_DEBUG("CAT LINK [%f - %f] %s %s %f", now, now+delta, resource, category_type, value);
68     container_t container = PJ_container_get (resource);
69     type_t type = PJ_type_get (category_type, container->type);
70     instr_event (now, delta, type, container, value);
71   }
72 }
73
74 /* TRACE_surf_host_set_utilization: entry point from SimGrid */
75 void TRACE_surf_host_set_utilization(const char *resource, const char *category, double value, double now, double delta)
76 {
77   //only trace host utilization if host is known by tracing mechanism
78   container_t container = PJ_container_get_or_null(resource);
79   if (not container || not value)
80     return;
81
82   //trace uncategorized host utilization
83   if (TRACE_uncategorized()){
84     XBT_DEBUG("UNCAT HOST [%f - %f] %s power_used %f", now, now+delta, resource, value);
85     type_t type = PJ_type_get ("power_used", container->type);
86     instr_event (now, delta, type, container, value);
87   }
88
89   //trace categorized utilization
90   if (TRACE_categorized()){
91     if (not category)
92       return;
93     //variable of this category starts by 'p', because we have a host here
94     char category_type[INSTR_DEFAULT_STR_SIZE];
95     snprintf (category_type, INSTR_DEFAULT_STR_SIZE, "p%s", category);
96     XBT_DEBUG("CAT HOST [%f - %f] %s %s %f", now, now+delta, resource, category_type, value);
97     type_t type = PJ_type_get (category_type, container->type);
98     instr_event (now, delta, type, container, value);
99   }
100 }