Open
Graph Drawing
Framework

 v.2012.05
 

System.h
Go to the documentation of this file.
00001 /*
00002  * $Revision: 2299 $
00003  * 
00004  * last checkin:
00005  *   $Author: gutwenger $ 
00006  *   $Date: 2012-05-07 15:57:08 +0200 (Mon, 07 May 2012) $ 
00007  ***************************************************************/
00008  
00044 #ifdef _MSC_VER
00045 #pragma once
00046 #endif
00047 
00048 #ifndef OGDF_SYSTEM_H
00049 #define OGDF_SYSTEM_H
00050 
00051 
00052 #include <ogdf/basic/basic.h>
00053 #if defined(OGDF_SYSTEM_OSX)
00054 #include <stdlib.h>
00055 #elif defined(OGDF_SYSTEM_UNIX)
00056 #include <malloc.h>
00057 #endif
00058 
00059 
00060 // detect processor architecture we're compiling for
00061 //
00062 // OGDF_ARCH_X86       Intel / AMD x86 32-bit processors
00063 // OGDF_ARCH_X64       Intel / AMD x86 64-bit processors
00064 // OGDF_ARCH_IA64      Intel Itanium
00065 // OGDF_ARCH_PPC       PowerPC
00066 // OGDF_ARCH_SPARC     SUN SPARC
00067 // OGDF_ARCH_SPARC_V9  SUN SPARC V9
00068 
00069 #if defined(_M_X64) || defined(__x86_64__)
00070 #define OGDF_ARCH_X64
00071 #elif defined(_M_IX86) || defined(__i386__)
00072 #define OGDF_ARCH_X86
00073 #elif defined(_M_IA64) || defined(__ia64__)
00074 #define OGDF_ARCH_IA64
00075 #elif defined(_M_MPPC) || defined(_M_PPC) || defined(__powerpc__)
00076 #define OGDF_ARCH_PPC
00077 #elif defined(__sparc__)
00078 #define OGDF_ARCH_SPARC
00079 #elif defined(__sparc_v9__)
00080 #define OGDF_ARCH_SPARC_V9
00081 #endif
00082 
00083 // use SSE2 always if x64-platform or compiler option is set
00084 #ifndef OGDF_USE_SSE2
00085 #if defined(OGDF_ARCH_X64)
00086 #define OGDF_USE_SSE2
00087 #elif defined(_MSC_VER)
00088 #if _M_IX86_FP >= 2
00089 #define OGDF_USE_SSE2
00090 #endif
00091 #endif
00092 #endif
00093 
00094 // macros to check for using special cpu features
00095 //
00096 // OGDF_CHECK_SSE2   returns true if SSE2 can be used
00097 
00098 #ifdef OGDF_USE_SSE2
00099 #define OGDF_CHECK_SSE2 true
00100 #elif defined(OGDF_ARCH_X86)
00101 #define OGDF_CHECK_SSE2 ogdf::System::cpuSupports(ogdf::cpufSSE2)
00102 #else
00103 #define OGDF_USE_SSE2 false
00104 #endif
00105 
00106 
00107 namespace ogdf {
00108 
00110 
00115 enum CPUFeature {
00116     cpufMMX,    
00117     cpufSSE,    
00118     cpufSSE2,   
00119     cpufSSE3,   
00120     cpufSSSE3,  
00121     cpufSSE4_1, 
00122     cpufSSE4_2, 
00123     cpufVMX,    
00124     cpufSMX,    
00125     cpufEST,    
00126     cpufMONITOR 
00127 };
00128 
00130 enum CPUFeatureMask {
00131     cpufmMMX     = 1 << cpufMMX,    
00132     cpufmSSE     = 1 << cpufSSE,    
00133     cpufmSSE2    = 1 << cpufSSE2,   
00134     cpufmSSE3    = 1 << cpufSSE3,   
00135     cpufmSSSE3   = 1 << cpufSSSE3,  
00136     cpufmSSE4_1  = 1 << cpufSSE4_1, 
00137     cpufmSSE4_2  = 1 << cpufSSE4_2, 
00138     cpufmVMX     = 1 << cpufVMX,    
00139     cpufmSMX     = 1 << cpufSMX,    
00140     cpufmEST     = 1 << cpufEST,    
00141     cpufmMONITOR = 1 << cpufMONITOR 
00142 };
00143 
00144 
00146 
00155 class OGDF_EXPORT System {
00156 
00157     //friend class ::OgdfInitialization;
00158 
00159 public:
00166 
00167     static void *alignedMemoryAlloc16(size_t size) {
00168         size_t alignment = 16;
00169 #ifdef OGDF_SYSTEM_WINDOWS
00170         return _aligned_malloc(size,alignment);
00171 #elif defined(OGDF_SYSTEM_OSX)
00172         // malloc returns 16 byte aligned memory on OS X.
00173         return malloc(size);
00174 #else
00175         return memalign(alignment,size);
00176 #endif
00177     }
00178 
00179     static void alignedMemoryFree(void *p) {
00180 #ifdef OGDF_SYSTEM_WINDOWS
00181         _aligned_free(p);
00182 #else
00183         free(p);
00184 #endif
00185     }
00186 
00188     static int pageSize() { return s_pageSize; }
00189 
00191     static long long physicalMemory();
00192     
00194     static long long availablePhysicalMemory();
00195 
00197     static size_t memoryUsedByProcess();
00198 
00199 #if defined(OGDF_SYSTEM_WINDOWS) || defined(__CYGWIN__)
00200 
00201     static size_t peakMemoryUsedByProcess();
00202 #endif
00203 
00205 
00215     static size_t memoryAllocatedByMemoryManager();
00216 
00218     static size_t memoryInGlobalFreeListOfMemoryManager();
00219 
00221     static size_t memoryInThreadFreeListOfMemoryManager();
00222 
00224 
00228     static size_t memoryAllocatedByMalloc();
00229 
00231 
00235     static size_t memoryInFreelistOfMalloc();
00236 
00237 #if defined(OGDF_SYSTEM_WINDOWS) || defined(__CYGWIN__)
00238 
00239 
00246 
00248     static void getHPCounter(LARGE_INTEGER &counter);
00249 
00251     static double elapsedSeconds(
00252         const LARGE_INTEGER &startCounter,
00253         const LARGE_INTEGER &endCounter);
00254 #endif
00255 
00257 
00263     static __int64 usedRealTime(__int64 &t);
00264 
00265 
00267 
00274 
00276     static int cpuFeatures() { return s_cpuFeatures; }
00277 
00279     static bool cpuSupports(CPUFeature feature) {
00280         return (s_cpuFeatures & (1 << feature)) != 0;
00281     }
00282 
00284     static int cacheSizeKBytes() { return s_cacheSize; }
00285 
00287     static int cacheLineBytes() { return s_cacheLine; }
00288 
00290     static int numberOfProcessors() { return s_numberOfProcessors; }
00291 
00293 
00294 private:
00295     static unsigned int s_cpuFeatures; 
00296     static int          s_cacheSize;   
00297     static int          s_cacheLine;   
00298     static int          s_numberOfProcessors; 
00299     static int          s_pageSize;    
00300 
00301 #if defined(OGDF_SYSTEM_WINDOWS) || defined(__CYGWIN__)
00302     static LARGE_INTEGER s_HPCounterFrequency; 
00303 #endif
00304 
00305 public:
00307     static void init();
00308 };
00309 
00310 
00311 } // end namespace ogdf
00312 
00313 
00314 #endif