Skip to content

Commit

Permalink
Use msync only in the end
Browse files Browse the repository at this point in the history
  • Loading branch information
JanSellner authored and FrancescAlted committed May 16, 2024
1 parent 88add69 commit 660cbc5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
43 changes: 24 additions & 19 deletions blosc/blosc2-stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void *blosc2_stdio_mmap_open(const char *urlpath, const char *mode, void* params
if (strcmp(mmap_file->urlpath, urlpath) != 0) {
BLOSC_TRACE_ERROR(
"The memory-mapped file is already opened with the path %s and hence cannot be reopened with the path %s. This "
"happens if you try to open a sframe (sparse frame) but please not that memory-mapped files are not supported "
"happens if you try to open a sframe (sparse frame); please note that memory-mapped files are not supported "
"for sframes.",
mmap_file->urlpath,
urlpath
Expand Down Expand Up @@ -326,15 +326,6 @@ int64_t blosc2_stdio_mmap_write(const void *ptr, int64_t size, int64_t nitems, i

mmap_file->addr = new_address;
}

memcpy(mmap_file->addr + position, ptr, n_bytes);

/* Ensure modified pages are written to disk */
if (!FlushViewOfFile(mmap_file->addr + position, n_bytes)) {
_print_last_error();
BLOSC_TRACE_ERROR("Cannot flush the memory-mapped file to disk.");
return 0;
}
#else
if (mmap_file->file_size < new_size) {
mmap_file->file_size = new_size;
Expand Down Expand Up @@ -385,17 +376,9 @@ int64_t blosc2_stdio_mmap_write(const void *ptr, int64_t size, int64_t nitems, i

mmap_file->addr = new_address;
}

memcpy(mmap_file->addr + position, ptr, n_bytes);

/* Ensure modified pages are written to disk */
int rc = msync(mmap_file->addr, mmap_file->file_size, MS_ASYNC);
if (rc < 0) {
BLOSC_TRACE_ERROR("Cannot sync the memory-mapped file to disk (error: %s).", strerror(errno));
return 0;
}
#endif

memcpy(mmap_file->addr + position, ptr, n_bytes);
return nitems;
}

Expand Down Expand Up @@ -440,6 +423,19 @@ int blosc2_stdio_mmap_destroy(void* params) {
int err = 0;

#if defined(_WIN32)
/* Ensure modified pages are written to disk */
if (!FlushViewOfFile(mmap_file->addr, mmap_file->file_size)) {
_print_last_error();
BLOSC_TRACE_ERROR("Cannot flush the memory-mapped view to disk.");
err = -1;
}
HANDLE file_handle = (HANDLE) _get_osfhandle(mmap_file->fd);
if (!FlushFileBuffers(file_handle)) {
_print_last_error();
BLOSC_TRACE_ERROR("Cannot flush the memory-mapped file to disk.");
err = -1;
}

if (!UnmapViewOfFile(mmap_file->addr)) {
_print_last_error();
BLOSC_TRACE_ERROR("Cannot unmap the memory-mapped file.");
Expand All @@ -456,6 +452,15 @@ int blosc2_stdio_mmap_destroy(void* params) {
err = -1;
}
#else
/* Ensure modified pages are written to disk */
/* This is important since not every munmap implementation flushes modified pages to disk
(e.g.: https://nfs.sourceforge.net/#faq_d8) */
int rc = msync(mmap_file->addr, mmap_file->file_size, MS_SYNC);
if (rc < 0) {
BLOSC_TRACE_ERROR("Cannot sync the memory-mapped file to disk (error: %s).", strerror(errno));
err = -1;
}

if (munmap(mmap_file->addr, mmap_file->mapping_size) < 0) {
BLOSC_TRACE_ERROR("Cannot unmap the memory-mapped file (error: %s).", strerror(errno));
err = -1;
Expand Down
7 changes: 4 additions & 3 deletions include/blosc2/blosc2-stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ BLOSC_EXPORT int blosc2_stdio_truncate(void *stream, int64_t size);


/**
* @brief Parameters for memory-mapped I/O. Existing schunks can be opened memory-mapped with the *_udio functions and
* new schunks be created by setting the io member of the #blosc2_storage struct (see test_mmap for examples). Please
* note that only cframes and not sframes can be opened memory-mapped.
* @brief Parameters for memory-mapped I/O. You can use the blosc2_schunk_open*_udio functions to memory-map existing
* schunk files from disk. To create a new schunk which is backed up by a memory-mapped file on disk, set the io member
* of the #blosc2_storage struct (see test_mmap for examples). Please note that memory-mapped I/O is only available for
* cframes and not sframes.
*/
typedef struct {
/* Arguments of the mapping */
Expand Down

0 comments on commit 660cbc5

Please sign in to comment.