utils.c 11 KB

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