utils.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. int err = errno;
  144. _STARPU_DISP("Could not create temporary file in directory '%s', mskostemp failed with error '%s'\n", directory, strerror(errno));
  145. free(baseCpy);
  146. errno = err;
  147. return NULL;
  148. }
  149. return baseCpy;
  150. }
  151. char *_starpu_mktemp_many(const char *directory, int depth, int flags, int *fd)
  152. {
  153. size_t len = strlen(directory);
  154. char path[len + depth*4 + 1];
  155. int i;
  156. struct stat sb;
  157. if (stat(directory, &sb) != 0)
  158. {
  159. _STARPU_DISP("Directory '%s' does not exist\n", directory);
  160. return NULL;
  161. }
  162. if (!S_ISDIR(sb.st_mode))
  163. {
  164. _STARPU_DISP("'%s' is not a directory\n", directory);
  165. return NULL;
  166. }
  167. memcpy(path, directory, len);
  168. for (i = 0; i < depth; i++)
  169. {
  170. int r = starpu_lrand48();
  171. int ret;
  172. path[len + i*4 + 0] = '/';
  173. path[len + i*4 + 1] = '0' + (r/1)%10;
  174. path[len + i*4 + 2] = '0' + (r/10)%10;
  175. path[len + i*4 + 3] = '0' + (r/100)%10;
  176. path[len + i*4 + 4] = 0;
  177. ret = mkdir(path, 0777);
  178. if (ret == 0)
  179. continue;
  180. if (errno == EEXIST)
  181. continue;
  182. if (errno == ENOENT)
  183. {
  184. /* D'oh, somebody removed our directories in between,
  185. * restart from scratch */
  186. i = -1;
  187. continue;
  188. }
  189. _STARPU_DISP("Could not create temporary directory '%s', mkdir failed with error '%s'\n", path, strerror(errno));
  190. return NULL;
  191. }
  192. return _starpu_mktemp(path, flags, fd);
  193. }
  194. void _starpu_rmtemp_many(char *path, int depth)
  195. {
  196. int i;
  197. for (i = 0; i < depth; i++)
  198. {
  199. path = dirname(path);
  200. if (rmdir(path) < 0 && errno != ENOTEMPTY && errno != EBUSY)
  201. _STARPU_DISP("Could not remove temporary directory '%s', rmdir failed with error '%s'\n", path, strerror(errno));
  202. }
  203. }
  204. int _starpu_ftruncate(int fd, size_t length)
  205. {
  206. return ftruncate(fd, length);
  207. }
  208. int _starpu_fftruncate(FILE *file, size_t length)
  209. {
  210. return ftruncate(fileno(file), length);
  211. }
  212. int _starpu_frdlock(FILE *file)
  213. {
  214. int ret;
  215. #if defined(_WIN32) && !defined(__CYGWIN__)
  216. do
  217. {
  218. ret = _locking(fileno(file), _LK_RLCK, 10);
  219. }
  220. while (ret == EDEADLOCK);
  221. #else
  222. struct flock lock =
  223. {
  224. .l_type = F_RDLCK,
  225. .l_whence = SEEK_SET,
  226. .l_start = 0,
  227. .l_len = 0
  228. };
  229. ret = fcntl(fileno(file), F_SETLKW, &lock);
  230. #endif
  231. STARPU_ASSERT(ret == 0);
  232. return ret;
  233. }
  234. int _starpu_frdunlock(FILE *file)
  235. {
  236. int ret;
  237. #if defined(_WIN32) && !defined(__CYGWIN__)
  238. # ifndef _LK_UNLCK
  239. # define _LK_UNLCK _LK_UNLOCK
  240. # endif
  241. ret = _lseek(fileno(file), 0, SEEK_SET);
  242. STARPU_ASSERT(ret == 0);
  243. ret = _locking(fileno(file), _LK_UNLCK, 10);
  244. #else
  245. struct flock lock =
  246. {
  247. .l_type = F_UNLCK,
  248. .l_whence = SEEK_SET,
  249. .l_start = 0,
  250. .l_len = 0
  251. };
  252. ret = fcntl(fileno(file), F_SETLKW, &lock);
  253. #endif
  254. STARPU_ASSERT(ret == 0);
  255. return ret;
  256. }
  257. int _starpu_fwrlock(FILE *file)
  258. {
  259. int ret;
  260. #if defined(_WIN32) && !defined(__CYGWIN__)
  261. ret = _lseek(fileno(file), 0, SEEK_SET);
  262. STARPU_ASSERT(ret == 0);
  263. do
  264. {
  265. ret = _locking(fileno(file), _LK_LOCK, 10);
  266. }
  267. while (ret == EDEADLOCK);
  268. #else
  269. struct flock lock =
  270. {
  271. .l_type = F_WRLCK,
  272. .l_whence = SEEK_SET,
  273. .l_start = 0,
  274. .l_len = 0
  275. };
  276. ret = fcntl(fileno(file), F_SETLKW, &lock);
  277. #endif
  278. STARPU_ASSERT(ret == 0);
  279. return ret;
  280. }
  281. int _starpu_fwrunlock(FILE *file)
  282. {
  283. return _starpu_frdunlock(file);
  284. }
  285. int _starpu_check_mutex_deadlock(starpu_pthread_mutex_t *mutex)
  286. {
  287. int ret;
  288. ret = starpu_pthread_mutex_trylock(mutex);
  289. if (!ret)
  290. {
  291. STARPU_PTHREAD_MUTEX_UNLOCK(mutex);
  292. return 0;
  293. }
  294. if (ret == EBUSY)
  295. return 0;
  296. STARPU_ASSERT (ret != EDEADLK);
  297. return 1;
  298. }
  299. char *_starpu_get_home_path(void)
  300. {
  301. char *path = starpu_getenv("XDG_CACHE_HOME");
  302. if (!path)
  303. path = starpu_getenv("STARPU_HOME");
  304. #ifdef _WIN32
  305. if (!path)
  306. path = starpu_getenv("LOCALAPPDATA");
  307. if (!path)
  308. path = starpu_getenv("USERPROFILE");
  309. #endif
  310. if (!path)
  311. path = starpu_getenv("HOME");
  312. if (!path)
  313. {
  314. static int warn;
  315. path = starpu_getenv("TMPDIR");
  316. if (!path)
  317. path = "/tmp";
  318. if (!warn)
  319. {
  320. warn = 1;
  321. _STARPU_DISP("couldn't find a $STARPU_HOME place to put .starpu data, using %s\n", path);
  322. }
  323. }
  324. return path;
  325. }
  326. void _starpu_gethostname(char *hostname, size_t size)
  327. {
  328. char *forced_hostname = starpu_getenv("STARPU_HOSTNAME");
  329. if (forced_hostname && forced_hostname[0])
  330. {
  331. snprintf(hostname, size-1, "%s", forced_hostname);
  332. hostname[size-1] = 0;
  333. }
  334. else
  335. {
  336. char *c;
  337. gethostname(hostname, size-1);
  338. hostname[size-1] = 0;
  339. c = strchr(hostname, '.');
  340. if (c)
  341. *c = 0;
  342. }
  343. }
  344. void starpu_sleep(float nb_sec)
  345. {
  346. #ifdef STARPU_SIMGRID
  347. MSG_process_sleep(nb_sec);
  348. #elif defined(STARPU_HAVE_WINDOWS)
  349. Sleep(nb_sec * 1000);
  350. #else
  351. struct timespec req, rem;
  352. req.tv_sec = nb_sec;
  353. req.tv_nsec = (nb_sec - (float) req.tv_sec) * 1000000000;
  354. while (nanosleep(&req, &rem))
  355. req = rem;
  356. #endif
  357. }
  358. char *starpu_getenv(const char *str)
  359. {
  360. #ifndef STARPU_SIMGRID
  361. #if defined(STARPU_DEVEL) || defined(STARPU_DEBUG)
  362. struct _starpu_worker * worker;
  363. worker = _starpu_get_local_worker_key();
  364. if (worker && worker->worker_is_initialized)
  365. _STARPU_DISP( "getenv should not be called from running workers, only for main() or worker initialization, since it is not reentrant\n");
  366. #endif
  367. #endif
  368. return getenv(str);
  369. }