Put CopyFile in the sys namespace.
[oota-llvm.git] / lib / System / Win32 / Process.cpp
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"
15 #include <psapi.h>
16 #include <malloc.h>
17
18 #pragma comment(lib, "psapi.lib")
19
20 //===----------------------------------------------------------------------===//
21 //=== WARNING: Implementation here must contain only Win32 specific code 
22 //===          and must not be UNIX code
23 //===----------------------------------------------------------------------===//
24
25 #ifdef __MINGW
26 // This ban should be lifted when MinGW 1.0+ has defined this value.
27 #  define _HEAPOK (-2)
28 #endif
29
30 namespace llvm {
31 using 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.
36 inline 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
47 unsigned 
48 Process::GetPageSize() {
49   static const unsigned PageSize = GetPageSizeOnce();
50   return PageSize;
51 }
52
53 uint64_t 
54 Process::GetMallocUsage()
55 {
56   _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;
65 }
66
67 uint64_t
68 Process::GetTotalMemoryUsage()
69 {
70   PROCESS_MEMORY_COUNTERS pmc;
71   GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
72   return pmc.PagefileUsage;
73 }
74
75 void
76 Process::GetTimeUsage(
77   TimeValue& elapsed, TimeValue& user_time, TimeValue& sys_time)
78 {
79   elapsed = TimeValue::now();
80
81   uint64_t ProcCreate, ProcExit, KernelTime, UserTime;
82   GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate, 
83                   (FILETIME*)&ProcExit, (FILETIME*)&KernelTime,
84                   (FILETIME*)&UserTime);
85
86   // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
87   user_time.seconds( UserTime / 10000000 );
88   user_time.nanoseconds( unsigned(UserTime % 10000000) * 100 );
89   sys_time.seconds( KernelTime / 10000000 );
90   sys_time.nanoseconds( unsigned(KernelTime % 10000000) * 100 );
91 }
92
93 }
94 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab