#include "Win32.h"
#include <psapi.h>
#include <malloc.h>
#include <io.h>
#ifdef __MINGW32__
#if (HAVE_LIBPSAPI != 1)
#error "libpsapi.a should be present"
#endif
#else
#pragma comment(lib, "psapi.lib")
#endif
#ifdef __MINGW32__
# define _HEAPOK (-2)
#endif
namespace llvm {
using namespace sys;
inline unsigned GetPageSizeOnce() {
SYSTEM_INFO info;
GetSystemInfo(&info);
return static_cast<unsigned>(info.dwPageSize);
}
unsigned
Process::GetPageSize() {
static const unsigned PageSize = GetPageSizeOnce();
return PageSize;
}
size_t
Process::GetMallocUsage()
{
_HEAPINFO hinfo;
hinfo._pentry = NULL;
size_t size = 0;
while (_heapwalk(&hinfo) == _HEAPOK)
size += hinfo._size;
return size;
}
size_t
Process::GetTotalMemoryUsage()
{
PROCESS_MEMORY_COUNTERS pmc;
GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
return pmc.PagefileUsage;
}
void
Process::GetTimeUsage(
TimeValue& elapsed, TimeValue& user_time, TimeValue& sys_time)
{
elapsed = TimeValue::now();
uint64_t ProcCreate, ProcExit, KernelTime, UserTime;
GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate,
(FILETIME*)&ProcExit, (FILETIME*)&KernelTime,
(FILETIME*)&UserTime);
user_time.seconds( UserTime / 10000000 );
user_time.nanoseconds( unsigned(UserTime % 10000000) * 100 );
sys_time.seconds( KernelTime / 10000000 );
sys_time.nanoseconds( unsigned(KernelTime % 10000000) * 100 );
}
int Process::GetCurrentUserId()
{
return 65536;
}
int Process::GetCurrentGroupId()
{
return 65536;
}
void Process::PreventCoreFiles() {
SetErrorMode(SEM_FAILCRITICALERRORS |
SEM_NOGPFAULTERRORBOX |
SEM_NOOPENFILEERRORBOX);
}
bool Process::StandardInIsUserInput() {
return GetFileType((HANDLE)_get_osfhandle(0)) == FILE_TYPE_CHAR;
}
bool Process::StandardOutIsDisplayed() {
return GetFileType((HANDLE)_get_osfhandle(1)) == FILE_TYPE_CHAR;
}
bool Process::StandardErrIsDisplayed() {
return GetFileType((HANDLE)_get_osfhandle(2)) == FILE_TYPE_CHAR;
}
}