Logo AND Algorithmique Numérique Distribuée

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