Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 #include "base/debug/gdi_debug_util_win.h" | 4 #include "base/debug/gdi_debug_util_win.h" |
| 5 | 5 |
| 6 #include <cmath> | 6 #include <cmath> |
| 7 | 7 |
| 8 #include <psapi.h> | 8 #include <psapi.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 #include <TlHelp32.h> | 10 #include <TlHelp32.h> |
| 11 | 11 |
| 12 #include "base/debug/alias.h" | 12 #include "base/debug/alias.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/win/scoped_handle.h" | 14 #include "base/win/scoped_handle.h" |
| 15 #include "base/win/win_util.h" | 15 #include "base/win/win_util.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 void CollectChildGDIUsageAndDie(DWORD parent_pid) { | 19 void CollectChildGDIUsageAndDie(DWORD parent_pid) { |
| 20 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); | 20 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 21 CHECK_NE(INVALID_HANDLE_VALUE, snapshot); | 21 CHECK_NE(INVALID_HANDLE_VALUE, snapshot); |
| 22 | 22 |
| 23 int total_process_count = 0; | |
| 24 base::debug::Alias(&total_process_count); | |
| 25 int total_peak_gdi_count = 0; | |
| 26 base::debug::Alias(&total_peak_gdi_count); | |
| 27 int total_gdi_count = 0; | |
| 28 base::debug::Alias(&total_gdi_count); | |
| 29 int total_user_count = 0; | |
| 30 base::debug::Alias(&total_user_count); | |
| 31 | |
| 23 int child_count = 0; | 32 int child_count = 0; |
| 24 base::debug::Alias(&child_count); | 33 base::debug::Alias(&child_count); |
| 25 int peak_gdi_count = 0; | 34 int peak_gdi_count = 0; |
| 26 base::debug::Alias(&peak_gdi_count); | 35 base::debug::Alias(&peak_gdi_count); |
| 27 int sum_gdi_count = 0; | 36 int sum_gdi_count = 0; |
| 28 base::debug::Alias(&sum_gdi_count); | 37 base::debug::Alias(&sum_gdi_count); |
| 29 int sum_user_count = 0; | 38 int sum_user_count = 0; |
| 30 base::debug::Alias(&sum_user_count); | 39 base::debug::Alias(&sum_user_count); |
| 31 | 40 |
| 32 PROCESSENTRY32 proc_entry = {0}; | 41 PROCESSENTRY32 proc_entry = {0}; |
| 33 proc_entry.dwSize = sizeof(PROCESSENTRY32); | 42 proc_entry.dwSize = sizeof(PROCESSENTRY32); |
| 34 CHECK(Process32First(snapshot, &proc_entry)); | 43 CHECK(Process32First(snapshot, &proc_entry)); |
| 35 | 44 |
| 36 do { | 45 do { |
| 37 if (parent_pid != proc_entry.th32ParentProcessID) | |
| 38 continue; | |
| 39 // Got a child process. Compute GDI usage. | |
| 40 base::win::ScopedHandle process( | 46 base::win::ScopedHandle process( |
| 41 OpenProcess(PROCESS_QUERY_INFORMATION, | 47 OpenProcess(PROCESS_QUERY_INFORMATION, |
| 42 FALSE, | 48 FALSE, |
| 43 proc_entry.th32ProcessID)); | 49 proc_entry.th32ProcessID)); |
| 44 if (!process.IsValid()) | 50 if (!process.IsValid()) |
| 45 continue; | 51 continue; |
| 46 | 52 |
| 47 int num_gdi_handles = GetGuiResources(process.Get(), GR_GDIOBJECTS); | 53 int num_gdi_handles = GetGuiResources(process.Get(), GR_GDIOBJECTS); |
| 48 int num_user_handles = GetGuiResources(process.Get(), GR_USEROBJECTS); | 54 int num_user_handles = GetGuiResources(process.Get(), GR_USEROBJECTS); |
| 49 | 55 |
| 50 // Compute sum and peak counts. | 56 // Compute sum and peak counts for all processes. |
| 57 ++total_process_count; | |
| 58 total_user_count += num_user_handles; | |
| 59 total_gdi_count += num_gdi_handles; | |
| 60 if (total_peak_gdi_count < num_gdi_handles) | |
|
Lei Zhang
2016/11/03 18:53:11
total_peak_gdi_count = std::max(total_peak_gdi_cou
| |
| 61 total_peak_gdi_count = num_gdi_handles; | |
| 62 | |
| 63 if (parent_pid != proc_entry.th32ParentProcessID) | |
| 64 continue; | |
| 65 | |
| 66 // Compute sum and peak counts for child processes. | |
| 51 ++child_count; | 67 ++child_count; |
| 52 sum_user_count += num_user_handles; | 68 sum_user_count += num_user_handles; |
| 53 sum_gdi_count += num_gdi_handles; | 69 sum_gdi_count += num_gdi_handles; |
| 54 if (peak_gdi_count < num_gdi_handles) | 70 if (peak_gdi_count < num_gdi_handles) |
| 55 peak_gdi_count = num_gdi_handles; | 71 peak_gdi_count = num_gdi_handles; |
| 56 | 72 |
| 57 } while (Process32Next(snapshot, &proc_entry)); | 73 } while (Process32Next(snapshot, &proc_entry)); |
| 58 | 74 |
| 59 CloseHandle(snapshot); | 75 CloseHandle(snapshot); |
| 60 CHECK(false); | 76 CHECK(false); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 0, &small_data, shared_section, 0); | 133 0, &small_data, shared_section, 0); |
| 118 CHECK(small_bitmap != nullptr); | 134 CHECK(small_bitmap != nullptr); |
| 119 DeleteObject(small_bitmap); | 135 DeleteObject(small_bitmap); |
| 120 } | 136 } |
| 121 // Maybe the child processes are the ones leaking GDI or USER resouces. | 137 // Maybe the child processes are the ones leaking GDI or USER resouces. |
| 122 CollectChildGDIUsageAndDie(GetCurrentProcessId()); | 138 CollectChildGDIUsageAndDie(GetCurrentProcessId()); |
| 123 } | 139 } |
| 124 | 140 |
| 125 } // namespace debug | 141 } // namespace debug |
| 126 } // namespace base | 142 } // namespace base |
| OLD | NEW |