utils.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012-2014 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include <common/config.h>
  19. #include <common/utils.h>
  20. #include <libgen.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #ifdef __MINGW32__
  24. #include <io.h>
  25. #define mkdir(path, mode) mkdir(path)
  26. #endif
  27. /* Function with behaviour like `mkdir -p'. This function was adapted from
  28. * http://niallohiggins.com/2009/01/08/mkpath-mkdir-p-alike-in-c-for-unix/ */
  29. int _starpu_mkpath(const char *s, mode_t mode)
  30. {
  31. int olderrno;
  32. char *q, *r = NULL, *path = NULL, *up = NULL;
  33. int rv;
  34. rv = -1;
  35. if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0
  36. #ifdef __MINGW32__
  37. /* C:/ or C:\ */
  38. || (s[0] && s[1] == ':' && (s[2] == '/' || s[2] == '\\') && !s[3])
  39. #endif
  40. )
  41. return 0;
  42. if ((path = strdup(s)) == NULL)
  43. STARPU_ABORT();
  44. if ((q = strdup(s)) == NULL)
  45. STARPU_ABORT();
  46. if ((r = dirname(q)) == NULL)
  47. goto out;
  48. if ((up = strdup(r)) == NULL)
  49. STARPU_ABORT();
  50. if ((_starpu_mkpath(up, mode) == -1) && (errno != EEXIST))
  51. goto out;
  52. if ((mkdir(path, mode) == -1) && (errno != EEXIST))
  53. rv = -1;
  54. else
  55. rv = 0;
  56. out:
  57. olderrno = errno;
  58. if (up)
  59. free(up);
  60. free(q);
  61. free(path);
  62. errno = olderrno;
  63. return rv;
  64. }
  65. void _starpu_mkpath_and_check(const char *path, mode_t mode)
  66. {
  67. int ret;
  68. ret = _starpu_mkpath(path, mode);
  69. if (ret == -1)
  70. {
  71. if (errno != EEXIST)
  72. {
  73. fprintf(stderr,"Error making StarPU directory %s:\n", path);
  74. perror("mkdir");
  75. STARPU_ABORT();
  76. }
  77. /* make sure that it is actually a directory */
  78. struct stat sb;
  79. stat(path, &sb);
  80. if (!S_ISDIR(sb.st_mode))
  81. {
  82. fprintf(stderr,"Error: %s is not a directory:\n", path);
  83. STARPU_ABORT();
  84. }
  85. }
  86. }
  87. int _starpu_check_mutex_deadlock(starpu_pthread_mutex_t *mutex)
  88. {
  89. int ret;
  90. ret = starpu_pthread_mutex_trylock(mutex);
  91. if (!ret)
  92. {
  93. STARPU_PTHREAD_MUTEX_UNLOCK(mutex);
  94. return 0;
  95. }
  96. if (ret == EBUSY)
  97. return 0;
  98. STARPU_ASSERT (ret != EDEADLK);
  99. return 1;
  100. }
  101. char *_starpu_get_home_path(void)
  102. {
  103. char *path = getenv("XDG_CACHE_HOME");
  104. if (!path)
  105. path = getenv("STARPU_HOME");
  106. if (!path)
  107. path = getenv("HOME");
  108. if (!path)
  109. path = getenv("USERPROFILE");
  110. if (!path) {
  111. static int warn;
  112. if (!warn) {
  113. warn = 1;
  114. _STARPU_DISP("couldn't find a $STARPU_HOME place to put .starpu data, using /tmp\n");
  115. }
  116. path = "/tmp";
  117. }
  118. return path;
  119. }
  120. void _starpu_gethostname(char *hostname, size_t size)
  121. {
  122. char *forced_hostname = getenv("STARPU_HOSTNAME");
  123. if (forced_hostname && forced_hostname[0])
  124. {
  125. snprintf(hostname, size-1, "%s", forced_hostname);
  126. hostname[size-1] = 0;
  127. }
  128. else
  129. {
  130. char *c;
  131. gethostname(hostname, size-1);
  132. hostname[size-1] = 0;
  133. c = strchr(hostname, '.');
  134. if (c)
  135. *c = 0;
  136. }
  137. }
  138. void _starpu_sleep(struct timespec ts)
  139. {
  140. #ifdef STARPU_SIMGRID
  141. MSG_process_sleep(ts.tv_sec + ts.tv_nsec / 1000000000.);
  142. #elif defined(STARPU_HAVE_WINDOWS)
  143. Sleep((ts.tv_sec * 1000) + (ts.tv_nsec / 1000000));
  144. #else
  145. struct timespec req, rem;
  146. req = ts;
  147. while (nanosleep(&req, &rem))
  148. req = rem;
  149. #endif
  150. }