Encapsulate use of nice()
authorTeemu Murtola <teemu.murtola@gmail.com>
Sun, 19 Oct 2014 06:55:37 +0000 (09:55 +0300)
committerTeemu Murtola <teemu.murtola@gmail.com>
Thu, 23 Oct 2014 03:42:28 +0000 (06:42 +0300)
- Instead of platform-specific #ifdefs in otherwise platform-agnostic
  code, encapsulate the call in sysinfo.h/.cpp.
- Make it fully CMake's responsibility to determine whether nice() is
  going to be used.  This could probably be simplified further (even to
  the point of using HAVE_NICE directly), but since I'm not sure how all
  the special cases work, left them as they were, but with TODO
  comments.
- Remove some unused platform-specific #includes.

Change-Id: I9f1172ba2c02bb042574ad5d2b9ccbf5d31a3d6f

CMakeLists.txt
src/config.h.cmakein
src/gromacs/commandline/cmdlinemodulemanager.cpp
src/gromacs/gmxpreprocess/fflibutil.cpp
src/gromacs/tools/dump.c
src/gromacs/utility/sysinfo.cpp
src/gromacs/utility/sysinfo.h
src/programs/mdrun/runner.cpp

index 1f52c9abc401bfca274abcbe5860d03dcff2fd9d..9d11c547093862f7cb0825c59c6e225c45d3e032 100644 (file)
@@ -354,6 +354,7 @@ check_function_exists(sysconf           HAVE_SYSCONF)
 check_function_exists(rsqrt             HAVE_RSQRT)
 check_function_exists(rsqrtf            HAVE_RSQRTF)
 check_function_exists(sqrtf             HAVE_SQRTF)
+check_function_exists(nice              HAVE_NICE)
 
 include(CheckLibraryExists)
 check_library_exists(m sqrt "" HAVE_LIBM)
@@ -365,6 +366,11 @@ test_sched_affinity(HAVE_SCHED_AFFINITY)
 include(TestBigEndian)
 test_big_endian(GMX_INTEGER_BIG_ENDIAN)
 
+set(GMX_USE_NICE 0)
+if (HAVE_UNISTD_H AND HAVE_NICE)
+    set(GMX_USE_NICE 1)
+endif()
+
 # Management of GROMACS options for specific toolchains should go
 # here. Because the initial settings for some of the main options have
 # already happened, but things like library detection and MPI compiler
@@ -670,7 +676,8 @@ option(GMX_NACL "Configure for Native Client builds" OFF)
 if (GMX_NACL)
   list(APPEND GMX_EXTRA_LIBRARIES nosys)
   set(GMX_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lnosys")
-  set(GMX_NO_NICE 1)
+  # TODO: Is this still necessary with the check for its presence?
+  set(GMX_USE_NICE 0)
   set(GMX_NO_RENAME 1)
 endif()
 mark_as_advanced(GMX_NACL)
index f79823c61cd70fb254f8f8174e3ffe6ea07b10aa..39b643332f8379ce1c1ce7bff3a4fed1ca18f6ce 100644 (file)
 /* Use OpenMP multithreading */
 #cmakedefine GMX_OPENMP
 
-/* Ignore calls to nice(3) */
-#cmakedefine GMX_NO_NICE
+/* Can and should use nice(3) to set priority */
+#cmakedefine GMX_USE_NICE
 
 /* Use if can't rename checkpoints */
 #cmakedefine GMX_NO_RENAME
index 74113cc53126c15e5eeef8b9c595c96674c67766..6c26c7c11dea44d7c96b6dfc92e214d7daf9d02f 100644 (file)
 #include <string>
 #include <utility>
 
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-
 #include "gromacs/commandline/cmdlinehelpcontext.h"
 #include "gromacs/commandline/cmdlineinit.h"
 #include "gromacs/commandline/cmdlinemodule.h"
@@ -67,6 +63,7 @@
 #include "gromacs/utility/fatalerror.h"
 #include "gromacs/utility/gmxassert.h"
 #include "gromacs/utility/stringutil.h"
+#include "gromacs/utility/sysinfo.h"
 
 #include "cmdlinehelpmodule.h"
 #include "cmdlinemodulemanager-impl.h"
@@ -578,21 +575,17 @@ int CommandLineModuleManager::run(int argc, char *argv[])
         fprintf(stderr, "Will write debug log file: %s\n", filename.c_str());
         gmx_init_debug(optionsHolder.debugLevel(), filename.c_str());
     }
-#if defined(HAVE_UNISTD_H) && !defined(GMX_NO_NICE) && !defined(__MINGW32__)
     // Set the nice level unless disabled in the configuration.
     if (optionsHolder.niceLevel() != 0)
     {
         static bool bNiceSet = false; // Only set it once.
         if (!bNiceSet)
         {
-            if (nice(optionsHolder.niceLevel()) == -1)
-            {
-                // Do nothing, but use the return value to avoid warnings.
-            }
+            // TODO: Diagnostic if this fails and the user explicitly requested it.
+            gmx_set_nice(optionsHolder.niceLevel());
             bNiceSet = true;
         }
     }
-#endif
 
     int rc = 0;
     if (!(module == impl_->helpModule_ && !bMaster))
index e87fb8f659a5183e0acac7ef7fbd4a02abae6390..962ed08e959657bdd953ec40c36701864b0de0fe 100644 (file)
 
 #include "fflibutil.h"
 
-#include "config.h"
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include <fcntl.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-
 #include "gromacs/legacyheaders/network.h"
 #include "gromacs/utility/cstringutil.h"
 #include "gromacs/utility/exceptions.h"
index 302ce2652552c062758599bb1a9211f48ea39695..e6dec521aa8c967c21fac69351e00395e6a5d38b 100644 (file)
 #include <stdio.h>
 #include <string.h>
 
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-
 #include "gromacs/commandline/pargs.h"
 #include "gromacs/fileio/enxio.h"
 #include "gromacs/fileio/gmxfio.h"
index d1a5353f1104445b9cde6c2a64fb905cfa514692..65a3363fdfb203bb89cd77690c43350bb53d4f8d 100644 (file)
@@ -62,6 +62,7 @@
 #include <unistd.h>
 #endif
 
+#include "gromacs/utility/basedefinitions.h"
 #include "gromacs/utility/gmxassert.h"
 
 namespace
@@ -145,3 +146,18 @@ void gmx_format_current_time(char *buf, size_t len)
     time_t clock = time(NULL);
     gmx_ctime_r(&clock, buf, len);
 }
+
+int gmx_set_nice(int level)
+{
+#ifdef GMX_USE_NICE
+    // TODO: This may not be reliable, but currently the return value is not
+    // used.
+    if (nice(level) != -1)
+    {
+        return 0;
+    }
+#else
+    GMX_UNUSED_VALUE(level);
+#endif
+    return -1;
+}
index 659048d4b14304e3c1325d213cfb0adfa5d98da0..3b3c50a40c4416b065ad87cf2bb8f5a884bdc817 100644 (file)
@@ -108,6 +108,13 @@ char *gmx_ctime_r(const time_t *clock, char *buf, size_t len);
  */
 void gmx_format_current_time(char *buf, size_t len);
 
+/*! \brief
+ * Wrapper for nice().
+ *
+ * Does not throw.
+ */
+int gmx_set_nice(int level);
+
 /*! \} */
 
 #ifdef __cplusplus
index 054b4fbd5b7bb350e2633d3a199ba359ef5f4655..36807f90166a6d25601b0ea1aa18c4551967d928 100644 (file)
@@ -45,9 +45,6 @@
 #include <string.h>
 
 #include <algorithm>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
 
 #include "gromacs/essentialdynamics/edsam.h"
 #include "gromacs/ewald/ewald-util.h"