1 //===- Win32/Process.cpp - Win32 Process Implementation ------- -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the Win32 specific implementation of the Process class.
12 //===----------------------------------------------------------------------===//
20 #if (HAVE_LIBPSAPI != 1)
21 #error "libpsapi.a should be present"
24 #pragma comment(lib, "psapi.lib")
27 //===----------------------------------------------------------------------===//
28 //=== WARNING: Implementation here must contain only Win32 specific code
29 //=== and must not be UNIX code
30 //===----------------------------------------------------------------------===//
33 // This ban should be lifted when MinGW 1.0+ has defined this value.
40 // This function retrieves the page size using GetSystemInfo and is present
41 // solely so it can be called once in Process::GetPageSize to initialize the
42 // static variable PageSize.
43 inline unsigned GetPageSizeOnce() {
44 // NOTE: A 32-bit application running under WOW64 is supposed to use
45 // GetNativeSystemInfo. However, this interface is not present prior
46 // to Windows XP so to use it requires dynamic linking. It is not clear
47 // how this affects the reported page size, if at all. One could argue
48 // that LLVM ought to run as 64-bits on a 64-bit system, anyway.
51 return static_cast<unsigned>(info.dwPageSize);
55 Process::GetPageSize() {
56 static const unsigned PageSize = GetPageSizeOnce();
61 Process::GetMallocUsage()
68 while (_heapwalk(&hinfo) == _HEAPOK)
75 Process::GetTotalMemoryUsage()
77 PROCESS_MEMORY_COUNTERS pmc;
78 GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
79 return pmc.PagefileUsage;
83 Process::GetTimeUsage(
84 TimeValue& elapsed, TimeValue& user_time, TimeValue& sys_time)
86 elapsed = TimeValue::now();
88 uint64_t ProcCreate, ProcExit, KernelTime, UserTime;
89 GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate,
90 (FILETIME*)&ProcExit, (FILETIME*)&KernelTime,
91 (FILETIME*)&UserTime);
93 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
94 user_time.seconds( UserTime / 10000000 );
95 user_time.nanoseconds( unsigned(UserTime % 10000000) * 100 );
96 sys_time.seconds( KernelTime / 10000000 );
97 sys_time.nanoseconds( unsigned(KernelTime % 10000000) * 100 );
100 int Process::GetCurrentUserId()
105 int Process::GetCurrentGroupId()
110 // Some LLVM programs such as bugpoint produce core files as a normal part of
111 // their operation. To prevent the disk from filling up, this configuration item
112 // does what's necessary to prevent their generation.
113 void Process::PreventCoreFiles() {
114 // Windows doesn't do core files, but it does do modal pop-up message
115 // boxes. As this method is used by bugpoint, preventing these pop-ups
116 // is the moral equivalent of suppressing core files.
117 SetErrorMode(SEM_FAILCRITICALERRORS |
118 SEM_NOGPFAULTERRORBOX |
119 SEM_NOOPENFILEERRORBOX);
122 bool Process::StandardInIsUserInput() {
123 return GetFileType((HANDLE)_get_osfhandle(0)) == FILE_TYPE_CHAR;
126 bool Process::StandardOutIsDisplayed() {
127 return GetFileType((HANDLE)_get_osfhandle(1)) == FILE_TYPE_CHAR;
130 bool Process::StandardErrIsDisplayed() {
131 return GetFileType((HANDLE)_get_osfhandle(2)) == FILE_TYPE_CHAR;
134 unsigned Process::StandardOutColumns() {
135 unsigned Columns = 0;
136 CONSOLE_SCREEN_BUFFER_INFO csbi;
137 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
138 Columns = csbi.dwSize.X;
142 unsigned Process::StandardErrColumns() {
143 unsigned Columns = 0;
144 CONSOLE_SCREEN_BUFFER_INFO csbi;
145 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi))
146 Columns = csbi.dwSize.X;
150 // It always has colors.
151 bool Process::StandardErrHasColors() {
152 return StandardErrIsDisplayed();
155 bool Process::StandardOutHasColors() {
156 return StandardOutIsDisplayed();
166 :defaultColor(GetCurrentColor()) {}
167 static unsigned GetCurrentColor() {
168 CONSOLE_SCREEN_BUFFER_INFO csbi;
169 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
170 return csbi.wAttributes;
173 WORD operator()() const { return defaultColor; }
176 DefaultColors defaultColors;
179 bool Process::ColorNeedsFlush() {
183 const char *Process::OutputBold(bool bg) {
184 WORD colors = DefaultColors::GetCurrentColor();
186 colors |= BACKGROUND_INTENSITY;
188 colors |= FOREGROUND_INTENSITY;
189 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
193 const char *Process::OutputColor(char code, bool bold, bool bg) {
196 colors = ((code&1) ? BACKGROUND_RED : 0) |
197 ((code&2) ? BACKGROUND_GREEN : 0 ) |
198 ((code&4) ? BACKGROUND_BLUE : 0);
200 colors |= BACKGROUND_INTENSITY;
202 colors = ((code&1) ? FOREGROUND_RED : 0) |
203 ((code&2) ? FOREGROUND_GREEN : 0 ) |
204 ((code&4) ? FOREGROUND_BLUE : 0);
206 colors |= FOREGROUND_INTENSITY;
208 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
212 const char *Process::ResetColor() {
213 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());