-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator.hpp
More file actions
328 lines (260 loc) · 11.7 KB
/
simulator.hpp
File metadata and controls
328 lines (260 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright (C) 2013 Paul Letnyanko *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the *
* "Software"), to deal in the Software without restriction, including *
* without limitation the rights to use, copy, modify, merge, publish, *
* distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to *
* the following conditions: *
* *
* The above copyright notice and this permission notice shall be included *
* in all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* CLAIM, OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE *
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef SIMULATOR_HPP
#define SIMULATOR_HPP
#include <string>
namespace rcs
{
/// One of the ways to simulate race condition is by synchronizing suspected piece of code.
/// This way, init routines won't make noise and interfere with the actual suspect.
struct RaceSuspect
{
virtual ~RaceSuspect()
{ }
/// If init returns false operation will be aborted.
virtual bool init() = 0;
/// This piece of code is what will be actually synchronized with
/// the other suspects on the first spawn.
/// This way we can cast away time, needed for init and put critical
/// suspected piece of code here.
virtual bool run() = 0;
/// Aftermath cleaner.
virtual bool shutdown() = 0;
};
typedef bool (*race_suspect_function_t)();
/// Per prosess/process group config.
struct Config
{
Config(const std::string & name = std::string(), size_t concurrent = 1, size_t respawns = 0)
:name(name),
concurrent(concurrent),
respawns(respawns)
{ }
/// Name of the proces or a group of processes.
std::string name;
/// Number of concurrently running processes.
size_t concurrent;
/// Number of process respawns.
size_t respawns;
};
namespace impl
{
struct Simulator;
}
/// A simple framework for Race Conditions simulation.
///
/// Entropy source is used to initalize standard random generator
/// (srand()) for each spawned child using providing seed.
class Simulator
{
public:
Simulator();
~Simulator();
/// ////////////////////////////////////// ///
/// ///
/// Add single process to the simulation ///
/// ///
/// ////////////////////////////////////// ///
///
///
/// Job as a RaceSuspect derived object.
/// Simulator takes ownership of the @param job object, so it must be allocated on heap.
void add_process(RaceSuspect * job, const std::string & name, size_t respawns = 0);
/// Job as a function.
inline void add_process(race_suspect_function_t job, const std::string & name, size_t respawns = 0);
/// Job as a functor.
/// Functor must have overloaded function operator taking no arguments, as well as available copy constructor.
template <typename F>
inline void add_process(F & job, const std::string & name, size_t respawns = 0);
/// ///////////////////////////////////// ///
/// ///
/// Add process group to the simulation ///
/// ///
/// ///////////////////////////////////// ///
///
///
/// Processes of group run simultaneously.
///
/// Job as a RaceSuspect derived object.
/// Simulator takes ownership of the @param job object, so it must be allocated on heap.
void add_process_group(RaceSuspect * job, size_t group_size, const std::string & name, size_t respawns = 0);
/// Job as a function.
inline void add_process_group(race_suspect_function_t job, size_t group_size, const std::string & name, size_t respawns = 0);
/// Job as a functor.
/// Functor must have overloaded function operator taking no arguments, as well as available copy constructor.
template <typename F>
inline void add_process_group(F & job, size_t group_size, const std::string & name, size_t respawns = 0);
/// //////////////////////////////////////////// ///
/// ///
/// Add process to the simulation using config ///
/// ///
/// //////////////////////////////////////////// ///
///
///
/// Job as a RaceSuspect derived object.
/// Simulator takes ownership of the @param job object, so it must be allocated on heap.
void add_process(RaceSuspect * job, const Config & config);
/// Job as a function.
inline void add_process(race_suspect_function_t job, const Config & config);
/// Job as a functor.
/// Functor must have overloaded function operator taking no arguments, as well as available copy constructor.
template <typename F>
inline void add_process(F & job, const Config & config);
/// ///////////////////////// ///
/// ///
/// Add pressure generators ///
/// ///
/// ///////////////////////// ///
///
///
/// Side note: you must not worry about specifying incorrect values for pressure
/// generatots; those will run in separate processes and will be
/// respawned, if crashed, having 0 influence on simulation outcome;
/// moreover, in real-life scenarious, unexpected behaviour is
/// the pie, when it comes to race conditions. So, be brave! =)
///
/// Add I/O pressure generator at the specific file system location.
/// Directory path is expected, and if process does not have permission
/// to create/unlink file there, false will be returned.
bool add_io_pressure(const std::string & where);
/// Add CPU pressure generator.
/// Pressure is generated by constantly copying data from one place to another.
/// You can adjust block size and that will basically reflect CPU cache pollution.
/// Block size is specified in kilobytes, with minimum size of 2 kilobytes.
void add_cpu_pressure(size_t kilobytes = 2);
/// Add RAM pressure generator.
/// Pressure is generated by allocating block of memory and keeping it resident
/// in RAM by first making pages dirty and then just sleeping.
/// So yeah, you should probably disable swap, unless you want your system dead =)
/// Block size is specified in megabytes, with minimum size of 1 megabyte.
void add_ram_pressure(size_t magabytes = 1);
/// ///////////////// ///
/// ///
/// Adjust settings ///
/// ///
/// ///////////////// ///
///
///
/// Check whether simulation will abort upon encountering first failure.
bool abort_on_first_failure() const;
/// Check whether simulation will abort upon encountering first failure.
void set_abort_on_first_failure(bool enabled = true);
/// Check if redirection of stdin is enabled.
/// If true, all data from stdin will be redirected to each simulated process.
bool redirect_stdin() const;
/// Enable or disable redirection of stdin.
/// By default, input redirection is disabled.
void set_redirect_stdin(bool enabled = true);
/// Check if logging to stdout/stderr enabled.
bool log_to_std() const;
/// Enable or disable logging to stdout/stderr.
/// Logging is disabled by default.
/// In any case, enabled logging to std or not, all logs
/// will be saved to corresponding buffers.
/// By default logging to stdout/stderr is disabled.
void set_log_to_std(bool enabled = true);
/// Get stdout log buffer content.
/// Simulator's own messages, as well as output from all childs'
/// stdout, will be sent to this buffer.
const std::string & log_messages() const;
/// Clear stdout log buffer.
void clear_log_messages();
/// Get stderr log buffer content.
/// Simulator's own error messages, as well as output from all childs'
/// stderr, will be sent to this buffer.
const std::string & error_log_messages() const;
/// Clear stderr log buffer.
void clear_error_log_messages();
/// ////////////////////// ///
/// ///
/// Begin the simulation ///
/// ///
/// ////////////////////// ///
///
///
/// Returns true if everything gone smooth.
/// If false is returned you can check out log messages.
/// False will be returned in such cases:
/// * internal error;
/// * at least one child process abnormally (by a signal) terminated;
/// * at least one child process's exit code is not 0;
/// * at least one child process's job handler returns false;
/// * at least one child process's job handler throws exception.
bool run_simulation();
/// Clear internal state, abandoning:
/// * added process templates;
/// * added process group templates;
/// * stdout/stderr log messages.
void clear();
private:
impl::Simulator * _p;
};
namespace impl
{
template <typename F>
struct FunctorWrapper : public RaceSuspect
{
FunctorWrapper(const F & functor)
:_functor(functor)
{ }
bool init()
{ return true; }
bool run()
{ return _functor(); }
bool shutdown()
{ return true; }
private:
F _functor;
};
struct FunctionWrapper : public RaceSuspect
{
FunctionWrapper(race_suspect_function_t function)
:_function(function)
{ }
bool init()
{ return true; }
bool run()
{ return _function(); }
bool shutdown()
{ return true; }
private:
race_suspect_function_t _function;
};
} /// namespace impl
inline void Simulator::add_process(race_suspect_function_t job, const std::string & name, size_t respawns)
{ add_process(new impl::FunctionWrapper(job), Config(name, 1, respawns)); }
template <typename F>
inline void Simulator::add_process(F & job, const std::string & name, size_t respawns)
{ add_process(new impl::FunctorWrapper<F>(job), Config(name, 1, respawns)); }
inline void Simulator::add_process_group(race_suspect_function_t job, size_t group_size, const std::string & name, size_t respawns)
{ add_process(new impl::FunctionWrapper(job), Config(name, group_size, respawns)); }
template <typename F>
inline void Simulator::add_process_group(F & job, size_t group_size, const std::string & name, size_t respawns)
{ add_process(new impl::FunctorWrapper<F>(job), Config(name, group_size, respawns)); }
inline void Simulator::add_process(race_suspect_function_t job, const Config & config)
{ add_process(new impl::FunctionWrapper(job), config); }
template <typename F>
inline void Simulator::add_process(F & job, const Config & config)
{ add_process(new impl::FunctorWrapper<F>(job), config); }
} /// namespace rcs
#endif // SIMULATOR_HPP