For PR495:
[oota-llvm.git] / lib / System / Win32 / Process.inc
1 //===- Win32/Process.cpp - Win32 Process Implementation ------- -*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
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 #pragma comment(lib, "psapi.lib")
20
21 //===----------------------------------------------------------------------===//
22 //=== WARNING: Implementation here must contain only Win32 specific code 
23 //===          and must not be UNIX code
24 //===----------------------------------------------------------------------===//
25
26 #ifdef __MINGW32__
27 // This ban should be lifted when MinGW 1.0+ has defined this value.
28 #  define _HEAPOK (-2)
29 #endif
30
31 namespace llvm {
32 using namespace sys;
33
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.
43   SYSTEM_INFO info;
44   GetSystemInfo(&info);
45   return static_cast<unsigned>(info.dwPageSize);
46 }
47
48 unsigned 
49 Process::GetPageSize() {
50   static const unsigned PageSize = GetPageSizeOnce();
51   return PageSize;
52 }
53
54 size_t 
55 Process::GetMallocUsage()
56 {
57   _HEAPINFO hinfo;
58   hinfo._pentry = NULL;
59
60   size_t size = 0;
61
62   while (_heapwalk(&hinfo) == _HEAPOK)
63     size += hinfo._size;
64
65   return size;
66 }
67
68 size_t
69 Process::GetTotalMemoryUsage()
70 {
71   PROCESS_MEMORY_COUNTERS pmc;
72   GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
73   return pmc.PagefileUsage;
74 }
75
76 void
77 Process::GetTimeUsage(
78   TimeValue& elapsed, TimeValue& user_time, TimeValue& sys_time)
79 {
80   elapsed = TimeValue::now();
81
82   uint64_t ProcCreate, ProcExit, KernelTime, UserTime;
83   GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate, 
84                   (FILETIME*)&ProcExit, (FILETIME*)&KernelTime,
85                   (FILETIME*)&UserTime);
86
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 );
92 }
93
94 int Process::GetCurrentUserId()
95 {
96   return 65536;
97 }
98
99 int Process::GetCurrentGroupId()
100 {
101   return 65536;
102 }
103
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);
114 }
115
116 bool Process::StandardInIsUserInput() {
117   return GetFileType((HANDLE)_get_osfhandle(0)) == FILE_TYPE_CHAR;
118 }
119
120 bool Process::StandardOutIsDisplayed() {
121   return GetFileType((HANDLE)_get_osfhandle(1)) == FILE_TYPE_CHAR;
122 }
123
124 bool Process::StandardErrIsDisplayed() {
125   return GetFileType((HANDLE)_get_osfhandle(2)) == FILE_TYPE_CHAR;
126 }
127
128 }