Skip to content

Commit 9ff04eb

Browse files
Merge pull request #324 from GameTechDev/feature/svc-launch
Service as Child
2 parents c42529c + c2678bd commit 9ff04eb

File tree

16 files changed

+155
-78
lines changed

16 files changed

+155
-78
lines changed

IntelPresentMon/AppCef/source/winmain.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@
99
#include <Core/source/cli/CliOptions.h>
1010
#include <CommonUtilities/log/IdentificationTable.h>
1111
#include <CommonUtilities/generated/build_id.h>
12+
#include <CommonUtilities/win/Utilities.h>
1213
#include <PresentMonAPIWrapper/DiagnosticHandler.h>
1314
#include <dwmapi.h>
1415

16+
#pragma warning(push)
17+
#pragma warning(disable : 4297)
18+
#include <boost/process.hpp>
19+
#pragma warning(pop)
20+
1521
#pragma comment(lib, "Dwmapi.lib")
1622

1723
using namespace p2c;
@@ -225,8 +231,27 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
225231
// configure the logging system (partially based on command line options)
226232
ConfigureLogging();
227233

228-
using namespace client;
229234
try {
235+
// service-as-child handling
236+
std::optional<boost::process::child> childSvc;
237+
if (!opt.cefType && opt.svcAsChild) {
238+
using namespace std::literals;
239+
namespace bp = boost::process;
240+
241+
childSvc.emplace("PresentMonService.exe"s,
242+
"--control-pipe"s, *opt.controlPipe,
243+
"--nsm-prefix"s, "pm-frame-nsm"s,
244+
"--intro-nsm"s, *opt.shmName,
245+
"--etw-session-name"s, *opt.etwSessionName);
246+
247+
if (!pmon::util::win::WaitForNamedPipe(*opt.controlPipe, 1500)) {
248+
pmlog_error(L"timeout waiting for child service control pipe to go online");
249+
return -1;
250+
}
251+
}
252+
253+
using namespace client;
254+
// cef process constellation fork control
230255
CefMainArgs main_args{ hInstance };
231256
CefRefPtr<ccef::NanoCefProcessHandler> app = new ccef::NanoCefProcessHandler{};
232257

IntelPresentMon/CommonUtilities/Exception.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "../CommonUtilities/win/Utilities.h"
33
#include <sstream>
44
#include "log/GlobalPolicy.h"
5+
#include "log/StackTrace.h"
56
#include <format>
67

78

IntelPresentMon/CommonUtilities/Exception.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
#pragma once
22
#include "str/String.h"
3-
#include "log/StackTrace.h"
43
#include <exception>
4+
#include <memory>
55

66
namespace pmon::util
77
{
8+
namespace log
9+
{
10+
class StackTrace;
11+
}
12+
813
class Exception : public std::exception
914
{
1015
public:
@@ -50,6 +55,7 @@ namespace pmon::util
5055

5156
void InstallSehTranslator() noexcept;
5257

58+
#define PM_DEFINE_EX_FROM(base, name) class name : public base { public: using base::base; }
5359
#define PM_DEFINE_EX(name) class name : public ::pmon::util::Exception { public: using Exception::Exception; }
5460

5561
// prevent any exceptions from leaking from this statement

IntelPresentMon/CommonUtilities/cli/CliFramework.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,22 +247,19 @@ namespace pmon::util::cli
247247
}
248248
};
249249

250-
class MutualInclusion : RuleBase_
250+
class Dependency : RuleBase_
251251
{
252252
public:
253253
template<class...T>
254-
MutualInclusion(T&...elements)
254+
Dependency(T&...elements)
255255
{
256-
Include(elements...);
256+
Depend_(elements...);
257257
}
258258
private:
259259
template<class T, class...Rest>
260-
void Include(T& pivot, const Rest&...rest) const
260+
void Depend_(T& pivot, const Rest&...rest) const
261261
{
262-
if constexpr (sizeof...(rest) > 0) {
263-
GetOption_(pivot)->needs(GetOption_(rest)...);
264-
Include(rest...);
265-
}
262+
GetOption_(pivot)->needs(GetOption_(rest)...);
266263
}
267264
};
268265

IntelPresentMon/CommonUtilities/win/Utilities.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
22
#include "Utilities.h"
33
#include "../log/Log.h"
4+
#include <chrono>
5+
#include <thread>
46

57
namespace pmon::util::win
68
{
@@ -106,4 +108,17 @@ namespace pmon::util::win
106108
return L"unknown_seh_code";
107109
#undef FOR_EACH_STA
108110
}
111+
112+
bool WaitForNamedPipe(const std::string& fullname, int timeoutMs)
113+
{
114+
using namespace std::literals; using clock = std::chrono::high_resolution_clock;
115+
const auto start = clock::now();
116+
while (!::WaitNamedPipeA(fullname.c_str(), 10)) {
117+
if (clock::now() - start >= timeoutMs * 1ms) {
118+
return false;
119+
}
120+
std::this_thread::sleep_for(10ms);
121+
}
122+
return true;
123+
}
109124
}

IntelPresentMon/CommonUtilities/win/Utilities.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
namespace pmon::util::win
66
{
7+
// convert WinAPI HRESULT code to human-readable string
78
std::wstring GetErrorDescription(HRESULT hr) noexcept;
9+
// convert Structured Exception Handling error code to human-readable string
810
std::wstring GetSEHSymbol(DWORD sehCode) noexcept;
11+
// returns true of the named pipe is available for connection
12+
bool WaitForNamedPipe(const std::string& fullname, int timeoutMs);
913
}

IntelPresentMon/ControlLib/Adl2Wrapper.h

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,27 @@
6868
lpOdFanControl)
6969

7070
namespace pwr::amd {
71-
72-
class Adl2Wrapper {
73-
public:
74-
Adl2Wrapper();
75-
Adl2Wrapper(const Adl2Wrapper& t) = delete;
76-
Adl2Wrapper& operator=(const Adl2Wrapper& t) = delete;
77-
~Adl2Wrapper();
78-
79-
static bool Ok(int sta) noexcept { return sta == ADL_OK; }
80-
// endpoint wrapper functions
71+
class Adl2Wrapper {
72+
public:
73+
Adl2Wrapper();
74+
Adl2Wrapper(const Adl2Wrapper& t) = delete;
75+
Adl2Wrapper& operator=(const Adl2Wrapper& t) = delete;
76+
~Adl2Wrapper();
77+
static bool Ok(int sta) noexcept { return sta == ADL_OK; }
78+
// endpoint wrapper functions
8179
#define X_(name, ...) int name(NVW_ARGS(__VA_ARGS__)) const noexcept;
82-
AMD_ADL2_ENDPOINT_LIST
80+
AMD_ADL2_ENDPOINT_LIST
8381
#undef X_
84-
private:
85-
86-
DllModule dll{{"atiadlxx.dll", "atiadlxy.dll"}};
82+
private:
83+
// data
84+
DllModule dll{ {"atiadlxx.dll", "atiadlxy.dll"} };
85+
ADL_CONTEXT_HANDLE adl_context_ = nullptr;
86+
// endpoint pointers
8787
#define X_(name, ...) \
8888
int (*p##name)(ADL_CONTEXT_HANDLE context, NVW_ARGS(__VA_ARGS__)) = nullptr;
89-
AMD_ADL2_ENDPOINT_LIST
89+
AMD_ADL2_ENDPOINT_LIST
9090
#undef X_
91-
// Private endpoint pointer to shutdown ADL2
92-
int (*ADL2_Main_Control_Destroy_ptr_)(ADL_CONTEXT_HANDLE) = nullptr;
93-
ADL_CONTEXT_HANDLE adl_context_ = nullptr;
94-
// Test function
95-
};
96-
91+
// Private endpoint pointer to shutdown ADL2
92+
int (*ADL2_Main_Control_Destroy_ptr_)(ADL_CONTEXT_HANDLE) = nullptr;
93+
};
9794
} // namespace pwr::amd

IntelPresentMon/ControlLib/ControlLib.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
<ClInclude Include="CpuTelemetryInfo.h" />
203203
<ClInclude Include="DllModule.h" />
204204
<ClInclude Include="CpuTelemetry.h" />
205+
<ClInclude Include="Exceptions.h" />
205206
<ClInclude Include="IntelPowerTelemetryAdapter.h" />
206207
<ClInclude Include="Logging.h" />
207208
<ClInclude Include="MacroHelpers.h" />

IntelPresentMon/ControlLib/ControlLib.vcxproj.filters

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
<ClInclude Include="PowerTelemetryProviderFactory.h" />
7676
<ClInclude Include="PresentMonPowerTelemetry.h" />
7777
<ClInclude Include="TelemetryHistory.h" />
78+
<ClInclude Include="Exceptions.h" />
7879
</ItemGroup>
7980
<ItemGroup>
8081
<ClCompile Include="NvapiWrapper.cpp">

IntelPresentMon/ControlLib/DllModule.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#include <vector>
88
#include <stdexcept>
99
#include <format>
10+
#include <cassert>
11+
#include "Exceptions.h"
12+
1013

1114
class DllModule
1215
{
@@ -15,20 +18,17 @@ class DllModule
1518
DllModule& operator=(const DllModule& t) = delete;
1619
DllModule(const std::vector<std::string>& dllFiles)
1720
{
18-
if (dllFiles.empty())
19-
{
20-
throw std::runtime_error{ "No dll files specified for dll module" };
21-
}
21+
assert(!dllFiles.empty());
22+
2223
// try list of dll candidates in order
23-
for (const auto& file : dllFiles)
24-
{
25-
if (hModule = LoadLibraryExA(file.c_str(), NULL, LOAD_LIBRARY_SEARCH_SYSTEM32))
26-
{
24+
for (const auto& file : dllFiles) {
25+
if (hModule = LoadLibraryExA(file.c_str(), NULL, LOAD_LIBRARY_SEARCH_SYSTEM32)) {
2726
return;
2827
}
2928
}
3029
// if all failed to load, throw exception
31-
throw std::runtime_error{ std::format("Failed to load library, last DLL tried: {}", dllFiles.back().c_str()) };
30+
auto msg = std::format("Unable to locate telemetry library, last DLL tried: {}", dllFiles.back().c_str());
31+
throw Except<TelemetrySubsystemAbsent>(std::move(msg));
3232
}
3333
~DllModule()
3434
{

0 commit comments

Comments
 (0)