Logo AND Algorithmique Numérique Distribuée

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