Logo AND Algorithmique Numérique Distribuée

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