utils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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_mkdtemp_internal(char *tmpl)
  126. {
  127. int len = (int)strlen(tmpl);
  128. int i;
  129. int ret;
  130. struct stat sb;
  131. // Initialize template
  132. for(i=len-6 ; i<len ; i++)
  133. {
  134. STARPU_ASSERT_MSG(tmpl[i] == 'X', "Template must terminate by XXXXXX\n");
  135. tmpl[i] = 'a';
  136. }
  137. // Find directory which does not exist by looping over all 6 last characters
  138. i=len-6;
  139. ret = stat(tmpl, &sb);
  140. while (ret == 0 && S_ISDIR(sb.st_mode))
  141. {
  142. if (tmpl[i] == 'z')
  143. i++;
  144. if (i == len)
  145. {
  146. _STARPU_MSG("Error making StarPU temporary directory\n");
  147. return NULL;
  148. }
  149. tmpl[i] ++;
  150. ret = stat(tmpl, &sb);
  151. }
  152. // Create directory
  153. ret = _starpu_mkpath(tmpl, 0777);
  154. return ret == 0 ? tmpl : NULL;
  155. }
  156. char *_starpu_mkdtemp(char *tmpl)
  157. {
  158. #if defined(HAVE_MKDTEMP)
  159. return mkdtemp(tmpl);
  160. #else
  161. return _starpu_mkdtemp_internal(tmpl);
  162. #endif
  163. }
  164. char *_starpu_mktemp(const char *directory, int flags, int *fd)
  165. {
  166. /* create template for mkstemp */
  167. const char *tmp = "STARPU_XXXXXX";
  168. char *baseCpy;
  169. _STARPU_MALLOC(baseCpy, strlen(directory)+1+strlen(tmp)+1);
  170. STARPU_ASSERT(baseCpy != NULL);
  171. strcpy(baseCpy, directory);
  172. strcat(baseCpy,"/");
  173. strcat(baseCpy,tmp);
  174. #if defined(STARPU_HAVE_WINDOWS)
  175. _mktemp(baseCpy);
  176. *fd = open(baseCpy, flags);
  177. #elif defined (HAVE_MKOSTEMP)
  178. *fd = mkostemp(baseCpy, flags);
  179. #else
  180. # ifdef O_DIRECT
  181. STARPU_ASSERT(flags == (O_RDWR | O_BINARY) || flags == (O_RDWR | O_BINARY | O_DIRECT));
  182. # else
  183. STARPU_ASSERT(flags == (O_RDWR | O_BINARY));
  184. # endif
  185. *fd = mkstemp(baseCpy);
  186. #endif
  187. /* fail */
  188. if (*fd < 0)
  189. {
  190. int err = errno;
  191. _STARPU_DISP("Could not create temporary file in directory '%s', mskostemp failed with error '%s'\n", directory, strerror(errno));
  192. free(baseCpy);
  193. errno = err;
  194. return NULL;
  195. }
  196. #if !defined(STARPU_HAVE_WINDOWS) && !defined (HAVE_MKOSTEMP) && defined(O_DIRECT)
  197. /* Add O_DIRECT after the mkstemp call */
  198. if ((flags & O_DIRECT) != 0)
  199. {
  200. int flag = fcntl(*fd, F_GETFL);
  201. flag |= O_DIRECT;
  202. if (fcntl(*fd, F_SETFL, flag) < 0)
  203. {
  204. int err = errno;
  205. _STARPU_DISP("Could set O_DIRECT on the temporary file in directory '%s', fcntl failed with error '%s'\n", directory, strerror(errno));
  206. free(baseCpy);
  207. errno = err;
  208. return NULL;
  209. }
  210. }
  211. #endif
  212. return baseCpy;
  213. }
  214. char *_starpu_mktemp_many(const char *directory, int depth, int flags, int *fd)
  215. {
  216. size_t len = strlen(directory);
  217. char path[len + depth*4 + 1];
  218. int i;
  219. struct stat sb;
  220. if (stat(directory, &sb) != 0)
  221. {
  222. _STARPU_DISP("Directory '%s' does not exist\n", directory);
  223. return NULL;
  224. }
  225. if (!S_ISDIR(sb.st_mode))
  226. {
  227. _STARPU_DISP("'%s' is not a directory\n", directory);
  228. return NULL;
  229. }
  230. memcpy(path, directory, len);
  231. for (i = 0; i < depth; i++)
  232. {
  233. int r = starpu_lrand48();
  234. int ret;
  235. path[len + i*4 + 0] = '/';
  236. path[len + i*4 + 1] = '0' + (r/1)%10;
  237. path[len + i*4 + 2] = '0' + (r/10)%10;
  238. path[len + i*4 + 3] = '0' + (r/100)%10;
  239. path[len + i*4 + 4] = 0;
  240. ret = mkdir(path, 0777);
  241. if (ret == 0)
  242. continue;
  243. if (errno == EEXIST)
  244. continue;
  245. if (errno == ENOENT)
  246. {
  247. /* D'oh, somebody removed our directories in between,
  248. * restart from scratch */
  249. i = -1;
  250. continue;
  251. }
  252. _STARPU_DISP("Could not create temporary directory '%s', mkdir failed with error '%s'\n", path, strerror(errno));
  253. return NULL;
  254. }
  255. return _starpu_mktemp(path, flags, fd);
  256. }
  257. void _starpu_rmtemp_many(char *path, int depth)
  258. {
  259. int i;
  260. for (i = 0; i < depth; i++)
  261. {
  262. path = dirname(path);
  263. if (rmdir(path) < 0 && errno != ENOTEMPTY && errno != EBUSY)
  264. _STARPU_DISP("Could not remove temporary directory '%s', rmdir failed with error '%s'\n", path, strerror(errno));
  265. }
  266. }
  267. int _starpu_ftruncate(int fd, size_t length)
  268. {
  269. return ftruncate(fd, length);
  270. }
  271. int _starpu_fftruncate(FILE *file, size_t length)
  272. {
  273. return ftruncate(fileno(file), length);
  274. }
  275. static int _starpu_warn_nolock(int err)
  276. {
  277. if (0
  278. #ifdef ENOLCK
  279. || err == ENOLCK
  280. #endif
  281. #ifdef ENOTSUP
  282. || err == ENOTSUP
  283. #endif
  284. #ifdef EOPNOTSUPP
  285. || err == EOPNOTSUPP
  286. #endif
  287. #ifdef EROFS
  288. || err == EROFS
  289. #endif
  290. )
  291. {
  292. static int warn;
  293. if (!warn)
  294. {
  295. warn = 1;
  296. _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());
  297. }
  298. return 1;
  299. }
  300. return 0;
  301. }
  302. int _starpu_frdlock(FILE *file)
  303. {
  304. int ret;
  305. #if defined(_WIN32) && !defined(__CYGWIN__)
  306. do
  307. {
  308. ret = _locking(fileno(file), _LK_RLCK, 10);
  309. }
  310. while (ret == EDEADLOCK);
  311. #else
  312. struct flock lock =
  313. {
  314. .l_type = F_RDLCK,
  315. .l_whence = SEEK_SET,
  316. .l_start = 0,
  317. .l_len = 0
  318. };
  319. ret = fcntl(fileno(file), F_SETLKW, &lock);
  320. #endif
  321. if (ret != 0 && _starpu_warn_nolock(errno))
  322. return -1;
  323. STARPU_ASSERT(ret == 0);
  324. return ret;
  325. }
  326. int _starpu_frdunlock(FILE *file)
  327. {
  328. int ret;
  329. #if defined(_WIN32) && !defined(__CYGWIN__)
  330. # ifndef _LK_UNLCK
  331. # define _LK_UNLCK _LK_UNLOCK
  332. # endif
  333. ret = _lseek(fileno(file), 0, SEEK_SET);
  334. STARPU_ASSERT(ret == 0);
  335. ret = _locking(fileno(file), _LK_UNLCK, 10);
  336. #else
  337. struct flock lock =
  338. {
  339. .l_type = F_UNLCK,
  340. .l_whence = SEEK_SET,
  341. .l_start = 0,
  342. .l_len = 0
  343. };
  344. ret = fcntl(fileno(file), F_SETLKW, &lock);
  345. #endif
  346. if (ret != 0 && _starpu_warn_nolock(errno))
  347. return -1;
  348. STARPU_ASSERT(ret == 0);
  349. return ret;
  350. }
  351. int _starpu_fwrlock(FILE *file)
  352. {
  353. int ret;
  354. #if defined(_WIN32) && !defined(__CYGWIN__)
  355. ret = _lseek(fileno(file), 0, SEEK_SET);
  356. STARPU_ASSERT(ret == 0);
  357. do
  358. {
  359. ret = _locking(fileno(file), _LK_LOCK, 10);
  360. }
  361. while (ret == EDEADLOCK);
  362. #else
  363. struct flock lock =
  364. {
  365. .l_type = F_WRLCK,
  366. .l_whence = SEEK_SET,
  367. .l_start = 0,
  368. .l_len = 0
  369. };
  370. ret = fcntl(fileno(file), F_SETLKW, &lock);
  371. #endif
  372. if (ret != 0 && _starpu_warn_nolock(errno))
  373. return -1;
  374. STARPU_ASSERT(ret == 0);
  375. return ret;
  376. }
  377. int _starpu_fwrunlock(FILE *file)
  378. {
  379. return _starpu_frdunlock(file);
  380. }
  381. int _starpu_check_mutex_deadlock(starpu_pthread_mutex_t *mutex)
  382. {
  383. int ret;
  384. ret = starpu_pthread_mutex_trylock(mutex);
  385. if (!ret)
  386. {
  387. STARPU_PTHREAD_MUTEX_UNLOCK(mutex);
  388. return 0;
  389. }
  390. if (ret == EBUSY)
  391. return 0;
  392. STARPU_ASSERT (ret != EDEADLK);
  393. return 1;
  394. }
  395. char *_starpu_get_home_path(void)
  396. {
  397. char *path = starpu_getenv("XDG_CACHE_HOME");
  398. if (!path)
  399. path = starpu_getenv("STARPU_HOME");
  400. #ifdef _WIN32
  401. if (!path)
  402. path = starpu_getenv("LOCALAPPDATA");
  403. if (!path)
  404. path = starpu_getenv("USERPROFILE");
  405. #endif
  406. if (!path)
  407. path = starpu_getenv("HOME");
  408. if (!path)
  409. {
  410. static int warn;
  411. path = starpu_getenv("TMPDIR");
  412. if (!path)
  413. path = "/tmp";
  414. if (!warn)
  415. {
  416. warn = 1;
  417. _STARPU_DISP("couldn't find a $STARPU_HOME place to put .starpu data, using %s\n", path);
  418. }
  419. }
  420. return path;
  421. }
  422. void _starpu_gethostname(char *hostname, size_t size)
  423. {
  424. char *forced_hostname = starpu_getenv("STARPU_HOSTNAME");
  425. if (forced_hostname && forced_hostname[0])
  426. {
  427. snprintf(hostname, size-1, "%s", forced_hostname);
  428. hostname[size-1] = 0;
  429. }
  430. else
  431. {
  432. char *c;
  433. gethostname(hostname, size-1);
  434. hostname[size-1] = 0;
  435. c = strchr(hostname, '.');
  436. if (c)
  437. *c = 0;
  438. }
  439. }
  440. void starpu_sleep(float nb_sec)
  441. {
  442. #ifdef STARPU_SIMGRID
  443. MSG_process_sleep(nb_sec);
  444. #elif defined(STARPU_HAVE_WINDOWS)
  445. Sleep(nb_sec * 1000);
  446. #else
  447. struct timespec req, rem;
  448. req.tv_sec = nb_sec;
  449. req.tv_nsec = (nb_sec - (float) req.tv_sec) * 1000000000;
  450. while (nanosleep(&req, &rem))
  451. req = rem;
  452. #endif
  453. }
  454. char *starpu_getenv(const char *str)
  455. {
  456. #ifndef STARPU_SIMGRID
  457. #if defined(STARPU_DEVEL) || defined(STARPU_DEBUG)
  458. struct _starpu_worker * worker;
  459. worker = _starpu_get_local_worker_key();
  460. if (worker && worker->worker_is_initialized)
  461. _STARPU_DISP( "getenv should not be called from running workers, only for main() or worker initialization, since it is not reentrant\n");
  462. #endif
  463. #endif
  464. return getenv(str);
  465. }