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 <simgrid/simix.h>
14 #include "src/mc/mc_replay.h"
15 #include "src/mc/mc_record.h"
16 #include "src/mc/mc_base.h"
19 #include "src/mc/mc_private.h"
20 #include "src/mc/mc_state.h"
21 #include "src/mc/mc_smx.h"
22 #include "src/mc/mc_liveness.h"
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_record, mc,
28 " Logging specific to MC record/replay facility");
30 char* MC_record_path = nullptr;
32 void MC_record_replay(mc_record_item_t start, std::size_t len)
34 MC_wait_for_requests();
35 mc_record_item_t end = start + len;
37 // Choose the recorded simcall and execute it:
38 for (mc_record_item_t item=start;item!=end; ++item) {
40 XBT_DEBUG("Executing %i$%i", item->pid, item->value);
42 if (xbt_dynar_is_empty(simix_global->process_to_run))
43 xbt_die("Unexpected end of application.");
47 smx_process_t process = SIMIX_process_from_PID(item->pid);
49 xbt_die("Unexpected process.");
50 smx_simcall_t simcall = &(process->simcall);
51 if(!simcall || simcall->call == SIMCALL_NONE)
52 xbt_die("No simcall for this process.");
53 if (!MC_request_is_visible(simcall) || !MC_request_is_enabled(simcall))
54 xbt_die("Unexpected simcall.");
56 // Execute the request:
57 SIMIX_simcall_handle(simcall, item->value);
58 MC_wait_for_requests();
62 xbt_dynar_t MC_record_from_string(const char* data)
64 XBT_INFO("path=%s", data);
65 if (!data || !data[0])
68 xbt_dynar_t dynar = xbt_dynar_new(sizeof(s_mc_record_item_t), nullptr);
70 const char* current = data;
73 s_mc_record_item_t item = { 0, 0 };
74 int count = sscanf(current, "%u/%u", &item.pid, &item.value);
75 if(count != 2 && count != 1)
77 xbt_dynar_push(dynar, &item);
80 const char* end = std::strchr(current, ';');
90 xbt_dynar_free(&dynar);
95 static char* MC_record_stack_to_string_liveness(xbt_fifo_t stack)
99 std::FILE* file = open_memstream(&buffer, &size);
101 xbt_fifo_item_t item;
102 xbt_fifo_item_t start = xbt_fifo_get_last_item(stack);
103 for (item = start; item; item = xbt_fifo_get_prev_item(item)) {
104 mc_pair_t pair = (mc_pair_t) xbt_fifo_get_item_content(item);
106 smx_simcall_t req = MC_state_get_executed_request(pair->graph_state, &value);
107 if (req && req->call != SIMCALL_NONE) {
108 smx_process_t issuer = MC_smx_simcall_get_issuer(req);
109 const int pid = issuer->pid;
111 // Serialization the (pid, value) pair:
112 const char* sep = (item!=start) ? ";" : "";
114 std::fprintf(file, "%s%u/%u", sep, pid, value);
116 std::fprintf(file, "%s%u", sep, pid);
124 char* MC_record_stack_to_string(xbt_fifo_t stack)
127 return MC_record_stack_to_string_liveness(stack);
129 xbt_fifo_item_t start = xbt_fifo_get_last_item(stack);
132 char* res = (char*) malloc(1 * sizeof(char));
139 std::FILE* file = open_memstream(&buffer, &size);
141 xbt_fifo_item_t item;
142 for (item = start; item; item = xbt_fifo_get_prev_item(item)) {
144 // Find (pid, value):
145 mc_state_t state = (mc_state_t) xbt_fifo_get_item_content(item);
147 smx_simcall_t saved_req = MC_state_get_executed_request(state, &value);
148 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
149 const int pid = issuer->pid;
151 // Serialization the (pid, value) pair:
152 const char* sep = (item!=start) ? ";" : "";
154 std::fprintf(file, "%s%u/%u", sep, pid, value);
156 std::fprintf(file, "%s%u", sep, pid);
163 void MC_record_dump_path(xbt_fifo_t stack)
165 if (MC_record_is_active()) {
166 char* path = MC_record_stack_to_string(stack);
167 XBT_INFO("Path = %s", path);
173 void MC_record_replay_from_string(const char* path_string)
175 xbt_dynar_t path = MC_record_from_string(path_string);
176 mc_record_item_t start = &xbt_dynar_get_as(path, 0, s_mc_record_item_t);
177 MC_record_replay(start, xbt_dynar_length(path));
178 xbt_dynar_free(&path);
181 void MC_record_replay_init()
183 mc_time = xbt_new0(double, simix_process_maxpid);