utils.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 <errno.h>
  21. #ifdef HAVE_UNISTD_H
  22. #include <unistd.h>
  23. #endif
  24. #include <fcntl.h>
  25. #if defined(_WIN32) && !defined(__CYGWIN__)
  26. #include <io.h>
  27. #include <sys/locking.h>
  28. #define mkdir(path, mode) mkdir(path)
  29. #if !defined(__MINGW32__)
  30. #define ftruncate(fd, length) _chsize(fd, length)
  31. #endif
  32. #endif
  33. #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__)
  34. #include <direct.h>
  35. static char * dirname(char * path)
  36. {
  37. char drive[_MAX_DRIVE];
  38. char dir[_MAX_DIR];
  39. /* Remove trailing slash */
  40. while (strlen(path) > 0 && (*(path+strlen(path)-1) == '/' || *(path+strlen(path)-1) == '\\'))
  41. *(path+strlen(path)-1) = '\0';
  42. _splitpath(path, drive, dir, NULL, NULL);
  43. _makepath(path, drive, dir, NULL, NULL);
  44. return path;
  45. }
  46. #else
  47. #include <libgen.h>
  48. #endif
  49. /* Function with behaviour like `mkdir -p'. This function was adapted from
  50. * http://niallohiggins.com/2009/01/08/mkpath-mkdir-p-alike-in-c-for-unix/ */
  51. int _starpu_mkpath(const char *s, mode_t mode)
  52. {
  53. int olderrno;
  54. char *q, *r = NULL, *path = NULL, *up = NULL;
  55. int rv;
  56. rv = -1;
  57. if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0
  58. #if defined(_WIN32)
  59. /* C:/ or C:\ */
  60. || (s[0] && s[1] == ':' && (s[2] == '/' || s[2] == '\\') && !s[3])
  61. #endif
  62. )
  63. return 0;
  64. if ((path = strdup(s)) == NULL)
  65. STARPU_ABORT();
  66. if ((q = strdup(s)) == NULL)
  67. STARPU_ABORT();
  68. if ((r = dirname(q)) == NULL)
  69. goto out;
  70. if ((up = strdup(r)) == NULL)
  71. STARPU_ABORT();
  72. if ((_starpu_mkpath(up, mode) == -1) && (errno != EEXIST))
  73. goto out;
  74. struct stat sb;
  75. if (stat(path, &sb) == 0)
  76. {
  77. if (!S_ISDIR(sb.st_mode))
  78. {
  79. fprintf(stderr,"Error: %s is not a directory:\n", path);
  80. STARPU_ABORT();
  81. }
  82. /* It already exists and is a directory. */
  83. rv = 0;
  84. }
  85. else
  86. {
  87. if ((mkdir(path, mode) == -1) && (errno != EEXIST))
  88. rv = -1;
  89. else
  90. rv = 0;
  91. }
  92. out:
  93. olderrno = errno;
  94. if (up)
  95. free(up);
  96. free(q);
  97. free(path);
  98. errno = olderrno;
  99. return rv;
  100. }
  101. void _starpu_mkpath_and_check(const char *path, mode_t mode)
  102. {
  103. int ret;
  104. ret = _starpu_mkpath(path, mode);
  105. if (ret == -1 && errno != EEXIST)
  106. {
  107. fprintf(stderr,"Error making StarPU directory %s:\n", path);
  108. perror("mkdir");
  109. STARPU_ABORT();
  110. }
  111. }
  112. int _starpu_ftruncate(FILE *file)
  113. {
  114. return ftruncate(fileno(file), 0);
  115. }
  116. int _starpu_frdlock(FILE *file)
  117. {
  118. #if defined(_WIN32) && !defined(__CYGWIN__)
  119. int ret;
  120. do {
  121. ret = _locking(fileno(file), _LK_RLCK, 10);
  122. } while (ret == EDEADLOCK);
  123. return ret;
  124. #else
  125. struct flock lock = {
  126. .l_type = F_RDLCK,
  127. .l_whence = SEEK_SET,
  128. .l_start = 0,
  129. .l_len = 0
  130. };
  131. return fcntl(fileno(file), F_SETLKW, &lock);
  132. #endif
  133. }
  134. int _starpu_frdunlock(FILE *file)
  135. {
  136. #if defined(_WIN32) && !defined(__CYGWIN__)
  137. # ifndef _LK_UNLCK
  138. # define _LK_UNLCK _LK_UNLOCK
  139. # endif
  140. return _locking(fileno(file), _LK_UNLCK, 10);
  141. #else
  142. struct flock lock = {
  143. .l_type = F_UNLCK,
  144. .l_whence = SEEK_SET,
  145. .l_start = 0,
  146. .l_len = 0
  147. };
  148. return fcntl(fileno(file), F_SETLKW, &lock);
  149. #endif
  150. }
  151. int _starpu_fwrlock(FILE *file)
  152. {
  153. #if defined(_WIN32) && !defined(__CYGWIN__)
  154. int ret;
  155. do {
  156. ret = _locking(fileno(file), _LK_LOCK, 10);
  157. } while (ret == EDEADLOCK);
  158. return ret;
  159. #else
  160. struct flock lock = {
  161. .l_type = F_WRLCK,
  162. .l_whence = SEEK_SET,
  163. .l_start = 0,
  164. .l_len = 0
  165. };
  166. return fcntl(fileno(file), F_SETLKW, &lock);
  167. #endif
  168. }
  169. int _starpu_fwrunlock(FILE *file)
  170. {
  171. return _starpu_frdunlock(file);
  172. }
  173. int _starpu_check_mutex_deadlock(starpu_pthread_mutex_t *mutex)
  174. {
  175. int ret;
  176. ret = starpu_pthread_mutex_trylock(mutex);
  177. if (!ret)
  178. {
  179. STARPU_PTHREAD_MUTEX_UNLOCK(mutex);
  180. return 0;
  181. }
  182. if (ret == EBUSY)
  183. return 0;
  184. STARPU_ASSERT (ret != EDEADLK);
  185. return 1;
  186. }
  187. char *_starpu_get_home_path(void)
  188. {
  189. char *path = getenv("XDG_CACHE_HOME");
  190. if (!path)
  191. path = getenv("STARPU_HOME");
  192. if (!path)
  193. path = getenv("HOME");
  194. if (!path)
  195. path = getenv("USERPROFILE");
  196. if (!path) {
  197. static int warn;
  198. if (!warn) {
  199. warn = 1;
  200. _STARPU_DISP("couldn't find a $STARPU_HOME place to put .starpu data, using /tmp\n");
  201. }
  202. path = "/tmp";
  203. }
  204. return path;
  205. }
  206. void _starpu_gethostname(char *hostname, size_t size)
  207. {
  208. char *forced_hostname = getenv("STARPU_HOSTNAME");
  209. if (forced_hostname && forced_hostname[0])
  210. {
  211. snprintf(hostname, size-1, "%s", forced_hostname);
  212. hostname[size-1] = 0;
  213. }
  214. else
  215. {
  216. char *c;
  217. gethostname(hostname, size-1);
  218. hostname[size-1] = 0;
  219. c = strchr(hostname, '.');
  220. if (c)
  221. *c = 0;
  222. }
  223. }
  224. void _starpu_sleep(struct timespec ts)
  225. {
  226. #ifdef STARPU_SIMGRID
  227. MSG_process_sleep(ts.tv_sec + ts.tv_nsec / 1000000000.);
  228. #elif defined(STARPU_HAVE_WINDOWS)
  229. Sleep((ts.tv_sec * 1000) + (ts.tv_nsec / 1000000));
  230. #else
  231. struct timespec req, rem;
  232. req = ts;
  233. while (nanosleep(&req, &rem))
  234. req = rem;
  235. #endif
  236. }