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. */
11 #include <simgrid/simix.h>
13 #include "mc_replay.h"
14 #include "mc_record.h"
18 #include "mc_private.h"
21 #include "mc_liveness.h"
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_record, mc,
27 " Logging specific to MC record/replay facility");
29 char* MC_record_path = NULL;
31 void MC_record_replay(mc_record_item_t start, size_t len)
33 MC_wait_for_requests();
34 mc_record_item_t end = start + len;
36 // Choose the recorded simcall and execute it:
37 for (mc_record_item_t item=start;item!=end; ++item) {
39 XBT_DEBUG("Executing %i$%i", item->pid, item->value);
41 if (xbt_dynar_is_empty(simix_global->process_to_run))
42 xbt_die("Unexpected end of application.");
46 smx_process_t process = SIMIX_process_from_PID(item->pid);
48 xbt_die("Unexpected process.");
49 smx_simcall_t simcall = &(process->simcall);
50 if(!simcall || simcall->call == SIMCALL_NONE)
51 xbt_die("No simcall for this process.");
52 if (!MC_request_is_visible(simcall) || !MC_request_is_enabled(simcall))
53 xbt_die("Unexpected simcall.");
55 // Execute the request:
56 SIMIX_simcall_handle(simcall, item->value);
57 MC_wait_for_requests();
61 xbt_dynar_t MC_record_from_string(const char* data)
63 XBT_INFO("path=%s", data);
64 if (!data || !data[0])
67 xbt_dynar_t dynar = xbt_dynar_new(sizeof(s_mc_record_item_t), NULL);
69 const char* current = data;
72 s_mc_record_item_t item = { 0, 0 };
73 int count = sscanf(current, "%u/%u", &item.pid, &item.value);
74 if(count != 2 && count != 1)
76 xbt_dynar_push(dynar, &item);
79 const char* end = strchr(current, ';');
89 xbt_dynar_free(&dynar);
94 static char* MC_record_stack_to_string_liveness(xbt_fifo_t stack)
98 FILE* file = open_memstream(&buffer, &size);
100 xbt_fifo_item_t item;
101 xbt_fifo_item_t start = xbt_fifo_get_last_item(stack);
102 for (item = start; item; item = xbt_fifo_get_prev_item(item)) {
103 mc_pair_t pair = (mc_pair_t) xbt_fifo_get_item_content(item);
105 smx_simcall_t req = MC_state_get_executed_request(pair->graph_state, &value);
106 if (req && req->call != SIMCALL_NONE) {
107 smx_process_t issuer = MC_smx_simcall_get_issuer(req);
108 const int pid = issuer->pid;
110 // Serialization the (pid, value) pair:
111 const char* sep = (item!=start) ? ";" : "";
113 fprintf(file, "%s%u/%u", sep, pid, value);
115 fprintf(file, "%s%u", sep, pid);
123 char* MC_record_stack_to_string(xbt_fifo_t stack)
126 return MC_record_stack_to_string_liveness(stack);
128 xbt_fifo_item_t start = xbt_fifo_get_last_item(stack);
131 char* res = (char*) malloc(1 * sizeof(char));
138 FILE* file = open_memstream(&buffer, &size);
140 xbt_fifo_item_t item;
141 for (item = start; item; item = xbt_fifo_get_prev_item(item)) {
143 // Find (pid, value):
144 mc_state_t state = (mc_state_t) xbt_fifo_get_item_content(item);
146 smx_simcall_t saved_req = MC_state_get_executed_request(state, &value);
147 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
148 const int pid = issuer->pid;
150 // Serialization the (pid, value) pair:
151 const char* sep = (item!=start) ? ";" : "";
153 fprintf(file, "%s%u/%u", sep, pid, value);
155 fprintf(file, "%s%u", sep, pid);
162 void MC_record_dump_path(xbt_fifo_t stack)
164 if (MC_record_is_active()) {
165 char* path = MC_record_stack_to_string(stack);
166 XBT_INFO("Path = %s", path);
172 void MC_record_replay_from_string(const char* path_string)
174 xbt_dynar_t path = MC_record_from_string(path_string);
175 mc_record_item_t start = &xbt_dynar_get_as(path, 0, s_mc_record_item_t);
176 MC_record_replay(start, xbt_dynar_length(path));
177 xbt_dynar_free(&path);
180 void MC_record_replay_init()
182 mc_time = xbt_new0(double, simix_process_maxpid);