WebKit/win:
authoralice.liu@apple.com <alice.liu@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 23 Sep 2008 06:25:03 +0000 (06:25 +0000)
committeralice.liu@apple.com <alice.liu@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 23 Sep 2008 06:25:03 +0000 (06:25 +0000)
2008-09-22  Alice Liu  <alice.liu@apple.com>

        Added record-memory-win.vcproj tool to the solution

        Reviewed by Steve Falkenburg.

        * WebKit.vcproj/WebKit.sln:

WebKitTools:

2008-09-22  Alice Liu  <alice.liu@apple.com>

        Adding a stand-alone Windows console application to record a process's memory usage

        Reviewed by Steve Falkenburg.

        * record-memory-win: Added.
        * record-memory-win/main.cpp: Added.
        * record-memory-win/record-memory-win.vcproj: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@36796 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebKit/win/ChangeLog
WebKit/win/WebKit.vcproj/WebKit.sln
WebKitTools/ChangeLog
WebKitTools/record-memory-win/main.cpp [new file with mode: 0644]
WebKitTools/record-memory-win/record-memory-win.vcproj [new file with mode: 0644]

index 67e633b..28f3b5f 100644 (file)
@@ -1,3 +1,11 @@
+2008-09-22  Alice Liu  <alice.liu@apple.com>
+
+        Added record-memory-win.vcproj tool to the solution
+
+        Reviewed by Steve Falkenburg.
+
+        * WebKit.vcproj/WebKit.sln:
+
 2008-09-20  Darin Adler  <darin@apple.com>
 
         - fix build
index a154fcc..beca071 100644 (file)
@@ -83,6 +83,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "testapi", "..\..\..\JavaScr
                {DA31DA52-6675-48D4-89E0-333A7144397C} = {DA31DA52-6675-48D4-89E0-333A7144397C}\r
        EndProjectSection\r
 EndProject\r
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "record-memory-win", "..\..\..\WebKitTools\record-memory-win\record-memory-win.vcproj", "{44B9C152-1870-4035-B94D-7B3285AA0C12}"\r
+EndProject\r
 Global\r
        GlobalSection(SolutionConfigurationPlatforms) = preSolution\r
                Debug_Cairo|Win32 = Debug_Cairo|Win32\r
@@ -262,6 +264,16 @@ Global
                {1AFD081F-1AC7-4A97-8EFA-6DF97761A3A2}.Release_Cairo|Win32.Build.0 = Release|Win32\r
                {1AFD081F-1AC7-4A97-8EFA-6DF97761A3A2}.Release|Win32.ActiveCfg = Release|Win32\r
                {1AFD081F-1AC7-4A97-8EFA-6DF97761A3A2}.Release|Win32.Build.0 = Release|Win32\r
+               {44B9C152-1870-4035-B94D-7B3285AA0C12}.Debug_Cairo|Win32.ActiveCfg = Release|Win32\r
+               {44B9C152-1870-4035-B94D-7B3285AA0C12}.Debug_Cairo|Win32.Build.0 = Release|Win32\r
+               {44B9C152-1870-4035-B94D-7B3285AA0C12}.Debug_Internal|Win32.ActiveCfg = Release|Win32\r
+               {44B9C152-1870-4035-B94D-7B3285AA0C12}.Debug_Internal|Win32.Build.0 = Release|Win32\r
+               {44B9C152-1870-4035-B94D-7B3285AA0C12}.Debug|Win32.ActiveCfg = Release|Win32\r
+               {44B9C152-1870-4035-B94D-7B3285AA0C12}.Debug|Win32.Build.0 = Release|Win32\r
+               {44B9C152-1870-4035-B94D-7B3285AA0C12}.Release_Cairo|Win32.ActiveCfg = Release|Win32\r
+               {44B9C152-1870-4035-B94D-7B3285AA0C12}.Release_Cairo|Win32.Build.0 = Release|Win32\r
+               {44B9C152-1870-4035-B94D-7B3285AA0C12}.Release|Win32.ActiveCfg = Release|Win32\r
+               {44B9C152-1870-4035-B94D-7B3285AA0C12}.Release|Win32.Build.0 = Release|Win32\r
        EndGlobalSection\r
        GlobalSection(SolutionProperties) = preSolution\r
                HideSolutionNode = FALSE\r
index 06629ad..81e3c02 100644 (file)
@@ -1,3 +1,13 @@
+2008-09-22  Alice Liu  <alice.liu@apple.com>
+
+        Adding a stand-alone Windows console application to record a process's memory usage
+
+        Reviewed by Steve Falkenburg.
+
+        * record-memory-win: Added.
+        * record-memory-win/main.cpp: Added.
+        * record-memory-win/record-memory-win.vcproj: Added.
+
 2008-09-22  Chris Fleizach  <cfleizach@apple.com>
 
         Reviewed by Darin Adler.
diff --git a/WebKitTools/record-memory-win/main.cpp b/WebKitTools/record-memory-win/main.cpp
new file mode 100644 (file)
index 0000000..7a2608f
--- /dev/null
@@ -0,0 +1,193 @@
+#include <assert.h>\r
+#include <atlstr.h>\r
+#include <psapi.h>\r
+#include <stdio.h>\r
+#include <tchar.h>\r
+#include <time.h>\r
+#include <windows.h>\r
+\r
+#pragma comment(lib, "psapi.lib")\r
+\r
+bool gSingleProcess = true;\r
+int gQueryInterval = 5; // seconds\r
+time_t gDuration = 0;   // seconds\r
+CString gCommandLine;\r
+\r
+HRESULT ProcessArgs(int argc, TCHAR *argv[]);\r
+HRESULT PrintUsage();\r
+void UseImage(void (functionForQueryType(HANDLE)));\r
+void QueryContinuously(HANDLE hProcess);\r
+time_t ElapsedTime(time_t startTime);\r
+unsigned int OneQuery(HANDLE hProcess);\r
+unsigned int OneQueryMP(HANDLE hProcess);\r
+\r
+int __cdecl _tmain (int argc, TCHAR *argv[])\r
+{\r
+    HRESULT result = ProcessArgs(argc, argv);\r
+    if (FAILED(result))\r
+        return result;\r
+\r
+    UseImage(QueryContinuously);\r
+    return S_OK;\r
+}\r
+\r
+HRESULT ProcessArgs(int argc, TCHAR *argv[])\r
+{\r
+    CString argument;\r
+    for( int count = 1; count < argc; count++ ) {\r
+        argument = argv[count];\r
+\r
+        if ((argument.Find(_T("-h")) != -1) ||\r
+            (argument.Find(_T("--help")) != -1))\r
+            return PrintUsage();\r
+        else if (argument.Find(_T("--exe")) != -1) {\r
+            gCommandLine = argv[++count];\r
+            if (gCommandLine.Find(_T("chrome.exe")) != -1)\r
+                gSingleProcess = false;\r
+        } else if ((argument.Find(_T("-i")) != -1) ||\r
+            (argument.Find(_T("--interval")) != -1)) {\r
+            gQueryInterval = _wtoi(argv[++count]);\r
+            if (gQueryInterval < 1) {\r
+                printf("ERROR: invalid interval\n");\r
+                return E_INVALIDARG;\r
+            }\r
+        } else if ((argument.Find(_T("-d")) != -1) ||\r
+            (argument.Find(_T("--duration")) != -1)) {\r
+            gDuration = _wtoi(argv[++count]);\r
+            if (gDuration < 1) {\r
+                printf("ERROR: invalid duration\n");\r
+                return E_INVALIDARG;\r
+            }\r
+        } else {\r
+            _tprintf(_T("ERROR: unrecognized argument \"%s\"\n"), (LPCTSTR)argument);\r
+            return PrintUsage();\r
+        }\r
+    }\r
+    if (argc < 2 || gCommandLine.IsEmpty()) {\r
+        printf("ERROR: executable path is required\n");\r
+        return PrintUsage();\r
+    }\r
+        \r
+    return S_OK;\r
+}\r
+\r
+HRESULT PrintUsage()\r
+{\r
+    printf("record-memory-win --exe EXE_PATH\n");\r
+    printf("    Launch an executable and print the memory usage (in Private Bytes)\n");\r
+    printf("    of the process.\n\n");\r
+    printf("Usage:\n");\r
+    printf("-h [--help]         : Print usage\n");\r
+    printf("--exe arg           : Launch specified image.  Required\n");\r
+    printf("-i [--interval] arg : Print memory usage every arg seconds.  Default: 5 seconds\n");\r
+    printf("-d [--duration] arg : Run for up to arg seconds.  Default: no limit\n\n");\r
+    printf("Examples:\n");\r
+    printf("    record-memory-win --exe \"C:\\Program Files\\Safari\\Safari.exe\"\n");\r
+    printf("    record-memory-win --exe Safari.exe -i 10 -d 7200\n");\r
+    return E_FAIL;\r
+}\r
+\r
+void UseImage(void (functionForQueryType(HANDLE)))\r
+{\r
+    STARTUPINFO si = {0};\r
+    si.cb = sizeof(STARTUPINFO);\r
+    PROCESS_INFORMATION pi = {0};\r
+\r
+    LPWSTR commandLine = gCommandLine.GetBuffer(MAX_PATH);  // WHAT'sTHIS? \r
+\r
+    // Start the child process. \r
+    if(!CreateProcess( NULL,   // No module name (use command line)\r
+        commandLine,        // Command line\r
+        NULL,           // Process handle not inheritable\r
+        NULL,           // Thread handle not inheritable\r
+        FALSE,          // Set handle inheritance to FALSE\r
+        0,              // No creation flags\r
+        NULL,           // Use parent's environment block\r
+        NULL,           // Use parent's starting directory \r
+        &si,            // Pointer to STARTUPINFO structure\r
+        &pi ))          // Pointer to PROCESS_INFORMATION structure\r
+        printf("CreateProcess failed (%d)\n", GetLastError());\r
+    else {\r
+        printf("Created process\n");\r
+        functionForQueryType(pi.hProcess);\r
+        // Close process and thread handles. \r
+        CloseHandle( pi.hProcess );\r
+        CloseHandle( pi.hThread );\r
+    }\r
+}\r
+\r
+void QueryContinuously(HANDLE hProcess)\r
+{\r
+    ::Sleep(2000); // give the process some time to launch\r
+    bool pastDuration = false;\r
+    time_t startTime = time(NULL);\r
+    unsigned int memUsage = gSingleProcess ? OneQuery(hProcess) : OneQueryMP(hProcess);\r
+    while(memUsage && !pastDuration) {\r
+        printf( "%u\n", memUsage );\r
+        ::Sleep(gQueryInterval*1000);\r
+        memUsage = gSingleProcess ? OneQuery(hProcess) : OneQueryMP(hProcess);\r
+        pastDuration = gDuration > 0 ? ElapsedTime(startTime) > gDuration : false;\r
+    } \r
+}\r
+\r
+// returns elapsed time in seconds\r
+time_t ElapsedTime(time_t startTime)\r
+{\r
+    time_t currentTime = time(NULL);\r
+    return currentTime - startTime;\r
+}\r
+\r
+// returns Commit Size (Private Bytes) in bytes\r
+unsigned int OneQuery(HANDLE hProcess)\r
+{\r
+    PROCESS_MEMORY_COUNTERS_EX pmc;\r
+    if (NULL == hProcess)\r
+        return 0;\r
+    if (::GetProcessMemoryInfo(hProcess, (PPROCESS_MEMORY_COUNTERS)&pmc, sizeof(pmc)))\r
+        return (unsigned)pmc.PrivateUsage;\r
+    return 0;\r
+}\r
+\r
+// returns Commit Size (Private Bytes) in bytes for multi-process executables\r
+unsigned int OneQueryMP(HANDLE hProcess)\r
+{\r
+    unsigned int memUsage = 0;\r
+    CString processName;\r
+    LPTSTR szProcessName = processName.GetBuffer(MAX_PATH);\r
+    ::GetProcessImageFileName(hProcess, szProcessName, processName.GetAllocLength());\r
+    processName.ReleaseBuffer();\r
+    CString shortProcessName = ::PathFindFileName(processName);\r
+    DWORD aProcesses[1024], cbNeeded, cProcesses;\r
+    HANDLE hSpawnedProcess;\r
+    if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))\r
+        return 0;\r
+\r
+    // Calculate how many process identifiers were returned.\r
+    cProcesses = cbNeeded / sizeof(DWORD);\r
+    // find existing process\r
+    for (unsigned int i = 0; i < cProcesses; i++)\r
+        if (aProcesses[i] != 0) {\r
+            DWORD retVal = 0;\r
+            TCHAR szProcessName[MAX_PATH];\r
+\r
+            // Get a handle to the process.\r
+            hSpawnedProcess = OpenProcess(PROCESS_QUERY_INFORMATION |\r
+                                   PROCESS_VM_READ,\r
+                                   FALSE, aProcesses[i]);\r
+\r
+            // Get the process name.\r
+            if (NULL != hSpawnedProcess) {\r
+                HMODULE hMod;\r
+                DWORD cbNeeded;\r
+\r
+                if (EnumProcessModules(hSpawnedProcess, &hMod, sizeof(hMod), &cbNeeded)) {\r
+                    GetModuleBaseName(hSpawnedProcess, hMod, szProcessName, sizeof(szProcessName)/sizeof(TCHAR));\r
+                    CString processName(szProcessName);\r
+                    if (processName.Find(shortProcessName) != -1)\r
+                        memUsage += OneQuery(hSpawnedProcess);\r
+                }\r
+            }\r
+            CloseHandle(hSpawnedProcess);\r
+        }\r
+    return memUsage;\r
+}\r
diff --git a/WebKitTools/record-memory-win/record-memory-win.vcproj b/WebKitTools/record-memory-win/record-memory-win.vcproj
new file mode 100644 (file)
index 0000000..6ec3c27
--- /dev/null
@@ -0,0 +1,124 @@
+<?xml version="1.0" encoding="Windows-1252"?>\r
+<VisualStudioProject\r
+       ProjectType="Visual C++"\r
+       Version="8.00"\r
+       Name="record-memory-win"\r
+       ProjectGUID="{44B9C152-1870-4035-B94D-7B3285AA0C12}"\r
+       RootNamespace="recordmemorywin"\r
+       Keyword="Win32Proj"\r
+       >\r
+       <Platforms>\r
+               <Platform\r
+                       Name="Win32"\r
+               />\r
+       </Platforms>\r
+       <ToolFiles>\r
+       </ToolFiles>\r
+       <Configurations>\r
+               <Configuration\r
+                       Name="Release|Win32"\r
+                       OutputDirectory="$(WebKitOutputDir)\bin"\r
+                       IntermediateDirectory="$(WebKitOutputDir)\obj\$(ProjectName)\$(ConfigurationName)"\r
+                       ConfigurationType="1"\r
+                       CharacterSet="1"\r
+                       WholeProgramOptimization="1"\r
+                       >\r
+                       <Tool\r
+                               Name="VCPreBuildEventTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCCustomBuildTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCXMLDataGeneratorTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCWebServiceProxyGeneratorTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCMIDLTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCCLCompilerTool"\r
+                               AdditionalIncludeDirectories="&quot;$(VCInstallDir)\PlatformSDK\Include&quot;;&quot;$(VCInstallDir)\Include&quot;"\r
+                               PreprocessorDefinitions="_WIN32_WINNT=0x501"\r
+                               RuntimeLibrary="2"\r
+                               UsePrecompiledHeader="0"\r
+                               WarningLevel="3"\r
+                               Detect64BitPortabilityProblems="true"\r
+                               DebugInformationFormat="3"\r
+                       />\r
+                       <Tool\r
+                               Name="VCManagedResourceCompilerTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCResourceCompilerTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCPreLinkEventTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCLinkerTool"\r
+                               LinkIncremental="1"\r
+                               AdditionalLibraryDirectories="&quot;$(VCInstallDir)\PlatformSDK\Lib&quot;"\r
+                               GenerateDebugInformation="true"\r
+                               SubSystem="1"\r
+                               OptimizeReferences="2"\r
+                               EnableCOMDATFolding="2"\r
+                               TargetMachine="1"\r
+                       />\r
+                       <Tool\r
+                               Name="VCALinkTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCManifestTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCXDCMakeTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCBscMakeTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCFxCopTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCAppVerifierTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCWebDeploymentTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCPostBuildEventTool"\r
+                       />\r
+               </Configuration>\r
+       </Configurations>\r
+       <References>\r
+       </References>\r
+       <Files>\r
+               <Filter\r
+                       Name="Source Files"\r
+                       Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"\r
+                       UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"\r
+                       >\r
+                       <File\r
+                               RelativePath=".\main.cpp"\r
+                               >\r
+                       </File>\r
+               </Filter>\r
+               <Filter\r
+                       Name="Header Files"\r
+                       Filter="h;hpp;hxx;hm;inl;inc;xsd"\r
+                       UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"\r
+                       >\r
+               </Filter>\r
+               <Filter\r
+                       Name="Resource Files"\r
+                       Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"\r
+                       UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"\r
+                       >\r
+               </Filter>\r
+       </Files>\r
+       <Globals>\r
+       </Globals>\r
+</VisualStudioProject>\r