| 1 |
/* Copyright 2009-2012 Thomas Schoebel-Theuer / 1&1 Internet AG |
| 2 |
* |
| 3 |
* Email: [email protected] |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU General Public License |
| 7 |
* as published by the Free Software Foundation; either version 2 |
| 8 |
* of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* Replay of blktrace traces. |
| 21 |
* |
| 22 |
* The standard input must be in a special format (see docs). |
| 23 |
* |
| 24 |
* TODO: Reorganize / modularize code. It grew over a too long time, out |
| 25 |
* of a really _trivial_ initial version. Too many features were added |
| 26 |
* which were originally never envisioned. |
| 27 |
* Replace some global variables with helper structs for |
| 28 |
* parameters, runtime states, etc. |
| 29 |
* |
| 30 |
* MAYBE: Add reasonable getopt() in place of wild-grown parameter parsing, |
| 31 |
* but KEEP PORTABILITY IN MIND, even to very old UNICES. |
| 32 |
* |
| 33 |
* Exception: for good timer resolution, we use nanosleep() & friends. |
| 34 |
* But this can be easily abstracted/interfaced to |
| 35 |
* elder kernel interfaces when necessary. |
| 36 |
*/ |
| 37 |
|
| 38 |
#define _FILE_OFFSET_BITS 64 |
| 39 |
#define _LARGEFILE64_SOURCE |
| 40 |
#define _GNU_SOURCE |
| 41 |
#include <config.h> |
| 42 |
|
| 43 |
#include <ctype.h> |
| 44 |
|
| 45 |
#include <stdio.h> |
| 46 |
|
| 47 |
#include <errno.h> |
| 48 |
|
| 49 |
#ifdef STDC_HEADERS |
| 50 |
# include <stdlib.h> |
| 51 |
# include <stddef.h> |
| 52 |
#else |
| 53 |
# ifdef HAVE_STDLIB_H |
| 54 |
# include <stdlib.h> |
| 55 |
# endif |
| 56 |
#endif |
| 57 |
|
| 58 |
#ifdef HAVE_UNISTD_H |
| 59 |
# include <unistd.h> |
| 60 |
#endif |
| 61 |
|
| 62 |
#ifdef HAVE_LIMITS_H |
| 63 |
# include <limits.h> |
| 64 |
#endif |
| 65 |
|
| 66 |
#include <fcntl.h> |
| 67 |
|
| 68 |
#ifdef HAVE_STRING_H |
| 69 |
# if !defined STDC_HEADERS && defined HAVE_MEMORY_H |
| 70 |
# include <memory.h> |
| 71 |
# endif |
| 72 |
#include <string.h> |
| 73 |
#endif |
| 74 |
#ifdef HAVE_STRINGS_H |
| 75 |
# include <strings.h> |
| 76 |
#endif |
| 77 |
|
| 78 |
#include <time.h> |
| 79 |
|
| 80 |
#include <math.h> |
| 81 |
|
| 82 |
#include <signal.h> |
| 83 |
|
| 84 |
#ifdef HAVE_SYS_STAT_H |
| 85 |
# include <sys/stat.h> |
| 86 |
#endif |
| 87 |
|
| 88 |
#ifdef HAVE_SYS_TYPES_H |
| 89 |
# include <sys/types.h> |
| 90 |
#endif |
| 91 |
|
| 92 |
/********************************************************** |
| 93 |
* |
| 94 |
*/ |
| 95 |
|
| 96 |
#ifndef HAVE_DECL_POSIX_MEMALIGN |
| 97 |
# if HAVE_MALLOC_H && HAVE_DECL_MEMALIGN |
| 98 |
# define posix_memalign(res,align,size) ((*(res)) = memalign((align), (size)), 0) |
| 99 |
# else |
| 100 |
# define posix_memalign(res,align,size) ((*(res)) = malloc(size), 0) |
| 101 |
# endif |
| 102 |
#endif |
| 103 |
|
| 104 |
#if !HAVE_DECL_LSEEK64 |
| 105 |
# define lseek64 lseek |
| 106 |
#endif |
| 107 |
|
| 108 |
// use -lrt -lm for linking. |
| 109 |
// |
| 110 |
// example: gcc -Wall -O2 blkreplay.c -lrt -lm -o blkreplay |
| 111 |
|
| 112 |
#define QUEUES 4096 |
| 113 |
#define MAX_THREADS 32768 |
| 114 |
#define FILL_FACTOR 8 |
| 115 |
#define DEFAULT_START_GRACE 15 |
| 116 |
#define DEFAULT_THREADS 1024 |
| 117 |
#define DEFAULT_FAN_OUT 4 |
| 118 |
#define DEFAULT_SPEEDUP 1.0 |
| 119 |
|
| 120 |
#ifndef TMP_DIR |
| 121 |
# define TMP_DIR "/tmp" |
| 122 |
#endif |
| 123 |
#define VERIFY_TABLE "verify_table" // default filename for verify table |
| 124 |
#define COMPLETION_TABLE "completion_table" // default filename for completed requests |
| 125 |
|
| 126 |
#define RQ_HASH_MAX (16 * QUEUES) |
| 127 |
#define FLOAT long double |
| 128 |
|
| 129 |
//#define DEBUG_TIMING |
| 130 |
|
| 131 |
FLOAT time_factor = DEFAULT_SPEEDUP; |
| 132 |
FLOAT time_stretch = 1.0 / DEFAULT_SPEEDUP; |
| 133 |
int replay_start = 0; |
| 134 |
int replay_end = 0; |
| 135 |
int replay_duration = 0; |
| 136 |
int replay_out = 0; |
| 137 |
int already_forked = 0; |
| 138 |
int overflow = 0; |
| 139 |
int verify_fd = -1; |
| 140 |
int complete_fd = -1; |
| 141 |
int main_fd = -1; |
| 142 |
char *mmap_ptr = NULL; |
| 143 |
char *main_name = NULL; |
| 144 |
long long main_size = 0; |
| 145 |
long long max_size = 0; |
| 146 |
int dry_run = 0; |
| 147 |
int fake_io = 0; |
| 148 |
int fork_dispatcher = 1; |
| 149 |
int use_o_direct = 1; |
| 150 |
int use_o_sync = 0; |
| 151 |
|
| 152 |
int mmap_mode = 0; |
| 153 |
int conflict_mode = 2; |
| 154 |
/* 0 = allow arbitrary permutations in ordering |
| 155 |
* 1 = drop conflicting requests |
| 156 |
* 2 = partial order by pushing back conflicting requests |
| 157 |
* 3 = enforce ordering by waiting for conflicts |
| 158 |
*/ |
| 159 |
int strong_mode = 1; |
| 160 |
/* 0 = conflicts are only counting between write/write |
| 161 |
* 1 = conflicts are between read/write and write/write |
| 162 |
* 2 = treat any damaged IO as conflicting |
| 163 |
*/ |
| 164 |
int verify_mode = 0; |
| 165 |
/* 0 = no verify |
| 166 |
* 1 = verify data during execution |
| 167 |
* 2 = additionally verify data after execution |
| 168 |
* 3 = additionally re-read and verify data immediatley after each write |
| 169 |
*/ |
| 170 |
int final_verify_mode = 0; |
| 171 |
|
| 172 |
int table_max = 0; |
| 173 |
int total_max = DEFAULT_THREADS; // parallelism |
| 174 |
int sub_max = 0; |
| 175 |
int fan_out = DEFAULT_FAN_OUT; |
| 176 |
int verbose = 0; |
| 177 |
int bottleneck = 0; |
| 178 |
|
| 179 |
int count_submitted = 0; // number of requests on the fly |
| 180 |
int count_catchup = 0; // number of requests catching up |
| 181 |
int count_pushback = 0; // number of requests on pushback list |
| 182 |
|
| 183 |
int max_submitted = 0; |
| 184 |
int max_pushback = 0; |
| 185 |
|
| 186 |
int statist_lines = 0; // total number of input lines |
| 187 |
int statist_total = 0; // total number of requests |
| 188 |
int statist_writes = 0; // total number of write requests |
| 189 |
int statist_completed = 0; // number of completed requests |
| 190 |
int statist_dropped = 0; // number of dropped requests (verify_mode == 1) |
| 191 |
int statist_pushback = 0; // number of pushed back requests (verify_mode == 2) |
| 192 |
int statist_ordered = 0; // number of waits (verify_mode == 3) |
| 193 |
long long verify_errors = 0; |
| 194 |
long long verify_errors_after = 0; |
| 195 |
long long verify_mismatches = 0; |
| 196 |
|
| 197 |
struct timespec start_grace = { |
| 198 |
.tv_sec = DEFAULT_START_GRACE, |
| 199 |
}; |
| 200 |
struct timespec start_stamp = {}; |
| 201 |
struct timespec first_stamp = {}; |
| 202 |
struct timespec timeshift = {}; |
| 203 |
struct timespec meta_delays = {}; |
| 204 |
long long meta_delay_count; |
| 205 |
struct timespec simulate_io = {}; |
| 206 |
struct timespec ahead_limit = {}; |
| 207 |
|
| 208 |
#define _STRINGIFY(x) #x |
| 209 |
#define STRINGIFY(x) _STRINGIFY(x) |
| 210 |
|
| 211 |
/////////////////////////////////////////////////////////////////////// |
| 212 |
|
| 213 |
#define NANO 1000000000 |
| 214 |
|
| 215 |
static |
| 216 |
void timespec_diff(struct timespec *res, struct timespec *start, struct timespec *time) |
| 217 |
{ |
| 218 |
res->tv_sec = time->tv_sec - start->tv_sec; |
| 219 |
res->tv_nsec = time->tv_nsec - start->tv_nsec; |
| 220 |
//if (time->tv_nsec < start->tv_nsec) { |
| 221 |
if (res->tv_nsec < 0) { |
| 222 |
res->tv_nsec += NANO; |
| 223 |
res->tv_sec--; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
static |
| 228 |
void timespec_add(struct timespec *res, struct timespec *time) |
| 229 |
{ |
| 230 |
res->tv_sec += time->tv_sec; |
| 231 |
res->tv_nsec += time->tv_nsec; |
| 232 |
if (res->tv_nsec >= NANO) { |
| 233 |
res->tv_nsec -= NANO; |
| 234 |
res->tv_sec++; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
static |
| 239 |
void timespec_multiply(struct timespec *res, FLOAT factor) |
| 240 |
{ |
| 241 |
FLOAT val = res->tv_nsec * (1.0/(FLOAT)NANO) + res->tv_sec; |
| 242 |
val *= factor; |
| 243 |
res->tv_sec = floor(val); |
| 244 |
res->tv_nsec = floor((val - res->tv_sec) * NANO); |
| 245 |
} |
| 246 |
|
| 247 |
/////////////////////////////////////////////////////////////////////// |
| 248 |
|
| 249 |
// output flushing |
| 250 |
|
| 251 |
static struct timespec flush_total = {}; |
| 252 |
char *my_role = "undefined"; |
| 253 |
|
| 254 |
static |
| 255 |
void flush_stdout(void) |
| 256 |
{ |
| 257 |
if (verbose > 0) { |
| 258 |
struct timespec before; |
| 259 |
struct timespec after; |
| 260 |
struct timespec diff; |
| 261 |
|
| 262 |
clock_gettime(CLOCK_REALTIME, &before); |
| 263 |
fflush(stdout); |
| 264 |
clock_gettime(CLOCK_REALTIME, &after); |
| 265 |
|
| 266 |
timespec_diff(&diff, &before, &after); |
| 267 |
timespec_add(&flush_total, &diff); |
| 268 |
} else { // save processor time and syscalls |
| 269 |
fflush(stdout); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
static |
| 274 |
void set_role(char *txt) |
| 275 |
{ |
| 276 |
my_role = txt; |
| 277 |
if (verbose > 0) { |
| 278 |
printf("INFO: process pid=%d role='%s'\n", |
| 279 |
getpid(), |
| 280 |
my_role); |
| 281 |
flush_stdout(); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
static |
| 286 |
void do_exit(int status) |
| 287 |
{ |
| 288 |
if (verbose > 0) { |
| 289 |
printf("INFO: exit pid=%d role='%s' status=%d flush_total=%lu.%09lu\n", |
| 290 |
getpid(), |
| 291 |
my_role, |
| 292 |
status, |
| 293 |
flush_total.tv_sec, flush_total.tv_nsec); |
| 294 |
} |
| 295 |
exit(status); |
| 296 |
} |
| 297 |
|
| 298 |
/////////////////////////////////////////////////////////////////////// |
| 299 |
|
| 300 |
// internal data structures |
| 301 |
|
| 302 |
struct verify_tag { |
| 303 |
long long tag_start; |
| 304 |
unsigned int tag_seqnr; |
| 305 |
unsigned int tag_write_seqnr; |
| 306 |
long long tag_len; |
| 307 |
long long tag_index; |
| 308 |
}; |
| 309 |
|
| 310 |
struct request { |
| 311 |
struct verify_tag tag; |
| 312 |
struct timespec orig_stamp; |
| 313 |
struct timespec orig_factor_stamp; |
| 314 |
struct timespec replay_stamp; |
| 315 |
struct timespec replay_duration; |
| 316 |
long long seqnr; |
| 317 |
long long sector; |
| 318 |
int length; |
| 319 |
int verify_errors; |
| 320 |
short q_nr; |
| 321 |
short q_index; |
| 322 |
char rwbs; |
| 323 |
char has_version; |
| 324 |
// starting from here, the rest is _not_ transferred over the pipelines |
| 325 |
struct request *next; |
| 326 |
unsigned int *old_version; |
| 327 |
}; |
| 328 |
|
| 329 |
// the following reduces a potential space bottleneck on the answer pipe |
| 330 |
#define RQ_SIZE (sizeof(struct request) - 2*sizeof(void*)) |
| 331 |
#ifdef PIPE_BUF |
| 332 |
# define FILL_MAX (PIPE_BUF / RQ_SIZE) |
| 333 |
#else |
| 334 |
# define FILL_MAX (1024 / RQ_SIZE) |
| 335 |
#endif |
| 336 |
|
| 337 |
static struct request *rq_hash[RQ_HASH_MAX] = {}; |
| 338 |
|
| 339 |
#define rq_hash_fn(sector) (sector % RQ_HASH_MAX) |
| 340 |
|
| 341 |
static |
| 342 |
struct request *find_request_seq(long long sector, long long seqnr) |
| 343 |
{ |
| 344 |
struct request *rq = rq_hash[rq_hash_fn(sector)]; |
| 345 |
while (rq && rq->seqnr != seqnr) |
| 346 |
rq = rq->next; |
| 347 |
return rq; |
| 348 |
} |
| 349 |
|
| 350 |
static |
| 351 |
void add_request(struct request *rq) |
| 352 |
{ |
| 353 |
struct request **ptr = &rq_hash[rq_hash_fn(rq->sector)]; |
| 354 |
rq->next = *ptr; |
| 355 |
*ptr = rq; |
| 356 |
} |
| 357 |
|
| 358 |
static |
| 359 |
void del_request(long long sector, long long seqnr) |
| 360 |
{ |
| 361 |
struct request **ptr = &rq_hash[rq_hash_fn(sector)]; |
| 362 |
struct request *found = NULL; |
| 363 |
while (*ptr && (*ptr)->seqnr != seqnr) { |
| 364 |
ptr = &(*ptr)->next; |
| 365 |
} |
| 366 |
if (*ptr && (*ptr)->sector == sector) { |
| 367 |
found = *ptr; |
| 368 |
*ptr = found->next; |
| 369 |
if (found->old_version) { |
| 370 |
free(found->old_version); |
| 371 |
} |
| 372 |
free(found); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
/////////////////////////////////////////////////////////////////////// |
| 377 |
|
| 378 |
/* Fast in-memory determination of conflicts. |
| 379 |
* Used in place of temporary files (where possible). |
| 380 |
*/ |
| 381 |
#define FLY_HASH 2048 |
| 382 |
#define FLY_ZONE 64 |
| 383 |
#define FLY_HASH_FN(sector) (((sector) / FLY_ZONE) % FLY_HASH) |
| 384 |
#define FLY_NEXT_ZONE(sector) (((sector / FLY_ZONE) + 1) * FLY_ZONE) |
| 385 |
|
| 386 |
struct fly { |
| 387 |
struct fly *fl_next; |
| 388 |
long long fl_sector; |
| 389 |
int fl_len; |
| 390 |
char fl_rwbs; |
| 391 |
}; |
| 392 |
|
| 393 |
struct fly_hash { |
| 394 |
struct fly *fly_hash_table[FLY_HASH]; |
| 395 |
int fly_count; |
| 396 |
}; |
| 397 |
|
| 398 |
static |
| 399 |
void fly_add(struct fly_hash *hash, long long sector, int len, char rwbs) |
| 400 |
{ |
| 401 |
while (len > 0) { |
| 402 |
struct fly *new = malloc(sizeof(struct fly)); |
| 403 |
int index = FLY_HASH_FN(sector); |
| 404 |
int max_len = FLY_NEXT_ZONE(sector) - sector; |
| 405 |
int this_len = len; |
| 406 |
if (this_len > max_len) |
| 407 |
this_len = max_len; |
| 408 |
|
| 409 |
if (!new) { |
| 410 |
printf("FATAL ERROR: out of memory for hashing\n"); |
| 411 |
flush_stdout(); |
| 412 |
do_exit(-1); |
| 413 |
} |
| 414 |
new->fl_sector = sector; |
| 415 |
new->fl_len = this_len; |
| 416 |
new->fl_rwbs = rwbs; |
| 417 |
new->fl_next = hash->fly_hash_table[index]; |
| 418 |
hash->fly_hash_table[index] = new; |
| 419 |
hash->fly_count++; |
| 420 |
|
| 421 |
sector += this_len; |
| 422 |
len -= this_len; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
static |
| 427 |
int fly_check(struct fly_hash *hash, long long sector, int len, char rwbs) |
| 428 |
{ |
| 429 |
while (len > 0) { |
| 430 |
struct fly *tmp = hash->fly_hash_table[FLY_HASH_FN(sector)]; |
| 431 |
int max_len = FLY_NEXT_ZONE(sector) - sector; |
| 432 |
int this_len = len; |
| 433 |
if (this_len > max_len) |
| 434 |
this_len = max_len; |
| 435 |
|
| 436 |
for (; tmp != NULL; tmp = tmp->fl_next) { |
| 437 |
if (sector + this_len > tmp->fl_sector && sector < tmp->fl_sector + tmp->fl_len) { |
| 438 |
if (strong_mode >= 2) |
| 439 |
return 1; |
| 440 |
if (rwbs == 'R' && toupper(tmp->fl_rwbs) == 'R') |
| 441 |
continue; |
| 442 |
if (strong_mode || |
| 443 |
(rwbs == 'W' && |
| 444 |
toupper(tmp->fl_rwbs) == 'W')) |
| 445 |
return 1; |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
sector += this_len; |
| 450 |
len -= this_len; |
| 451 |
} |
| 452 |
return 0; |
| 453 |
} |
| 454 |
|
| 455 |
static |
| 456 |
void fly_delete(struct fly_hash *hash, long long sector, int len, char rwbs) |
| 457 |
{ |
| 458 |
while (len > 0) { |
| 459 |
struct fly **res = &hash->fly_hash_table[FLY_HASH_FN(sector)]; |
| 460 |
struct fly *tmp; |
| 461 |
int max_len = FLY_NEXT_ZONE(sector) - sector; |
| 462 |
int this_len = len; |
| 463 |
if (this_len > max_len) |
| 464 |
this_len = max_len; |
| 465 |
|
| 466 |
for (tmp = *res; tmp != NULL; res = &tmp->fl_next, tmp = *res) { |
| 467 |
if (tmp->fl_sector == sector && tmp->fl_len == this_len && toupper(tmp->fl_rwbs) == rwbs) { |
| 468 |
*res = tmp->fl_next; |
| 469 |
hash->fly_count--; |
| 470 |
free(tmp); |
| 471 |
break; |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
sector += this_len; |
| 476 |
len -= this_len; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
static struct fly_hash fly_hash = {}; |
| 481 |
|
| 482 |
/////////////////////////////////////////////////////////////////////// |
| 483 |
|
| 484 |
// abstracting read() and write() |
| 485 |
|
| 486 |
static |
| 487 |
int do_read(void *buffer, int len) |
| 488 |
{ |
| 489 |
#ifdef HAVE_DECL_NANOSLEEP |
| 490 |
if (simulate_io.tv_sec || simulate_io.tv_nsec) |
| 491 |
nanosleep(&simulate_io, NULL); |
| 492 |
#endif |
| 493 |
if (dry_run) |
| 494 |
return len; |
| 495 |
if (mmap_ptr) { |
| 496 |
} |
| 497 |
return read(main_fd, buffer, len); |
| 498 |
} |
| 499 |
|
| 500 |
static |
| 501 |
int do_write(void *buffer, int len) |
| 502 |
{ |
| 503 |
#ifdef HAVE_DECL_NANOSLEEP |
| 504 |
if (simulate_io.tv_sec || simulate_io.tv_nsec) |
| 505 |
nanosleep(&simulate_io, NULL); |
| 506 |
#endif |
| 507 |
if (dry_run) |
| 508 |
return len; |
| 509 |
if (mmap_ptr) { |
| 510 |
} |
| 511 |
return write(main_fd, buffer, len); |
| 512 |
} |
| 513 |
|
| 514 |
/////////////////////////////////////////////////////////////////////// |
| 515 |
|
| 516 |
// version number bookkeeping |
| 517 |
|
| 518 |
static |
| 519 |
unsigned int *get_blockversion(int fd, long long blocknr, int count) |
| 520 |
{ |
| 521 |
int status; |
| 522 |
void *data; |
| 523 |
int memlen = count * sizeof(unsigned int); |
| 524 |
struct timespec t0; |
| 525 |
struct timespec t1; |
| 526 |
struct timespec delay; |
| 527 |
|
| 528 |
if (!verify_mode) |
| 529 |
return NULL; |
| 530 |
|
| 531 |
clock_gettime(CLOCK_REALTIME, &t0); |
| 532 |
|
| 533 |
data = malloc(memlen); |
| 534 |
if (!data) { |
| 535 |
printf("FATAL ERROR: out of memory for hashing\n"); |
| 536 |
flush_stdout(); |
| 537 |
verify_mode = 0; |
| 538 |
return NULL; |
| 539 |
} |
| 540 |
if (lseek64(fd, blocknr * sizeof(unsigned int), SEEK_SET) != blocknr * sizeof(unsigned int)) { |
| 541 |
printf("FATAL ERROR: llseek(%lld) in verify table failed %d (%s)\n", blocknr, errno, strerror(errno)); |
| 542 |
flush_stdout(); |
| 543 |
verify_mode = 0; |
| 544 |
free(data); |
| 545 |
return NULL; |
| 546 |
} |
| 547 |
|
| 548 |
status = read(fd, data, memlen); |
| 549 |
|
| 550 |
clock_gettime(CLOCK_REALTIME, &t1); |
| 551 |
timespec_diff(&delay, &t0, &t1); |
| 552 |
timespec_add(&meta_delays, &delay); |
| 553 |
meta_delay_count++; |
| 554 |
|
| 555 |
if (status < 0) { |
| 556 |
printf("FATAL ERROR: read(%lld) from verify table failed %d %d (%s)\n", blocknr, status, errno, strerror(errno)); |
| 557 |
flush_stdout(); |
| 558 |
verify_mode = 0; |
| 559 |
free(data); |
| 560 |
return NULL; |
| 561 |
} |
| 562 |
if (status < memlen) { // this may result from a sparse file |
| 563 |
memset(data+status, 0, memlen - status); |
| 564 |
} |
| 565 |
return data; |
| 566 |
} |
| 567 |
|
| 568 |
static |
| 569 |
void put_blockversion(int fd, unsigned int *data, long long blocknr, int count) |
| 570 |
{ |
| 571 |
int status; |
| 572 |
struct timespec t0; |
| 573 |
struct timespec t1; |
| 574 |
struct timespec delay; |
| 575 |
|
| 576 |
if (!verify_mode) |
| 577 |
return; |
| 578 |
|
| 579 |
clock_gettime(CLOCK_REALTIME, &t0); |
| 580 |
|
| 581 |
if (lseek64(fd, blocknr * sizeof(unsigned int), SEEK_SET) != blocknr * sizeof(unsigned int)) { |
| 582 |
printf("FATAL ERROR: llseek(%lld) in verify table failed %d (%s)\n", blocknr, errno, strerror(errno)); |
| 583 |
flush_stdout(); |
| 584 |
verify_mode = 0; |
| 585 |
return; |
| 586 |
} |
| 587 |
|
| 588 |
status = write(fd, data, count * sizeof(unsigned int)); |
| 589 |
|
| 590 |
clock_gettime(CLOCK_REALTIME, &t1); |
| 591 |
timespec_diff(&delay, &t0, &t1); |
| 592 |
timespec_add(&meta_delays, &delay); |
| 593 |
meta_delay_count++; |
| 594 |
|
| 595 |
if (status < 0) { |
| 596 |
printf("FATAL ERROR: write to verify table failed %d (%s)\n", errno, strerror(errno)); |
| 597 |
flush_stdout(); |
| 598 |
verify_mode = 0; |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
static |
| 603 |
void force_blockversion(int fd, unsigned int version, long long blocknr, int count) |
| 604 |
{ |
| 605 |
if (!verify_mode) |
| 606 |
return; |
| 607 |
{ |
| 608 |
unsigned int data[count]; |
| 609 |
int i; |
| 610 |
for (i = 0; i < count; i++) |
| 611 |
data[i] = version; |
| 612 |
put_blockversion(fd, data, blocknr, count); |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
static |
| 617 |
int compare_blockversion(unsigned int *a, unsigned int *b, int count) |
| 618 |
{ |
| 619 |
for (; count > 0; a++, b++, count--) { |
| 620 |
if (!*a) |
| 621 |
continue; // ignore |
| 622 |
if (!*b || *a > *b) |
| 623 |
return 1; // mismatch |
| 624 |
} |
| 625 |
return 0; |
| 626 |
} |
| 627 |
|
| 628 |
static |
| 629 |
void advance_blockversion(unsigned int *data, int count, unsigned int version) |
| 630 |
{ |
| 631 |
for (; count > 0; data++, count--) { |
| 632 |
if (*data < version) |
| 633 |
*data = version; |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
/////////////////////////////////////////////////////////////////////// |
| 638 |
|
| 639 |
// infrastructure for verify mode |
| 640 |
|
| 641 |
static |
| 642 |
void check_tags(struct request *rq, void *buffer, int len, int do_write) |
| 643 |
{ |
| 644 |
int i; |
| 645 |
char *mode = do_write ? "write" : "read"; |
| 646 |
if (!verify_mode || !rq->old_version) |
| 647 |
return; |
| 648 |
for (i = 0; i < len; i += 512) { |
| 649 |
struct verify_tag *tag = buffer+i; |
| 650 |
if (!rq->old_version[i/512]) { // version not yet valid |
| 651 |
continue; |
| 652 |
} |
| 653 |
if (dry_run) |
| 654 |
continue; |
| 655 |
if (tag->tag_start != start_stamp.tv_sec) { |
| 656 |
printf("VERIFY ERROR (%s): bad start tag at sector %lld+%d (tag %lld != [expected] %ld)\n", mode, rq->sector, i/512, tag->tag_start, start_stamp.tv_sec); |
| 657 |
flush_stdout(); |
| 658 |
rq->verify_errors++; |
| 659 |
continue; |
| 660 |
} |
| 661 |
/* Only writes are fully protected from overlaps with other writes. |
| 662 |
* Reads may overlap with concurrent writes to some degree. |
| 663 |
* Therefore we check reads only for backslips in time |
| 664 |
* (which never should happen). |
| 665 |
*/ |
| 666 |
if ((do_write && tag->tag_write_seqnr != rq->old_version[i/512]) || |
| 667 |
tag->tag_write_seqnr < rq->old_version[i/512]) { |
| 668 |
printf("VERIFY ERROR (%s): data version mismatch at sector %lld+%d (seqnr %u != [expected] %u)\n", mode, rq->sector, i/512, tag->tag_write_seqnr, rq->old_version[i/512]); |
| 669 |
flush_stdout(); |
| 670 |
rq->verify_errors++; |
| 671 |
continue; |
| 672 |
} |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
static |
| 677 |
void make_tags(struct request *rq, void *buffer, int len) |
| 678 |
{ |
| 679 |
int i; |
| 680 |
|
| 681 |
// check old tag before overwriting |
| 682 |
if (verify_mode >= 1) { |
| 683 |
int status; |
| 684 |
status = do_read(buffer, len); |
| 685 |
if (status == len) { |
| 686 |
check_tags(rq, buffer, len, 1); |
| 687 |
} |
| 688 |
if (lseek64(main_fd, rq->sector * 512, SEEK_SET) != rq->sector * 512) { |
| 689 |
printf("ERROR: bad lseek at make_tags()\n"); |
| 690 |
flush_stdout(); |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
memset(buffer, 0, len); |
| 695 |
|
| 696 |
for (i = 0; i < len; i += 512) { |
| 697 |
struct verify_tag *tag = buffer+i; |
| 698 |
memcpy(tag, &rq->tag, sizeof(*tag)); |
| 699 |
tag->tag_len = len; |
| 700 |
tag->tag_index = i; |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
#define TAG_CHUNK (4096 * 1024 / sizeof(unsigned int)) |
| 705 |
|
| 706 |
static |
| 707 |
void check_all_tags() |
| 708 |
{ |
| 709 |
long long blocknr = 0; |
| 710 |
int status; |
| 711 |
long long checked = 0; |
| 712 |
int i; |
| 713 |
unsigned int *table = NULL; |
| 714 |
unsigned int *table2 = NULL; |
| 715 |
void *buffer = NULL; |
| 716 |
struct verify_tag *tag; |
| 717 |
|
| 718 |
printf("checking all tags..........\n"); |
| 719 |
flush_stdout(); |
| 720 |
|
| 721 |
if (posix_memalign((void**)&table, 4096, TAG_CHUNK) || |
| 722 |
posix_memalign((void**)&table2, 4096, TAG_CHUNK) || |
| 723 |
posix_memalign(&buffer, 512, 512)) { |
| 724 |
printf("FATAL ERROR: cannot allocate memory\n"); |
| 725 |
flush_stdout(); |
| 726 |
do_exit(-1); |
| 727 |
} |
| 728 |
tag = buffer; |
| 729 |
|
| 730 |
lseek64(verify_fd, 0, SEEK_SET); |
| 731 |
lseek64(complete_fd, 0, SEEK_SET); |
| 732 |
|
| 733 |
for (;;) { |
| 734 |
status = read(verify_fd, table, TAG_CHUNK); |
| 735 |
if (!status) |
| 736 |
break; |
| 737 |
if (status < 0) { |
| 738 |
printf("ERROR: cannot read version table for block %lld: %d %s\n", blocknr, errno, strerror(errno)); |
| 739 |
flush_stdout(); |
| 740 |
break; |
| 741 |
} |
| 742 |
status = read(complete_fd, table2, TAG_CHUNK); |
| 743 |
if (status <= 0) { |
| 744 |
printf("ERROR: cannot read completion table for block %lld: %d %s\n", blocknr, errno, strerror(errno)); |
| 745 |
flush_stdout(); |
| 746 |
break; |
| 747 |
} |
| 748 |
|
| 749 |
for (i = 0; i < status / sizeof(unsigned int); i++, blocknr++) { |
| 750 |
unsigned int version = table[i]; |
| 751 |
unsigned int version2; |
| 752 |
|
| 753 |
if (!version) |
| 754 |
continue; |
| 755 |
|
| 756 |
if (lseek64(main_fd, blocknr * 512, SEEK_SET) != blocknr * 512) { |
| 757 |
printf("ERROR: bad lseek in check_all_tags()\n"); |
| 758 |
flush_stdout(); |
| 759 |
} |
| 760 |
if (do_read(buffer, 512) != 512) { |
| 761 |
printf("ERROR: bad read in check_all_tags(): %d %s\n", errno, strerror(errno)); |
| 762 |
flush_stdout(); |
| 763 |
} |
| 764 |
checked++; |
| 765 |
version2 = table2[i]; |
| 766 |
if (dry_run) |
| 767 |
continue; |
| 768 |
if (tag->tag_write_seqnr != version && tag->tag_write_seqnr != version2) { |
| 769 |
if (version != version2) { |
| 770 |
printf("VERIFY MISMATCH: at block %lld (%u != %u != %u)\n", blocknr, tag->tag_write_seqnr, version, version2); |
| 771 |
flush_stdout(); |
| 772 |
verify_mismatches++; |
| 773 |
} else { |
| 774 |
printf("VERIFY ERROR: at block %lld (%u != [expected] %u)\n", blocknr, tag->tag_write_seqnr, version); |
| 775 |
flush_stdout(); |
| 776 |
verify_errors_after++; |
| 777 |
} |
| 778 |
} |
| 779 |
} |
| 780 |
} |
| 781 |
printf("SUMMARY: checked %lld / %lld blocks (%1.3f%%), found %lld errors, %lld mismatches\n", checked, max_size, 100.0 * (double)checked / (double)max_size, verify_errors_after, verify_mismatches); |
| 782 |
flush_stdout(); |
| 783 |
free(table); |
| 784 |
free(table2); |
| 785 |
free(buffer); |
| 786 |
} |
| 787 |
|
| 788 |
static |
| 789 |
void paranoia_check(struct request *rq, void *buffer) |
| 790 |
{ |
| 791 |
int len = rq->length * 512; |
| 792 |
long long newpos = (long long)rq->sector * 512; |
| 793 |
int status2; |
| 794 |
void *buffer2 = NULL; |
| 795 |
struct verify_tag *tag = buffer; |
| 796 |
struct verify_tag *tag2; |
| 797 |
long long s_status; |
| 798 |
|
| 799 |
if (posix_memalign(&buffer2, 4096, len)) { |
| 800 |
printf("VERIFY ERROR: cannot allocate memory\n"); |
| 801 |
flush_stdout(); |
| 802 |
rq->verify_errors++; |
| 803 |
goto done; |
| 804 |
} |
| 805 |
tag2 = buffer2; |
| 806 |
s_status = lseek64(main_fd, newpos, SEEK_SET); |
| 807 |
if (s_status != newpos) { |
| 808 |
printf("VERIFY ERROR: bad lseek64() %lld on main_fd=%d at pos %lld (%s) pid=%d\n", s_status, main_fd, rq->sector, strerror(errno), getpid()); |
| 809 |
flush_stdout(); |
| 810 |
rq->verify_errors++; |
| 811 |
goto done; |
| 812 |
} |
| 813 |
status2 = do_read(buffer2, len); |
| 814 |
if (status2 != len) { |
| 815 |
printf("VERIFY ERROR: bad %cIO %d / %d on %d at pos %lld (%s)\n", rq->rwbs, status2, errno, main_fd, rq->sector, strerror(errno)); |
| 816 |
flush_stdout(); |
| 817 |
rq->verify_errors++; |
| 818 |
goto done; |
| 819 |
} |
| 820 |
if (dry_run) |
| 821 |
goto done; |
| 822 |
if (memcmp(buffer, buffer2, len)) { |
| 823 |
printf("VERIFY ERROR: memcmp(): bad storage semantics at sector = %lld len = %d tag_start = %lld tag2_start = %lld\n", rq->sector, len, tag->tag_start, tag2->tag_start); |
| 824 |
flush_stdout(); |
| 825 |
rq->verify_errors++; |
| 826 |
goto done; |
| 827 |
} |
| 828 |
done: |
| 829 |
free(buffer2); |
| 830 |
} |
| 831 |
|
| 832 |
/////////////////////////////////////////////////////////////////////// |
| 833 |
|
| 834 |
void grace_diff(struct timespec *diff, struct timespec *now) |
| 835 |
{ |
| 836 |
struct timespec grace; |
| 837 |
clock_gettime(CLOCK_REALTIME, now); |
| 838 |
timespec_diff(&grace, &start_stamp, now); |
| 839 |
timespec_diff(diff, &start_grace, &grace); |
| 840 |
} |
| 841 |
|
| 842 |
void verbose_status(struct request *rq, char *info) |
| 843 |
{ |
| 844 |
struct timespec now; |
| 845 |
struct timespec diff; |
| 846 |
|
| 847 |
grace_diff(&diff, &now); |
| 848 |
|
| 849 |
printf("INFO:" |
| 850 |
" action='%s'" |
| 851 |
" pid=%d" |
| 852 |
" count_submitted=%d" |
| 853 |
" count_catchup=%d" |
| 854 |
" count_pushback=%d" |
| 855 |
" real_time=%ld.%09ld", |
| 856 |
info, |
| 857 |
getpid(), |
| 858 |
count_submitted, |
| 859 |
count_catchup, |
| 860 |
count_pushback, |
| 861 |
diff.tv_sec, diff.tv_nsec); |
| 862 |
|
| 863 |
if (rq) { |
| 864 |
printf(" rq_time=%ld.%09ld" |
| 865 |
" seqnr=%lld" |
| 866 |
" q_nr=%d" |
| 867 |
" block=%lld" |
| 868 |
" len=%d" |
| 869 |
" mode='%c'", |
| 870 |
rq->orig_stamp.tv_sec, rq->orig_stamp.tv_nsec, |
| 871 |
rq->seqnr, |
| 872 |
rq->q_nr, |
| 873 |
rq->sector, |
| 874 |
rq->length, |
| 875 |
rq->rwbs); |
| 876 |
} |
| 877 |
|
| 878 |
printf("\n"); |
| 879 |
flush_stdout(); |
| 880 |
} |
| 881 |
|
| 882 |
/////////////////////////////////////////////////////////////////////// |
| 883 |
|
| 884 |
static |
| 885 |
void do_wait(struct request *rq, struct timespec *now) |
| 886 |
{ |
| 887 |
#ifdef DEBUG_TIMING |
| 888 |
printf("do_wait\n"); |
| 889 |
#endif |
| 890 |
|
| 891 |
for (;;) { |
| 892 |
struct timespec rest_wait = {}; |
| 893 |
|
| 894 |
grace_diff(&rq->replay_stamp, now); |
| 895 |
timespec_diff(&rest_wait, &rq->replay_stamp, &rq->orig_factor_stamp); |
| 896 |
|
| 897 |
#ifdef DEBUG_TIMING |
| 898 |
printf("(%d) %lld.%09ld %lld.%09ld %lld.%09ld\n", |
| 899 |
getpid(), |
| 900 |
(long long)now->tv_sec, |
| 901 |
now->tv_nsec, |
| 902 |
(long long)rq->replay_stamp.tv_sec, |
| 903 |
rq->replay_stamp.tv_nsec, |
| 904 |
(long long)rest_wait.tv_sec, |
| 905 |
rest_wait.tv_nsec); |
| 906 |
flush_stdout(); |
| 907 |
#endif |
| 908 |
|
| 909 |
if ((long long)rest_wait.tv_sec < 0) { |
| 910 |
break; |
| 911 |
} |
| 912 |
|
| 913 |
if (verbose > 3) { |
| 914 |
verbose_status(rq, "nanosleep"); |
| 915 |
} |
| 916 |
|
| 917 |
nanosleep(&rest_wait, NULL); |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
/////////////////////////////////////////////////////////////////////// |
| 922 |
|
| 923 |
static |
| 924 |
int do_action(struct request *rq) |
| 925 |
{ |
| 926 |
struct timespec t0 = {}; |
| 927 |
struct timespec t1 = {}; |
| 928 |
int len = rq->length * 512; |
| 929 |
long long newpos; |
| 930 |
long long s_status; |
| 931 |
|
| 932 |
if (main_fd < 0) { |
| 933 |
return -1; |
| 934 |
} |
| 935 |
if (rq->sector + rq->length >= main_size) { |
| 936 |
printf("ERROR: trying to position at %lld (main_size=%lld)\n", rq->sector, main_size); |
| 937 |
flush_stdout(); |
| 938 |
} |
| 939 |
|
| 940 |
newpos = (long long)rq->sector * 512; |
| 941 |
s_status = lseek64(main_fd, newpos, SEEK_SET); |
| 942 |
if (s_status != newpos) { |
| 943 |
printf("ERROR: bad lseek64() %lld on main_fd=%d at pos %lld (%s) pid=%d\n", s_status, main_fd, rq->sector, strerror(errno), getpid()); |
| 944 |
flush_stdout(); |
| 945 |
return -1; |
| 946 |
} |
| 947 |
{ |
| 948 |
int status = -1; |
| 949 |
void *buffer = NULL; |
| 950 |
if (posix_memalign(&buffer, 4096, len)) { |
| 951 |
printf("ERROR: cannot allocate memory\n"); |
| 952 |
flush_stdout(); |
| 953 |
return -1; |
| 954 |
} |
| 955 |
if (toupper(rq->rwbs) == 'R') { |
| 956 |
do_wait(rq, &t0); |
| 957 |
|
| 958 |
if (!fake_io) |
| 959 |
status = do_read(buffer, len); |
| 960 |
|
| 961 |
clock_gettime(CLOCK_REALTIME, &t1); |
| 962 |
timespec_diff(&rq->replay_duration, &t0, &t1); |
| 963 |
|
| 964 |
check_tags(rq, buffer, len, 0); |
| 965 |
} else { |
| 966 |
make_tags(rq, buffer, len); |
| 967 |
|
| 968 |
do_wait(rq, &t0); |
| 969 |
|
| 970 |
if (!fake_io) |
| 971 |
status = do_write(buffer, len); |
| 972 |
|
| 973 |
clock_gettime(CLOCK_REALTIME, &t1); |
| 974 |
timespec_diff(&rq->replay_duration, &t0, &t1); |
| 975 |
|
| 976 |
if (verify_mode >= 3 && status == len) { // additional re-read and verify |
| 977 |
paranoia_check(rq, buffer); |
| 978 |
} |
| 979 |
} |
| 980 |
free(buffer); |
| 981 |
if (!fake_io && status != len) { |
| 982 |
printf("ERROR: bad %cIO %d / %d on %d at pos %lld (%s)\n", rq->rwbs, status, errno, main_fd, rq->sector, strerror(errno)); |
| 983 |
flush_stdout(); |
| 984 |
//do_exit(-1); |
| 985 |
} |
| 986 |
} |
| 987 |
return 0; |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
/////////////////////////////////////////////////////////////////////// |
| 992 |
|
| 993 |
// submitting requests over pipes |
| 994 |
|
| 995 |
static |
| 996 |
void pipe_write(int fd, void *data, int len) |
| 997 |
{ |
| 998 |
while (len > 0) { |
| 999 |
int status = write(fd, data, len); |
| 1000 |
if (status > 0) { |
| 1001 |
data += status; |
| 1002 |
len -= status; |
| 1003 |
} else { |
| 1004 |
printf("FATAL ERROR: bad pipe write fd=%d status=%d (%d %s)\n", fd, status, errno, strerror(errno)); |
| 1005 |
flush_stdout(); |
| 1006 |
do_exit(-1); |
| 1007 |
} |
| 1008 |
} |
| 1009 |
} |
| 1010 |
|
| 1011 |
static |
| 1012 |
int pipe_read(int fd, void *data, int len) |
| 1013 |
{ |
| 1014 |
int status = read(fd, data, len); |
| 1015 |
if (status < 0) { |
| 1016 |
printf("FATAL ERROR: bad pipe read fd=%d status=%d (%d %s)\n", fd, status, errno, strerror(errno)); |
| 1017 |
flush_stdout(); |
| 1018 |
do_exit(-1); |
| 1019 |
} |
| 1020 |
return status; |
| 1021 |
} |
| 1022 |
|
| 1023 |
static |
| 1024 |
void submit_request(int fd, struct request *rq) |
| 1025 |
{ |
| 1026 |
pipe_write(fd, rq, RQ_SIZE); |
| 1027 |
if (rq->has_version) { |
| 1028 |
int size = rq->length * sizeof(unsigned int); |
| 1029 |
pipe_write(fd, rq->old_version, size); |
| 1030 |
} |
| 1031 |
} |
| 1032 |
|
| 1033 |
static |
| 1034 |
int get_request(int fd, struct request *rq) |
| 1035 |
{ |
| 1036 |
int status = pipe_read(fd, rq, RQ_SIZE); |
| 1037 |
if (status == RQ_SIZE && rq->has_version) { |
| 1038 |
int size = rq->length * sizeof(unsigned int); |
| 1039 |
rq->old_version = malloc(size); |
| 1040 |
if (!rq->old_version) { |
| 1041 |
printf("FATAL ERROR: out of memory\n"); |
| 1042 |
flush_stdout(); |
| 1043 |
do_exit(-1); |
| 1044 |
} |
| 1045 |
pipe_read(fd, rq->old_version, size); |
| 1046 |
} |
| 1047 |
return status; |
| 1048 |
} |
| 1049 |
|
| 1050 |
/////////////////////////////////////////////////////////////////////// |
| 1051 |
|
| 1052 |
// the pipes |
| 1053 |
|
| 1054 |
static int queue[QUEUES][2]; |
| 1055 |
static int answer[2]; |
| 1056 |
|
| 1057 |
static |
| 1058 |
void make_all_queues(int q[][2], int max) |
| 1059 |
{ |
| 1060 |
int i; |
| 1061 |
for(i = 0; i < max; i++) { |
| 1062 |
int status = pipe(q[i]); |
| 1063 |
if (status < 0) { |
| 1064 |
printf("FATAL ERROR: cannot create pipe %d\n", i); |
| 1065 |
do_exit(-1); |
| 1066 |
} |
| 1067 |
} |
| 1068 |
} |
| 1069 |
|
| 1070 |
static |
| 1071 |
void close_all_queues(int q[][2], int max, int i2, int except) |
| 1072 |
{ |
| 1073 |
int i; |
| 1074 |
for(i = 0; i < max; i++) { |
| 1075 |
if (i == except) |
| 1076 |
continue; |
| 1077 |
close(q[i][i2]); |
| 1078 |
} |
| 1079 |
} |
| 1080 |
|
| 1081 |
/////////////////////////////////////////////////////////////////////// |
| 1082 |
|
| 1083 |
// positions: which queue to take? |
| 1084 |
|
| 1085 |
static short *pos_table = NULL; |
| 1086 |
|
| 1087 |
static |
| 1088 |
int pos_get(int offset, int max_filled) |
| 1089 |
{ |
| 1090 |
static int pos_last[2] = {}; |
| 1091 |
int start; |
| 1092 |
int max; |
| 1093 |
int best; |
| 1094 |
int besti; |
| 1095 |
int i; |
| 1096 |
|
| 1097 |
if (!pos_table) { |
| 1098 |
pos_table = malloc(table_max * sizeof(short)); |
| 1099 |
if (!pos_table) { |
| 1100 |
printf("FATAL ERROR: out of memory for pos_table\n"); |
| 1101 |
flush_stdout(); |
| 1102 |
do_exit(-1); |
| 1103 |
} |
| 1104 |
memset(pos_table, 0, table_max * sizeof(short)); |
| 1105 |
} |
| 1106 |
|
| 1107 |
start = (pos_last[offset / total_max] + 1) % total_max; |
| 1108 |
|
| 1109 |
for (max = total_max, i = start; --max >= 0; i = (i + 1) % total_max) { |
| 1110 |
if (pos_table[i + offset] < max_filled) |
| 1111 |
goto ok; |
| 1112 |
} |
| 1113 |
|
| 1114 |
printf("WARN: cannot allocate pipe slot!\n" |
| 1115 |
"WARN: table_max=%d total_max=%d max_filled=%d\n" |
| 1116 |
"WARN: count_submitted=%d count_catchup=%d count_pushback=%d\n" |
| 1117 |
"WARN: offset=%d i=%d pos_table[i+offset]=%d\n" |
| 1118 |
"WARN: pos_last[0]=%d pos_last[1]=%d\n" |
| 1119 |
"WARN: this is no real harm, but your measurements might be DISTORTED\n" |
| 1120 |
"WARN: by some unnecessary artificial delays.\n" |
| 1121 |
"HINT: increase the --threads= parameter.\n", |
| 1122 |
table_max, |
| 1123 |
total_max, |
| 1124 |
max_filled, |
| 1125 |
count_submitted, |
| 1126 |
count_catchup, |
| 1127 |
count_pushback, |
| 1128 |
offset, |
| 1129 |
i, |
| 1130 |
pos_table[i + offset], |
| 1131 |
pos_last[0], |
| 1132 |
pos_last[1]); |
| 1133 |
|
| 1134 |
/* Try to make the best of the mess... |
| 1135 |
*/ |
| 1136 |
best = INT_MAX; |
| 1137 |
besti = -1; |
| 1138 |
for (max = total_max, i = start; --max >= 0; i = (i + 1) % total_max) { |
| 1139 |
if (pos_table[i + offset] < best) { |
| 1140 |
best = pos_table[i + offset]; |
| 1141 |
besti = i; |
| 1142 |
} |
| 1143 |
} |
| 1144 |
if (besti >= 0) { |
| 1145 |
i = besti; |
| 1146 |
printf("WARN: remapped to i=%d pos_table[i+offset]=%d\n", |
| 1147 |
i, |
| 1148 |
pos_table[i + offset]); |
| 1149 |
} |
| 1150 |
flush_stdout(); |
| 1151 |
|
| 1152 |
ok: |
| 1153 |
count_submitted++; |
| 1154 |
if (count_submitted > max_submitted) |
| 1155 |
max_submitted = count_submitted; |
| 1156 |
|
| 1157 |
if (offset > 0) |
| 1158 |
count_catchup++; |
| 1159 |
|
| 1160 |
pos_last[offset / total_max] = i; |
| 1161 |
pos_table[i + offset]++; |
| 1162 |
return i + offset; |
| 1163 |
} |
| 1164 |
|
| 1165 |
static |
| 1166 |
void pos_put(int pos) |
| 1167 |
{ |
| 1168 |
if (pos >= total_max) |
| 1169 |
count_catchup--; |
| 1170 |
count_submitted--; |
| 1171 |
pos_table[pos]--; |
| 1172 |
if (pos_table[pos] < 0) { |
| 1173 |
printf("ERROR: imbalanced pos_table at %d (%d), count_submitted=%d count_catchup=%d\n", |
| 1174 |
pos, |
| 1175 |
pos_table[pos], |
| 1176 |
count_submitted, |
| 1177 |
count_catchup); |
| 1178 |
flush_stdout(); |
| 1179 |
} |
| 1180 |
} |
| 1181 |
|
| 1182 |
/////////////////////////////////////////////////////////////////////// |
| 1183 |
|
| 1184 |
// submission to the right queue |
| 1185 |
|
| 1186 |
static |
| 1187 |
void submit_to_queues(struct request *rq, int is_pushback) |
| 1188 |
{ |
| 1189 |
static long long seqnr = 0; |
| 1190 |
static long long write_seqnr = 0; |
| 1191 |
int index; |
| 1192 |
|
| 1193 |
if (is_pushback) { |
| 1194 |
rq->q_nr = pos_get(total_max, 1); |
| 1195 |
} else { |
| 1196 |
rq->q_nr = pos_get(0, FILL_MAX); |
| 1197 |
} |
| 1198 |
|
| 1199 |
index = rq->q_nr % sub_max; |
| 1200 |
rq->q_index = rq->q_nr / sub_max; |
| 1201 |
|
| 1202 |
// generate write tag |
| 1203 |
rq->tag.tag_start = start_stamp.tv_sec; |
| 1204 |
rq->tag.tag_seqnr = ++seqnr; |
| 1205 |
if (conflict_mode && |
| 1206 |
(strong_mode || toupper(rq->rwbs) == 'W')) |
| 1207 |
fly_add(&fly_hash, rq->sector, rq->length, toupper(rq->rwbs)); |
| 1208 |
|
| 1209 |
if (toupper(rq->rwbs) != 'R') { |
| 1210 |
write_seqnr++; |
| 1211 |
force_blockversion(verify_fd, write_seqnr, rq->sector, rq->length); |
| 1212 |
} |
| 1213 |
rq->tag.tag_write_seqnr = write_seqnr; |
| 1214 |
rq->has_version = !!rq->old_version; |
| 1215 |
|
| 1216 |
submit_request(queue[index][1], rq); |
| 1217 |
|
| 1218 |
if (verbose) { |
| 1219 |
verbose_status(rq, "submit"); |
| 1220 |
} |
| 1221 |
} |
| 1222 |
|
| 1223 |
/////////////////////////////////////////////////////////////////////// |
| 1224 |
|
| 1225 |
// pushback handling (used for partial ordering) |
| 1226 |
|
| 1227 |
static struct request *pushback_head = NULL; |
| 1228 |
static struct request *pushback_tail = NULL; |
| 1229 |
|
| 1230 |
static |
| 1231 |
void add_pushback(struct request *rq) |
| 1232 |
{ |
| 1233 |
if (pushback_tail) { |
| 1234 |
pushback_tail->next = rq; |
| 1235 |
} else { |
| 1236 |
pushback_head = rq; |
| 1237 |
} |
| 1238 |
pushback_tail = rq; |
| 1239 |
rq->next = NULL; |
| 1240 |
|
| 1241 |
rq->rwbs = tolower(rq->rwbs); |
| 1242 |
|
| 1243 |
statist_pushback++; |
| 1244 |
count_pushback++; |
| 1245 |
if (count_pushback > max_pushback) |
| 1246 |
max_pushback = count_pushback; |
| 1247 |
|
| 1248 |
if (verbose) { |
| 1249 |
verbose_status(rq, "pushback"); |
| 1250 |
} |
| 1251 |
} |
| 1252 |
|
| 1253 |
static |
| 1254 |
void check_pushback(void) |
| 1255 |
{ |
| 1256 |
struct request **ptr = &pushback_head; |
| 1257 |
struct request *tmp = *ptr; |
| 1258 |
struct request *prev = NULL; |
| 1259 |
|
| 1260 |
while (tmp) { |
| 1261 |
int has_conflict; |
| 1262 |
|
| 1263 |
if (count_catchup >= total_max) { |
| 1264 |
printf("INFO: stopping pushback scan at" |
| 1265 |
" count_submitted=%d" |
| 1266 |
" count_catchup=%d" |
| 1267 |
" count_pushback=%d\n" |
| 1268 |
"HINT: this means that catch-up is limited by available threads\n", |
| 1269 |
count_submitted, |
| 1270 |
count_catchup, |
| 1271 |
count_pushback); |
| 1272 |
flush_stdout(); |
| 1273 |
break; |
| 1274 |
} |
| 1275 |
|
| 1276 |
has_conflict = fly_check(&fly_hash, tmp->sector, tmp->length, toupper(tmp->rwbs)); |
| 1277 |
if (has_conflict) { |
| 1278 |
prev = tmp; |
| 1279 |
ptr = &tmp->next; |
| 1280 |
tmp = *ptr; |
| 1281 |
continue; |
| 1282 |
} |
| 1283 |
|
| 1284 |
// remove from list |
| 1285 |
*ptr = tmp->next; |
| 1286 |
tmp->next = NULL; |
| 1287 |
count_pushback--; |
| 1288 |
|
| 1289 |
// inform... |
| 1290 |
if (verbose) { |
| 1291 |
verbose_status(tmp, "revival"); |
| 1292 |
} |
| 1293 |
|
| 1294 |
// delayed submit |
| 1295 |
submit_to_queues(tmp, 1); |
| 1296 |
add_request(tmp); |
| 1297 |
|
| 1298 |
// special conditions |
| 1299 |
if (tmp == pushback_tail) { |
| 1300 |
pushback_tail = prev; |
| 1301 |
if (!pushback_tail) |
| 1302 |
pushback_head = NULL; |
| 1303 |
} else if (!pushback_head) |
| 1304 |
pushback_tail = NULL; |
| 1305 |
tmp = *ptr; |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
/////////////////////////////////////////////////////////////////////// |
| 1310 |
|
| 1311 |
static |
| 1312 |
void dump_request(struct request *rq) |
| 1313 |
{ |
| 1314 |
struct timespec delay; |
| 1315 |
static int did_head = 0; |
| 1316 |
if (!did_head) { |
| 1317 |
printf("orig_start ; sector; length ; op ; replay_delay; replay_duration\n"); |
| 1318 |
did_head++; |
| 1319 |
} |
| 1320 |
|
| 1321 |
timespec_diff(&delay, &rq->orig_factor_stamp, &rq->replay_stamp); |
| 1322 |
|
| 1323 |
printf("%4lu.%09lu ; %10lld ; %3d ; %c ; %3lu.%09lu ; %3lu.%09lu\n", |
| 1324 |
rq->orig_factor_stamp.tv_sec, |
| 1325 |
rq->orig_factor_stamp.tv_nsec, |
| 1326 |
rq->sector, |
| 1327 |
rq->length, |
| 1328 |
rq->rwbs, |
| 1329 |
|
| 1330 |
delay.tv_sec + replay_out, |
| 1331 |
delay.tv_nsec, |
| 1332 |
|
| 1333 |
rq->replay_duration.tv_sec, |
| 1334 |
rq->replay_duration.tv_nsec); |
| 1335 |
|
| 1336 |
flush_stdout(); |
| 1337 |
statist_completed++; |
| 1338 |
} |
| 1339 |
|
| 1340 |
static |
| 1341 |
int get_answer(void) |
| 1342 |
{ |
| 1343 |
struct request rq = {}; |
| 1344 |
int status; |
| 1345 |
int res = 0; |
| 1346 |
|
| 1347 |
#ifdef DEBUG_TIMING |
| 1348 |
printf("get_answer\n"); |
| 1349 |
#endif |
| 1350 |
if (!already_forked) |
| 1351 |
goto done; |
| 1352 |
|
| 1353 |
if (verbose > 3) { |
| 1354 |
verbose_status(NULL, "wait_for_answer"); |
| 1355 |
} |
| 1356 |
|
| 1357 |
status = get_request(answer[0], &rq); |
| 1358 |
if (status == RQ_SIZE) { |
| 1359 |
struct request *old; |
| 1360 |
|
| 1361 |
pos_put(rq.q_nr); |
| 1362 |
verify_errors += rq.verify_errors; |
| 1363 |
res = 1; |
| 1364 |
|
| 1365 |
if (verbose) { |
| 1366 |
verbose_status(&rq, "got_answer"); |
| 1367 |
} |
| 1368 |
|
| 1369 |
old = find_request_seq(rq.sector, rq.seqnr); |
| 1370 |
if (old) { |
| 1371 |
memcpy(&old->replay_stamp, &rq.replay_stamp, sizeof(old->replay_stamp)); |
| 1372 |
memcpy(&old->replay_duration, &rq.replay_duration, sizeof(old->replay_duration)); |
| 1373 |
memcpy(&old->orig_factor_stamp, &rq.orig_factor_stamp, sizeof(old->orig_factor_stamp)); |
| 1374 |
dump_request(old); |
| 1375 |
del_request(old->sector, old->seqnr); |
| 1376 |
} else { |
| 1377 |
printf("ERROR: request %lld vanished\n", rq.sector); |
| 1378 |
} |
| 1379 |
|
| 1380 |
if (conflict_mode && |
| 1381 |
(strong_mode || toupper(rq.rwbs) == 'W')) |
| 1382 |
fly_delete(&fly_hash, rq.sector, rq.length, toupper(rq.rwbs)); |
| 1383 |
if (toupper(rq.rwbs) != 'R') { |
| 1384 |
if (verify_mode) { |
| 1385 |
unsigned int *data = get_blockversion(complete_fd, rq.sector, rq.length); |
| 1386 |
advance_blockversion(data, rq.length, rq.tag.tag_write_seqnr); |
| 1387 |
|
| 1388 |
put_blockversion(complete_fd, data, rq.sector, rq.length); |
| 1389 |
} |
| 1390 |
} |
| 1391 |
if (conflict_mode == 2) { |
| 1392 |
/* Handle pushback requests. |
| 1393 |
*/ |
| 1394 |
check_pushback(); |
| 1395 |
} |
| 1396 |
} |
| 1397 |
|
| 1398 |
done: |
| 1399 |
#ifdef DEBUG_TIMING |
| 1400 |
printf("aw=%d\n", res); |
| 1401 |
flush_stdout(); |
| 1402 |
#endif |
| 1403 |
return res; |
| 1404 |
} |
| 1405 |
|
| 1406 |
/////////////////////////////////////////////////////////////////////// |
| 1407 |
|
| 1408 |
static |
| 1409 |
void main_open(int again) |
| 1410 |
{ |
| 1411 |
long long size; |
| 1412 |
int flags = O_RDWR; |
| 1413 |
if (again || dry_run) { |
| 1414 |
flags = O_RDONLY; |
| 1415 |
} |
| 1416 |
#ifdef O_LARGEFILE |
| 1417 |
flags |= O_LARGEFILE; |
| 1418 |
#endif |
| 1419 |
if (use_o_direct) |
| 1420 |
flags |= O_DIRECT; |
| 1421 |
if (use_o_sync) |
| 1422 |
flags |= O_SYNC; |
| 1423 |
|
| 1424 |
main_fd = open(main_name, flags); |
| 1425 |
if (main_fd < 0) { |
| 1426 |
printf("ERROR: cannot open file '%s', errno = %d (%s)\n", main_name, errno, strerror(errno)); |
| 1427 |
do_exit(-1); |
| 1428 |
} |
| 1429 |
size = lseek64(main_fd, 0, SEEK_END); |
| 1430 |
if (size <= 0) { |
| 1431 |
printf("ERROR: cannot determine size of device (%d %s)\n", errno, strerror(errno)); |
| 1432 |
do_exit(-1); |
| 1433 |
} |
| 1434 |
if (!main_size) { |
| 1435 |
main_size = size / 512; |
| 1436 |
printf("INFO: device=%s has blocks=%lld (%lld kB).\n", main_name, main_size, main_size/2); |
| 1437 |
} |
| 1438 |
} |
| 1439 |
|
| 1440 |
static |
| 1441 |
char *mk_temp(const char *basename) |
| 1442 |
{ |
| 1443 |
char *tmpdir = getenv("TMPDIR"); |
| 1444 |
char *res = malloc(1024); |
| 1445 |
int len; |
| 1446 |
if (!res) { |
| 1447 |
printf("FATAL ERROR: out of memory for tmp pathname\n"); |
| 1448 |
do_exit(-1); |
| 1449 |
} |
| 1450 |
if (!tmpdir) |
| 1451 |
tmpdir = TMP_DIR; |
| 1452 |
len = snprintf(res, 1024, "%s/blkreplay.%d/", tmpdir, getpid()); |
| 1453 |
(void)mkdir(res, 0700); |
| 1454 |
snprintf(res + len, 1024 - len, "%s", basename); |
| 1455 |
return res; |
| 1456 |
} |
| 1457 |
|
| 1458 |
static |
| 1459 |
void verify_open(int again) |
| 1460 |
{ |
| 1461 |
int flags; |
| 1462 |
|
| 1463 |
if (!verify_mode) |
| 1464 |
return; |
| 1465 |
|
| 1466 |
flags = O_RDWR | O_CREAT | O_TRUNC; |
| 1467 |
if (again) { |
| 1468 |
flags = O_RDONLY; |
| 1469 |
} |
| 1470 |
#ifdef O_LARGEFILE |
| 1471 |
flags |= O_LARGEFILE; |
| 1472 |
#endif |
| 1473 |
if (verify_fd < 0) { |
| 1474 |
char *file = getenv("VERIFY_TABLE"); |
| 1475 |
if (!file) { |
| 1476 |
file = mk_temp(VERIFY_TABLE); |
| 1477 |
} |
| 1478 |
verify_fd = open(file, flags, S_IRUSR | S_IWUSR); |
| 1479 |
if (verify_fd < 0) { |
| 1480 |
printf("ERROR: cannot open '%s' (%d %s)\n", VERIFY_TABLE, errno, strerror(errno)); |
| 1481 |
flush_stdout(); |
| 1482 |
verify_mode = 0; |
| 1483 |
} |
| 1484 |
} |
| 1485 |
if (complete_fd < 0) { |
| 1486 |
char *file = getenv("COMPLETION_TABLE"); |
| 1487 |
if (!file) { |
| 1488 |
file = mk_temp(COMPLETION_TABLE); |
| 1489 |
} |
| 1490 |
complete_fd = open(file, flags, S_IRUSR | S_IWUSR); |
| 1491 |
if (complete_fd < 0) { |
| 1492 |
printf("ERROR: cannot open '%s' (%d %s)\n", COMPLETION_TABLE, errno, strerror(errno)); |
| 1493 |
flush_stdout(); |
| 1494 |
verify_mode = 0; |
| 1495 |
} |
| 1496 |
} |
| 1497 |
} |
| 1498 |
|
| 1499 |
/////////////////////////////////////////////////////////////////////// |
| 1500 |
|
| 1501 |
static |
| 1502 |
void do_worker(int in_fd, int back_fd) |
| 1503 |
{ |
| 1504 |
int count = 0; |
| 1505 |
/* Each worker needs his own filehandle instance to avoid races |
| 1506 |
* between seek() and read()/write(). |
| 1507 |
* So open it again. |
| 1508 |
*/ |
| 1509 |
if (main_name && main_name[0]) { |
| 1510 |
main_open(0); |
| 1511 |
} |
| 1512 |
for (;;) { |
| 1513 |
struct request rq = {}; |
| 1514 |
int status = get_request(in_fd, &rq); |
| 1515 |
if (!status) |
| 1516 |
break; |
| 1517 |
|
| 1518 |
if (verbose > 1) { |
| 1519 |
verbose_status(&rq, "worker_got_rq"); |
| 1520 |
} |
| 1521 |
|
| 1522 |
(void)do_action(&rq); |
| 1523 |
count++; |
| 1524 |
|
| 1525 |
if (rq.old_version) { |
| 1526 |
free(rq.old_version); |
| 1527 |
rq.old_version = NULL; |
| 1528 |
} |
| 1529 |
rq.has_version = 0; |
| 1530 |
|
| 1531 |
if (verbose > 1) { |
| 1532 |
verbose_status(&rq, "worker_send_answer"); |
| 1533 |
} |
| 1534 |
|
| 1535 |
submit_request(back_fd, &rq); |
| 1536 |
} |
| 1537 |
close(in_fd); |
| 1538 |
close(back_fd); |
| 1539 |
if (verbose > 2) { |
| 1540 |
printf("worker %d count = %d\n", getpid(), count); |
| 1541 |
flush_stdout(); |
| 1542 |
} |
| 1543 |
} |
| 1544 |
|
| 1545 |
/* Intermediate copy thread. |
| 1546 |
* When too many threads are writing into the same pipe, |
| 1547 |
* lock contention may put a serious limit onto overall |
| 1548 |
* throughput. |
| 1549 |
* In order to limit the fan-in / fan-out, we create intermediate |
| 1550 |
* "distributor threads" limiting the maximum competition. |
| 1551 |
*/ |
| 1552 |
#define CP_SIZE (RQ_SIZE * FILL_MAX) |
| 1553 |
|
| 1554 |
static |
| 1555 |
void do_dispatcher_copy(int in_fd, int out_fd) |
| 1556 |
{ |
| 1557 |
int packet_len = CP_SIZE; |
| 1558 |
#if 0 // is this necessary in general? Linux pipe semantics seems to deliver short reads whenever possible, but do other kernels also? |
| 1559 |
if (conflict_mode >= 2) { |
| 1560 |
/* Ensure that replies are delivered as fast as possible. |
| 1561 |
*/ |
| 1562 |
packet_len = RQ_SIZE; |
| 1563 |
} |
| 1564 |
#endif |
| 1565 |
|
| 1566 |
if (verbose > 2) { |
| 1567 |
printf("dispatcher %d fan_out = %d fill_max = %lu packet_len = %d\n", getpid(), fan_out, (unsigned long)FILL_MAX, packet_len); |
| 1568 |
flush_stdout(); |
| 1569 |
} |
| 1570 |
|
| 1571 |
for (;;) { |
| 1572 |
char buf[CP_SIZE]; |
| 1573 |
int status; |
| 1574 |
|
| 1575 |
status = pipe_read(in_fd, buf, packet_len); |
| 1576 |
if (status <= 0) |
| 1577 |
break; |
| 1578 |
if ((status % RQ_SIZE) != 0) { |
| 1579 |
printf("FATAL ERROR: bad record len %d from pipe fd=%d status=%d\n", status % (int)RQ_SIZE, in_fd, status); |
| 1580 |
flush_stdout(); |
| 1581 |
do_exit(-1); |
| 1582 |
} |
| 1583 |
|
| 1584 |
pipe_write(out_fd, buf, status); |
| 1585 |
} |
| 1586 |
} |
| 1587 |
|
| 1588 |
static |
| 1589 |
void _fork_answer_dispatcher(int close_fd) |
| 1590 |
{ |
| 1591 |
int old_answer_1 = answer[1]; |
| 1592 |
int status; |
| 1593 |
pid_t pid; |
| 1594 |
|
| 1595 |
status = pipe(answer); |
| 1596 |
if (status < 0) { |
| 1597 |
printf("FATAL ERROR: cannot create sub-answer pipe\n"); |
| 1598 |
do_exit(-1); |
| 1599 |
} |
| 1600 |
|
| 1601 |
flush_stdout(); |
| 1602 |
pid = fork(); |
| 1603 |
if (pid < 0) { |
| 1604 |
printf("FATAL ERROR: cannot fork answer dispatcher\n"); |
| 1605 |
do_exit(-1); |
| 1606 |
} |
| 1607 |
if (!pid) { // son |
| 1608 |
set_role("answer_dispatcher"); |
| 1609 |
close(close_fd); |
| 1610 |
close(answer[1]); |
| 1611 |
|
| 1612 |
do_dispatcher_copy(answer[0], old_answer_1); |
| 1613 |
|
| 1614 |
do_exit(0); |
| 1615 |
} |
| 1616 |
close(old_answer_1); |
| 1617 |
} |
| 1618 |
|
| 1619 |
/* Intermediate submit thread. |
| 1620 |
* Also useful to reduce contention. |
| 1621 |
* Necessary to distribute the requests to more than 1000 workers, |
| 1622 |
* since the max number of filehandles / pipes is limited. |
| 1623 |
*/ |
| 1624 |
static |
| 1625 |
void _fork_childs(int in_fd, int this_max) |
| 1626 |
{ |
| 1627 |
static int next_max[QUEUES] = {}; |
| 1628 |
int rest; |
| 1629 |
int i; |
| 1630 |
|
| 1631 |
if (this_max <= 1 && in_fd >= 0) { |
| 1632 |
set_role("worker"); |
| 1633 |
do_worker(in_fd, answer[1]); |
| 1634 |
return; |
| 1635 |
} |
| 1636 |
|
| 1637 |
sub_max = fan_out; |
| 1638 |
if (this_max < sub_max) |
| 1639 |
sub_max = this_max; |
| 1640 |
|
| 1641 |
rest = this_max - ((this_max / sub_max) * sub_max); |
| 1642 |
for (i = 0; i < sub_max; i++) { |
| 1643 |
next_max[i] = this_max / sub_max; |
| 1644 |
if (rest-- > 0) |
| 1645 |
next_max[i]++; |
| 1646 |
} |
| 1647 |
|
| 1648 |
make_all_queues(queue, sub_max); |
| 1649 |
flush_stdout(); |
| 1650 |
|
| 1651 |
for (i = 0; i < sub_max; i++) { |
| 1652 |
pid_t pid; |
| 1653 |
|
| 1654 |
pid = fork(); |
| 1655 |
if (pid < 0) { |
| 1656 |
printf("FATAL ERROR: cannot fork child\n"); |
| 1657 |
do_exit(-1); |
| 1658 |
} |
| 1659 |
if (!pid) { // son |
| 1660 |
if (next_max[i] > 1) |
| 1661 |
set_role("submit_dispatcher"); |
| 1662 |
|
| 1663 |
if (in_fd < 0) |
| 1664 |
fclose(stdin); |
| 1665 |
else |
| 1666 |
close(answer[0]); |
| 1667 |
close_all_queues(queue, sub_max, 0, i); |
| 1668 |
close_all_queues(queue, sub_max, 1, -1); |
| 1669 |
|
| 1670 |
if (fork_dispatcher && |
| 1671 |
next_max[i] > 1) { |
| 1672 |
_fork_answer_dispatcher(queue[i][0]); |
| 1673 |
} |
| 1674 |
|
| 1675 |
_fork_childs(queue[i][0], next_max[i]); |
| 1676 |
|
| 1677 |
do_exit(0); |
| 1678 |
} |
| 1679 |
} |
| 1680 |
|
| 1681 |
close_all_queues(queue, sub_max, 0, -1); |
| 1682 |
close(answer[1]); |
| 1683 |
|
| 1684 |
if (in_fd < 0) |
| 1685 |
return; |
| 1686 |
|
| 1687 |
for (;;) { |
| 1688 |
struct request rq = {}; |
| 1689 |
int index; |
| 1690 |
int status = get_request(in_fd, &rq); |
| 1691 |
if (!status) |
| 1692 |
break; |
| 1693 |
|
| 1694 |
index = rq.q_index % sub_max; |
| 1695 |
rq.q_index /= sub_max; |
| 1696 |
submit_request(queue[index][1], &rq); |
| 1697 |
|
| 1698 |
if (rq.old_version) { |
| 1699 |
free(rq.old_version); |
| 1700 |
rq.old_version = NULL; |
| 1701 |
} |
| 1702 |
} |
| 1703 |
} |
| 1704 |
|
| 1705 |
static |
| 1706 |
void fork_childs() |
| 1707 |
{ |
| 1708 |
int status; |
| 1709 |
|
| 1710 |
if (already_forked) |
| 1711 |
return; |
| 1712 |
already_forked++; |
| 1713 |
|
| 1714 |
set_role("main"); |
| 1715 |
|
| 1716 |
// setup pipes and fork() |
| 1717 |
|
| 1718 |
status = pipe(answer); |
| 1719 |
if (status < 0) { |
| 1720 |
printf("FATAL ERROR: cannot create answer pipe\n"); |
| 1721 |
do_exit(-1); |
| 1722 |
} |
| 1723 |
|
| 1724 |
if (verbose > 2) { |
| 1725 |
printf("forking %d child processes in total for parallelism=%d\n", table_max, total_max); |
| 1726 |
flush_stdout(); |
| 1727 |
} |
| 1728 |
|
| 1729 |
_fork_childs(-1, table_max); |
| 1730 |
|
| 1731 |
if (verbose > 2) { |
| 1732 |
printf("done forking (fan_out=%d)\n\n", sub_max); |
| 1733 |
flush_stdout(); |
| 1734 |
} |
| 1735 |
} |
| 1736 |
|
| 1737 |
/////////////////////////////////////////////////////////////////////// |
| 1738 |
|
| 1739 |
static |
| 1740 |
void execute(struct request *rq) |
| 1741 |
{ |
| 1742 |
static int is_called = 0; |
| 1743 |
int first_time; |
| 1744 |
|
| 1745 |
if (!is_called) { // only upon first call |
| 1746 |
is_called++; |
| 1747 |
fork_childs(); |
| 1748 |
} |
| 1749 |
|
| 1750 |
if (rq->old_version) { |
| 1751 |
free(rq->old_version); |
| 1752 |
rq->old_version = NULL; |
| 1753 |
} |
| 1754 |
|
| 1755 |
if (verify_mode) { |
| 1756 |
rq->old_version = get_blockversion(verify_fd, rq->sector, rq->length); |
| 1757 |
} |
| 1758 |
|
| 1759 |
first_time = 0; |
| 1760 |
for (;;) { |
| 1761 |
int status = 0; |
| 1762 |
|
| 1763 |
if (verbose > 3) { |
| 1764 |
verbose_status(rq, "check_for_conflicts"); |
| 1765 |
} |
| 1766 |
|
| 1767 |
if (conflict_mode) { |
| 1768 |
status = fly_check(&fly_hash, rq->sector, rq->length, toupper(rq->rwbs)); |
| 1769 |
} else if (verify_mode) { |
| 1770 |
unsigned int *now_version = get_blockversion(complete_fd, rq->sector, rq->length); |
| 1771 |
status = compare_blockversion(rq->old_version, now_version, rq->length); |
| 1772 |
free(now_version); |
| 1773 |
} |
| 1774 |
if (!status) { // no conflict |
| 1775 |
break; |
| 1776 |
} |
| 1777 |
// well, we have a conflict. |
| 1778 |
if (conflict_mode == 1) { // drop request |
| 1779 |
printf("INFO: dropping block=%lld len=%d mode=%c\n", rq->sector, rq->length, rq->rwbs); |
| 1780 |
flush_stdout(); |
| 1781 |
statist_dropped++; |
| 1782 |
return; |
| 1783 |
} |
| 1784 |
if (conflict_mode == 2) { // pushback request |
| 1785 |
add_pushback(rq); |
| 1786 |
return; |
| 1787 |
} |
| 1788 |
// wait until conflict has gone... |
| 1789 |
if (count_submitted > 0) { |
| 1790 |
get_answer(); |
| 1791 |
if (!first_time++) |
| 1792 |
statist_ordered++; |
| 1793 |
continue; |
| 1794 |
} |
| 1795 |
printf("FATAL ERROR: block %lld waiting for up-to-date version\n", rq->sector); |
| 1796 |
do_exit(-1); |
| 1797 |
} |
| 1798 |
|
| 1799 |
// submit request to worker threads |
| 1800 |
submit_to_queues(rq, 0); |
| 1801 |
add_request(rq); |
| 1802 |
} |
| 1803 |
|
| 1804 |
/////////////////////////////////////////////////////////////////////// |
| 1805 |
|
| 1806 |
// predicates for strategic decisions |
| 1807 |
|
| 1808 |
/* Ensure that the submit queues cannot be filled too much, |
| 1809 |
* measured in real time. |
| 1810 |
* Too much filling could result in unnecessary drops, for example. |
| 1811 |
* Too much filling will not result in any advantage. |
| 1812 |
*/ |
| 1813 |
static |
| 1814 |
int delay_distance(struct timespec *check) |
| 1815 |
{ |
| 1816 |
struct timespec now; |
| 1817 |
struct timespec elapsed; |
| 1818 |
struct timespec diff; |
| 1819 |
struct timespec delta; |
| 1820 |
|
| 1821 |
if (!check->tv_sec) |
| 1822 |
return 0; |
| 1823 |
|
| 1824 |
grace_diff(&elapsed, &now); |
| 1825 |
timespec_diff(&diff, &elapsed, check); |
| 1826 |
timespec_diff(&delta, &diff, &ahead_limit); |
| 1827 |
if ((long)delta.tv_sec < 0) |
| 1828 |
return 1; |
| 1829 |
|
| 1830 |
return 0; |
| 1831 |
} |
| 1832 |
|
| 1833 |
/////////////////////////////////////////////////////////////////////// |
| 1834 |
|
| 1835 |
/* Main dispatcher routine. |
| 1836 |
*/ |
| 1837 |
static |
| 1838 |
void parse(FILE *inp) |
| 1839 |
{ |
| 1840 |
static int first_call = 0; |
| 1841 |
char buffer[4096]; |
| 1842 |
struct timespec old_stamp = {}; |
| 1843 |
struct request *rq = NULL; |
| 1844 |
|
| 1845 |
if (main_name && main_name[0]) { // just determine main_size |
| 1846 |
main_open(0); |
| 1847 |
close(main_fd); |
| 1848 |
main_fd = -1; |
| 1849 |
} |
| 1850 |
verify_open(0); |
| 1851 |
|
| 1852 |
// get start time, but only after opening everything, since open() may produce delays |
| 1853 |
clock_gettime(CLOCK_REALTIME, &start_stamp); |
| 1854 |
|
| 1855 |
if (verbose) { |
| 1856 |
printf("INFO: tag_start=%ld\n", start_stamp.tv_sec); |
| 1857 |
flush_stdout(); |
| 1858 |
} |
| 1859 |
|
| 1860 |
for (;;) { |
| 1861 |
int count; |
| 1862 |
|
| 1863 |
if (verbose > 3) { |
| 1864 |
verbose_status(NULL, "wait_for_input"); |
| 1865 |
} |
| 1866 |
|
| 1867 |
if (!fgets(buffer, sizeof(buffer), inp)) |
| 1868 |
break; |
| 1869 |
|
| 1870 |
statist_lines++; |
| 1871 |
|
| 1872 |
if (!rq) |
| 1873 |
rq = malloc(sizeof(struct request)); |
| 1874 |
if (!rq) { |
| 1875 |
printf("FATAL ERROR: out of memory for requests\n"); |
| 1876 |
flush_stdout(); |
| 1877 |
do_exit(-1); |
| 1878 |
} |
| 1879 |
memset(rq, 0, sizeof(struct request)); |
| 1880 |
|
| 1881 |
count = sscanf(buffer, "%ld.%ld ; %lld ; %d ; %c", &rq->orig_stamp.tv_sec, &rq->orig_stamp.tv_nsec, &rq->sector, &rq->length, &rq->rwbs); |
| 1882 |
if (count != 5) { |
| 1883 |
printf("ERROR: bad input count=%d, line='%s'\n", count, buffer); |
| 1884 |
flush_stdout(); |
| 1885 |
continue; |
| 1886 |
} |
| 1887 |
|
| 1888 |
// treat backshifts in time (caused by repeated input files) |
| 1889 |
timespec_add(&rq->orig_stamp, ×hift); |
| 1890 |
if (rq->orig_stamp.tv_sec < old_stamp.tv_sec) { |
| 1891 |
struct timespec delta = {}; |
| 1892 |
timespec_diff(&delta, &rq->orig_stamp, &old_stamp); |
| 1893 |
timespec_add(×hift, &delta); |
| 1894 |
timespec_add(&rq->orig_stamp, &delta); |
| 1895 |
printf("INFO: backshift at time=%ld.%09ld s detected (delta=%ld.%09ld s, total timeshift=%ld.%09ld s)\n", |
| 1896 |
rq->orig_stamp.tv_sec, rq->orig_stamp.tv_nsec, |
| 1897 |
delta.tv_sec, delta.tv_nsec, |
| 1898 |
timeshift.tv_sec, timeshift.tv_nsec); |
| 1899 |
flush_stdout(); |
| 1900 |
} |
| 1901 |
memcpy(&old_stamp, &rq->orig_stamp, sizeof(old_stamp)); |
| 1902 |
|
| 1903 |
if (verbose) { |
| 1904 |
verbose_status(rq, "got_input"); |
| 1905 |
} |
| 1906 |
|
| 1907 |
// check replay time window |
| 1908 |
if (rq->orig_stamp.tv_sec < replay_start) { |
| 1909 |
continue; |
| 1910 |
} |
| 1911 |
if (replay_end && rq->orig_stamp.tv_sec >= replay_end) { |
| 1912 |
break; |
| 1913 |
} |
| 1914 |
rq->rwbs = toupper(rq->rwbs); |
| 1915 |
if (rq->rwbs != 'R' && rq->rwbs != 'W') { |
| 1916 |
printf("ERROR: bad character '%c'\n", rq->rwbs); |
| 1917 |
continue; |
| 1918 |
} |
| 1919 |
|
| 1920 |
// compute virtual start time |
| 1921 |
if (!first_call) { |
| 1922 |
first_call++; |
| 1923 |
memcpy(&first_stamp, &rq->orig_stamp, sizeof(first_stamp)); |
| 1924 |
} |
| 1925 |
timespec_diff(&rq->orig_factor_stamp, &first_stamp, &rq->orig_stamp); |
| 1926 |
timespec_multiply(&rq->orig_factor_stamp, time_stretch); |
| 1927 |
|
| 1928 |
// avoid flooding the pipelines too much |
| 1929 |
while (count_submitted > bottleneck || |
| 1930 |
((conflict_mode == 1 || count_pushback > 0) && |
| 1931 |
count_submitted > 1 && |
| 1932 |
delay_distance(&rq->orig_factor_stamp))) { |
| 1933 |
get_answer(); |
| 1934 |
} |
| 1935 |
|
| 1936 |
rq->seqnr = ++statist_total; |
| 1937 |
if (rq->rwbs != 'R') |
| 1938 |
statist_writes++; |
| 1939 |
|
| 1940 |
if (rq->sector + rq->length > max_size) |
| 1941 |
max_size = rq->sector + rq->length; |
| 1942 |
|
| 1943 |
if (rq->sector + rq->length > main_size && main_size) { |
| 1944 |
if (!overflow) { |
| 1945 |
printf("INFO: sector %lld+%d exceeds %lld, turning on wraparound.\n", rq->sector, rq->length, main_size); |
| 1946 |
flush_stdout(); |
| 1947 |
overflow++; |
| 1948 |
} |
| 1949 |
rq->sector %= main_size; |
| 1950 |
if (rq->sector + rq->length > main_size) { // damn, it spans the border |
| 1951 |
printf("WARN: cannot process sector %lld+%d at border %lld, please use a larger device\n", rq->sector, rq->length, main_size); |
| 1952 |
flush_stdout(); |
| 1953 |
continue; |
| 1954 |
} |
| 1955 |
} |
| 1956 |
|
| 1957 |
// add new element |
| 1958 |
execute(rq); |
| 1959 |
rq = NULL; |
| 1960 |
} |
| 1961 |
|
| 1962 |
printf("--------------------------------------\n"); |
| 1963 |
flush_stdout(); |
| 1964 |
|
| 1965 |
while (count_submitted > 0 && get_answer()) { |
| 1966 |
} |
| 1967 |
|
| 1968 |
printf("--------------------------------------\n"); |
| 1969 |
flush_stdout(); |
| 1970 |
|
| 1971 |
// close all pipes => leads to EOF at childs |
| 1972 |
close_all_queues(queue, sub_max, 1, -1); |
| 1973 |
|
| 1974 |
printf("=======================================\n\n"); |
| 1975 |
printf("meta_ops=%lld, total meta_delay=%lu.%09ld, avg=%lf\n", |
| 1976 |
meta_delay_count, |
| 1977 |
meta_delays.tv_sec, |
| 1978 |
meta_delays.tv_nsec, |
| 1979 |
meta_delay_count ? (meta_delays.tv_nsec * ((double)1.0/NANO) + (double)meta_delays.tv_sec) / meta_delay_count : 0); |
| 1980 |
printf("# total input lines : %6d\n", statist_lines); |
| 1981 |
printf("# total requests : %6d\n", statist_total); |
| 1982 |
printf("# completed requests : %6d\n", statist_completed); |
| 1983 |
printf("# write requests : %6d\n", statist_writes); |
| 1984 |
printf("# dropped requests : %6d\n", statist_dropped); |
| 1985 |
printf("# pushback requests : %6d\n", statist_pushback); |
| 1986 |
printf("# ordered requests (waits) : %6d\n", statist_ordered); |
| 1987 |
printf("# verify errors during replay : %6lld\n", verify_errors); |
| 1988 |
printf("conflict_mode : %6d\n", conflict_mode); |
| 1989 |
printf("strong_mode : %6d\n", strong_mode); |
| 1990 |
printf("verify_mode : %6d\n", verify_mode); |
| 1991 |
if (fly_hash.fly_count) |
| 1992 |
printf("fly_count : %6d\n", fly_hash.fly_count); |
| 1993 |
if (count_submitted) |
| 1994 |
printf("count_submitted : %6d\n", count_submitted); |
| 1995 |
if (count_catchup) |
| 1996 |
printf("count_catchup : %6d\n", count_catchup); |
| 1997 |
if (count_pushback) |
| 1998 |
printf("count_pushback : %6d\n", count_pushback); |
| 1999 |
|
| 2000 |
printf("max_submitted : %6d\n", max_submitted); |
| 2001 |
printf("max_pushback : %6d\n", max_pushback); |
| 2002 |
|
| 2003 |
printf("size of device: %12lld blocks (%lld kB)\n", main_size, main_size/2); |
| 2004 |
printf("max block# occurred: %12lld blocks (%lld kB)\n", max_size, max_size/2); |
| 2005 |
printf("wraparound factor: %6.3f\n", (double)max_size / (double)main_size); |
| 2006 |
flush_stdout(); |
| 2007 |
} |
| 2008 |
|
| 2009 |
/////////////////////////////////////////////////////////////////////// |
| 2010 |
|
| 2011 |
// arg parsing |
| 2012 |
// don't use GNU getopt, it's not available everywhere |
| 2013 |
|
| 2014 |
#define ARG_INT -1 |
| 2015 |
#define ARG_FLOAT -2 |
| 2016 |
#define ARG_TIMESPEC -3 |
| 2017 |
|
| 2018 |
#define ARG_ADD -10 |
| 2019 |
#define ARG_SUB -11 |
| 2020 |
|
| 2021 |
struct arg { |
| 2022 |
char *arg_name; |
| 2023 |
char *arg_descr; |
| 2024 |
int arg_const; |
| 2025 |
void *arg_val; |
| 2026 |
}; |
| 2027 |
|
| 2028 |
static |
| 2029 |
const struct arg arg_table[] = { |
| 2030 |
{ |
| 2031 |
.arg_name = "|", |
| 2032 |
.arg_descr = "Influence replay duration:", |
| 2033 |
}, |
| 2034 |
{ |
| 2035 |
.arg_name = "replay-start", |
| 2036 |
.arg_descr = "start offset (in seconds, 0=from_start)", |
| 2037 |
.arg_const = ARG_INT, |
| 2038 |
.arg_val = &replay_start, |
| 2039 |
}, |
| 2040 |
{ |
| 2041 |
.arg_name = "replay-end", |
| 2042 |
.arg_descr = "end offset (in seconds, 0=unlimited)", |
| 2043 |
.arg_const = ARG_INT, |
| 2044 |
.arg_val = &replay_end, |
| 2045 |
}, |
| 2046 |
{ |
| 2047 |
.arg_name = "replay-duration", |
| 2048 |
.arg_descr = "alternatively specify the end offset as delta", |
| 2049 |
.arg_const = ARG_INT, |
| 2050 |
.arg_val = &replay_duration, |
| 2051 |
}, |
| 2052 |
{ |
| 2053 |
.arg_name = "replay-out", |
| 2054 |
.arg_descr = "start offset, used for output (in seconds)", |
| 2055 |
.arg_const = ARG_INT, |
| 2056 |
.arg_val = &replay_out, |
| 2057 |
}, |
| 2058 |
{ |
| 2059 |
.arg_name = "start-grace", |
| 2060 |
.arg_descr = "start after grace period for filling the pipes (in seconds)", |
| 2061 |
.arg_const = ARG_INT, |
| 2062 |
.arg_val = &start_grace.tv_sec, |
| 2063 |
}, |
| 2064 |
|
| 2065 |
|
| 2066 |
{ |
| 2067 |
.arg_name = "|", |
| 2068 |
.arg_descr = "Handling of conflicting IO requests:", |
| 2069 |
}, |
| 2070 |
{ |
| 2071 |
.arg_name = "with-conflicts", |
| 2072 |
.arg_descr = "conflicting writes are ALLOWED (damaged IO)", |
| 2073 |
.arg_const = 0, |
| 2074 |
.arg_val = &conflict_mode, |
| 2075 |
}, |
| 2076 |
{ |
| 2077 |
.arg_name = "with-drop", |
| 2078 |
.arg_descr = "conflicting writes are simply dropped", |
| 2079 |
.arg_const = 1, |
| 2080 |
.arg_val = &conflict_mode, |
| 2081 |
}, |
| 2082 |
{ |
| 2083 |
.arg_name = "with-partial", |
| 2084 |
.arg_descr = "partial ordering by pushing back conflicts (default)", |
| 2085 |
.arg_const = 2, |
| 2086 |
.arg_val = &conflict_mode, |
| 2087 |
}, |
| 2088 |
{ |
| 2089 |
.arg_name = "with-ordering", |
| 2090 |
.arg_descr = "enforce total order in case of conflicts", |
| 2091 |
.arg_const = 3, |
| 2092 |
.arg_val = &conflict_mode, |
| 2093 |
}, |
| 2094 |
{ |
| 2095 |
.arg_name = "strong", |
| 2096 |
.arg_descr = "mode between 0 and 2, see docs (default=1)", |
| 2097 |
.arg_const = ARG_INT, |
| 2098 |
.arg_val = &strong_mode, |
| 2099 |
}, |
| 2100 |
|
| 2101 |
|
| 2102 |
|
| 2103 |
{ |
| 2104 |
.arg_name = "|", |
| 2105 |
.arg_descr = "Replay parameters:", |
| 2106 |
}, |
| 2107 |
{ |
| 2108 |
.arg_name = "threads", |
| 2109 |
.arg_descr = "parallelism (default=" STRINGIFY(DEFAULT_THREADS) ")", |
| 2110 |
.arg_const = ARG_INT, |
| 2111 |
.arg_val = &total_max, |
| 2112 |
}, |
| 2113 |
|
| 2114 |
|
| 2115 |
|
| 2116 |
{ |
| 2117 |
.arg_name = "|", |
| 2118 |
.arg_descr = "Verification modes:", |
| 2119 |
}, |
| 2120 |
{ |
| 2121 |
.arg_name = "no-overhead", |
| 2122 |
.arg_descr = "verify is OFF (default)", |
| 2123 |
.arg_const = 0, |
| 2124 |
.arg_val = &verify_mode, |
| 2125 |
}, |
| 2126 |
{ |
| 2127 |
.arg_name = "with-verify", |
| 2128 |
.arg_descr = "verify on reads", |
| 2129 |
.arg_const = 1, |
| 2130 |
.arg_val = &verify_mode, |
| 2131 |
}, |
| 2132 |
{ |
| 2133 |
.arg_name = "with-final-verify", |
| 2134 |
.arg_descr = "additional verify pass at the end", |
| 2135 |
.arg_const = 2, |
| 2136 |
.arg_val = &verify_mode, |
| 2137 |
}, |
| 2138 |
{ |
| 2139 |
.arg_name = "with-paranoia", |
| 2140 |
.arg_descr = "re-read after each write (destroys performance)", |
| 2141 |
.arg_const = 3, |
| 2142 |
.arg_val = &verify_mode, |
| 2143 |
}, |
| 2144 |
|
| 2145 |
|
| 2146 |
|
| 2147 |
{ |
| 2148 |
.arg_name = "|", |
| 2149 |
.arg_descr = "Convenience:", |
| 2150 |
}, |
| 2151 |
{ |
| 2152 |
.arg_name = "verbose", |
| 2153 |
.arg_descr = "increase verbosity, show additional INFO: output", |
| 2154 |
.arg_const = ARG_ADD, |
| 2155 |
.arg_val = &verbose, |
| 2156 |
}, |
| 2157 |
|
| 2158 |
{ |
| 2159 |
.arg_name = "|", |
| 2160 |
.arg_descr = "Expert options (DANGEROUS):", |
| 2161 |
}, |
| 2162 |
{ |
| 2163 |
.arg_name = "o-direct", |
| 2164 |
.arg_descr = "use O_DIRECT (default)", |
| 2165 |
.arg_const = 1, |
| 2166 |
.arg_val = &use_o_direct, |
| 2167 |
}, |
| 2168 |
{ |
| 2169 |
.arg_name = "no-o-direct", |
| 2170 |
.arg_descr = "don't use O_DIRECT, deliver FAKE results", |
| 2171 |
.arg_const = 0, |
| 2172 |
.arg_val = &use_o_direct, |
| 2173 |
}, |
| 2174 |
{ |
| 2175 |
.arg_name = "o-sync", |
| 2176 |
.arg_descr = "use O_SYNC", |
| 2177 |
.arg_const = 1, |
| 2178 |
.arg_val = &use_o_sync, |
| 2179 |
}, |
| 2180 |
{ |
| 2181 |
.arg_name = "no-o-sync", |
| 2182 |
.arg_descr = "don't use O_SYNC (default)", |
| 2183 |
.arg_const = 0, |
| 2184 |
.arg_val = &use_o_sync, |
| 2185 |
}, |
| 2186 |
{ |
| 2187 |
.arg_name = "dry-run", |
| 2188 |
.arg_descr = "don't actually do IO, measure internal overhead", |
| 2189 |
.arg_const = 1, |
| 2190 |
.arg_val = &dry_run, |
| 2191 |
}, |
| 2192 |
{ |
| 2193 |
.arg_name = "fake-io", |
| 2194 |
.arg_descr = "omit lseek() and tags, even less internal overhead", |
| 2195 |
.arg_const = 1, |
| 2196 |
.arg_val = &fake_io, |
| 2197 |
}, |
| 2198 |
#ifdef HAVE_DECL_NANOSLEEP |
| 2199 |
{ |
| 2200 |
.arg_name = "simulate-io", |
| 2201 |
.arg_descr = "delay value for IO simulation (timespec <sec>.<nsec>)", |
| 2202 |
.arg_const = ARG_TIMESPEC, |
| 2203 |
.arg_val = &simulate_io, |
| 2204 |
}, |
| 2205 |
#endif |
| 2206 |
{ |
| 2207 |
.arg_name = "ahead-limit", |
| 2208 |
.arg_descr = "limit pipe fillahead (realtime <sec>.<nsec>)", |
| 2209 |
.arg_const = ARG_TIMESPEC, |
| 2210 |
.arg_val = &ahead_limit, |
| 2211 |
}, |
| 2212 |
{ |
| 2213 |
.arg_name = "fan-out", |
| 2214 |
.arg_descr = "only for kernel hackers (default=" STRINGIFY(DEFAULT_FAN_OUT) ")", |
| 2215 |
.arg_const = ARG_INT, |
| 2216 |
.arg_val = &fan_out, |
| 2217 |
}, |
| 2218 |
{ |
| 2219 |
.arg_name = "no-dispatcher", |
| 2220 |
.arg_descr = "only for kernel hackers", |
| 2221 |
.arg_const = 0, |
| 2222 |
.arg_val = &fork_dispatcher, |
| 2223 |
}, |
| 2224 |
{ |
| 2225 |
.arg_name = "bottleneck", |
| 2226 |
.arg_descr = "max #requests on dispatch", |
| 2227 |
.arg_const = ARG_INT, |
| 2228 |
.arg_val = &bottleneck, |
| 2229 |
}, |
| 2230 |
{ |
| 2231 |
.arg_name = "speedup", |
| 2232 |
.arg_descr = "speedup / slowdown by REAL factor (default=" STRINGIFY(DEFAULT_SPEEDUP) ")", |
| 2233 |
.arg_const = ARG_FLOAT, |
| 2234 |
.arg_val = &time_factor, |
| 2235 |
}, |
| 2236 |
{ |
| 2237 |
.arg_name = "mmap-mode", |
| 2238 |
.arg_descr = "use mmap() instead of read() / write() [NYI]", |
| 2239 |
.arg_const = 1, |
| 2240 |
.arg_val = &mmap_mode, |
| 2241 |
}, |
| 2242 |
{} |
| 2243 |
}; |
| 2244 |
|
| 2245 |
static |
| 2246 |
void usage(void) |
| 2247 |
{ |
| 2248 |
const struct arg *tmp; |
| 2249 |
|
| 2250 |
printf("usage: blkreplay {--<option>[=<value>]} <device>\n"); |
| 2251 |
|
| 2252 |
for (tmp = arg_table; tmp->arg_name; tmp++) { |
| 2253 |
int len; |
| 2254 |
|
| 2255 |
if (tmp->arg_name[0] == '|') { |
| 2256 |
printf("\n%s\n", tmp->arg_descr); |
| 2257 |
continue; |
| 2258 |
} |
| 2259 |
|
| 2260 |
len = strlen(tmp->arg_name); |
| 2261 |
printf(" --%s", tmp->arg_name); |
| 2262 |
if (tmp->arg_const < 0) { |
| 2263 |
printf("=<val>"); |
| 2264 |
len += 6; |
| 2265 |
} |
| 2266 |
while (len++ < 21) { |
| 2267 |
printf(" "); |
| 2268 |
} |
| 2269 |
printf(" %s\n", tmp->arg_descr); |
| 2270 |
} |
| 2271 |
do_exit(-1); |
| 2272 |
} |
| 2273 |
|
| 2274 |
static |
| 2275 |
void parse_args(int argc, char *argv[]) |
| 2276 |
{ |
| 2277 |
int i; |
| 2278 |
|
| 2279 |
for (i = 1; i < argc; i++) { |
| 2280 |
char *this = argv[i]; |
| 2281 |
const struct arg *tmp; |
| 2282 |
int count = 1; |
| 2283 |
|
| 2284 |
if (this[0] != '-') { |
| 2285 |
if (main_name) { |
| 2286 |
printf("<device> must not be set twice\n"); |
| 2287 |
usage(); |
| 2288 |
} |
| 2289 |
main_name = this; |
| 2290 |
continue; |
| 2291 |
} |
| 2292 |
this++; |
| 2293 |
if (this[0] != '-') { |
| 2294 |
printf("only double options starting with -- are allowed\n"); |
| 2295 |
usage(); |
| 2296 |
} |
| 2297 |
this++; |
| 2298 |
for (tmp = arg_table; tmp->arg_name; tmp++) { |
| 2299 |
int len = strlen(tmp->arg_name); |
| 2300 |
if (!strncmp(tmp->arg_name, this, len)) { |
| 2301 |
this += len; |
| 2302 |
break; |
| 2303 |
} |
| 2304 |
} |
| 2305 |
|
| 2306 |
if (!tmp->arg_name) { |
| 2307 |
printf("unknown option '%s'\n", this); |
| 2308 |
usage(); |
| 2309 |
} |
| 2310 |
|
| 2311 |
if (tmp->arg_const >= 0) { |
| 2312 |
*(int*)tmp->arg_val = tmp->arg_const; |
| 2313 |
continue; |
| 2314 |
} |
| 2315 |
|
| 2316 |
while (*this == ' ') |
| 2317 |
this++; |
| 2318 |
if (*this == '=') |
| 2319 |
this++; |
| 2320 |
|
| 2321 |
switch (tmp->arg_const) { |
| 2322 |
case ARG_INT: |
| 2323 |
count = sscanf(this, "%d", (int*)tmp->arg_val); |
| 2324 |
break; |
| 2325 |
case ARG_FLOAT: |
| 2326 |
*(FLOAT*)tmp->arg_val = atof(this); |
| 2327 |
break; |
| 2328 |
case ARG_TIMESPEC: |
| 2329 |
count = sscanf(this, "%lu.%lu", |
| 2330 |
&((struct timespec*)tmp->arg_val)->tv_sec, |
| 2331 |
&((struct timespec*)tmp->arg_val)->tv_nsec); |
| 2332 |
if (count == 2) |
| 2333 |
count = 1; |
| 2334 |
break; |
| 2335 |
case ARG_ADD: |
| 2336 |
case ARG_SUB: |
| 2337 |
count = sscanf(this, "%d", (int*)tmp->arg_val); |
| 2338 |
if (!count) { |
| 2339 |
if (tmp->arg_const == ARG_ADD) |
| 2340 |
(*(int*)tmp->arg_val)++; |
| 2341 |
else |
| 2342 |
(*(int*)tmp->arg_val)--; |
| 2343 |
} |
| 2344 |
count = 1; |
| 2345 |
break; |
| 2346 |
default: ; |
| 2347 |
} |
| 2348 |
if (count != 1) { |
| 2349 |
printf("cannot parse <val> '%s'\n", this); |
| 2350 |
usage(); |
| 2351 |
} |
| 2352 |
} |
| 2353 |
|
| 2354 |
if (!main_name) { |
| 2355 |
printf("you forgot to provide a <device>\n"); |
| 2356 |
usage(); |
| 2357 |
} |
| 2358 |
} |
| 2359 |
|
| 2360 |
void print_fake(void) |
| 2361 |
{ |
| 2362 |
printf("INFO: use_o_direct=%d\n", use_o_direct); |
| 2363 |
printf("INFO: use_o_sync=%d\n", use_o_sync); |
| 2364 |
printf("INFO: ahead_limit=%lu.%09lu\n", ahead_limit.tv_sec, ahead_limit.tv_nsec); |
| 2365 |
printf("INFO: simulate_io=%lu.%09lu\n", simulate_io.tv_sec, simulate_io.tv_nsec); |
| 2366 |
printf("INFO: dry_run=%d\n", dry_run); |
| 2367 |
|
| 2368 |
if (dry_run || !use_o_direct) { |
| 2369 |
printf("\n" |
| 2370 |
"INFO: measurement results are thus FAKE results!!!\n" |
| 2371 |
"\n" |
| 2372 |
); |
| 2373 |
} |
| 2374 |
flush_stdout(); |
| 2375 |
} |
| 2376 |
|
| 2377 |
int main(int argc, char *argv[]) |
| 2378 |
{ |
| 2379 |
int max_threads; |
| 2380 |
time_t now = time(NULL); |
| 2381 |
/* avoid zombies */ |
| 2382 |
signal(SIGCHLD, SIG_IGN); |
| 2383 |
|
| 2384 |
/* argument parsing */ |
| 2385 |
parse_args(argc, argv); |
| 2386 |
|
| 2387 |
if (verify_mode >= 2) |
| 2388 |
final_verify_mode++; |
| 2389 |
|
| 2390 |
max_threads = MAX_THREADS; |
| 2391 |
if (conflict_mode == 2) |
| 2392 |
max_threads /= 2; |
| 2393 |
if (total_max > max_threads) |
| 2394 |
total_max = max_threads; |
| 2395 |
if (total_max < 1) |
| 2396 |
total_max = 1; |
| 2397 |
table_max = total_max; |
| 2398 |
if (conflict_mode == 2) |
| 2399 |
table_max *= 2; |
| 2400 |
|
| 2401 |
if (fan_out < 2) |
| 2402 |
fan_out = 2; |
| 2403 |
if (fan_out > QUEUES) |
| 2404 |
fan_out = QUEUES; |
| 2405 |
|
| 2406 |
if (bottleneck <= 0) |
| 2407 |
bottleneck = total_max * FILL_FACTOR; |
| 2408 |
|
| 2409 |
if (replay_duration > 0) |
| 2410 |
replay_end = replay_start + replay_duration; |
| 2411 |
|
| 2412 |
if (fake_io) |
| 2413 |
dry_run = 1; |
| 2414 |
if (simulate_io.tv_sec || simulate_io.tv_nsec) |
| 2415 |
dry_run = 1; |
| 2416 |
|
| 2417 |
if (ahead_limit.tv_sec <= 0 && ahead_limit.tv_nsec <= 0) |
| 2418 |
ahead_limit.tv_sec = 1; |
| 2419 |
|
| 2420 |
if (time_factor != 0.0) { |
| 2421 |
time_stretch = 1.0 / time_factor; |
| 2422 |
|
| 2423 |
/* Notice: the following GNU all-permissive license applies |
| 2424 |
* to the generated DATA file only, and does not change |
| 2425 |
* the GPL of this program. |
| 2426 |
*/ |
| 2427 |
|
| 2428 |
printf( |
| 2429 |
"Copyright Thomas Schoebel-Theuer / 1&1 Internet AG\n" |
| 2430 |
"\n" |
| 2431 |
"This file was automatically generated by blkreplay\n" |
| 2432 |
"\n" |
| 2433 |
"Copying and distribution of this file, with or without modification,\n" |
| 2434 |
"are permitted in any medium without royalty provided the copyright\n" |
| 2435 |
"notice and this notice are preserved. This file is offered as-is,\n" |
| 2436 |
"without any warranty.\n" |
| 2437 |
"\n" |
| 2438 |
"\n" |
| 2439 |
"blkreplay on %s started at %s\n" |
| 2440 |
"\n", |
| 2441 |
main_name, |
| 2442 |
ctime(&now)); |
| 2443 |
flush_stdout(); |
| 2444 |
|
| 2445 |
print_fake(); |
| 2446 |
|
| 2447 |
parse(stdin); |
| 2448 |
|
| 2449 |
printf("blkreplay on %s ended at %s\n", |
| 2450 |
main_name, |
| 2451 |
ctime(&now)); |
| 2452 |
flush_stdout(); |
| 2453 |
} |
| 2454 |
|
| 2455 |
// verify the end result |
| 2456 |
if (final_verify_mode) { |
| 2457 |
printf("-------------------------------\n"); |
| 2458 |
printf("verifying the end result.......\n"); |
| 2459 |
flush_stdout(); |
| 2460 |
verify_open(1); |
| 2461 |
main_open(1); |
| 2462 |
sleep(3); |
| 2463 |
check_all_tags(); |
| 2464 |
} |
| 2465 |
|
| 2466 |
print_fake(); |
| 2467 |
|
| 2468 |
do_exit(0); |
| 2469 |
return 0; |
| 2470 |
} |