Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
leak --(cherry picked from commit dd1330c53beec9c5abb8b23375062e6acf214632)
[simgrid.git] / src / instr / instr_TI_trace.c
1 /* Copyright (c) 2010-2013. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6 #include "instr/instr_private.h"
7 #include "xbt/virtu.h" /* sg_cmdline */
8 #include "xbt/xbt_os_time.h"
9 #include "simgrid/sg_config.h"
10
11 #include <errno.h>
12 #include <string.h>
13 #include <sys/stat.h>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_TI_trace, instr_trace, "tracing event system");
16
17 extern FILE *tracing_file;
18 double prefix=0.0;
19
20 xbt_dict_t tracing_files = NULL;
21
22 extern s_instr_trace_writer_t active_writer;
23
24 void TRACE_TI_init(void)
25 {
26   active_writer.print_PushState = print_TIPushState;
27   active_writer.print_CreateContainer=print_TICreateContainer;
28   active_writer.print_DestroyContainer=print_TIDestroyContainer;
29 }
30
31 void TRACE_TI_start(void)
32 {
33   char *filename = TRACE_get_filename();
34   tracing_file = fopen(filename, "w");
35   if (tracing_file == NULL) {
36     THROWF(system_error, 1, "Tracefile %s could not be opened for writing.",
37            filename);
38   }
39
40   XBT_DEBUG("Filename %s is open for writing", filename);
41
42   /* output one line comment */
43   dump_comment(TRACE_get_comment());
44
45   /* output comment file */
46   dump_comment_file(TRACE_get_comment_file());
47
48 }
49
50 void TRACE_TI_end(void)
51 {
52   xbt_dict_free(&tracing_files);
53   fclose(tracing_file);
54   char *filename = TRACE_get_filename();
55   XBT_DEBUG("Filename %s is closed", filename);
56 }
57
58 void print_TICreateContainer(paje_event_t event)
59 {
60   //if we are in the mode with only one file
61   static FILE *temp = NULL;
62
63   if (tracing_files == NULL) {
64     tracing_files = xbt_dict_new_homogeneous(NULL);
65     //generate unique run id with time
66     prefix = xbt_os_time();
67   }
68
69   if (!xbt_cfg_get_boolean(_sg_cfg_set, "tracing/smpi/format/ti_one_file")
70       || temp == NULL) {
71     char *folder_name = bprintf("%s_files", TRACE_get_filename());
72     char *filename = bprintf("%s/%f_%s.txt", folder_name, prefix,
73                              ((createContainer_t) event->data)->container->name);
74     mkdir(folder_name, S_IRWXU | S_IRWXG | S_IRWXO);
75     temp = fopen(filename, "w");
76     if (temp == NULL)
77       xbt_die("Tracefile %s could not be opened for writing: %s",
78               filename, strerror(errno));
79     fprintf(tracing_file, "%s\n", filename);
80
81     xbt_free(folder_name);
82     xbt_free(filename);
83   }
84
85   xbt_dict_set(tracing_files,
86                ((createContainer_t) event->data)->container->name,
87                (void *) temp, NULL);
88 }
89
90 void print_TIDestroyContainer(paje_event_t event)
91 {
92   if (!xbt_cfg_get_boolean(_sg_cfg_set, "tracing/smpi/format/ti_one_file")||
93       xbt_dict_length(tracing_files) == 1) {
94     FILE* f = xbt_dict_get_or_null(tracing_files,
95         ((destroyContainer_t) event->data)->container->name);
96     fclose(f);
97   }
98   xbt_dict_remove(tracing_files, ((destroyContainer_t) event->data)->container->name);
99 }
100
101
102 void print_TIPushState(paje_event_t event)
103 {
104
105
106   int i;
107
108   //char* function=NULL;
109   if (((pushState_t) event->data)->extra == NULL)
110     return;
111   instr_extra_data extra =
112       (instr_extra_data) (((pushState_t) event->data)->extra);
113
114   char *process_id = NULL;
115   //FIXME: dirty extract "rank-" from the name, as we want the bare process id here
116   if (strstr(((pushState_t) event->data)->container->name, "rank-") == NULL)
117     process_id = xbt_strdup(((pushState_t) event->data)->container->name);
118   else
119     process_id = xbt_strdup(((pushState_t) event->data)->container->name + 5);
120
121   FILE* trace_file =  (FILE* )xbt_dict_get(tracing_files, ((pushState_t) event->data)->container->name);
122
123   switch (extra->type) {
124
125   case TRACING_INIT:
126     fprintf(trace_file, "%s init\n", process_id);
127     break;
128   case TRACING_FINALIZE:
129     fprintf(trace_file, "%s finalize\n", process_id);
130     break;
131   case TRACING_SEND:
132     fprintf(trace_file, "%s send %d %d %s\n", process_id, extra->dst,
133             extra->send_size, extra->datatype1);
134     break;
135   case TRACING_ISEND:
136     fprintf(trace_file, "%s isend %d %d %s\n", process_id, extra->dst,
137             extra->send_size, extra->datatype1);
138     break;
139   case TRACING_RECV:
140     fprintf(trace_file, "%s recv %d %d %s\n", process_id, extra->src,
141             extra->send_size, extra->datatype1);
142     break;
143   case TRACING_IRECV:
144     fprintf(trace_file, "%s irecv %d %d %s\n", process_id, extra->src,
145             extra->send_size, extra->datatype1);
146     break;
147   case TRACING_WAIT:
148     fprintf(trace_file, "%s wait\n", process_id);
149     break;
150   case TRACING_WAITALL:
151     fprintf(trace_file, "%s waitall\n", process_id);
152     break;
153   case TRACING_BARRIER:
154     fprintf(trace_file, "%s barrier\n", process_id);
155     break;
156   case TRACING_BCAST:          // rank bcast size (root) (datatype)
157     fprintf(trace_file, "%s bcast %d ", process_id, extra->send_size);
158     if (extra->root != 0 || (extra->datatype1 && strcmp(extra->datatype1, "")))
159       fprintf(trace_file, "%d %s", extra->root, extra->datatype1);
160     fprintf(trace_file, "\n");
161     break;
162   case TRACING_REDUCE:         // rank reduce comm_size comp_size (root) (datatype)
163     fprintf(trace_file, "%s reduce %d %f ", process_id, extra->send_size,
164             extra->comp_size);
165     if (extra->root != 0 || (extra->datatype1 && strcmp(extra->datatype1, "")))
166       fprintf(trace_file, "%d %s", extra->root, extra->datatype1);
167     fprintf(trace_file, "\n");
168     break;
169   case TRACING_ALLREDUCE:      // rank allreduce comm_size comp_size (datatype)
170     fprintf(trace_file, "%s allreduce %d %f %s\n", process_id,
171             extra->send_size, extra->comp_size, extra->datatype1);
172     break;
173   case TRACING_ALLTOALL:       // rank alltoall send_size recv_size (sendtype) (recvtype)
174     fprintf(trace_file, "%s alltoall %d %d %s %s\n", process_id,
175             extra->send_size, extra->recv_size, extra->datatype1,
176             extra->datatype2);
177     break;
178   case TRACING_ALLTOALLV:      // rank alltoallv send_size [sendcounts] recv_size [recvcounts] (sendtype) (recvtype)
179     fprintf(trace_file, "%s alltoallv %d ", process_id, extra->send_size);
180     for (i = 0; i < extra->num_processes; i++)
181       fprintf(trace_file, "%d ", extra->sendcounts[i]);
182     fprintf(trace_file, "%d ", extra->recv_size);
183     for (i = 0; i < extra->num_processes; i++)
184       fprintf(trace_file, "%d ", extra->recvcounts[i]);
185     fprintf(trace_file, "%s %s \n", extra->datatype1, extra->datatype2);
186     break;
187   case TRACING_GATHER:         // rank gather send_size recv_size root (sendtype) (recvtype)
188     fprintf(trace_file, "%s gather %d %d %d %s %s\n", process_id,
189             extra->send_size, extra->recv_size, extra->root, extra->datatype1,
190             extra->datatype2);
191     break;
192   case TRACING_ALLGATHERV:     // rank allgatherv send_size [recvcounts] (sendtype) (recvtype)
193     fprintf(trace_file, "%s allgatherv %d ", process_id, extra->send_size);
194     for (i = 0; i < extra->num_processes; i++)
195       fprintf(trace_file, "%d ", extra->recvcounts[i]);
196     fprintf(trace_file, "%s %s \n", extra->datatype1, extra->datatype2);
197     break;
198   case TRACING_REDUCE_SCATTER: // rank reducescatter [recvcounts] comp_size (sendtype)
199     fprintf(trace_file, "%s reducescatter ", process_id);
200     for (i = 0; i < extra->num_processes; i++)
201       fprintf(trace_file, "%d ", extra->recvcounts[i]);
202     fprintf(trace_file, "%f %s\n", extra->comp_size, extra->datatype1);
203     break;
204   case TRACING_COMPUTING:
205     fprintf(trace_file, "%s compute %f\n", process_id, extra->comp_size);
206     break;
207   case TRACING_GATHERV: // rank gatherv send_size [recvcounts] root (sendtype) (recvtype)
208     fprintf(trace_file, "%s gatherv %d ", process_id, extra->send_size);
209     for (i = 0; i < extra->num_processes; i++)
210       fprintf(trace_file, "%d ", extra->recvcounts[i]);
211     fprintf(trace_file, "%d %s %s\n", extra->root, extra->datatype1, extra->datatype2);
212     break;
213   case TRACING_WAITANY:
214   case TRACING_SENDRECV:
215   case TRACING_SCATTER:
216   case TRACING_SCATTERV:
217   case TRACING_ALLGATHER:
218   case TRACING_SCAN:
219   case TRACING_EXSCAN:
220   case TRACING_COMM_SIZE:
221   case TRACING_COMM_SPLIT:
222   case TRACING_COMM_DUP:
223   case TRACING_SSEND:
224   case TRACING_ISSEND:
225   default:
226
227     XBT_WARN
228         ("Call from %s impossible to translate into replay command : Not implemented (yet)",
229          ((pushState_t) event->data)->value->name);
230     break;
231   }
232
233   if (extra->recvcounts != NULL)
234     xbt_free(extra->recvcounts);
235   if (extra->sendcounts != NULL)
236     xbt_free(extra->sendcounts);
237   xbt_free(process_id);
238   xbt_free(extra);
239 }