Skip to content

Commit b13f6a8

Browse files
N-Dekkerthewtex
authored andcommitted
PERF: Remove SystemInformation data from ResourceProbe, fix issue #350
ITK 4.9 added system info to `itk::ResourceProbe` (the base class of `itk::TimeProbe` and `itk::MemoryProbe`), by calling `GetSystemInformation()` in its constructor. This function call appeared very time consuming. Using Visual Studio 2017 (Release configuration), it was observed that a single `GetSystemInformation()` function call took more than 0.1 second. This commit removes the `this->GetSystemInformation()` function call from ResourceProbe, deprecates the member function, removes the related data members, and only retrieves the system info when and where it is actually being used: in `PrintSystemInformation` and `PrintJSONSystemInformation`. Fixes issue #350, "Major performance issue TimeProbe, ResourceProbe constructor", which was based on the analysis of an `elastix` example case by Theo van Walsum (@tvanwalsum).
1 parent 4a6e8c8 commit b13f6a8

2 files changed

Lines changed: 53 additions & 79 deletions

File tree

Modules/Core/Common/include/itkResourceProbe.h

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ class ITK_TEMPLATE_EXPORT ResourceProbe
151151
void PrintJSONvar(std::ostream & os, const char* varName, T varValue,
152152
unsigned indent = 4, bool comma = true);
153153

154-
/** Get System information */
155-
virtual void GetSystemInformation();
154+
/** Obsolete member function from ITK 4.8 - 4.13. Does not do anything anymore. */
155+
itkLegacyMacro(virtual void GetSystemInformation());
156156

157157
private:
158158

@@ -174,22 +174,6 @@ class ITK_TEMPLATE_EXPORT ResourceProbe
174174
std::string m_TypeString;
175175
std::string m_UnitString;
176176

177-
std::string m_SystemName;
178-
std::string m_ProcessorName;
179-
int m_ProcessorCacheSize;
180-
float m_ProcessorClockFrequency;
181-
unsigned int m_NumberOfPhysicalCPU;
182-
unsigned int m_NumberOfLogicalCPU;
183-
std::string m_OSName;
184-
std::string m_OSRelease;
185-
std::string m_OSVersion;
186-
std::string m_OSPlatform;
187-
bool m_Is64Bits;
188-
std::string m_ITKVersion;
189-
size_t m_TotalVirtualMemory;
190-
size_t m_AvailableVirtualMemory;
191-
size_t m_TotalPhysicalMemory;
192-
size_t m_AvailablePhysicalMemory;
193177

194178
static ITK_CONSTEXPR_VAR unsigned int tabwidth = 15;
195179
};

Modules/Core/Common/include/itkResourceProbe.hxx

Lines changed: 51 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ ResourceProbe< ValueType, MeanType >
4949
::ResourceProbe(const std::string & type, const std::string & unit):
5050
m_TypeString(type), m_UnitString(unit)
5151
{
52-
this->GetSystemInformation();
5352
this->Reset();
5453
}
5554

@@ -261,30 +260,35 @@ void
261260
ResourceProbe< ValueType, MeanType >
262261
::PrintSystemInformation(std::ostream & os)
263262
{
264-
os << "System: " << m_SystemName << std::endl;
265-
os << "Processor: " << m_ProcessorName << std::endl;
266-
os << " Cache: " << m_ProcessorCacheSize << std::endl;
267-
os << " Clock: " << m_ProcessorClockFrequency << std::endl;
268-
os << " Physical CPUs: " << m_NumberOfPhysicalCPU << std::endl;
269-
os << " Logical CPUs: " << m_NumberOfLogicalCPU << std::endl;
270-
// Retrieve memory information in megabyte.
263+
itksys::SystemInformation systeminfo;
264+
systeminfo.RunCPUCheck();
265+
systeminfo.RunMemoryCheck();
266+
systeminfo.RunOSCheck();
267+
268+
os << "System: " << systeminfo.GetHostname() << std::endl;
269+
os << "Processor: " << systeminfo.GetExtendedProcessorName() << std::endl;
270+
os << " Cache: " << systeminfo.GetProcessorCacheSize() << std::endl;
271+
os << " Clock: " << systeminfo.GetProcessorClockFrequency() << std::endl;
272+
os << " Physical CPUs: " << systeminfo.GetNumberOfPhysicalCPU() << std::endl;
273+
os << " Logical CPUs: " << systeminfo.GetNumberOfLogicalCPU() << std::endl;
274+
// Retrieve memory information in mebibytes.
271275
os << " Virtual Memory: Total: "
272-
<< std::left << std::setw( tabwidth ) << m_TotalVirtualMemory
273-
<<" Available: "<< m_AvailableVirtualMemory << std::endl;
276+
<< std::left << std::setw( tabwidth ) << systeminfo.GetTotalVirtualMemory()
277+
<<" Available: "<< systeminfo.GetAvailableVirtualMemory() << std::endl;
274278
os << " Physical Memory: Total: "
275-
<< std::left << std::setw( tabwidth ) << m_TotalPhysicalMemory
276-
<<" Available: "<< m_AvailablePhysicalMemory << std::endl;
279+
<< std::left << std::setw( tabwidth ) << systeminfo.GetTotalPhysicalMemory()
280+
<<" Available: "<< systeminfo.GetAvailablePhysicalMemory() << std::endl;
277281

278-
os << "OSName: "<< m_OSName << std::endl;
279-
os << " Release: "<< m_OSRelease << std::endl;
280-
os << " Version: "<< m_OSVersion << std::endl;
281-
os << " Platform: "<< m_OSPlatform << std::endl;
282+
os << "OSName: "<< systeminfo.GetOSName() << std::endl;
283+
os << " Release: "<< systeminfo.GetOSRelease() << std::endl;
284+
os << " Version: "<< systeminfo.GetOSVersion() << std::endl;
285+
os << " Platform: "<< systeminfo.GetOSPlatform() << std::endl;
282286

283287
os << " Operating System is "
284-
<< (m_Is64Bits?"64 bit":"32 bit") << std::endl;
288+
<< (systeminfo.Is64Bits()?"64 bit":"32 bit") << std::endl;
285289

286290
os << "ITK Version: "
287-
<< m_ITKVersion << std::endl;
291+
<< ITK_VERSION_STRING << "." << ITK_VERSION_PATCH << std::endl;
288292
}
289293

290294

@@ -574,66 +578,52 @@ void
574578
ResourceProbe< ValueType, MeanType >
575579
::PrintJSONSystemInformation(std::ostream & os)
576580
{
581+
itksys::SystemInformation systeminfo;
582+
systeminfo.RunCPUCheck();
583+
systeminfo.RunMemoryCheck();
584+
systeminfo.RunOSCheck();
585+
577586
os << "{\n";
578-
PrintJSONvar(os, "System", m_SystemName);
587+
PrintJSONvar(os, "System", systeminfo.GetHostname());
579588

580589
os << " \"Processor\" :{\n";
581-
PrintJSONvar(os, "Name", m_ProcessorName, 6);
582-
PrintJSONvar(os, "Cache", m_ProcessorCacheSize, 6);
583-
PrintJSONvar(os, "Clock", m_ProcessorClockFrequency, 6);
584-
PrintJSONvar(os, "Physical CPUs", m_NumberOfPhysicalCPU, 6);
585-
PrintJSONvar(os, "Logical CPUs", m_NumberOfLogicalCPU, 6);
586-
PrintJSONvar(os, "Virtual Memory Total", m_TotalVirtualMemory, 6);
587-
PrintJSONvar(os, "Virtual Memory Available", m_AvailableVirtualMemory, 6);
588-
PrintJSONvar(os, "Physical Memory Total", m_TotalPhysicalMemory, 6);
589-
PrintJSONvar(os, "Physical Memory Available", m_AvailablePhysicalMemory, 6, false);
590+
PrintJSONvar(os, "Name", systeminfo.GetExtendedProcessorName(), 6);
591+
PrintJSONvar(os, "Cache", systeminfo.GetProcessorCacheSize(), 6);
592+
PrintJSONvar(os, "Clock", systeminfo.GetProcessorClockFrequency(), 6);
593+
PrintJSONvar(os, "Physical CPUs", systeminfo.GetNumberOfPhysicalCPU(), 6);
594+
PrintJSONvar(os, "Logical CPUs", systeminfo.GetNumberOfLogicalCPU(), 6);
595+
PrintJSONvar(os, "Virtual Memory Total", systeminfo.GetTotalVirtualMemory(), 6);
596+
PrintJSONvar(os, "Virtual Memory Available", systeminfo.GetAvailableVirtualMemory(), 6);
597+
PrintJSONvar(os, "Physical Memory Total", systeminfo.GetTotalPhysicalMemory(), 6);
598+
PrintJSONvar(os, "Physical Memory Available", systeminfo.GetAvailablePhysicalMemory(), 6, false);
590599
os << " },\n";
591600

592601
os << " \"OperatingSystem\" :{\n";
593-
PrintJSONvar(os, "Name", m_OSName, 6);
594-
PrintJSONvar(os, "Release", m_OSRelease, 6);
595-
PrintJSONvar(os, "Version", m_OSVersion, 6);
596-
PrintJSONvar(os, "Platform", m_OSPlatform, 6);
597-
PrintJSONvar(os, "Bitness", (m_Is64Bits ? "64 bit" : "32 bit"), 6, false);
602+
PrintJSONvar(os, "Name", systeminfo.GetOSName(), 6);
603+
PrintJSONvar(os, "Release", systeminfo.GetOSRelease(), 6);
604+
PrintJSONvar(os, "Version", systeminfo.GetOSVersion(), 6);
605+
PrintJSONvar(os, "Platform", systeminfo.GetOSPlatform(), 6);
606+
PrintJSONvar(os, "Bitness", (systeminfo.Is64Bits() ? "64 bit" : "32 bit"), 6, false);
598607
os << " },\n";
599608

600-
PrintJSONvar(os, "ITKVersion", m_ITKVersion, 4, false);
609+
std::ostringstream itkVersionStringStream;
610+
itkVersionStringStream << ITK_VERSION_STRING << "." << ITK_VERSION_PATCH;
611+
612+
PrintJSONvar(os, "ITKVersion", itkVersionStringStream.str(), 4, false);
601613
os << " }";
602614
}
603615

616+
617+
// This protected member function that was introduced with ITK 4.8 has been deprecated
618+
// as of ITK 5.0. Please do not call or override this member function.
619+
#if !defined(ITK_LEGACY_REMOVE)
604620
template< typename ValueType, typename MeanType >
605621
void
606622
ResourceProbe< ValueType, MeanType >
607623
::GetSystemInformation()
608624
{
609-
itksys::SystemInformation systeminfo;
610-
systeminfo.RunCPUCheck();
611-
systeminfo.RunMemoryCheck();
612-
systeminfo.RunOSCheck();
613-
614-
m_SystemName = systeminfo.GetHostname();
615-
m_ProcessorName = systeminfo.GetExtendedProcessorName();
616-
m_ProcessorCacheSize = systeminfo.GetProcessorCacheSize();
617-
m_ProcessorClockFrequency = systeminfo.GetProcessorClockFrequency();
618-
m_NumberOfPhysicalCPU = systeminfo.GetNumberOfPhysicalCPU();
619-
m_NumberOfLogicalCPU = systeminfo.GetNumberOfLogicalCPU();
620-
621-
m_OSName = systeminfo.GetOSName();
622-
m_OSRelease = systeminfo.GetOSRelease();
623-
m_OSVersion = systeminfo.GetOSVersion();
624-
m_OSPlatform = systeminfo.GetOSPlatform();
625-
626-
m_Is64Bits = systeminfo.Is64Bits();
627-
std::ostringstream itkversion;
628-
itkversion << ITK_VERSION_MAJOR << "." << ITK_VERSION_MINOR << "." << ITK_VERSION_PATCH;
629-
m_ITKVersion = itkversion.str();
630-
631-
// Retrieve memory information in megabyte.
632-
m_TotalVirtualMemory = systeminfo.GetTotalVirtualMemory();
633-
m_AvailableVirtualMemory = systeminfo.GetAvailableVirtualMemory();
634-
m_TotalPhysicalMemory = systeminfo.GetTotalPhysicalMemory();
635-
m_AvailablePhysicalMemory = systeminfo.GetAvailablePhysicalMemory();
636625
}
626+
#endif
637627

638628
} // end namespace itk
639629

0 commit comments

Comments
 (0)