Remove an unused function in the old Process interface.
[oota-llvm.git] / lib / Support / Windows / Process.inc
1 //===- Win32/Process.cpp - Win32 Process Implementation ------- -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides the Win32 specific implementation of the Process class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Windows.h"
15 #include <direct.h>
16 #include <io.h>
17 #include <malloc.h>
18 #include <psapi.h>
19
20 #ifdef __MINGW32__
21  #if (HAVE_LIBPSAPI != 1)
22   #error "libpsapi.a should be present"
23  #endif
24 #else
25  #pragma comment(lib, "psapi.lib")
26 #endif
27
28 //===----------------------------------------------------------------------===//
29 //=== WARNING: Implementation here must contain only Win32 specific code
30 //===          and must not be UNIX code
31 //===----------------------------------------------------------------------===//
32
33 #ifdef __MINGW32__
34 // This ban should be lifted when MinGW 1.0+ has defined this value.
35 #  define _HEAPOK (-2)
36 #endif
37
38 using namespace llvm;
39 using namespace sys;
40
41
42 process::id_type self_process::get_id() {
43   return GetCurrentProcess();
44 }
45
46
47 // This function retrieves the page size using GetSystemInfo and is present
48 // solely so it can be called once in Process::GetPageSize to initialize the
49 // static variable PageSize.
50 inline unsigned GetPageSizeOnce() {
51   // NOTE: A 32-bit application running under WOW64 is supposed to use
52   // GetNativeSystemInfo.  However, this interface is not present prior
53   // to Windows XP so to use it requires dynamic linking.  It is not clear
54   // how this affects the reported page size, if at all.  One could argue
55   // that LLVM ought to run as 64-bits on a 64-bit system, anyway.
56   SYSTEM_INFO info;
57   GetSystemInfo(&info);
58   return static_cast<unsigned>(info.dwPageSize);
59 }
60
61 unsigned
62 Process::GetPageSize() {
63   static const unsigned PageSize = GetPageSizeOnce();
64   return PageSize;
65 }
66
67 size_t
68 Process::GetMallocUsage()
69 {
70   _HEAPINFO hinfo;
71   hinfo._pentry = NULL;
72
73   size_t size = 0;
74
75   while (_heapwalk(&hinfo) == _HEAPOK)
76     size += hinfo._size;
77
78   return size;
79 }
80
81 void
82 Process::GetTimeUsage(
83   TimeValue& elapsed, TimeValue& user_time, TimeValue& sys_time)
84 {
85   elapsed = TimeValue::now();
86
87   uint64_t ProcCreate, ProcExit, KernelTime, UserTime;
88   GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate,
89                   (FILETIME*)&ProcExit, (FILETIME*)&KernelTime,
90                   (FILETIME*)&UserTime);
91
92   // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
93   user_time.seconds( UserTime / 10000000 );
94   user_time.nanoseconds( unsigned(UserTime % 10000000) * 100 );
95   sys_time.seconds( KernelTime / 10000000 );
96   sys_time.nanoseconds( unsigned(KernelTime % 10000000) * 100 );
97 }
98
99 int Process::GetCurrentUserId()
100 {
101   return 65536;
102 }
103
104 int Process::GetCurrentGroupId()
105 {
106   return 65536;
107 }
108
109 // Some LLVM programs such as bugpoint produce core files as a normal part of
110 // their operation. To prevent the disk from filling up, this configuration item
111 // does what's necessary to prevent their generation.
112 void Process::PreventCoreFiles() {
113   // Windows doesn't do core files, but it does do modal pop-up message
114   // boxes.  As this method is used by bugpoint, preventing these pop-ups
115   // is the moral equivalent of suppressing core files.
116   SetErrorMode(SEM_FAILCRITICALERRORS |
117                SEM_NOGPFAULTERRORBOX |
118                SEM_NOOPENFILEERRORBOX);
119 }
120
121 bool Process::StandardInIsUserInput() {
122   return FileDescriptorIsDisplayed(0);
123 }
124
125 bool Process::StandardOutIsDisplayed() {
126   return FileDescriptorIsDisplayed(1);
127 }
128
129 bool Process::StandardErrIsDisplayed() {
130   return FileDescriptorIsDisplayed(2);
131 }
132
133 bool Process::FileDescriptorIsDisplayed(int fd) {
134   DWORD Mode;  // Unused
135   return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0);
136 }
137
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
154 // The terminal always has colors.
155 bool Process::FileDescriptorHasColors(int fd) {
156   return FileDescriptorIsDisplayed(fd);
157 }
158
159 bool Process::StandardOutHasColors() {
160   return FileDescriptorHasColors(1);
161 }
162
163 bool Process::StandardErrHasColors() {
164   return FileDescriptorHasColors(2);
165 }
166
167 namespace {
168 class DefaultColors
169 {
170   private:
171     WORD defaultColor;
172   public:
173     DefaultColors()
174      :defaultColor(GetCurrentColor()) {}
175     static unsigned GetCurrentColor() {
176       CONSOLE_SCREEN_BUFFER_INFO csbi;
177       if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
178         return csbi.wAttributes;
179       return 0;
180     }
181     WORD operator()() const { return defaultColor; }
182 };
183
184 DefaultColors defaultColors;
185 }
186
187 bool Process::ColorNeedsFlush() {
188   return true;
189 }
190
191 const char *Process::OutputBold(bool bg) {
192   WORD colors = DefaultColors::GetCurrentColor();
193   if (bg)
194     colors |= BACKGROUND_INTENSITY;
195   else
196     colors |= FOREGROUND_INTENSITY;
197   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
198   return 0;
199 }
200
201 const char *Process::OutputColor(char code, bool bold, bool bg) {
202   WORD colors;
203   if (bg) {
204     colors = ((code&1) ? BACKGROUND_RED : 0) |
205       ((code&2) ? BACKGROUND_GREEN : 0 ) |
206       ((code&4) ? BACKGROUND_BLUE : 0);
207     if (bold)
208       colors |= BACKGROUND_INTENSITY;
209   } else {
210     colors = ((code&1) ? FOREGROUND_RED : 0) |
211       ((code&2) ? FOREGROUND_GREEN : 0 ) |
212       ((code&4) ? FOREGROUND_BLUE : 0);
213     if (bold)
214       colors |= FOREGROUND_INTENSITY;
215   }
216   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
217   return 0;
218 }
219
220 static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) {
221   CONSOLE_SCREEN_BUFFER_INFO info;
222   GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
223   return info.wAttributes;
224 }
225
226 const char *Process::OutputReverse() {
227   const WORD attributes
228    = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE));
229
230   const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN |
231     FOREGROUND_RED | FOREGROUND_INTENSITY;
232   const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN |
233     BACKGROUND_RED | BACKGROUND_INTENSITY;
234   const WORD color_mask = foreground_mask | background_mask;
235
236   WORD new_attributes =
237     ((attributes & FOREGROUND_BLUE     )?BACKGROUND_BLUE     :0) |
238     ((attributes & FOREGROUND_GREEN    )?BACKGROUND_GREEN    :0) |
239     ((attributes & FOREGROUND_RED      )?BACKGROUND_RED      :0) |
240     ((attributes & FOREGROUND_INTENSITY)?BACKGROUND_INTENSITY:0) |
241     ((attributes & BACKGROUND_BLUE     )?FOREGROUND_BLUE     :0) |
242     ((attributes & BACKGROUND_GREEN    )?FOREGROUND_GREEN    :0) |
243     ((attributes & BACKGROUND_RED      )?FOREGROUND_RED      :0) |
244     ((attributes & BACKGROUND_INTENSITY)?FOREGROUND_INTENSITY:0) |
245     0;
246   new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask);
247
248   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), new_attributes);
249   return 0;
250 }
251
252 const char *Process::ResetColor() {
253   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());
254   return 0;
255 }