1 /* Copyright (c) 2014. 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_record.h"
17 #include "mc_private.h"
18 #include "mc_model_checker.h"
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_record, mc,
26 " Logging specific to MC record/replay facility");
28 char* MC_record_path = NULL;
30 void MC_record_replay(mc_record_item_t start, size_t len)
32 MC_wait_for_requests();
33 mc_record_item_t end = start + len;
35 // Choose the recorded simcall and execute it:
36 for (mc_record_item_t item=start;item!=end; ++item) {
38 XBT_DEBUG("Executing %i$%i", item->pid, item->value);
40 if (xbt_dynar_is_empty(simix_global->process_to_run))
41 xbt_die("Unexpected end of application.");
45 smx_process_t process = SIMIX_process_from_PID(item->pid);
47 xbt_die("Unexpected process.");
48 smx_simcall_t simcall = &(process->simcall);
49 if(!simcall || simcall->call == SIMCALL_NONE)
50 xbt_die("No simcall for this process.");
51 if (!MC_request_is_visible(simcall) || !MC_request_is_enabled(simcall))
52 xbt_die("Unexpected simcall.");
54 // Execute the request:
55 SIMIX_simcall_handle(simcall, item->value);
56 MC_wait_for_requests();
60 xbt_dynar_t MC_record_from_string(const char* data)
62 XBT_INFO("path=%s", data);
63 if (!data || !data[0])
66 xbt_dynar_t dynar = xbt_dynar_new(sizeof(s_mc_record_item_t), NULL);
68 const char* current = data;
71 s_mc_record_item_t item = { 0, 0 };
72 int count = sscanf(current, "%u/%u", &item.pid, &item.value);
73 if(count != 2 && count != 1)
75 xbt_dynar_push(dynar, &item);
78 const char* end = strchr(current, ';');
88 xbt_dynar_free(&dynar);
93 char* MC_record_stack_to_string(xbt_fifo_t stack)
95 xbt_fifo_item_t start = xbt_fifo_get_last_item(stack);
98 char* res = (char*) malloc(1 * sizeof(char));
105 FILE* file = open_memstream(&buffer, &size);
107 xbt_fifo_item_t item;
108 for (item = start; item; item = xbt_fifo_get_prev_item(item)) {
110 // Find (pid, value):
111 mc_state_t state = (mc_state_t) xbt_fifo_get_item_content(item);
113 smx_simcall_t saved_req = MC_state_get_executed_request(state, &value);
114 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
115 const int pid = issuer->pid;
117 // Serialization the (pid, value) pair:
118 const char* sep = (item!=start) ? ";" : "";
120 fprintf(file, "%s%u/%u", sep, pid, value);
122 fprintf(file, "%s%u", sep, pid);
129 void MC_record_dump_path(xbt_fifo_t stack)
131 if (MC_record_is_active()) {
132 char* path = MC_record_stack_to_string(stack);
133 XBT_INFO("Path = %s", path);
139 void MC_record_replay_from_string(const char* path_string)
141 xbt_dynar_t path = MC_record_from_string(path_string);
142 mc_record_item_t start = &xbt_dynar_get_as(path, 0, s_mc_record_item_t);
143 MC_record_replay(start, xbt_dynar_length(path));
144 xbt_dynar_free(&path);
147 void MC_record_replay_init()
149 mc_time = xbt_new0(double, simix_process_maxpid);