Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c8f0ee2e3fcac37eb47cb802bfb2ecaa72b9fa31
[simgrid.git] / src / instr / instr_paje_types.c
1 /* Copyright (c) 2012. 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 #include "instr/instr_private.h"
7
8 #ifdef HAVE_TRACING
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_types, instr, "Paje tracing event system (types)");
11
12 static type_t rootType = NULL;        /* the root type */
13 static xbt_dict_t allTypes = NULL;    /* all declared types indexed by name */
14
15 void PJ_type_alloc ()
16 {
17   allTypes = xbt_dict_new_homogeneous (NULL);
18 }
19
20 void PJ_type_release ()
21 {
22   xbt_dict_free (&allTypes);
23   rootType = NULL;
24   allTypes = NULL;
25 }
26
27 type_t PJ_type_get_root ()
28 {
29   return rootType;
30 }
31
32 static type_t newType (const char *typename, const char *key, const char *color, e_entity_types kind, type_t father)
33 {
34   if (typename == NULL || key == NULL){
35     THROWF(tracing_error, 0, "can't create a new type with name or key equal NULL");
36   }
37
38   type_t ret = xbt_new0(s_type_t, 1);
39   ret->name = xbt_strdup (typename);
40   ret->father = father;
41   ret->kind = kind;
42   ret->children = xbt_dict_new_homogeneous(NULL);
43   ret->values = xbt_dict_new_homogeneous(NULL);
44   ret->color = xbt_strdup (color);
45
46   char str_id[INSTR_DEFAULT_STR_SIZE];
47   snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", instr_new_paje_id());
48   ret->id = xbt_strdup (str_id);
49
50   if (father != NULL){
51     xbt_dict_set (father->children, key, ret, NULL);
52     XBT_DEBUG("new type %s, child of %s", typename, father->name);
53   }
54
55   xbt_dict_set (allTypes, typename, ret, NULL);
56   return ret;
57 }
58
59 void PJ_type_free (type_t type)
60 {
61   xbt_dict_remove (allTypes, type->name);
62
63   val_t value;
64   char *value_name;
65   xbt_dict_cursor_t cursor = NULL;
66   xbt_dict_foreach(type->values, cursor, value_name, value) {
67     PJ_value_free (value);
68   }
69   xbt_dict_free (&type->values);
70   xbt_free (type->name);
71   xbt_free (type->id);
72   xbt_free (type->color);
73   xbt_dict_free (&type->children);
74   xbt_free (type);
75   type = NULL;
76 }
77
78 static void recursiveDestroyType (type_t type)
79 {
80   XBT_DEBUG("recursiveDestroyType %s", type->name);
81   xbt_dict_cursor_t cursor = NULL;
82   type_t child;
83   char *child_name;
84   xbt_dict_foreach(type->children, cursor, child_name, child) {
85     recursiveDestroyType (child);
86   }
87   PJ_type_free(type);
88 }
89
90 void PJ_type_free_all (void)
91 {
92   recursiveDestroyType (PJ_type_get_root());
93   rootType = NULL;
94
95   //checks
96   if (xbt_dict_length(allTypes) != 0){
97     THROWF(tracing_error, 0, "some types still present even after destroying all of them");
98   }
99 }
100
101 static type_t recursiveGetType (const char *name, type_t root)
102 {
103   if (name == NULL || root == NULL){
104     THROWF (tracing_error, 0, "can't get type with a NULL name (or a NULL father given)");
105   }
106
107   if (strcmp (root->name, name) == 0) return root;
108
109   xbt_dict_cursor_t cursor = NULL;
110   type_t child;
111   char *child_name;
112   type_t ret = NULL;
113   xbt_dict_foreach(root->children, cursor, child_name, child) {
114     type_t found = recursiveGetType(name, child);
115     if (found){
116       if (ret == NULL){
117         ret = found;
118       }else{
119         THROWF(tracing_error, 0, "found two types with the same name");
120       }
121     }
122   }
123   return ret;
124 }
125
126 type_t PJ_type_get (const char *name, type_t father)
127 {
128   return recursiveGetType (name, father);
129 }
130
131 type_t PJ_type_container_new (const char *name, type_t father)
132 {
133   if (name == NULL){
134     THROWF (tracing_error, 0, "can't create a container type with a NULL name");
135   }
136
137   type_t ret = (type_t)xbt_dict_get_or_null (allTypes, name);
138   if (ret){
139     THROWF(tracing_error, 1, "container type with name %s already exists", name);
140   }
141
142   ret = newType (name, name, NULL, TYPE_CONTAINER, father);
143   if (father == NULL){
144     rootType = ret;
145   }
146
147   if(father){
148     XBT_DEBUG("ContainerType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
149     new_pajeDefineContainerType (ret);
150   }
151   return ret;
152 }
153
154 type_t PJ_type_event_new (const char *name, const char *color, type_t father)
155 {
156   if (name == NULL){
157     THROWF (tracing_error, 0, "can't create an event type with a NULL name");
158   }
159
160   type_t ret = (type_t)xbt_dict_get_or_null (allTypes, name);
161   if (ret){
162     THROWF(tracing_error, 1, "event type with name %s already exists", name);
163   }
164
165   char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
166   if (!color){
167     ret = newType (name, name, white, TYPE_EVENT, father);
168   }else{
169     ret = newType (name, name, color, TYPE_EVENT, father);
170   }
171   XBT_DEBUG("EventType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
172   new_pajeDefineEventType(ret);
173   return ret;
174 }
175
176 type_t PJ_type_variable_new (const char *name, const char *color, type_t father)
177 {
178   if (name == NULL){
179     THROWF (tracing_error, 0, "can't create a variable type with a NULL name");
180   }
181
182   type_t ret = (type_t)xbt_dict_get_or_null (allTypes, name);
183   if (ret){
184     THROWF(tracing_error, 1, "variable type with name %s already exists", name);
185   }
186
187   char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
188   if (!color){
189     ret = newType (name, name, white, TYPE_VARIABLE, father);
190   }else{
191     ret = newType (name, name, color, TYPE_VARIABLE, father);
192   }
193   XBT_DEBUG("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
194   new_pajeDefineVariableType (ret);
195   return ret;
196 }
197
198 type_t PJ_type_link_new (const char *name, type_t father, type_t source, type_t dest)
199 {
200   if (name == NULL){
201     THROWF (tracing_error, 0, "can't create a link type with a NULL name");
202   }
203
204   type_t ret = (type_t)xbt_dict_get_or_null (allTypes, name);
205   if (ret){
206     THROWF(tracing_error, 1, "link type with name %s already exists", name);
207   }
208
209   char key[INSTR_DEFAULT_STR_SIZE];
210   snprintf (key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", name, source->id, dest->id);
211   ret = newType (name, key, NULL, TYPE_LINK, father);
212   XBT_DEBUG("LinkType %s(%s), child of %s(%s)  %s(%s)->%s(%s)", ret->name, ret->id, father->name, father->id, source->name, source->id, dest->name, dest->id);
213   new_pajeDefineLinkType(ret, source, dest);
214   return ret;
215 }
216
217 type_t PJ_type_state_new (const char *name, type_t father)
218 {
219   if (name == NULL){
220     THROWF (tracing_error, 0, "can't create a state type with a NULL name");
221   }
222
223   type_t ret = (type_t)xbt_dict_get_or_null (allTypes, name);
224   if (ret){
225     THROWF(tracing_error, 1, "state type with name %s already exists", name);
226   }
227
228   ret = newType (name, name, NULL, TYPE_STATE, father);
229   XBT_DEBUG("StateType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
230   new_pajeDefineStateType(ret);
231   return ret;
232 }
233
234 #endif