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 val_t newValue (const char *valuename, const char *color, type_t father)
29 val_t ret = xbt_new0(s_val_t, 1);
30 ret->name = xbt_strdup (valuename);
32 ret->color = xbt_strdup (color);
34 static long long int type_id = 0;
35 char str_id[INSTR_DEFAULT_STR_SIZE];
36 snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "v%lld", type_id++);
37 ret->id = xbt_strdup (str_id);
39 xbt_dict_set (father->values, valuename, ret, NULL);
40 DEBUG2("new value %s, child of %s", ret->name, ret->father->name);
44 val_t getValue (const char *valuename, const char *color, type_t father)
46 if (father->kind == TYPE_VARIABLE) return NULL; //Variables can't have different values
48 val_t ret = (val_t)xbt_dict_get_or_null (father->values, valuename);
50 ret = newValue (valuename, color, father);
51 DEBUG4("EntityValue %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
52 new_pajeDefineEntityValue(ret);
57 val_t getValueByName (const char *valuename, type_t father)
59 return getValue (valuename, NULL, father);
62 static type_t newType (const char *typename, const char *key, const char *color, e_entity_types kind, type_t father)
64 type_t ret = xbt_new0(s_type_t, 1);
65 ret->name = xbt_strdup (typename);
68 ret->children = xbt_dict_new ();
69 ret->values = xbt_dict_new ();
70 ret->color = xbt_strdup (color);
72 static long long int type_id = 0;
73 char str_id[INSTR_DEFAULT_STR_SIZE];
74 snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", type_id++);
75 ret->id = xbt_strdup (str_id);
78 xbt_dict_set (father->children, key, ret, NULL);
79 DEBUG2("new type %s, child of %s", typename, father->name);
89 type_t getContainerType (const char *typename, type_t father)
93 ret = newType (typename, typename, NULL, TYPE_CONTAINER, father);
94 if (father) new_pajeDefineContainerType (ret);
97 //check if my father type already has my typename
98 ret = (type_t)xbt_dict_get_or_null (father->children, typename);
100 ret = newType (typename, typename, NULL, TYPE_CONTAINER, father);
101 new_pajeDefineContainerType (ret);
107 type_t getEventType (const char *typename, const char *color, type_t father)
109 type_t ret = xbt_dict_get_or_null (father->children, typename);
111 char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
113 ret = newType (typename, typename, white, TYPE_EVENT, father);
115 ret = newType (typename, typename, color, TYPE_EVENT, father);
117 DEBUG4("EventType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
118 new_pajeDefineEventType(ret);
123 type_t getVariableType (const char *typename, const char *color, type_t father)
125 type_t ret = xbt_dict_get_or_null (father->children, typename);
127 char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
129 ret = newType (typename, typename, white, TYPE_VARIABLE, father);
131 ret = newType (typename, typename, color, TYPE_VARIABLE, father);
133 DEBUG4("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
134 new_pajeDefineVariableType (ret);
139 char *getVariableTypeIdByName (const char *name, type_t father)
141 xbt_dict_cursor_t cursor = NULL;
144 xbt_dict_foreach(father->children, cursor, key, type) {
145 if (strcmp (name, type->name) == 0) return type->id;
150 type_t getLinkType (const char *typename, type_t father, type_t source, type_t dest)
152 char key[INSTR_DEFAULT_STR_SIZE];
153 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", typename, source->id, dest->id);
154 type_t ret = xbt_dict_get_or_null (father->children, key);
156 ret = newType (typename, key, NULL, TYPE_LINK, father);
157 DEBUG8("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);
158 new_pajeDefineLinkType(ret, source, dest);
163 type_t getStateType (const char *typename, type_t father)
165 type_t ret = xbt_dict_get_or_null (father->children, typename);
167 ret = newType (typename, typename, NULL, TYPE_STATE, father);
168 DEBUG4("StateType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
169 new_pajeDefineStateType(ret);
174 container_t newContainer (const char *name, e_container_types kind, container_t father)
176 static long long int container_id = 0;
177 char id_str[INSTR_DEFAULT_STR_SIZE];
178 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", container_id++);
180 container_t new = xbt_new0(s_container_t, 1);
181 new->name = xbt_strdup (name); // name of the container
182 new->id = xbt_strdup (id_str); // id (or alias) of the container
183 new->father = father;
184 // level depends on level of father
186 new->level = new->father->level+1;
187 DEBUG2("new container %s, child of %s", name, father->name);
191 // type definition (method depends on kind of this new container)
193 if (new->kind == INSTR_AS){
194 //if this container is of an AS, its type name depends on its level
195 char as_typename[INSTR_DEFAULT_STR_SIZE];
196 snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
198 new->type = getContainerType (as_typename, new->father->type);
200 new->type = getContainerType ("0", NULL);
203 //otherwise, the name is its kind
205 case INSTR_HOST: new->type = getContainerType ("HOST", new->father->type); break;
206 case INSTR_LINK: new->type = getContainerType ("LINK", new->father->type); break;
207 case INSTR_ROUTER: new->type = getContainerType ("ROUTER", new->father->type); break;
208 case INSTR_SMPI: new->type = getContainerType ("MPI", new->father->type); break;
209 case INSTR_MSG_PROCESS: new->type = getContainerType ("MSG_PROCESS", new->father->type); break;
210 case INSTR_MSG_TASK: new->type = getContainerType ("MSG_TASK", new->father->type); break;
211 default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
214 new->children = xbt_dict_new();
216 xbt_dict_set(new->father->children, new->name, new, NULL);
217 new_pajeCreateContainer (new);
220 //register hosts, routers, links containers
221 if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
222 xbt_dict_set (allContainers, new->name, new, NULL);
224 //register NODE types for triva configuration
225 xbt_dict_set (trivaNodeTypes, new->type->name, xbt_strdup("1"), xbt_free);
230 static container_t recursiveGetContainer (const char *name, container_t root)
232 if (strcmp (root->name, name) == 0) return root;
234 xbt_dict_cursor_t cursor = NULL;
237 xbt_dict_foreach(root->children, cursor, child_name, child) {
238 container_t ret = recursiveGetContainer(name, child);
244 container_t getContainer (const char *name)
246 return recursiveGetContainer(name, rootContainer);
249 container_t getContainerByName (const char *name)
251 return (container_t)xbt_dict_get_or_null (allContainers, name);
254 char *getContainerIdByName (const char *name)
256 return getContainerByName(name)->id;
259 container_t getRootContainer ()
261 return rootContainer;
264 static type_t recursiveGetType (const char *name, type_t root)
266 if (strcmp (root->name, name) == 0) return root;
268 xbt_dict_cursor_t cursor = NULL;
271 xbt_dict_foreach(root->children, cursor, child_name, child) {
272 type_t ret = recursiveGetType(name, child);
278 type_t getType (const char *name, type_t father)
280 return recursiveGetType (name, father);
283 void destroyContainer (container_t container)
285 //remove me from my father
286 if (container->father){
287 xbt_dict_remove(container->father->children, container->name);
290 DEBUG1("destroy container %s", container->name);
292 //obligation to dump previous events because they might
293 //reference the container that is about to be destroyed
294 TRACE_last_timestamp_to_dump = surf_get_clock();
295 TRACE_paje_dump_buffer(1);
297 //trace my destruction
298 new_pajeDestroyContainer(container);
301 xbt_free (container->name);
302 xbt_free (container->id);
303 xbt_free (container->children);
304 xbt_free (container);
308 static void recursiveDestroyContainer (container_t container)
310 xbt_dict_cursor_t cursor = NULL;
313 xbt_dict_foreach(container->children, cursor, child_name, child) {
314 recursiveDestroyContainer (child);
316 destroyContainer (container);
319 static void recursiveDestroyType (type_t type)
321 xbt_dict_cursor_t cursor = NULL;
324 xbt_dict_foreach(type->children, cursor, child_name, child) {
325 recursiveDestroyType (child);
327 xbt_free (type->name);
329 xbt_free (type->children);
334 void destroyAllContainers ()
336 if (getRootContainer()) recursiveDestroyContainer (getRootContainer());
337 if (getRootType()) recursiveDestroyType (getRootType());
338 rootContainer = NULL;
343 #endif /* HAVE_TRACING */