Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- Win32/Process.cpp - Win32 Process Implementation ------- -*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides the Win32 specific implementation of the Process class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Win32.h" |
| 15 | #include <psapi.h> |
| 16 | #include <malloc.h> |
| 17 | #include <io.h> |
| 18 | |
| 19 | #ifdef __MINGW32__ |
| 20 | #if (HAVE_LIBPSAPI != 1) |
| 21 | #error "libpsapi.a should be present" |
| 22 | #endif |
| 23 | #else |
| 24 | #pragma comment(lib, "psapi.lib") |
| 25 | #endif |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | //=== WARNING: Implementation here must contain only Win32 specific code |
| 29 | //=== and must not be UNIX code |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
| 32 | #ifdef __MINGW32__ |
| 33 | // This ban should be lifted when MinGW 1.0+ has defined this value. |
| 34 | # define _HEAPOK (-2) |
| 35 | #endif |
| 36 | |
| 37 | namespace llvm { |
| 38 | using namespace sys; |
| 39 | |
| 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. |
| 49 | SYSTEM_INFO info; |
| 50 | GetSystemInfo(&info); |
| 51 | return static_cast<unsigned>(info.dwPageSize); |
| 52 | } |
| 53 | |
| 54 | unsigned |
| 55 | Process::GetPageSize() { |
| 56 | static const unsigned PageSize = GetPageSizeOnce(); |
| 57 | return PageSize; |
| 58 | } |
| 59 | |
| 60 | size_t |
| 61 | Process::GetMallocUsage() |
| 62 | { |
| 63 | _HEAPINFO hinfo; |
| 64 | hinfo._pentry = NULL; |
| 65 | |
| 66 | size_t size = 0; |
| 67 | |
| 68 | while (_heapwalk(&hinfo) == _HEAPOK) |
| 69 | size += hinfo._size; |
| 70 | |
| 71 | return size; |
| 72 | } |
| 73 | |
| 74 | size_t |
| 75 | Process::GetTotalMemoryUsage() |
| 76 | { |
| 77 | PROCESS_MEMORY_COUNTERS pmc; |
| 78 | GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)); |
| 79 | return pmc.PagefileUsage; |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | Process::GetTimeUsage( |
| 84 | TimeValue& elapsed, TimeValue& user_time, TimeValue& sys_time) |
| 85 | { |
| 86 | elapsed = TimeValue::now(); |
| 87 | |
| 88 | uint64_t ProcCreate, ProcExit, KernelTime, UserTime; |
| 89 | GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate, |
| 90 | (FILETIME*)&ProcExit, (FILETIME*)&KernelTime, |
| 91 | (FILETIME*)&UserTime); |
| 92 | |
| 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 ); |
| 98 | } |
| 99 | |
| 100 | int Process::GetCurrentUserId() |
| 101 | { |
| 102 | return 65536; |
| 103 | } |
| 104 | |
| 105 | int Process::GetCurrentGroupId() |
| 106 | { |
| 107 | return 65536; |
| 108 | } |
| 109 | |
| 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); |
| 120 | } |
| 121 | |
| 122 | bool Process::StandardInIsUserInput() { |
Dan Gohman | 31b20c7 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 123 | return FileDescriptorIsDisplayed(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | bool Process::StandardOutIsDisplayed() { |
Dan Gohman | 31b20c7 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 127 | return FileDescriptorIsDisplayed(1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | bool Process::StandardErrIsDisplayed() { |
Dan Gohman | 31b20c7 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 131 | return FileDescriptorIsDisplayed(2); |
| 132 | } |
| 133 | |
| 134 | bool Process::FileDescriptorIsDisplayed(int fd) { |
| 135 | return GetFileType((HANDLE)_get_osfhandle(fd)) == FILE_TYPE_CHAR; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Douglas Gregor | d1e1dbc | 2009-05-11 18:05:52 +0000 | [diff] [blame] | 138 | unsigned Process::StandardOutColumns() { |
| 139 | unsigned Columns = 0; |
| 140 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 141 | if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) |
| 142 | Columns = csbi.dwSize.X; |
| 143 | return Columns; |
| 144 | } |
| 145 | |
| 146 | unsigned Process::StandardErrColumns() { |
| 147 | unsigned Columns = 0; |
| 148 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 149 | if (GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi)) |
| 150 | Columns = csbi.dwSize.X; |
| 151 | return Columns; |
| 152 | } |
| 153 | |
Edwin Török | 41c6666 | 2009-06-04 08:18:25 +0000 | [diff] [blame] | 154 | // It always has colors. |
Edwin Török | 8a06a5b | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 155 | bool Process::StandardErrHasColors() { |
| 156 | return StandardErrIsDisplayed(); |
| 157 | } |
| 158 | |
| 159 | bool Process::StandardOutHasColors() { |
| 160 | return StandardOutIsDisplayed(); |
| 161 | } |
Edwin Török | 41c6666 | 2009-06-04 08:18:25 +0000 | [diff] [blame] | 162 | |
Edwin Török | 8a06a5b | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 163 | namespace { |
| 164 | class DefaultColors |
| 165 | { |
| 166 | private: |
| 167 | WORD defaultColor; |
| 168 | public: |
| 169 | DefaultColors() |
| 170 | :defaultColor(GetCurrentColor()) {} |
| 171 | static unsigned GetCurrentColor() { |
| 172 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 173 | if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) |
| 174 | return csbi.wAttributes; |
| 175 | return 0; |
| 176 | } |
| 177 | WORD operator()() const { return defaultColor; } |
| 178 | }; |
| 179 | |
| 180 | DefaultColors defaultColors; |
| 181 | } |
| 182 | |
| 183 | bool Process::ColorNeedsFlush() { |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | const char *Process::OutputBold(bool bg) { |
| 188 | WORD colors = DefaultColors::GetCurrentColor(); |
| 189 | if (bg) |
| 190 | colors |= BACKGROUND_INTENSITY; |
| 191 | else |
| 192 | colors |= FOREGROUND_INTENSITY; |
| 193 | SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors); |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | const char *Process::OutputColor(char code, bool bold, bool bg) { |
| 198 | WORD colors; |
| 199 | if (bg) { |
| 200 | colors = ((code&1) ? BACKGROUND_RED : 0) | |
| 201 | ((code&2) ? BACKGROUND_GREEN : 0 ) | |
| 202 | ((code&4) ? BACKGROUND_BLUE : 0); |
| 203 | if (bold) |
| 204 | colors |= BACKGROUND_INTENSITY; |
| 205 | } else { |
| 206 | colors = ((code&1) ? FOREGROUND_RED : 0) | |
| 207 | ((code&2) ? FOREGROUND_GREEN : 0 ) | |
| 208 | ((code&4) ? FOREGROUND_BLUE : 0); |
| 209 | if (bold) |
| 210 | colors |= FOREGROUND_INTENSITY; |
| 211 | } |
| 212 | SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors); |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | const char *Process::ResetColor() { |
| 217 | SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors()); |
| 218 | return 0; |
| 219 | } |
| 220 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 221 | } |