Reid Spencer | 437b079 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 1 | //===- Win32/Process.cpp - Win32 Process Implementation ------- -*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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. |
Reid Spencer | 437b079 | 2004-09-15 05:47:40 +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" |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 15 | #include <psapi.h> |
| 16 | #include <malloc.h> |
Jeff Cohen | c6dffe0 | 2005-01-01 22:54:05 +0000 | [diff] [blame] | 17 | #include <io.h> |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 18 | |
Reid Spencer | 48fdf91 | 2006-06-01 19:03:21 +0000 | [diff] [blame] | 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 |
Reid Spencer | 437b079 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | //=== WARNING: Implementation here must contain only Win32 specific code |
| 29 | //=== and must not be UNIX code |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
Jeff Cohen | 23a1cf3 | 2005-02-19 03:01:13 +0000 | [diff] [blame] | 32 | #ifdef __MINGW32__ |
Jeff Cohen | 2e5f445 | 2004-12-23 03:44:40 +0000 | [diff] [blame] | 33 | // This ban should be lifted when MinGW 1.0+ has defined this value. |
| 34 | # define _HEAPOK (-2) |
| 35 | #endif |
| 36 | |
Reid Spencer | 437b079 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 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 | |
Jeff Cohen | e269a1a | 2005-01-08 20:15:57 +0000 | [diff] [blame] | 60 | size_t |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 61 | Process::GetMallocUsage() |
| 62 | { |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 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; |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Jeff Cohen | e269a1a | 2005-01-08 20:15:57 +0000 | [diff] [blame] | 74 | size_t |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 75 | Process::GetTotalMemoryUsage() |
| 76 | { |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 77 | PROCESS_MEMORY_COUNTERS pmc; |
| 78 | GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)); |
| 79 | return pmc.PagefileUsage; |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void |
| 83 | Process::GetTimeUsage( |
| 84 | TimeValue& elapsed, TimeValue& user_time, TimeValue& sys_time) |
| 85 | { |
| 86 | elapsed = TimeValue::now(); |
| 87 | |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 88 | uint64_t ProcCreate, ProcExit, KernelTime, UserTime; |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 89 | GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate, |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 90 | (FILETIME*)&ProcExit, (FILETIME*)&KernelTime, |
| 91 | (FILETIME*)&UserTime); |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 92 | |
| 93 | // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond) |
| 94 | user_time.seconds( UserTime / 10000000 ); |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 95 | user_time.nanoseconds( unsigned(UserTime % 10000000) * 100 ); |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 96 | sys_time.seconds( KernelTime / 10000000 ); |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 97 | sys_time.nanoseconds( unsigned(KernelTime % 10000000) * 100 ); |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Reid Spencer | 93b3473 | 2005-04-21 16:12:57 +0000 | [diff] [blame] | 100 | int Process::GetCurrentUserId() |
| 101 | { |
| 102 | return 65536; |
| 103 | } |
| 104 | |
| 105 | int Process::GetCurrentGroupId() |
| 106 | { |
| 107 | return 65536; |
| 108 | } |
| 109 | |
Reid Spencer | 68fdcc1 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 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() { |
Jeff Cohen | d79bcfd | 2005-02-18 07:05:18 +0000 | [diff] [blame] | 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); |
Reid Spencer | 68fdcc1 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Jeff Cohen | c6dffe0 | 2005-01-01 22:54:05 +0000 | [diff] [blame] | 122 | bool Process::StandardInIsUserInput() { |
| 123 | return GetFileType((HANDLE)_get_osfhandle(0)) == FILE_TYPE_CHAR; |
| 124 | } |
| 125 | |
| 126 | bool Process::StandardOutIsDisplayed() { |
| 127 | return GetFileType((HANDLE)_get_osfhandle(1)) == FILE_TYPE_CHAR; |
| 128 | } |
| 129 | |
| 130 | bool Process::StandardErrIsDisplayed() { |
| 131 | return GetFileType((HANDLE)_get_osfhandle(2)) == FILE_TYPE_CHAR; |
| 132 | } |
| 133 | |
Reid Spencer | 437b079 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 134 | } |