1 /* Copyright (c) 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "instr/instr_private.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje, instr, "Paje tracing event system (data structures)");
13 static type_t rootType = NULL; /* the root type */
14 static container_t rootContainer = NULL; /* the root container */
15 static xbt_dict_t allContainers = NULL; /* all created containers indexed by name */
16 xbt_dict_t trivaNodeTypes = NULL; /* all link types defined */
17 xbt_dict_t trivaEdgeTypes = NULL; /* all host types defined */
19 void instr_paje_init (container_t root)
21 allContainers = xbt_dict_new ();
22 trivaNodeTypes = xbt_dict_new ();
23 trivaEdgeTypes = xbt_dict_new ();
27 static long long int newTypeId ()
29 static long long int counter = 0;
33 static type_t newType (const char *typename, const char *key, const char *color, e_entity_types kind, type_t father)
35 type_t ret = xbt_new0(s_type_t, 1);
36 ret->name = xbt_strdup (typename);
39 ret->children = xbt_dict_new ();
40 ret->color = xbt_strdup (color);
42 long long int id = newTypeId();
43 char str_id[INSTR_DEFAULT_STR_SIZE];
44 snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", id);
45 ret->id = xbt_strdup (str_id);
48 xbt_dict_set (father->children, key, ret, NULL);
58 type_t getContainerType (const char *typename, type_t father)
62 ret = newType (typename, typename, NULL, TYPE_CONTAINER, father);
63 if (father) new_pajeDefineContainerType (ret);
66 //check if my father type already has my typename
67 ret = (type_t)xbt_dict_get_or_null (father->children, typename);
69 ret = newType (typename, typename, NULL, TYPE_CONTAINER, father);
70 new_pajeDefineContainerType (ret);
76 type_t getEventType (const char *typename, const char *color, type_t father)
78 type_t ret = xbt_dict_get_or_null (father->children, typename);
80 char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
82 ret = newType (typename, typename, white, TYPE_EVENT, father);
84 ret = newType (typename, typename, color, TYPE_EVENT, father);
86 //INFO4("EventType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
87 new_pajeDefineEventType(ret);
92 type_t getVariableType (const char *typename, const char *color, type_t father)
94 type_t ret = xbt_dict_get_or_null (father->children, typename);
96 char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
98 ret = newType (typename, typename, white, TYPE_VARIABLE, father);
100 ret = newType (typename, typename, color, TYPE_VARIABLE, father);
102 //INFO4("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
103 new_pajeDefineVariableType (ret);
108 char *getVariableTypeIdByName (const char *name, type_t father)
110 xbt_dict_cursor_t cursor = NULL;
113 xbt_dict_foreach(father->children, cursor, key, type) {
114 if (strcmp (name, type->name) == 0) return type->id;
119 type_t getLinkType (const char *typename, type_t father, type_t source, type_t dest)
121 char key[INSTR_DEFAULT_STR_SIZE];
122 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", typename, source->id, dest->id);
123 type_t ret = xbt_dict_get_or_null (father->children, key);
125 ret = newType (typename, key, NULL, TYPE_LINK, father);
126 //INFO8("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);
127 new_pajeDefineLinkType(ret, source, dest);
132 type_t getStateType (const char *typename, type_t father)
134 type_t ret = xbt_dict_get_or_null (father->children, typename);
136 ret = newType (typename, typename, NULL, TYPE_STATE, father);
137 //INFO4("StateType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
138 new_pajeDefineStateType(ret);
144 static long long int newContainedId ()
146 static long long counter = 0;
150 container_t newContainer (const char *name, e_container_types kind, container_t father)
152 long long int counter = newContainedId();
153 char id_str[INSTR_DEFAULT_STR_SIZE];
154 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", counter);
156 container_t new = xbt_new0(s_container_t, 1);
157 new->name = xbt_strdup (name); // name of the container
158 new->id = xbt_strdup (id_str); // id (or alias) of the container
159 new->father = father;
160 // level depends on level of father
162 new->level = new->father->level+1;
166 // type definition (method depends on kind of this new container)
168 if (new->kind == INSTR_AS){
169 //if this container is of an AS, its type name depends on its level
170 char as_typename[INSTR_DEFAULT_STR_SIZE];
171 snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
173 new->type = getContainerType (as_typename, new->father->type);
175 new->type = getContainerType ("0", NULL);
178 //otherwise, the name is its kind
180 case INSTR_HOST: new->type = getContainerType ("HOST", new->father->type); break;
181 case INSTR_LINK: new->type = getContainerType ("LINK", new->father->type); break;
182 case INSTR_ROUTER: new->type = getContainerType ("ROUTER", new->father->type); break;
183 case INSTR_SMPI: new->type = getContainerType ("MPI", new->father->type); break;
184 case INSTR_MSG_PROCESS: new->type = getContainerType ("MSG_PROCESS", new->father->type); break;
185 case INSTR_MSG_TASK: new->type = getContainerType ("MSG_TASK", new->father->type); break;
186 default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
189 new->children = xbt_dict_new();
191 xbt_dict_set(new->father->children, new->name, new, NULL);
192 new_pajeCreateContainer (new);
195 //register hosts, routers, links containers
196 if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
197 xbt_dict_set (allContainers, new->name, new, NULL);
199 //register NODE types for triva configuration
200 xbt_dict_set (trivaNodeTypes, new->type->name, xbt_strdup("1"), xbt_free);
205 static container_t recursiveGetContainer (const char *name, container_t root)
207 if (strcmp (root->name, name) == 0) return root;
209 xbt_dict_cursor_t cursor = NULL;
212 xbt_dict_foreach(root->children, cursor, child_name, child) {
213 container_t ret = recursiveGetContainer(name, child);
219 container_t getContainer (const char *name)
221 return recursiveGetContainer(name, rootContainer);
224 container_t getContainerByName (const char *name)
226 return (container_t)xbt_dict_get (allContainers, name);
229 char *getContainerIdByName (const char *name)
231 return getContainerByName(name)->id;
234 container_t getRootContainer ()
236 return rootContainer;
239 static type_t recursiveGetType (const char *name, type_t root)
241 if (strcmp (root->name, name) == 0) return root;
243 xbt_dict_cursor_t cursor = NULL;
246 xbt_dict_foreach(root->children, cursor, child_name, child) {
247 type_t ret = recursiveGetType(name, child);
253 type_t getType (const char *name)
255 return recursiveGetType (name, rootType);
258 void destroyContainer (container_t container)
260 //remove me from my father
261 if (container->father){
262 xbt_dict_remove(container->father->children, container->name);
265 //trace my destruction
266 new_pajeDestroyContainer(container);
269 xbt_free (container->name);
270 xbt_free (container->id);
271 xbt_free (container->children);
272 xbt_free (container);
276 static void recursiveDestroyContainer (container_t container)
278 xbt_dict_cursor_t cursor = NULL;
281 xbt_dict_foreach(container->children, cursor, child_name, child) {
282 recursiveDestroyContainer (child);
284 destroyContainer (container);
287 static void recursiveDestroyType (type_t type)
289 xbt_dict_cursor_t cursor = NULL;
292 xbt_dict_foreach(type->children, cursor, child_name, child) {
293 recursiveDestroyType (child);
295 xbt_free (type->name);
297 xbt_free (type->children);
302 void destroyAllContainers ()
304 if (getRootContainer()) recursiveDestroyContainer (getRootContainer());
305 if (getRootType()) recursiveDestroyType (getRootType());
306 rootContainer = NULL;
311 #endif /* HAVE_TRACING */