forked from laurenz/pgreplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplayfile.c
437 lines (387 loc) · 8.93 KB
/
replayfile.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include "pgreplay.h"
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#ifdef WINDOWS
# include <io.h>
# include <winsock.h>
# define FILE_MODE S_IRUSR | S_IWUSR
#else
# ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
# endif
# define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
/* input or output file */
static int filed=0;
/* functions to convert 64-bit integers between host and network byte order */
#ifndef htonll
# ifdef WORDS_BIGENDIAN
# define htonll(x) (x)
# define ntohll(x) (x)
# else
# define htonll(x) ((((uint64_t)htonl(x)) << 32) + htonl(x >> 32))
# define ntohll(x) ((((uint64_t)ntohl(x)) << 32) + ntohl(x >> 32))
# endif
#endif
/* this length indicates a null value */
#define NULL_VALUE 0x80000000
/* wrapper functions for read and write */
static int do_write(const void * const buf, size_t count) {
int rc = write(filed, buf, count);
if (-1 == rc) {
perror("Error writing to output file");
return 0;
} else if (count != rc) {
fprintf(stderr, "Error: not all bytes written to output file\n");
return 0;
}
return 1;
}
static int do_read(void *buf, size_t count, int *eof_indicator) {
int rc = read(filed, buf, count);
if (eof_indicator) {
*eof_indicator = 0;
}
if (-1 == rc) {
perror("Error reading from input file");
return 0;
} else if (eof_indicator && (0 == rc)) {
*eof_indicator = 1;
} else if (count != rc) {
fprintf(stderr, "Error: unexpected end of file on input file\n");
return 0;
}
return 1;
}
/* write a string to the output file */
static int write_string(char const * const s) {
uint32_t u32, len;
/* write length + NULL indicator (4 byte) */
if (NULL == s) {
len = NULL_VALUE;
} else {
len = strlen(s);
}
u32 = htonl(len);
if (! do_write(&u32, 4)) {
return 0;
} else if (NULL != s) {
/* write string */
if (! do_write(s, len)) {
return 0;
}
}
return 1;
}
/* malloc and read a string from the input file */
static int read_string(char ** const s) {
uint32_t u32, len;
/* read length (4 byte) */
if (! do_read(&u32, 4, NULL)) {
return 0;
}
len = ntohl(u32);
if (NULL_VALUE == len) {
*s = NULL;
} else {
/* allocate the string */
if (! (*s = malloc(len + 1))) {
fprintf(stderr, "Cannot allocate %d bytes of memory\n", len + 1);
return 0;
} else {
/* read string */
if (! do_read(*s, len, NULL)) {
return 0;
}
(*s)[len] = '\0';
}
}
return 1;
}
int file_provider_init(const char *infile, int cvs, const char *begin_time, const char *end_time, const char *db_only, const char *usr_only) {
int rc = 1;
debug(3, "Entering file_provider_init%s\n", "");
if (NULL == infile) {
filed = 0;
#ifdef WINDOWS
setmode(filed, O_BINARY);
#endif
} else {
if (-1 == (filed = open(infile, O_RDONLY
#ifdef WINDOWS
| O_BINARY
#endif
))) {
perror("Error opening input file:");
rc = 0;
}
}
debug(3, "Leaving file_provider_init%s\n", "");
return rc;
}
void file_provider_finish() {
debug(3, "Entering file_provider_finish%s\n", "");
if (0 != filed) {
if (close(filed)) {
perror("Error closing input file:");
}
}
debug(3, "Leaving file_provider_finish%s\n", "");
}
replay_item * file_provider() {
replay_item *r = NULL;
uint16_t u16;
uint32_t u32;
uint64_t u64, session_id = 0;
struct timeval tv;
replay_type type = -1;
int ok = 1, i = 0, eof;
unsigned long count;
char *user, *database, *statement, *name, **values, nl;
debug(3, "Entering file_provider%s\n", "");
/* read timestamp (8 byte) */
if (! do_read(&u32, 4, &eof)) {
ok = 0;
} else {
/* handle expected end-of-file condition */
if (eof) {
return end_item;
}
tv.tv_sec = ntohl(u32);
if (! do_read(&u32, 4, NULL)) {
ok = 0;
} else {
tv.tv_usec = ntohl(u32);
}
}
/* read session_id (8 byte) */
if (ok && do_read(&u64, 8, NULL)) {
session_id = ntohll(u64);
} else {
ok = 0;
}
/* read type (1 byte) */
if (ok) {
u16 = 0;
if (! do_read((char *)(&u16) + 1, 1, NULL)) {
ok = 0;
} else {
type = ntohs(u16);
if ((type < pg_connect) || (type > pg_cancel)) {
fprintf(stderr, "Error: unknown type %u encountered\n", type);
ok = 0;
}
}
}
/* read type specific stuff */
if (ok) {
switch (type) {
case pg_connect:
if (read_string(&user)) {
if (read_string(&database)) {
r = replay_create_connect(&tv, session_id, user, database);
free(database);
}
free(user);
}
break;
case pg_disconnect:
r = replay_create_disconnect(&tv, session_id);
break;
case pg_execute:
if (read_string(&statement)) {
r = replay_create_execute(&tv, session_id, statement);
free(statement);
}
break;
case pg_prepare:
if (read_string(&statement)) {
if (read_string(&name)) {
r = replay_create_prepare(&tv, session_id, name, statement);
free(name);
}
free(statement);
}
break;
case pg_exec_prepared:
/* read statement name */
if (read_string(&name)) {
/* number of bind arguments (2 byte) */
if (do_read(&u16, 2, NULL)) {
count = ntohs(u16);
if (NULL == (values = calloc(count, sizeof(char *)))) {
fprintf(stderr, "Cannot allocate %lu bytes of memory\n", count * sizeof(char *));
} else {
/* read bind values */
while (i < count) {
if (read_string(values + i)) {
++i;
} else {
break;
}
}
if (i == count) {
r = replay_create_exec_prepared(&tv, session_id, name, count, values);
}
while (--i >= 0) {
if (values[i]) {
free(values[i]);
}
}
free(values);
}
}
free(name);
}
break;
case pg_cancel:
r = replay_create_cancel(&tv, session_id);
break;
}
}
/* read new-line at the end of the record */
if (r && do_read(&nl, 1, NULL) && ('\n' != nl)) {
fprintf(stderr, "Error: missing new-line at end of line\n");
if (r) {
replay_free(r);
r = NULL;
}
}
if (r && (1 <= debug_level) && (end_item != r)) {
replay_print_debug(r);
}
debug(3, "Leaving file_provider%s\n", "");
return r;
}
int file_consumer_init(const char *outfile, const char *host, int port, const char *passwd, double factor) {
debug(3, "Entering file_consumer_init%s\n", "");
if ((NULL == outfile) || ('\0' == outfile[0])
|| (('-' == outfile[0]) && ('\0' == outfile[1]))) {
filed = 1;
#ifdef WINDOWS
/* set stdout to binary mode */
setmode(filed, O_BINARY);
#endif
} else {
if (-1 == (filed = open(outfile, O_WRONLY | O_CREAT | O_TRUNC
#ifdef WINDOWS
| O_BINARY
#endif
, FILE_MODE))) {
perror("Error opening output file:");
return 0;
}
}
debug(3, "Leaving file_consumer_init%s\n", "");
return 1;
}
void file_consumer_finish(int dry_run) {
debug(3, "Entering file_consumer_finish%s\n", "");
if (1 != filed) {
if (close(filed)) {
perror("Error closing output file:");
}
}
debug(3, "Leaving file_consumer_finish%s\n", "");
}
int file_consumer(replay_item *item) {
const struct timeval *tv = replay_get_time(item);
uint16_t count;
const replay_type type = replay_get_type(item);
uint16_t u16, i;
uint32_t u32;
uint64_t u64;
int rc = 1;
const char * const *values;
debug(3, "Entering file_consumer%s\n", "");
/* write timestamp (8 byte) */
u32 = htonl(tv->tv_sec);
if (! do_write(&u32, 4)) {
rc = -1;
} else {
u32 = htonl(tv->tv_usec);
if (! do_write(&u32, 4)) {
rc = -1;
}
}
/* write session_id (8 byte) */
if (1 == rc) {
u64 = htonll(replay_get_session_id(item));
if (! do_write(&u64, 8)) {
rc = -1;
}
}
/* write type (1 byte) */
if (1 == rc) {
u16 = htons((uint16_t) type);
if (! do_write((char *)(&u16) + 1, 1)) {
rc = -1;
}
}
/* write type specific stuff */
if (1 == rc) {
switch (type) {
case pg_connect:
if (! write_string(replay_get_user(item))) {
rc = -1;
} else if (! write_string(replay_get_database(item))) {
rc = -1;
}
break;
case pg_disconnect:
break;
case pg_execute:
if (! write_string(replay_get_statement(item))) {
rc = -1;
}
break;
case pg_prepare:
if (! write_string(replay_get_statement(item))) {
rc = -1;
} else if (! write_string(replay_get_name(item))) {
rc = -1;
}
break;
case pg_exec_prepared:
count = replay_get_valuecount(item);
/* write statement name */
if (! write_string(replay_get_name(item))) {
rc = -1;
} else {
/* write count (2 byte) */
u16 = htons(count);
if (! do_write(&u16, 2)) {
rc = -1;
} else {
/* write values */
values = replay_get_values(item);
for (i=0; i<count; ++i) {
if (! write_string(values[i])) {
rc = -1;
break;
}
}
}
}
break;
case pg_cancel:
break;
}
}
/* write new-line (1 byte) */
if (1 == rc) {
if (! do_write("\n", 1)) {
rc = -1;
}
}
replay_free(item);
debug(3, "Leaving file_consumer%s\n", "");
return rc;
}