utils.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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, 2016 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. fprintf(stderr,"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. fprintf(stderr,"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. int _starpu_frdlock(FILE *file)
  237. {
  238. int ret;
  239. #if defined(_WIN32) && !defined(__CYGWIN__)
  240. do
  241. {
  242. ret = _locking(fileno(file), _LK_RLCK, 10);
  243. }
  244. while (ret == EDEADLOCK);
  245. #else
  246. struct flock lock =
  247. {
  248. .l_type = F_RDLCK,
  249. .l_whence = SEEK_SET,
  250. .l_start = 0,
  251. .l_len = 0
  252. };
  253. ret = fcntl(fileno(file), F_SETLKW, &lock);
  254. #endif
  255. #ifdef ENOLCK
  256. if (ret != 0 && errno == ENOLCK)
  257. {
  258. static int warn;
  259. if (!warn) {
  260. warn = 1;
  261. _STARPU_DISP("warning: Couldn't lock performance file, StarPU home is probably on NFS which does not support locking.\n");
  262. }
  263. return -1;
  264. }
  265. #endif
  266. STARPU_ASSERT(ret == 0);
  267. return ret;
  268. }
  269. int _starpu_frdunlock(FILE *file)
  270. {
  271. int ret;
  272. #if defined(_WIN32) && !defined(__CYGWIN__)
  273. # ifndef _LK_UNLCK
  274. # define _LK_UNLCK _LK_UNLOCK
  275. # endif
  276. ret = _lseek(fileno(file), 0, SEEK_SET);
  277. STARPU_ASSERT(ret == 0);
  278. ret = _locking(fileno(file), _LK_UNLCK, 10);
  279. #else
  280. struct flock lock =
  281. {
  282. .l_type = F_UNLCK,
  283. .l_whence = SEEK_SET,
  284. .l_start = 0,
  285. .l_len = 0
  286. };
  287. ret = fcntl(fileno(file), F_SETLKW, &lock);
  288. #endif
  289. STARPU_ASSERT(ret == 0);
  290. return ret;
  291. }
  292. int _starpu_fwrlock(FILE *file)
  293. {
  294. int ret;
  295. #if defined(_WIN32) && !defined(__CYGWIN__)
  296. ret = _lseek(fileno(file), 0, SEEK_SET);
  297. STARPU_ASSERT(ret == 0);
  298. do
  299. {
  300. ret = _locking(fileno(file), _LK_LOCK, 10);
  301. }
  302. while (ret == EDEADLOCK);
  303. #else
  304. struct flock lock =
  305. {
  306. .l_type = F_WRLCK,
  307. .l_whence = SEEK_SET,
  308. .l_start = 0,
  309. .l_len = 0
  310. };
  311. ret = fcntl(fileno(file), F_SETLKW, &lock);
  312. #endif
  313. #ifdef ENOLCK
  314. if (ret != 0 && errno == ENOLCK)
  315. {
  316. static int warn;
  317. if (!warn) {
  318. warn = 1;
  319. _STARPU_DISP("warning: Couldn't lock performance file, StarPU home is probably on NFS which does not support locking.\n");
  320. }
  321. return -1;
  322. }
  323. #endif
  324. STARPU_ASSERT(ret == 0);
  325. return ret;
  326. }
  327. int _starpu_fwrunlock(FILE *file)
  328. {
  329. return _starpu_frdunlock(file);
  330. }
  331. int _starpu_check_mutex_deadlock(starpu_pthread_mutex_t *mutex)
  332. {
  333. int ret;
  334. ret = starpu_pthread_mutex_trylock(mutex);
  335. if (!ret)
  336. {
  337. STARPU_PTHREAD_MUTEX_UNLOCK(mutex);
  338. return 0;
  339. }
  340. if (ret == EBUSY)
  341. return 0;
  342. STARPU_ASSERT (ret != EDEADLK);
  343. return 1;
  344. }
  345. char *_starpu_get_home_path(void)
  346. {
  347. char *path = starpu_getenv("XDG_CACHE_HOME");
  348. if (!path)
  349. path = starpu_getenv("STARPU_HOME");
  350. #ifdef _WIN32
  351. if (!path)
  352. path = starpu_getenv("LOCALAPPDATA");
  353. if (!path)
  354. path = starpu_getenv("USERPROFILE");
  355. #endif
  356. if (!path)
  357. path = starpu_getenv("HOME");
  358. if (!path)
  359. {
  360. static int warn;
  361. path = starpu_getenv("TMPDIR");
  362. if (!path)
  363. path = "/tmp";
  364. if (!warn)
  365. {
  366. warn = 1;
  367. _STARPU_DISP("couldn't find a $STARPU_HOME place to put .starpu data, using %s\n", path);
  368. }
  369. }
  370. return path;
  371. }
  372. void _starpu_gethostname(char *hostname, size_t size)
  373. {
  374. char *forced_hostname = starpu_getenv("STARPU_HOSTNAME");
  375. if (forced_hostname && forced_hostname[0])
  376. {
  377. snprintf(hostname, size-1, "%s", forced_hostname);
  378. hostname[size-1] = 0;
  379. }
  380. else
  381. {
  382. char *c;
  383. gethostname(hostname, size-1);
  384. hostname[size-1] = 0;
  385. c = strchr(hostname, '.');
  386. if (c)
  387. *c = 0;
  388. }
  389. }
  390. void starpu_sleep(float nb_sec)
  391. {
  392. #ifdef STARPU_SIMGRID
  393. MSG_process_sleep(nb_sec);
  394. #elif defined(STARPU_HAVE_WINDOWS)
  395. Sleep(nb_sec * 1000);
  396. #else
  397. struct timespec req, rem;
  398. req.tv_sec = nb_sec;
  399. req.tv_nsec = (nb_sec - (float) req.tv_sec) * 1000000000;
  400. while (nanosleep(&req, &rem))
  401. req = rem;
  402. #endif
  403. }
  404. char *starpu_getenv(const char *str)
  405. {
  406. #ifndef STARPU_SIMGRID
  407. #if defined(STARPU_DEVEL) || defined(STARPU_DEBUG)
  408. struct _starpu_worker * worker;
  409. worker = _starpu_get_local_worker_key();
  410. if (worker && worker->worker_is_initialized)
  411. _STARPU_DISP( "getenv should not be called from running workers, only for main() or worker initialization, since it is not reentrant\n");
  412. #endif
  413. #endif
  414. return getenv(str);
  415. }