Dumpcap: Set a bigger IO buffer (64KiB).

Set a bigger IO buffer to avoid syscall overhead.
See https://github.com/the-tcpdump-group/libpcap/issues/792

Change-Id: If370da5ab2b70a9d0c925dd7c4c5c135c675c3f6
Reviewed-on: https://code.wireshark.org/review/31326
Petri-Dish: Anders Broman <a.broman58@gmail.com>
Tested-by: Petri Dish Buildbot
Reviewed-by: Guy Harris <guy@alum.mit.edu>
Reviewed-by: Anders Broman <a.broman58@gmail.com>
This commit is contained in:
Anders Broman 2019-01-03 15:51:56 +01:00 committed by Anders Broman
parent 101fed9420
commit a55111610a
3 changed files with 62 additions and 10 deletions

View File

@ -306,6 +306,7 @@ typedef struct _loop_data {
/* output file(s) */
FILE *pdh;
int save_file_fd;
char *io_buffer; /**< Our IO buffer if we increase the size from the standard size */
guint64 bytes_written; /**< Bytes written for the current file. */
/* autostop conditions */
int packets_written; /**< Packets written for the current file. */
@ -3189,6 +3190,21 @@ capture_loop_init_output(capture_options *capture_opts, loop_data *ld, char *err
ld->pdh = ws_fdopen(ld->save_file_fd, "wb");
if (ld->pdh == NULL) {
err = errno;
} else {
size_t buffsize = IO_BUF_SIZE;
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
ws_statb64 statb;
if (ws_fstat64(ld->save_file_fd, &statb) == 0) {
if (statb.st_blksize > IO_BUF_SIZE) {
buffsize = statb.st_blksize;
}
}
#endif
/* Increase the size of the IO buffer */
ld->io_buffer = (char *)g_malloc(buffsize);
setvbuf(ld->pdh, ld->io_buffer, _IOFBF, buffsize);
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_init_output: buffsize %zu", buffsize);
}
}
if (ld->pdh) {
@ -3208,6 +3224,8 @@ capture_loop_init_output(capture_options *capture_opts, loop_data *ld, char *err
if (!successful) {
fclose(ld->pdh);
ld->pdh = NULL;
g_free(ld->io_buffer);
ld->io_buffer = NULL;
}
}
@ -3244,6 +3262,7 @@ capture_loop_close_output(capture_options *capture_opts, loop_data *ld, int *err
unsigned int i;
capture_src *pcap_src;
guint64 end_time = create_timestamp();
gboolean success;
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_close_output");
@ -3280,10 +3299,13 @@ capture_loop_close_output(capture_options *capture_opts, loop_data *ld, int *err
if (err_close != NULL) {
*err_close = errno;
}
return (FALSE);
success = FALSE;
} else {
return (TRUE);
success = TRUE;
}
g_free(ld->io_buffer);
ld->io_buffer = NULL;
return success;
}
}
@ -3722,6 +3744,8 @@ do_file_switch_or_stop(capture_options *capture_opts)
fclose(global_ld.pdh);
global_ld.pdh = NULL;
global_ld.go = FALSE;
g_free(global_ld.io_buffer);
global_ld.io_buffer = NULL;
return FALSE;
}
if (global_ld.file_duration_timer) {
@ -3846,6 +3870,7 @@ capture_loop_start(capture_options *capture_opts, gboolean *stats_known, struct
global_ld.err = 0; /* no error seen yet */
global_ld.pdh = NULL;
global_ld.save_file_fd = -1;
global_ld.io_buffer = NULL;
global_ld.file_count = 0;
global_ld.file_duration_timer = NULL;
global_ld.next_interval_time = 0;

View File

@ -50,18 +50,19 @@ typedef struct _rb_file {
gchar *name;
} rb_file;
/* Ringbuffer data structure */
/** Ringbuffer data structure */
typedef struct _ringbuf_data {
rb_file *files;
guint num_files; /* Number of ringbuffer files (1 to ...) */
guint curr_file_num; /* Number of the current file (ever increasing) */
gchar *fprefix; /* Filename prefix */
gchar *fsuffix; /* Filename suffix */
gboolean unlimited; /* TRUE if unlimited number of files */
guint num_files; /**< Number of ringbuffer files (1 to ...) */
guint curr_file_num; /**< Number of the current file (ever increasing) */
gchar *fprefix; /**< Filename prefix */
gchar *fsuffix; /**< Filename suffix */
gboolean unlimited; /**< TRUE if unlimited number of files */
int fd; /* Current ringbuffer file descriptor */
int fd; /**< Current ringbuffer file descriptor */
FILE *pdh;
gboolean group_read_access; /* TRUE if files need to be opened with group read access */
char *io_buffer; /**< The IO buffer used to write to the file */
gboolean group_read_access; /**< TRUE if files need to be opened with group read access */
} ringbuf_data;
static ringbuf_data rb_data;
@ -229,7 +230,22 @@ ringbuf_init_libpcap_fdopen(int *err)
if (err != NULL) {
*err = errno;
}
} else {
size_t buffsize = IO_BUF_SIZE;
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
ws_statb64 statb;
if (ws_fstat64(rb_data.fd, &statb) == 0) {
if (statb.st_blksize > IO_BUF_SIZE) {
buffsize = statb.st_blksize;
}
}
#endif
/* Increase the size of the IO bubffer */
rb_data.io_buffer = (char *)g_malloc(buffsize);
setvbuf(rb_data.pdh, rb_data.io_buffer, _IOFBF, buffsize);
}
return rb_data.pdh;
}
@ -251,6 +267,8 @@ ringbuf_switch_file(FILE **pdh, gchar **save_file, int *save_file_fd, int *err)
ws_close(rb_data.fd); /* XXX - the above should have closed this already */
rb_data.pdh = NULL; /* it's still closed, we just got an error while closing */
rb_data.fd = -1;
g_free(rb_data.io_buffer);
rb_data.io_buffer = NULL;
return FALSE;
}
@ -298,6 +316,9 @@ ringbuf_libpcap_dump_close(gchar **save_file, int *err)
}
rb_data.pdh = NULL;
rb_data.fd = -1;
g_free(rb_data.io_buffer);
rb_data.io_buffer = NULL;
}
/* set the save file name to the current file */
@ -362,6 +383,9 @@ ringbuf_error_cleanup(void)
}
}
}
g_free(rb_data.io_buffer);
rb_data.io_buffer = NULL;
/* free the memory */
ringbuf_free();
}

View File

@ -36,6 +36,9 @@ extern "C" {
#include <sys/stat.h> /* for stat() and struct stat */
/* We set a larger IO Buffer size for the capture files */
#define IO_BUF_SIZE (64 * 1024)
/*
* Visual C++ on Win32 systems doesn't define these. (Old UNIX systems don't
* define them either.)