我想要做的是写一个小程序,它连续地实时计算特定进程在足够小的单位时间内经历的上下文切换的数量。我在软件"Process Explorer“中观察到了此功能,因此我知道这绝对是可能的。
不幸的是,我对如何开始编写代码知之甚少,而且到目前为止还无法在网上找到任何有用的代码片段。因此,一个实现每个进程和每个单位时间的活动上下文切换计数的小工作示例将对我有很大的帮助。
发布于 2012-09-29 23:22:04
这里有一种方法-这将打印notepad的线程0每秒使用的上下文切换量(您可以在CounterPathBuffer初始化中替换您想要的任何进程和线程数):
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <pdh.h>
#include <pdhmsg.h>
#pragma comment(lib, "pdh.lib")
using namespace std;
CONST ULONG SAMPLE_INTERVAL_MS = 1000;
CONST PWSTR BROWSE_DIALOG_CAPTION = L"Select a counter to monitor.";
void wmain(void)
{
PDH_STATUS Status;
HQUERY Query = NULL;
HCOUNTER Counter;
PDH_FMT_COUNTERVALUE DisplayValue;
DWORD CounterType;
SYSTEMTIME SampleTime;
PDH_BROWSE_DLG_CONFIG BrowseDlgData;
WCHAR CounterPathBuffer[PDH_MAX_COUNTER_PATH] = L"\\\\ComputerNameGoesHere\\Thread(notepad/0)\\Context Switches/sec";
//
// Create a query.
//
Status = PdhOpenQuery(NULL, NULL, &Query);
if (Status != ERROR_SUCCESS)
{
wprintf(L"\nPdhOpenQuery failed with status 0x%x.", Status);
goto Cleanup;
}
//
// Initialize the browser dialog window settings.
//
ZeroMemory(&BrowseDlgData, sizeof(PDH_BROWSE_DLG_CONFIG));
BrowseDlgData.bIncludeInstanceIndex = FALSE;
BrowseDlgData.bSingleCounterPerAdd = TRUE;
BrowseDlgData.bSingleCounterPerDialog = TRUE;
BrowseDlgData.bLocalCountersOnly = FALSE;
BrowseDlgData.bWildCardInstances = TRUE;
BrowseDlgData.bHideDetailBox = TRUE;
BrowseDlgData.bInitializePath = FALSE;
BrowseDlgData.bDisableMachineSelection = FALSE;
BrowseDlgData.bIncludeCostlyObjects = FALSE;
BrowseDlgData.bShowObjectBrowser = FALSE;
BrowseDlgData.hWndOwner = NULL;
BrowseDlgData.szReturnPathBuffer = CounterPathBuffer;
BrowseDlgData.cchReturnPathLength = PDH_MAX_COUNTER_PATH;
BrowseDlgData.pCallBack = NULL;
BrowseDlgData.dwCallBackArg = 0;
BrowseDlgData.CallBackStatus = ERROR_SUCCESS;
BrowseDlgData.dwDefaultDetailLevel = PERF_DETAIL_WIZARD;
BrowseDlgData.szDialogBoxCaption = BROWSE_DIALOG_CAPTION;
//
// Add the selected counter to the query.
//
Status = PdhAddCounter(Query, CounterPathBuffer, 0, &Counter);
if (Status != ERROR_SUCCESS)
{
wprintf(L"\nPdhAddCounter failed with status 0x%x.", Status);
goto Cleanup;
}
//
// Most counters require two sample values to display a formatted value.
// PDH stores the current sample value and the previously collected
// sample value. This call retrieves the first value that will be used
// by PdhGetFormattedCounterValue in the first iteration of the loop
// Note that this value is lost if the counter does not require two
// values to compute a displayable value.
//
Status = PdhCollectQueryData(Query);
if (Status != ERROR_SUCCESS)
{
wprintf(L"\nPdhCollectQueryData failed with 0x%x.\n", Status);
goto Cleanup;
}
//
// Print counter values until a key is pressed.
//
while (!_kbhit())
{
Sleep(SAMPLE_INTERVAL_MS);
GetLocalTime(&SampleTime);
Status = PdhCollectQueryData(Query);
if (Status != ERROR_SUCCESS)
{
wprintf(L"\nPdhCollectQueryData failed with status 0x%x.", Status);
}
wprintf(L"\n\"%2.2d/%2.2d/%4.4d %2.2d:%2.2d:%2.2d.%3.3d\"",
SampleTime.wMonth,
SampleTime.wDay,
SampleTime.wYear,
SampleTime.wHour,
SampleTime.wMinute,
SampleTime.wSecond,
SampleTime.wMilliseconds);
//
// Compute a displayable value for the counter.
//
Status = PdhGetFormattedCounterValue(Counter,
PDH_FMT_DOUBLE,
&CounterType,
&DisplayValue);
if (Status != ERROR_SUCCESS)
{
wprintf(L"\nPdhGetFormattedCounterValue failed with status 0x%x.", Status);
goto Cleanup;
}
wprintf(L",\"%.20g\"", DisplayValue.doubleValue);
}
Cleanup:
//
// Close the query.
//
if (Query)
{
PdhCloseQuery(Query);
}
int x;
cin >>x;
}大部分代码都来自这个源码:msdn.microsoft.com/en-us/library/aa371886%28v=vs.85%29.aspx。我想缩短上下文切换的连续检查之间的时间量(使其少于一秒)。如果任何人有任何关于如何做到这一点的想法,那就太好了。
发布于 2012-09-29 11:43:31
在互联网上搜索了一下,我发现了一个叫做"Performance Counters“的东西,你可以提供计数器数据,也可以用它来消费计数器数据。在您的情况下,我相信您希望从性能计数器读取数据。
您可以使用注册表接口或PDH接口使用性能数据。PDH接口比注册表接口更易于使用,建议用于大多数性能数据收集任务。PDH接口本质上是注册表接口提供的功能的更高级别的抽象。
这是微软在Monitoring Context Switches上的另一篇文章。
发布于 2012-10-04 11:43:45
如果你愿意,ETW (Event Tracing for Windows)将为你提供更深入的上下文切换信息。代码可能比访问性能计数器复杂得多。
初始链接:Core OS Events in Windows 7和Windows Performance Toolkit Technical Reference
搜索词:"ETW context switches","xperf“
https://stackoverflow.com/questions/12607795
复制相似问题