Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] re-writing some tracing functions
[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   type_t ret = xbt_new0(s_type_t, 1);
35   ret->name = xbt_strdup (typename);
36   ret->father = father;
37   ret->kind = kind;
38   ret->children = xbt_dict_new_homogeneous(NULL);
39   ret->values = xbt_dict_new_homogeneous(NULL);
40   ret->color = xbt_strdup (color);
41
42   char str_id[INSTR_DEFAULT_STR_SIZE];
43   snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", instr_new_paje_id());
44   ret->id = xbt_strdup (str_id);
45
46   if (father != NULL){
47     xbt_dict_set (father->children, key, ret, NULL);
48     XBT_DEBUG("new type %s, child of %s", typename, father->name);
49   }
50
51   xbt_dict_set (allTypes, typename, ret, NULL);
52   return ret;
53 }
54
55 void PJ_type_free (type_t type)
56 {
57   xbt_dict_remove (allTypes, type->name);
58
59   val_t value;
60   char *value_name;
61   xbt_dict_cursor_t cursor = NULL;
62   xbt_dict_foreach(type->values, cursor, value_name, value) {
63     PJ_value_free (value);
64   }
65   xbt_dict_free (&type->values);
66   xbt_free (type->name);
67   xbt_free (type->id);
68   xbt_free (type->color);
69   xbt_dict_free (&type->children);
70   xbt_free (type);
71   type = NULL;
72 }
73
74 static void recursiveDestroyType (type_t type)
75 {
76   XBT_DEBUG("recursiveDestroyType %s", type->name);
77   xbt_dict_cursor_t cursor = NULL;
78   type_t child;
79   char *child_name;
80   xbt_dict_foreach(type->children, cursor, child_name, child) {
81     recursiveDestroyType (child);
82   }
83   PJ_type_free(type);
84 }
85
86 void PJ_type_free_all (void)
87 {
88   recursiveDestroyType (PJ_type_get_root());
89   rootType = NULL;
90
91   //checks
92   if (xbt_dict_length(allTypes) != 0){
93     THROWF(tracing_error, 0, "some types still present even after destroying all of them");
94   }
95 }
96
97 static type_t recursiveGetType (const char *name, type_t root)
98 {
99   if (strcmp (root->name, name) == 0) return root;
100
101   xbt_dict_cursor_t cursor = NULL;
102   type_t child;
103   char *child_name;
104   type_t ret = NULL;
105   xbt_dict_foreach(root->children, cursor, child_name, child) {
106     type_t found = recursiveGetType(name, child);
107     if (found){
108       if (ret == NULL){
109         ret = found;
110       }else{
111         THROWF(tracing_error, 0, "found two types with the same name");
112       }
113     }
114   }
115   return ret;
116 }
117
118 type_t PJ_type_get (const char *name, type_t father)
119 {
120   return recursiveGetType (name, father);
121 }
122
123 type_t PJ_type_container_new (const char *name, type_t father)
124 {
125   type_t ret = (type_t)xbt_dict_get_or_null (allTypes, name);
126   if (ret){
127     THROWF(tracing_error, 1, "container type with name %s already exists", name);
128   }
129
130   ret = newType (name, name, NULL, TYPE_CONTAINER, father);
131   if (father == NULL){
132     rootType = ret;
133   }
134
135   if(father){
136     XBT_DEBUG("ContainerType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
137     new_pajeDefineContainerType (ret);
138   }
139   return ret;
140 }
141
142 type_t PJ_type_event_new (const char *name, const char *color, type_t father)
143 {
144   type_t ret = (type_t)xbt_dict_get_or_null (allTypes, name);
145   if (ret){
146     THROWF(tracing_error, 1, "event type with name %s already exists", name);
147   }
148
149   char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
150   if (!color){
151     ret = newType (name, name, white, TYPE_EVENT, father);
152   }else{
153     ret = newType (name, name, color, TYPE_EVENT, father);
154   }
155   XBT_DEBUG("EventType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
156   new_pajeDefineEventType(ret);
157   return ret;
158 }
159
160 type_t PJ_type_variable_new (const char *name, const char *color, type_t father)
161 {
162   type_t ret = (type_t)xbt_dict_get_or_null (allTypes, name);
163   if (ret){
164     THROWF(tracing_error, 1, "variable type with name %s already exists", name);
165   }
166
167   char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
168   if (!color){
169     ret = newType (name, name, white, TYPE_VARIABLE, father);
170   }else{
171     ret = newType (name, name, color, TYPE_VARIABLE, father);
172   }
173   XBT_DEBUG("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
174   new_pajeDefineVariableType (ret);
175   return ret;
176 }
177
178 type_t PJ_type_link_new (const char *name, type_t father, type_t source, type_t dest)
179 {
180   type_t ret = (type_t)xbt_dict_get_or_null (allTypes, name);
181   if (ret){
182     THROWF(tracing_error, 1, "link type with name %s already exists", name);
183   }
184
185   char key[INSTR_DEFAULT_STR_SIZE];
186   snprintf (key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", name, source->id, dest->id);
187   ret = newType (name, key, NULL, TYPE_LINK, father);
188   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);
189   new_pajeDefineLinkType(ret, source, dest);
190   return ret;
191 }
192
193 type_t PJ_type_state_new (const char *name, type_t father)
194 {
195   type_t ret = (type_t)xbt_dict_get_or_null (allTypes, name);
196   if (ret){
197     THROWF(tracing_error, 1, "state type with name %s already exists", name);
198   }
199
200   ret = newType (name, name, NULL, TYPE_STATE, father);
201   XBT_DEBUG("StateType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
202   new_pajeDefineStateType(ret);
203   return ret;
204 }
205
206 #endif