Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / omap_bench.h
1 /*
2  * Generate latency statistics for a configurable number of object map write
3  * operations of configurable size.
4  *
5  *  Created on: May 21, 2012
6  *      Author: Eleanor Cawthon
7  *
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.
12  */
13
14 #ifndef OMAP_BENCH_HPP_
15 #define OMAP_BENCH_HPP_
16
17 #include "common/Mutex.h"
18 #include "common/Cond.h"
19 #include "include/rados/librados.hpp"
20 #include <string>
21 #include <map>
22 #include <cfloat>
23
24 using ceph::bufferlist;
25
26 struct o_bench_data {
27   double avg_latency;
28   double min_latency;
29   double max_latency;
30   double total_latency;
31   int started_ops;
32   int completed_ops;
33   std::map<int,int> freq_map;
34   pair<int,int> mode;
35   o_bench_data()
36   : avg_latency(0.0), min_latency(DBL_MAX), max_latency(0.0),
37     total_latency(0.0),
38     started_ops(0), completed_ops(0)
39   {}
40 };
41
42 class OmapBench;
43
44 typedef int (*omap_generator_t)(const int omap_entries, const int key_size,
45                                 const int value_size,
46                                 std::map<std::string,bufferlist> *out_omap);
47 typedef int (OmapBench::*test_t)(omap_generator_t omap_gen);
48
49
50 class Writer{
51 protected:
52   string oid;
53   utime_t begin_time;
54   utime_t end_time;
55   std::map<std::string,bufferlist> omap;
56   OmapBench *ob;
57   friend class OmapBench;
58 public:
59   Writer(OmapBench *omap_bench);
60   virtual ~Writer(){};
61   virtual void start_time();
62   virtual void stop_time();
63   virtual double get_time();
64   virtual string get_oid();
65   virtual std::map<std::string,bufferlist> & get_omap();
66 };
67
68 class AioWriter : public Writer{
69 protected:
70   librados::AioCompletion * aioc;
71   friend class OmapBench;
72
73 public:
74   AioWriter(OmapBench *omap_bench);
75   ~AioWriter() override;
76   virtual librados::AioCompletion * get_aioc();
77   virtual void set_aioc(librados::callback_t complete,
78       librados::callback_t safe);
79 };
80
81 class OmapBench{
82 protected:
83   librados::IoCtx io_ctx;
84   librados::Rados rados;
85   struct o_bench_data data;
86   test_t test;
87   omap_generator_t omap_generator;
88
89   //aio things
90   Cond thread_is_free;
91   Mutex thread_is_free_lock;
92   Mutex  data_lock;
93   int busythreads_count;
94   librados::callback_t comp;
95   librados::callback_t safe;
96
97   string pool_name;
98   string rados_id;
99   string prefix;
100   int threads;
101   int objects;
102   int entries_per_omap;
103   int key_size;
104   int value_size;
105   double increment;
106
107   friend class Writer;
108   friend class AioWriter;
109
110 public:
111   OmapBench()
112     : test(&OmapBench::test_write_objects_in_parallel),
113       omap_generator(generate_uniform_omap),
114       thread_is_free_lock("OmapBench::thread_is_free_lock"),
115       data_lock("OmapBench::data_lock"),
116       busythreads_count(0),
117       comp(NULL), safe(aio_is_safe),
118       pool_name("rbd"),
119       rados_id("admin"),
120       prefix(rados_id+".obj."),
121       threads(3), objects(100), entries_per_omap(10), key_size(10),
122       value_size(100), increment(10)
123   {}
124   /**
125    * Parses command line args, initializes rados and ioctx
126    */
127   int setup(int argc, const char** argv);
128
129   /**
130    * Callback for when an AioCompletion (called from an AioWriter)
131    * is safe. deletes the AioWriter that called it,
132    * Updates data, updates busythreads, and signals thread_is_free.
133    *
134    * @param c provided by aio_write - not used
135    * @param arg the AioWriter that contains this AioCompletion
136    */
137   static void aio_is_safe(rados_completion_t c, void *arg);
138
139   /**
140    * Generates a random string len characters long
141    */
142   static string random_string(int len);
143
144   /*
145    * runs the test specified by test using the omap generator specified by
146    * omap_generator
147    *
148    * @return error code
149    */
150   int run();
151
152   /*
153    * Prints all keys and values for all omap entries for all objects
154    */
155   int print_written_omap();
156
157   /*
158    * Displays relevant constants and the histogram generated through a test
159    */
160   void print_results();
161
162   /**
163    * Writes an object with the specified AioWriter.
164    *
165    * @param aiow the AioWriter to write with
166    * @param omap the omap to write
167    * @post: an asynchronous omap_set is launched
168    */
169   int write_omap_asynchronously(AioWriter *aiow,
170       const std::map<std::string,bufferlist> &map);
171
172
173   /**
174    * Generates an omap with omap_entries entries, each with keys key_size
175    * characters long and with string values value_size characters long.
176    *
177    * @param out_map pointer to the map to be created
178    * @return error code
179    */
180   static int generate_uniform_omap(const int omap_entries, const int key_size,
181       const int value_size, std::map<std::string,bufferlist> * out_omap);
182
183   /**
184    * The same as generate_uniform_omap except that string lengths are picked
185    * randomly between 1 and the int arguments
186    */
187   static int generate_non_uniform_omap(const int omap_entries,
188       const int key_size,
189       const int value_size, std::map<std::string,bufferlist> * out_omap);
190
191   static int generate_small_non_random_omap(const int omap_entries,
192       const int key_size, const int value_size,
193       std::map<std::string,bufferlist> * out_omap);
194
195   /*
196    * Uses aio_write to write omaps generated by omap_gen to OBJECTS objects
197    * using THREADS AioWriters at a time.
198    *
199    * @param omap_gen the method used to generate the omaps.
200    */
201   int test_write_objects_in_parallel(omap_generator_t omap_gen);
202
203 };
204
205
206
207 #endif /* OMAP_BENCH_HPP_ */
208