Logo AND Algorithmique Numérique Distribuée

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