Skip to content

Commit

Permalink
Added reporting of allocated memory use across all browser processes …
Browse files Browse the repository at this point in the history
…and the number of browser processes.
  • Loading branch information
pmeenan committed Sep 5, 2013
1 parent 1bf2a78 commit 601fcd5
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 46 deletions.
7 changes: 7 additions & 0 deletions agent/wptglobal/wptglobal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
HINSTANCE global_dll_handle = NULL; // DLL handle
AngleHook * g_angle_hook = NULL;
Dx9Hook * g_dx9_hook = NULL;
UINT process_message = 0;

/*-----------------------------------------------------------------------------
Install the global hook
Expand Down Expand Up @@ -83,6 +84,9 @@ void Initialize() {
g_dx9_hook->Init();
}
}
process_message = RegisterWindowMessage(_T("WPT Process Change"));
PostMessage(HWND_BROADCAST, process_message,
(WPARAM)1, (LPARAM)GetCurrentProcessId());
}
}

Expand All @@ -100,4 +104,7 @@ void Unload() {
delete g_dx9_hook;
g_dx9_hook = NULL;
}
if (process_message)
PostMessage(HWND_BROADCAST, process_message,
(WPARAM)0, (LPARAM)GetCurrentProcessId());
}
18 changes: 1 addition & 17 deletions agent/wpthook/hook_gdi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,6 @@ bool CGDIHook::IsDocumentWindow(HWND hWnd) {
document_windows_.SetAt(hWnd, is_document);
}

if (hWnd && hWnd != test_state_._document_window &&
!test_state_._exit && test_state_._active && is_document) {
test_state_.SetDocument(hWnd);
}

return is_document;
}

Expand Down Expand Up @@ -252,19 +247,8 @@ int CGDIHook::ReleaseDC(HWND hWnd, HDC hDC)
if( ReleaseDC_ )
ret = ReleaseDC_(hWnd, hDC);

bool is_document = false;
if (!document_windows_.Lookup(hWnd, is_document)) {
is_document = IsBrowserDocument(hWnd);
document_windows_.SetAt(hWnd, is_document);
}

if (hWnd && hWnd != test_state_._document_window &&
!test_state_._exit && test_state_._active && is_document) {
test_state_.SetDocument(hWnd);
}

if (!wpt_capturing_screen && !test_state_._exit && test_state_._active &&
hWnd == test_state_._document_window) {
IsDocumentWindow(hWnd)) {
test_state_._screen_updated = true;
test_state_.CheckStartRender();
}
Expand Down
22 changes: 17 additions & 5 deletions agent/wpthook/results.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ void Results::Reset(void) {
count_not_found_doc_ = 0;
count_other_ = 0;
count_other_doc_ = 0;
peak_memory_ = 0;
peak_process_count_ = 0;
}

/*-----------------------------------------------------------------------------
Expand Down Expand Up @@ -147,21 +149,25 @@ void Results::Save(void) {
void Results::SaveProgressData(void) {
CStringA progress;
POSITION pos = _test_state._progress_data.GetHeadPosition();
while( pos )
{
if( progress.IsEmpty() )
peak_memory_ = 0;
peak_process_count_ = 0;
while( pos ) {
if (progress.IsEmpty())
progress = "Offset Time (ms),Bandwidth In (kbps),"
"CPU Utilization (%),Memory Use (KB)\r\n";
ProgressData data = _test_state._progress_data.GetNext(pos);
DWORD ms = _test_state.ElapsedMsFromStart(data._time);
CStringA buff;
buff.Format("%d,%d,%0.2f,%d\r\n", ms, data._bpsIn, data._cpu, data._mem );
progress += buff;
if (data._mem > peak_memory_)
peak_memory_ = data._mem;
if (data._process_count > peak_process_count_)
peak_process_count_ = data._process_count;
}
HANDLE hFile = CreateFile(_file_base + PROGRESS_DATA_FILE, GENERIC_WRITE, 0,
NULL, CREATE_ALWAYS, 0, 0);
if( hFile != INVALID_HANDLE_VALUE )
{
if (hFile != INVALID_HANDLE_VALUE) {
DWORD dwBytes;
WriteFile(hFile, (LPCSTR)progress, progress.GetLength(), &dwBytes, 0);
CloseHandle(hFile);
Expand Down Expand Up @@ -638,6 +644,12 @@ void Results::SavePageData(OptimizationChecks& checks){
// W3C Navigation timing first paint (MS-specific right now)
buff.Format("%d\t", _test_state._first_paint);
result += buff;
// Peak memory allocation across all browser processes
buff.Format("%d\t", peak_memory_);
result += buff;
// Peak number of running browser processes
buff.Format("%d\t", peak_process_count_);
result += buff;

result += "\r\n";

Expand Down
3 changes: 3 additions & 0 deletions agent/wpthook/results.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class Results {
int count_other_;
int count_other_doc_;

DWORD peak_memory_;
DWORD peak_process_count_;

void ProcessRequests(void);
void SavePageData(OptimizationChecks&);
void SaveRequests(OptimizationChecks&);
Expand Down
96 changes: 77 additions & 19 deletions agent/wpthook/test_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "../wptdriver/util.h"
#include "cximage/ximage.h"
#include <Mmsystem.h>
#include <WtsApi32.h>
#include "wpt_test_hook.h"
#include "dev_tools.h"

Expand Down Expand Up @@ -67,13 +68,35 @@ TestState::TestState(Results& results, ScreenCapture& screen_capture,
InitializeCriticalSection(&_data_cs);
FindBrowserNameAndVersion();
paint_msg_ = RegisterWindowMessage(_T("WPT Browser Paint"));
process_msg_ = RegisterWindowMessage(_T("WPT Process Change"));

// build an initial list of known browser processes
InitializeCriticalSection(&_cs_browser_processes);
_browser_processes.InitHashTable(257);
TCHAR path[10000];
if (GetProcessImageFileName(GetCurrentProcess(), path, _countof(path))) {
process_full_path_ = path;
CString process_base_exe_ = PathFindFileName(path);;
WTS_PROCESS_INFO * proc = NULL;
DWORD proc_count = 0;
if (WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0, 1, &proc,
&proc_count)) {
for (DWORD i = 0; i < proc_count; i++) {
TCHAR * file = PathFindFileName(proc[i].pProcessName);
if (!process_base_exe_.CompareNoCase(file))
ProcessStarted(proc[i].ProcessId);
}
WTSFreeMemory(proc);
}
}
}

/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
TestState::~TestState(void) {
Done(true);
DeleteCriticalSection(&_data_cs);
DeleteCriticalSection(&_cs_browser_processes);
}

/*-----------------------------------------------------------------------------
Expand Down Expand Up @@ -465,20 +488,6 @@ void TestState::UpdateBrowserWindow() {
}
}

/*-----------------------------------------------------------------------------
Change the document window
-----------------------------------------------------------------------------*/
void TestState::SetDocument(HWND wnd) {
WptTrace(loglevel::kFunction,
_T("[wpthook] - TestState::SetDocument(%d)"), wnd);
if (!_started && wnd != _document_window) {
_document_window = wnd;
_frame_window = GetAncestor(_document_window, GA_ROOTOWNER);
if (_document_window == _frame_window)
FindViewport();
}
}

/*-----------------------------------------------------------------------------
Grab a video frame if it is appropriate
-----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -690,13 +699,33 @@ void TestState::CollectSystemStats(LARGE_INTEGER &now) {
_last_cpu_user.QuadPart = u.QuadPart;
}

// get the memory use (working set - task-manager style)
// get the allocated memory across all instances of the current exe
if (msElapsed) {
PROCESS_MEMORY_COUNTERS mem;
mem.cb = sizeof(mem);
if( GetProcessMemoryInfo(GetCurrentProcess(), &mem, sizeof(mem)) )
data._mem = mem.WorkingSetSize / 1024;
DWORD total_mem = 0;
DWORD process_count = 0;
EnterCriticalSection(&_cs_browser_processes);
if (!_browser_processes.IsEmpty()) {
POSITION pos = _browser_processes.GetStartPosition();
while (pos) {
DWORD process_id = _browser_processes.GetNextKey(pos);
HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, process_id);
if (process) {
PROCESS_MEMORY_COUNTERS mem;
mem.cb = sizeof(mem);
if (GetProcessMemoryInfo(process, &mem, sizeof(mem))) {
total_mem += mem.PagefileUsage / 1024;
process_count++;
}
CloseHandle(process);
}
}
}
LeaveCriticalSection(&_cs_browser_processes);

data._mem = total_mem;
data._process_count = process_count;
_progress_data.AddTail(data);
}
}
Expand Down Expand Up @@ -940,3 +969,32 @@ CString TestState::GetTimedEventsJSON() {
LeaveCriticalSection(&_data_cs);
return ret;
}

/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void TestState::ProcessStarted(DWORD process_id) {
AtlTrace(_T("[wpthook] - Process %d Started"), process_id);
HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, process_id);
if (process) {
TCHAR path[10000];
if (GetProcessImageFileName(process, path, _countof(path))) {
AtlTrace(_T("[wpthook] - Process Started: %s"), path);
if (!process_full_path_.CompareNoCase(path)) {
AtlTrace(_T("[wpthook] - Browser Process Started: %s"), path);
EnterCriticalSection(&_cs_browser_processes);
_browser_processes.SetAt(process_id, true);
LeaveCriticalSection(&_cs_browser_processes);
}
}
CloseHandle(process);
}
}

/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void TestState::ProcessStopped(DWORD process_id) {
AtlTrace(_T("[wpthook] - Process %d Stopped"), process_id);
EnterCriticalSection(&_cs_browser_processes);
_browser_processes.RemoveKey(process_id);
LeaveCriticalSection(&_cs_browser_processes);
}
17 changes: 14 additions & 3 deletions agent/wpthook/test_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,25 @@ const int TEST_RESULT_CONTENT_ERROR = 99999;

class ProgressData {
public:
ProgressData(void):_bpsIn(0),_cpu(0.0),_mem(0){ _time.QuadPart = 0; }
ProgressData(void):_bpsIn(0),_cpu(0.0),_mem(0),_process_count(0) {
_time.QuadPart = 0;
}
ProgressData(const ProgressData& src){*this = src;}
~ProgressData(){ }
const ProgressData& operator =(const ProgressData& src) {
_time.QuadPart = src._time.QuadPart;
_bpsIn = src._bpsIn;
_cpu = src._cpu;
_mem = src._mem;
_process_count = src._process_count;
return src;
}

LARGE_INTEGER _time;
DWORD _bpsIn; // inbound bandwidth
double _cpu; // CPU utilization
DWORD _mem; // Working set size (in KB)
DWORD _mem; // allocated memory (in KB)
DWORD _process_count; // number of browser processes
};

class StatusMessage {
Expand Down Expand Up @@ -101,13 +105,14 @@ class TestState {
void Init();
void TitleSet(CString title);
void UpdateBrowserWindow();
void SetDocument(HWND wnd);
DWORD ElapsedMsFromStart(LARGE_INTEGER end) const;
void FindBrowserNameAndVersion();
void AddConsoleLogMessage(CString message);
void AddTimedEvent(CString timed_event);
CString GetConsoleLogJSON();
CString GetTimedEventsJSON();
void ProcessStarted(DWORD process_id);
void ProcessStopped(DWORD process_id);

// times
LARGE_INTEGER _start;
Expand Down Expand Up @@ -152,6 +157,7 @@ class TestState {
bool no_gdi_;
bool gdi_only_;
UINT paint_msg_;
UINT process_msg_;

HWND _frame_window;
HWND _document_window;
Expand All @@ -173,6 +179,11 @@ class TestState {
HANDLE _data_timer;
CAtlList<CString> _console_log_messages; // messages to the console
CAtlList<CString> _timed_events; // any supported timed events
CRITICAL_SECTION _cs_browser_processes;
CAtlMap<DWORD, bool> _browser_processes;
CString process_full_path_;
CString process_base_exe_;


// tracking of the periodic data capture
LARGE_INTEGER _last_data;
Expand Down
5 changes: 5 additions & 0 deletions agent/wpthook/wpthook.cc
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ bool WptHook::OnMessage(UINT message, WPARAM wParam, LPARAM lParam) {
test_state_.PaintEvent(LOWORD(wParam), HIWORD(wParam),
LOWORD(lParam), HIWORD(lParam));
}
} else if (message == test_state_.process_msg_) {
if (wParam)
test_state_.ProcessStarted((DWORD)lParam);
else
test_state_.ProcessStopped((DWORD)lParam);
} else if (message == report_message_) {
OnReport();
} else {
Expand Down
4 changes: 2 additions & 2 deletions agent/wpthook/wpthook.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>kernel32.lib;user32.lib;Psapi.lib;Imagehlp.lib;version.lib;../wptdriver/dbghelp/dbghelp.lib;wininet.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Ws2_32.lib;winmm.lib;jpeg/lib/turbojpeg-static.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>kernel32.lib;user32.lib;Psapi.lib;Imagehlp.lib;version.lib;../wptdriver/dbghelp/dbghelp.lib;wininet.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Ws2_32.lib;winmm.lib;Wtsapi32.lib;jpeg/lib/turbojpeg-static.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>
</AdditionalLibraryDirectories>
</Link>
Expand All @@ -84,7 +84,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>kernel32.lib;user32.lib;Psapi.lib;Imagehlp.lib;version.lib;../wptdriver/dbghelp/dbghelp.lib;wininet.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Ws2_32.lib;winmm.lib;jpeg/lib/turbojpeg-static.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>kernel32.lib;user32.lib;Psapi.lib;Imagehlp.lib;version.lib;../wptdriver/dbghelp/dbghelp.lib;wininet.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Ws2_32.lib;winmm.lib;Wtsapi32.lib;jpeg/lib/turbojpeg-static.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>
</AdditionalLibraryDirectories>
</Link>
Expand Down
2 changes: 2 additions & 0 deletions www/page_data.inc
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ function loadPageData($file, $options = null)
$ret['fixed_viewport'] = (array_key_exists(88, $fields) && strlen(trim($fields[88]))) ? (int)trim($fields[88]) : -1;
$ret['score_progressive_jpeg'] = (array_key_exists(89, $fields) && strlen(trim($fields[89]))) ? (int)trim($fields[89]) : -1;
$ret['firstPaint'] = (array_key_exists(90, $fields) && strlen(trim($fields[90]))) ? (int)trim($fields[90]) : 0;
$ret['peakMem'] = (array_key_exists(91, $fields) && strlen(trim($fields[91]))) ? (int)trim($fields[91]) : 0;
$ret['processCount'] = (array_key_exists(92, $fields) && strlen(trim($fields[92]))) ? (int)trim($fields[92]) : 0;

$ret['date'] = strtotime(trim($fields[0]) . ' ' . trim($fields[1]));
if (!strlen($ret['pageSpeedVersion']))
Expand Down

0 comments on commit 601fcd5

Please sign in to comment.