📄 ncbi_system.cpp
字号:
/* * =========================================================================== * PRODUCTION $Log: ncbi_system.cpp,v $ * PRODUCTION Revision 1000.3 2004/06/01 19:08:44 gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.38 * PRODUCTION * =========================================================================== *//* $Id: ncbi_system.cpp,v 1000.3 2004/06/01 19:08:44 gouriano Exp $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Authors: Vladimir Ivanov, Denis Vakatov, Anton Lavrentiev * * File Description: System functions * */#include <ncbi_pch.hpp>#include <corelib/ncbimtx.hpp>#include <corelib/ncbi_system.hpp>#if defined(NCBI_OS_MAC)# include <OpenTransport.h>#endif#if defined(NCBI_OS_UNIX)# include <sys/time.h># include <sys/resource.h># include <sys/times.h># include <limits.h># include <unistd.h># if defined(NCBI_OS_BSD) || defined(NCBI_OS_DARWIN)# include <sys/sysctl.h># endif# define USE_SETHEAPLIMIT# define USE_SETCPULIMIT#endif#if defined(NCBI_OS_DARWIN)extern "C" {# if defined(NCBI_COMPILER_MW_MSL)# include <ncbi_mslextras.h># endif# include <mach/mach.h># include <mach/mach_host.h># include <mach/host_info.h>}#endif#if defined(USE_SETCPULIMIT)# include <signal.h>#endifBEGIN_NCBI_SCOPE// MIPSpro 7.3 workarounds:// 1) it declares set_new_handler() in both global and std:: namespaces;// 2) it apparently gets totally confused by `extern "C"' inside a namespace.#if defined(NCBI_COMPILER_MIPSPRO)# define set_new_handler std::set_new_handler#elseextern "C" { static void s_ExitHandler(void); static void s_SignalHandler(int sig);}#endif /* NCBI_COMPILER_MIPSPRO */#ifdef NCBI_OS_UNIXDEFINE_STATIC_FAST_MUTEX(s_ExitHandler_Mutex);static bool s_ExitHandlerIsSet = false;static ELimitsExitCode s_ExitCode = eLEC_None;static CTime s_TimeSet;static size_t s_HeapLimit = 0;static size_t s_CpuTimeLimit = 0;static char* s_ReserveMemory = 0;static TLimitsPrintHandler s_PrintHandler = 0;static TLimitsPrintParameter s_PrintHandlerParam = 0;#if !defined(CLK_TCK) && defined(CLOCKS_PER_SEC)# define CLK_TCK CLOCKS_PER_SEC#endif// Routine to be called at the exit from application//static void s_ExitHandler(void){ CFastMutexGuard LOCK(s_ExitHandler_Mutex); // Free reserved memory if ( s_ReserveMemory ) { delete[] s_ReserveMemory; s_ReserveMemory = 0; } // User defined dump if ( s_PrintHandler ) { size_t limit_size; switch ( s_ExitCode ) { case eLEC_Memory: { limit_size = s_HeapLimit; break; } case eLEC_Cpu: { limit_size = s_CpuTimeLimit; break; } default: return; } // Call user's print handler (*s_PrintHandler)(s_ExitCode, limit_size, s_TimeSet, s_PrintHandlerParam); return; } // Standard dump switch ( s_ExitCode ) { case eLEC_Memory: { ERR_POST("Memory heap limit exceeded in allocating memory " \ "by operator new (" << s_HeapLimit << " bytes)"); break; } case eLEC_Cpu: { ERR_POST("CPU time limit exceeded (" << s_CpuTimeLimit << " sec)"); tms buffer; if (times(&buffer) == (clock_t)(-1)) { ERR_POST("Error in getting CPU time consumed by program"); break; } LOG_POST("\tuser CPU time : " << buffer.tms_utime/CLK_TCK << " sec"); LOG_POST("\tsystem CPU time : " << buffer.tms_stime/CLK_TCK << " sec"); LOG_POST("\ttotal CPU time : " << (buffer.tms_stime + buffer.tms_utime)/CLK_TCK << " sec"); break; } default: return; } // Write program's time CTime ct(CTime::eCurrent); CTime et(2000, 1, 1); et.AddSecond((int) (ct.GetTimeT() - s_TimeSet.GetTimeT())); LOG_POST("Program's time: " << Endm << "\tstart limit - " << s_TimeSet.AsString() << Endm << "\ttermination - " << ct.AsString() << Endm); et.SetFormat("h:m:s"); LOG_POST("\texecution - " << et.AsString());}// Set routine to be called at the exit from application//static bool s_SetExitHandler(TLimitsPrintHandler handler, TLimitsPrintParameter parameter){ // Set exit routine if it not set yet CFastMutexGuard LOCK(s_ExitHandler_Mutex); if ( !s_ExitHandlerIsSet ) { if (atexit(s_ExitHandler) != 0) { return false; } s_ExitHandlerIsSet = true; s_TimeSet.SetCurrent(); // Store print handler and parameter s_PrintHandler = handler; s_PrintHandlerParam = parameter; // Reserve some memory (10Kb) s_ReserveMemory = new char[10000]; } return true;} #endif /* NCBI_OS_UNIX *////////////////////////////////////////////////////////////////////////////////// SetHeapLimit//#ifdef USE_SETHEAPLIMIT// Handler for operator newstatic void s_NewHandler(void){ s_ExitCode = eLEC_Memory; exit(-1);}bool SetHeapLimit(size_t max_heap_size, TLimitsPrintHandler handler, TLimitsPrintParameter parameter){ if (s_HeapLimit == max_heap_size) return true; if ( !s_SetExitHandler(handler, parameter) ) return false; // Set new heap limit CFastMutexGuard LOCK(s_ExitHandler_Mutex); rlimit rl; if ( max_heap_size ) { set_new_handler(s_NewHandler); rl.rlim_cur = rl.rlim_max = max_heap_size; } else { // Set off heap limit set_new_handler(0); rl.rlim_cur = rl.rlim_max = RLIM_INFINITY; } if (setrlimit(RLIMIT_DATA, &rl) != 0) return false; s_HeapLimit = max_heap_size; return true;}#elsebool SetHeapLimit(size_t max_heap_size, TLimitsPrintHandler handler, TLimitsPrintParameter parameter){ return false;}#endif /* USE_SETHEAPLIMIT *////////////////////////////////////////////////////////////////////////////////// SetCpuTimeLimit//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -