[m-rev.] diff: improve runtime sanity checking and error messages

Julien Fischer jfischer at opturion.com
Thu Sep 18 11:45:18 AEST 2014


Improve runtime sanity checking and error messages.

Always check the return value of calls to the function sem_init.
This function is not implemented on OS X and always returns an error.

Replace some direct calls to perror in the runtime with calls to MR_perror.
The latter ensures that the error message mentions that the error originates
from a call within the Mercury runtime.

runtime/mercury_context.c
runtime/mercury_prof_time.c:
runtime/mercury_threadscope.c:
 	As above.

Julien.

diff --git a/runtime/mercury_context.c b/runtime/mercury_context.c
index e5963a7..a797fb4 100644
--- a/runtime/mercury_context.c
+++ b/runtime/mercury_context.c
@@ -365,7 +365,10 @@ MR_init_context_stuff(void)
      #ifdef MR_DEBUG_RUNTIME_GRANULARITY_CONTROL
      pthread_mutex_init(&MR_par_cond_stats_lock, MR_MUTEX_ATTR);
      #endif
-    sem_init(&shutdown_ws_semaphore, 0, 0);
+    if (sem_init(&shutdown_ws_semaphore, 0, 0) == -1) {
+        MR_perror("cannot initialize semaphore");
+        exit(EXIT_FAILURE);
+    }
    #endif
      pthread_mutex_init(&MR_STM_lock, MR_MUTEX_ATTR);

@@ -399,7 +402,10 @@ MR_init_context_stuff(void)
      for (i = 0; i < MR_max_engines; i++) {
          engine_sleep_sync *esync = get_engine_sleep_sync(i);

-        sem_init(&esync->d.es_sleep_semaphore, 0, 0);
+        if (sem_init(&esync->d.es_sleep_semaphore, 0, 0) == -1 ) {
+            MR_perror("cannot initialize semaphore");
+            exit(EXIT_FAILURE);
+        };
          pthread_mutex_init(&esync->d.es_wake_lock, MR_MUTEX_ATTR);
          /*
          ** All engines are initially working (because telling them to wake up
@@ -470,7 +476,7 @@ MR_reset_available_cpus(void)
      }

      if (-1 == sched_getaffinity(0, cpuset_size, MR_available_cpus)) {
-        perror("Couldn't get CPU affinity");
+        MR_perror("Couldn't get CPU affinity");
          MR_thread_pinning = MR_FALSE;
          CPU_FREE(MR_available_cpus);
          MR_available_cpus = NULL;
@@ -636,7 +642,7 @@ static int MR_current_cpu(void)
          os_cpu = 0;

          if (MR_thread_pinning) {
-            perror("Warning: unable to determine the current CPU for "
+            MR_perror("Warning: unable to determine the current CPU for "
                  "this thread: ");
          }
      }
@@ -692,7 +698,7 @@ MR_do_pin_thread(int cpu)
      errno = hwloc_set_cpubind(MR_hw_topology, pu->cpuset,
          HWLOC_CPUBIND_THREAD);
      if (errno != 0) {
-        perror("Warning: Couldn't set CPU affinity: ");
+        MR_perror("Warning: Couldn't set CPU affinity: ");
          MR_thread_pinning = MR_FALSE;
          return MR_FALSE;
      }
@@ -704,7 +710,7 @@ MR_do_pin_thread(int cpu)
      CPU_ZERO_S(MR_cpuset_size, cpus);
      CPU_SET_S(cpu, MR_cpuset_size, cpus);
      if (sched_setaffinity(0, MR_cpuset_size, cpus) == -1) {
-        perror("Warning: Couldn't set CPU affinity: ");
+        MR_perror("Warning: Couldn't set CPU affinity: ");
          /*
          ** If this failed once, it will probably fail again,
          ** so we disable it.
@@ -886,7 +892,7 @@ MR_write_out_profiling_parallel_execution(void)
      return;

  Error:
-    perror(MR_PROFILE_PARALLEL_EXECUTION_FILENAME);
+    MR_perror(MR_PROFILE_PARALLEL_EXECUTION_FILENAME);
      abort();
  }

@@ -2445,7 +2451,7 @@ retry_sleep:
                      }
                      goto retry_sleep;
                  default:
-                    perror("sem_timedwait");
+                    MR_perror("sem_timedwait");
                      abort();
              }
          } /* if sem_wait raised an error */
diff --git a/runtime/mercury_prof_time.c b/runtime/mercury_prof_time.c
index 2b5d2ce..d03aef6 100644
--- a/runtime/mercury_prof_time.c
+++ b/runtime/mercury_prof_time.c
@@ -139,8 +139,8 @@ MR_checked_setitimer(int which, struct itimerval *value)
  {
      errno = 0;
      if (setitimer(which, value, NULL) != 0) {
-        perror("Mercury runtime: cannot set timer for profiling");
-        exit(1);
+        MR_perror("cannot set timer for profiling");
+        exit(EXIT_FAILURE);
      }
  }

diff --git a/runtime/mercury_threadscope.c b/runtime/mercury_threadscope.c
index 8164c2b..31f1815 100644
--- a/runtime/mercury_threadscope.c
+++ b/runtime/mercury_threadscope.c
@@ -1946,7 +1946,7 @@ MR_open_output_file_and_write_prelude(void)

      MR_threadscope_output_file = fopen(MR_threadscope_output_filename, "w");
      if (!MR_threadscope_output_file) {
-        perror(MR_threadscope_output_filename);
+        MR_perror(MR_threadscope_output_filename);
          return;
      }

@@ -1972,7 +1972,7 @@ MR_close_output_file(void)
          put_be_uint16(&global_buffer, MR_TS_EVENT_DATA_END);
          if (flush_event_buffer(&global_buffer)) {
              if (EOF == fclose(MR_threadscope_output_file)) {
-                perror(MR_threadscope_output_filename);
+                MR_perror(MR_threadscope_output_filename);
              }
              MR_threadscope_output_file = NULL;
              MR_threadscope_output_filename = NULL;
@@ -2033,7 +2033,7 @@ flush_event_buffer(struct MR_threadscope_event_buffer *buffer)
          if (0 == fwrite(buffer->MR_tsbuffer_data, buffer->MR_tsbuffer_pos, 1,
              MR_threadscope_output_file))
          {
-            perror(MR_threadscope_output_filename);
+            MR_perror(MR_threadscope_output_filename);
              MR_threadscope_output_file = NULL;
              MR_threadscope_output_filename = NULL;
          }
@@ -2253,7 +2253,7 @@ gettimeofday_nsecs(void)
      struct timeval      tv;

      if (0 != gettimeofday(&tv, NULL)) {
-        perror("gettimeofday()");
+        MR_perror("gettimeofday()");
          /*
          ** Return a stupid value generating an obviously bad logfile
          ** rather than crashing a program that may otherwise work.



More information about the reviews mailing list