Merge pull request #5413 from electron/process-resource-usage

Fetch Process resource usage from WebContents
This commit is contained in:
Cheng Zhao 2016-05-14 23:28:51 +09:00
commit 5ec2e8d7e5
6 changed files with 114 additions and 8 deletions

View file

@ -12,6 +12,7 @@
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "content/public/browser/native_web_keyboard_event.h" #include "content/public/browser/native_web_keyboard_event.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
#include "third_party/WebKit/public/web/WebCache.h"
#include "third_party/WebKit/public/web/WebDeviceEmulationParams.h" #include "third_party/WebKit/public/web/WebDeviceEmulationParams.h"
#include "third_party/WebKit/public/web/WebFindOptions.h" #include "third_party/WebKit/public/web/WebFindOptions.h"
#include "third_party/WebKit/public/web/WebInputEvent.h" #include "third_party/WebKit/public/web/WebInputEvent.h"
@ -384,4 +385,30 @@ v8::Local<v8::Value> MediaFlagsToV8(v8::Isolate* isolate, int mediaFlags) {
return mate::ConvertToV8(isolate, dict); return mate::ConvertToV8(isolate, dict);
} }
v8::Local<v8::Value> Converter<blink::WebCache::ResourceTypeStat>::ToV8(
v8::Isolate* isolate,
const blink::WebCache::ResourceTypeStat& stat) {
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
dict.Set("count", static_cast<uint32_t>(stat.count));
dict.Set("size", static_cast<double>(stat.size));
dict.Set("liveSize", static_cast<double>(stat.liveSize));
dict.Set("decodedSize", static_cast<double>(stat.decodedSize));
dict.Set("purgedSize", static_cast<double>(stat.purgedSize));
dict.Set("purgeableSize", static_cast<double>(stat.purgeableSize));
return dict.GetHandle();
}
v8::Local<v8::Value> Converter<blink::WebCache::ResourceTypeStats>::ToV8(
v8::Isolate* isolate,
const blink::WebCache::ResourceTypeStats& stats) {
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
dict.Set("images", stats.images);
dict.Set("scripts", stats.scripts);
dict.Set("cssStyleSheets", stats.cssStyleSheets);
dict.Set("xslStyleSheets", stats.xslStyleSheets);
dict.Set("fonts", stats.fonts);
dict.Set("other", stats.other);
return dict.GetHandle();
}
} // namespace mate } // namespace mate

View file

@ -6,6 +6,7 @@
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_BLINK_CONVERTER_H_ #define ATOM_COMMON_NATIVE_MATE_CONVERTERS_BLINK_CONVERTER_H_
#include "native_mate/converter.h" #include "native_mate/converter.h"
#include "third_party/WebKit/public/web/WebCache.h"
#include "third_party/WebKit/public/web/WebContextMenuData.h" #include "third_party/WebKit/public/web/WebContextMenuData.h"
namespace blink { namespace blink {
@ -100,8 +101,21 @@ struct Converter<blink::WebContextMenuData::InputFieldType> {
const blink::WebContextMenuData::InputFieldType& in); const blink::WebContextMenuData::InputFieldType& in);
}; };
v8::Local<v8::Value> EditFlagsToV8(v8::Isolate* isolate, int editFlags); template<>
struct Converter<blink::WebCache::ResourceTypeStat> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate,
const blink::WebCache::ResourceTypeStat& stat);
};
template<>
struct Converter<blink::WebCache::ResourceTypeStats> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate,
const blink::WebCache::ResourceTypeStats& stats);
};
v8::Local<v8::Value> EditFlagsToV8(v8::Isolate* isolate, int editFlags);
v8::Local<v8::Value> MediaFlagsToV8(v8::Isolate* isolate, int mediaFlags); v8::Local<v8::Value> MediaFlagsToV8(v8::Isolate* isolate, int mediaFlags);
} // namespace mate } // namespace mate

View file

@ -5,14 +5,17 @@
#include "atom/renderer/api/atom_api_web_frame.h" #include "atom/renderer/api/atom_api_web_frame.h"
#include "atom/common/api/event_emitter_caller.h" #include "atom/common/api/event_emitter_caller.h"
#include "atom/common/native_mate_converters/blink_converter.h"
#include "atom/common/native_mate_converters/callback.h" #include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/gfx_converter.h" #include "atom/common/native_mate_converters/gfx_converter.h"
#include "atom/common/native_mate_converters/string16_converter.h" #include "atom/common/native_mate_converters/string16_converter.h"
#include "atom/renderer/api/atom_api_spell_check_client.h" #include "atom/renderer/api/atom_api_spell_check_client.h"
#include "base/memory/memory_pressure_listener.h"
#include "content/public/renderer/render_frame.h" #include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_view.h" #include "content/public/renderer/render_view.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h" #include "native_mate/object_template_builder.h"
#include "third_party/WebKit/public/web/WebCache.h"
#include "third_party/WebKit/public/web/WebDocument.h" #include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h" #include "third_party/WebKit/public/web/WebLocalFrame.h"
#include "third_party/WebKit/public/web/WebScriptExecutionCallback.h" #include "third_party/WebKit/public/web/WebScriptExecutionCallback.h"
@ -168,6 +171,20 @@ mate::Handle<WebFrame> WebFrame::Create(v8::Isolate* isolate) {
return mate::CreateHandle(isolate, new WebFrame(isolate)); return mate::CreateHandle(isolate, new WebFrame(isolate));
} }
blink::WebCache::ResourceTypeStats WebFrame::GetResourceUsage(
v8::Isolate* isolate) {
blink::WebCache::ResourceTypeStats stats;
blink::WebCache::getResourceTypeStats(&stats);
return stats;
}
void WebFrame::ClearCache(v8::Isolate* isolate) {
isolate->IdleNotificationDeadline(0.5);
blink::WebCache::clear();
base::MemoryPressureListener::NotifyMemoryPressure(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
}
// static // static
void WebFrame::BuildPrototype( void WebFrame::BuildPrototype(
v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> prototype) { v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> prototype) {
@ -191,7 +208,9 @@ void WebFrame::BuildPrototype(
.SetMethod("registerURLSchemeAsPrivileged", .SetMethod("registerURLSchemeAsPrivileged",
&WebFrame::RegisterURLSchemeAsPrivileged) &WebFrame::RegisterURLSchemeAsPrivileged)
.SetMethod("insertText", &WebFrame::InsertText) .SetMethod("insertText", &WebFrame::InsertText)
.SetMethod("executeJavaScript", &WebFrame::ExecuteJavaScript); .SetMethod("executeJavaScript", &WebFrame::ExecuteJavaScript)
.SetMethod("getResourceUsage", &WebFrame::GetResourceUsage)
.SetMethod("clearCache", &WebFrame::ClearCache);
} }
} // namespace api } // namespace api

View file

@ -11,6 +11,7 @@
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "native_mate/handle.h" #include "native_mate/handle.h"
#include "native_mate/wrappable.h" #include "native_mate/wrappable.h"
#include "third_party/WebKit/public/web/WebCache.h"
namespace blink { namespace blink {
class WebLocalFrame; class WebLocalFrame;
@ -69,6 +70,10 @@ class WebFrame : public mate::Wrappable<WebFrame> {
// Excecuting scripts. // Excecuting scripts.
void ExecuteJavaScript(const base::string16& code, mate::Arguments* args); void ExecuteJavaScript(const base::string16& code, mate::Arguments* args);
// Resource related methods
blink::WebCache::ResourceTypeStats GetResourceUsage(v8::Isolate* isolate);
void ClearCache(v8::Isolate* isolate);
scoped_ptr<SpellCheckClient> spell_check_client_; scoped_ptr<SpellCheckClient> spell_check_client_;
blink::WebLocalFrame* web_frame_; blink::WebLocalFrame* web_frame_;

View file

@ -106,4 +106,43 @@ In the browser window some HTML APIs like `requestFullScreen` can only be
invoked by a gesture from the user. Setting `userGesture` to `true` will remove invoked by a gesture from the user. Setting `userGesture` to `true` will remove
this limitation. this limitation.
### `webFrame.getResourceUsage()`
Returns an object describing usage information of Blink's internal memory
caches.
```javascript
console.log(webFrame.getResourceUsage())
```
This will generate:
```javascript
{
images: {
count: 22,
size: 2549,
liveSize: 2542,
decodedSize: 478,
purgedSize: 0,
purgeableSize: 0
},
cssStyleSheets: { /* same with "images" */ },
xslStyleSheets: { /* same with "images" */ },
fonts: { /* same with "images" */ },
other: { /* same with "images" */ },
}
```
### `webFrame.clearCache()`
Attempts to free memory that is no longer being used (like images from a
previous navigation).
Note that blindly calling this method probably makes Electron slower since it
will have to refill these emptied caches, you should only call it if an event
in your app has occured that makes you think your page is actually using less
memory (i.e. you have navigated from a super heavy page to a mostly empty one,
and intend to stay there).
[spellchecker]: https://github.com/atom/node-spellchecker [spellchecker]: https://github.com/atom/node-spellchecker

View file

@ -52,9 +52,6 @@ def main():
if PLATFORM != 'win32': if PLATFORM != 'win32':
# Download prebuilt clang binaries. # Download prebuilt clang binaries.
update_clang() update_clang()
if not args.disable_clang and args.clang_dir == '':
# Build with prebuilt clang.
set_clang_env(os.environ)
setup_python_libs() setup_python_libs()
update_node_modules('.') update_node_modules('.')
@ -67,7 +64,7 @@ def main():
create_chrome_version_h() create_chrome_version_h()
touch_config_gypi() touch_config_gypi()
run_update(defines) run_update(defines, args.disable_clang, args.clang_dir)
update_electron_modules('spec', args.target_arch) update_electron_modules('spec', args.target_arch)
@ -250,9 +247,14 @@ def touch_config_gypi():
f.write(content) f.write(content)
def run_update(defines): def run_update(defines, disable_clang, clang_dir):
env = os.environ.copy()
if not disable_clang and clang_dir == '':
# Build with prebuilt clang.
set_clang_env(env)
update = os.path.join(SOURCE_ROOT, 'script', 'update.py') update = os.path.join(SOURCE_ROOT, 'script', 'update.py')
execute_stdout([sys.executable, update, '--defines', defines]) execute_stdout([sys.executable, update, '--defines', defines], env)
if __name__ == '__main__': if __name__ == '__main__':