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_dynar_t allLinkTypes = NULL; /* all link types defined */
17 xbt_dynar_t allHostTypes = NULL; /* all host types defined */
19 void instr_paje_init (container_t root)
21 allContainers = xbt_dict_new ();
22 allLinkTypes = xbt_dynar_new (sizeof(s_type_t), NULL);
23 allHostTypes = xbt_dynar_new (sizeof(s_type_t), NULL);
27 static long long int newTypeId ()
29 static long long int counter = 0;
33 static type_t newType (const char *typename, const char *key, 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 ();
41 long long int id = newTypeId();
42 char str_id[INSTR_DEFAULT_STR_SIZE];
43 snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", id);
44 ret->id = xbt_strdup (str_id);
47 xbt_dict_set (father->children, key, ret, NULL);
57 type_t getContainerType (const char *typename, type_t father)
61 ret = newType (typename, typename, TYPE_CONTAINER, father);
62 if (father) pajeDefineContainerType(ret->id, ret->father->id, ret->name);
65 //check if my father type already has my typename
66 ret = (type_t)xbt_dict_get_or_null (father->children, typename);
68 ret = newType (typename, typename, TYPE_CONTAINER, father);
69 pajeDefineContainerType(ret->id, ret->father->id, ret->name);
75 type_t getEventType (const char *typename, const char *color, type_t father)
77 type_t ret = xbt_dict_get_or_null (father->children, typename);
79 ret = newType (typename, typename, TYPE_EVENT, father);
80 //INFO4("EventType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
82 pajeDefineEventTypeWithColor (ret->id, ret->father->id, ret->name, color);
84 pajeDefineEventType(ret->id, ret->father->id, ret->name);
90 type_t getVariableType (const char *typename, const char *color, type_t father)
92 type_t ret = xbt_dict_get_or_null (father->children, typename);
94 ret = newType (typename, typename, TYPE_VARIABLE, father);
95 //INFO4("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
97 pajeDefineVariableTypeWithColor(ret->id, ret->father->id, ret->name, color);
99 pajeDefineVariableType(ret->id, ret->father->id, ret->name);
105 char *getVariableTypeIdByName (const char *name, type_t father)
107 xbt_dict_cursor_t cursor = NULL;
110 xbt_dict_foreach(father->children, cursor, key, type) {
111 if (strcmp (name, type->name) == 0) return type->id;
116 type_t getLinkType (const char *typename, type_t father, type_t source, type_t dest)
118 //FIXME should check using source and dest here and not by the typename (g5k example)
119 char key[INSTR_DEFAULT_STR_SIZE];
120 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", typename, source->id, dest->id);
121 type_t ret = xbt_dict_get_or_null (father->children, key);
123 ret = newType (typename, key, TYPE_LINK, father);
124 //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);
125 pajeDefineLinkType(ret->id, ret->father->id, source->id, dest->id, ret->name);
130 type_t getStateType (const char *typename, type_t father)
132 type_t ret = xbt_dict_get_or_null (father->children, typename);
134 ret = newType (typename, typename, TYPE_STATE, father);
135 //INFO4("StateType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
136 pajeDefineStateType(ret->id, ret->father->id, ret->name);
142 static long long int newContainedId ()
144 static long long counter = 0;
148 container_t newContainer (const char *name, e_container_types kind, container_t father)
150 long long int counter = newContainedId();
151 char id_str[INSTR_DEFAULT_STR_SIZE];
152 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", counter);
154 container_t new = xbt_new0(s_container_t, 1);
155 new->name = xbt_strdup (name); // name of the container
156 new->id = xbt_strdup (id_str); // id (or alias) of the container
157 new->father = father;
158 // level depends on level of father
160 new->level = new->father->level+1;
164 // type definition (method depends on kind of this new container)
166 if (new->kind == INSTR_AS){
167 //if this container is of an AS, its type name depends on its level
168 char as_typename[INSTR_DEFAULT_STR_SIZE];
169 snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
171 new->type = getContainerType (as_typename, new->father->type);
173 new->type = getContainerType ("0", NULL);
176 //otherwise, the name is its kind
178 case INSTR_HOST: new->type = getContainerType ("HOST", new->father->type); break;
179 case INSTR_LINK: new->type = getContainerType ("LINK", new->father->type); break;
180 case INSTR_ROUTER: new->type = getContainerType ("ROUTER", new->father->type); break;
181 case INSTR_SMPI: new->type = getContainerType ("MPI", new->father->type); break;
182 case INSTR_MSG_PROCESS: new->type = getContainerType ("MSG_PROCESS", new->father->type); break;
183 case INSTR_MSG_TASK: new->type = getContainerType ("MSG_TASK", new->father->type); break;
184 default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
187 new->children = xbt_dict_new();
189 xbt_dict_set(new->father->children, new->name, new, NULL);
190 pajeCreateContainer (SIMIX_get_clock(), new->id, new->type->id, new->father->id, new->name);
193 //register hosts, routers, links containers
194 if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
195 xbt_dict_set (allContainers, new->name, new, NULL);
198 //register the host container types
199 if (new->kind == INSTR_HOST){
200 xbt_dynar_push_as (allHostTypes, type_t, new->type);
203 //register the link container types
204 if (new->kind == INSTR_LINK){
205 xbt_dynar_push_as(allLinkTypes, type_t, new->type);
210 static container_t recursiveGetContainer (const char *name, container_t root)
212 if (strcmp (root->name, name) == 0) return root;
214 xbt_dict_cursor_t cursor = NULL;
217 xbt_dict_foreach(root->children, cursor, child_name, child) {
218 container_t ret = recursiveGetContainer(name, child);
224 container_t getContainer (const char *name)
226 return recursiveGetContainer(name, rootContainer);
229 container_t getContainerByName (const char *name)
231 return (container_t)xbt_dict_get (allContainers, name);
234 char *getContainerIdByName (const char *name)
236 return getContainerByName(name)->id;
239 container_t getRootContainer ()
241 return rootContainer;
244 static type_t recursiveGetType (const char *name, type_t root)
246 if (strcmp (root->name, name) == 0) return root;
248 xbt_dict_cursor_t cursor = NULL;
251 xbt_dict_foreach(root->children, cursor, child_name, child) {
252 type_t ret = recursiveGetType(name, child);
258 type_t getType (const char *name)
260 return recursiveGetType (name, rootType);
263 void destroyContainer (container_t container)
265 //remove me from my father
266 if (container->father){
267 xbt_dict_remove(container->father->children, container->name);
270 //trace my destruction
271 pajeDestroyContainer(SIMIX_get_clock(), container->type->id, container->id);
274 xbt_free (container->name);
275 xbt_free (container->id);
276 xbt_free (container->children);
277 xbt_free (container);
281 static void recursiveDestroyContainer (container_t container)
283 xbt_dict_cursor_t cursor = NULL;
286 xbt_dict_foreach(container->children, cursor, child_name, child) {
287 recursiveDestroyContainer (child);
289 destroyContainer (container);
292 static void recursiveDestroyType (type_t type)
294 xbt_dict_cursor_t cursor = NULL;
297 xbt_dict_foreach(type->children, cursor, child_name, child) {
298 recursiveDestroyType (child);
300 xbt_free (type->name);
302 xbt_free (type->children);
307 void destroyAllContainers ()
309 if (getRootContainer()) recursiveDestroyContainer (getRootContainer());
310 if (getRootType()) recursiveDestroyType (getRootType());
311 rootContainer = NULL;
316 #endif /* HAVE_TRACING */