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 new_type_id (void)
29 static long long int type_id = 0;
33 static val_t newValue (const char *valuename, const char *color, type_t father)
35 val_t ret = xbt_new0(s_val_t, 1);
36 ret->name = xbt_strdup (valuename);
38 ret->color = xbt_strdup (color);
40 char str_id[INSTR_DEFAULT_STR_SIZE];
41 snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", new_type_id());
42 ret->id = xbt_strdup (str_id);
44 xbt_dict_set (father->values, valuename, ret, NULL);
45 XBT_DEBUG("new value %s, child of %s", ret->name, ret->father->name);
49 val_t getValue (const char *valuename, const char *color, type_t father)
51 if (father->kind == TYPE_VARIABLE) return NULL; //Variables can't have different values
53 val_t ret = (val_t)xbt_dict_get_or_null (father->values, valuename);
55 ret = newValue (valuename, color, father);
56 XBT_DEBUG("EntityValue %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
57 new_pajeDefineEntityValue(ret);
62 val_t getValueByName (const char *valuename, type_t father)
64 return getValue (valuename, NULL, father);
67 static type_t newType (const char *typename, const char *key, const char *color, e_entity_types kind, type_t father)
69 type_t ret = xbt_new0(s_type_t, 1);
70 ret->name = xbt_strdup (typename);
73 ret->children = xbt_dict_new ();
74 ret->values = xbt_dict_new ();
75 ret->color = xbt_strdup (color);
77 char str_id[INSTR_DEFAULT_STR_SIZE];
78 snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", new_type_id());
79 ret->id = xbt_strdup (str_id);
82 xbt_dict_set (father->children, key, ret, NULL);
83 XBT_DEBUG("new type %s, child of %s", typename, father->name);
93 type_t getContainerType (const char *typename, type_t father)
97 ret = newType (typename, typename, NULL, TYPE_CONTAINER, father);
98 if (father) new_pajeDefineContainerType (ret);
101 //check if my father type already has my typename
102 ret = (type_t)xbt_dict_get_or_null (father->children, typename);
104 ret = newType (typename, typename, NULL, TYPE_CONTAINER, father);
105 new_pajeDefineContainerType (ret);
111 type_t getEventType (const char *typename, const char *color, type_t father)
113 type_t ret = xbt_dict_get_or_null (father->children, typename);
115 char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
117 ret = newType (typename, typename, white, TYPE_EVENT, father);
119 ret = newType (typename, typename, color, TYPE_EVENT, father);
121 XBT_DEBUG("EventType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
122 new_pajeDefineEventType(ret);
127 type_t getVariableType (const char *typename, const char *color, type_t father)
129 type_t ret = xbt_dict_get_or_null (father->children, typename);
131 char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
133 ret = newType (typename, typename, white, TYPE_VARIABLE, father);
135 ret = newType (typename, typename, color, TYPE_VARIABLE, father);
137 XBT_DEBUG("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
138 new_pajeDefineVariableType (ret);
143 char *getVariableTypeIdByName (const char *name, type_t father)
145 xbt_dict_cursor_t cursor = NULL;
148 xbt_dict_foreach(father->children, cursor, key, type) {
149 if (strcmp (name, type->name) == 0) return type->id;
154 type_t getLinkType (const char *typename, type_t father, type_t source, type_t dest)
156 char key[INSTR_DEFAULT_STR_SIZE];
157 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", typename, source->id, dest->id);
158 type_t ret = xbt_dict_get_or_null (father->children, key);
160 ret = newType (typename, key, NULL, TYPE_LINK, father);
161 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);
162 new_pajeDefineLinkType(ret, source, dest);
167 type_t getStateType (const char *typename, type_t father)
169 type_t ret = xbt_dict_get_or_null (father->children, typename);
171 ret = newType (typename, typename, NULL, TYPE_STATE, father);
172 XBT_DEBUG("StateType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
173 new_pajeDefineStateType(ret);
178 container_t newContainer (const char *name, e_container_types kind, container_t father)
180 static long long int container_id = 0;
181 char id_str[INSTR_DEFAULT_STR_SIZE];
182 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", container_id++);
184 container_t new = xbt_new0(s_container_t, 1);
185 new->name = xbt_strdup (name); // name of the container
186 new->id = xbt_strdup (id_str); // id (or alias) of the container
187 new->father = father;
188 // level depends on level of father
190 new->level = new->father->level+1;
191 XBT_DEBUG("new container %s, child of %s", name, father->name);
195 // type definition (method depends on kind of this new container)
197 if (new->kind == INSTR_AS){
198 //if this container is of an AS, its type name depends on its level
199 char as_typename[INSTR_DEFAULT_STR_SIZE];
200 snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
202 new->type = getContainerType (as_typename, new->father->type);
204 new->type = getContainerType ("0", NULL);
207 //otherwise, the name is its kind
209 case INSTR_HOST: new->type = getContainerType ("HOST", new->father->type); break;
210 case INSTR_LINK: new->type = getContainerType ("LINK", new->father->type); break;
211 case INSTR_ROUTER: new->type = getContainerType ("ROUTER", new->father->type); break;
212 case INSTR_SMPI: new->type = getContainerType ("MPI", new->father->type); break;
213 case INSTR_MSG_PROCESS: new->type = getContainerType ("MSG_PROCESS", new->father->type); break;
214 case INSTR_MSG_TASK: new->type = getContainerType ("MSG_TASK", new->father->type); break;
215 default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
218 new->children = xbt_dict_new();
220 xbt_dict_set(new->father->children, new->name, new, NULL);
221 new_pajeCreateContainer (new);
224 //register hosts, routers, links containers
225 if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
226 xbt_dict_set (allContainers, new->name, new, NULL);
228 //register NODE types for triva configuration
229 xbt_dict_set (trivaNodeTypes, new->type->name, xbt_strdup("1"), xbt_free);
234 static container_t recursiveGetContainer (const char *name, container_t root)
236 if (strcmp (root->name, name) == 0) return root;
238 xbt_dict_cursor_t cursor = NULL;
241 xbt_dict_foreach(root->children, cursor, child_name, child) {
242 container_t ret = recursiveGetContainer(name, child);
248 container_t getContainer (const char *name)
250 return recursiveGetContainer(name, rootContainer);
253 container_t getContainerByName (const char *name)
255 return (container_t)xbt_dict_get (allContainers, name);
258 char *getContainerIdByName (const char *name)
260 return getContainerByName(name)->id;
263 container_t getRootContainer ()
265 return rootContainer;
268 static type_t recursiveGetType (const char *name, type_t root)
270 if (strcmp (root->name, name) == 0) return root;
272 xbt_dict_cursor_t cursor = NULL;
275 xbt_dict_foreach(root->children, cursor, child_name, child) {
276 type_t ret = recursiveGetType(name, child);
282 type_t getType (const char *name, type_t father)
284 return recursiveGetType (name, father);
287 void destroyContainer (container_t container)
289 //remove me from my father
290 if (container->father){
291 xbt_dict_remove(container->father->children, container->name);
294 XBT_DEBUG("destroy container %s", container->name);
296 //obligation to dump previous events because they might
297 //reference the container that is about to be destroyed
298 TRACE_last_timestamp_to_dump = surf_get_clock();
299 TRACE_paje_dump_buffer(1);
301 //trace my destruction
302 new_pajeDestroyContainer(container);
305 xbt_free (container->name);
306 xbt_free (container->id);
307 xbt_free (container->children);
308 xbt_free (container);
312 static void recursiveDestroyContainer (container_t container)
314 xbt_dict_cursor_t cursor = NULL;
317 xbt_dict_foreach(container->children, cursor, child_name, child) {
318 recursiveDestroyContainer (child);
320 destroyContainer (container);
323 static void recursiveDestroyType (type_t type)
325 xbt_dict_cursor_t cursor = NULL;
328 xbt_dict_foreach(type->children, cursor, child_name, child) {
329 recursiveDestroyType (child);
331 xbt_free (type->name);
333 xbt_free (type->children);
334 xbt_free (type->values);
339 void destroyAllContainers ()
341 if (getRootContainer()) recursiveDestroyContainer (getRootContainer());
342 if (getRootType()) recursiveDestroyType (getRootType());
343 rootContainer = NULL;
348 #endif /* HAVE_TRACING */