Logo AND Algorithmique Numérique Distribuée

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