Logo AND Algorithmique Numérique Distribuée

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