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. */
12 #include <xbt/sysdep.h>
14 #include "simgrid/simix.h"
16 #include "src/simix/smx_private.h"
17 #include "src/simix/smx_process_private.h"
19 #include "src/mc/mc_replay.h"
20 #include "src/mc/mc_record.h"
21 #include "src/mc/mc_base.h"
24 #include "src/mc/mc_private.h"
25 #include "src/mc/mc_state.h"
26 #include "src/mc/mc_smx.h"
27 #include "src/mc/mc_liveness.h"
32 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_record, mc,
33 " Logging specific to MC record/replay facility");
35 char* MC_record_path = nullptr;
37 void MC_record_replay(mc_record_item_t start, std::size_t len)
39 MC_wait_for_requests();
40 mc_record_item_t end = start + len;
42 // Choose the recorded simcall and execute it:
43 for (mc_record_item_t item=start;item!=end; ++item) {
45 XBT_DEBUG("Executing %i$%i", item->pid, item->value);
47 if (xbt_dynar_is_empty(simix_global->process_to_run))
48 xbt_die("Unexpected end of application.");
52 smx_process_t process = SIMIX_process_from_PID(item->pid);
54 xbt_die("Unexpected process.");
55 smx_simcall_t simcall = &(process->simcall);
56 if(!simcall || simcall->call == SIMCALL_NONE)
57 xbt_die("No simcall for this process.");
58 if (!MC_request_is_visible(simcall) || !MC_request_is_enabled(simcall))
59 xbt_die("Unexpected simcall.");
61 // Execute the request:
62 SIMIX_simcall_handle(simcall, item->value);
63 MC_wait_for_requests();
67 xbt_dynar_t MC_record_from_string(const char* data)
69 XBT_INFO("path=%s", data);
70 if (!data || !data[0])
73 xbt_dynar_t dynar = xbt_dynar_new(sizeof(s_mc_record_item_t), nullptr);
75 const char* current = data;
78 s_mc_record_item_t item = { 0, 0 };
79 int count = sscanf(current, "%u/%u", &item.pid, &item.value);
80 if(count != 2 && count != 1)
82 xbt_dynar_push(dynar, &item);
85 const char* end = std::strchr(current, ';');
95 xbt_dynar_free(&dynar);
100 static char* MC_record_stack_to_string_liveness(xbt_fifo_t stack)
104 std::FILE* file = open_memstream(&buffer, &size);
106 xbt_fifo_item_t item;
107 xbt_fifo_item_t start = xbt_fifo_get_last_item(stack);
108 for (item = start; item; item = xbt_fifo_get_prev_item(item)) {
109 mc_pair_t pair = (mc_pair_t) xbt_fifo_get_item_content(item);
111 smx_simcall_t req = MC_state_get_executed_request(pair->graph_state, &value);
112 if (req && req->call != SIMCALL_NONE) {
113 smx_process_t issuer = MC_smx_simcall_get_issuer(req);
114 const int pid = issuer->pid;
116 // Serialization the (pid, value) pair:
117 const char* sep = (item!=start) ? ";" : "";
119 std::fprintf(file, "%s%u/%u", sep, pid, value);
121 std::fprintf(file, "%s%u", sep, pid);
129 char* MC_record_stack_to_string(xbt_fifo_t stack)
132 return MC_record_stack_to_string_liveness(stack);
134 xbt_fifo_item_t start = xbt_fifo_get_last_item(stack);
137 char* res = (char*) malloc(1 * sizeof(char));
144 std::FILE* file = open_memstream(&buffer, &size);
146 xbt_fifo_item_t item;
147 for (item = start; item; item = xbt_fifo_get_prev_item(item)) {
149 // Find (pid, value):
150 mc_state_t state = (mc_state_t) xbt_fifo_get_item_content(item);
152 smx_simcall_t saved_req = MC_state_get_executed_request(state, &value);
153 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
154 const int pid = issuer->pid;
156 // Serialization the (pid, value) pair:
157 const char* sep = (item!=start) ? ";" : "";
159 std::fprintf(file, "%s%u/%u", sep, pid, value);
161 std::fprintf(file, "%s%u", sep, pid);
168 void MC_record_dump_path(xbt_fifo_t stack)
170 if (MC_record_is_active()) {
171 char* path = MC_record_stack_to_string(stack);
172 XBT_INFO("Path = %s", path);
178 void MC_record_replay_from_string(const char* path_string)
180 xbt_dynar_t path = MC_record_from_string(path_string);
181 mc_record_item_t start = &xbt_dynar_get_as(path, 0, s_mc_record_item_t);
182 MC_record_replay(start, xbt_dynar_length(path));
183 xbt_dynar_free(&path);
186 void MC_record_replay_init()
188 mc_time = xbt_new0(double, simix_process_maxpid);