-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathlibutils.h
More file actions
117 lines (89 loc) · 3.04 KB
/
libutils.h
File metadata and controls
117 lines (89 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#ifndef HEADER_LIBUTILS_H
#define HEADER_LIBUTILS_H
#include <stdint.h>
#include <argp.h> /* For struct argp_state. */
#include <time.h> /* For struct timespec. */
#include <sys/time.h> /* For struct timeval. */
#define SECTOR_SIZE (512)
#define SECTOR_ORDER (9)
#define UNUSED(x) ((void)x)
#define DIM(x) (sizeof(x) / sizeof((x)[0]))
typedef void (*progress_cb)(unsigned int indent, const char *format, ...);
void printf_cb(unsigned int indent, const char *format, ...);
void printf_flush_cb(unsigned int indent, const char *format, ...);
void dummy_cb(unsigned int indent, const char *format, ...);
int ilog2(uint64_t x);
/* Least power of 2 greater than or equal to x. */
uint64_t clp2(uint64_t x);
static inline int ceiling_log2(uint64_t x)
{
return ilog2(clp2(x));
}
const char *adjust_unit(double *ptr_bytes);
#define TIME_STR_SIZE 128
int nsec_to_str(uint64_t nsec, char *str);
/*
* The functions align_head() and align_mem() are used to align pointers.
*
* The following example allocates two block on stack and makes sure that
* the blocks are aligned with the block size.
*
* // The number 2 below means two blocks.
* char stack[align_head(block_order) + (2 << block_order)];
* char *stamp_blk, *probe_blk;
* stamp_blk = align_mem(stack, block_order);
* probe_blk = stamp_blk + block_size;
*/
static inline int align_head(int order)
{
return (1 << order) - 1;
}
void *align_mem2(void *p, int order, int *shift);
static inline void *align_mem(void *p, int order)
{
int shift;
return align_mem2(p, order, &shift);
}
void print_header(FILE *f, const char *name);
long long arg_to_ll_bytes(const struct argp_state *state, const char *arg);
/* Dependent on the byte order of the processor (i.e. endianness). */
void fill_buffer_with_block(void *buf, int block_order, uint64_t offset,
uint64_t salt);
enum block_state {
bs_unknown,
bs_good,
bs_bad,
bs_changed,
bs_overwritten,
};
const char *block_state_to_str(enum block_state state);
struct block_stats {
uint64_t ok;
uint64_t bad;
uint64_t changed;
uint64_t overwritten;
};
/* Dependent on the byte order of the processor (i.e. endianness). */
enum block_state validate_buffer_with_block(const void *buf, int block_order,
uint64_t expected_offset, uint64_t *pfound_offset, uint64_t salt);
enum block_state validate_block_update_stats(const void *buf, int block_order,
uint64_t expected_offset, uint64_t *pfound_offset, uint64_t salt,
struct block_stats *stats);
static inline uint64_t diff_timeval_us(const struct timeval *t1,
const struct timeval *t2)
{
return (t2->tv_sec - t1->tv_sec) * 1000000ULL +
t2->tv_usec - t1->tv_usec;
}
static inline uint64_t diff_timespec_ns(const struct timespec *t1,
const struct timespec *t2)
{
return (t2->tv_sec - t1->tv_sec) * 1000000000ULL +
t2->tv_nsec - t1->tv_nsec;
}
void print_stats(const struct block_stats *stats, int block_size,
const char *unit_name);
void report_io_speed(unsigned int indent, progress_cb cb, const char *prefix,
uint64_t blocks, const char *block_unit, uint64_t time_ns,
int block_order);
#endif /* HEADER_LIBUTILS_H */