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_private.h"
26 #include "src/mc/mc_state.h"
27 #include "src/mc/mc_smx.h"
28 #include "src/mc/mc_liveness.h"
33 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_record, mc,
34 " Logging specific to MC record/replay facility");
36 char* MC_record_path = nullptr;
38 void MC_record_replay(mc_record_item_t start, std::size_t len)
40 MC_wait_for_requests();
41 mc_record_item_t end = start + len;
43 // Choose the recorded simcall and execute it:
44 for (mc_record_item_t item=start;item!=end; ++item) {
46 XBT_DEBUG("Executing %i$%i", item->pid, item->value);
48 if (xbt_dynar_is_empty(simix_global->process_to_run))
49 xbt_die("Unexpected end of application.");
53 smx_process_t process = SIMIX_process_from_PID(item->pid);
55 xbt_die("Unexpected process.");
56 smx_simcall_t simcall = &(process->simcall);
57 if(!simcall || simcall->call == SIMCALL_NONE)
58 xbt_die("No simcall for this process.");
59 if (!MC_request_is_visible(simcall) || !MC_request_is_enabled(simcall))
60 xbt_die("Unexpected simcall.");
62 // Execute the request:
63 SIMIX_simcall_handle(simcall, item->value);
64 MC_wait_for_requests();
68 xbt_dynar_t MC_record_from_string(const char* data)
70 XBT_INFO("path=%s", data);
71 if (!data || !data[0])
74 xbt_dynar_t dynar = xbt_dynar_new(sizeof(s_mc_record_item_t), nullptr);
76 const char* current = data;
79 s_mc_record_item_t item = { 0, 0 };
80 int count = sscanf(current, "%u/%u", &item.pid, &item.value);
81 if(count != 2 && count != 1)
83 xbt_dynar_push(dynar, &item);
86 const char* end = std::strchr(current, ';');
96 xbt_dynar_free(&dynar);
101 static char* MC_record_stack_to_string_liveness(xbt_fifo_t stack)
105 std::FILE* file = open_memstream(&buffer, &size);
107 xbt_fifo_item_t item;
108 xbt_fifo_item_t start = xbt_fifo_get_last_item(stack);
109 for (item = start; item; item = xbt_fifo_get_prev_item(item)) {
110 mc_pair_t pair = (mc_pair_t) xbt_fifo_get_item_content(item);
112 smx_simcall_t req = MC_state_get_executed_request(pair->graph_state, &value);
113 if (req && req->call != SIMCALL_NONE) {
114 smx_process_t issuer = MC_smx_simcall_get_issuer(req);
115 const int pid = issuer->pid;
117 // Serialization the (pid, value) pair:
118 const char* sep = (item!=start) ? ";" : "";
120 std::fprintf(file, "%s%u/%u", sep, pid, value);
122 std::fprintf(file, "%s%u", sep, pid);
130 char* MC_record_stack_to_string(xbt_fifo_t stack)
133 return MC_record_stack_to_string_liveness(stack);
135 xbt_fifo_item_t start = xbt_fifo_get_last_item(stack);
138 char* res = (char*) malloc(1 * sizeof(char));
145 std::FILE* file = open_memstream(&buffer, &size);
147 xbt_fifo_item_t item;
148 for (item = start; item; item = xbt_fifo_get_prev_item(item)) {
150 // Find (pid, value):
151 mc_state_t state = (mc_state_t) xbt_fifo_get_item_content(item);
153 smx_simcall_t saved_req = MC_state_get_executed_request(state, &value);
154 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
155 const int pid = issuer->pid;
157 // Serialization the (pid, value) pair:
158 const char* sep = (item!=start) ? ";" : "";
160 std::fprintf(file, "%s%u/%u", sep, pid, value);
162 std::fprintf(file, "%s%u", sep, pid);
169 void MC_record_dump_path(xbt_fifo_t stack)
171 if (MC_record_is_active()) {
172 char* path = MC_record_stack_to_string(stack);
173 XBT_INFO("Path = %s", path);
179 void MC_record_replay_from_string(const char* path_string)
181 xbt_dynar_t path = MC_record_from_string(path_string);
182 mc_record_item_t start = &xbt_dynar_get_as(path, 0, s_mc_record_item_t);
183 MC_record_replay(start, xbt_dynar_length(path));
184 xbt_dynar_free(&path);
187 void MC_record_replay_init()
189 mc_time = xbt_new0(double, simix_process_maxpid);