773099d469c14d0943a5eb76b10e5871a0bce352
[archipelago] / xseg / peers / user / bench-xseg.h
1 /*
2  * Copyright 2012 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *   2. Redistributions in binary form must reproduce the above
12  *      copyright notice, this list of conditions and the following
13  *      disclaimer in the documentation and/or other materials
14  *      provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * The views and conclusions contained in the software and
30  * documentation are those of the authors and should not be
31  * interpreted as representing official policies, either expressed
32  * or implied, of GRNET S.A.
33  */
34
35 #include <xseg/protocol.h>
36 #include <bench-lfsr.h>
37
38 #ifdef __GNUC__
39 #define LIKELY(x)       __builtin_expect(!!(x),1)
40 #define UNLIKELY(x)     __builtin_expect(!!(x),0)
41 #else
42 #define LIKELY(x)       (x)
43 #define UNLIKELY(x)     (x)
44 #endif
45
46 /*
47  * If CLOCK_MONOTONIC_RAW is not defined in our system, use CLOCK_MONOTONIC
48  * instead. CLOCK_MONOTONIC_RAW is preferred since we are guaranteed that the
49  * clock won't skew.
50  */
51 #ifdef CLOCK_MONOTONIC_RAW
52 #define CLOCK_BENCH CLOCK_MONOTONIC_RAW
53 #else
54 #define CLOCK_BENCH CLOCK_MONOTONIC
55 #endif
56
57
58 #define MAX_ARG_LEN 10
59
60 /*
61  * Pattern type occupies 1st flag bit.
62  * If 0, it's sequential, if 1, it's random.
63  */
64 #define PATTERN_FLAG_POS 0
65 #define PATTERN_BITMASK 1
66 #define PATTERN_SEQ 0
67 #define PATTERN_RAND 1
68
69 /*
70  * Verify mode occupies 2nd and 3rd flag bit.
71  * If 01, it uses metadata for verification, if 11 it writes pseudorandom nums
72  * in chunk's memory range and if 00, it's off.
73  */
74 #define VERIFY_FLAG_POS 1
75 #define VERIFY_BITMASK 3        /* i.e. "11" in binary form */
76 #define VERIFY_NO 0
77 #define VERIFY_META 1
78 #define VERIFY_FULL 2
79
80 /* Timer insanity occupies 4th and 5th flag bit */
81 #define INSANITY_FLAG_POS 3
82 #define INSANITY_BITMASK 3      /* i.e. "11" in binary form */
83 #define INSANITY_SANE 0
84 #define INSANITY_ECCENTRIC 1
85 #define INSANITY_MANIC 2
86 #define INSANITY_PARANOID 3
87
88 /* Progress bar option occupies 6th flag bit */
89 #define PROGRESS_FLAG_POS 5
90 #define PROGRESS_BITMASK 1
91 #define PROGRESS_NO 0
92 #define PROGRESS_YES 1
93
94 /* Ping option occupies 7th flag bit */
95 #define PING_FLAG_POS 6
96 #define PING_BITMASK 1
97 #define PING_MODE_OFF 0
98 #define PING_MODE_ON 1
99
100 /*
101  * Current bench flags representation:
102  * 64  8  7  6  5  4  3  2  1 : bits
103  * ... 0  0  0  0  0  0  0  0
104  *       |_||_||____||____||_|
105  *        ^  ^    ^     ^   ^
106  *        |  |    |     |   |
107  *      ping | insanity | pattern
108  *        progress   verify
109  */
110
111 /*
112  * Find position of flag, make it zero, get requested flag value, store it to
113  * this position
114  */
115 #define SET_FLAG(__ftype, __flag, __val)        \
116         __flag = (__flag & ~(__ftype##_BITMASK << __ftype##_FLAG_POS)) | \
117         (__val << __ftype##_FLAG_POS);
118
119 /* Apply bitmask to flags, shift result to the right to get correct value */
120 #define GET_FLAG(__ftype, __flag)                       \
121         (__flag & (__ftype##_BITMASK << __ftype##_FLAG_POS)) >> __ftype##_FLAG_POS
122
123 /*
124  * For now, the seed length is fixed to 9 digits whereas the object number
125  * length is fixed to fifteen digits.
126  */
127 #define SEEDLEN 9
128 #define OBJNUMLEN 15
129
130 struct bench {
131         uint64_t to; //Total number of objects (not for read/write)
132         uint64_t ts; //Total I/O size
133         uint64_t os; //Object size
134         uint64_t bs; //Block size
135         uint32_t iodepth; //Num of in-flight xseg reqs
136         xport dst_port;
137         xport src_port;
138         uint32_t op;    //xseg operation
139         uint64_t flags;
140         struct peerd *peer;
141         struct req_status *status;
142         struct bench_lfsr *lfsr;
143         struct object_vars *objvars;
144         struct timer *total_tm; //Total time for benchmark
145         struct timer *get_tm;   //Time for xseg_get_request
146         struct timer *sub_tm;   //Time for xseg_submit_request
147         struct timer *rec_tm;   //Time for xseg_receive_request
148 };
149
150 struct object_vars {
151         char name[XSEG_MAX_TARGETLEN];
152         int namelen;
153         char prefix[XSEG_MAX_TARGETLEN];
154         int prefixlen;
155         uint64_t seed;
156         int seedlen; /* seed length is hardcoded for now*/
157         uint64_t objnum;
158         int objnumlen;  /* object number length is hardcoded for now*/
159 };
160
161 struct req_status {
162         uint64_t max;           /* Max requests for benchmark */
163         uint64_t submitted;
164         uint64_t received;
165         uint64_t corrupted;     /* Requests that did not pass verification */
166         uint64_t failed;
167 };
168
169 /*
170  * Custom timespec. Made to calculate variance, where we need the square of a
171  * timespec struct. This struct should be more than enough to hold the square
172  * of the biggest timespec.
173  */
174 struct timespec2 {
175         unsigned long tv_sec2;
176         uint64_t tv_nsec2;
177 };
178
179 /*
180  * struct timer fields
181  * ====================
182  * completed: number of completed requests
183  * start_time: submission time of a request
184  * sum: the sum of elapsed times of every completed request
185  * sum_sq: the sum of the squares of elapsed times
186  * insanity: benchmarking level, higher means that the request associated with
187  *           this timer is more trivial.
188  */
189 struct timer {
190         struct timespec sum;
191         struct timespec2 sum_sq;
192         struct timespec start_time;
193         struct timespec elapsed_time;
194         uint64_t completed;
195         int insanity;
196 };
197
198 struct tm_result {
199         unsigned int s;
200         unsigned int ms;
201         unsigned int us;
202         unsigned int ns;
203 };
204
205 struct signature {
206         uint64_t id;
207         uint64_t object;
208         uint64_t offset;
209 };
210
211 struct bw {
212         double val;
213         char unit[5];
214 };
215
216 int bench_peerd_loop(void *arg);
217
218 void timer_start(struct bench *prefs, struct timer *sample_req);
219 void timer_stop(struct bench *prefs, struct timer *sample_tm,
220                 struct timespec *start);
221 int init_timer(struct timer **tm, int insanity);
222 uint64_t str2num(char *str);
223 int read_op(char *op);
224 int read_pattern(char *pattern);
225 int read_insanity(char *insanity);
226 int read_verify(char *insanity);
227 int read_progress(char *progress);
228 int read_ping(char *progress);
229 void clear_lines(struct bench *prefs);
230 void print_res(struct bench *prefs);
231 void print_stats(struct bench *prefs);
232 void print_progress(struct bench *prefs);
233 void print_remaining(struct bench *prefs);
234 void create_target(struct bench *prefs, struct xseg_request *req);
235 void create_chunk(struct bench *prefs, struct xseg_request *req, uint64_t new);
236 int read_chunk(struct bench *prefs, struct xseg_request *req);
237 uint64_t determine_next(struct bench *prefs);
238 uint64_t calculate_offset(struct bench *prefs, uint64_t new);
239 uint64_t calculate_prog_quantum(struct bench *prefs);
240 int validate_seed(struct bench *prefs, unsigned long seed);
241
242 void inspect_obv(struct object_vars *obv);
243 uint64_t __get_object(struct bench *prefs, uint64_t new);
244