Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
portable implementation of sendfile
authorMartin Quinson <martin.quinson@loria.fr>
Thu, 13 Apr 2017 01:17:35 +0000 (03:17 +0200)
committerMartin Quinson <martin.quinson@loria.fr>
Thu, 13 Apr 2017 01:17:35 +0000 (03:17 +0200)
CMakeLists.txt
src/smpi/smpi_global.cpp
tools/cmake/src/internal_config.h.in

index ab63f19..961c856 100644 (file)
@@ -341,6 +341,14 @@ else()
   set(HAVE_THREAD_LOCAL_STORAGE 0)
 endif()
 
   set(HAVE_THREAD_LOCAL_STORAGE 0)
 endif()
 
+CHECK_INCLUDE_FILE("sys/sendfile.h" HAVE_SENDFILE_H)
+CHECK_FUNCTION_EXISTS(sendfile HAVE_SENDFILE)
+if(HAVE_SENDFILE_H AND HAVE_SENDFILE)
+  set(HAVE_SENDFILE 1)
+else()
+  set(HAVE_SENDFILE 0)
+endif()
+
 if(enable_model-checking AND NOT "${CMAKE_SYSTEM}" MATCHES "Linux|FreeBSD")
   message(WARNING "Support for model-checking has not been enabled on ${CMAKE_SYSTEM}: disabling it")
   set(enable_model-checking FALSE)
 if(enable_model-checking AND NOT "${CMAKE_SYSTEM}" MATCHES "Linux|FreeBSD")
   message(WARNING "Support for model-checking has not been enabled on ${CMAKE_SYSTEM}: disabling it")
   set(enable_model-checking FALSE)
index aad63d8..2551821 100644 (file)
@@ -6,7 +6,6 @@
 #include <dlfcn.h>
 #include <fcntl.h>
 #include <spawn.h>
 #include <dlfcn.h>
 #include <fcntl.h>
 #include <spawn.h>
-#include <sys/sendfile.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <vector>
 #include <memory>
 
 #include <vector>
 #include <memory>
 
+#if HAVE_SENDFILE
+#include <sys/sendfile.h>
+#endif
+
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi, "Logging specific to SMPI (kernel)");
 #include <boost/tokenizer.hpp>
 #include <boost/algorithm/string.hpp> /* trim_right / trim_left */
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi, "Logging specific to SMPI (kernel)");
 #include <boost/tokenizer.hpp>
 #include <boost/algorithm/string.hpp> /* trim_right / trim_left */
@@ -543,7 +546,28 @@ int smpi_main(const char* executable, int argc, char *argv[])
         int fdout = open(target_executable.c_str(), O_WRONLY);
         xbt_assert(fdout >= 0, "Cannot write into %s", target_executable.c_str());
 
         int fdout = open(target_executable.c_str(), O_WRONLY);
         xbt_assert(fdout >= 0, "Cannot write into %s", target_executable.c_str());
 
+#if HAVE_SENDFILE
         sendfile(fdout, fdin, NULL, fdin_size);
         sendfile(fdout, fdin, NULL, fdin_size);
+#else
+        const int bufsize = 1024 * 1024 * 4;
+        char buf[bufsize];
+        while (int got = read(fdin, buf, bufsize)) {
+          if (got == -1) {
+            xbt_assert(errno == EINTR, "Cannot read from %s", executable_copy.c_str());
+          } else {
+            char* p  = buf;
+            int todo = got;
+            while (int done = write(fdout, p, todo)) {
+              if (done == -1) {
+                xbt_assert(errno == EINTR, "Cannot write into %s", target_executable.c_str());
+              } else {
+                p += done;
+                todo -= done;
+              }
+            }
+          }
+        }
+#endif
         close(fdout);
 
         // Load the copy and resolve the entry point:
         close(fdout);
 
         // Load the copy and resolve the entry point:
index 569e39d..4bc7710 100644 (file)
@@ -77,6 +77,8 @@
 #cmakedefine01 HAVE_PRIVATIZATION
 /* We have PAPI to fine-grain trace execution time */
 #cmakedefine01 HAVE_PAPI
 #cmakedefine01 HAVE_PRIVATIZATION
 /* We have PAPI to fine-grain trace execution time */
 #cmakedefine01 HAVE_PAPI
+/* We have sendfile to efficiently copy files for dl-open privatization */
+#cmakedefine01 HAVE_SENDFILE
 
 /* Other function checks */
 /* Function backtrace */
 
 /* Other function checks */
 /* Function backtrace */