Logo AND Algorithmique Numérique Distribuée

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