1 /* Copyright (c) 2010-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
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"
17 #include <direct.h> // _mkdir
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_TI_trace, instr_trace, "tracing event system");
22 extern FILE *tracing_file;
25 xbt_dict_t tracing_files = nullptr;
27 extern s_instr_trace_writer_t active_writer;
29 void TRACE_TI_init(void)
31 active_writer.print_PushState = print_TIPushState;
32 active_writer.print_CreateContainer=print_TICreateContainer;
33 active_writer.print_DestroyContainer=print_TIDestroyContainer;
36 void TRACE_TI_start(void)
38 char *filename = TRACE_get_filename();
39 tracing_file = fopen(filename, "w");
40 if (tracing_file == nullptr) {
41 THROWF(system_error, 1, "Tracefile %s could not be opened for writing.", filename);
44 XBT_DEBUG("Filename %s is open for writing", filename);
46 /* output one line comment */
47 dump_comment(TRACE_get_comment());
49 /* output comment file */
50 dump_comment_file(TRACE_get_comment_file());
53 void TRACE_TI_end(void)
55 xbt_dict_free(&tracing_files);
57 char *filename = TRACE_get_filename();
58 XBT_DEBUG("Filename %s is closed", filename);
61 void print_TICreateContainer(paje_event_t event)
63 //if we are in the mode with only one file
64 static FILE *temp = nullptr;
66 if (tracing_files == nullptr) {
67 tracing_files = xbt_dict_new_homogeneous(nullptr);
68 //generate unique run id with time
69 prefix = xbt_os_time();
72 if (!xbt_cfg_get_boolean("tracing/smpi/format/ti-one-file") || temp == nullptr) {
73 char *folder_name = bprintf("%s_files", TRACE_get_filename());
74 char *filename = bprintf("%s/%f_%s.txt", folder_name, prefix, ((createContainer_t) event->data)->container->name);
78 mkdir(folder_name, S_IRWXU | S_IRWXG | S_IRWXO);
80 temp = fopen(filename, "w");
81 xbt_assert(temp, "Tracefile %s could not be opened for writing: %s", filename, strerror(errno));
82 fprintf(tracing_file, "%s\n", filename);
84 xbt_free(folder_name);
88 xbt_dict_set(tracing_files, ((createContainer_t) event->data)->container->name, (void *) temp, nullptr);
91 void print_TIDestroyContainer(paje_event_t event)
93 if (!xbt_cfg_get_boolean("tracing/smpi/format/ti-one-file")|| xbt_dict_length(tracing_files) == 1) {
94 FILE* f = (FILE*)xbt_dict_get_or_null(tracing_files, ((destroyContainer_t) event->data)->container->name);
97 xbt_dict_remove(tracing_files, ((destroyContainer_t) event->data)->container->name);
100 void print_TIPushState(paje_event_t event)
104 //char* function=nullptr;
105 if (((pushState_t) event->data)->extra == nullptr)
107 instr_extra_data extra = (instr_extra_data) (((pushState_t) event->data)->extra);
109 char *process_id = nullptr;
110 //FIXME: dirty extract "rank-" from the name, as we want the bare process id here
111 if (strstr(((pushState_t) event->data)->container->name, "rank-") == nullptr)
112 process_id = xbt_strdup(((pushState_t) event->data)->container->name);
114 process_id = xbt_strdup(((pushState_t) event->data)->container->name + 5);
116 FILE* trace_file = (FILE* )xbt_dict_get(tracing_files, ((pushState_t) event->data)->container->name);
118 switch (extra->type) {
120 fprintf(trace_file, "%s init\n", process_id);
122 case TRACING_FINALIZE:
123 fprintf(trace_file, "%s finalize\n", process_id);
126 fprintf(trace_file, "%s send %d %d %s\n", process_id, extra->dst, extra->send_size, extra->datatype1);
129 fprintf(trace_file, "%s isend %d %d %s\n", process_id, extra->dst, extra->send_size, extra->datatype1);
132 fprintf(trace_file, "%s recv %d %d %s\n", process_id, extra->src, extra->send_size, extra->datatype1);
135 fprintf(trace_file, "%s irecv %d %d %s\n", process_id, extra->src, extra->send_size, extra->datatype1);
138 fprintf(trace_file, "%s test\n", process_id);
141 fprintf(trace_file, "%s wait\n", process_id);
143 case TRACING_WAITALL:
144 fprintf(trace_file, "%s waitall\n", process_id);
146 case TRACING_BARRIER:
147 fprintf(trace_file, "%s barrier\n", process_id);
149 case TRACING_BCAST: // rank bcast size (root) (datatype)
150 fprintf(trace_file, "%s bcast %d ", process_id, extra->send_size);
151 if (extra->root != 0 || (extra->datatype1 && strcmp(extra->datatype1, "")))
152 fprintf(trace_file, "%d %s", extra->root, extra->datatype1);
153 fprintf(trace_file, "\n");
155 case TRACING_REDUCE: // rank reduce comm_size comp_size (root) (datatype)
156 fprintf(trace_file, "%s reduce %d %f ", process_id, extra->send_size, extra->comp_size);
157 if (extra->root != 0 || (extra->datatype1 && strcmp(extra->datatype1, "")))
158 fprintf(trace_file, "%d %s", extra->root, extra->datatype1);
159 fprintf(trace_file, "\n");
161 case TRACING_ALLREDUCE: // rank allreduce comm_size comp_size (datatype)
162 fprintf(trace_file, "%s allreduce %d %f %s\n", process_id, extra->send_size, extra->comp_size, extra->datatype1);
164 case TRACING_ALLTOALL: // rank alltoall send_size recv_size (sendtype) (recvtype)
165 fprintf(trace_file, "%s alltoall %d %d %s %s\n", process_id, extra->send_size, extra->recv_size, extra->datatype1,
168 case TRACING_ALLTOALLV: // rank alltoallv send_size [sendcounts] recv_size [recvcounts] (sendtype) (recvtype)
169 fprintf(trace_file, "%s alltoallv %d ", process_id, extra->send_size);
170 for (i = 0; i < extra->num_processes; i++)
171 fprintf(trace_file, "%d ", extra->sendcounts[i]);
172 fprintf(trace_file, "%d ", extra->recv_size);
173 for (i = 0; i < extra->num_processes; i++)
174 fprintf(trace_file, "%d ", extra->recvcounts[i]);
175 fprintf(trace_file, "%s %s \n", extra->datatype1, extra->datatype2);
177 case TRACING_GATHER: // rank gather send_size recv_size root (sendtype) (recvtype)
178 fprintf(trace_file, "%s gather %d %d %d %s %s\n", process_id, extra->send_size, extra->recv_size, extra->root,
179 extra->datatype1, extra->datatype2);
181 case TRACING_ALLGATHERV: // rank allgatherv send_size [recvcounts] (sendtype) (recvtype)
182 fprintf(trace_file, "%s allgatherv %d ", process_id, extra->send_size);
183 for (i = 0; i < extra->num_processes; i++)
184 fprintf(trace_file, "%d ", extra->recvcounts[i]);
185 fprintf(trace_file, "%s %s \n", extra->datatype1, extra->datatype2);
187 case TRACING_REDUCE_SCATTER: // rank reducescatter [recvcounts] comp_size (sendtype)
188 fprintf(trace_file, "%s reducescatter ", process_id);
189 for (i = 0; i < extra->num_processes; i++)
190 fprintf(trace_file, "%d ", extra->recvcounts[i]);
191 fprintf(trace_file, "%f %s\n", extra->comp_size, extra->datatype1);
193 case TRACING_COMPUTING:
194 fprintf(trace_file, "%s compute %f\n", process_id, extra->comp_size);
196 case TRACING_SLEEPING:
197 fprintf(trace_file, "%s sleep %f\n", process_id, extra->sleep_duration);
199 case TRACING_GATHERV: // rank gatherv send_size [recvcounts] root (sendtype) (recvtype)
200 fprintf(trace_file, "%s gatherv %d ", process_id, extra->send_size);
201 for (i = 0; i < extra->num_processes; i++)
202 fprintf(trace_file, "%d ", extra->recvcounts[i]);
203 fprintf(trace_file, "%d %s %s\n", extra->root, extra->datatype1, extra->datatype2);
205 case TRACING_WAITANY:
206 case TRACING_SENDRECV:
207 case TRACING_SCATTER:
208 case TRACING_SCATTERV:
209 case TRACING_ALLGATHER:
212 case TRACING_COMM_SIZE:
213 case TRACING_COMM_SPLIT:
214 case TRACING_COMM_DUP:
218 XBT_WARN ("Call from %s impossible to translate into replay command : Not implemented (yet)",
219 ((pushState_t) event->data)->value->name);
223 if (extra->recvcounts != nullptr)
224 xbt_free(extra->recvcounts);
225 if (extra->sendcounts != nullptr)
226 xbt_free(extra->sendcounts);
227 xbt_free(process_id);