Logo AND Algorithmique Numérique Distribuée

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