Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further cleanups in instr
[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.hpp"
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 simgrid::instr::Type* PJ_type_get_root()
14 {
15   return rootType;
16 }
17
18 simgrid::instr::Type::Type(const char* typeNameBuff, const char* key, std::string color, e_entity_types kind,
19                            Type* father)
20     : color_(color), kind_(kind), father_(father)
21 {
22   if (typeNameBuff == nullptr || key == nullptr){
23     THROWF(tracing_error, 0, "can't create a new type with name or key equal nullptr");
24   }
25
26   this->name_     = xbt_strdup(typeNameBuff);
27   this->children_ = xbt_dict_new_homogeneous(nullptr);
28
29   this->id_ = bprintf("%lld", instr_new_paje_id());
30
31   if (father != nullptr){
32     xbt_dict_set(father->children_, key, this, nullptr);
33     XBT_DEBUG("new type %s, child of %s", typeNameBuff, father->name_);
34   }
35 }
36
37 simgrid::instr::Type::~Type()
38 {
39   xbt_dict_cursor_t cursor = nullptr;
40   for (auto elm : values_) {
41     XBT_DEBUG("free value %s, child of %s", elm.second->getCname(), elm.second->father_->name_);
42     delete elm.second;
43   }
44   simgrid::instr::Type* child;
45   char *child_name;
46   xbt_dict_foreach (children_, cursor, child_name, child) {
47     delete child;
48   }
49   xbt_dict_free(&children_);
50   xbt_free(name_);
51   xbt_free(id_);
52 }
53
54 simgrid::instr::Type* simgrid::instr::Type::getChild(const char* name)
55 {
56   simgrid::instr::Type* ret = this->getChildOrNull(name);
57   if (ret == nullptr)
58     THROWF(tracing_error, 2, "type with name (%s) not found in father type (%s)", name, this->name_);
59   return ret;
60 }
61
62 simgrid::instr::Type* simgrid::instr::Type::getChildOrNull(const char* name)
63 {
64   xbt_assert(name != nullptr, "can't get type with a nullptr name");
65
66   simgrid::instr::Type* ret = nullptr;
67   simgrid::instr::Type* child;
68   char *child_name;
69   xbt_dict_cursor_t cursor = nullptr;
70   xbt_dict_foreach (children_, cursor, child_name, child) {
71     if (strcmp(child->name_, name) == 0) {
72       if (ret != nullptr) {
73         THROWF (tracing_error, 0, "there are two children types with the same name?");
74       } else {
75         ret = child;
76       }
77     }
78   }
79   return ret;
80 }
81
82 simgrid::instr::Type* simgrid::instr::Type::containerNew(const char* name, simgrid::instr::Type* father)
83 {
84   if (name == nullptr){
85     THROWF (tracing_error, 0, "can't create a container type with a nullptr name");
86   }
87
88   simgrid::instr::Type* ret = new simgrid::instr::Type(name, name, "", TYPE_CONTAINER, father);
89   if (father == nullptr) {
90     rootType = ret;
91   } else {
92     XBT_DEBUG("ContainerType %s(%s), child of %s(%s)", ret->name_, ret->id_, father->name_, father->id_);
93     LogContainerTypeDefinition(ret);
94   }
95   return ret;
96 }
97
98 simgrid::instr::Type* simgrid::instr::Type::eventNew(const char* name, simgrid::instr::Type* father)
99 {
100   if (name == nullptr){
101     THROWF (tracing_error, 0, "can't create an event type with a nullptr name");
102   }
103
104   Type* ret = new Type(name, name, "", TYPE_EVENT, father);
105   XBT_DEBUG("EventType %s(%s), child of %s(%s)", ret->name_, ret->id_, father->name_, father->id_);
106   LogDefineEventType(ret);
107   return ret;
108 }
109
110 simgrid::instr::Type* simgrid::instr::Type::variableNew(const char* name, std::string color,
111                                                         simgrid::instr::Type* father)
112 {
113   if (name == nullptr){
114     THROWF (tracing_error, 0, "can't create a variable type with a nullptr name");
115   }
116
117   Type* ret = nullptr;
118
119   if (color.empty()) {
120     char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
121     ret = new Type (name, name, white, TYPE_VARIABLE, father);
122   }else{
123     ret = new Type (name, name, color, TYPE_VARIABLE, father);
124   }
125   XBT_DEBUG("VariableType %s(%s), child of %s(%s)", ret->name_, ret->id_, father->name_, father->id_);
126   LogVariableTypeDefinition (ret);
127   return ret;
128 }
129
130 simgrid::instr::Type* simgrid::instr::Type::linkNew(const char* name, Type* father, Type* source, Type* dest)
131 {
132   if (name == nullptr){
133     THROWF (tracing_error, 0, "can't create a link type with a nullptr name");
134   }
135
136   char key[INSTR_DEFAULT_STR_SIZE];
137   snprintf(key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", name, source->id_, dest->id_);
138   Type* ret = new Type(name, key, "", TYPE_LINK, father);
139   XBT_DEBUG("LinkType %s(%s), child of %s(%s)  %s(%s)->%s(%s)", ret->name_, ret->id_, father->name_, father->id_,
140             source->name_, source->id_, dest->name_, dest->id_);
141   LogLinkTypeDefinition(ret, source, dest);
142   return ret;
143 }
144
145 simgrid::instr::Type* simgrid::instr::Type::stateNew(const char* name, Type* father)
146 {
147   if (name == nullptr){
148     THROWF (tracing_error, 0, "can't create a state type with a nullptr name");
149   }
150
151   Type* ret = new Type(name, name, "", TYPE_STATE, father);
152   XBT_DEBUG("StateType %s(%s), child of %s(%s)", ret->name_, ret->id_, father->name_, father->id_);
153   LogStateTypeDefinition(ret);
154   return ret;
155 }