Logo AND Algorithmique Numérique Distribuée

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