1 //===- Win32/Process.cpp - Win32 Process Implementation ------- -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Jeff Cohen and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the Win32 specific implementation of the Process class.
12 //===----------------------------------------------------------------------===//
19 #pragma comment(lib, "psapi.lib")
21 //===----------------------------------------------------------------------===//
22 //=== WARNING: Implementation here must contain only Win32 specific code
23 //=== and must not be UNIX code
24 //===----------------------------------------------------------------------===//
27 // This ban should be lifted when MinGW 1.0+ has defined this value.
34 // This function retrieves the page size using GetSystemInfo and is present
35 // solely so it can be called once in Process::GetPageSize to initialize the
36 // static variable PageSize.
37 inline unsigned GetPageSizeOnce() {
38 // NOTE: A 32-bit application running under WOW64 is supposed to use
39 // GetNativeSystemInfo. However, this interface is not present prior
40 // to Windows XP so to use it requires dynamic linking. It is not clear
41 // how this affects the reported page size, if at all. One could argue
42 // that LLVM ought to run as 64-bits on a 64-bit system, anyway.
45 return static_cast<unsigned>(info.dwPageSize);
49 Process::GetPageSize() {
50 static const unsigned PageSize = GetPageSizeOnce();
55 Process::GetMallocUsage()
62 while (_heapwalk(&hinfo) == _HEAPOK)
69 Process::GetTotalMemoryUsage()
71 PROCESS_MEMORY_COUNTERS pmc;
72 GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
73 return pmc.PagefileUsage;
77 Process::GetTimeUsage(
78 TimeValue& elapsed, TimeValue& user_time, TimeValue& sys_time)
80 elapsed = TimeValue::now();
82 uint64_t ProcCreate, ProcExit, KernelTime, UserTime;
83 GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate,
84 (FILETIME*)&ProcExit, (FILETIME*)&KernelTime,
85 (FILETIME*)&UserTime);
87 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
88 user_time.seconds( UserTime / 10000000 );
89 user_time.nanoseconds( unsigned(UserTime % 10000000) * 100 );
90 sys_time.seconds( KernelTime / 10000000 );
91 sys_time.nanoseconds( unsigned(KernelTime % 10000000) * 100 );
94 int Process::GetCurrentUserId()
99 int Process::GetCurrentGroupId()
104 // Some LLVM programs such as bugpoint produce core files as a normal part of
105 // their operation. To prevent the disk from filling up, this configuration item
106 // does what's necessary to prevent their generation.
107 void Process::PreventCoreFiles() {
108 // Windows doesn't do core files, but it does do modal pop-up message
109 // boxes. As this method is used by bugpoint, preventing these pop-ups
110 // is the moral equivalent of suppressing core files.
111 SetErrorMode(SEM_FAILCRITICALERRORS |
112 SEM_NOGPFAULTERRORBOX |
113 SEM_NOOPENFILEERRORBOX);
116 bool Process::StandardInIsUserInput() {
117 return GetFileType((HANDLE)_get_osfhandle(0)) == FILE_TYPE_CHAR;
120 bool Process::StandardOutIsDisplayed() {
121 return GetFileType((HANDLE)_get_osfhandle(1)) == FILE_TYPE_CHAR;
124 bool Process::StandardErrIsDisplayed() {
125 return GetFileType((HANDLE)_get_osfhandle(2)) == FILE_TYPE_CHAR;