sysdep-unix.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /*
  2. * sysdep-unix.c
  3. *
  4. *************************************************************************
  5. *
  6. * @copyright
  7. * Copyright (C) 2010-2013, Intel Corporation
  8. * All rights reserved.
  9. *
  10. * @copyright
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * * Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. * * Neither the name of Intel Corporation nor the names of its
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * @copyright
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  29. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  30. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  33. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  34. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  36. * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. **************************************************************************
  40. */
  41. #ifdef __linux__
  42. // define _GNU_SOURCE before *any* #include.
  43. // Even <stdint.h> will break later #includes if this macro is not
  44. // already defined when it is #included.
  45. # define _GNU_SOURCE
  46. #endif
  47. #include "sysdep.h"
  48. #include "os.h"
  49. #include "bug.h"
  50. #include "local_state.h"
  51. #include "signal_node.h"
  52. #include "full_frame.h"
  53. #include "jmpbuf.h"
  54. #include "cilk_malloc.h"
  55. #include "reducer_impl.h"
  56. #include "metacall_impl.h"
  57. // On x86 processors (but not MIC processors), the compiler generated code to
  58. // save the FP state (rounding mode and the like) before calling setjmp. We
  59. // will need to restore that state when we resume.
  60. #ifndef __MIC__
  61. # if defined(__i386__) || defined(__x86_64)
  62. # define RESTORE_X86_FP_STATE
  63. # endif // defined(__i386__) || defined(__x86_64)
  64. #endif // __MIC__
  65. // contains notification macros for VTune.
  66. #include "cilk-ittnotify.h"
  67. #include <stddef.h>
  68. #ifdef __CYGWIN__
  69. // On Cygwin, string.h doesnt declare strcasecmp if __STRICT_ANSI__ is defined
  70. # undef __STRICT_ANSI__
  71. #endif
  72. #include <string.h>
  73. #include <pthread.h>
  74. #include <unistd.h>
  75. #if defined HAVE_ALLOCA_H
  76. # include <alloca.h>
  77. #elif defined __GNUC__
  78. # define alloca __builtin_alloca
  79. #elif defined _AIX
  80. # define alloca __alloca
  81. #else
  82. # include <stddef.h>
  83. # ifdef __cplusplus
  84. extern "C"
  85. # endif
  86. void *alloca (size_t);
  87. #endif
  88. #ifdef __APPLE__
  89. //# include <scheduler.h> // Angle brackets include Apple's scheduler.h, not ours.
  90. #endif
  91. #ifdef __linux__
  92. # include <sys/resource.h>
  93. # include <sys/sysinfo.h>
  94. #endif
  95. #ifdef __FreeBSD__
  96. # include <sys/resource.h>
  97. // BSD does not define MAP_ANONYMOUS, but *does* define MAP_ANON. Aren't standards great!
  98. # define MAP_ANONYMOUS MAP_ANON
  99. #endif
  100. #ifdef __VXWORKS__
  101. # include <vxWorks.h>
  102. # include <vxCpuLib.h>
  103. #endif
  104. struct global_sysdep_state
  105. {
  106. pthread_t *threads; ///< Array of pthreads for system workers
  107. size_t pthread_t_size; ///< for cilk_db
  108. };
  109. static void internal_enforce_global_visibility();
  110. COMMON_SYSDEP
  111. void __cilkrts_init_worker_sysdep(struct __cilkrts_worker *w)
  112. {
  113. ITT_SYNC_CREATE(w, "Scheduler");
  114. }
  115. COMMON_SYSDEP
  116. void __cilkrts_destroy_worker_sysdep(struct __cilkrts_worker *w)
  117. {
  118. }
  119. COMMON_SYSDEP
  120. void __cilkrts_init_global_sysdep(global_state_t *g)
  121. {
  122. internal_enforce_global_visibility();
  123. __cilkrts_init_tls_variables();
  124. CILK_ASSERT(g->total_workers >= g->P - 1);
  125. g->sysdep = __cilkrts_malloc(sizeof (struct global_sysdep_state));
  126. CILK_ASSERT(g->sysdep);
  127. g->sysdep->pthread_t_size = sizeof (pthread_t);
  128. // TBD: Should this value be g->total_workers, or g->P?
  129. // Need to check what we are using this field for.
  130. g->sysdep->threads = __cilkrts_malloc(sizeof(pthread_t) * g->total_workers);
  131. CILK_ASSERT(g->sysdep->threads);
  132. return;
  133. }
  134. COMMON_SYSDEP
  135. void __cilkrts_destroy_global_sysdep(global_state_t *g)
  136. {
  137. if (g->sysdep->threads)
  138. __cilkrts_free(g->sysdep->threads);
  139. __cilkrts_free(g->sysdep);
  140. }
  141. /*************************************************************
  142. Creation of worker threads:
  143. *************************************************************/
  144. static void internal_run_scheduler_with_exceptions(__cilkrts_worker *w)
  145. {
  146. /* We assume the stack grows down. */
  147. char var;
  148. __cilkrts_cilkscreen_establish_c_stack(&var - 1000000, &var);
  149. __cilkrts_run_scheduler_with_exceptions(w);
  150. }
  151. /*
  152. * scheduler_thread_proc_for_system_worker
  153. *
  154. * Thread start function called when we start a new worker.
  155. *
  156. */
  157. NON_COMMON void* scheduler_thread_proc_for_system_worker(void *arg)
  158. {
  159. /*int status;*/
  160. __cilkrts_worker *w = (__cilkrts_worker *)arg;
  161. #ifdef __INTEL_COMPILER
  162. #ifdef USE_ITTNOTIFY
  163. // Name the threads for Advisor. They don't want a worker number.
  164. __itt_thread_set_name("Cilk Worker");
  165. #endif // defined USE_ITTNOTIFY
  166. #endif // defined __INTEL_COMPILER
  167. /* Worker startup is serialized
  168. status = pthread_mutex_lock(&__cilkrts_global_mutex);
  169. CILK_ASSERT(status == 0);*/
  170. CILK_ASSERT(w->l->type == WORKER_SYSTEM);
  171. /*status = pthread_mutex_unlock(&__cilkrts_global_mutex);
  172. CILK_ASSERT(status == 0);*/
  173. __cilkrts_set_tls_worker(w);
  174. // Create a cilk fiber for this worker on this thread.
  175. START_INTERVAL(w, INTERVAL_FIBER_ALLOCATE_FROM_THREAD) {
  176. w->l->scheduling_fiber = cilk_fiber_allocate_from_thread();
  177. cilk_fiber_set_owner(w->l->scheduling_fiber, w);
  178. } STOP_INTERVAL(w, INTERVAL_FIBER_ALLOCATE_FROM_THREAD);
  179. internal_run_scheduler_with_exceptions(w);
  180. START_INTERVAL(w, INTERVAL_FIBER_DEALLOCATE_FROM_THREAD) {
  181. // Deallocate the scheduling fiber. This operation reverses the
  182. // effect cilk_fiber_allocate_from_thread() and must be done in this
  183. // thread before it exits.
  184. int ref_count = cilk_fiber_deallocate_from_thread(w->l->scheduling_fiber);
  185. // Scheduling fibers should never have extra references to them.
  186. // We only get extra references into fibers because of Windows
  187. // exceptions.
  188. CILK_ASSERT(0 == ref_count);
  189. w->l->scheduling_fiber = NULL;
  190. } STOP_INTERVAL(w, INTERVAL_FIBER_DEALLOCATE_FROM_THREAD);
  191. return 0;
  192. }
  193. /*
  194. * __cilkrts_user_worker_scheduling_stub
  195. *
  196. * Routine for the scheduling fiber created for an imported user
  197. * worker thread. This method is analogous to
  198. * scheduler_thread_proc_for_system_worker.
  199. *
  200. */
  201. void __cilkrts_user_worker_scheduling_stub(cilk_fiber* fiber, void* null_arg)
  202. {
  203. __cilkrts_worker *w = __cilkrts_get_tls_worker();
  204. // Sanity check.
  205. CILK_ASSERT(WORKER_USER == w->l->type);
  206. // Enter the scheduling loop on the user worker.
  207. // This function will never return.
  208. __cilkrts_run_scheduler_with_exceptions(w);
  209. // A WORKER_USER, at some point, will resume on the original stack and leave
  210. // Cilk. Under no circumstances do we ever exit off of the bottom of this
  211. // stack.
  212. CILK_ASSERT(0);
  213. }
  214. /**
  215. * We are exporting a function with this name to Inspector?
  216. * What a confusing name...
  217. *
  218. * This function is exported so Piersol's stack trace displays
  219. * reasonable information.
  220. */
  221. void* __cilkrts_worker_stub(void* arg)
  222. {
  223. return scheduler_thread_proc_for_system_worker(arg);
  224. }
  225. // /* Return the lesser of the argument and the operating system
  226. // limit on the number of workers (threads) that may or ought
  227. // to be created. */
  228. // int sysdep_thread_limit(int n, int physical_cpus)
  229. // {
  230. // /* On Linux thread creation fails somewhere short of the
  231. // number of available processes. */
  232. // struct rlimit lim;
  233. // if (n > 256 + 2 * physical_cpus)
  234. // n = 256 + 2 * physical_cpus;
  235. // if (getrlimit(RLIMIT_NPROC, &lim) == 0 && lim.rlim_cur != RLIM_INFINITY)
  236. // {
  237. // /* If the limit reads 0 or absurdly small, ignore it. */
  238. // unsigned int maxproc = (lim.rlim_cur * 3 + 3) / 4;
  239. // if (maxproc > 8 + 2 * physical_cpus && maxproc < n)
  240. // n = maxproc;
  241. // }
  242. // return n;
  243. // }
  244. static void write_version_file (global_state_t *, int);
  245. /* Create n worker threads from base..top-1
  246. */
  247. static void create_threads(global_state_t *g, int base, int top)
  248. {
  249. // TBD(11/30/12): We want to insert code providing the option of
  250. // pinning system workers to cores.
  251. for (int i = base; i < top; i++) {
  252. int status = pthread_create(&g->sysdep->threads[i],
  253. NULL,
  254. scheduler_thread_proc_for_system_worker,
  255. g->workers[i]);
  256. if (status != 0)
  257. __cilkrts_bug("Cilk runtime error: thread creation (%d) failed: %d\n", i, status);
  258. }
  259. }
  260. #if PARALLEL_THREAD_CREATE
  261. static int volatile threads_created = 0;
  262. // Create approximately half of the worker threads, and then become a worker
  263. // ourselves.
  264. static void * create_threads_and_work (void * arg)
  265. {
  266. global_state_t *g = ((__cilkrts_worker *)arg)->g;
  267. create_threads(g, g->P/2, g->P-1);
  268. // Let the initial thread know that we're done.
  269. threads_created = 1;
  270. // Ideally this turns into a tail call that wipes out this stack frame.
  271. return scheduler_thread_proc_for_system_worker(arg);
  272. }
  273. #endif
  274. void __cilkrts_start_workers(global_state_t *g, int n)
  275. {
  276. g->workers_running = 1;
  277. g->work_done = 0;
  278. if (!g->sysdep->threads)
  279. return;
  280. // Do we actually have any threads to create?
  281. if (n > 0)
  282. {
  283. #if PARALLEL_THREAD_CREATE
  284. int status;
  285. // We create (a rounded up) half of the threads, thread one creates the rest
  286. int half_threads = (n+1)/2;
  287. // Create the first thread passing a different thread function, so that it creates threads itself
  288. status = pthread_create(&g->sysdep->threads[0], NULL, create_threads_and_work, g->workers[0]);
  289. if (status != 0)
  290. __cilkrts_bug("Cilk runtime error: thread creation (0) failed: %d\n", status);
  291. // Then the rest of the ones we have to create
  292. create_threads(g, 1, half_threads);
  293. // Now wait for the first created thread to tell us it's created all of its threads.
  294. // We could maybe drop this a bit lower and overlap with write_version_file.
  295. while (!threads_created)
  296. __cilkrts_yield();
  297. #else
  298. // Simply create all the threads linearly here.
  299. create_threads(g, 0, n);
  300. #endif
  301. }
  302. // write the version information to a file if the environment is configured
  303. // for it (the function makes the check).
  304. write_version_file(g, n);
  305. return;
  306. }
  307. void __cilkrts_stop_workers(global_state_t *g)
  308. {
  309. int i;
  310. // Tell the workers to give up
  311. g->work_done = 1;
  312. if (g->workers_running == 0)
  313. return;
  314. if (!g->sysdep->threads)
  315. return;
  316. /* Make them all runnable. */
  317. if (g->P > 1) {
  318. CILK_ASSERT(g->workers[0]->l->signal_node);
  319. signal_node_msg(g->workers[0]->l->signal_node, 1);
  320. }
  321. for (i = 0; i < g->P - 1; ++i) {
  322. int sc_status;
  323. void *th_status;
  324. sc_status = pthread_join(g->sysdep->threads[i], &th_status);
  325. if (sc_status != 0)
  326. __cilkrts_bug("Cilk runtime error: thread join (%d) failed: %d\n", i, sc_status);
  327. }
  328. g->workers_running = 0;
  329. return;
  330. }
  331. /*
  332. * @brief Returns the stack address for resuming execution of sf.
  333. *
  334. * This method takes in the top of the stack to use, and then returns
  335. * a properly aligned address for resuming execution of sf.
  336. *
  337. * @param sf - The stack frame we want to resume executing.
  338. * @param stack_base - The top of the stack we want to execute sf on.
  339. *
  340. */
  341. static char* get_sp_for_executing_sf(char* stack_base,
  342. full_frame *ff,
  343. __cilkrts_stack_frame *sf)
  344. {
  345. // The original calculation that had been done to correct the stack
  346. // pointer when resuming execution.
  347. //
  348. // But this code was never getting called in the eng branch anyway...
  349. //
  350. // TBD(11/30/12): This logic needs to be revisited to make sure that
  351. // we are doing the proper calculation in reserving space for outgoing
  352. // arguments on all platforms and architectures.
  353. #if 0
  354. /* Preserve outgoing argument space and stack alignment on steal.
  355. Outgoing argument space is bounded by the difference between
  356. stack and frame pointers. Some user code is known to rely on
  357. 16 byte alignment. Maintain 32 byte alignment for future
  358. compatibility. */
  359. #define SMASK 31 /* 32 byte alignment */
  360. if (sf) {
  361. char *fp = FP(sf), *sp = SP(sf);
  362. int fp_align = (int)(size_t)fp & SMASK;
  363. ptrdiff_t space = fp - sp;
  364. fprintf(stderr, "Here: fp = %p, sp = %p\n", fp, sp);
  365. char *top_aligned = (char *)((((size_t)stack_base - SMASK) & ~(size_t)SMASK) | fp_align);
  366. /* Don't allocate an unreasonable amount of stack space. */
  367. fprintf(stderr, "Here: stack_base = %p, top_aligned=%p, space=%ld\n",
  368. stack_base, top_aligned, space);
  369. if (space < 32)
  370. space = 32 + (space & SMASK);
  371. else if (space > 40 * 1024)
  372. space = 40 * 1024 + (space & SMASK);
  373. return top_aligned - space;
  374. }
  375. #endif
  376. #define PERFORM_FRAME_SIZE_CALCULATION 0
  377. char* new_stack_base = stack_base - 256;
  378. #if PERFORM_FRAME_SIZE_CALCULATION
  379. // If there is a frame size saved, then use that as the
  380. // correction instead of 256.
  381. if (ff->frame_size > 0) {
  382. if (ff->frame_size < 40*1024) {
  383. new_stack_base = stack_base - ff->frame_size;
  384. }
  385. else {
  386. // If for some reason, our frame size calculation is giving us
  387. // a number which is bigger than about 10 pages, then
  388. // there is likely something wrong here? Don't allocate
  389. // an unreasonable amount of space.
  390. new_stack_base = stack_base - 40*1024;
  391. }
  392. }
  393. #endif
  394. // Whatever correction we choose, align the final stack top.
  395. // This alignment seems to be necessary in particular on 32-bit
  396. // Linux, and possibly Mac. (Is 32-byte alignment is sufficient?)
  397. /* 256-byte alignment. Why not? */
  398. const uintptr_t align_mask = ~(256 -1);
  399. new_stack_base = (char*)((size_t)new_stack_base & align_mask);
  400. return new_stack_base;
  401. }
  402. char* sysdep_reset_jump_buffers_for_resume(cilk_fiber* fiber,
  403. full_frame *ff,
  404. __cilkrts_stack_frame *sf)
  405. {
  406. #if FIBER_DEBUG >= 4
  407. fprintf(stderr, "ThreadId=%p (fiber_proc_to_resume), Fiber %p. sf = %p. ff=%p, ff->sync_sp=%p\n",
  408. cilkos_get_current_thread_id(),
  409. fiber,
  410. sf,
  411. ff, ff->sync_sp);
  412. #endif
  413. CILK_ASSERT(fiber);
  414. void* sp = (void*)get_sp_for_executing_sf(cilk_fiber_get_stack_base(fiber), ff, sf);
  415. SP(sf) = sp;
  416. /* Debugging: make sure stack is accessible. */
  417. ((volatile char *)sp)[-1];
  418. // Adjust the saved_sp to account for the SP we're about to run. This will
  419. // allow us to track fluctations in the stack
  420. #if FIBER_DEBUG >= 4
  421. fprintf(stderr, "ThreadId=%p, about to take stack ff=%p, sp=%p, sync_sp=%p\n",
  422. cilkos_get_current_thread_id(),
  423. ff,
  424. sp,
  425. ff->sync_sp);
  426. #endif
  427. __cilkrts_take_stack(ff, sp);
  428. return sp;
  429. }
  430. NORETURN sysdep_longjmp_to_sf(char* new_sp,
  431. __cilkrts_stack_frame *sf,
  432. full_frame *ff_for_exceptions /* UNUSED on Unix */)
  433. {
  434. #if FIBER_DEBUG >= 3
  435. fprintf(stderr,
  436. "ThreadId=%p. resume user code, sf=%p, new_sp = %p, original SP(sf) = %p, FP(sf) = %p\n",
  437. cilkos_get_current_thread_id(), sf, new_sp, SP(sf), FP(sf));
  438. #endif
  439. // Set the stack pointer.
  440. SP(sf) = new_sp;
  441. #ifdef RESTORE_X86_FP_STATE
  442. if (CILK_FRAME_VERSION_VALUE(sf->flags) >= 1) {
  443. // Restore the floating point state that was set in this frame at the
  444. // last spawn.
  445. //
  446. // This feature is only available in ABI 1 or later frames, and only
  447. // needed on IA64 or Intel64 processors.
  448. restore_x86_fp_state(sf);
  449. }
  450. #endif
  451. CILK_LONGJMP(sf->ctx);
  452. }
  453. #include <stddef.h>
  454. #include <stdlib.h>
  455. #include <string.h>
  456. #include <sys/mman.h>
  457. #include <errno.h>
  458. void __cilkrts_make_unrunnable_sysdep(__cilkrts_worker *w,
  459. full_frame *ff,
  460. __cilkrts_stack_frame *sf,
  461. int is_loot,
  462. const char *why)
  463. {
  464. (void)w; /* unused */
  465. sf->except_data = 0;
  466. if (is_loot)
  467. {
  468. if (ff->frame_size == 0)
  469. ff->frame_size = __cilkrts_get_frame_size(sf);
  470. // Null loot's sp for debugging purposes (so we'll know it's not valid)
  471. SP(sf) = 0;
  472. }
  473. }
  474. /*
  475. * __cilkrts_sysdep_is_worker_thread_id
  476. *
  477. * Returns true if the thread ID specified matches the thread ID we saved
  478. * for a worker.
  479. */
  480. int __cilkrts_sysdep_is_worker_thread_id(global_state_t *g,
  481. int i,
  482. void *thread_id)
  483. {
  484. #if defined( __linux__) || defined(__VXWORKS__)
  485. pthread_t tid = *(pthread_t *)thread_id;
  486. if (i < 0 || i > g->total_workers)
  487. return 0;
  488. return g->sysdep->threads[i] == tid;
  489. #else
  490. // Needs to be implemented
  491. return 0;
  492. #endif
  493. }
  494. /*************************************************************
  495. Version information:
  496. *************************************************************/
  497. #include <dlfcn.h>
  498. #include "internal/cilk_version.h"
  499. #include <stdio.h>
  500. #include <sys/utsname.h>
  501. #ifdef __VXWORKS__
  502. #include <version.h>
  503. # endif
  504. /* (Non-static) dummy function is used by get_runtime_path() to find the path
  505. * to the .so containing the Cilk runtime.
  506. */
  507. void dummy_function() { }
  508. /* return a string with the path to the Cilk runtime, or "unknown" if the path
  509. * cannot be determined.
  510. */
  511. static const char *get_runtime_path ()
  512. {
  513. #ifdef __CYGWIN__
  514. // Cygwin doesn't support dladdr, which sucks
  515. return "unknown";
  516. #else
  517. Dl_info info;
  518. if (0 == dladdr(dummy_function, &info)) return "unknown";
  519. return info.dli_fname;
  520. #endif
  521. }
  522. /* if the environment variable, CILK_VERSION, is defined, writes the version
  523. * information to the specified file.
  524. * g is the global state that was just created, and n is the number of workers
  525. * that were made (or requested from RML) for it.
  526. */
  527. static void write_version_file (global_state_t *g, int n)
  528. {
  529. const char *env; // environment variable.
  530. char buf[256]; // print buffer.
  531. time_t t;
  532. FILE *fp;
  533. struct utsname sys_info;
  534. int err; // error code from system calls.
  535. // if CILK_VERSION is not set, or if the file cannot be opened, fail
  536. // silently. Otherwise open the file for writing (or use stderr or stdout
  537. // if the user specifies).
  538. if (NULL == (env = getenv("CILK_VERSION"))) return;
  539. if (0 == strcasecmp(env, "stderr")) fp = stderr;
  540. else if (0 == strcasecmp(env, "stdout")) fp = stdout;
  541. else if (NULL == (fp = fopen(env, "w"))) return;
  542. // get a string for the current time. E.g.,
  543. // Cilk runtime initialized: Thu Jun 10 13:28:00 2010
  544. t = time(NULL);
  545. strftime(buf, 256, "%a %b %d %H:%M:%S %Y", localtime(&t));
  546. fprintf(fp, "Cilk runtime initialized: %s\n", buf);
  547. // Print runtime info. E.g.,
  548. // Cilk runtime information
  549. // ========================
  550. // Cilk version: 2.0.0 Build 9184
  551. // Built by willtor on host willtor-desktop
  552. // Compilation date: Thu Jun 10 13:27:42 2010
  553. // Compiled with ICC V99.9.9, ICC build date: 20100610
  554. fprintf(fp, "\nCilk runtime information\n");
  555. fprintf(fp, "========================\n");
  556. fprintf(fp, "Cilk version: %d.%d.%d Build %d\n",
  557. VERSION_MAJOR,
  558. VERSION_MINOR,
  559. VERSION_REV,
  560. VERSION_BUILD);
  561. #ifdef __VXWORKS__
  562. char * vxWorksVer = VXWORKS_VERSION;
  563. fprintf(fp, "Cross compiled for %s\n",vxWorksVer);
  564. // user and host not avalible if VxWorks cross compiled on windows build host
  565. #else
  566. // User and host are not available for GCC builds
  567. #ifdef BUILD_USER
  568. fprintf(fp, "Built by "BUILD_USER" on host "BUILD_HOST"\n");
  569. #endif // BUILD_USER
  570. #endif // __VXWORKS__
  571. // GCC has requested that this be removed for GCC builds
  572. #ifdef BUILD_USER
  573. fprintf(fp, "Compilation date: "__DATE__" "__TIME__"\n");
  574. #endif // BUILD_USER
  575. #ifdef __INTEL_COMPILER
  576. // Compiled by the Intel C/C++ compiler.
  577. fprintf(fp, "Compiled with ICC V%d.%d.%d, ICC build date: %d\n",
  578. __INTEL_COMPILER / 100,
  579. (__INTEL_COMPILER / 10) % 10,
  580. __INTEL_COMPILER % 10,
  581. __INTEL_COMPILER_BUILD_DATE);
  582. #else
  583. // Compiled by GCC.
  584. fprintf(fp, "Compiled with GCC V%d.%d.%d\n",
  585. __GNUC__,
  586. __GNUC_MINOR__,
  587. __GNUC_PATCHLEVEL__);
  588. #endif // defined __INTEL_COMPILER
  589. // Print system info. E.g.,
  590. // System information
  591. // ==================
  592. // Cilk runtime path: /opt/icc/64/lib/libcilkrts.so.5
  593. // System OS: Linux, release 2.6.28-19-generic
  594. // System architecture: x86_64
  595. err = uname(&sys_info);
  596. fprintf(fp, "\nSystem information\n");
  597. fprintf(fp, "==================\n");
  598. fprintf(fp, "Cilk runtime path: %s\n", get_runtime_path());
  599. fprintf(fp, "System OS: %s, release %s\n",
  600. err < 0 ? "unknown" : sys_info.sysname,
  601. err < 0 ? "?" : sys_info.release);
  602. fprintf(fp, "System architecture: %s\n",
  603. err < 0 ? "unknown" : sys_info.machine);
  604. // Print thread info. E.g.,
  605. // Thread information
  606. // ==================
  607. // System cores: 8
  608. // Cilk workers requested: 8
  609. // Thread creator: Private
  610. fprintf(fp, "\nThread information\n");
  611. fprintf(fp, "==================\n");
  612. #ifdef __VXWORKS__
  613. fprintf(fp, "System cores: %d\n", (int)__builtin_popcount(vxCpuEnabledGet()));
  614. #else
  615. fprintf(fp, "System cores: %d\n", (int)sysconf(_SC_NPROCESSORS_ONLN));
  616. #endif
  617. fprintf(fp, "Cilk workers requested: %d\n", n);
  618. #if (PARALLEL_THREAD_CREATE)
  619. fprintf(fp, "Thread creator: Private (parallel)\n");
  620. #else
  621. fprintf(fp, "Thread creator: Private\n");
  622. #endif
  623. if (fp != stderr && fp != stdout) fclose(fp);
  624. else fflush(fp); // flush the handle buffer if it is stdout or stderr.
  625. }
  626. /*
  627. * __cilkrts_establish_c_stack
  628. *
  629. * Tell Cilkscreen about the user stack bounds.
  630. *
  631. * Note that the Cilk V1 runtime only included the portion of the stack from
  632. * the entry into Cilk, down. We don't appear to be able to find that, but
  633. * I think this will be sufficient.
  634. */
  635. void __cilkrts_establish_c_stack(void)
  636. {
  637. /* FIXME: Not implemented. */
  638. /* TBD: Do we need this */
  639. /*
  640. void __cilkrts_cilkscreen_establish_c_stack(char *begin, char *end);
  641. size_t r;
  642. MEMORY_BASIC_INFORMATION mbi;
  643. r = VirtualQuery (&mbi,
  644. &mbi,
  645. sizeof(mbi));
  646. __cilkrts_cilkscreen_establish_c_stack((char *)mbi.BaseAddress,
  647. (char *)mbi.BaseAddress + mbi.RegionSize);
  648. */
  649. }
  650. /*
  651. * internal_enforce_global_visibility
  652. *
  653. * Ensure global visibility of public symbols, for proper Cilk-TBB interop.
  654. *
  655. * If Cilk runtime is loaded dynamically, its symbols might remain unavailable
  656. * for global search with dladdr; that might prevent TBB from finding Cilk
  657. * in the process address space and initiating the interop protocol.
  658. * The workaround is for the library to open itself with RTLD_GLOBAL flag.
  659. */
  660. static __attribute__((noinline))
  661. void internal_enforce_global_visibility()
  662. {
  663. void* handle = dlopen( get_runtime_path(), RTLD_GLOBAL|RTLD_LAZY );
  664. /* For proper reference counting, close the handle immediately. */
  665. if( handle) dlclose(handle);
  666. }
  667. /*
  668. Local Variables: **
  669. c-file-style:"bsd" **
  670. c-basic-offset:4 **
  671. indent-tabs-mode:nil **
  672. End: **
  673. */