Skip to content

Commit

Permalink
Updated sec2, log, and core VFDs to use pread/pwrite when
Browse files Browse the repository at this point in the history
available (can be controlled via a configure/CMake option)
  • Loading branch information
derobins committed Jan 31, 2019
1 parent 9149d5a commit f833001
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 23 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,15 @@ if (HDF5_MEMORY_ALLOC_SANITY_CHECK)
set (H5_MEMORY_ALLOC_SANITY_CHECK 1)
endif ()

#-----------------------------------------------------------------------------
# Option to enable/disable using pread/pwrite for VFDs
#-----------------------------------------------------------------------------
option (HDF5_ENABLE_PREADWRITE "Use pread/pwrite in sec2/log/core VFDs in place of read/write (when available)" ON)
mark_as_advanced (HDF5_ENABLE_PREADWRITE)
if (HDF5_ENABLE_PREADWRITE AND ${HDF_PREFIX}_HAVE_PREAD AND ${HDF_PREFIX}_HAVE_PWRITE)
set (H5_HAVE_PREADWRITE 1)
endif ()

#-----------------------------------------------------------------------------
# Option to use deprecated public API symbols
#-----------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions config/cmake/H5pubconf.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@
/* Define if we have parallel support */
#cmakedefine H5_HAVE_PARALLEL @H5_HAVE_PARALLEL@

/* Define if both pread and pwrite exist. */
#cmakedefine H5_HAVE_PREADWRITE @H5_HAVE_PREADWRITE@

/* Define to 1 if you have the <pthread.h> header file. */
#cmakedefine H5_HAVE_PTHREAD_H @H5_HAVE_PTHREAD_H@

Expand Down
8 changes: 6 additions & 2 deletions config/cmake_ext_mod/ConfigureChecks.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,12 @@ if (NOT WINDOWS)
# functionality so clock_gettime and CLOCK_MONOTONIC are defined
# correctly. This was later updated to 200112L so that
# posix_memalign() is visible for the direct VFD code on Linux
# systems.
# systems. Even later, this was changed to 200809L to support
# pread/pwrite in VFDs.
#
# POSIX feature information can be found in the gcc manual at:
# http://www.gnu.org/s/libc/manual/html_node/Feature-Test-Macros.html
set (HDF_EXTRA_C_FLAGS -D_POSIX_C_SOURCE=200112L)
set (HDF_EXTRA_C_FLAGS -D_POSIX_C_SOURCE=200809L)

# Need to add this so that O_DIRECT is visible for the direct
# VFD on Linux systems.
Expand Down Expand Up @@ -506,6 +508,8 @@ CHECK_FUNCTION_EXISTS (lround ${HDF_PREFIX}_HAVE_LROUND)
CHECK_FUNCTION_EXISTS (lroundf ${HDF_PREFIX}_HAVE_LROUNDF)
CHECK_FUNCTION_EXISTS (lstat ${HDF_PREFIX}_HAVE_LSTAT)

CHECK_FUNCTION_EXISTS (pread ${HDF_PREFIX}_HAVE_PREAD)
CHECK_FUNCTION_EXISTS (pwrite ${HDF_PREFIX}_HAVE_PWRITE)
CHECK_FUNCTION_EXISTS (rand_r ${HDF_PREFIX}_HAVE_RAND_R)
CHECK_FUNCTION_EXISTS (random ${HDF_PREFIX}_HAVE_RANDOM)
CHECK_FUNCTION_EXISTS (round ${HDF_PREFIX}_HAVE_ROUND)
Expand Down
46 changes: 44 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1130,11 +1130,12 @@ case "$host_cpu-$host_vendor-$host_os" in
## functionality so clock_gettime and CLOCK_MONOTONIC are defined
## correctly. This was later updated to 200112L so that
## posix_memalign() is visible for the direct VFD code on Linux
## systems.
## systems. Even later, this was changed to 200809L to support
## pread/pwrite in VFDs.
##
## POSIX feature information can be found in the gcc manual at:
## http://www.gnu.org/s/libc/manual/html_node/Feature-Test-Macros.html
H5_CPPFLAGS="-D_POSIX_C_SOURCE=200112L $H5_CPPFLAGS"
H5_CPPFLAGS="-D_POSIX_C_SOURCE=200809L $H5_CPPFLAGS"

## Need to add this so that O_DIRECT is visible for the direct
## VFD on Linux systems.
Expand Down Expand Up @@ -3265,6 +3266,47 @@ case "X-$STRICT_FORMAT_CHECKS" in
esac


## ----------------------------------------------------------------------
## Enable use of pread/pwrite instead of read/write in certain VFDs.
##
AC_SUBST([PREADWRITE])

## Check these first to avoid interspersed output in the AC_ARG_ENABLE line
## below. (Probably overkill to check for both, but we'll be extra careful)
PREADWRITE_HAVE_BOTH=yes
AC_CHECK_FUNC([pread], [], [PREADWRITE_HAVE_BOTH=no])
AC_CHECK_FUNC([pwrite], [], [PREADWRITE_HAVE_BOTH=no])

AC_MSG_CHECKING([whether to use pread/pwrite instead of read/write in certain VFDs])
AC_ARG_ENABLE([preadwrite],
[AS_HELP_STRING([--enable-preadwrite],
[Enable using pread/pwrite instead of read/write in sec2/log/core VFDs.
[default=yes if pread/pwrite are present]])],
[PREADWRITE=$enableval])

## Set the default level.
if test "X-$PREADWRITE" = X- ; then
PREADWRITE=yes
fi

case "X-$PREADWRITE" in
X-yes)
if test "X-$PREADWRITE_HAVE_BOTH" = "X-yes"; then
AC_DEFINE([HAVE_PREADWRITE], [1], [Define if both pread and pwrite exist.])
AC_MSG_RESULT([yes])
else
AC_MSG_RESULT([no])
fi
;;
X-no)
AC_MSG_RESULT([no])
;;
*)
AC_MSG_ERROR([Unrecognized value: $PREADWRITE])
;;
esac


## ----------------------------------------------------------------------
## Enable embedded library information
##
Expand Down
28 changes: 22 additions & 6 deletions src/H5FDcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,17 @@ H5FD__core_write_to_bstore(H5FD_core_t *file, haddr_t addr, size_t size)

HDassert(file);

/* Write to backing store */
if((off_t)addr != HDlseek(file->fd, (off_t)addr, SEEK_SET))
#ifndef H5_HAVE_PREADWRITE
/* Seek to the correct location (if we don't have pwrite) */
if((HDoff_t)addr != HDlseek(file->fd, (off_t)addr, SEEK_SET))
HGOTO_ERROR(H5E_IO, H5E_SEEKERROR, FAIL, "error seeking in backing store")
#endif /* H5_HAVE_PREADWRITE */

while (size > 0) {

h5_posix_io_t bytes_in = 0; /* # of bytes to write */
h5_posix_io_ret_t bytes_wrote = -1; /* # of bytes written */
HDoff_t offset = (HDoff_t)addr;

/* Trying to write more bytes than the return type can handle is
* undefined behavior in POSIX.
Expand All @@ -366,15 +369,21 @@ H5FD__core_write_to_bstore(H5FD_core_t *file, haddr_t addr, size_t size)
bytes_in = (h5_posix_io_t)size;

do {
#ifdef H5_HAVE_PREADWRITE
bytes_wrote = HDpwrite(file->fd, ptr, bytes_in, offset);
offset += bytes_wrote;
#else
bytes_wrote = HDwrite(file->fd, ptr, bytes_in);
#endif /* H5_HAVE_PREADWRITE */
} while(-1 == bytes_wrote && EINTR == errno);

if(-1 == bytes_wrote) { /* error */
int myerrno = errno;
time_t mytime = HDtime(NULL);
HDoff_t myoffset = HDlseek(file->fd, (HDoff_t)0, SEEK_CUR);

HGOTO_ERROR(H5E_IO, H5E_WRITEERROR, FAIL, "write to backing store failed: time = %s, filename = '%s', file descriptor = %d, errno = %d, error message = '%s', ptr = %p, total write size = %llu, bytes this sub-write = %llu, bytes actually written = %llu, offset = %llu", HDctime(&mytime), file->name, file->fd, myerrno, HDstrerror(myerrno), ptr, (unsigned long long)size, (unsigned long long)bytes_in, (unsigned long long)bytes_wrote, (unsigned long long)myoffset);
offset = HDlseek(file->fd, (HDoff_t)0, SEEK_CUR);

HGOTO_ERROR(H5E_IO, H5E_WRITEERROR, FAIL, "write to backing store failed: time = %s, filename = '%s', file descriptor = %d, errno = %d, error message = '%s', ptr = %p, total write size = %llu, bytes this sub-write = %llu, bytes actually written = %llu, offset = %llu", HDctime(&mytime), file->name, file->fd, myerrno, HDstrerror(myerrno), ptr, (unsigned long long)size, (unsigned long long)bytes_in, (unsigned long long)bytes_wrote, (unsigned long long)offset);
} /* end if */

HDassert(bytes_wrote > 0);
Expand Down Expand Up @@ -852,6 +861,7 @@ H5FD__core_open(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxaddr
while(size > 0) {
h5_posix_io_t bytes_in = 0; /* # of bytes to read */
h5_posix_io_ret_t bytes_read = -1; /* # of bytes actually read */
HDoff_t offset = (HDoff_t)0;

/* Trying to read more bytes than the return type can handle is
* undefined behavior in POSIX.
Expand All @@ -862,15 +872,21 @@ H5FD__core_open(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxaddr
bytes_in = (h5_posix_io_t)size;

do {
#ifdef H5_HAVE_PREADWRITE
bytes_read = HDpread(file->fd, mem, bytes_in, offset);
offset += bytes_read;
#else
bytes_read = HDread(file->fd, mem, bytes_in);
#endif /* H5_HAVE_PREADWRITE */
} while(-1 == bytes_read && EINTR == errno);

if(-1 == bytes_read) { /* error */
int myerrno = errno;
time_t mytime = HDtime(NULL);
HDoff_t myoffset = HDlseek(file->fd, (HDoff_t)0, SEEK_CUR);

HGOTO_ERROR(H5E_IO, H5E_READERROR, NULL, "file read failed: time = %s, filename = '%s', file descriptor = %d, errno = %d, error message = '%s', file->mem = %p, total read size = %llu, bytes this sub-read = %llu, bytes actually read = %llu, offset = %llu", HDctime(&mytime), file->name, file->fd, myerrno, HDstrerror(myerrno), file->mem, (unsigned long long)size, (unsigned long long)bytes_in, (unsigned long long)bytes_read, (unsigned long long)myoffset);
offset = HDlseek(file->fd, (HDoff_t)0, SEEK_CUR);

HGOTO_ERROR(H5E_IO, H5E_READERROR, NULL, "file read failed: time = %s, filename = '%s', file descriptor = %d, errno = %d, error message = '%s', file->mem = %p, total read size = %llu, bytes this sub-read = %llu, bytes actually read = %llu, offset = %llu", HDctime(&mytime), file->name, file->fd, myerrno, HDstrerror(myerrno), file->mem, (unsigned long long)size, (unsigned long long)bytes_in, (unsigned long long)bytes_read, (unsigned long long)offset);
} /* end if */

HDassert(bytes_read >= 0);
Expand Down
30 changes: 24 additions & 6 deletions src/H5FDlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,8 @@ H5FD_log_read(H5FD_t *_file, H5FD_mem_t type, hid_t H5_ATTR_UNUSED dxpl_id, hadd
} /* end if */
} /* end if */

/* Seek to the correct location */
#ifndef H5_HAVE_PREADWRITE
/* Seek to the correct location (if we don't have pread) */
if(addr != file->pos || OP_READ != file->op) {
#ifdef H5_HAVE_GETTIMEOFDAY
if(file->fa.flags & H5FD_LOG_TIME_SEEK)
Expand Down Expand Up @@ -1234,6 +1235,7 @@ H5FD_log_read(H5FD_t *_file, H5FD_mem_t type, hid_t H5_ATTR_UNUSED dxpl_id, hadd
#endif /* H5_HAVE_GETTIMEOFDAY */
} /* end if */
} /* end if */
#endif /* H5_HAVE_PREADWRITE */

/*
* Read data, being careful of interrupted system calls, partial results,
Expand All @@ -1247,6 +1249,7 @@ H5FD_log_read(H5FD_t *_file, H5FD_mem_t type, hid_t H5_ATTR_UNUSED dxpl_id, hadd

h5_posix_io_t bytes_in = 0; /* # of bytes to read */
h5_posix_io_ret_t bytes_read = -1; /* # of bytes actually read */
HDoff_t offset = (HDoff_t)addr;

/* Trying to read more bytes than the return type can handle is
* undefined behavior in POSIX.
Expand All @@ -1257,18 +1260,24 @@ H5FD_log_read(H5FD_t *_file, H5FD_mem_t type, hid_t H5_ATTR_UNUSED dxpl_id, hadd
bytes_in = (h5_posix_io_t)size;

do {
#ifdef H5_HAVE_PREADWRITE
bytes_read = HDpread(file->fd, buf, bytes_in, offset);
offset += bytes_read;
#else
bytes_read = HDread(file->fd, buf, bytes_in);
#endif /* H5_HAVE_PREADWRITE */
} while(-1 == bytes_read && EINTR == errno);

if(-1 == bytes_read) { /* error */
int myerrno = errno;
time_t mytime = HDtime(NULL);
HDoff_t myoffset = HDlseek(file->fd, (HDoff_t)0, SEEK_CUR);

offset = HDlseek(file->fd, (HDoff_t)0, SEEK_CUR);

if(file->fa.flags & H5FD_LOG_LOC_READ)
HDfprintf(file->logfp, "Error! Reading: %10a-%10a (%10Zu bytes)\n", orig_addr, (orig_addr + orig_size) - 1, orig_size);

HGOTO_ERROR(H5E_IO, H5E_READERROR, FAIL, "file read failed: time = %s, filename = '%s', file descriptor = %d, errno = %d, error message = '%s', buf = %p, total read size = %llu, bytes this sub-read = %llu, bytes actually read = %llu, offset = %llu", HDctime(&mytime), file->filename, file->fd, myerrno, HDstrerror(myerrno), buf, (unsigned long long)size, (unsigned long long)bytes_in, (unsigned long long)bytes_read, (unsigned long long)myoffset);
HGOTO_ERROR(H5E_IO, H5E_READERROR, FAIL, "file read failed: time = %s, filename = '%s', file descriptor = %d, errno = %d, error message = '%s', buf = %p, total read size = %llu, bytes this sub-read = %llu, bytes actually read = %llu, offset = %llu", HDctime(&mytime), file->filename, file->fd, myerrno, HDstrerror(myerrno), buf, (unsigned long long)size, (unsigned long long)bytes_in, (unsigned long long)bytes_read, (unsigned long long)offset);
} /* end if */

if(0 == bytes_read) {
Expand Down Expand Up @@ -1398,7 +1407,8 @@ H5FD_log_write(H5FD_t *_file, H5FD_mem_t type, hid_t H5_ATTR_UNUSED dxpl_id, had
file->nwrite[tmp_addr++]++;
} /* end if */

/* Seek to the correct location */
#ifndef H5_HAVE_PREADWRITE
/* Seek to the correct location (if we don't have pwrite) */
if(addr != file->pos || OP_WRITE != file->op) {
#ifdef H5_HAVE_GETTIMEOFDAY
if(file->fa.flags & H5FD_LOG_TIME_SEEK)
Expand Down Expand Up @@ -1441,6 +1451,7 @@ H5FD_log_write(H5FD_t *_file, H5FD_mem_t type, hid_t H5_ATTR_UNUSED dxpl_id, had
#endif /* H5_HAVE_GETTIMEOFDAY */
} /* end if */
} /* end if */
#endif /* H5_HAVE_PREADWRITE */

/*
* Write the data, being careful of interrupted system calls and partial
Expand All @@ -1454,6 +1465,7 @@ H5FD_log_write(H5FD_t *_file, H5FD_mem_t type, hid_t H5_ATTR_UNUSED dxpl_id, had

h5_posix_io_t bytes_in = 0; /* # of bytes to write */
h5_posix_io_ret_t bytes_wrote = -1; /* # of bytes written */
HDoff_t offset = (HDoff_t)addr;

/* Trying to write more bytes than the return type can handle is
* undefined behavior in POSIX.
Expand All @@ -1464,18 +1476,24 @@ H5FD_log_write(H5FD_t *_file, H5FD_mem_t type, hid_t H5_ATTR_UNUSED dxpl_id, had
bytes_in = (h5_posix_io_t)size;

do {
#ifdef H5_HAVE_PREADWRITE
bytes_wrote = HDpwrite(file->fd, buf, bytes_in, offset);
offset += bytes_wrote;
#else
bytes_wrote = HDwrite(file->fd, buf, bytes_in);
#endif /* H5_HAVE_PREADWRITE */
} while(-1 == bytes_wrote && EINTR == errno);

if(-1 == bytes_wrote) { /* error */
int myerrno = errno;
time_t mytime = HDtime(NULL);
HDoff_t myoffset = HDlseek(file->fd, (HDoff_t)0, SEEK_CUR);

offset = HDlseek(file->fd, (HDoff_t)0, SEEK_CUR);

if(file->fa.flags & H5FD_LOG_LOC_WRITE)
HDfprintf(file->logfp, "Error! Writing: %10a-%10a (%10Zu bytes)\n", orig_addr, (orig_addr + orig_size) - 1, orig_size);

HGOTO_ERROR(H5E_IO, H5E_WRITEERROR, FAIL, "file write failed: time = %s, filename = '%s', file descriptor = %d, errno = %d, error message = '%s', buf = %p, total write size = %llu, bytes this sub-write = %llu, bytes actually written = %llu, offset = %llu", HDctime(&mytime), file->filename, file->fd, myerrno, HDstrerror(myerrno), buf, (unsigned long long)size, (unsigned long long)bytes_in, (unsigned long long)bytes_wrote, (unsigned long long)myoffset);
HGOTO_ERROR(H5E_IO, H5E_WRITEERROR, FAIL, "file write failed: time = %s, filename = '%s', file descriptor = %d, errno = %d, error message = '%s', buf = %p, total write size = %llu, bytes this sub-write = %llu, bytes actually written = %llu, offset = %llu", HDctime(&mytime), file->filename, file->fd, myerrno, HDstrerror(myerrno), buf, (unsigned long long)size, (unsigned long long)bytes_in, (unsigned long long)bytes_wrote, (unsigned long long)offset);
} /* end if */

HDassert(bytes_wrote > 0);
Expand Down
Loading

0 comments on commit f833001

Please sign in to comment.