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_homogeneous(NULL);
22 trivaNodeTypes = xbt_dict_new_homogeneous(xbt_free);
23 trivaEdgeTypes = xbt_dict_new_homogeneous(xbt_free);
27 void instr_paje_free (void)
29 xbt_dict_free (&allContainers);
30 xbt_dict_free (&trivaNodeTypes);
31 xbt_dict_free (&trivaEdgeTypes);
34 static long long int new_type_id (void)
36 static long long int type_id = 0;
40 static void destroyValue (void *value)
42 xbt_free(((val_t)value)->name);
43 xbt_free(((val_t)value)->color);
44 xbt_free(((val_t)value)->id);
48 static val_t newValue (const char *valuename, const char *color, type_t father)
50 val_t ret = xbt_new0(s_val_t, 1);
51 ret->name = xbt_strdup (valuename);
53 ret->color = xbt_strdup (color);
55 char str_id[INSTR_DEFAULT_STR_SIZE];
56 snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", new_type_id());
57 ret->id = xbt_strdup (str_id);
59 xbt_dict_set (father->values, valuename, ret, NULL);
60 XBT_DEBUG("new value %s, child of %s", ret->name, ret->father->name);
64 val_t getValue (const char *valuename, const char *color, type_t father)
66 if (father->kind == TYPE_VARIABLE) return NULL; //Variables can't have different values
68 val_t ret = (val_t)xbt_dict_get_or_null (father->values, valuename);
70 ret = newValue (valuename, color, father);
71 XBT_DEBUG("EntityValue %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
72 new_pajeDefineEntityValue(ret);
77 val_t getValueByName (const char *valuename, type_t father)
79 return getValue (valuename, NULL, father);
82 static type_t newType (const char *typename, const char *key, const char *color, e_entity_types kind, type_t father)
84 type_t ret = xbt_new0(s_type_t, 1);
85 ret->name = xbt_strdup (typename);
88 ret->children = xbt_dict_new_homogeneous(NULL);
89 ret->values = xbt_dict_new_homogeneous(NULL);
90 ret->color = xbt_strdup (color);
92 char str_id[INSTR_DEFAULT_STR_SIZE];
93 snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", new_type_id());
94 ret->id = xbt_strdup (str_id);
97 xbt_dict_set (father->children, key, ret, NULL);
98 XBT_DEBUG("new type %s, child of %s", typename, father->name);
103 type_t getRootType ()
108 type_t getContainerType (const char *typename, type_t father)
112 ret = newType (typename, typename, NULL, TYPE_CONTAINER, father);
113 if (father) new_pajeDefineContainerType (ret);
116 //check if my father type already has my typename
117 ret = (type_t)xbt_dict_get_or_null (father->children, typename);
119 ret = newType (typename, typename, NULL, TYPE_CONTAINER, father);
120 new_pajeDefineContainerType (ret);
126 type_t getEventType (const char *typename, const char *color, type_t father)
128 type_t ret = xbt_dict_get_or_null (father->children, typename);
130 char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
132 ret = newType (typename, typename, white, TYPE_EVENT, father);
134 ret = newType (typename, typename, color, TYPE_EVENT, father);
136 XBT_DEBUG("EventType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
137 new_pajeDefineEventType(ret);
142 type_t getVariableType (const char *typename, const char *color, type_t father)
144 type_t ret = xbt_dict_get_or_null (father->children, typename);
146 char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
148 ret = newType (typename, typename, white, TYPE_VARIABLE, father);
150 ret = newType (typename, typename, color, TYPE_VARIABLE, father);
152 XBT_DEBUG("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
153 new_pajeDefineVariableType (ret);
158 char *getVariableTypeIdByName (const char *name, type_t father)
160 xbt_dict_cursor_t cursor = NULL;
163 xbt_dict_foreach(father->children, cursor, key, type) {
164 if (strcmp (name, type->name) == 0) return type->id;
169 type_t getLinkType (const char *typename, type_t father, type_t source, type_t dest)
171 char key[INSTR_DEFAULT_STR_SIZE];
172 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", typename, source->id, dest->id);
173 type_t ret = xbt_dict_get_or_null (father->children, key);
175 ret = newType (typename, key, NULL, TYPE_LINK, father);
176 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);
177 new_pajeDefineLinkType(ret, source, dest);
182 type_t getStateType (const char *typename, type_t father)
184 type_t ret = xbt_dict_get_or_null (father->children, typename);
186 ret = newType (typename, typename, NULL, TYPE_STATE, father);
187 XBT_DEBUG("StateType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
188 new_pajeDefineStateType(ret);
193 container_t newContainer (const char *name, e_container_types kind, container_t father)
195 static long long int container_id = 0;
196 char id_str[INSTR_DEFAULT_STR_SIZE];
197 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", container_id++);
199 container_t new = xbt_new0(s_container_t, 1);
200 new->name = xbt_strdup (name); // name of the container
201 new->id = xbt_strdup (id_str); // id (or alias) of the container
202 new->father = father;
203 // level depends on level of father
205 new->level = new->father->level+1;
206 XBT_DEBUG("new container %s, child of %s", name, father->name);
210 // type definition (method depends on kind of this new container)
212 if (new->kind == INSTR_AS){
213 //if this container is of an AS, its type name depends on its level
214 char as_typename[INSTR_DEFAULT_STR_SIZE];
215 snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
217 new->type = getContainerType (as_typename, new->father->type);
219 new->type = getContainerType ("0", NULL);
222 //otherwise, the name is its kind
224 case INSTR_HOST: new->type = getContainerType ("HOST", new->father->type); break;
225 case INSTR_LINK: new->type = getContainerType ("LINK", new->father->type); break;
226 case INSTR_ROUTER: new->type = getContainerType ("ROUTER", new->father->type); break;
227 case INSTR_SMPI: new->type = getContainerType ("MPI", new->father->type); break;
228 case INSTR_MSG_PROCESS: new->type = getContainerType ("MSG_PROCESS", new->father->type); break;
229 case INSTR_MSG_TASK: new->type = getContainerType ("MSG_TASK", new->father->type); break;
230 default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
233 new->children = xbt_dict_new_homogeneous(NULL);
235 xbt_dict_set(new->father->children, new->name, new, NULL);
236 new_pajeCreateContainer (new);
239 //register hosts, routers, links containers
240 if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
241 xbt_dict_set (allContainers, new->name, new, NULL);
243 //register NODE types for triva configuration
244 xbt_dict_set (trivaNodeTypes, new->type->name, xbt_strdup("1"), NULL);
249 static container_t recursiveGetContainer (const char *name, container_t root)
251 if (name == NULL || root == NULL) return NULL;
252 if (strcmp (root->name, name) == 0) return root;
254 xbt_dict_cursor_t cursor = NULL;
257 xbt_dict_foreach(root->children, cursor, child_name, child) {
258 container_t ret = recursiveGetContainer(name, child);
264 container_t getContainer (const char *name)
266 if (name == NULL) return NULL;
267 return recursiveGetContainer(name, rootContainer);
270 int knownContainerWithName (const char *name)
272 if (xbt_dict_get_or_null (allContainers, name)){
279 container_t getContainerByName (const char *name)
281 return (container_t)xbt_dict_get (allContainers, name);
284 char *getContainerIdByName (const char *name)
286 return getContainerByName(name)->id;
289 container_t getRootContainer ()
291 return rootContainer;
294 static type_t recursiveGetType (const char *name, type_t root)
296 if (strcmp (root->name, name) == 0) return root;
298 xbt_dict_cursor_t cursor = NULL;
301 xbt_dict_foreach(root->children, cursor, child_name, child) {
302 type_t ret = recursiveGetType(name, child);
308 type_t getType (const char *name, type_t father)
310 return recursiveGetType (name, father);
313 void destroyContainer (container_t container)
315 XBT_DEBUG("destroy container %s", container->name);
317 //obligation to dump previous events because they might
318 //reference the container that is about to be destroyed
319 TRACE_last_timestamp_to_dump = surf_get_clock();
320 TRACE_paje_dump_buffer(1);
322 //trace my destruction
323 if (!TRACE_disable_destroy()){
324 //do not trace the container destruction if user requests
325 new_pajeDestroyContainer(container);
329 xbt_free (container->name);
330 xbt_free (container->id);
331 xbt_dict_free (&container->children);
332 xbt_free (container);
336 static void recursiveDestroyContainer (container_t container)
338 xbt_dict_cursor_t cursor = NULL;
341 xbt_dict_foreach(container->children, cursor, child_name, child) {
342 recursiveDestroyContainer (child);
344 destroyContainer (container);
347 static void recursiveDestroyType (type_t type)
349 xbt_dict_cursor_t cursor = NULL;
352 xbt_dict_foreach(type->children, cursor, child_name, child) {
353 recursiveDestroyType (child);
355 xbt_free (type->name);
357 xbt_free (type->color);
358 xbt_dict_free (&type->children);
361 xbt_dict_foreach(type->values, cursor, value_name, value) {
362 destroyValue (value);
364 xbt_dict_free (&type->values);
369 void destroyAllContainers ()
371 if (getRootContainer()) recursiveDestroyContainer (getRootContainer());
372 if (getRootType()) recursiveDestroyType (getRootType());
373 rootContainer = NULL;
378 #endif /* HAVE_TRACING */