• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // system_utils_win.cpp: Implementation of OS-specific functions for Windows
8 
9 #include "system_utils.h"
10 
11 #include <stdarg.h>
12 #include <windows.h>
13 #include <array>
14 #include <vector>
15 
16 namespace angle
17 {
18 
19 namespace
20 {
21 
GetPath(HMODULE module)22 std::string GetPath(HMODULE module)
23 {
24     std::array<char, MAX_PATH> executableFileBuf;
25     DWORD executablePathLen = GetModuleFileNameA(module, executableFileBuf.data(),
26                                                  static_cast<DWORD>(executableFileBuf.size()));
27     return (executablePathLen > 0 ? std::string(executableFileBuf.data()) : "");
28 }
29 
GetDirectory(HMODULE module)30 std::string GetDirectory(HMODULE module)
31 {
32     std::string executablePath = GetPath(module);
33     size_t lastPathSepLoc      = executablePath.find_last_of("\\/");
34     return (lastPathSepLoc != std::string::npos) ? executablePath.substr(0, lastPathSepLoc) : "";
35 }
36 
37 }  // anonymous namespace
38 
GetExecutablePath()39 std::string GetExecutablePath()
40 {
41     return GetPath(nullptr);
42 }
43 
GetExecutableDirectory()44 std::string GetExecutableDirectory()
45 {
46     return GetDirectory(nullptr);
47 }
48 
GetSharedLibraryExtension()49 const char *GetSharedLibraryExtension()
50 {
51     return "dll";
52 }
53 
GetCWD()54 Optional<std::string> GetCWD()
55 {
56     std::array<char, MAX_PATH> pathBuf;
57     DWORD result = GetCurrentDirectoryA(static_cast<DWORD>(pathBuf.size()), pathBuf.data());
58     if (result == 0)
59     {
60         return Optional<std::string>::Invalid();
61     }
62     return std::string(pathBuf.data());
63 }
64 
SetCWD(const char * dirName)65 bool SetCWD(const char *dirName)
66 {
67     return (SetCurrentDirectoryA(dirName) == TRUE);
68 }
69 
GetPathSeparatorForEnvironmentVar()70 const char *GetPathSeparatorForEnvironmentVar()
71 {
72     return ";";
73 }
74 
GetCurrentSystemTime()75 double GetCurrentSystemTime()
76 {
77     LARGE_INTEGER frequency = {};
78     QueryPerformanceFrequency(&frequency);
79 
80     LARGE_INTEGER curTime;
81     QueryPerformanceCounter(&curTime);
82 
83     return static_cast<double>(curTime.QuadPart) / frequency.QuadPart;
84 }
85 
GetCurrentProcessCpuTime()86 double GetCurrentProcessCpuTime()
87 {
88     FILETIME creationTime = {};
89     FILETIME exitTime     = {};
90     FILETIME kernelTime   = {};
91     FILETIME userTime     = {};
92 
93     // Note this will not give accurate results if the current thread is
94     // scheduled for less than the tick rate, which is often 15 ms. In that
95     // case, GetProcessTimes will not return different values, making it
96     // possible to end up with 0 ms for a process that takes 93% of a core
97     // (14/15 ms)!  An alternative is QueryProcessCycleTime but there is no
98     // simple way to convert cycles back to seconds, and on top of that, it's
99     // not supported pre-Windows Vista.
100 
101     // Returns 100-ns intervals, so we want to divide by 1e7 to get seconds
102     GetProcessTimes(GetCurrentProcess(), &creationTime, &exitTime, &kernelTime, &userTime);
103 
104     ULARGE_INTEGER kernelInt64;
105     kernelInt64.LowPart      = kernelTime.dwLowDateTime;
106     kernelInt64.HighPart     = kernelTime.dwHighDateTime;
107     double systemTimeSeconds = static_cast<double>(kernelInt64.QuadPart) * 1e-7;
108 
109     ULARGE_INTEGER userInt64;
110     userInt64.LowPart      = userTime.dwLowDateTime;
111     userInt64.HighPart     = userTime.dwHighDateTime;
112     double userTimeSeconds = static_cast<double>(userInt64.QuadPart) * 1e-7;
113 
114     return systemTimeSeconds + userTimeSeconds;
115 }
116 
IsDirectory(const char * filename)117 bool IsDirectory(const char *filename)
118 {
119     WIN32_FILE_ATTRIBUTE_DATA fileInformation;
120 
121     BOOL result = GetFileAttributesExA(filename, GetFileExInfoStandard, &fileInformation);
122     if (result)
123     {
124         DWORD attribs = fileInformation.dwFileAttributes;
125         return (attribs != INVALID_FILE_ATTRIBUTES) && ((attribs & FILE_ATTRIBUTE_DIRECTORY) > 0);
126     }
127 
128     return false;
129 }
130 
IsDebuggerAttached()131 bool IsDebuggerAttached()
132 {
133     return !!::IsDebuggerPresent();
134 }
135 
BreakDebugger()136 void BreakDebugger()
137 {
138     __debugbreak();
139 }
140 
GetExecutableExtension()141 const char *GetExecutableExtension()
142 {
143     return ".exe";
144 }
145 
GetPathSeparator()146 char GetPathSeparator()
147 {
148     return '\\';
149 }
150 
GetModuleDirectory()151 std::string GetModuleDirectory()
152 {
153 // GetModuleHandleEx is unavailable on UWP
154 #if !defined(ANGLE_IS_WINUWP)
155     static int placeholderSymbol = 0;
156     HMODULE module               = nullptr;
157     if (GetModuleHandleExA(
158             GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
159             reinterpret_cast<LPCSTR>(&placeholderSymbol), &module))
160     {
161         return GetDirectory(module);
162     }
163 #endif
164     return GetDirectory(nullptr);
165 }
166 
GetRootDirectory()167 std::string GetRootDirectory()
168 {
169     return "C:\\";
170 }
171 
172 }  // namespace angle
173