Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[INSTR] Rename paje headers for call-location tracing
[simgrid.git] / src / instr / instr_paje_types.cpp
1 /* Copyright (c) 2012, 2014-2015. 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 type_t rootType = NULL;        /* the root type */
12
13 void PJ_type_alloc ()
14 {
15 }
16
17 void PJ_type_release ()
18 {
19   rootType = NULL;
20 }
21
22 type_t PJ_type_get_root ()
23 {
24   return rootType;
25 }
26
27 static type_t newType (const char *typeNameBuff, const char *key, const char *color, e_entity_types kind, type_t father)
28 {
29   if (typeNameBuff == NULL || key == NULL){
30     THROWF(tracing_error, 0, "can't create a new type with name or key equal NULL");
31   }
32
33   type_t ret = xbt_new0(s_type_t, 1);
34   ret->name = xbt_strdup (typeNameBuff);
35   ret->father = father;
36   ret->kind = kind;
37   ret->children = xbt_dict_new_homogeneous(NULL);
38   ret->values = xbt_dict_new_homogeneous(NULL);
39   ret->color = xbt_strdup (color);
40
41   char str_id[INSTR_DEFAULT_STR_SIZE];
42   snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", instr_new_paje_id());
43   ret->id = xbt_strdup (str_id);
44
45   if (father != NULL){
46     xbt_dict_set (father->children, key, ret, NULL);
47     XBT_DEBUG("new type %s, child of %s", typeNameBuff, father->name);
48   }
49   return ret;
50 }
51
52 void PJ_type_free (type_t type)
53 {
54   val_t value;
55   char *value_name;
56   xbt_dict_cursor_t cursor = NULL;
57   xbt_dict_foreach(type->values, cursor, value_name, value) {
58     PJ_value_free (value);
59   }
60   xbt_dict_free (&type->values);
61   xbt_free (type->name);
62   xbt_free (type->id);
63   xbt_free (type->color);
64   xbt_dict_free (&type->children);
65   xbt_free (type);
66   type = NULL;
67 }
68
69 static void recursiveDestroyType (type_t type)
70 {
71   XBT_DEBUG("recursiveDestroyType %s", type->name);
72   xbt_dict_cursor_t cursor = NULL;
73   type_t child;
74   char *child_name;
75   xbt_dict_foreach(type->children, cursor, child_name, child) {
76     recursiveDestroyType (child);
77   }
78   PJ_type_free(type);
79 }
80
81 void PJ_type_free_all (void)
82 {
83   recursiveDestroyType (PJ_type_get_root());
84   rootType = NULL;
85 }
86
87 type_t PJ_type_get (const char *name, type_t father)
88 {
89   type_t ret = PJ_type_get_or_null (name, father);
90   if (ret == NULL){
91     THROWF (tracing_error, 2, "type with name (%s) not found in father type (%s)", name, father->name);
92   }
93   return ret;
94 }
95
96 type_t PJ_type_get_or_null (const char *name, type_t father)
97 {
98   if (name == NULL || father == NULL){
99     THROWF (tracing_error, 0, "can't get type with a NULL name or from a NULL father");
100   }
101
102   type_t ret = NULL, child;
103   char *child_name;
104   xbt_dict_cursor_t cursor = NULL;
105   xbt_dict_foreach(father->children, cursor, child_name, child) {
106     if (strcmp (child->name, name) == 0){
107       if (ret != NULL){
108         THROWF (tracing_error, 0, "there are two children types with the same name?");
109       }else{
110         ret = child;
111       }
112     }
113   }
114   return ret;
115 }
116
117 type_t PJ_type_container_new (const char *name, type_t father)
118 {
119   if (name == NULL){
120     THROWF (tracing_error, 0, "can't create a container type with a NULL name");
121   }
122
123   type_t ret = NULL;
124
125   ret = newType (name, name, NULL, TYPE_CONTAINER, father);
126   if (father == NULL){
127     rootType = ret;
128   }
129
130   if(father){
131     XBT_DEBUG("ContainerType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
132     new_pajeDefineContainerType (ret);
133   }
134   return ret;
135 }
136
137 type_t PJ_type_event_new (const char *name, type_t father)
138 {
139   if (name == NULL){
140     THROWF (tracing_error, 0, "can't create an event type with a NULL name");
141   }
142
143   type_t ret = newType (name, name, NULL, TYPE_EVENT, father);
144   XBT_DEBUG("EventType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
145   new_pajeDefineEventType(ret);
146   return ret;
147 }
148
149 type_t PJ_type_variable_new (const char *name, const char *color, type_t father)
150 {
151   if (name == NULL){
152     THROWF (tracing_error, 0, "can't create a variable type with a NULL name");
153   }
154
155   type_t ret = NULL;
156
157   char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
158   if (!color){
159     ret = newType (name, name, white, TYPE_VARIABLE, father);
160   }else{
161     ret = newType (name, name, color, TYPE_VARIABLE, father);
162   }
163   XBT_DEBUG("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
164   new_pajeDefineVariableType (ret);
165   return ret;
166 }
167
168 type_t PJ_type_link_new (const char *name, type_t father, type_t source, type_t dest)
169 {
170   if (name == NULL){
171     THROWF (tracing_error, 0, "can't create a link type with a NULL name");
172   }
173
174   type_t ret = NULL;
175
176   char key[INSTR_DEFAULT_STR_SIZE];
177   snprintf (key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", name, source->id, dest->id);
178   ret = newType (name, key, NULL, TYPE_LINK, father);
179   XBT_DEBUG("LinkType %s(%s), child of %s(%s)  %s(%s)->%s(%s)", ret->name, ret->id, father->name, father->id,
180             source->name, source->id, dest->name, dest->id);
181   new_pajeDefineLinkType(ret, source, dest);
182   return ret;
183 }
184
185 type_t PJ_type_state_new (const char *name, type_t father)
186 {
187   if (name == NULL){
188     THROWF (tracing_error, 0, "can't create a state type with a NULL name");
189   }
190
191   type_t ret = NULL;
192
193   ret = newType (name, name, NULL, TYPE_STATE, father);
194   XBT_DEBUG("StateType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
195   new_pajeDefineStateType(ret);
196   return ret;
197 }