24 #include <stk_util/util/FeatureTest.hpp>
25 #include <stk_util/diag/Env.hpp>
26 #include <stk_util/diag/Platform.hpp>
28 #include <stk_util/parallel/ExceptionReport.hpp>
29 #include <stk_util/parallel/MPI.hpp>
30 #ifdef STK_BUILT_IN_SIERRA
33 #include <stk_util/environment/ProductRegistry.hpp>
35 #include <stk_util/diag/Writer.hpp>
36 #include <stk_util/diag/SlibDiagWriter.hpp>
37 #include <stk_util/diag/Timer.hpp>
38 #include <stk_util/diag/Trace.hpp>
47 #include <sys/malloc.h>
51 #include <sys/resource.h>
52 #if __GNUC__ == 3 || __GNUC__ == 4
61 #include <sys/resource.h>
66 #include <sys/resource.h>
67 #include <sys/systeminfo.h>
68 #include <sys/utsname.h>
71 #elif defined(__SUNPRO_CC)
72 #include <sys/resource.h>
74 #include <sys/utsname.h>
78 #if defined(__PUMAGON__)
81 #include <sys/param.h>
86 #include <sys/resource.h>
89 #include <sys/param.h>
90 #include <sys/utsname.h>
92 #include <sys/resource.h>
94 #include <catamount/catmalloc.h>
95 extern void *__heap_start;
96 extern void *__heap_end;
99 #include <sys/param.h>
100 #include <sys/utsname.h>
101 #include <sys/time.h>
102 #include <sys/resource.h>
103 #include <sys/wait.h>
106 #elif defined(__IBMC__) || defined(__IBMCPP__)
107 #include <sys/utsname.h>
108 #include <sys/time.h>
109 #include <sys/resource.h>
113 #include <sys/utsname.h>
114 #include <sys/time.h>
118 #include <stk_util/util/MallocUsed.h>
128 size_t get_redstorm_base_available_memory();
134 #ifdef SIERRA_USE_PLATFORM_DEMANGLER
137 #if defined(__GNUC__)
156 char *demangled_symbol = abi::__cxa_demangle(symbol, 0, &len, &status);
158 if (demangled_symbol) {
159 s = std::string(demangled_symbol);
160 free(demangled_symbol);
164 s = std::string(symbol);
170 #elif (__GNUC__ == 4)
188 char *demangled_symbol = __cxxabiv1::__cxa_demangle(symbol, 0, &len, &status);
190 if (demangled_symbol) {
191 s = std::string(demangled_symbol);
192 free(demangled_symbol);
196 s = std::string(symbol);
201 #endif // (__GNUC__ == 3)
204 std::string
demangle(
const char *symbol) {
207 #endif // defined(__GNUC__)
213 #endif // SIERRA_USE_PLATFORM_DEMANGLER
223 static char redstorm_cout_buf[32678];
224 std::cout.rdbuf()->pubsetbuf(redstorm_cout_buf,
sizeof(redstorm_cout_buf));
226 static char redstorm_stdout_buf[32678];
227 std::setvbuf(stdout, redstorm_stdout_buf, _IOFBF,
sizeof(redstorm_stdout_buf));
229 static char redstorm_cerr_buf[32678];
230 std::cerr.rdbuf()->pubsetbuf(redstorm_cerr_buf,
sizeof(redstorm_cerr_buf));
232 static char redstorm_stderr_buf[32678];
233 std::setvbuf(stderr, redstorm_stderr_buf, _IOFBF,
sizeof(redstorm_stderr_buf));
243 std::locale loc(
"POSIX");
245 std::locale::global(loc);
246 std::cout.imbue(loc);
249 std::ostringstream strout;
250 strout <<
"Don't ask why the IBM locale works if I do this " << 10000000 << std::endl;
265 gettimeofday(&tp, &tz);
266 return (tp.tv_sec + (((
double)(tp.tv_usec))/1000000.0));
274 struct rusage my_rusage;
276 getrusage(RUSAGE_SELF, &my_rusage);
278 return (
double) (my_rusage.ru_utime.tv_sec)
279 + ((
double)(my_rusage.ru_utime.tv_usec))*1.0e-6;
281 #elif ! defined(__PGI)
282 struct rusage my_rusage;
284 getrusage(RUSAGE_SELF, &my_rusage);
286 return (
double) (my_rusage.ru_utime.tv_sec + my_rusage.ru_stime.tv_sec)
287 + ((
double)(my_rusage.ru_utime.tv_usec + my_rusage.ru_stime.tv_usec))*1.0e-6;
297 size_t & largest_free)
302 #if defined(SIERRA_HEAP_INFO)
304 # if defined(SIERRA_PTMALLOC3_ALLOCATOR) || defined(SIERRA_PTMALLOC2_ALLOCATOR)
305 heap_size = malloc_used();
307 # elif 0 // if defined(REDS) // Redstorm now links in gnu's malloc
308 static size_t reds_fragments;
309 static unsigned long reds_total_free;
310 static unsigned long reds_heap_size;
311 static unsigned long reds_largest_free;
313 ::heap_info(&reds_fragments, &reds_total_free, &reds_largest_free, &reds_heap_size);
315 heap_size = reds_heap_size;
316 largest_free = reds_largest_free;
318 slibout.m(Slib::LOG_MEMORY) <<
"reds_fragments " << reds_fragments
319 <<
", reds_total_free " << reds_total_free
320 <<
", reds_largest_free " << reds_largest_free
321 <<
", reds_heap_size " << reds_heap_size <<
Diag::dendl;
323 # elif ( defined(__linux__) || defined(REDS) ) && ! defined(__IBMCPP__)
324 static struct mallinfo minfo;
326 heap_size = (
unsigned int) minfo.uordblks + (
unsigned int) minfo.hblkhd;
327 largest_free = (
unsigned int) minfo.fordblks;
329 slibout.m(Slib::LOG_MEMORY) <<
"size_t size " <<
sizeof(size_t)*8 <<
" bits"
330 <<
", heap size " << heap_size
331 <<
", arena " << (
unsigned int) minfo.arena
332 <<
", ordblks " << minfo.ordblks
333 <<
", smblks " << minfo.smblks
334 <<
", hblks " << minfo.hblks
335 <<
", hblkhd " << (
unsigned int) minfo.hblkhd
336 <<
", usmblks " << minfo.usmblks
337 <<
", fsmblks " << minfo.fsmblks
338 <<
", uordblks " << (
unsigned int) minfo.uordblks
339 <<
", fordblks " << (
unsigned int) minfo.fordblks
343 # elif defined(__sun)
344 pstatus_t proc_status;
346 std::ifstream proc(
"/proc/self/status", std::ios_base::in|std::ios_base::binary);
348 proc.read((
char *)&proc_status,
sizeof(proc_status));
349 heap_size = proc_status.pr_brksize;
350 slibout.m(Slib::LOG_MEMORY) <<
"pr_brksize " << proc_status.pr_brksize
351 <<
", pr_stksize " << proc_status.pr_stksize <<
Diag::dendl;
354 #endif // defined(SIERRA_HEAP_INFO)
364 #if !defined(__APPLE__) && !defined(__FreeBSD__)
365 static size_t pagesize = getpagesize();
366 size_t avail = sysconf(_SC_AVPHYS_PAGES);
367 return avail * pagesize;
377 static size_t initial_memory_size = 0;
378 if (initial_memory_size == 0) {
379 initial_memory_size = get_redstorm_base_available_memory();
387 size_t & memory_usage,
393 #if defined(SIERRA_MEMORY_INFO)
397 # elif defined(__linux__)
398 std::ifstream proc(
"/proc/self/stat", std::ios_base::in|std::ios_base::binary);
403 for (i = 0; i < 11; ++i)
412 proc >> memory_usage;
415 # elif defined(__sun)
420 std::ifstream proc(
"/proc/self/psinfo", std::ios_base::in|std::ios_base::binary);
422 proc.read((
char *)&proc_info,
sizeof(proc_info));
423 memory_usage = proc_info.pr_size*1024;
428 prusage_t proc_usage;
430 std::ifstream proc(
"/proc/self/usage", std::ios_base::in|std::ios_base::binary);
432 proc.read((
char *)&proc_usage,
sizeof(proc_usage));
433 faults = proc_usage.pr_majf;
437 #endif // defined(SIERRA_MEMORY_INFO)
449 return (
double) memory_usage;
457 ::gethostname(buf,
sizeof(buf));
458 return std::string(buf);
465 #if defined(__PUMAGON__) || defined(REDS)
466 return std::string(
".sandia.gov");
469 std::string domain(
".");
472 ::sysinfo(SI_SRPC_DOMAIN, buf,
sizeof(buf));
473 if (std::strlen(buf)) {
478 #else //#elif defined(__linux) || defined(__sgi) || defined(_AIX)
479 std::string domain(
".");
482 ::getdomainname(buf,
sizeof(buf));
495 #if defined(REDS) || defined(__CRAYXT_COMPUTE_LINUX_TARGET)
501 struct passwd *user_info = ::getpwuid(::geteuid());
503 return (user_info ? user_info->pw_name :
"unknown");
512 struct utsname uts_name;
516 return uts_name.machine;
528 struct utsname uts_name;
532 return uts_name.sysname;
544 struct utsname uts_name;
548 return uts_name.release;
566 #if defined(__PUMAGON__) || defined(REDS)
576 const std::string & name,
580 return !name.empty() && ::access(name.c_str(), mode) == 0;
586 const std::string & name)
594 const std::string & name)
602 const std::string & name)
616 static struct flock ret;
619 ret.l_whence = whence;
631 int i =::fcntl(fd, F_SETLK, file_lock(F_WRLCK, SEEK_SET));
643 int i =::fcntl(fd, F_SETLK, file_lock(F_UNLCK, SEEK_SET));
655 return ::fcntl(fd, F_SETLK, file_lock(F_RDLCK, SEEK_SET)) != -1;
663 return ::fcntl(fd, F_SETLK, file_lock(F_WRLCK, SEEK_END)) != -1;
671 #if defined(__GNUC__)
673 size_t get_redstorm_base_available_memory()
680 #include <catamount/data.h>
683 void stk_ptr (
unsigned long *sp) {
684 asm (
"movq %rsp, (%rdi)");
687 size_t get_redstorm_base_available_memory()
695 size_t unmapped_top = 4 * 1048576;
696 size_t os_foot = 140 * 1048576;
703 stk_ptr (&stack_top);
708 stack_top &= ~0x1fffff;
709 stack_top += 0x200000;
716 avail_mem = stack_top + unmapped_top;
721 avail_mem -= os_foot;
727 avail_mem /= _my_pcb->upcb_vnm_degree;
733 p1 = (
char *) malloc (1);
734 heap_base = (size_t) p1;
742 avail_heap = avail_mem - heap_base;
748 stack_base = (size_t) _my_pcb;
749 stack_base &= ~0x1fffff;
750 stack_base += 0x400000;
757 stack_size = stack_top - stack_base;
758 avail_heap -= unmapped_top;
759 avail_heap -= stack_size;