Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 47 additions & 5 deletions IntelPresentMon/CommonUtilities/log/ChannelFlusher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,46 @@
namespace pmon::util::log
{
using namespace std::literals;
ChannelFlusher::ChannelFlusher(std::weak_ptr<IEntrySink> pChan)
ChannelFlusher::ChannelFlusher(std::weak_ptr<IEntrySink> pChan, bool initiallyEnabled)
:
pChan_{ std::move(pChan) }
pChan_{ std::move(pChan) },
enabled_{ initiallyEnabled },
stateChangeEvent_{ false, false }
{
worker_ = mt::Thread{ "log-flush", [this] {
try {
while (!win::WaitAnyEventFor(500ms, exitEvent_)) {
pChan_.lock()->Flush();
auto flush = [this] {
if (auto pChan = pChan_.lock()) {
pChan->Flush();
return true;
}
return false;
};
while (true) {
if (!enabled_.load(std::memory_order_acquire)) {
if (flushBeforeSuspend_.exchange(false, std::memory_order_acq_rel)) {
if (!flush()) {
return;
}
}
if (win::WaitAnyEvent(exitEvent_, stateChangeEvent_) == 0) {
return;
}
continue;
}

if (auto waitResult = win::WaitAnyEventFor(500ms, exitEvent_, stateChangeEvent_)) {
if (*waitResult == 0) {
return;
}
continue;
}

if (enabled_.load(std::memory_order_acquire)) {
if (!flush()) {
return;
}
}
}
}
catch (...) {
Expand All @@ -27,4 +59,14 @@ namespace pmon::util::log
{
pmquell(exitEvent_.Set());
}
}
void ChannelFlusher::SetEnabled(bool enabled)
{
const auto wasEnabled = enabled_.exchange(enabled, std::memory_order_acq_rel);
if (wasEnabled != enabled) {
if (!enabled) {
flushBeforeSuspend_.store(true, std::memory_order_release);
}
stateChangeEvent_.Set();
}
}
}
9 changes: 7 additions & 2 deletions IntelPresentMon/CommonUtilities/log/ChannelFlusher.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <memory>
#include <atomic>
#include "../mt/Thread.h"
#include "../win/Event.h"
#include "IChannelObject.h"
Expand All @@ -11,18 +12,22 @@ namespace pmon::util::log
class ChannelFlusher : public IChannelObject
{
public:
ChannelFlusher(std::weak_ptr<IEntrySink> pChan);
ChannelFlusher(std::weak_ptr<IEntrySink> pChan, bool initiallyEnabled = true);
~ChannelFlusher();

ChannelFlusher(const ChannelFlusher&) = delete;
ChannelFlusher & operator=(const ChannelFlusher&) = delete;
ChannelFlusher(ChannelFlusher&&) = delete;
ChannelFlusher & operator=(ChannelFlusher&&) = delete;

void SetEnabled(bool enabled);

private:
std::weak_ptr<IEntrySink> pChan_;
std::atomic<bool> enabled_;
std::atomic<bool> flushBeforeSuspend_ = false;
mt::Thread worker_;
win::Event exitEvent_;
win::Event stateChangeEvent_;
};
}

17 changes: 17 additions & 0 deletions IntelPresentMon/PresentMonService/ActionExecutionContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <CommonUtilities/rng/MemberSlice.h>
#include <CommonUtilities/rng/OptionalMinMax.h>
#include "../Interprocess/source/act/ActionHelper.h"
#include "LogSetup.h"
#include <cereal/types/unordered_set.hpp>
#include <vector>

Expand All @@ -24,6 +25,7 @@ namespace pmon::svc::acts
// tracked pids cleanup
stx.trackedPids.clear();
pPmon->UpdateTracking(GetTrackedPidSet());
UpdatePeriodicLogFlushing();
// telemetry period cleanup
stx.requestedTelemetryPeriodMs.reset();
UpdateTelemetryPeriod();
Expand Down Expand Up @@ -71,6 +73,21 @@ namespace pmon::svc::acts
}
}
pPmon->SetDeviceMetricUsage(std::move(deviceMetricUsage));
UpdatePeriodicLogFlushing();
}
void ActionExecutionContext::UpdatePeriodicLogFlushing() const
{
bool active = false;
if (pSessionMap != nullptr) {
for (const auto& sessionEntry : *pSessionMap) {
const auto& session = sessionEntry.second;
if (!session.trackedPids.empty() || !session.metricUsage.empty()) {
active = true;
break;
}
}
}
logsetup::SetPeriodicLogFlushingEnabled(active);
}

std::unordered_set<uint32_t> ActionExecutionContext::GetTrackedPidSet() const
Expand Down
1 change: 1 addition & 0 deletions IntelPresentMon/PresentMonService/ActionExecutionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ namespace pmon::svc::acts
void UpdateTelemetryPeriod() const;
void UpdateEtwFlushPeriod() const;
void UpdateMetricUsage() const;
void UpdatePeriodicLogFlushing() const;
std::unordered_set<uint32_t> GetTrackedPidSet() const;
void ReleaseBackpressure(uint32_t pid) const;
};
Expand Down
17 changes: 16 additions & 1 deletion IntelPresentMon/PresentMonService/LogSetup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace pmon::util::log
pChannel->AttachComponent(std::make_shared<MsvcDebugDriver>(pFormatter), "drv:dbg");
pChannel->AttachComponent(std::make_shared<StdioDriver>(pFormatter), "drv:std");
// flusher
pChannel->AttachComponent(std::make_shared<ChannelFlusher>(pChannel), "obj:fsh");
pChannel->AttachComponent(std::make_shared<ChannelFlusher>(pChannel, false), "obj:fsh");

return pChannel;
}
Expand Down Expand Up @@ -130,6 +130,21 @@ namespace logsetup
pmlog_panic_("Failed configuring log in server");
}
}
void SetPeriodicLogFlushingEnabled(bool enabled) noexcept
{
try {
if (auto pChannel = GetDefaultChannel()) {
if (auto pComponent = pChannel->GetComponent("obj:fsh")) {
if (auto pFlusher = std::dynamic_pointer_cast<ChannelFlusher>(pComponent)) {
pFlusher->SetEnabled(enabled);
}
}
}
}
catch (...) {
pmlog_panic_("Failed setting periodic log flushing state in server");
}
}

LogChannelManager::LogChannelManager() noexcept
{
Expand Down
3 changes: 2 additions & 1 deletion IntelPresentMon/PresentMonService/LogSetup.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ namespace logsetup
};
// call after command line arguments have been parsed
void ConfigureLogging(bool asApp) noexcept;
}
void SetPeriodicLogFlushingEnabled(bool enabled) noexcept;
}
1 change: 1 addition & 0 deletions IntelPresentMon/PresentMonService/acts/StartTracking.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ namespace pmon::svc::acts
}
target.pSegment = std::move(pSegment);
stx.trackedPids.emplace(in.targetPid, std::move(target));
ctx.UpdatePeriodicLogFlushing();
pmlog_info(std::format("StartTracking action from [{}] targeting [{}]",
stx.remotePid, in.targetPid));
return {};
Expand Down
1 change: 1 addition & 0 deletions IntelPresentMon/PresentMonService/acts/StopTracking.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace pmon::svc::acts
}
stx.trackedPids.erase(tpidIt);
ctx.pPmon->UpdateTracking(ctx.GetTrackedPidSet());
ctx.UpdatePeriodicLogFlushing();
pmlog_info(std::format("StopTracking action from [{}] un-targeting [{}]", stx.remotePid, in.targetPid));
return {};
}
Expand Down