blob: 10fec05cb3d2d951f8e4eb9050bf15c0fe6a00c8 [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>
17
18#pragma comment(lib, "psapi.lib")
Reid Spencer437b0792004-09-15 05:47:40 +000019
20//===----------------------------------------------------------------------===//
21//=== WARNING: Implementation here must contain only Win32 specific code
22//=== and must not be UNIX code
23//===----------------------------------------------------------------------===//
24
Jeff Cohen2e5f4452004-12-23 03:44:40 +000025#ifdef __MINGW
26// This ban should be lifted when MinGW 1.0+ has defined this value.
27# define _HEAPOK (-2)
28#endif
29
Reid Spencer437b0792004-09-15 05:47:40 +000030namespace llvm {
31using namespace sys;
32
33// This function retrieves the page size using GetSystemInfo and is present
34// solely so it can be called once in Process::GetPageSize to initialize the
35// static variable PageSize.
36inline unsigned GetPageSizeOnce() {
37 // NOTE: A 32-bit application running under WOW64 is supposed to use
38 // GetNativeSystemInfo. However, this interface is not present prior
39 // to Windows XP so to use it requires dynamic linking. It is not clear
40 // how this affects the reported page size, if at all. One could argue
41 // that LLVM ought to run as 64-bits on a 64-bit system, anyway.
42 SYSTEM_INFO info;
43 GetSystemInfo(&info);
44 return static_cast<unsigned>(info.dwPageSize);
45}
46
47unsigned
48Process::GetPageSize() {
49 static const unsigned PageSize = GetPageSizeOnce();
50 return PageSize;
51}
52
Reid Spencer721d9aa2004-12-20 00:59:28 +000053uint64_t
54Process::GetMallocUsage()
55{
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000056 _HEAPINFO hinfo;
57 hinfo._pentry = NULL;
58
59 size_t size = 0;
60
61 while (_heapwalk(&hinfo) == _HEAPOK)
62 size += hinfo._size;
63
64 return size;
Reid Spencer721d9aa2004-12-20 00:59:28 +000065}
66
67uint64_t
68Process::GetTotalMemoryUsage()
69{
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000070 PROCESS_MEMORY_COUNTERS pmc;
71 GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
72 return pmc.PagefileUsage;
Reid Spencer721d9aa2004-12-20 00:59:28 +000073}
74
75void
76Process::GetTimeUsage(
77 TimeValue& elapsed, TimeValue& user_time, TimeValue& sys_time)
78{
79 elapsed = TimeValue::now();
80
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000081 uint64_t ProcCreate, ProcExit, KernelTime, UserTime;
Reid Spencer721d9aa2004-12-20 00:59:28 +000082 GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate,
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000083 (FILETIME*)&ProcExit, (FILETIME*)&KernelTime,
84 (FILETIME*)&UserTime);
Reid Spencer721d9aa2004-12-20 00:59:28 +000085
86 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
87 user_time.seconds( UserTime / 10000000 );
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000088 user_time.nanoseconds( unsigned(UserTime % 10000000) * 100 );
Reid Spencer721d9aa2004-12-20 00:59:28 +000089 sys_time.seconds( KernelTime / 10000000 );
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000090 sys_time.nanoseconds( unsigned(KernelTime % 10000000) * 100 );
Reid Spencer721d9aa2004-12-20 00:59:28 +000091}
92
Reid Spencer68fdcc12004-12-27 06:17:27 +000093// Some LLVM programs such as bugpoint produce core files as a normal part of
94// their operation. To prevent the disk from filling up, this configuration item
95// does what's necessary to prevent their generation.
96void Process::PreventCoreFiles() {
97 // Windows doesn't do core files, so nothing to do.
98 // Although... it might be nice to prevent the do-you-want-to-debug
99 // dialog box from coming up. Or maybe not...
100}
101
Reid Spencer437b0792004-09-15 05:47:40 +0000102}
103// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab