blob: dc4664109dee96ff01adbc31ede0406502c5d31f [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//
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 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
19#pragma comment(lib, "psapi.lib")
Reid Spencer437b0792004-09-15 05:47:40 +000020
21//===----------------------------------------------------------------------===//
22//=== WARNING: Implementation here must contain only Win32 specific code
23//=== and must not be UNIX code
24//===----------------------------------------------------------------------===//
25
Jeff Cohen2e5f4452004-12-23 03:44:40 +000026#ifdef __MINGW
27// This ban should be lifted when MinGW 1.0+ has defined this value.
28# define _HEAPOK (-2)
29#endif
30
Reid Spencer437b0792004-09-15 05:47:40 +000031namespace llvm {
32using 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.
37inline 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
48unsigned
49Process::GetPageSize() {
50 static const unsigned PageSize = GetPageSizeOnce();
51 return PageSize;
52}
53
Reid Spencer721d9aa2004-12-20 00:59:28 +000054uint64_t
55Process::GetMallocUsage()
56{
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000057 _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 Spencer721d9aa2004-12-20 00:59:28 +000066}
67
68uint64_t
69Process::GetTotalMemoryUsage()
70{
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000071 PROCESS_MEMORY_COUNTERS pmc;
72 GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
73 return pmc.PagefileUsage;
Reid Spencer721d9aa2004-12-20 00:59:28 +000074}
75
76void
77Process::GetTimeUsage(
78 TimeValue& elapsed, TimeValue& user_time, TimeValue& sys_time)
79{
80 elapsed = TimeValue::now();
81
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000082 uint64_t ProcCreate, ProcExit, KernelTime, UserTime;
Reid Spencer721d9aa2004-12-20 00:59:28 +000083 GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate,
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000084 (FILETIME*)&ProcExit, (FILETIME*)&KernelTime,
85 (FILETIME*)&UserTime);
Reid Spencer721d9aa2004-12-20 00:59:28 +000086
87 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
88 user_time.seconds( UserTime / 10000000 );
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000089 user_time.nanoseconds( unsigned(UserTime % 10000000) * 100 );
Reid Spencer721d9aa2004-12-20 00:59:28 +000090 sys_time.seconds( KernelTime / 10000000 );
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000091 sys_time.nanoseconds( unsigned(KernelTime % 10000000) * 100 );
Reid Spencer721d9aa2004-12-20 00:59:28 +000092}
93
Reid Spencer68fdcc12004-12-27 06:17:27 +000094// 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.
97void 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 Cohenc6dffe02005-01-01 22:54:05 +0000103bool Process::StandardInIsUserInput() {
104 return GetFileType((HANDLE)_get_osfhandle(0)) == FILE_TYPE_CHAR;
105}
106
107bool Process::StandardOutIsDisplayed() {
108 return GetFileType((HANDLE)_get_osfhandle(1)) == FILE_TYPE_CHAR;
109}
110
111bool Process::StandardErrIsDisplayed() {
112 return GetFileType((HANDLE)_get_osfhandle(2)) == FILE_TYPE_CHAR;
113}
114
Reid Spencer437b0792004-09-15 05:47:40 +0000115}
116// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab