summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/bench/workgen/workgen.h
blob: c6c739d54cfa618d8d3aa355af1e9e3f0aec9582 (plain)
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*-
 * Public Domain 2014-2019 MongoDB, Inc.
 * Public Domain 2008-2014 WiredTiger, Inc.
 *
 * This is free and unencumbered software released into the public domain.
 *
 * Anyone is free to copy, modify, publish, use, compile, sell, or
 * distribute this software, either in source code form or as a compiled
 * binary, for any purpose, commercial or non-commercial, and by any
 * means.
 *
 * In jurisdictions that recognize copyright laws, the author or authors
 * of this software dedicate any and all copyright interest in the
 * software to the public domain. We make this dedication for the benefit
 * of the public at large and to the detriment of our heirs and
 * successors. We intend this dedication to be an overt act of
 * relinquishment in perpetuity of all present and future rights to this
 * software under copyright law.
 *
 * 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 BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
#include <ostream>
#include <string>
#include <vector>
#include <map>

// For convenience: A type exposed to Python that cannot be negative.
typedef unsigned int uint_t;

namespace workgen {

struct ContextInternal;
struct OperationInternal;
struct TableInternal;
struct ThreadRunner;
struct Thread;
struct Transaction;

#ifndef SWIG
struct OptionsList {
    OptionsList();
    OptionsList(const OptionsList &other);

    void add_int(const char *name, int default_value, const char *desc);
    void add_bool(const char *name, bool default_value, const char *desc);
    void add_double(const char *name, double default_value, const char *desc);
    void add_string(const char *name, const std::string &default_value,
      const char *desc);

    std::string help() const;
    std::string help_description(const char *option_name) const;
    std::string help_type(const char *option_name) const;

private:
    void add_option(const char *name, const std::string typestr,
      const char *desc);
    typedef std::pair<std::string, std::string> TypeDescPair;
    std::map<std::string, TypeDescPair> _option_map;
};
#endif

// These classes are all exposed to Python via SWIG. While they may contain
// data that is private to C++, such data must not prevent the objects from
// being shared. Tables, Keys, Values, Operations and Threads can be shared: a
// single Key object might appear in many operations; Operations may appear
// multiple times in a Thread or in different Threads; the same Thread may
// appear multiple times in a Workload list, etc.
//
// Certain kinds of state are allowed: A Table contains a unique pointer that
// is used within the internal part of the Context.  Stats contain lots
// of state, but is made available after a Workload.run().
//
// Python controls the lifetime of (nearly) all objects of these classes.
// The exception is Stat/Track objects, which are also created/used
// internally to calculate and show statistics during a run.
//
struct Track {
    // Threads maintain the total thread operation and total latency they've
    // experienced.

    uint64_t ops_in_progress;           // Total operations not completed */
    uint64_t ops;                       // Total operations completed */
    uint64_t rollbacks;                 // Total operations rolled back */
    uint64_t latency_ops;               // Total ops sampled for latency
    uint64_t latency;                   // Total latency */
    uint64_t bucket_ops;                // Computed for percentile_latency

    // Minimum/maximum latency, shared with the monitor thread, that is, the
    // monitor thread clears it so it's recalculated again for each period.

    uint32_t min_latency;                // Minimum latency (uS)
    uint32_t max_latency;                // Maximum latency (uS)

    Track(bool latency_tracking = false);
    Track(const Track &other);
    ~Track();

    void add(Track&, bool reset = false);
    void assign(const Track&);
    uint64_t average_latency() const;
    void begin();
    void clear();
    void complete();
    void complete_with_latency(uint64_t usecs);
    uint64_t percentile_latency(int percent) const;
    void subtract(const Track&);
    void track_latency(bool);
    bool track_latency() const { return (us != NULL); }

    void _get_us(long *);
    void _get_ms(long *);
    void _get_sec(long *);

private:
    // Latency buckets. From python, accessed via methods us(), ms(), sec()
    uint32_t *us;                        // < 1us ... 1000us
    uint32_t *ms;                        // < 1ms ... 1000ms
    uint32_t *sec;                       // < 1s 2s ... 100s

    Track & operator=(const Track &other);   // use explicit assign method
};

struct Stats {
    Track checkpoint;
    Track insert;
    Track not_found;
    Track read;
    Track remove;
    Track update;
    Track truncate;

    Stats(bool latency = false);
    Stats(const Stats &other);
    ~Stats();

    void add(Stats&, bool reset = false);
    void assign(const Stats&);
    void clear();
    void describe(std::ostream &os) const;
#ifndef SWIG
    void final_report(std::ostream &os, timespec &totalsecs) const;
    void report(std::ostream &os) const;
#endif
    void subtract(const Stats&);
    void track_latency(bool);
    bool track_latency() const { return (insert.track_latency()); }

private:
    Stats & operator=(const Stats &other);   // use explicit assign method
};

// A Context tracks the current record number for each uri, used
// for key generation.
//
struct Context {
    bool _verbose;
    ContextInternal *_internal;

    Context();
    ~Context();
    void describe(std::ostream &os) const {
	os << "Context: verbose " << (_verbose ? "true" : "false");
    }

#ifndef SWIG
    Context& operator=(const Context &other);
#endif
};

// To prevent silent errors, this class is set up in Python so that new
// properties are prevented, only existing properties can be set.
//
struct TableOptions {
    uint_t key_size;
    uint_t value_size;
    uint_t value_compressibility;
    bool random_value;
    uint_t range;

    TableOptions();
    TableOptions(const TableOptions &other);
    ~TableOptions();

    void describe(std::ostream &os) const {
	os << "key_size " << key_size;
	os << ", value_size " << value_size;
	os << ", random_value " << random_value;
	os << ", range " << range;
    }

    std::string help() const { return _options.help(); }
    std::string help_description(const char *option_name) const {
	return _options.help_description(option_name); }
    std::string help_type(const char *option_name) const {
	return _options.help_type(option_name); }

private:
    OptionsList _options;
};

struct Table {
    TableOptions options;
    std::string _uri;
    TableInternal *_internal;

    /* XXX select table from range */

    Table();
    Table(const char *tablename);
    Table(const Table &other);
    ~Table();

    void describe(std::ostream &os) const;

#ifndef SWIG
    Table& operator=(const Table &other);
#endif
};

struct ParetoOptions {
    int param;
    double range_low;
    double range_high;
    ParetoOptions(int param = 0);
    ParetoOptions(const ParetoOptions &other);
    ~ParetoOptions();

    void describe(std::ostream &os) const {
	os << "parameter " << param;
	if (range_low != 0.0 || range_high != 1.0) {
	    os << "range [" << range_low << "-" << range_high << "]";
	}
    }

    std::string help() const { return _options.help(); }
    std::string help_description(const char *option_name) const {
	return _options.help_description(option_name); }
    std::string help_type(const char *option_name) const {
	return _options.help_type(option_name); }

    static ParetoOptions DEFAULT;
private:
    OptionsList _options;
};

struct Key {
    typedef enum {
	KEYGEN_AUTO, KEYGEN_APPEND, KEYGEN_PARETO, KEYGEN_UNIFORM } KeyType;
    KeyType _keytype;
    int _size;
    ParetoOptions _pareto;

    /* XXX specify more about key distribution */
    Key() : _keytype(KEYGEN_AUTO), _size(0), _pareto(ParetoOptions::DEFAULT) {}
    Key(KeyType keytype, int size=0,
      const ParetoOptions &pareto=ParetoOptions::DEFAULT) :
	_keytype(keytype), _size(size), _pareto(pareto) {}
    Key(const Key &other) : _keytype(other._keytype), _size(other._size),
	_pareto(other._pareto) {}
    ~Key() {}

    void describe(std::ostream &os) const {
	os << "Key: type " << _keytype << ", size " << _size; }
};

struct Value {
    int _size;

    /* XXX specify how value is calculated */
    Value() : _size(0) {}
    Value(int size) : _size(size) {}
    Value(const Value &other) : _size(other._size) {}
    ~Value() {}

    void describe(std::ostream &os) const { os << "Value: size " << _size; }
};

struct Operation {
    enum OpType {
	OP_CHECKPOINT, OP_INSERT, OP_LOG_FLUSH, OP_NONE, OP_NOOP,
	OP_REMOVE, OP_SEARCH, OP_SLEEP, OP_UPDATE };
    OpType _optype;
    OperationInternal *_internal;

    Table _table;
    Key _key;
    Value _value;
    std::string _config;
    Transaction *_transaction;
    std::vector<Operation> *_group;
    int _repeatgroup;
    double _timed;

    Operation();
    Operation(OpType optype, Table table, Key key, Value value);
    Operation(OpType optype, Table table, Key key);
    Operation(OpType optype, Table table);
    // Constructor with string applies to NOOP, SLEEP, CHECKPOINT
    Operation(OpType optype, const char *config);
    Operation(const Operation &other);
    ~Operation();

    // Check if adding (via Python '+') another operation to this one is
    // as easy as appending the new operation to the _group.
    bool combinable() const;
    void describe(std::ostream &os) const;
#ifndef SWIG
    Operation& operator=(const Operation &other);
    void init_internal(OperationInternal *other);
    void create_all();
    void get_static_counts(Stats &stats, int multiplier);
    bool is_table_op() const;
    void kv_compute_max(bool iskey, bool has_random);
    void kv_gen(ThreadRunner *runner, bool iskey, uint64_t compressibility,
       uint64_t n, char *result) const;
    void kv_size_buffer(bool iskey, size_t &size) const;
    void size_check() const;
    void synchronized_check() const;
#endif
};

// To prevent silent errors, this class is set up in Python so that new
// properties are prevented, only existing properties can be set.
//
struct ThreadOptions {
    std::string name;
    double throttle;
    double throttle_burst;
    bool synchronized;

    ThreadOptions();
    ThreadOptions(const ThreadOptions &other);
    ~ThreadOptions();

    void describe(std::ostream &os) const {
	os << "throttle " << throttle;
	os << ", throttle_burst " << throttle_burst;
	os << ", synchronized " << synchronized;
    }

    std::string help() const { return _options.help(); }
    std::string help_description(const char *option_name) const {
	return _options.help_description(option_name); }
    std::string help_type(const char *option_name) const {
	return _options.help_type(option_name); }

private:
    OptionsList _options;
};

// This is a list of threads, which may be used in the Workload constructor.
// It participates with ThreadList defined on the SWIG/Python side and
// some Python operators added to Thread to allow Threads to be easily
// composed using '+' and multiplied (by integer counts) using '*'.
// Users of the workgen API in Python don't ever need to use
// ThreadListWrapper or ThreadList.
struct ThreadListWrapper {
    std::vector<Thread> _threads;

    ThreadListWrapper() : _threads() {}
    ThreadListWrapper(const ThreadListWrapper &other) :
	_threads(other._threads) {}
    ThreadListWrapper(const std::vector<Thread> &threads) : _threads(threads) {}
    void extend(const ThreadListWrapper &);
    void append(const Thread &);
    void multiply(const int);
};

struct Thread {
    ThreadOptions options;
    Operation _op;

    Thread();
    Thread(const Operation &op);
    Thread(const Thread &other);
    ~Thread();

    void describe(std::ostream &os) const;
};

struct Transaction {
    bool _rollback;
    std::string _begin_config;
    std::string _commit_config;

    Transaction(const char *_config = NULL) : _rollback(false),
       _begin_config(_config == NULL ? "" : _config), _commit_config() {}

    void describe(std::ostream &os) const {
	os << "Transaction: ";
	if (_rollback)
	    os << "(rollback) ";
	os << "begin_config: " << _begin_config;
	if (!_commit_config.empty())
	    os << ", commit_config: " << _commit_config;
    }
};

// To prevent silent errors, this class is set up in Python so that new
// properties are prevented, only existing properties can be set.
//
struct WorkloadOptions {
    int max_latency;
    std::string report_file;
    int report_interval;
    int run_time;
    int sample_interval_ms;
    int sample_rate;
    std::string sample_file;
    int warmup;

    WorkloadOptions();
    WorkloadOptions(const WorkloadOptions &other);
    ~WorkloadOptions();

    void describe(std::ostream &os) const {
	os << "run_time " << run_time;
	os << ", report_interval " << report_interval;
    }

    std::string help() const { return _options.help(); }
    std::string help_description(const char *option_name) const {
	return _options.help_description(option_name); }
    std::string help_type(const char *option_name) const {
	return _options.help_type(option_name); }

private:
    OptionsList _options;
};

struct Workload {
    WorkloadOptions options;
    Stats stats;
    Context *_context;
    std::vector<Thread> _threads;

    Workload(Context *context, const ThreadListWrapper &threadlist);
    Workload(Context *context, const Thread &thread);
    Workload(const Workload &other);
    ~Workload();

#ifndef SWIG
    Workload& operator=(const Workload &other);
#endif

    void describe(std::ostream &os) const {
	os << "Workload: ";
	_context->describe(os);
	os << ", ";
	options.describe(os);
	os << ", [" << std::endl;
	for (std::vector<Thread>::const_iterator i = _threads.begin(); i != _threads.end(); i++) {
	    os << "  "; i->describe(os); os << std::endl;
	}
	os << "]";
    }
    int run(WT_CONNECTION *conn);
};

}