Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9ac3ecb5367264f2e8a83e7da3ead765028bedd7
[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/log.h>
12 #include <xbt/sysdep.h>
13
14 #include "simgrid/simix.h"
15
16 #include "src/simix/smx_private.h"
17 #include "src/simix/smx_process_private.h"
18
19 #include "src/mc/mc_replay.h"
20 #include "src/mc/mc_record.h"
21 #include "src/mc/mc_base.h"
22
23 #ifdef HAVE_MC
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"
28 #endif
29
30 extern "C" {
31
32 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_record, mc,
33   " Logging specific to MC record/replay facility");
34
35 char* MC_record_path = nullptr;
36
37 void MC_record_replay(mc_record_item_t start, std::size_t len)
38 {
39   MC_wait_for_requests();
40   mc_record_item_t end = start + len;
41
42   // Choose the recorded simcall and execute it:
43   for (mc_record_item_t item=start;item!=end; ++item) {
44
45     XBT_DEBUG("Executing %i$%i", item->pid, item->value);
46 /*
47     if (xbt_dynar_is_empty(simix_global->process_to_run))
48       xbt_die("Unexpected end of application.");
49 */
50
51     // Choose a request:
52     smx_process_t process = SIMIX_process_from_PID(item->pid);
53     if (!process)
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.");
60
61     // Execute the request:
62     SIMIX_simcall_handle(simcall, item->value);
63     MC_wait_for_requests();
64   }
65 }
66
67 xbt_dynar_t MC_record_from_string(const char* data)
68 {
69   XBT_INFO("path=%s", data);
70   if (!data || !data[0])
71     return nullptr;
72
73   xbt_dynar_t dynar = xbt_dynar_new(sizeof(s_mc_record_item_t), nullptr);
74
75   const char* current = data;
76   while (*current) {
77
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)
81       goto fail;
82     xbt_dynar_push(dynar, &item);
83
84     // Find next chunk:
85     const char* end = std::strchr(current, ';');
86     if(end == nullptr)
87       break;
88     else
89       current = end + 1;
90   }
91
92   return dynar;
93
94 fail:
95   xbt_dynar_free(&dynar);
96   return nullptr;
97 }
98
99 #ifdef HAVE_MC
100 static char* MC_record_stack_to_string_liveness(xbt_fifo_t stack)
101 {
102   char* buffer;
103   std::size_t size;
104   std::FILE* file = open_memstream(&buffer, &size);
105
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);
110     int value;
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;
115
116       // Serialization the (pid, value) pair:
117       const char* sep = (item!=start) ? ";" : "";
118       if (value)
119         std::fprintf(file, "%s%u/%u", sep, pid, value);
120       else
121         std::fprintf(file, "%s%u", sep, pid);
122     }
123   }
124
125   std::fclose(file);
126   return buffer;
127 }
128
129 char* MC_record_stack_to_string(xbt_fifo_t stack)
130 {
131   if (_sg_mc_liveness)
132     return MC_record_stack_to_string_liveness(stack);
133
134   xbt_fifo_item_t start = xbt_fifo_get_last_item(stack);
135
136   if (!start) {
137     char* res = (char*) malloc(1 * sizeof(char));
138     res[0] = '\0';
139     return res;
140   }
141
142   char* buffer;
143   std::size_t size;
144   std::FILE* file = open_memstream(&buffer, &size);
145
146   xbt_fifo_item_t item;
147   for (item = start; item; item = xbt_fifo_get_prev_item(item)) {
148
149     // Find (pid, value):
150     mc_state_t state = (mc_state_t) xbt_fifo_get_item_content(item);
151     int value = 0;
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;
155
156     // Serialization the (pid, value) pair:
157     const char* sep = (item!=start) ? ";" : "";
158     if (value)
159       std::fprintf(file, "%s%u/%u", sep, pid, value);
160     else
161       std::fprintf(file, "%s%u", sep, pid);
162   }
163
164   std::fclose(file);
165   return buffer;
166 }
167
168 void MC_record_dump_path(xbt_fifo_t stack)
169 {
170   if (MC_record_is_active()) {
171     char* path = MC_record_stack_to_string(stack);
172     XBT_INFO("Path = %s", path);
173     std::free(path);
174   }
175 }
176 #endif
177
178 void MC_record_replay_from_string(const char* path_string)
179 {
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);
184 }
185
186 void MC_record_replay_init()
187 {
188   mc_time = xbt_new0(double, simix_process_maxpid);
189 }
190
191 }