Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Do not execute MCer code in MCed mode in MC_remove_ignore_heap()
[simgrid.git] / src / mc / mc_comm_pattern.cpp
1 /* Copyright (c) 2007-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 <string.h>
8
9 #include <xbt/sysdep.h>
10 #include <xbt/dynar.h>
11
12 #include "mc_comm_pattern.h"
13 #include "mc_smx.h"
14
15 extern "C" {
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_comm_pattern, mc,
18                                 "Logging specific to MC communication patterns");
19
20 mc_comm_pattern_t MC_comm_pattern_dup(mc_comm_pattern_t comm)
21 {
22   mc_comm_pattern_t res = xbt_new0(s_mc_comm_pattern_t, 1);
23   res->index = comm->index;
24   res->type = comm->type;
25   res->comm_addr = comm->comm_addr;
26   res->rdv = strdup(comm->rdv);
27   res->data_size = -1;
28   res->data = NULL;
29   if (comm->type == SIMIX_COMM_SEND) {
30     res->src_proc = comm->src_proc;
31     res->src_host = comm->src_host;
32     if (comm->data != NULL) {
33       res->data_size = comm->data_size;
34       res->data = xbt_malloc0(comm->data_size);
35       memcpy(res->data, comm->data, comm->data_size);
36     }
37   } else {
38     res->dst_proc = comm->dst_proc;
39     res->dst_host = comm->dst_host;
40   }
41   return res;
42 }
43
44 xbt_dynar_t MC_comm_patterns_dup(xbt_dynar_t patterns)
45 {
46   xbt_dynar_t res = xbt_dynar_new(sizeof(mc_comm_pattern_t), MC_comm_pattern_free_voidp);
47
48   mc_comm_pattern_t comm;
49   unsigned int cursor;
50   xbt_dynar_foreach(patterns, cursor, comm) {
51     mc_comm_pattern_t copy_comm = MC_comm_pattern_dup(comm);
52     xbt_dynar_push(res, &copy_comm);
53   }
54
55   return res;
56 }
57
58 static void MC_patterns_copy(xbt_dynar_t dest, xbt_dynar_t source)
59 {
60   xbt_dynar_reset(dest);
61
62   unsigned int cursor;
63   mc_comm_pattern_t comm;
64   xbt_dynar_foreach(source, cursor, comm) {
65     mc_comm_pattern_t copy_comm = MC_comm_pattern_dup(comm);
66     xbt_dynar_push(dest, &copy_comm);
67   }
68 }
69
70 void MC_restore_communications_pattern(mc_state_t state)
71 {
72   mc_list_comm_pattern_t list_process_comm;
73   unsigned int cursor;
74
75   xbt_dynar_foreach(initial_communications_pattern, cursor, list_process_comm){
76     list_process_comm->index_comm = (int)xbt_dynar_get_as(state->index_comm, cursor, int);
77   }
78
79   for (unsigned i = 0; i < MC_smx_get_maxpid(); i++) {
80     MC_patterns_copy(
81       xbt_dynar_get_as(incomplete_communications_pattern, i, xbt_dynar_t),
82       xbt_dynar_get_as(state->incomplete_comm_pattern, i, xbt_dynar_t)
83     );
84   }
85 }
86
87 void MC_state_copy_incomplete_communications_pattern(mc_state_t state)
88 {
89   state->incomplete_comm_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
90
91   for (unsigned i=0; i < MC_smx_get_maxpid(); i++) {
92     xbt_dynar_t comms = xbt_dynar_get_as(incomplete_communications_pattern, i, xbt_dynar_t);
93     xbt_dynar_t copy = MC_comm_patterns_dup(comms);
94     xbt_dynar_insert_at(state->incomplete_comm_pattern, i, &copy);
95   }
96 }
97
98 void MC_state_copy_index_communications_pattern(mc_state_t state)
99 {
100   state->index_comm = xbt_dynar_new(sizeof(unsigned int), NULL);
101   mc_list_comm_pattern_t list_process_comm;
102   unsigned int cursor;
103   xbt_dynar_foreach(initial_communications_pattern, cursor, list_process_comm){
104     xbt_dynar_push_as(state->index_comm, unsigned int, list_process_comm->index_comm);
105   }
106 }
107
108 void MC_handle_comm_pattern(
109   e_mc_call_type_t call_type, smx_simcall_t req,
110   int value, xbt_dynar_t pattern, int backtracking)
111 {
112
113   switch(call_type) {
114   case MC_CALL_TYPE_NONE:
115     break;
116   case MC_CALL_TYPE_SEND:
117   case MC_CALL_TYPE_RECV:
118     MC_get_comm_pattern(pattern, req, call_type, backtracking);
119     break;
120   case MC_CALL_TYPE_WAIT:
121   case MC_CALL_TYPE_WAITANY:
122     {
123       smx_synchro_t comm_addr = NULL;
124       if (call_type == MC_CALL_TYPE_WAIT)
125         comm_addr = simcall_comm_wait__get__comm(req);
126       else
127         // comm_addr = REMOTE(xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_synchro_t)):
128         MC_process_read_dynar_element(&mc_model_checker->process(), &comm_addr,
129           simcall_comm_waitany__get__comms(req), value, sizeof(comm_addr));
130       MC_complete_comm_pattern(pattern, comm_addr,
131         MC_smx_simcall_get_issuer(req)->pid, backtracking);
132     }
133     break;
134   default:
135     xbt_die("Unexpected call type %i", (int)call_type);
136   }
137
138 }
139
140 void MC_comm_pattern_free(mc_comm_pattern_t p)
141 {
142   xbt_free(p->rdv);
143   xbt_free(p->data);
144   xbt_free(p);
145   p = NULL;
146 }
147
148 static void MC_list_comm_pattern_free(mc_list_comm_pattern_t l)
149 {
150   xbt_dynar_free(&(l->list));
151   xbt_free(l);
152   l = NULL;
153 }
154
155 void MC_comm_pattern_free_voidp(void *p)
156 {
157   MC_comm_pattern_free((mc_comm_pattern_t) * (void **) p);
158 }
159
160 void MC_list_comm_pattern_free_voidp(void *p)
161 {
162   MC_list_comm_pattern_free((mc_list_comm_pattern_t) * (void **) p);
163 }
164
165 }