Skip to content

Commit

Permalink
Add support for disabling background monitoring
Browse files Browse the repository at this point in the history
This change adds a "Background monitoring" check box to the settings
dialog. This new check box is checked by default but can be unchecked
to disable most of the background monitoring threads, in order to
minimize how disruption of power measurements for light CPU loads, and
to allow long-term tracing without excessively draining power.
  • Loading branch information
randomascii committed Aug 4, 2017
1 parent a068e88 commit 02a3c58
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 23 deletions.
2 changes: 1 addition & 1 deletion EventEmitter/EventEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
CPowerStatusMonitor powerMonitor;
powerMonitor.SetPerfCounters(perfCounters);
if (bMonitorPowerStuff)
powerMonitor.StartThreads();
powerMonitor.StartThreads(CPowerStatusMonitor::MonitorType::HeavyLoad);

CCPUFrequencyMonitor CPUFrequencyMonitor;
if (bMonitorFrequencyStuff)
Expand Down
16 changes: 12 additions & 4 deletions UIforETW/PowerStatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ limitations under the License.

// This sampling frequency leads to roughly 20 context switches per second, which is
// perhaps okay when tracing but must be avoided when tracing is not running.
const int kSamplingInterval = 200;
const int kHeavySamplingInterval = 200;
const int kLightSamplingInterval = 1000;

// These correspond to the funcID values returned by GetMsrFunc
// They are documented here:
Expand Down Expand Up @@ -382,13 +383,19 @@ void CPowerStatusMonitor::PowerMonitorThread()
}

unsigned sampleNumber = 0;
DWORD samplingInterval = (monitorType_ == MonitorType::HeavyLoad) ?
kHeavySamplingInterval :
kLightSamplingInterval;
for (;;)
{
DWORD result = WaitForSingleObject(hExitEvent_, kSamplingInterval);
DWORD result = WaitForSingleObject(hExitEvent_, samplingInterval);
if (result == WAIT_OBJECT_0)
break;

SampleBatteryStat();
if (monitorType_ == MonitorType::HeavyLoad)
{
SampleBatteryStat();
}
SampleCPUPowerState();
SampleTimerState();

Expand Down Expand Up @@ -473,12 +480,13 @@ void CPowerStatusMonitor::SetPerfCounters(std::wstring& perfCounters)
}
}

void CPowerStatusMonitor::StartThreads()
void CPowerStatusMonitor::StartThreads(MonitorType monitorType)
{
UIETWASSERT(!hExitEvent_);

if (!hExitEvent_)
{
monitorType_ = monitorType;
hExitEvent_ = CreateEvent(nullptr, FALSE, FALSE, nullptr);
hThread_ = CreateThread(NULL, 0, StaticPowerMonitorThread, this, 0, nullptr);
}
Expand Down
8 changes: 7 additions & 1 deletion UIforETW/PowerStatus.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ typedef int(*GetMaxTemperature_t)(int iNode, int* degreeC);
class CPowerStatusMonitor
{
public:
enum class MonitorType
{
LightLoad,
HeavyLoad
};
CPowerStatusMonitor();
~CPowerStatusMonitor();

Expand All @@ -39,7 +44,7 @@ class CPowerStatusMonitor

// Start and stop the sampling threads so that they aren't running
// when tracing is not running.
void StartThreads();
void StartThreads(MonitorType monitorType);
void StopThreads();

private:
Expand All @@ -53,6 +58,7 @@ class CPowerStatusMonitor

HANDLE hThread_ = nullptr;
HANDLE hExitEvent_ = nullptr;
MonitorType monitorType_ = MonitorType::HeavyLoad;

HMODULE energyLib_ = nullptr;
IntelEnergyLibInitialize_t IntelEnergyLibInitialize = nullptr;
Expand Down
1 change: 1 addition & 0 deletions UIforETW/Resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
#define IDC_RECORD_PRE_TRACE 1039
#define IDC_RECORD_PRE_TRACE2 1040
#define IDC_IDENTIFY_CHROME_CPU 1040
#define IDC_BACKGROUND_MONITORING 1041
#define ID_TRACES_OPENTRACEINWPA 32771
#define ID_TRACES_DELETETRACE 32772
#define ID_TRACES_COMPRESSTRACE 32773
Expand Down
13 changes: 13 additions & 0 deletions UIforETW/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ void CSettings::DoDataExchange(CDataExchange* pDX)
DDX_Control(pDX, IDC_CHECKFORNEWVERSIONS, btVersionChecks_);
DDX_Control(pDX, IDC_CHROME_CATEGORIES, btChromeCategories_);
DDX_Control(pDX, IDC_IDENTIFY_CHROME_CPU, btIdentifyChromeProcessesCPU_);
DDX_Control(pDX, IDC_BACKGROUND_MONITORING, btBackgroundMonitoring_);

CDialog::DoDataExchange(pDX);
}
Expand All @@ -120,6 +121,7 @@ BEGIN_MESSAGE_MAP(CSettings, CDialog)
ON_BN_CLICKED(IDC_USE_OTHER_KERNEL_LOGGER, &CSettings::OnBnClickedUseOtherKernelLogger)
ON_BN_CLICKED(IDC_RECORD_PRE_TRACE, &CSettings::OnBnClickedRecordPreTrace)
ON_BN_CLICKED(IDC_IDENTIFY_CHROME_CPU, &CSettings::OnBnClickedIdentifyChromeCpu)
ON_BN_CLICKED(IDC_BACKGROUND_MONITORING, &CSettings::OnBnClickedBackgroundMonitoring)
END_MESSAGE_MAP()

BOOL CSettings::OnInitDialog()
Expand All @@ -128,6 +130,7 @@ BOOL CSettings::OnInitDialog()

SetDlgItemText(IDC_HEAPEXE, heapTracingExes_.c_str());
CheckDlgButton(IDC_USE_OTHER_KERNEL_LOGGER, bUseOtherKernelLogger_);
CheckDlgButton(IDC_BACKGROUND_MONITORING, bBackgroundTracing_);
CheckDlgButton(IDC_CHROMEDEVELOPER, bChromeDeveloper_);
CheckDlgButton(IDC_IDENTIFY_CHROME_CPU, bIdentifyChromeProcessesCPU_);
CheckDlgButton(IDC_AUTOVIEWTRACES, bAutoViewTraces_);
Expand Down Expand Up @@ -214,6 +217,10 @@ BOOL CSettings::OnInitDialog()
toolTip_.AddTool(&btIdentifyChromeProcessesCPU_, L"If this is checked and \"Chrome developer\" is "
L"checked then CPU usage details of Chrome processes will be added to the trace information "
L"file when traces are recorded. Calculating the CPU usage details can take a while.");
toolTip_.AddTool(&btBackgroundMonitoring_, L"When this is checked background threads will periodically "
L"(a few times per second) record information about system performance to the trace being "
L"recorded. This can be helpful in understanding performance problems but may affect "
L"power consumption.");
}

// Initialize the list of check boxes with all of the Chrome categories which
Expand Down Expand Up @@ -404,3 +411,9 @@ void CSettings::OnBnClickedRecordPreTrace()
{
bRecordPreTrace_ = !bRecordPreTrace_;
}


void CSettings::OnBnClickedBackgroundMonitoring()
{
bBackgroundTracing_ = !bBackgroundTracing_;
}
3 changes: 3 additions & 0 deletions UIforETW/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class CSettings : public CDialog
std::wstring extraUserProviders_;
std::wstring perfCounters_;
bool bUseOtherKernelLogger_ = false;
bool bBackgroundTracing_ = true;
bool bChromeDeveloper_ = false;
bool bIdentifyChromeProcessesCPU_ = false;
bool bAutoViewTraces_ = false;
Expand All @@ -64,6 +65,7 @@ class CSettings : public CDialog
CButton btUseOtherKernelLogger_;
CButton btChromeDeveloper_;
CButton btIdentifyChromeProcessesCPU_;
CButton btBackgroundMonitoring_;
CButton btAutoViewTraces_;
CButton btRecordPreTrace_;
CButton btHeapStacks_;
Expand Down Expand Up @@ -96,4 +98,5 @@ class CSettings : public CDialog
afx_msg void OnBnClickedUseOtherKernelLogger();
afx_msg void OnBnClickedRecordPreTrace();
afx_msg void OnBnClickedIdentifyChromeCpu();
afx_msg void OnBnClickedBackgroundMonitoring();
};
1 change: 1 addition & 0 deletions UIforETW/Support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void CUIforETWDlg::TransferSettings(bool saving)
{ L"CLRTracing", &bCLRTracing_ },
{ L"ShowCommands", &bShowCommands_ },
{ L"UseOtherKernelLogger", &bUseOtherKernelLogger_ },
{ L"BackgroundMonitoring", &bBackgroundMonitoring_ },
{ L"ChromeDeveloper", &bChromeDeveloper_ },
{ L"IdentifyChromeProcessesCPU", &bIdentifyChromeProcessesCPU_ },
{ L"AutoViewTraces", &bAutoViewTraces_ },
Expand Down
30 changes: 16 additions & 14 deletions UIforETW/UIforETW.rc
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ BEGIN
CONTROL "Trace size:",IDC_TRACESIZE,"Static",SS_LEFTNOWORDWRAP | WS_GROUP,94,135,76,8
END

IDD_SETTINGS DIALOGEX 0, 0, 401, 197
IDD_SETTINGS DIALOGEX 0, 0, 401, 203
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Settings"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
Expand All @@ -149,21 +149,23 @@ BEGIN
CONTROL "Perf counters:",IDC_STATIC,"Static",SS_LEFTNOWORDWRAP | WS_GROUP,7,112,52,8
PUSHBUTTON "Se&lect...",IDC_SELECT_PERF_COUNTERS,61,109,38,14
EDITTEXT IDC_PERFORMANCECOUNTERS,102,109,133,14,ES_AUTOHSCROLL
PUSHBUTTON "&Copy startup profiles",IDC_COPYSTARTUPPROFILE,7,134,75,14
CONTROL "&Identify Chrome CPU",IDC_IDENTIFY_CHROME_CPU,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,154,83,10
CONTROL "Record pre-trace",IDC_RECORD_PRE_TRACE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,167,71,10
PUSHBUTTON "&Copy startup profiles",IDC_COPYSTARTUPPROFILE,7,126,75,14
CONTROL "&Background monitoring",IDC_BACKGROUND_MONITORING,
"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,147,89,10
CONTROL "&Identify Chrome CPU",IDC_IDENTIFY_CHROME_CPU,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,160,83,10
CONTROL "Record pre-trace",IDC_RECORD_PRE_TRACE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,173,71,10
CONTROL "Use other kernel logger",IDC_USE_OTHER_KERNEL_LOGGER,
"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,180,91,10
CONTROL "Ch&rome developer",IDC_CHROMEDEVELOPER,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,103,128,75,10
CONTROL "&Auto view traces",IDC_AUTOVIEWTRACES,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,103,141,70,10
CONTROL "&Stacks on heap tracing",IDC_HEAPSTACKS,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,103,154,89,10
"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,186,91,10
CONTROL "Ch&rome developer",IDC_CHROMEDEVELOPER,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,103,134,75,10
CONTROL "&Auto view traces",IDC_AUTOVIEWTRACES,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,103,147,70,10
CONTROL "&Stacks on heap tracing",IDC_HEAPSTACKS,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,103,160,89,10
CONTROL "&VirtualAlloc stacks always",IDC_VIRTUALALLOCSTACKS,
"Button",BS_AUTOCHECKBOX | WS_TABSTOP,103,167,97,10
CONTROL "Check for new versions",IDC_CHECKFORNEWVERSIONS,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,103,180,105,10
"Button",BS_AUTOCHECKBOX | WS_TABSTOP,103,173,97,10
CONTROL "Check for new versions",IDC_CHECKFORNEWVERSIONS,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,103,186,105,10
CONTROL "Chrome tracing cate&gories:",IDC_STATIC,"Static",SS_LEFTNOWORDWRAP | WS_GROUP,245,7,131,8
LISTBOX IDC_CHROME_CATEGORIES,245,18,149,155,LBS_OWNERDRAWFIXED | LBS_HASSTRINGS | LBS_NOINTEGRALHEIGHT | LBS_DISABLENOSCROLL | WS_VSCROLL | WS_TABSTOP
DEFPUSHBUTTON "OK",IDOK,286,176,50,14
PUSHBUTTON "Cancel",IDCANCEL,344,176,50,14
LISTBOX IDC_CHROME_CATEGORIES,245,18,149,162,LBS_OWNERDRAWFIXED | LBS_HASSTRINGS | LBS_NOINTEGRALHEIGHT | LBS_DISABLENOSCROLL | WS_VSCROLL | WS_TABSTOP
DEFPUSHBUTTON "OK",IDOK,286,182,50,14
PUSHBUTTON "Cancel",IDCANCEL,344,182,50,14
END


Expand Down Expand Up @@ -235,7 +237,7 @@ BEGIN
LEFTMARGIN, 7
RIGHTMARGIN, 394
TOPMARGIN, 7
BOTTOMMARGIN, 190
BOTTOMMARGIN, 196
END
END
#endif // APSTUDIO_INVOKED
Expand Down
13 changes: 10 additions & 3 deletions UIforETW/UIforETWDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -911,10 +911,15 @@ void CUIforETWDlg::StartEventThreads()
// that has run "too long". Checking every thirty seconds should be fine.
SetTimer(kTimerID, 30000, nullptr);

CPUFrequencyMonitor_.StartThreads();
if (bBackgroundMonitoring_)
{
CPUFrequencyMonitor_.StartThreads();
workingSetThread_.StartThreads();
}
PowerMonitor_.SetPerfCounters(perfCounters_);
PowerMonitor_.StartThreads();
workingSetThread_.StartThreads();
PowerMonitor_.StartThreads(bBackgroundMonitoring_ ?
CPowerStatusMonitor::MonitorType::HeavyLoad :
CPowerStatusMonitor::MonitorType::LightLoad);
}

void CUIforETWDlg::StopEventThreads()
Expand Down Expand Up @@ -1850,6 +1855,7 @@ void CUIforETWDlg::OnBnClickedSettings()
dlgSettings.extraUserProviders_ = extraUserProviders_;
dlgSettings.perfCounters_ = perfCounters_;
dlgSettings.bUseOtherKernelLogger_ = bUseOtherKernelLogger_;
dlgSettings.bBackgroundTracing_ = bBackgroundMonitoring_;
dlgSettings.bChromeDeveloper_ = bChromeDeveloper_;
dlgSettings.bIdentifyChromeProcessesCPU_ = bIdentifyChromeProcessesCPU_;
dlgSettings.bAutoViewTraces_ = bAutoViewTraces_;
Expand Down Expand Up @@ -1883,6 +1889,7 @@ void CUIforETWDlg::OnBnClickedSettings()
bIdentifyChromeProcessesCPU_ = dlgSettings.bIdentifyChromeProcessesCPU_;

// Copy over the remaining settings.
bBackgroundMonitoring_ = dlgSettings.bBackgroundTracing_;
bUseOtherKernelLogger_ = dlgSettings.bUseOtherKernelLogger_;
bRecordPreTrace_ = dlgSettings.bRecordPreTrace_;
WSMonitoredProcesses_ = dlgSettings.WSMonitoredProcesses_;
Expand Down
1 change: 1 addition & 0 deletions UIforETW/UIforETWDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ class CUIforETWDlg : public CDialog
CToolTipCtrl toolTip_;

// Editable only by the settings dialog.
bool bBackgroundMonitoring_ = true;
bool bChromeDeveloper_ = false;
bool bIdentifyChromeProcessesCPU_ = false;
bool bAutoViewTraces_ = false;
Expand Down

0 comments on commit 02a3c58

Please sign in to comment.