Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / mc / mc_record.cpp
1 /* Copyright (c) 2014-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <cstring>
8 #include <cstdio>
9 #include <cstdlib>
10
11 #include <xbt/fifo.h>
12 #include <xbt/log.h>
13 #include <xbt/sysdep.h>
14
15 #include "simgrid/simix.h"
16
17 #include "src/simix/smx_private.h"
18 #include "src/simix/smx_process_private.h"
19
20 #include "src/mc/mc_replay.h"
21 #include "src/mc/mc_record.h"
22 #include "src/mc/mc_base.h"
23
24 #if HAVE_MC
25 #include "src/mc/mc_request.h"
26 #include "src/mc/mc_private.h"
27 #include "src/mc/mc_state.h"
28 #include "src/mc/mc_smx.h"
29 #include "src/mc/mc_liveness.h"
30 #endif
31
32 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_record, mc,
33   " Logging specific to MC record/replay facility");
34
35 extern "C" {
36
37 char* MC_record_path = nullptr;
38
39 void MC_record_replay(mc_record_item_t start, std::size_t len)
40 {
41   simgrid::mc::wait_for_requests();
42   mc_record_item_t end = start + len;
43
44   // Choose the recorded simcall and execute it:
45   for (mc_record_item_t item=start;item!=end; ++item) {
46
47     XBT_DEBUG("Executing %i$%i", item->pid, item->value);
48 /*
49     if (xbt_dynar_is_empty(simix_global->process_to_run))
50       xbt_die("Unexpected end of application.");
51 */
52
53     // Choose a request:
54     smx_process_t process = SIMIX_process_from_PID(item->pid);
55     if (!process)
56       xbt_die("Unexpected process.");
57     smx_simcall_t simcall = &(process->simcall);
58     if(!simcall || simcall->call == SIMCALL_NONE)
59       xbt_die("No simcall for this process.");
60     if (!simgrid::mc::request_is_visible(simcall)
61         || !simgrid::mc::request_is_enabled(simcall))
62       xbt_die("Unexpected simcall.");
63
64     // Execute the request:
65     SIMIX_simcall_handle(simcall, item->value);
66     simgrid::mc::wait_for_requests();
67   }
68 }
69
70 xbt_dynar_t MC_record_from_string(const char* data)
71 {
72   XBT_INFO("path=%s", data);
73   if (!data || !data[0])
74     return nullptr;
75
76   xbt_dynar_t dynar = xbt_dynar_new(sizeof(s_mc_record_item_t), nullptr);
77
78   const char* current = data;
79   while (*current) {
80
81     s_mc_record_item_t item = { 0, 0 };
82     int count = sscanf(current, "%u/%u", &item.pid, &item.value);
83     if(count != 2 && count != 1)
84       goto fail;
85     xbt_dynar_push(dynar, &item);
86
87     // Find next chunk:
88     const char* end = std::strchr(current, ';');
89     if(end == nullptr)
90       break;
91     else
92       current = end + 1;
93   }
94
95   return dynar;
96
97 fail:
98   xbt_dynar_free(&dynar);
99   return nullptr;
100 }
101
102 #if HAVE_MC
103 static char* MC_record_stack_to_string_liveness(xbt_fifo_t stack)
104 {
105   char* buffer;
106   std::size_t size;
107   std::FILE* file = open_memstream(&buffer, &size);
108
109   xbt_fifo_item_t item;
110   xbt_fifo_item_t start = xbt_fifo_get_last_item(stack);
111   for (item = start; item; item = xbt_fifo_get_prev_item(item)) {
112     simgrid::mc::Pair* pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
113     int value;
114     smx_simcall_t req = MC_state_get_executed_request(pair->graph_state, &value);
115     if (req && req->call != SIMCALL_NONE) {
116       smx_process_t issuer = MC_smx_simcall_get_issuer(req);
117       const int pid = issuer->pid;
118
119       // Serialization the (pid, value) pair:
120       const char* sep = (item!=start) ? ";" : "";
121       if (value)
122         std::fprintf(file, "%s%u/%u", sep, pid, value);
123       else
124         std::fprintf(file, "%s%u", sep, pid);
125     }
126   }
127
128   std::fclose(file);
129   return buffer;
130 }
131
132 char* MC_record_stack_to_string(xbt_fifo_t stack)
133 {
134   if (_sg_mc_liveness)
135     return MC_record_stack_to_string_liveness(stack);
136
137   xbt_fifo_item_t start = xbt_fifo_get_last_item(stack);
138
139   if (!start) {
140     char* res = (char*) malloc(1 * sizeof(char));
141     res[0] = '\0';
142     return res;
143   }
144
145   char* buffer;
146   std::size_t size;
147   std::FILE* file = open_memstream(&buffer, &size);
148
149   xbt_fifo_item_t item;
150   for (item = start; item; item = xbt_fifo_get_prev_item(item)) {
151
152     // Find (pid, value):
153     mc_state_t state = (mc_state_t) xbt_fifo_get_item_content(item);
154     int value = 0;
155     smx_simcall_t saved_req = MC_state_get_executed_request(state, &value);
156     const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
157     const int pid = issuer->pid;
158
159     // Serialization the (pid, value) pair:
160     const char* sep = (item!=start) ? ";" : "";
161     if (value)
162       std::fprintf(file, "%s%u/%u", sep, pid, value);
163     else
164       std::fprintf(file, "%s%u", sep, pid);
165   }
166
167   std::fclose(file);
168   return buffer;
169 }
170
171 void MC_record_dump_path(xbt_fifo_t stack)
172 {
173   if (MC_record_is_active()) {
174     char* path = MC_record_stack_to_string(stack);
175     XBT_INFO("Path = %s", path);
176     std::free(path);
177   }
178 }
179 #endif
180
181 void MC_record_replay_from_string(const char* path_string)
182 {
183   xbt_dynar_t path = MC_record_from_string(path_string);
184   mc_record_item_t start = &xbt_dynar_get_as(path, 0, s_mc_record_item_t);
185   MC_record_replay(start, xbt_dynar_length(path));
186   xbt_dynar_free(&path);
187 }
188
189 void MC_record_replay_init()
190 {
191   simgrid::mc::processes_time.resize(simix_process_maxpid);
192 }
193
194 }