blob: 3aa6ad4d499153b95f397847c3334ca682eb838c [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
25namespace llvm {
26using namespace sys;
27
28// This function retrieves the page size using GetSystemInfo and is present
29// solely so it can be called once in Process::GetPageSize to initialize the
30// static variable PageSize.
31inline unsigned GetPageSizeOnce() {
32 // NOTE: A 32-bit application running under WOW64 is supposed to use
33 // GetNativeSystemInfo. However, this interface is not present prior
34 // to Windows XP so to use it requires dynamic linking. It is not clear
35 // how this affects the reported page size, if at all. One could argue
36 // that LLVM ought to run as 64-bits on a 64-bit system, anyway.
37 SYSTEM_INFO info;
38 GetSystemInfo(&info);
39 return static_cast<unsigned>(info.dwPageSize);
40}
41
42unsigned
43Process::GetPageSize() {
44 static const unsigned PageSize = GetPageSizeOnce();
45 return PageSize;
46}
47
Reid Spencer721d9aa2004-12-20 00:59:28 +000048uint64_t
49Process::GetMallocUsage()
50{
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000051 _HEAPINFO hinfo;
52 hinfo._pentry = NULL;
53
54 size_t size = 0;
55
56 while (_heapwalk(&hinfo) == _HEAPOK)
57 size += hinfo._size;
58
59 return size;
Reid Spencer721d9aa2004-12-20 00:59:28 +000060}
61
62uint64_t
63Process::GetTotalMemoryUsage()
64{
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000065 PROCESS_MEMORY_COUNTERS pmc;
66 GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
67 return pmc.PagefileUsage;
Reid Spencer721d9aa2004-12-20 00:59:28 +000068}
69
70void
71Process::GetTimeUsage(
72 TimeValue& elapsed, TimeValue& user_time, TimeValue& sys_time)
73{
74 elapsed = TimeValue::now();
75
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000076 uint64_t ProcCreate, ProcExit, KernelTime, UserTime;
Reid Spencer721d9aa2004-12-20 00:59:28 +000077 GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate,
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000078 (FILETIME*)&ProcExit, (FILETIME*)&KernelTime,
79 (FILETIME*)&UserTime);
Reid Spencer721d9aa2004-12-20 00:59:28 +000080
81 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
82 user_time.seconds( UserTime / 10000000 );
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000083 user_time.nanoseconds( unsigned(UserTime % 10000000) * 100 );
Reid Spencer721d9aa2004-12-20 00:59:28 +000084 sys_time.seconds( KernelTime / 10000000 );
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000085 sys_time.nanoseconds( unsigned(KernelTime % 10000000) * 100 );
Reid Spencer721d9aa2004-12-20 00:59:28 +000086}
87
Reid Spencer437b0792004-09-15 05:47:40 +000088}
89// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab