Logo AND Algorithmique Numérique Distribuée

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