blob: 15b41f00eafbe78fa748da0d6d579c80afd9c0b6 [file] [log] [blame]
Reid Spencer437b0792004-09-15 05:47:40 +00001//===- Win32/Process.cpp - Win32 Process Implementation ------- -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer437b0792004-09-15 05:47:40 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file provides the Win32 specific implementation of the Process class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Win32.h"
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000015#include <psapi.h>
16#include <malloc.h>
Jeff Cohenc6dffe02005-01-01 22:54:05 +000017#include <io.h>
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000018
Reid Spencer48fdf912006-06-01 19:03:21 +000019#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
Reid Spencer437b0792004-09-15 05:47:40 +000026
27//===----------------------------------------------------------------------===//
28//=== WARNING: Implementation here must contain only Win32 specific code
29//=== and must not be UNIX code
30//===----------------------------------------------------------------------===//
31
Jeff Cohen23a1cf32005-02-19 03:01:13 +000032#ifdef __MINGW32__
Jeff Cohen2e5f4452004-12-23 03:44:40 +000033// This ban should be lifted when MinGW 1.0+ has defined this value.
34# define _HEAPOK (-2)
35#endif
36
Reid Spencer437b0792004-09-15 05:47:40 +000037namespace llvm {
38using 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.
43inline 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
54unsigned
55Process::GetPageSize() {
56 static const unsigned PageSize = GetPageSizeOnce();
57 return PageSize;
58}
59
Jeff Cohene269a1a2005-01-08 20:15:57 +000060size_t
Reid Spencer721d9aa2004-12-20 00:59:28 +000061Process::GetMallocUsage()
62{
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000063 _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;
Reid Spencer721d9aa2004-12-20 00:59:28 +000072}
73
Jeff Cohene269a1a2005-01-08 20:15:57 +000074size_t
Reid Spencer721d9aa2004-12-20 00:59:28 +000075Process::GetTotalMemoryUsage()
76{
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000077 PROCESS_MEMORY_COUNTERS pmc;
78 GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
79 return pmc.PagefileUsage;
Reid Spencer721d9aa2004-12-20 00:59:28 +000080}
81
82void
83Process::GetTimeUsage(
84 TimeValue& elapsed, TimeValue& user_time, TimeValue& sys_time)
85{
86 elapsed = TimeValue::now();
87
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000088 uint64_t ProcCreate, ProcExit, KernelTime, UserTime;
Reid Spencer721d9aa2004-12-20 00:59:28 +000089 GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate,
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000090 (FILETIME*)&ProcExit, (FILETIME*)&KernelTime,
91 (FILETIME*)&UserTime);
Reid Spencer721d9aa2004-12-20 00:59:28 +000092
93 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
94 user_time.seconds( UserTime / 10000000 );
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000095 user_time.nanoseconds( unsigned(UserTime % 10000000) * 100 );
Reid Spencer721d9aa2004-12-20 00:59:28 +000096 sys_time.seconds( KernelTime / 10000000 );
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000097 sys_time.nanoseconds( unsigned(KernelTime % 10000000) * 100 );
Reid Spencer721d9aa2004-12-20 00:59:28 +000098}
99
Reid Spencer93b34732005-04-21 16:12:57 +0000100int Process::GetCurrentUserId()
101{
102 return 65536;
103}
104
105int Process::GetCurrentGroupId()
106{
107 return 65536;
108}
109
Reid Spencer68fdcc12004-12-27 06:17:27 +0000110// 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.
113void Process::PreventCoreFiles() {
Jeff Cohend79bcfd2005-02-18 07:05:18 +0000114 // 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);
Reid Spencer68fdcc12004-12-27 06:17:27 +0000120}
121
Jeff Cohenc6dffe02005-01-01 22:54:05 +0000122bool Process::StandardInIsUserInput() {
Dan Gohmanec080462009-09-11 20:46:33 +0000123 return FileDescriptorIsDisplayed(0);
Jeff Cohenc6dffe02005-01-01 22:54:05 +0000124}
125
126bool Process::StandardOutIsDisplayed() {
Dan Gohmanec080462009-09-11 20:46:33 +0000127 return FileDescriptorIsDisplayed(1);
Jeff Cohenc6dffe02005-01-01 22:54:05 +0000128}
129
130bool Process::StandardErrIsDisplayed() {
Dan Gohmanec080462009-09-11 20:46:33 +0000131 return FileDescriptorIsDisplayed(2);
132}
133
134bool Process::FileDescriptorIsDisplayed(int fd) {
NAKAMURA Takumi46cf7c52010-11-10 08:37:47 +0000135 DWORD Mode; // Unused
136 return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0);
Jeff Cohenc6dffe02005-01-01 22:54:05 +0000137}
138
Douglas Gregor01746742009-05-11 18:05:52 +0000139unsigned Process::StandardOutColumns() {
140 unsigned Columns = 0;
141 CONSOLE_SCREEN_BUFFER_INFO csbi;
142 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
143 Columns = csbi.dwSize.X;
144 return Columns;
145}
146
147unsigned Process::StandardErrColumns() {
148 unsigned Columns = 0;
149 CONSOLE_SCREEN_BUFFER_INFO csbi;
150 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi))
151 Columns = csbi.dwSize.X;
152 return Columns;
153}
154
Torok Edwin6c8db342009-06-04 08:18:25 +0000155// It always has colors.
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000156bool Process::StandardErrHasColors() {
157 return StandardErrIsDisplayed();
158}
159
160bool Process::StandardOutHasColors() {
161 return StandardOutIsDisplayed();
162}
Torok Edwin6c8db342009-06-04 08:18:25 +0000163
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000164namespace {
165class DefaultColors
166{
167 private:
168 WORD defaultColor;
169 public:
170 DefaultColors()
171 :defaultColor(GetCurrentColor()) {}
172 static unsigned GetCurrentColor() {
173 CONSOLE_SCREEN_BUFFER_INFO csbi;
174 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
175 return csbi.wAttributes;
176 return 0;
177 }
178 WORD operator()() const { return defaultColor; }
179};
180
181DefaultColors defaultColors;
182}
183
184bool Process::ColorNeedsFlush() {
185 return true;
186}
187
188const char *Process::OutputBold(bool bg) {
189 WORD colors = DefaultColors::GetCurrentColor();
190 if (bg)
191 colors |= BACKGROUND_INTENSITY;
192 else
193 colors |= FOREGROUND_INTENSITY;
194 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
195 return 0;
196}
197
198const char *Process::OutputColor(char code, bool bold, bool bg) {
199 WORD colors;
200 if (bg) {
201 colors = ((code&1) ? BACKGROUND_RED : 0) |
202 ((code&2) ? BACKGROUND_GREEN : 0 ) |
203 ((code&4) ? BACKGROUND_BLUE : 0);
204 if (bold)
205 colors |= BACKGROUND_INTENSITY;
206 } else {
207 colors = ((code&1) ? FOREGROUND_RED : 0) |
208 ((code&2) ? FOREGROUND_GREEN : 0 ) |
209 ((code&4) ? FOREGROUND_BLUE : 0);
210 if (bold)
211 colors |= FOREGROUND_INTENSITY;
212 }
213 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
214 return 0;
215}
216
217const char *Process::ResetColor() {
218 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());
219 return 0;
220}
221
Reid Spencer437b0792004-09-15 05:47:40 +0000222}