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 (void)
21 allContainers = xbt_dict_new_homogeneous(NULL);
22 trivaNodeTypes = xbt_dict_new_homogeneous(xbt_free);
23 trivaEdgeTypes = xbt_dict_new_homogeneous(xbt_free);
26 void instr_paje_set_root (container_t root)
31 void instr_paje_free (void)
33 xbt_dict_free (&allContainers);
34 xbt_dict_free (&trivaNodeTypes);
35 xbt_dict_free (&trivaEdgeTypes);
38 static long long int new_type_id (void)
40 static long long int type_id = 0;
44 static void destroyValue (void *value)
46 xbt_free(((val_t)value)->name);
47 xbt_free(((val_t)value)->color);
48 xbt_free(((val_t)value)->id);
52 static val_t newValue (const char *valuename, const char *color, type_t father)
54 val_t ret = xbt_new0(s_val_t, 1);
55 ret->name = xbt_strdup (valuename);
57 ret->color = xbt_strdup (color);
59 char str_id[INSTR_DEFAULT_STR_SIZE];
60 snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", new_type_id());
61 ret->id = xbt_strdup (str_id);
63 xbt_dict_set (father->values, valuename, ret, NULL);
64 XBT_DEBUG("new value %s, child of %s", ret->name, ret->father->name);
68 val_t getValue (const char *valuename, const char *color, type_t father)
70 if (father->kind == TYPE_VARIABLE) return NULL; //Variables can't have different values
72 val_t ret = (val_t)xbt_dict_get_or_null (father->values, valuename);
74 ret = newValue (valuename, color, father);
75 XBT_DEBUG("EntityValue %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
76 new_pajeDefineEntityValue(ret);
81 val_t getValueByName (const char *valuename, type_t father)
83 return getValue (valuename, NULL, father);
86 static type_t newType (const char *typename, const char *key, const char *color, e_entity_types kind, type_t father)
88 type_t ret = xbt_new0(s_type_t, 1);
89 ret->name = xbt_strdup (typename);
92 ret->children = xbt_dict_new_homogeneous(NULL);
93 ret->values = xbt_dict_new_homogeneous(NULL);
94 ret->color = xbt_strdup (color);
96 char str_id[INSTR_DEFAULT_STR_SIZE];
97 snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", new_type_id());
98 ret->id = xbt_strdup (str_id);
101 xbt_dict_set (father->children, key, ret, NULL);
102 XBT_DEBUG("new type %s, child of %s", typename, father->name);
107 type_t getRootType ()
112 type_t getContainerType (const char *typename, type_t father)
116 ret = newType (typename, typename, NULL, TYPE_CONTAINER, father);
117 if (father) new_pajeDefineContainerType (ret);
120 //check if my father type already has my typename
121 ret = (type_t)xbt_dict_get_or_null (father->children, typename);
123 ret = newType (typename, typename, NULL, TYPE_CONTAINER, father);
124 new_pajeDefineContainerType (ret);
130 type_t getEventType (const char *typename, const char *color, type_t father)
132 type_t ret = xbt_dict_get_or_null (father->children, typename);
134 char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
136 ret = newType (typename, typename, white, TYPE_EVENT, father);
138 ret = newType (typename, typename, color, TYPE_EVENT, father);
140 XBT_DEBUG("EventType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
141 new_pajeDefineEventType(ret);
146 type_t getVariableType (const char *typename, const char *color, type_t father)
148 type_t ret = xbt_dict_get_or_null (father->children, typename);
150 char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
152 ret = newType (typename, typename, white, TYPE_VARIABLE, father);
154 ret = newType (typename, typename, color, TYPE_VARIABLE, father);
156 XBT_DEBUG("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
157 new_pajeDefineVariableType (ret);
162 char *getVariableTypeIdByName (const char *name, type_t father)
164 xbt_dict_cursor_t cursor = NULL;
167 xbt_dict_foreach(father->children, cursor, key, type) {
168 if (strcmp (name, type->name) == 0) return type->id;
173 type_t getLinkType (const char *typename, type_t father, type_t source, type_t dest)
175 char key[INSTR_DEFAULT_STR_SIZE];
176 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", typename, source->id, dest->id);
177 type_t ret = xbt_dict_get_or_null (father->children, key);
179 ret = newType (typename, key, NULL, TYPE_LINK, father);
180 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);
181 new_pajeDefineLinkType(ret, source, dest);
186 type_t getStateType (const char *typename, type_t father)
188 type_t ret = xbt_dict_get_or_null (father->children, typename);
190 ret = newType (typename, typename, NULL, TYPE_STATE, father);
191 XBT_DEBUG("StateType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
192 new_pajeDefineStateType(ret);
197 container_t newContainer (const char *name, e_container_types kind, container_t father)
199 static long long int container_id = 0;
200 char id_str[INSTR_DEFAULT_STR_SIZE];
201 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", container_id++);
203 container_t new = xbt_new0(s_container_t, 1);
204 new->name = xbt_strdup (name); // name of the container
205 new->id = xbt_strdup (id_str); // id (or alias) of the container
206 new->father = father;
207 // level depends on level of father
209 new->level = new->father->level+1;
210 XBT_DEBUG("new container %s, child of %s", name, father->name);
214 // type definition (method depends on kind of this new container)
216 if (new->kind == INSTR_AS){
217 //if this container is of an AS, its type name depends on its level
218 char as_typename[INSTR_DEFAULT_STR_SIZE];
219 snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
221 new->type = getContainerType (as_typename, new->father->type);
223 new->type = getContainerType ("0", NULL);
226 //otherwise, the name is its kind
228 case INSTR_HOST: new->type = getContainerType ("HOST", new->father->type); break;
229 case INSTR_LINK: new->type = getContainerType ("LINK", new->father->type); break;
230 case INSTR_ROUTER: new->type = getContainerType ("ROUTER", new->father->type); break;
231 case INSTR_SMPI: new->type = getContainerType ("MPI", new->father->type); break;
232 case INSTR_MSG_PROCESS: new->type = getContainerType ("MSG_PROCESS", new->father->type); break;
233 case INSTR_MSG_TASK: new->type = getContainerType ("MSG_TASK", new->father->type); break;
234 default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
237 new->children = xbt_dict_new_homogeneous(NULL);
239 xbt_dict_set(new->father->children, new->name, new, NULL);
240 new_pajeCreateContainer (new);
243 //register hosts, routers, links containers
244 if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
245 xbt_dict_set (allContainers, new->name, new, NULL);
247 //register NODE types for triva configuration
248 xbt_dict_set (trivaNodeTypes, new->type->name, xbt_strdup("1"), NULL);
253 static container_t recursiveGetContainer (const char *name, container_t root)
255 if (name == NULL || root == NULL) return NULL;
256 if (strcmp (root->name, name) == 0) return root;
258 xbt_dict_cursor_t cursor = NULL;
261 xbt_dict_foreach(root->children, cursor, child_name, child) {
262 container_t ret = recursiveGetContainer(name, child);
268 container_t getContainer (const char *name)
270 if (name == NULL) return NULL;
271 return recursiveGetContainer(name, rootContainer);
274 int knownContainerWithName (const char *name)
276 if (xbt_dict_get_or_null (allContainers, name)){
283 container_t getContainerByName (const char *name)
285 return (container_t)xbt_dict_get (allContainers, name);
288 char *getContainerIdByName (const char *name)
290 return getContainerByName(name)->id;
293 container_t getRootContainer ()
295 return rootContainer;
298 static type_t recursiveGetType (const char *name, type_t root)
300 if (strcmp (root->name, name) == 0) return root;
302 xbt_dict_cursor_t cursor = NULL;
306 xbt_dict_foreach(root->children, cursor, child_name, child) {
307 type_t found = recursiveGetType(name, child);
312 XBT_CRITICAL("[tracing] found two types with the same name");
319 type_t getType (const char *name, type_t father)
321 return recursiveGetType (name, father);
324 void removeContainerFromParent (container_t child)
326 container_t parent = child->father;
328 XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ",
331 xbt_dict_remove (parent->children, child->name);
335 void destroyContainer (container_t container)
337 XBT_DEBUG("destroy container %s", container->name);
339 //obligation to dump previous events because they might
340 //reference the container that is about to be destroyed
341 TRACE_last_timestamp_to_dump = surf_get_clock();
342 TRACE_paje_dump_buffer(1);
344 //trace my destruction
345 if (!TRACE_disable_destroy()){
346 //do not trace the container destruction if user requests
347 new_pajeDestroyContainer(container);
350 //remove it from allContainers data structure
351 xbt_dict_remove (allContainers, container->name);
354 xbt_free (container->name);
355 xbt_free (container->id);
356 xbt_dict_free (&container->children);
357 xbt_free (container);
361 static void recursiveDestroyContainer (container_t container)
363 XBT_DEBUG("recursiveDestroyContainer %s", container->name);
364 xbt_dict_cursor_t cursor = NULL;
367 xbt_dict_foreach(container->children, cursor, child_name, child) {
368 recursiveDestroyContainer (child);
370 destroyContainer (container);
373 static void recursiveDestroyType (type_t type)
375 XBT_DEBUG("recursiveDestroyType %s", type->name);
376 xbt_dict_cursor_t cursor = NULL;
379 xbt_dict_foreach(type->children, cursor, child_name, child) {
380 recursiveDestroyType (child);
382 xbt_free (type->name);
384 xbt_free (type->color);
385 xbt_dict_free (&type->children);
388 xbt_dict_foreach(type->values, cursor, value_name, value) {
389 destroyValue (value);
391 xbt_dict_free (&type->values);
396 void destroyAllContainers ()
398 if (getRootContainer()) recursiveDestroyContainer (getRootContainer());
399 if (getRootType()) recursiveDestroyType (getRootType());
400 rootContainer = NULL;
405 #endif /* HAVE_TRACING */