NCBI C++ ToolKit
Macros | Functions | Variables
ncbi_system.cpp File Reference
#include <ncbi_pch.hpp>
#include <windows.h>
#include <crtdbg.h>
#include <stdlib.h>
#include <corelib/ncbimtx.hpp>
#include <corelib/ncbi_system.hpp>
#include <corelib/ncbi_safe_static.hpp>
#include <corelib/error_codes.hpp>
#include <corelib/ncbierror.hpp>
#include "ncbisys.hpp"
#include <array>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/times.h>
#include <sys/types.h>
#include <dirent.h>
#include <limits.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include "ncbi_os_unix_p.hpp"
#include <signal.h>
#include <corelib/ncbidll.hpp>
#include "ncbi_os_mswin_p.hpp"
#include <dbghelp.h>
#include <intrin.h>
+ Include dependency graph for ncbi_system.cpp:

Go to the source code of this file.

Go to the SVN repository for this file.

Macros

#define NCBI_USE_ERRCODE_X   Corelib_System
 
#define USE_SETLIMITS
 
#define USE_SETMEMLIMIT
 
#define USE_SETCPULIMIT
 
#define HAVE_MADVISE   1
 

Functions

static void s_DefaultPrintHandler (void)
 
static void s_HandlerGuard (void)
 
static void s_SignalHandler (int sig)
 
 DEFINE_STATIC_FAST_MUTEX (s_Limits_Handler_Mutex)
 
static void s_SetPrintHandler (TLimitsPrintHandler handler, TLimitsPrintParameter parameter)
 
static void s_NewHandler (void)
 
bool SetMemoryLimit (size_t max_size, TLimitsPrintHandler handler, TLimitsPrintParameter parameter)
 [UNIX only] Set memory limit. More...
 
bool SetMemoryLimitSoft (size_t max_size, TLimitsPrintHandler handler, TLimitsPrintParameter parameter)
 [UNIX only] Set soft memory limit. More...
 
bool SetMemoryLimitHard (size_t max_size, TLimitsPrintHandler handler, TLimitsPrintParameter parameter)
 [UNIX only] Set hard memory limit. More...
 
size_t GetVirtualMemoryLimitSoft (void)
 [UNIX only] Get "soft" memory limit of the virtual memory (address space) in bytes for a current process. More...
 
size_t GetVirtualMemoryLimitHard (void)
 [UNIX only] Get "hard" memory limit of the virtual memory (address space) in bytes for a current process. More...
 
bool SetCpuTimeLimit (unsigned int max_cpu_time, unsigned int terminate_delay_time, TLimitsPrintHandler handler, TLimitsPrintParameter parameter)
 [UNIX only] Set CPU time usage limit. More...
 
bool GetMemoryUsage (size_t *total, size_t *resident, size_t *shared)
 
bool GetCurrentProcessTimes (double *user_time, double *system_time)
 
int GetProcessFDCount (int *soft_limit, int *hard_limit)
 
int GetProcessThreadCount (void)
 [Linux only] Provides the number of threads in the current process. More...
 
bool MemoryAdvise (void *addr, size_t len, EMemoryAdvise advise)
 [UNIX only] Advise on memory usage for specified memory region. More...
 
void SleepMicroSec (unsigned long mc_sec, EInterruptOnSignal onsignal)
 
void SleepMilliSec (unsigned long ml_sec, EInterruptOnSignal onsignal)
 
void SleepSec (unsigned long sec, EInterruptOnSignal onsignal)
 Sleep. More...
 
static LONG CALLBACK _SEH_Handler (EXCEPTION_POINTERS *ep)
 
void SuppressSystemMessageBox (TSuppressSystemMessageBox mode)
 Suppress popup messages on execution errors. More...
 
void DisableSuppressSystemMessageBox (void)
 Prevent run of SuppressSystemMessageBox(). More...
 
bool IsSuppressedDebugSystemMessageBox (void)
 Check if system message box has been suppressed for debug library. More...
 
bool VerifyCpuCompatibility (string *message)
 CPU. More...
 

Variables

std::atomic< bools_IsHandlerSet { false }
 
std::atomic< bools_IsHandlerActive { false }
 
std::atomic< ELimitsExitCodes_Reason { eLEC_None }
 
static CSafeStatic< CTimes_TimeSet
 
static size_t s_MemoryLimitSoft = 0
 
static size_t s_MemoryLimitHard = 0
 
static size_t s_CpuTimeLimit = 0
 
static char * s_ReservedMemory = nullptr
 
static TLimitsPrintHandler s_PrintHandler = nullptr
 
static TLimitsPrintParameter s_PrintHandlerParam = nullptr
 
static bool s_EnableSuppressSystemMessageBox = true
 Suppress Diagnostic Popup Messages (all MS-Win specific) More...
 
static bool s_DoneSuppressSystemMessageBox = false
 
static bool s_SuppressedDebugSystemMessageBox = false
 

Macro Definition Documentation

◆ HAVE_MADVISE

#define HAVE_MADVISE   1

Definition at line 81 of file ncbi_system.cpp.

◆ NCBI_USE_ERRCODE_X

#define NCBI_USE_ERRCODE_X   Corelib_System

Definition at line 48 of file ncbi_system.cpp.

◆ USE_SETCPULIMIT

#define USE_SETCPULIMIT

Definition at line 79 of file ncbi_system.cpp.

◆ USE_SETLIMITS

#define USE_SETLIMITS

Definition at line 77 of file ncbi_system.cpp.

◆ USE_SETMEMLIMIT

#define USE_SETMEMLIMIT

Definition at line 78 of file ncbi_system.cpp.

Function Documentation

◆ _SEH_Handler()

static LONG CALLBACK _SEH_Handler ( EXCEPTION_POINTERS *  ep)
static

Definition at line 1352 of file ncbi_system.cpp.

References FALSE, HANDLE, hex(), INVALID_HANDLE_VALUE, NULL, strcat, and tmp.

Referenced by SuppressSystemMessageBox().

◆ DEFINE_STATIC_FAST_MUTEX()

DEFINE_STATIC_FAST_MUTEX ( s_Limits_Handler_Mutex  )

◆ DisableSuppressSystemMessageBox()

void DisableSuppressSystemMessageBox ( )

Prevent run of SuppressSystemMessageBox().

NOTE: MS Windows-specific. If this function is called, all following calls of SuppressSystemMessageBox() will be ignored. If SuppressSystemMessageBox() was executed before, that this function print out a critical error message. For example can be used in CGI applications where SuppressSystemMessageBox always calls in the CCgiApplication constructor.

Definition at line 1423 of file ncbi_system.cpp.

References Critical(), ERR_POST_X, s_DoneSuppressSystemMessageBox, and s_EnableSuppressSystemMessageBox.

◆ GetCurrentProcessTimes()

bool GetCurrentProcessTimes ( double *  user_time,
double *  system_time 
)
Deprecated:

Definition at line 1032 of file ncbi_system.cpp.

References buf, CLOCKS_PER_SEC, and t.

◆ GetMemoryUsage()

bool GetMemoryUsage ( size_t *  total,
size_t *  resident,
size_t *  shared 
)

◆ GetProcessFDCount()

int GetProcessFDCount ( int soft_limit,
int hard_limit 
)
Deprecated:

Definition at line 1080 of file ncbi_system.cpp.

References NULL.

◆ GetProcessThreadCount()

int GetProcessThreadCount ( void  )

[Linux only] Provides the number of threads in the current process.

Deprecated:

Definition at line 1152 of file ncbi_system.cpp.

References NULL.

◆ GetVirtualMemoryLimitHard()

size_t GetVirtualMemoryLimitHard ( void  )

[UNIX only] Get "hard" memory limit of the virtual memory (address space) in bytes for a current process.

Returns
Returns "hard" value set by setrlimit(), SetMemoryLimit() or ulimit command line utility for virtual memory address space. 0 - if an error occurs and CNcbiError is set, or the memory limit is set to "unlimited".
Note
The implementation of malloc() can be different on many flavors of UNIX, and we usually don't know how exactly it is implemented on the current system. Some systems use sbrk()-based implementation (heap), other use mmap() system call and virtual memory (address space) to allocate memory, some other use hybrid approach and may allocate memory in two different ways depending on requested memory size and certain parameters. Almost all modern Unix versions uses mmap()-based approach for all memory allocations or at least for big chunks of memory, so probably virtual memory limits is more important nowadays.
See also
SetMemoryLimit, GetVirtualMemoryLimitSoft

Definition at line 453 of file ncbi_system.cpp.

References CNcbiError::eNotSupported, CNcbiError::Set(), and CNcbiError::SetFromErrno().

◆ GetVirtualMemoryLimitSoft()

size_t GetVirtualMemoryLimitSoft ( void  )

[UNIX only] Get "soft" memory limit of the virtual memory (address space) in bytes for a current process.

Returns
Returns "soft" value set by setrlimit(), SetMemoryLimit() or ulimit command line utility for virtual memory address space. 0 - if an error occurs and CNcbiError is set, or the memory limit is set to "unlimited".
Note
The implementation of malloc() can be different on many flavors of UNIX, and we usually don't know how exactly it is implemented on the current system. Some systems use sbrk()-based implementation (heap), other use mmap() system call and virtual memory (address space) to allocate memory, some other use hybrid approach and may allocate memory in two different ways depending on requested memory size and certain parameters. Almost all modern Unix versions uses mmap()-based approach for all memory allocations or at least for big chunks of memory, so probably virtual memory limits is more important nowadays.
See also
SetMemoryLimit, GetVirtualMemoryLimitHard

Definition at line 434 of file ncbi_system.cpp.

References CNcbiError::eNotSupported, CNcbiError::Set(), and CNcbiError::SetFromErrno().

◆ IsSuppressedDebugSystemMessageBox()

bool IsSuppressedDebugSystemMessageBox ( )

Check if system message box has been suppressed for debug library.

NOTE: MS Windows-specific.

Definition at line 1434 of file ncbi_system.cpp.

References s_DoneSuppressSystemMessageBox, and s_SuppressedDebugSystemMessageBox.

Referenced by CNcbiDiag::DiagAssertIfSuppressedSystemMessageBox().

◆ MemoryAdvise()

bool MemoryAdvise ( void *  addr,
size_t  len,
EMemoryAdvise  advise 
)

[UNIX only] Advise on memory usage for specified memory region.

Parameters
addrAddress of memory region whose usage is being advised. Some implementation requires that the address start be page-aligned.
lenLength of memory region whose usage is being advised.
adviseAdvise on expected memory usage pattern.
Returns
  • TRUE, if memory advise operation successful.
  • FALSE, if memory advise operation not successful, or is not supported on current platform.
See also
EMemoryAdvise

Definition at line 1188 of file ncbi_system.cpp.

References _T_STDSTRING, _TROUBLE, CNcbiError::eBadAddress, eMADV_DoFork, eMADV_DontFork, eMADV_DontNeed, eMADV_Mergeable, eMADV_Normal, eMADV_Random, eMADV_Sequential, eMADV_Unmergeable, eMADV_WillNeed, CNcbiError::eNotSupported, ERR_POST_X, ERR_POST_X_ONCE, len, NcbiSys_strerror, CNcbiError::Set(), CNcbiError::SetErrno(), and Warning().

Referenced by CMemoryFile_Base::MemMapAdviseAddr().

◆ s_DefaultPrintHandler()

static void s_DefaultPrintHandler ( void  )
static

◆ s_HandlerGuard()

static void s_HandlerGuard ( void  )
static

◆ s_NewHandler()

static void s_NewHandler ( void  )
static

◆ s_SetPrintHandler()

static void s_SetPrintHandler ( TLimitsPrintHandler  handler,
TLimitsPrintParameter  parameter 
)
static

◆ s_SignalHandler()

static void s_SignalHandler ( int  sig)
static

Definition at line 521 of file ncbi_system.cpp.

References _ASSERT, _VERIFY, eLEC_Cpu, s_HandlerGuard(), s_IsHandlerSet, and s_Reason.

Referenced by SetCpuTimeLimit().

◆ SetCpuTimeLimit()

bool SetCpuTimeLimit ( unsigned int  max_cpu_time,
unsigned int  terminate_delay_time,
TLimitsPrintHandler  handler = NULL,
TLimitsPrintParameter  parameter = NULL 
)

[UNIX only] Set CPU time usage limit.

Set the limit for the CPU time that can be consumed by current process.

Parameters
max_cpu_timeThe maximal amount of seconds of CPU time can be consumed by the process. The 0 value lifts off the CPU time restrictions if allowed to do so. For multi-threaded applications this can be a sum of time for all threads (OS dependent).
terminate_delay_timeThe time in seconds that the process will have to terminate itself after receiving a signal about exceeding CPU usage limit. After that it can be killed by OS.
handlerPointer to a print handler used for dump output in the case of reaching CPU usage limit. Use default handler if passed as NULL. Note, that default handler is not async-safe (see attention below), and can lead to coredump and application crash instead of program termination, so use it on your own risk.
parameterParameter carried into the print handler. Can be passed as NULL.
Returns
Completion status.
Note
Setting a low CPU time limit cannot be generally undone to a value higher than "max_cpu_time + terminate_delay_time" at a later time.
Attention
The exit print handler can be registered only once at first call to SetCpuTimeLimit() or SetMemoryLimit(), even if you don't specify handler directly and passed it as NULL! Be aware. All subsequent attempts to set new handler will be ignored, but limits will be changed anyway.
Only async-safe library functions and system calls can be used in the print handler. For example, you cannot use C++ streams (cout/cerr) and printf() calls here, but write() is allowed... You can find a list of such functions in the C++ documentation (see man, Internet and etc). Also, avoid to alter any shared (global) variables, except that are declared to be of storage class and type "volatile sig_atomic_t".
See also
SetMemoryLimit, TLimitsPrintHandler

Definition at line 534 of file ncbi_system.cpp.

References s_CpuTimeLimit, s_SetPrintHandler(), and s_SignalHandler().

Referenced by CNcbiApplicationAPI::x_HonorStandardSettings().

◆ SetMemoryLimit()

bool SetMemoryLimit ( size_t  max_size,
TLimitsPrintHandler  handler = NULL,
TLimitsPrintParameter  parameter = NULL 
)

[UNIX only] Set memory limit.

Set the limit for the size of used memory allocated by the process.

Parameters
max_sizeThe maximal amount of memory in bytes that can be allocated by the process. Use the same limits for process's data segment (including heap) and virtual memory (address space). On 32-bit systems limit is at most 2 GiB, or this resource is unlimited. The 0 value lift off the heap restrictions. This value cannot exceed current hard limit set for the process.
handlerPointer to a print handler used for dump output in the case of reaching memory limit. Use default handler if passed as NULL.
parameterParameter carried into the print handler. Can be passed as NULL. Useful if singular handler is used for setting some limits. See also SetCpuTimeLimit().
Returns
Completion status. It returns TRUE if both, the memory limits for the data segment and virtual memory, limitations were set. It can return FALSE if the limits for the data segment has changed, but setting new values for the virtual memory fails.
Note
By default it sets soft and hard memory limits to the same value.
Setting a limits may not work on some systems, depends on OS, compilation options and etc. Some systems enforce memory limits, other didn't. Also, only privileged process can set hard memory limit.
If the memory limit is reached, any subsequent memory allocations fails.
Attention
The exit print handler can be registered only once at first call to SetCpuTimeLimit() or SetMemoryLimit(), even if you don't specify handler directly and passed it as NULL. Be aware. All subsequent attempts to set new handler will be ignored, but limits will be changed anyway.
if you use std::set_new_handler(), it should be called after SetMemoryLimit(). It override any memory related print handler set previously.
See also
SetCpuTimeLimit, TLimitsPrintHandler, SetMemoryLimitSoft, SetMemoryLimitHard

Definition at line 282 of file ncbi_system.cpp.

References s_MemoryLimitHard, s_MemoryLimitSoft, s_NewHandler(), s_SetPrintHandler(), and CNcbiError::SetFromErrno().

Referenced by CNcbiApplicationAPI::x_HonorStandardSettings().

◆ SetMemoryLimitHard()

bool SetMemoryLimitHard ( size_t  max_size,
TLimitsPrintHandler  handler = NULL,
TLimitsPrintParameter  parameter = NULL 
)

[UNIX only] Set hard memory limit.

Note
The hard limit acts as a ceiling for the soft limit: Current soft memory limit will be automatically decreased, if it exceed new value for the hard memory limit.
Only privileged process can increase current hard level limit.
See also
SetMemoryLimit, SetMemoryLimitSoft

Definition at line 369 of file ncbi_system.cpp.

References s_MemoryLimitHard, s_MemoryLimitSoft, s_NewHandler(), s_SetPrintHandler(), and CNcbiError::SetFromErrno().

◆ SetMemoryLimitSoft()

bool SetMemoryLimitSoft ( size_t  max_size,
TLimitsPrintHandler  handler = NULL,
TLimitsPrintParameter  parameter = NULL 
)

[UNIX only] Set soft memory limit.

Note
The soft limit is the value that the kernel enforces for the corresponding resource. An unprivileged process may only set its soft limit to a value in the range from 0 up to the hard limit, and (irreversibly) lower its hard limit. A privileged process may make arbitrary changes to either limit value.
See also
SetMemoryLimit, SetMemoryLimitHard

Definition at line 322 of file ncbi_system.cpp.

References s_MemoryLimitSoft, s_NewHandler(), s_SetPrintHandler(), and CNcbiError::SetFromErrno().

◆ SleepMicroSec()

void SleepMicroSec ( unsigned long  mc_sec,
EInterruptOnSignal  onsignal 
)

◆ SleepMilliSec()

void SleepMilliSec ( unsigned long  ml_sec,
EInterruptOnSignal  onsignal 
)

Definition at line 1323 of file ncbi_system.cpp.

References kMicroSecondsPerSecond, kMilliSecondsPerSecond, and SleepMicroSec().

Referenced by CEutilsClient::CallWithRetry(), CPSGDataLoader_Impl::CallWithRetry(), CallWithRetry(), CCSRADataLoader_Impl::CallWithRetry(), CSNPDataLoader_Impl::CallWithRetry(), CVDBGraphDataLoader_Impl::CallWithRetry(), CWGSDataLoader_Impl::CallWithRetry(), CGridCommandLineInterfaceApp::Cmd_Suspend(), SNetServerImpl::ConnectAndExec(), CBamUtils::CreateCoverageGraphs(), CServiceDiscovery::DiscoverImpl(), CLockVectorGuard< TLockVect >::DoLock(), CPipe::ExecWait(), CTestThread::GlobalSyncPoint(), SNetServiceImpl::IterateUntilExecOK(), CProcess::Kill(), CProcess::KillGroupById(), CNamedPipeHandle::Listen(), CInterProcessLock::Lock(), CWeakExecuteGuard::Lock(), CMainLoopThread::Main(), SGridWorkerNodeImpl::OfflineRun(), CAppJobDispatcher::OnEngineJobStateChanged(), CNamedPipeHandle::Open(), SRetryProcessing::operator()(), CObjectManagerEngine::PrefetchNotify(), CGridCgiApplication::ProcessRequest(), CPipeHandle::Read(), CProjectServiceTestJob::Run(), CPrimeNumbersJob::Run(), CPeriodicTestJob::Run(), CGetAssembliesJob::Run(), CSeqDBDemo_Threaded::Run(), SGridWorkerNodeImpl::Run(), CAppJobTestApplication::Run(), CGridClientSampleApp::Run(), CSampleNetScheduleClient::Run(), CSampleNetScheduleNode::Run(), CRemoteAppClientSampleApp::Run(), s_ConnectToNamedPipe(), s_GetObject(), s_PeekStdin(), s_ProcessErrorCode(), CPrimeNumberClient::StartJobs(), CThreadGroup::SyncPoint(), CExec::Wait(), CProcess::Wait(), CThread::WaitForAllThreads(), CNamedPipeHandle::Write(), CPipeHandle::Write(), CThreadPoolEngine::x_OnTaskStatusChange(), CPipeHandle::x_Poll(), CNSTDatabase::x_PreCheckConnection(), CAppJobDispatcher::x_StartJob(), CNamedPipeHandle::x_WaitForRead(), and CAsyncWriteCache::~CAsyncWriteCache().

◆ SleepSec()

void SleepSec ( unsigned long  sec,
EInterruptOnSignal  onsignal = eRestartOnSignal 
)

◆ SuppressSystemMessageBox()

void SuppressSystemMessageBox ( TSuppressSystemMessageBox  mode = fSuppress_Default)

Suppress popup messages on execution errors.

NOTE: MS Windows-specific. Suppresses all error message boxes in both runtime and in debug libraries, as well as all General Protection Fault messages.

Definition at line 1383 of file ncbi_system.cpp.

References _SEH_Handler(), fSuppress_Debug, fSuppress_Exception, fSuppress_Runtime, fSuppress_System, s_DoneSuppressSystemMessageBox, s_EnableSuppressSystemMessageBox, and s_SuppressedDebugSystemMessageBox.

Referenced by CNcbiApplicationAPI::AppMain(), CCgiApplication::CCgiApplication(), InitDiagHandler(), main(), and CNcbiTestApplication::x_SetupBoostReporters().

◆ VerifyCpuCompatibility()

bool VerifyCpuCompatibility ( string message)

CPU.

Verify that the CPU, where an application run, is compatible with flags it compiled for.

Definition at line 1453 of file ncbi_system.cpp.

Referenced by CNcbiApplicationAPI::CNcbiApplicationAPI(), and CNcbiApplicationAPI::x_TryInit().

Variable Documentation

◆ s_CpuTimeLimit

size_t s_CpuTimeLimit = 0
static

Definition at line 138 of file ncbi_system.cpp.

Referenced by s_DefaultPrintHandler(), s_HandlerGuard(), and SetCpuTimeLimit().

◆ s_DoneSuppressSystemMessageBox

bool s_DoneSuppressSystemMessageBox = false
static

◆ s_EnableSuppressSystemMessageBox

bool s_EnableSuppressSystemMessageBox = true
static

Suppress Diagnostic Popup Messages (all MS-Win specific)

Definition at line 1347 of file ncbi_system.cpp.

Referenced by DisableSuppressSystemMessageBox(), and SuppressSystemMessageBox().

◆ s_IsHandlerActive

std::atomic<bool> s_IsHandlerActive { false }

Definition at line 132 of file ncbi_system.cpp.

Referenced by s_HandlerGuard().

◆ s_IsHandlerSet

std::atomic<bool> s_IsHandlerSet { false }

Definition at line 131 of file ncbi_system.cpp.

Referenced by s_NewHandler(), s_SetPrintHandler(), and s_SignalHandler().

◆ s_MemoryLimitHard

size_t s_MemoryLimitHard = 0
static

Definition at line 137 of file ncbi_system.cpp.

Referenced by SetMemoryLimit(), and SetMemoryLimitHard().

◆ s_MemoryLimitSoft

size_t s_MemoryLimitSoft = 0
static

◆ s_PrintHandler

TLimitsPrintHandler s_PrintHandler = nullptr
static

Definition at line 140 of file ncbi_system.cpp.

Referenced by s_HandlerGuard(), and s_SetPrintHandler().

◆ s_PrintHandlerParam

TLimitsPrintParameter s_PrintHandlerParam = nullptr
static

Definition at line 141 of file ncbi_system.cpp.

Referenced by s_HandlerGuard(), and s_SetPrintHandler().

◆ s_Reason

std::atomic<ELimitsExitCode> s_Reason { eLEC_None }

◆ s_ReservedMemory

char* s_ReservedMemory = nullptr
static

Definition at line 139 of file ncbi_system.cpp.

Referenced by s_HandlerGuard(), and s_SetPrintHandler().

◆ s_SuppressedDebugSystemMessageBox

bool s_SuppressedDebugSystemMessageBox = false
static

◆ s_TimeSet

CSafeStatic<CTime> s_TimeSet
static

Definition at line 135 of file ncbi_system.cpp.

Referenced by s_HandlerGuard(), and s_SetPrintHandler().

Modified on Fri Jul 19 17:16:45 2024 by modify_doxy.py rev. 669887