1 /* Copyright (c) 2014-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. */
13 #include <xbt/sysdep.h>
15 #include "simgrid/simix.h"
17 #include "src/simix/smx_private.h"
18 #include "src/simix/smx_process_private.h"
20 #include "src/mc/mc_replay.h"
21 #include "src/mc/mc_record.h"
22 #include "src/mc/mc_base.h"
25 #include "src/mc/mc_request.h"
26 #include "src/mc/mc_private.h"
27 #include "src/mc/mc_state.h"
28 #include "src/mc/mc_smx.h"
29 #include "src/mc/mc_liveness.h"
32 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_record, mc,
33 " Logging specific to MC record/replay facility");
37 char* MC_record_path = nullptr;
39 void MC_record_replay(mc_record_item_t start, std::size_t len)
41 simgrid::mc::wait_for_requests();
42 mc_record_item_t end = start + len;
44 // Choose the recorded simcall and execute it:
45 for (mc_record_item_t item=start;item!=end; ++item) {
47 XBT_DEBUG("Executing %i$%i", item->pid, item->value);
49 if (xbt_dynar_is_empty(simix_global->process_to_run))
50 xbt_die("Unexpected end of application.");
54 smx_process_t process = SIMIX_process_from_PID(item->pid);
56 xbt_die("Unexpected process.");
57 smx_simcall_t simcall = &(process->simcall);
58 if(!simcall || simcall->call == SIMCALL_NONE)
59 xbt_die("No simcall for this process.");
60 if (!simgrid::mc::request_is_visible(simcall)
61 || !simgrid::mc::request_is_enabled(simcall))
62 xbt_die("Unexpected simcall.");
64 // Execute the request:
65 SIMIX_simcall_handle(simcall, item->value);
66 simgrid::mc::wait_for_requests();
70 xbt_dynar_t MC_record_from_string(const char* data)
72 XBT_INFO("path=%s", data);
73 if (!data || !data[0])
76 xbt_dynar_t dynar = xbt_dynar_new(sizeof(s_mc_record_item_t), nullptr);
78 const char* current = data;
81 s_mc_record_item_t item = { 0, 0 };
82 int count = sscanf(current, "%u/%u", &item.pid, &item.value);
83 if(count != 2 && count != 1)
85 xbt_dynar_push(dynar, &item);
88 const char* end = std::strchr(current, ';');
98 xbt_dynar_free(&dynar);
103 static char* MC_record_stack_to_string_liveness(xbt_fifo_t stack)
107 std::FILE* file = open_memstream(&buffer, &size);
109 xbt_fifo_item_t item;
110 xbt_fifo_item_t start = xbt_fifo_get_last_item(stack);
111 for (item = start; item; item = xbt_fifo_get_prev_item(item)) {
112 simgrid::mc::Pair* pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
114 smx_simcall_t req = MC_state_get_executed_request(pair->graph_state, &value);
115 if (req && req->call != SIMCALL_NONE) {
116 smx_process_t issuer = MC_smx_simcall_get_issuer(req);
117 const int pid = issuer->pid;
119 // Serialization the (pid, value) pair:
120 const char* sep = (item!=start) ? ";" : "";
122 std::fprintf(file, "%s%u/%u", sep, pid, value);
124 std::fprintf(file, "%s%u", sep, pid);
132 char* MC_record_stack_to_string(xbt_fifo_t stack)
135 return MC_record_stack_to_string_liveness(stack);
137 xbt_fifo_item_t start = xbt_fifo_get_last_item(stack);
140 char* res = (char*) malloc(1 * sizeof(char));
147 std::FILE* file = open_memstream(&buffer, &size);
149 xbt_fifo_item_t item;
150 for (item = start; item; item = xbt_fifo_get_prev_item(item)) {
152 // Find (pid, value):
153 mc_state_t state = (mc_state_t) xbt_fifo_get_item_content(item);
155 smx_simcall_t saved_req = MC_state_get_executed_request(state, &value);
156 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
157 const int pid = issuer->pid;
159 // Serialization the (pid, value) pair:
160 const char* sep = (item!=start) ? ";" : "";
162 std::fprintf(file, "%s%u/%u", sep, pid, value);
164 std::fprintf(file, "%s%u", sep, pid);
171 void MC_record_dump_path(xbt_fifo_t stack)
173 if (MC_record_is_active()) {
174 char* path = MC_record_stack_to_string(stack);
175 XBT_INFO("Path = %s", path);
181 void MC_record_replay_from_string(const char* path_string)
183 simgrid::mc::processes_time.resize(simix_process_maxpid);
184 xbt_dynar_t path = MC_record_from_string(path_string);
185 mc_record_item_t start = &xbt_dynar_get_as(path, 0, s_mc_record_item_t);
186 MC_record_replay(start, xbt_dynar_length(path));
187 xbt_dynar_free(&path);
188 simgrid::mc::processes_time.clear();