LLVM API Documentation
00001 //===- Win32/Process.cpp - Win32 Process Implementation ------- -*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by Jeff Cohen and is distributed under the 00006 // University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file provides the Win32 specific implementation of the Process class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "Win32.h" 00015 #include <psapi.h> 00016 #include <malloc.h> 00017 #include <io.h> 00018 00019 #pragma comment(lib, "psapi.lib") 00020 00021 //===----------------------------------------------------------------------===// 00022 //=== WARNING: Implementation here must contain only Win32 specific code 00023 //=== and must not be UNIX code 00024 //===----------------------------------------------------------------------===// 00025 00026 #ifdef __MINGW32__ 00027 // This ban should be lifted when MinGW 1.0+ has defined this value. 00028 # define _HEAPOK (-2) 00029 #endif 00030 00031 namespace llvm { 00032 using namespace sys; 00033 00034 // This function retrieves the page size using GetSystemInfo and is present 00035 // solely so it can be called once in Process::GetPageSize to initialize the 00036 // static variable PageSize. 00037 inline unsigned GetPageSizeOnce() { 00038 // NOTE: A 32-bit application running under WOW64 is supposed to use 00039 // GetNativeSystemInfo. However, this interface is not present prior 00040 // to Windows XP so to use it requires dynamic linking. It is not clear 00041 // how this affects the reported page size, if at all. One could argue 00042 // that LLVM ought to run as 64-bits on a 64-bit system, anyway. 00043 SYSTEM_INFO info; 00044 GetSystemInfo(&info); 00045 return static_cast<unsigned>(info.dwPageSize); 00046 } 00047 00048 unsigned 00049 Process::GetPageSize() { 00050 static const unsigned PageSize = GetPageSizeOnce(); 00051 return PageSize; 00052 } 00053 00054 size_t 00055 Process::GetMallocUsage() 00056 { 00057 _HEAPINFO hinfo; 00058 hinfo._pentry = NULL; 00059 00060 size_t size = 0; 00061 00062 while (_heapwalk(&hinfo) == _HEAPOK) 00063 size += hinfo._size; 00064 00065 return size; 00066 } 00067 00068 size_t 00069 Process::GetTotalMemoryUsage() 00070 { 00071 PROCESS_MEMORY_COUNTERS pmc; 00072 GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)); 00073 return pmc.PagefileUsage; 00074 } 00075 00076 void 00077 Process::GetTimeUsage( 00078 TimeValue& elapsed, TimeValue& user_time, TimeValue& sys_time) 00079 { 00080 elapsed = TimeValue::now(); 00081 00082 uint64_t ProcCreate, ProcExit, KernelTime, UserTime; 00083 GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate, 00084 (FILETIME*)&ProcExit, (FILETIME*)&KernelTime, 00085 (FILETIME*)&UserTime); 00086 00087 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond) 00088 user_time.seconds( UserTime / 10000000 ); 00089 user_time.nanoseconds( unsigned(UserTime % 10000000) * 100 ); 00090 sys_time.seconds( KernelTime / 10000000 ); 00091 sys_time.nanoseconds( unsigned(KernelTime % 10000000) * 100 ); 00092 } 00093 00094 int Process::GetCurrentUserId() 00095 { 00096 return 65536; 00097 } 00098 00099 int Process::GetCurrentGroupId() 00100 { 00101 return 65536; 00102 } 00103 00104 // Some LLVM programs such as bugpoint produce core files as a normal part of 00105 // their operation. To prevent the disk from filling up, this configuration item 00106 // does what's necessary to prevent their generation. 00107 void Process::PreventCoreFiles() { 00108 // Windows doesn't do core files, but it does do modal pop-up message 00109 // boxes. As this method is used by bugpoint, preventing these pop-ups 00110 // is the moral equivalent of suppressing core files. 00111 SetErrorMode(SEM_FAILCRITICALERRORS | 00112 SEM_NOGPFAULTERRORBOX | 00113 SEM_NOOPENFILEERRORBOX); 00114 } 00115 00116 bool Process::StandardInIsUserInput() { 00117 return GetFileType((HANDLE)_get_osfhandle(0)) == FILE_TYPE_CHAR; 00118 } 00119 00120 bool Process::StandardOutIsDisplayed() { 00121 return GetFileType((HANDLE)_get_osfhandle(1)) == FILE_TYPE_CHAR; 00122 } 00123 00124 bool Process::StandardErrIsDisplayed() { 00125 return GetFileType((HANDLE)_get_osfhandle(2)) == FILE_TYPE_CHAR; 00126 } 00127 00128 }