forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bdb_thd_io.c
190 lines (153 loc) · 3.88 KB
/
bdb_thd_io.c
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
Copyright 2015 Bloomberg Finance L.P.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <epochlib.h>
#include <build/db.h>
#include <logmsg.h>
static void (*io_start)() = 0;
static void (*io_cmplt)() = 0;
/* gather some data, reset periodically
same variable for read/pread and write/pwrite
*/
static int long_reads = 0;
static int norm_reads = 0;
static int long_writes = 0;
static int norm_writes = 0;
static int bdb_pread(int fd, void *buf, size_t nbytes, off_t offset)
{
int rc;
int t1, t2;
int errno_sav;
io_start();
t1 = comdb2_time_epochms();
rc = pread(fd, buf, nbytes, offset);
errno_sav = errno;
t2 = comdb2_time_epochms();
if ((t2 - t1) > 2000) {
logmsg(LOGMSG_WARN, "LONG PREAD: %d ms\n", t2 - t1);
long_reads++;
} else {
norm_reads++;
}
io_cmplt();
errno = errno_sav;
return rc;
}
static ssize_t bdb_read(int fd, void *buf, size_t nbytes)
{
int rc;
int t1, t2;
int errno_sav;
io_start();
t1 = comdb2_time_epochms();
rc = read(fd, buf, nbytes);
errno_sav = errno;
t2 = comdb2_time_epochms();
if ((t2 - t1) > 2000) {
logmsg(LOGMSG_WARN, "LONG READ: %d ms\n", t2 - t1);
long_reads++;
} else {
norm_reads++;
}
io_cmplt();
errno = errno_sav;
return rc;
}
static int bdb_fsync(int fd)
{
int rc;
int t1, t2;
int errno_sav;
/* io_start(); */
t1 = comdb2_time_epochms();
rc = fsync(fd);
errno_sav = errno;
t2 = comdb2_time_epochms();
if ((t2 - t1) > 2000)
logmsg(LOGMSG_WARN, "LONG FSYNC: %d ms\n", t2 - t1);
/* io_cmplt(); */
errno = errno_sav;
return rc;
}
static int bdb_pwrite(int fd, const void *buf, size_t nbytes, off_t offset)
{
int rc;
int t1, t2;
int errno_sav;
io_start();
t1 = comdb2_time_epochms();
rc = pwrite(fd, buf, nbytes, offset);
errno_sav = errno;
t2 = comdb2_time_epochms();
if ((t2 - t1) > 2000) {
logmsg(LOGMSG_WARN, "LONG PWRITE: %d ms\n", t2 - t1);
long_writes++;
} else {
norm_writes++;
}
io_cmplt();
errno = errno_sav;
return rc;
}
static ssize_t bdb_write(int fd, const void *buf, size_t nbytes)
{
int rc;
int t1, t2;
int errno_sav;
io_start();
t1 = comdb2_time_epochms();
rc = write(fd, buf, nbytes);
errno_sav = errno;
t2 = comdb2_time_epochms();
if ((t2 - t1) > 2000) {
logmsg(LOGMSG_WARN, "LONG WRITE: %d ms\n", t2 - t1);
long_writes++;
} else {
norm_writes++;
}
io_cmplt();
errno = errno_sav;
return rc;
}
void bdb_set_io_control(void (*start)(), void (*cmplt)())
{
logmsg(LOGMSG_DEBUG, "IO CONTROL disabled\n");
if (1)
return;
#if 0
io_start=start;
io_cmplt=cmplt;
if
(
db_env_set_func_read(bdb_read) ||
db_env_set_func_fsync(bdb_fsync) ||
db_env_set_func_write(bdb_write)
)
{
printf("**FAILED db_env_set_func_pread RC\n");
abort();
}
printf("SET UP THD IO CONTROL\n");
#endif
}
void bdb_get_iostats(int *n_reads, int *l_reads, int *n_writes, int *l_writes)
{
*n_reads = norm_reads;
*l_reads = long_reads;
*n_writes = norm_writes;
*l_writes = long_writes;
norm_reads = long_reads = norm_writes = long_writes = 0;
}