Logo AND Algorithmique Numérique Distribuée

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