1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2012 New Dream Network
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
13 #ifndef WORKLOAD_GENERATOR_H_
14 #define WORKLOAD_GENERATOR_H_
16 #include "os/ObjectStore.h"
17 #include <boost/scoped_ptr.hpp>
18 #include <boost/random/mersenne_twister.hpp>
19 #include <boost/random/uniform_int.hpp>
22 #include "TestObjectStoreState.h"
27 typedef boost::mt11213b rngen_t;
29 class WorkloadGenerator : public TestObjectStoreState {
31 static const int def_max_in_flight = 50;
33 static const int def_destroy_coll_every_nr_runs = 100;
34 static const int def_num_obj_per_coll = 6000;
35 static const int def_num_colls = 30;
37 static const size_t min_write_bytes = 1;
38 static const size_t max_write_mb = 5;
39 static const size_t max_write_bytes = (max_write_mb * 1024 * 1024);
41 static const size_t min_xattr_obj_bytes = 2;
42 static const size_t max_xattr_obj_bytes = 300;
43 static const size_t min_xattr_coll_bytes = 4;
44 static const size_t max_xattr_coll_bytes = 600;
46 static const size_t log_append_bytes = 1024;
50 unsigned int written_data;
51 WorkloadGenerator *wrkldgen;
53 C_StatState(WorkloadGenerator *state, utime_t s)
54 : start(s), written_data(0), wrkldgen(state) { }
61 int m_destroy_coll_every_nr_runs;
62 std::atomic<int> m_nr_runs = { 0 };
68 map<coll_t, uint64_t> pg_log_size;
70 size_t m_write_data_bytes;
71 size_t m_write_xattr_obj_bytes;
72 size_t m_write_xattr_coll_bytes;
73 size_t m_write_pglog_bytes;
75 bool m_suppress_write_data;
76 bool m_suppress_write_xattr_obj;
77 bool m_suppress_write_xattr_coll;
78 bool m_suppress_write_log;
82 int m_stats_finished_txs;
84 int m_stats_show_secs;
86 size_t m_stats_total_written;
87 utime_t m_stats_begin;
91 void _suppress_ops_or_die(std::string& val);
92 size_t _parse_size_or_die(std::string& val);
93 void init_args(vector<const char*> args);
95 int get_uniform_random_value(int min, int max);
96 coll_entry_t *get_rnd_coll_entry(bool erase);
97 hobject_t *get_rnd_obj(coll_entry_t *entry);
98 int get_random_collection_nr();
99 int get_random_object_nr(int coll_nr);
101 size_t get_random_byte_amount(size_t min, size_t max);
102 void get_filled_byte_array(bufferlist& bl, size_t size);
104 void do_write_object(ObjectStore::Transaction *t,
105 coll_t coll, hobject_t obj, C_StatState *stat);
106 void do_setattr_object(ObjectStore::Transaction *t,
107 coll_t coll, hobject_t obj, C_StatState *stat);
108 void do_pgmeta_omap_set(ObjectStore::Transaction *t, spg_t pgid, coll_t coll,
110 void do_append_log(ObjectStore::Transaction *t, coll_entry_t *entry,
113 bool should_destroy_collection() {
114 return ((m_destroy_coll_every_nr_runs > 0) &&
115 (m_nr_runs >= m_destroy_coll_every_nr_runs));
117 void do_destroy_collection(ObjectStore::Transaction *t, coll_entry_t *entry,
119 coll_entry_t *do_create_collection(ObjectStore::Transaction *t,
125 explicit WorkloadGenerator(vector<const char*> args);
126 ~WorkloadGenerator() {
130 class C_OnReadable: public TestObjectStoreState::C_OnFinished {
131 WorkloadGenerator *wrkldgen_state;
134 explicit C_OnReadable(WorkloadGenerator *state)
135 :TestObjectStoreState::C_OnFinished(state), wrkldgen_state(state) { }
137 void finish(int r) override
139 TestObjectStoreState::C_OnFinished::finish(r);
140 wrkldgen_state->m_nr_runs++;
144 class C_OnDestroyed: public C_OnReadable {
145 coll_entry_t *m_entry;
148 C_OnDestroyed(WorkloadGenerator *state, coll_entry_t *entry) :
149 C_OnReadable(state), m_entry(entry) {}
151 void finish(int r) override {
152 C_OnReadable::finish(r);
157 class C_StatWrapper : public Context {
158 C_StatState *stat_state;
162 C_StatWrapper(C_StatState *state, Context *context)
163 : stat_state(state), ctx(context) { }
165 void finish(int r) override {
168 stat_state->wrkldgen->m_stats_lock.Lock();
170 stat_state->wrkldgen->m_stats_total_written += stat_state->written_data;
171 stat_state->wrkldgen->m_stats_finished_txs ++;
172 stat_state->wrkldgen->m_stats_lock.Unlock();
179 bool operator<(const WorkloadGenerator::coll_entry_t& l,
180 const WorkloadGenerator::coll_entry_t& r) {
181 return (l.m_id < r.m_id);
184 #endif /* WORKLOAD_GENERATOR_H_ */