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 type_t newType (const char *typename, const char *key, const char *color, e_entity_types kind, type_t father)
29 type_t ret = xbt_new0(s_type_t, 1);
30 ret->name = xbt_strdup (typename);
33 ret->children = xbt_dict_new ();
34 ret->color = xbt_strdup (color);
36 static long long int type_id = 0;
37 char str_id[INSTR_DEFAULT_STR_SIZE];
38 snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", type_id++);
39 ret->id = xbt_strdup (str_id);
42 xbt_dict_set (father->children, key, ret, NULL);
43 DEBUG2("new type %s, child of %s", typename, father->name);
53 type_t getContainerType (const char *typename, type_t father)
57 ret = newType (typename, typename, NULL, TYPE_CONTAINER, father);
58 if (father) new_pajeDefineContainerType (ret);
61 //check if my father type already has my typename
62 ret = (type_t)xbt_dict_get_or_null (father->children, typename);
64 ret = newType (typename, typename, NULL, TYPE_CONTAINER, father);
65 new_pajeDefineContainerType (ret);
71 type_t getEventType (const char *typename, const char *color, type_t father)
73 type_t ret = xbt_dict_get_or_null (father->children, typename);
75 char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
77 ret = newType (typename, typename, white, TYPE_EVENT, father);
79 ret = newType (typename, typename, color, TYPE_EVENT, father);
81 DEBUG4("EventType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
82 new_pajeDefineEventType(ret);
87 type_t getVariableType (const char *typename, const char *color, type_t father)
89 type_t ret = xbt_dict_get_or_null (father->children, typename);
91 char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
93 ret = newType (typename, typename, white, TYPE_VARIABLE, father);
95 ret = newType (typename, typename, color, TYPE_VARIABLE, father);
97 DEBUG4("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
98 new_pajeDefineVariableType (ret);
103 char *getVariableTypeIdByName (const char *name, type_t father)
105 xbt_dict_cursor_t cursor = NULL;
108 xbt_dict_foreach(father->children, cursor, key, type) {
109 if (strcmp (name, type->name) == 0) return type->id;
114 type_t getLinkType (const char *typename, type_t father, type_t source, type_t dest)
116 char key[INSTR_DEFAULT_STR_SIZE];
117 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", typename, source->id, dest->id);
118 type_t ret = xbt_dict_get_or_null (father->children, key);
120 ret = newType (typename, key, NULL, TYPE_LINK, father);
121 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);
122 new_pajeDefineLinkType(ret, source, dest);
127 type_t getStateType (const char *typename, type_t father)
129 type_t ret = xbt_dict_get_or_null (father->children, typename);
131 ret = newType (typename, typename, NULL, TYPE_STATE, father);
132 DEBUG4("StateType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
133 new_pajeDefineStateType(ret);
138 container_t newContainer (const char *name, e_container_types kind, container_t father)
140 static long long int container_id = 0;
141 char id_str[INSTR_DEFAULT_STR_SIZE];
142 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", container_id++);
144 container_t new = xbt_new0(s_container_t, 1);
145 new->name = xbt_strdup (name); // name of the container
146 new->id = xbt_strdup (id_str); // id (or alias) of the container
147 new->father = father;
148 // level depends on level of father
150 new->level = new->father->level+1;
151 DEBUG2("new container %s, child of %s", name, father->name);
155 // type definition (method depends on kind of this new container)
157 if (new->kind == INSTR_AS){
158 //if this container is of an AS, its type name depends on its level
159 char as_typename[INSTR_DEFAULT_STR_SIZE];
160 snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
162 new->type = getContainerType (as_typename, new->father->type);
164 new->type = getContainerType ("0", NULL);
167 //otherwise, the name is its kind
169 case INSTR_HOST: new->type = getContainerType ("HOST", new->father->type); break;
170 case INSTR_LINK: new->type = getContainerType ("LINK", new->father->type); break;
171 case INSTR_ROUTER: new->type = getContainerType ("ROUTER", new->father->type); break;
172 case INSTR_SMPI: new->type = getContainerType ("MPI", new->father->type); break;
173 case INSTR_MSG_PROCESS: new->type = getContainerType ("MSG_PROCESS", new->father->type); break;
174 case INSTR_MSG_TASK: new->type = getContainerType ("MSG_TASK", new->father->type); break;
175 default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
178 new->children = xbt_dict_new();
180 xbt_dict_set(new->father->children, new->name, new, NULL);
181 new_pajeCreateContainer (new);
184 //register hosts, routers, links containers
185 if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
186 xbt_dict_set (allContainers, new->name, new, NULL);
188 //register NODE types for triva configuration
189 xbt_dict_set (trivaNodeTypes, new->type->name, xbt_strdup("1"), xbt_free);
194 static container_t recursiveGetContainer (const char *name, container_t root)
196 if (strcmp (root->name, name) == 0) return root;
198 xbt_dict_cursor_t cursor = NULL;
201 xbt_dict_foreach(root->children, cursor, child_name, child) {
202 container_t ret = recursiveGetContainer(name, child);
208 container_t getContainer (const char *name)
210 return recursiveGetContainer(name, rootContainer);
213 container_t getContainerByName (const char *name)
215 return (container_t)xbt_dict_get (allContainers, name);
218 char *getContainerIdByName (const char *name)
220 return getContainerByName(name)->id;
223 container_t getRootContainer ()
225 return rootContainer;
228 static type_t recursiveGetType (const char *name, type_t root)
230 if (strcmp (root->name, name) == 0) return root;
232 xbt_dict_cursor_t cursor = NULL;
235 xbt_dict_foreach(root->children, cursor, child_name, child) {
236 type_t ret = recursiveGetType(name, child);
242 type_t getType (const char *name)
244 return recursiveGetType (name, rootType);
247 void destroyContainer (container_t container)
249 //remove me from my father
250 if (container->father){
251 xbt_dict_remove(container->father->children, container->name);
254 DEBUG1("destroy container %s", container->name);
256 //obligation to dump previous events because they might
257 //reference the container that is about to be destroyed
258 TRACE_last_timestamp_to_dump = surf_get_clock();
259 TRACE_paje_dump_buffer(1);
261 //trace my destruction
262 new_pajeDestroyContainer(container);
265 xbt_free (container->name);
266 xbt_free (container->id);
267 xbt_free (container->children);
268 xbt_free (container);
272 static void recursiveDestroyContainer (container_t container)
274 xbt_dict_cursor_t cursor = NULL;
277 xbt_dict_foreach(container->children, cursor, child_name, child) {
278 recursiveDestroyContainer (child);
280 destroyContainer (container);
283 static void recursiveDestroyType (type_t type)
285 xbt_dict_cursor_t cursor = NULL;
288 xbt_dict_foreach(type->children, cursor, child_name, child) {
289 recursiveDestroyType (child);
291 xbt_free (type->name);
293 xbt_free (type->children);
298 void destroyAllContainers ()
300 if (getRootContainer()) recursiveDestroyContainer (getRootContainer());
301 if (getRootType()) recursiveDestroyType (getRootType());
302 rootContainer = NULL;
307 #endif /* HAVE_TRACING */