utils.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015 CNRS
  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 <core/workers.h>
  21. #include <errno.h>
  22. #ifdef HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #endif
  25. #include <fcntl.h>
  26. #if defined(_WIN32) && !defined(__CYGWIN__)
  27. #include <io.h>
  28. #include <sys/locking.h>
  29. #define mkdir(path, mode) mkdir(path)
  30. #if !defined(__MINGW32__)
  31. #define ftruncate(fd, length) _chsize(fd, length)
  32. #endif
  33. #endif
  34. #ifndef O_BINARY
  35. #define O_BINARY 0
  36. #endif
  37. int _starpu_silent;
  38. void _starpu_util_init(void)
  39. {
  40. _starpu_silent = starpu_get_env_number_default("STARPU_SILENT", 0);
  41. STARPU_HG_DISABLE_CHECKING(_starpu_silent);
  42. }
  43. #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__)
  44. #include <direct.h>
  45. static char * dirname(char * path)
  46. {
  47. char drive[_MAX_DRIVE];
  48. char dir[_MAX_DIR];
  49. /* Remove trailing slash */
  50. while (strlen(path) > 0 && (*(path+strlen(path)-1) == '/' || *(path+strlen(path)-1) == '\\'))
  51. *(path+strlen(path)-1) = '\0';
  52. _splitpath(path, drive, dir, NULL, NULL);
  53. _makepath(path, drive, dir, NULL, NULL);
  54. return path;
  55. }
  56. #else
  57. #include <libgen.h>
  58. #endif
  59. /* Function with behaviour like `mkdir -p'. This function was adapted from
  60. * http://niallohiggins.com/2009/01/08/mkpath-mkdir-p-alike-in-c-for-unix/ */
  61. int _starpu_mkpath(const char *s, mode_t mode)
  62. {
  63. int olderrno;
  64. char *q, *r = NULL, *path = NULL, *up = NULL;
  65. int rv;
  66. rv = -1;
  67. if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0
  68. #if defined(_WIN32)
  69. /* C:/ or C:\ */
  70. || (s[0] && s[1] == ':' && (s[2] == '/' || s[2] == '\\') && !s[3])
  71. #endif
  72. )
  73. return 0;
  74. if ((path = strdup(s)) == NULL)
  75. STARPU_ABORT();
  76. if ((q = strdup(s)) == NULL)
  77. STARPU_ABORT();
  78. if ((r = dirname(q)) == NULL)
  79. goto out;
  80. if ((up = strdup(r)) == NULL)
  81. STARPU_ABORT();
  82. if ((_starpu_mkpath(up, mode) == -1) && (errno != EEXIST))
  83. goto out;
  84. struct stat sb;
  85. if (stat(path, &sb) == 0)
  86. {
  87. if (!S_ISDIR(sb.st_mode))
  88. {
  89. fprintf(stderr,"Error: %s is not a directory:\n", path);
  90. STARPU_ABORT();
  91. }
  92. /* It already exists and is a directory. */
  93. rv = 0;
  94. }
  95. else
  96. {
  97. if ((mkdir(path, mode) == -1) && (errno != EEXIST))
  98. rv = -1;
  99. else
  100. rv = 0;
  101. }
  102. out:
  103. olderrno = errno;
  104. if (up)
  105. free(up);
  106. free(q);
  107. free(path);
  108. errno = olderrno;
  109. return rv;
  110. }
  111. void _starpu_mkpath_and_check(const char *path, mode_t mode)
  112. {
  113. int ret;
  114. ret = _starpu_mkpath(path, mode);
  115. if (ret == -1 && errno != EEXIST)
  116. {
  117. fprintf(stderr,"Error making StarPU directory %s:\n", path);
  118. perror("mkdir");
  119. STARPU_ABORT();
  120. }
  121. }
  122. char *_starpu_mktemp(const char *directory, int flags, int *fd)
  123. {
  124. /* create template for mkstemp */
  125. const char *tmp = "STARPU_XXXXXX";
  126. char *baseCpy = malloc(strlen(directory)+1+strlen(tmp)+1);
  127. STARPU_ASSERT(baseCpy != NULL);
  128. strcpy(baseCpy, directory);
  129. strcat(baseCpy,"/");
  130. strcat(baseCpy,tmp);
  131. #if defined(STARPU_HAVE_WINDOWS)
  132. _mktemp(baseCpy);
  133. *fd = open(baseCpy, flags);
  134. #elif defined (HAVE_MKOSTEMP)
  135. *fd = mkostemp(baseCpy, flags);
  136. #else
  137. STARPU_ASSERT(flags == (O_RDWR | O_BINARY));
  138. *fd = mkstemp(baseCpy);
  139. #endif
  140. /* fail */
  141. if (*fd < 0)
  142. {
  143. _STARPU_DISP("Could not create temporary file in directory '%s', mskostemp failed with error '%s'\n", directory, strerror(errno));
  144. free(baseCpy);
  145. return NULL;
  146. }
  147. return baseCpy;
  148. }
  149. char *_starpu_mktemp_many(const char *directory, int depth, int flags, int *fd)
  150. {
  151. size_t len = strlen(directory);
  152. char path[len + depth*4 + 1];
  153. int i;
  154. memcpy(path, directory, len);
  155. for (i = 0; i < depth; i++)
  156. {
  157. int r = starpu_lrand48();
  158. path[len + i*4 + 0] = '/';
  159. path[len + i*4 + 1] = '0' + (r/1)%10;
  160. path[len + i*4 + 2] = '0' + (r/10)%10;
  161. path[len + i*4 + 3] = '0' + (r/100)%10;
  162. path[len + i*4 + 4] = 0;
  163. if (mkdir(path, 0777) < 0 && errno != EEXIST)
  164. {
  165. _STARPU_DISP("Could not create temporary directory '%s', mkdir failed with error '%s'\n", path, strerror(errno));
  166. return NULL;
  167. }
  168. }
  169. return _starpu_mktemp(path, flags, fd);
  170. }
  171. void _starpu_rmtemp_many(char *path, int depth)
  172. {
  173. int i;
  174. for (i = 0; i < depth; i++)
  175. {
  176. path = dirname(path);
  177. if (rmdir(path) < 0 && errno != ENOTEMPTY && errno != EBUSY)
  178. _STARPU_DISP("Could not remove temporary directory '%s', rmdir failed with error '%s'\n", path, strerror(errno));
  179. }
  180. }
  181. int _starpu_ftruncate(FILE *file)
  182. {
  183. return ftruncate(fileno(file), 0);
  184. }
  185. int _starpu_frdlock(FILE *file)
  186. {
  187. int ret;
  188. #if defined(_WIN32) && !defined(__CYGWIN__)
  189. do
  190. {
  191. ret = _locking(fileno(file), _LK_RLCK, 10);
  192. }
  193. while (ret == EDEADLOCK);
  194. #else
  195. struct flock lock =
  196. {
  197. .l_type = F_RDLCK,
  198. .l_whence = SEEK_SET,
  199. .l_start = 0,
  200. .l_len = 0
  201. };
  202. ret = fcntl(fileno(file), F_SETLKW, &lock);
  203. #endif
  204. STARPU_ASSERT(ret == 0);
  205. return ret;
  206. }
  207. int _starpu_frdunlock(FILE *file)
  208. {
  209. int ret;
  210. #if defined(_WIN32) && !defined(__CYGWIN__)
  211. # ifndef _LK_UNLCK
  212. # define _LK_UNLCK _LK_UNLOCK
  213. # endif
  214. ret = _locking(fileno(file), _LK_UNLCK, 10);
  215. #else
  216. struct flock lock =
  217. {
  218. .l_type = F_UNLCK,
  219. .l_whence = SEEK_SET,
  220. .l_start = 0,
  221. .l_len = 0
  222. };
  223. ret = fcntl(fileno(file), F_SETLKW, &lock);
  224. #endif
  225. STARPU_ASSERT(ret == 0);
  226. return ret;
  227. }
  228. int _starpu_fwrlock(FILE *file)
  229. {
  230. int ret;
  231. #if defined(_WIN32) && !defined(__CYGWIN__)
  232. do
  233. {
  234. ret = _locking(fileno(file), _LK_LOCK, 10);
  235. }
  236. while (ret == EDEADLOCK);
  237. #else
  238. struct flock lock =
  239. {
  240. .l_type = F_WRLCK,
  241. .l_whence = SEEK_SET,
  242. .l_start = 0,
  243. .l_len = 0
  244. };
  245. ret = fcntl(fileno(file), F_SETLKW, &lock);
  246. #endif
  247. STARPU_ASSERT(ret == 0);
  248. return ret;
  249. }
  250. int _starpu_fwrunlock(FILE *file)
  251. {
  252. return _starpu_frdunlock(file);
  253. }
  254. int _starpu_check_mutex_deadlock(starpu_pthread_mutex_t *mutex)
  255. {
  256. int ret;
  257. ret = starpu_pthread_mutex_trylock(mutex);
  258. if (!ret)
  259. {
  260. STARPU_PTHREAD_MUTEX_UNLOCK(mutex);
  261. return 0;
  262. }
  263. if (ret == EBUSY)
  264. return 0;
  265. STARPU_ASSERT (ret != EDEADLK);
  266. return 1;
  267. }
  268. char *_starpu_get_home_path(void)
  269. {
  270. char *path = starpu_getenv("XDG_CACHE_HOME");
  271. if (!path)
  272. path = starpu_getenv("STARPU_HOME");
  273. if (!path)
  274. path = starpu_getenv("HOME");
  275. if (!path)
  276. path = starpu_getenv("USERPROFILE");
  277. if (!path)
  278. {
  279. static int warn;
  280. if (!warn)
  281. {
  282. warn = 1;
  283. _STARPU_DISP("couldn't find a $STARPU_HOME place to put .starpu data, using /tmp\n");
  284. }
  285. path = "/tmp";
  286. }
  287. return path;
  288. }
  289. void _starpu_gethostname(char *hostname, size_t size)
  290. {
  291. char *forced_hostname = starpu_getenv("STARPU_HOSTNAME");
  292. if (forced_hostname && forced_hostname[0])
  293. {
  294. snprintf(hostname, size-1, "%s", forced_hostname);
  295. hostname[size-1] = 0;
  296. }
  297. else
  298. {
  299. char *c;
  300. gethostname(hostname, size-1);
  301. hostname[size-1] = 0;
  302. c = strchr(hostname, '.');
  303. if (c)
  304. *c = 0;
  305. }
  306. }
  307. void _starpu_sleep(struct timespec ts)
  308. {
  309. #ifdef STARPU_SIMGRID
  310. MSG_process_sleep(ts.tv_sec + ts.tv_nsec / 1000000000.);
  311. #elif defined(STARPU_HAVE_WINDOWS)
  312. Sleep((ts.tv_sec * 1000) + (ts.tv_nsec / 1000000));
  313. #else
  314. struct timespec req, rem;
  315. req = ts;
  316. while (nanosleep(&req, &rem))
  317. req = rem;
  318. #endif
  319. }
  320. char *starpu_getenv(const char *str)
  321. {
  322. #ifndef STARPU_SIMGRID
  323. #if defined(STARPU_DEVEL) || defined(STARPU_DEBUG)
  324. struct _starpu_worker * worker;
  325. worker = _starpu_get_local_worker_key();
  326. if (worker && worker->worker_is_initialized)
  327. _STARPU_DISP( "getenv should not be called from running workers, only for main() or worker initialization, since it is not reentrant\n");
  328. #endif
  329. #endif
  330. return getenv(str);
  331. }