LLVM API Documentation

Win32/Process.inc

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