Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Compare file prefix only.
[simgrid.git] / src / instr / instr_paje_types.cpp
1 /* Copyright (c) 2012, 2014-2017. 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
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_types, instr, "Paje tracing event system (types)");
10
11 static simgrid::instr::Type* rootType = nullptr; /* the root type */
12
13 void PJ_type_release ()
14 {
15   rootType = nullptr;
16 }
17
18 simgrid::instr::Type* PJ_type_get_root()
19 {
20   return rootType;
21 }
22
23 simgrid::instr::Type::Type(const char* typeNameBuff, const char* key, const char* color, e_entity_types kind,
24                            Type* father)
25     : kind_(kind), father_(father)
26 {
27   if (typeNameBuff == nullptr || key == nullptr){
28     THROWF(tracing_error, 0, "can't create a new type with name or key equal nullptr");
29   }
30
31   this->name_     = xbt_strdup(typeNameBuff);
32   this->children_ = xbt_dict_new_homogeneous(nullptr);
33   this->values_   = xbt_dict_new_homogeneous(nullptr);
34   this->color_    = xbt_strdup(color);
35
36   char str_id[INSTR_DEFAULT_STR_SIZE];
37   snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", instr_new_paje_id());
38   this->id_ = xbt_strdup(str_id);
39
40   if (father != nullptr){
41     xbt_dict_set(father->children_, key, this, nullptr);
42     XBT_DEBUG("new type %s, child of %s", typeNameBuff, father->name_);
43   }
44 }
45
46 void PJ_type_free(simgrid::instr::Type* type)
47 {
48   simgrid::instr::Value* val;
49   char *value_name;
50   xbt_dict_cursor_t cursor = nullptr;
51   xbt_dict_foreach (type->values_, cursor, value_name, val) {
52     XBT_DEBUG("free value %s, child of %s", val->name_, val->father_->name_);
53     xbt_free(val);
54   }
55   xbt_dict_free(&type->values_);
56   xbt_free(type->name_);
57   xbt_free(type->id_);
58   xbt_free(type->color_);
59   xbt_dict_free(&type->children_);
60   xbt_free (type);
61   type = nullptr;
62 }
63
64 void recursiveDestroyType(simgrid::instr::Type* type)
65 {
66   XBT_DEBUG("recursiveDestroyType %s", type->name_);
67   xbt_dict_cursor_t cursor = nullptr;
68   simgrid::instr::Type* child;
69   char *child_name;
70   xbt_dict_foreach (type->children_, cursor, child_name, child) {
71     recursiveDestroyType (child);
72   }
73   PJ_type_free(type);
74 }
75
76 simgrid::instr::Type* PJ_type_get(const char* name, simgrid::instr::Type* father)
77 {
78   simgrid::instr::Type* ret = simgrid::instr::Type::getOrNull(name, father);
79   if (ret == nullptr){
80     THROWF(tracing_error, 2, "type with name (%s) not found in father type (%s)", name, father->name_);
81   }
82   return ret;
83 }
84
85 simgrid::instr::Type* simgrid::instr::Type::getOrNull(const char* name, simgrid::instr::Type* father)
86 {
87   if (name == nullptr || father == nullptr){
88     THROWF (tracing_error, 0, "can't get type with a nullptr name or from a nullptr father");
89   }
90
91   simgrid::instr::Type* ret = nullptr;
92   simgrid::instr::Type* child;
93   char *child_name;
94   xbt_dict_cursor_t cursor = nullptr;
95   xbt_dict_foreach (father->children_, cursor, child_name, child) {
96     if (strcmp(child->name_, name) == 0) {
97       if (ret != nullptr){
98         THROWF (tracing_error, 0, "there are two children types with the same name?");
99       }else{
100         ret = child;
101       }
102     }
103   }
104   return ret;
105 }
106
107 simgrid::instr::Type* simgrid::instr::Type::containerNew(const char* name, simgrid::instr::Type* father)
108 {
109   if (name == nullptr){
110     THROWF (tracing_error, 0, "can't create a container type with a nullptr name");
111   }
112
113   simgrid::instr::Type* ret = new simgrid::instr::Type(name, name, nullptr, TYPE_CONTAINER, father);
114   if (father == nullptr) {
115     rootType = ret;
116   } else {
117     XBT_DEBUG("ContainerType %s(%s), child of %s(%s)", ret->name_, ret->id_, father->name_, father->id_);
118     LogContainerTypeDefinition(ret);
119   }
120   return ret;
121 }
122
123 simgrid::instr::Type* simgrid::instr::Type::eventNew(const char* name, simgrid::instr::Type* father)
124 {
125   if (name == nullptr){
126     THROWF (tracing_error, 0, "can't create an event type with a nullptr name");
127   }
128
129   Type* ret = new Type (name, name, nullptr, TYPE_EVENT, father);
130   XBT_DEBUG("EventType %s(%s), child of %s(%s)", ret->name_, ret->id_, father->name_, father->id_);
131   LogDefineEventType(ret);
132   return ret;
133 }
134
135 simgrid::instr::Type* simgrid::instr::Type::variableNew(const char* name, const char* color,
136                                                         simgrid::instr::Type* father)
137 {
138   if (name == nullptr){
139     THROWF (tracing_error, 0, "can't create a variable type with a nullptr name");
140   }
141
142   Type* ret = nullptr;
143
144   if (not color) {
145     char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
146     ret = new Type (name, name, white, TYPE_VARIABLE, father);
147   }else{
148     ret = new Type (name, name, color, TYPE_VARIABLE, father);
149   }
150   XBT_DEBUG("VariableType %s(%s), child of %s(%s)", ret->name_, ret->id_, father->name_, father->id_);
151   LogVariableTypeDefinition (ret);
152   return ret;
153 }
154
155 simgrid::instr::Type* simgrid::instr::Type::linkNew(const char* name, Type* father, Type* source, Type* dest)
156 {
157   if (name == nullptr){
158     THROWF (tracing_error, 0, "can't create a link type with a nullptr name");
159   }
160
161   Type* ret = nullptr;
162
163   char key[INSTR_DEFAULT_STR_SIZE];
164   snprintf(key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", name, source->id_, dest->id_);
165   ret = new Type (name, key, nullptr, TYPE_LINK, father);
166   XBT_DEBUG("LinkType %s(%s), child of %s(%s)  %s(%s)->%s(%s)", ret->name_, ret->id_, father->name_, father->id_,
167             source->name_, source->id_, dest->name_, dest->id_);
168   LogLinkTypeDefinition(ret, source, dest);
169   return ret;
170 }
171
172 simgrid::instr::Type* simgrid::instr::Type::stateNew(const char* name, Type* father)
173 {
174   if (name == nullptr){
175     THROWF (tracing_error, 0, "can't create a state type with a nullptr name");
176   }
177
178   Type* ret = nullptr;
179
180   ret = new Type (name, name, nullptr, TYPE_STATE, father);
181   XBT_DEBUG("StateType %s(%s), child of %s(%s)", ret->name_, ret->id_, father->name_, father->id_);
182   LogStateTypeDefinition(ret);
183   return ret;
184 }