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 | // |
| 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" |
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 | |
| 19 | #pragma comment(lib, "psapi.lib") |
Reid Spencer | 437b079 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 20 | |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | //=== WARNING: Implementation here must contain only Win32 specific code |
| 23 | //=== and must not be UNIX code |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
Jeff Cohen | 2e5f445 | 2004-12-23 03:44:40 +0000 | [diff] [blame] | 26 | #ifdef __MINGW |
| 27 | // This ban should be lifted when MinGW 1.0+ has defined this value. |
| 28 | # define _HEAPOK (-2) |
| 29 | #endif |
| 30 | |
Reid Spencer | 437b079 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 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 | |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 54 | uint64_t |
| 55 | Process::GetMallocUsage() |
| 56 | { |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 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; |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | uint64_t |
| 69 | Process::GetTotalMemoryUsage() |
| 70 | { |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 71 | PROCESS_MEMORY_COUNTERS pmc; |
| 72 | GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)); |
| 73 | return pmc.PagefileUsage; |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void |
| 77 | Process::GetTimeUsage( |
| 78 | TimeValue& elapsed, TimeValue& user_time, TimeValue& sys_time) |
| 79 | { |
| 80 | elapsed = TimeValue::now(); |
| 81 | |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 82 | uint64_t ProcCreate, ProcExit, KernelTime, UserTime; |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 83 | GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate, |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 84 | (FILETIME*)&ProcExit, (FILETIME*)&KernelTime, |
| 85 | (FILETIME*)&UserTime); |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 86 | |
| 87 | // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond) |
| 88 | user_time.seconds( UserTime / 10000000 ); |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 89 | user_time.nanoseconds( unsigned(UserTime % 10000000) * 100 ); |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 90 | sys_time.seconds( KernelTime / 10000000 ); |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 91 | sys_time.nanoseconds( unsigned(KernelTime % 10000000) * 100 ); |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Reid Spencer | 68fdcc1 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 94 | // Some LLVM programs such as bugpoint produce core files as a normal part of |
| 95 | // their operation. To prevent the disk from filling up, this configuration item |
| 96 | // does what's necessary to prevent their generation. |
| 97 | void Process::PreventCoreFiles() { |
| 98 | // Windows doesn't do core files, so nothing to do. |
| 99 | // Although... it might be nice to prevent the do-you-want-to-debug |
| 100 | // dialog box from coming up. Or maybe not... |
| 101 | } |
| 102 | |
Jeff Cohen | c6dffe0 | 2005-01-01 22:54:05 +0000 | [diff] [blame] | 103 | bool Process::StandardInIsUserInput() { |
| 104 | return GetFileType((HANDLE)_get_osfhandle(0)) == FILE_TYPE_CHAR; |
| 105 | } |
| 106 | |
| 107 | bool Process::StandardOutIsDisplayed() { |
| 108 | return GetFileType((HANDLE)_get_osfhandle(1)) == FILE_TYPE_CHAR; |
| 109 | } |
| 110 | |
| 111 | bool Process::StandardErrIsDisplayed() { |
| 112 | return GetFileType((HANDLE)_get_osfhandle(2)) == FILE_TYPE_CHAR; |
| 113 | } |
| 114 | |
Reid Spencer | 437b079 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 115 | } |
| 116 | // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab |