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 <TlHelp32.h> | 9 #include <TlHelp32.h> |
10 | 10 |
11 #include "base/debug/alias.h" | 11 #include "base/debug/alias.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/win/scoped_handle.h" | 13 #include "base/win/scoped_handle.h" |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 void CollectChildGDIUsageAndDie(DWORD parent_pid) { | 17 void CollectChildGDIUsageAndDie(DWORD parent_pid) { |
18 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) ; | 18 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
19 if(snapshot == INVALID_HANDLE_VALUE) | 19 CHECK_NE(INVALID_HANDLE_VALUE, snapshot); |
20 CHECK(false); | |
21 | 20 |
22 int child_count = 0; | 21 int child_count = 0; |
23 base::debug::Alias(&child_count); | 22 base::debug::Alias(&child_count); |
24 int peak_gdi_count = 0; | 23 int peak_gdi_count = 0; |
25 base::debug::Alias(&peak_gdi_count); | 24 base::debug::Alias(&peak_gdi_count); |
26 int sum_gdi_count = 0; | 25 int sum_gdi_count = 0; |
27 base::debug::Alias(&sum_gdi_count); | 26 base::debug::Alias(&sum_gdi_count); |
28 int sum_user_count = 0; | 27 int sum_user_count = 0; |
29 base::debug::Alias(&sum_user_count); | 28 base::debug::Alias(&sum_user_count); |
30 | 29 |
31 PROCESSENTRY32 proc_entry = {0}; | 30 PROCESSENTRY32 proc_entry = {0}; |
32 proc_entry.dwSize = sizeof(PROCESSENTRY32) ; | 31 proc_entry.dwSize = sizeof(PROCESSENTRY32); |
33 if(!Process32First(snapshot, &proc_entry)) | 32 CHECK(Process32First(snapshot, &proc_entry)); |
34 CHECK(false); | |
35 | 33 |
36 do { | 34 do { |
37 if (parent_pid != proc_entry.th32ParentProcessID) | 35 if (parent_pid != proc_entry.th32ParentProcessID) |
38 continue; | 36 continue; |
39 // Got a child process. Compute GDI usage. | 37 // Got a child process. Compute GDI usage. |
40 base::win::ScopedHandle process( | 38 base::win::ScopedHandle process( |
41 ::OpenProcess(PROCESS_QUERY_INFORMATION, | 39 ::OpenProcess(PROCESS_QUERY_INFORMATION, |
42 FALSE, | 40 FALSE, |
43 proc_entry.th32ParentProcessID)); | 41 proc_entry.th32ParentProcessID)); |
44 if (!process) | 42 if (!process) |
45 continue; | 43 continue; |
46 | 44 |
47 int num_gdi_handles = ::GetGuiResources(process.Get(), GR_GDIOBJECTS); | 45 int num_gdi_handles = ::GetGuiResources(process.Get(), GR_GDIOBJECTS); |
48 int num_user_handles = ::GetGuiResources(process.Get(), GR_USEROBJECTS); | 46 int num_user_handles = ::GetGuiResources(process.Get(), GR_USEROBJECTS); |
49 | 47 |
50 // Compute sum and peak counts. | 48 // Compute sum and peak counts. |
51 ++child_count; | 49 ++child_count; |
52 sum_user_count += num_user_handles; | 50 sum_user_count += num_user_handles; |
53 sum_gdi_count += num_gdi_handles; | 51 sum_gdi_count += num_gdi_handles; |
54 if (peak_gdi_count < num_gdi_handles) | 52 if (peak_gdi_count < num_gdi_handles) |
55 peak_gdi_count = num_gdi_handles; | 53 peak_gdi_count = num_gdi_handles; |
56 | 54 |
57 } while(Process32Next(snapshot, &proc_entry)); | 55 } while (Process32Next(snapshot, &proc_entry)); |
58 | 56 |
cpu_(ooo_6.6-7.5)
2014/09/26 01:17:50
there is an inconsistency in this file wrt windows
Peter Kasting
2014/09/29 19:29:45
Done.
| |
59 ::CloseHandle(snapshot) ; | 57 ::CloseHandle(snapshot); |
60 CHECK(false); | 58 CHECK(false); |
cpu_(ooo_6.6-7.5)
2014/09/26 01:17:50
I have no idea why we are closing the snapshot han
Peter Kasting
2014/09/26 01:21:33
It wasn't me :)
cpu_(ooo_6.6-7.5)
2014/09/29 19:01:48
Its best to not do cleanup if we are intent in gen
Peter Kasting
2014/09/29 19:29:45
OK. I think I'll leave the code as-is rather than
| |
61 } | 59 } |
62 | 60 |
63 } // namespace | 61 } // namespace |
64 | 62 |
65 namespace base { | 63 namespace base { |
66 namespace debug { | 64 namespace debug { |
67 | 65 |
68 void GDIBitmapAllocFailure(BITMAPINFOHEADER* header, HANDLE shared_section) { | 66 void GDIBitmapAllocFailure(BITMAPINFOHEADER* header, HANDLE shared_section) { |
69 // Make sure parameters are saved in the minidump. | 67 // Make sure parameters are saved in the minidump. |
70 DWORD last_error = ::GetLastError(); | 68 DWORD last_error = ::GetLastError(); |
(...skipping 14 matching lines...) Expand all Loading... | |
85 if (num_gdi_handles == 0) { | 83 if (num_gdi_handles == 0) { |
86 DWORD get_gui_resources_error = GetLastError(); | 84 DWORD get_gui_resources_error = GetLastError(); |
87 base::debug::Alias(&get_gui_resources_error); | 85 base::debug::Alias(&get_gui_resources_error); |
88 CHECK(false); | 86 CHECK(false); |
89 } | 87 } |
90 | 88 |
91 base::debug::Alias(&num_gdi_handles); | 89 base::debug::Alias(&num_gdi_handles); |
92 base::debug::Alias(&num_user_handles); | 90 base::debug::Alias(&num_user_handles); |
93 | 91 |
94 const DWORD kLotsOfHandles = 9990; | 92 const DWORD kLotsOfHandles = 9990; |
95 if (num_gdi_handles > kLotsOfHandles) | 93 CHECK_LE(num_gdi_handles, kLotsOfHandles); |
96 CHECK(false); | |
97 | 94 |
98 PROCESS_MEMORY_COUNTERS_EX pmc; | 95 PROCESS_MEMORY_COUNTERS_EX pmc; |
99 pmc.cb = sizeof(pmc); | 96 pmc.cb = sizeof(pmc); |
100 if (!GetProcessMemoryInfo(GetCurrentProcess(), | 97 CHECK(GetProcessMemoryInfo(GetCurrentProcess(), |
101 reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc), | 98 reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc), |
102 sizeof(pmc))) { | 99 sizeof(pmc))); |
103 CHECK(false); | |
104 } | |
105 const size_t kLotsOfMemory = 1500 * 1024 * 1024; // 1.5GB | 100 const size_t kLotsOfMemory = 1500 * 1024 * 1024; // 1.5GB |
106 if (pmc.PagefileUsage > kLotsOfMemory) | 101 CHECK_LE(pmc.PagefileUsage, kLotsOfMemory); |
107 CHECK(false); | 102 CHECK_LE(pmc.PrivateUsage, kLotsOfMemory); |
108 if (pmc.PrivateUsage > kLotsOfMemory) | |
109 CHECK(false); | |
110 | 103 |
111 void* small_data = NULL; | 104 void* small_data = NULL; |
112 base::debug::Alias(&small_data); | 105 base::debug::Alias(&small_data); |
113 | 106 |
114 if (std::abs(heigth) * width > 100) { | 107 if (std::abs(heigth) * width > 100) { |
115 // Huh, that's weird. We don't have crazy handle count, we don't have | 108 // Huh, that's weird. We don't have crazy handle count, we don't have |
116 // ridiculous memory usage. Try to allocate a small bitmap and see if that | 109 // ridiculous memory usage. Try to allocate a small bitmap and see if that |
117 // fails too. | 110 // fails too. |
118 header->biWidth = 5; | 111 header->biWidth = 5; |
119 header->biHeight = -5; | 112 header->biHeight = -5; |
120 HBITMAP small_bitmap = CreateDIBSection( | 113 HBITMAP small_bitmap = CreateDIBSection( |
121 NULL, reinterpret_cast<BITMAPINFO*>(&header), | 114 NULL, reinterpret_cast<BITMAPINFO*>(&header), |
122 0, &small_data, shared_section, 0); | 115 0, &small_data, shared_section, 0); |
116 CHECK(small_bitmap != NULL); | |
117 DeleteObject(small_bitmap); | |
123 } | 118 } |
124 // Maybe the child processes are the ones leaking GDI or USER resouces. | 119 // Maybe the child processes are the ones leaking GDI or USER resouces. |
125 CollectChildGDIUsageAndDie(::GetCurrentProcessId()); | 120 CollectChildGDIUsageAndDie(::GetCurrentProcessId()); |
126 } | 121 } |
127 | 122 |
128 } // namespace debug | 123 } // namespace debug |
129 } // namespace base | 124 } // namespace base |
OLD | NEW |