From f3df64fa3ddea9c0c6dbf852da5ce2f436c72cea Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Mon, 23 Mar 2015 14:48:06 -0700 Subject: [PATCH] Let Dart code running in Sky add events to the trace timeline This will let us form a wholistic picture of work done in the framework and in the engine. R=ojan@chromium.org, rafaelw@chromium.org Review URL: https://codereview.chromium.org/1028243003 --- engine/core/core.gni | 3 ++ engine/core/frame/LocalDOMWindow.cpp | 8 +++++ engine/core/frame/LocalDOMWindow.h | 5 ++- engine/core/frame/Tracing.cpp | 33 +++++++++++++++++++ engine/core/frame/Tracing.h | 30 +++++++++++++++++ engine/core/frame/Tracing.idl | 8 +++++ engine/core/frame/Window.idl | 2 ++ engine/core/script/dart_controller.cc | 2 ++ examples/stocks-fn/lib/stock_app.dart | 5 ++- examples/stocks-fn/lib/stock_data.dart | 19 +++++------ .../components/fixed_height_scrollable.dart | 33 ++++++++++--------- framework/debug/tracing.dart | 12 +++++++ framework/fn.dart | 9 +++-- 13 files changed, 140 insertions(+), 29 deletions(-) create mode 100644 engine/core/frame/Tracing.cpp create mode 100644 engine/core/frame/Tracing.h create mode 100644 engine/core/frame/Tracing.idl create mode 100644 framework/debug/tracing.dart diff --git a/engine/core/core.gni b/engine/core/core.gni index 58ed24012c6..3914d5b484a 100644 --- a/engine/core/core.gni +++ b/engine/core/core.gni @@ -703,6 +703,8 @@ sky_core_files = [ "frame/SettingsDelegate.h", "frame/SuspendableTimer.cpp", "frame/SuspendableTimer.h", + "frame/Tracing.cpp", + "frame/Tracing.h", "html/canvas/ANGLEInstancedArrays.cpp", "html/canvas/ANGLEInstancedArrays.h", "html/canvas/Canvas2DContextAttributes.cpp", @@ -1186,6 +1188,7 @@ core_idl_files = get_path_info([ "frame/ImageBitmap.idl", "frame/Location.idl", "frame/Screen.idl", + "frame/Tracing.idl", "frame/Window.idl", "html/canvas/ANGLEInstancedArrays.idl", "html/canvas/Canvas2DContextAttributes.idl", diff --git a/engine/core/frame/LocalDOMWindow.cpp b/engine/core/frame/LocalDOMWindow.cpp index 5d267952989..03429144776 100644 --- a/engine/core/frame/LocalDOMWindow.cpp +++ b/engine/core/frame/LocalDOMWindow.cpp @@ -57,6 +57,7 @@ #include "sky/engine/core/frame/Location.h" #include "sky/engine/core/frame/Screen.h" #include "sky/engine/core/frame/Settings.h" +#include "sky/engine/core/frame/Tracing.h" #include "sky/engine/core/inspector/ConsoleMessage.h" #include "sky/engine/core/loader/FrameLoaderClient.h" #include "sky/engine/core/page/ChromeClient.h" @@ -418,6 +419,13 @@ Location& LocalDOMWindow::location() const return *m_location; } +Tracing& LocalDOMWindow::tracing() const +{ + if (!m_tracing) + m_tracing = Tracing::create(); + return *m_tracing; +} + DOMSelection* LocalDOMWindow::getSelection() { return m_frame->document()->getSelection(); diff --git a/engine/core/frame/LocalDOMWindow.h b/engine/core/frame/LocalDOMWindow.h index 4a03070bbd9..f61ebdd3544 100644 --- a/engine/core/frame/LocalDOMWindow.h +++ b/engine/core/frame/LocalDOMWindow.h @@ -67,6 +67,7 @@ class ScheduledAction; class Screen; class ScriptCallStack; class StyleMedia; +class Tracing; enum PageshowEventPersistence { PageshowEventNotPersisted = 0, @@ -124,6 +125,8 @@ public: int screenLeft() const { return screenX(); } int screenTop() const { return screenY(); } + Tracing& tracing() const; + // FIXME(sky): keeping self for now since js-test.html uses it. LocalDOMWindow* window() const; @@ -232,7 +235,7 @@ private: mutable RefPtr m_screen; mutable RefPtr m_location; - + mutable RefPtr m_tracing; mutable RefPtr m_css; RefPtr m_eventQueue; diff --git a/engine/core/frame/Tracing.cpp b/engine/core/frame/Tracing.cpp new file mode 100644 index 00000000000..3359da2abb7 --- /dev/null +++ b/engine/core/frame/Tracing.cpp @@ -0,0 +1,33 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sky/engine/config.h" +#include "sky/engine/core/frame/Tracing.h" + +#include "base/trace_event/trace_event.h" +#include "sky/engine/wtf/text/StringUTF8Adaptor.h" + +namespace blink { + +Tracing::Tracing() +{ +} + +Tracing::~Tracing() +{ +} + +void Tracing::begin(const String& name) +{ + StringUTF8Adaptor utf8(name); + TRACE_EVENT_COPY_BEGIN0("script", utf8.data()); +} + +void Tracing::end(const String& name) +{ + StringUTF8Adaptor utf8(name); + TRACE_EVENT_COPY_END0("script", utf8.data()); +} + +} // namespace blink diff --git a/engine/core/frame/Tracing.h b/engine/core/frame/Tracing.h new file mode 100644 index 00000000000..99da6718f10 --- /dev/null +++ b/engine/core/frame/Tracing.h @@ -0,0 +1,30 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SKY_ENGINE_CORE_FRAME_TRACING_H_ +#define SKY_ENGINE_CORE_FRAME_TRACING_H_ + +#include "sky/engine/tonic/dart_wrappable.h" +#include "sky/engine/wtf/PassRefPtr.h" +#include "sky/engine/wtf/RefCounted.h" +#include "sky/engine/wtf/text/WTFString.h" + +namespace blink { + +class Tracing : public RefCounted, public DartWrappable { + DEFINE_WRAPPERTYPEINFO(); +public: + ~Tracing() override; + static PassRefPtr create() { return adoptRef(new Tracing); } + + void begin(const String& name); + void end(const String& name); + +private: + Tracing(); +}; + +} // namespace blink + +#endif // SKY_ENGINE_CORE_FRAME_TRACING_H_ diff --git a/engine/core/frame/Tracing.idl b/engine/core/frame/Tracing.idl new file mode 100644 index 00000000000..7f3e407c067 --- /dev/null +++ b/engine/core/frame/Tracing.idl @@ -0,0 +1,8 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +interface Tracing { + void begin(DOMString name); + void end(DOMString name); +}; diff --git a/engine/core/frame/Window.idl b/engine/core/frame/Window.idl index 5e01b1e364c..1d966dbf73d 100644 --- a/engine/core/frame/Window.idl +++ b/engine/core/frame/Window.idl @@ -72,6 +72,8 @@ long requestAnimationFrame(RequestAnimationFrameCallback callback); void cancelAnimationFrame(long id); + readonly attribute Tracing tracing; + // [Replaceable] readonly attribute CSS CSS; }; diff --git a/engine/core/script/dart_controller.cc b/engine/core/script/dart_controller.cc index 270ed4e0eda..3fa1fbb45a7 100644 --- a/engine/core/script/dart_controller.cc +++ b/engine/core/script/dart_controller.cc @@ -212,6 +212,8 @@ static Dart_Isolate IsolateCreateCallback(const char* script_uri, } static void CallHandleMessage(base::WeakPtr dart_state) { + TRACE_EVENT0("sky", "CallHandleMessage"); + if (!dart_state) return; diff --git a/examples/stocks-fn/lib/stock_app.dart b/examples/stocks-fn/lib/stock_app.dart index 0331b5233ad..50085d63a87 100644 --- a/examples/stocks-fn/lib/stock_app.dart +++ b/examples/stocks-fn/lib/stock_app.dart @@ -12,6 +12,7 @@ import 'package:sky/framework/components/menu_divider.dart'; import 'package:sky/framework/components/menu_item.dart'; import 'package:sky/framework/components/popup_menu.dart'; import 'package:sky/framework/components/scaffold.dart'; +import 'package:sky/framework/debug/tracing.dart'; import 'package:sky/framework/fn.dart'; import 'package:sky/framework/theme/typography.dart' as typography; import 'stock_data.dart'; @@ -42,7 +43,9 @@ class StocksApp extends App { fetchStockOracle().then((oracle) { setState(() { _sortedStocks = oracle.stocks; - _sortedStocks.sort((a, b) => a.symbol.compareTo(b.symbol)); + trace('StocksApp::sortStocks', () { + _sortedStocks.sort((a, b) => a.symbol.compareTo(b.symbol)); + }); }); }); } diff --git a/examples/stocks-fn/lib/stock_data.dart b/examples/stocks-fn/lib/stock_data.dart index 17e083c1cc3..5381ab5af81 100644 --- a/examples/stocks-fn/lib/stock_data.dart +++ b/examples/stocks-fn/lib/stock_data.dart @@ -4,17 +4,13 @@ import 'dart:convert'; import 'dart:math'; +import 'package:sky/framework/debug/tracing.dart'; import 'package:sky/framework/net/fetch.dart'; // Snapshot from http://www.nasdaq.com/screening/company-list.aspx // Fetched 2/23/2014. // "Symbol","Name","LastSale","MarketCap","IPOyear","Sector","industry","Summary Quote", -// final List> _kCompanyList = [ -// ["TFSC","1347 Capital Corp.","9.43","\$56.09M","2014","Finance","Business Services","http://www.nasdaq.com/symbol/tfsc"], -// ["TFSCR","1347 Capital Corp.","0.37","n/a","2014","Finance","Business Services","http://www.nasdaq.com/symbol/tfscr"], -// ["TFSCU","1347 Capital Corp.","9.97","\$41.67M","2014","n/a","n/a","http://www.nasdaq.com/symbol/tfscu"], -// ["TFSCW","1347 Capital Corp.","0.2","n/a","2014","Finance","Business Services","http://www.nasdaq.com/symbol/tfscw"], -// ]; +// Data in stock_data.json class Stock { String symbol; @@ -60,8 +56,11 @@ class StockOracle { Future fetchStockOracle() async { Response response = await fetch('lib/stock_data.json'); - String json = response.bodyAsString(); - JsonDecoder decoder = new JsonDecoder(); - var companyList = decoder.convert(json); - return new StockOracle.fromCompanyList(companyList); + + return trace('stocks::fetchStockOracle', () { + String json = response.bodyAsString(); + JsonDecoder decoder = new JsonDecoder(); + var companyList = decoder.convert(json); + return new StockOracle.fromCompanyList(companyList); + }); } diff --git a/framework/components/fixed_height_scrollable.dart b/framework/components/fixed_height_scrollable.dart index 7343b553037..e6765038c3c 100644 --- a/framework/components/fixed_height_scrollable.dart +++ b/framework/components/fixed_height_scrollable.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import '../animation/scroll_behavior.dart'; +import '../debug/tracing.dart'; import '../fn.dart'; import 'dart:math' as math; import 'dart:sky' as sky; @@ -30,22 +31,24 @@ abstract class FixedHeightScrollable extends Scrollable { }) : super(key: key, scrollBehavior: scrollBehavior); void _measureHeights() { - if (_itemHeight != null) - return; - var root = getRoot(); - if (root == null) - return; - var item = root.firstChild.firstChild; - if (item == null) - return; - sky.ClientRect scrollRect = root.getBoundingClientRect(); - sky.ClientRect itemRect = item.getBoundingClientRect(); - assert(scrollRect.height > 0); - assert(itemRect.height > 0); + trace('FixedHeightScrollable::_measureHeights', () { + if (_itemHeight != null) + return; + var root = getRoot(); + if (root == null) + return; + var item = root.firstChild.firstChild; + if (item == null) + return; + sky.ClientRect scrollRect = root.getBoundingClientRect(); + sky.ClientRect itemRect = item.getBoundingClientRect(); + assert(scrollRect.height > 0); + assert(itemRect.height > 0); - setState(() { - _height = scrollRect.height; - _itemHeight = itemRect.height; + setState(() { + _height = scrollRect.height; + _itemHeight = itemRect.height; + }); }); } diff --git a/framework/debug/tracing.dart b/framework/debug/tracing.dart new file mode 100644 index 00000000000..557bd281884 --- /dev/null +++ b/framework/debug/tracing.dart @@ -0,0 +1,12 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:sky' as sky; + +dynamic trace(String name, Function f) { + sky.window.tracing.begin(name); + var result = f(); + sky.window.tracing.end(name); + return result; +} diff --git a/framework/fn.dart b/framework/fn.dart index 505c1a447a8..e669d7e92e7 100644 --- a/framework/fn.dart +++ b/framework/fn.dart @@ -9,6 +9,8 @@ import 'dart:collection'; import 'dart:sky' as sky; import 'reflect.dart' as reflect; +final sky.Tracing _tracing = sky.window.tracing; + bool _initIsInCheckedMode() { String testFn(i) { double d = i; return d.toString(); } try { @@ -713,8 +715,9 @@ List _dirtyComponents = new List(); bool _buildScheduled = false; bool _inRenderDirtyComponents = false; - void _buildDirtyComponents() { + _tracing.begin('fn::_buildDirtyComponents'); + Stopwatch sw; if (_shouldLogRenderDuration) sw = new Stopwatch()..start(); @@ -737,8 +740,10 @@ void _buildDirtyComponents() { if (_shouldLogRenderDuration) { sw.stop(); - print("Render took ${sw.elapsedMicroseconds} microseconds"); + print('Render took ${sw.elapsedMicroseconds} microseconds'); } + + _tracing.end('fn::_buildDirtyComponents'); } void _scheduleComponentForRender(Component c) {