Logo AND Algorithmique Numérique Distribuée

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