utils.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012-2017 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 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. #if !defined(O_DIRECT) && defined(F_NOCACHE)
  38. #define O_DIRECT F_NOCACHE
  39. #endif
  40. int _starpu_silent;
  41. void _starpu_util_init(void)
  42. {
  43. _starpu_silent = starpu_get_env_number_default("STARPU_SILENT", 0);
  44. STARPU_HG_DISABLE_CHECKING(_starpu_silent);
  45. }
  46. #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__)
  47. #include <direct.h>
  48. static char * dirname(char * path)
  49. {
  50. char drive[_MAX_DRIVE];
  51. char dir[_MAX_DIR];
  52. /* Remove trailing slash */
  53. while (strlen(path) > 0 && (*(path+strlen(path)-1) == '/' || *(path+strlen(path)-1) == '\\'))
  54. *(path+strlen(path)-1) = '\0';
  55. _splitpath(path, drive, dir, NULL, NULL);
  56. _makepath(path, drive, dir, NULL, NULL);
  57. return path;
  58. }
  59. #else
  60. #include <libgen.h>
  61. #endif
  62. /* Function with behaviour like `mkdir -p'. This function was adapted from
  63. * http://niallohiggins.com/2009/01/08/mkpath-mkdir-p-alike-in-c-for-unix/ */
  64. int _starpu_mkpath(const char *s, mode_t mode)
  65. {
  66. int olderrno;
  67. char *q, *r = NULL, *path = NULL, *up = NULL;
  68. int rv;
  69. rv = -1;
  70. if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0
  71. #if defined(_WIN32)
  72. /* C:/ or C:\ */
  73. || (s[0] && s[1] == ':' && (s[2] == '/' || s[2] == '\\') && !s[3])
  74. #endif
  75. )
  76. return 0;
  77. if ((path = strdup(s)) == NULL)
  78. STARPU_ABORT();
  79. if ((q = strdup(s)) == NULL)
  80. STARPU_ABORT();
  81. if ((r = dirname(q)) == NULL)
  82. goto out;
  83. if ((up = strdup(r)) == NULL)
  84. STARPU_ABORT();
  85. if ((_starpu_mkpath(up, mode) == -1) && (errno != EEXIST))
  86. goto out;
  87. struct stat sb;
  88. if (stat(path, &sb) == 0)
  89. {
  90. if (!S_ISDIR(sb.st_mode))
  91. {
  92. _STARPU_MSG("Error: %s is not a directory:\n", path);
  93. STARPU_ABORT();
  94. }
  95. /* It already exists and is a directory. */
  96. rv = 0;
  97. }
  98. else
  99. {
  100. if ((mkdir(path, mode) == -1) && (errno != EEXIST))
  101. rv = -1;
  102. else
  103. rv = 0;
  104. }
  105. out:
  106. olderrno = errno;
  107. if (up)
  108. free(up);
  109. free(q);
  110. free(path);
  111. errno = olderrno;
  112. return rv;
  113. }
  114. void _starpu_mkpath_and_check(const char *path, mode_t mode)
  115. {
  116. int ret;
  117. ret = _starpu_mkpath(path, mode);
  118. if (ret == -1 && errno != EEXIST)
  119. {
  120. _STARPU_MSG("Error making StarPU directory %s:\n", path);
  121. perror("mkdir");
  122. STARPU_ABORT();
  123. }
  124. }
  125. char *_starpu_mktemp(const char *directory, int flags, int *fd)
  126. {
  127. /* create template for mkstemp */
  128. const char *tmp = "STARPU_XXXXXX";
  129. char *baseCpy;
  130. _STARPU_MALLOC(baseCpy, strlen(directory)+1+strlen(tmp)+1);
  131. STARPU_ASSERT(baseCpy != NULL);
  132. strcpy(baseCpy, directory);
  133. strcat(baseCpy,"/");
  134. strcat(baseCpy,tmp);
  135. #if defined(STARPU_HAVE_WINDOWS)
  136. _mktemp(baseCpy);
  137. *fd = open(baseCpy, flags);
  138. #elif defined (HAVE_MKOSTEMP)
  139. *fd = mkostemp(baseCpy, flags);
  140. #else
  141. # ifdef O_DIRECT
  142. STARPU_ASSERT(flags == (O_RDWR | O_BINARY) || flags == (O_RDWR | O_BINARY | O_DIRECT));
  143. # else
  144. STARPU_ASSERT(flags == (O_RDWR | O_BINARY));
  145. # endif
  146. *fd = mkstemp(baseCpy);
  147. #endif
  148. /* fail */
  149. if (*fd < 0)
  150. {
  151. int err = errno;
  152. _STARPU_DISP("Could not create temporary file in directory '%s', mskostemp failed with error '%s'\n", directory, strerror(errno));
  153. free(baseCpy);
  154. errno = err;
  155. return NULL;
  156. }
  157. #if !defined(STARPU_HAVE_WINDOWS) && !defined (HAVE_MKOSTEMP) && defined(O_DIRECT)
  158. /* Add O_DIRECT after the mkstemp call */
  159. if ((flags & O_DIRECT) != 0)
  160. {
  161. int flag = fcntl(*fd, F_GETFL);
  162. flag |= O_DIRECT;
  163. if (fcntl(*fd, F_SETFL, flag) < 0)
  164. {
  165. int err = errno;
  166. _STARPU_DISP("Could set O_DIRECT on the temporary file in directory '%s', fcntl failed with error '%s'\n", directory, strerror(errno));
  167. free(baseCpy);
  168. errno = err;
  169. return NULL;
  170. }
  171. }
  172. #endif
  173. return baseCpy;
  174. }
  175. char *_starpu_mktemp_many(const char *directory, int depth, int flags, int *fd)
  176. {
  177. size_t len = strlen(directory);
  178. char path[len + depth*4 + 1];
  179. int i;
  180. struct stat sb;
  181. if (stat(directory, &sb) != 0)
  182. {
  183. _STARPU_DISP("Directory '%s' does not exist\n", directory);
  184. return NULL;
  185. }
  186. if (!S_ISDIR(sb.st_mode))
  187. {
  188. _STARPU_DISP("'%s' is not a directory\n", directory);
  189. return NULL;
  190. }
  191. memcpy(path, directory, len);
  192. for (i = 0; i < depth; i++)
  193. {
  194. int r = starpu_lrand48();
  195. int ret;
  196. path[len + i*4 + 0] = '/';
  197. path[len + i*4 + 1] = '0' + (r/1)%10;
  198. path[len + i*4 + 2] = '0' + (r/10)%10;
  199. path[len + i*4 + 3] = '0' + (r/100)%10;
  200. path[len + i*4 + 4] = 0;
  201. ret = mkdir(path, 0777);
  202. if (ret == 0)
  203. continue;
  204. if (errno == EEXIST)
  205. continue;
  206. if (errno == ENOENT)
  207. {
  208. /* D'oh, somebody removed our directories in between,
  209. * restart from scratch */
  210. i = -1;
  211. continue;
  212. }
  213. _STARPU_DISP("Could not create temporary directory '%s', mkdir failed with error '%s'\n", path, strerror(errno));
  214. return NULL;
  215. }
  216. return _starpu_mktemp(path, flags, fd);
  217. }
  218. void _starpu_rmtemp_many(char *path, int depth)
  219. {
  220. int i;
  221. for (i = 0; i < depth; i++)
  222. {
  223. path = dirname(path);
  224. if (rmdir(path) < 0 && errno != ENOTEMPTY && errno != EBUSY)
  225. _STARPU_DISP("Could not remove temporary directory '%s', rmdir failed with error '%s'\n", path, strerror(errno));
  226. }
  227. }
  228. int _starpu_ftruncate(int fd, size_t length)
  229. {
  230. return ftruncate(fd, length);
  231. }
  232. int _starpu_fftruncate(FILE *file, size_t length)
  233. {
  234. return ftruncate(fileno(file), length);
  235. }
  236. static int _starpu_warn_nolock(int err)
  237. {
  238. if (0
  239. #ifdef ENOLCK
  240. || err == ENOLCK
  241. #endif
  242. #ifdef ENOTSUP
  243. || err == ENOTSUP
  244. #endif
  245. #ifdef EOPNOTSUPP
  246. || err == EOPNOTSUPP
  247. #endif
  248. #ifdef EROFS
  249. || err == EROFS
  250. #endif
  251. )
  252. {
  253. static int warn;
  254. if (!warn) {
  255. warn = 1;
  256. _STARPU_DISP("warning: Couldn't lock performance file, StarPU home (%s, coming from $HOME or $STARPU_HOME) is probably on some network filesystem like NFS which does not support locking.\n", _starpu_get_home_path());
  257. }
  258. return 1;
  259. }
  260. return 0;
  261. }
  262. int _starpu_frdlock(FILE *file)
  263. {
  264. int ret;
  265. #if defined(_WIN32) && !defined(__CYGWIN__)
  266. do
  267. {
  268. ret = _locking(fileno(file), _LK_RLCK, 10);
  269. }
  270. while (ret == EDEADLOCK);
  271. #else
  272. struct flock lock =
  273. {
  274. .l_type = F_RDLCK,
  275. .l_whence = SEEK_SET,
  276. .l_start = 0,
  277. .l_len = 0
  278. };
  279. ret = fcntl(fileno(file), F_SETLKW, &lock);
  280. #endif
  281. if (ret != 0 && _starpu_warn_nolock(errno))
  282. return -1;
  283. STARPU_ASSERT(ret == 0);
  284. return ret;
  285. }
  286. int _starpu_frdunlock(FILE *file)
  287. {
  288. int ret;
  289. #if defined(_WIN32) && !defined(__CYGWIN__)
  290. # ifndef _LK_UNLCK
  291. # define _LK_UNLCK _LK_UNLOCK
  292. # endif
  293. ret = _lseek(fileno(file), 0, SEEK_SET);
  294. STARPU_ASSERT(ret == 0);
  295. ret = _locking(fileno(file), _LK_UNLCK, 10);
  296. #else
  297. struct flock lock =
  298. {
  299. .l_type = F_UNLCK,
  300. .l_whence = SEEK_SET,
  301. .l_start = 0,
  302. .l_len = 0
  303. };
  304. ret = fcntl(fileno(file), F_SETLKW, &lock);
  305. #endif
  306. if (ret != 0 && _starpu_warn_nolock(errno))
  307. return -1;
  308. STARPU_ASSERT(ret == 0);
  309. return ret;
  310. }
  311. int _starpu_fwrlock(FILE *file)
  312. {
  313. int ret;
  314. #if defined(_WIN32) && !defined(__CYGWIN__)
  315. ret = _lseek(fileno(file), 0, SEEK_SET);
  316. STARPU_ASSERT(ret == 0);
  317. do
  318. {
  319. ret = _locking(fileno(file), _LK_LOCK, 10);
  320. }
  321. while (ret == EDEADLOCK);
  322. #else
  323. struct flock lock =
  324. {
  325. .l_type = F_WRLCK,
  326. .l_whence = SEEK_SET,
  327. .l_start = 0,
  328. .l_len = 0
  329. };
  330. ret = fcntl(fileno(file), F_SETLKW, &lock);
  331. #endif
  332. if (ret != 0 && _starpu_warn_nolock(errno))
  333. return -1;
  334. STARPU_ASSERT(ret == 0);
  335. return ret;
  336. }
  337. int _starpu_fwrunlock(FILE *file)
  338. {
  339. return _starpu_frdunlock(file);
  340. }
  341. int _starpu_check_mutex_deadlock(starpu_pthread_mutex_t *mutex)
  342. {
  343. int ret;
  344. ret = starpu_pthread_mutex_trylock(mutex);
  345. if (!ret)
  346. {
  347. STARPU_PTHREAD_MUTEX_UNLOCK(mutex);
  348. return 0;
  349. }
  350. if (ret == EBUSY)
  351. return 0;
  352. STARPU_ASSERT (ret != EDEADLK);
  353. return 1;
  354. }
  355. char *_starpu_get_home_path(void)
  356. {
  357. char *path = starpu_getenv("XDG_CACHE_HOME");
  358. if (!path)
  359. path = starpu_getenv("STARPU_HOME");
  360. #ifdef _WIN32
  361. if (!path)
  362. path = starpu_getenv("LOCALAPPDATA");
  363. if (!path)
  364. path = starpu_getenv("USERPROFILE");
  365. #endif
  366. if (!path)
  367. path = starpu_getenv("HOME");
  368. if (!path)
  369. {
  370. static int warn;
  371. path = starpu_getenv("TMPDIR");
  372. if (!path)
  373. path = "/tmp";
  374. if (!warn)
  375. {
  376. warn = 1;
  377. _STARPU_DISP("couldn't find a $STARPU_HOME place to put .starpu data, using %s\n", path);
  378. }
  379. }
  380. return path;
  381. }
  382. void _starpu_gethostname(char *hostname, size_t size)
  383. {
  384. char *forced_hostname = starpu_getenv("STARPU_HOSTNAME");
  385. if (forced_hostname && forced_hostname[0])
  386. {
  387. snprintf(hostname, size-1, "%s", forced_hostname);
  388. hostname[size-1] = 0;
  389. }
  390. else
  391. {
  392. char *c;
  393. gethostname(hostname, size-1);
  394. hostname[size-1] = 0;
  395. c = strchr(hostname, '.');
  396. if (c)
  397. *c = 0;
  398. }
  399. }
  400. void starpu_sleep(float nb_sec)
  401. {
  402. #ifdef STARPU_SIMGRID
  403. MSG_process_sleep(nb_sec);
  404. #elif defined(STARPU_HAVE_WINDOWS)
  405. Sleep(nb_sec * 1000);
  406. #else
  407. struct timespec req, rem;
  408. req.tv_sec = nb_sec;
  409. req.tv_nsec = (nb_sec - (float) req.tv_sec) * 1000000000;
  410. while (nanosleep(&req, &rem))
  411. req = rem;
  412. #endif
  413. }
  414. char *starpu_getenv(const char *str)
  415. {
  416. #ifndef STARPU_SIMGRID
  417. #if defined(STARPU_DEVEL) || defined(STARPU_DEBUG)
  418. struct _starpu_worker * worker;
  419. worker = _starpu_get_local_worker_key();
  420. if (worker && worker->worker_is_initialized)
  421. _STARPU_DISP( "getenv should not be called from running workers, only for main() or worker initialization, since it is not reentrant\n");
  422. #endif
  423. #endif
  424. return getenv(str);
  425. }