NCBI C++ ToolKit
Classes | Typedefs | Enumerations | Functions
ncbi_system.hpp File Reference
#include <corelib/ncbitime.hpp>
#include <time.h>
#include <vector>
#include <bitset>
#include <corelib/ncbi_os_mswin.hpp>
+ Include dependency graph for ncbi_system.hpp:

Go to the source code of this file.

Go to the SVN repository for this file.

Classes

class  CCpuFeatures
 CCpuFeatures –. More...
 
class  CCpuFeatures::InstructionSet
 
class  CSystemInfo
 CSystemInfo –. More...
 

Typedefs

typedef void * TLimitsPrintParameter
 Type of parameter for print handler. More...
 
typedef void(* TLimitsPrintHandler) (ELimitsExitCode, size_t, CTime &, TLimitsPrintParameter)
 Type of handler for printing a dump information after generating any limitation event. More...
 
typedef int TSuppressSystemMessageBox
 Binary OR of "ESuppressSystemMessageBox". More...
 

Enumerations

enum  ELimitsExitCode { eLEC_None , eLEC_Memory , eLEC_Cpu }
 Process limits. More...
 
enum  EMemoryAdvise {
  eMADV_Normal , eMADV_Random , eMADV_Sequential , eMADV_WillNeed ,
  eMADV_DontNeed , eMADV_DoFork , eMADV_DontFork , eMADV_Mergeable ,
  eMADV_Unmergeable
}
 Memory advise. More...
 
enum  ESuppressSystemMessageBox {
  fSuppress_System = (1<<0) , fSuppress_Runtime = (1<<1) , fSuppress_Debug = (1<<2) , fSuppress_Exception = (1<<3) ,
  fSuppress_All , fSuppress_Default = fSuppress_All
}
 Suppress Diagnostic Popup Messages. More...
 

Functions

bool SetMemoryLimit (size_t max_size, TLimitsPrintHandler handler=NULL, TLimitsPrintParameter parameter=NULL)
 [UNIX only] Set memory limit. More...
 
bool SetMemoryLimitSoft (size_t max_size, TLimitsPrintHandler handler=NULL, TLimitsPrintParameter parameter=NULL)
 [UNIX only] Set soft memory limit. More...
 
bool SetMemoryLimitHard (size_t max_size, TLimitsPrintHandler handler=NULL, TLimitsPrintParameter parameter=NULL)
 [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=NULL, TLimitsPrintParameter parameter=NULL)
 [UNIX only] Set CPU time usage limit. More...
 
bool VerifyCpuCompatibility (string *message=nullptr)
 Verify that the CPU, where an application run, is compatible with flags it compiled for. More...
 
string GetProcessUserName (void)
 System/memory information (deprecated, please use CSystemInfo class) More...
 
unsigned int GetCpuCount (void)
 Return number of active CPUs (never less than 1). More...
 
unsigned long GetVirtualMemoryPageSize (void)
 Return virtual memory page size. More...
 
unsigned long GetVirtualMemoryAllocationGranularity (void)
 Return size of an allocation unit (usually it is a multiple of page size). More...
 
Uint8 GetPhysicalMemorySize (void)
 Return the amount of physical memory available in the system. 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=NULL, int *hard_limit=NULL)
 
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 SleepSec (unsigned long sec, EInterruptOnSignal onsignal=eRestartOnSignal)
 Sleep. More...
 
void SleepMilliSec (unsigned long ml_sec, EInterruptOnSignal onsignal=eRestartOnSignal)
 
void SleepMicroSec (unsigned long mc_sec, EInterruptOnSignal onsignal=eRestartOnSignal)
 
void SuppressSystemMessageBox (TSuppressSystemMessageBox mode=fSuppress_Default)
 Suppress popup messages on execution errors. More...
 
void DisableSuppressSystemMessageBox ()
 Prevent run of SuppressSystemMessageBox(). More...
 
bool IsSuppressedDebugSystemMessageBox ()
 Check if system message box has been suppressed for debug library. More...
 

Typedef Documentation

◆ TLimitsPrintHandler

typedef void(* TLimitsPrintHandler) (ELimitsExitCode, size_t, CTime &, TLimitsPrintParameter)

Type of handler for printing a dump information after generating any limitation event.

Attention
If you use handlers together with SetCpuLimit(), be aware! See SetCpuLimit() for details.
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.
See also
SetCpuTimeLimit, SetMemoryLimit

Definition at line 84 of file ncbi_system.hpp.

◆ TLimitsPrintParameter

typedef void* TLimitsPrintParameter

Type of parameter for print handler.

Definition at line 70 of file ncbi_system.hpp.

◆ TSuppressSystemMessageBox

Binary OR of "ESuppressSystemMessageBox".

Definition at line 591 of file ncbi_system.hpp.

Enumeration Type Documentation

◆ ELimitsExitCode

Process limits.

If, during the program execution, the process exceed any from limits (see ELimitsExitCodeMemory) then: 1) Dump info about current program's state to log-stream.

  • if defined print handler "handler", then it will be used.
  • if defined "parameter", it will be passed into print handler; 2) Terminate the program. One joint print handler for all limit types is used. Code for program's exit handler.
Enumerator
eLEC_None 

Normal exit.

eLEC_Memory 

Memory limit.

eLEC_Cpu 

CPU time usage limit.

Definition at line 63 of file ncbi_system.hpp.

◆ EMemoryAdvise

Memory advise.

What type of data access pattern will be used for specified memory region.

Advises the VM system that the a certain region of memory will be accessed following a type of pattern. The VM system uses this information to optimize work with mapped memory.

NOTE: Works on UNIX platform only.

Enumerator
eMADV_Normal 

No further special treatment – by default.

eMADV_Random 

Expect random page references.

eMADV_Sequential 

Expect sequential page references.

eMADV_WillNeed 

Expect access in the near future.

eMADV_DontNeed 

Do not expect access in the near future.

eMADV_DoFork 

Do inherit across fork() – by default.

eMADV_DontFork 

Don't inherit across fork()

eMADV_Mergeable 

KSM may merge identical pages.

eMADV_Unmergeable 

KSM may not merge identical pages – by default.

Definition at line 513 of file ncbi_system.hpp.

◆ ESuppressSystemMessageBox

Suppress Diagnostic Popup Messages.

Suppress modes

Enumerator
fSuppress_System 

System errors.

fSuppress_Runtime 

Runtime library.

fSuppress_Debug 

Debug library.

fSuppress_Exception 

Unhandled exceptions.

fSuppress_All 
fSuppress_Default 

Definition at line 581 of file ncbi_system.hpp.

Function Documentation

◆ 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.

◆ GetCpuCount()

unsigned int GetCpuCount ( void  )
inline

Return number of active CPUs (never less than 1).

Deprecated:
Please use CSystemInfo::GetCpuCount()

Definition at line 445 of file ncbi_system.hpp.

References CSystemInfo::GetCpuCount().

Referenced by CMapperMTArgs::ExtractAlgorithmOptions(), COMSSABase::SetThreadCount(), and CWriteDB_LMDB::x_CommitTransaction().

◆ GetCurrentProcessTimes()

bool GetCurrentProcessTimes ( double *  user_time,
double *  system_time 
)
Deprecated:
Please use C[Current]Process::GetTimes()
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 
)

◆ GetPhysicalMemorySize()

Uint8 GetPhysicalMemorySize ( void  )
inline

Return the amount of physical memory available in the system.

Deprecated:
Please use CSystemInfo::GetTotalPhysicalMemorySize()

Definition at line 469 of file ncbi_system.hpp.

References CSystemInfo::GetTotalPhysicalMemorySize().

Referenced by CContigAssembly::BandedGlobalAlignment().

◆ GetProcessFDCount()

int GetProcessFDCount ( int soft_limit,
int hard_limit 
)
Deprecated:
Please use C[Current]Process::GetFileDescriptorsCount
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:
Please use C[Current]Process::GetThreadCount
Deprecated:

Definition at line 1152 of file ncbi_system.cpp.

References NULL.

◆ GetProcessUserName()

string GetProcessUserName ( void  )
inline

System/memory information (deprecated, please use CSystemInfo class)

Get process owner actual user name

Deprecated:
Please use CSystemInfo::GetUserName()

Definition at line 437 of file ncbi_system.hpp.

References CSystemInfo::GetUserName().

◆ GetVirtualMemoryAllocationGranularity()

unsigned long GetVirtualMemoryAllocationGranularity ( void  )
inline

Return size of an allocation unit (usually it is a multiple of page size).

Deprecated:
Please use CSystemInfo::GetVirtualMemoryAllocationGranularity()

Definition at line 461 of file ncbi_system.hpp.

References CSystemInfo::GetVirtualMemoryAllocationGranularity().

◆ 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().

◆ GetVirtualMemoryPageSize()

unsigned long GetVirtualMemoryPageSize ( void  )
inline

Return virtual memory page size.

Deprecated:
Please use CSystemInfo::GetVirtualMemoryPageSize()

Definition at line 453 of file ncbi_system.hpp.

References CSystemInfo::GetVirtualMemoryPageSize().

◆ 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().

◆ 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 = eRestartOnSignal 
)

◆ SleepMilliSec()

void SleepMilliSec ( unsigned long  ml_sec,
EInterruptOnSignal  onsignal = eRestartOnSignal 
)

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)

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

Right now it checks on SSE 4.2 only.

Parameters
messageOptional pointer to a string that will receive a description of the problem if function returns FALSE.
Returns
TRUE if CPU is compatible and application can run on it, FALSE otherwise.

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().

Modified on Wed Sep 04 15:06:15 2024 by modify_doxy.py rev. 669887