Skip to content

Commit

Permalink
Reland ""Add support for trace counters with variable arguments and i…
Browse files Browse the repository at this point in the history
…nstrument the raster cache." (flutter#8145)

This reverts commit bc90132 and fixes the
discovered on Windows builds.
  • Loading branch information
chinmaygarde authored Mar 13, 2019
1 parent 3c93817 commit 906d684
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 21 deletions.
35 changes: 33 additions & 2 deletions flow/raster_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ static RasterCacheResult Rasterize(
bool checkerboard,
const SkRect& logical_rect,
std::function<void(SkCanvas*)> draw_function) {
TRACE_EVENT0("flutter", "RasterCachePopulate");
SkIRect cache_rect = RasterCache::GetDeviceBounds(logical_rect, ctm);

const SkImageInfo image_info = SkImageInfo::MakeN32Premul(
Expand Down Expand Up @@ -129,8 +130,6 @@ RasterCacheResult RasterizePicture(SkPicture* picture,
const SkMatrix& ctm,
SkColorSpace* dst_color_space,
bool checkerboard) {
TRACE_EVENT0("flutter", "RasterCachePopulate");

return Rasterize(context, ctm, dst_color_space, checkerboard,
picture->cullRect(),
[=](SkCanvas* canvas) { canvas->drawPicture(picture); });
Expand Down Expand Up @@ -240,6 +239,7 @@ void RasterCache::SweepAfterFrame() {
SweepOneCacheAfterFrame<PictureCache, PictureCache::iterator>(picture_cache_);
SweepOneCacheAfterFrame<LayerCache, LayerCache::iterator>(layer_cache_);
picture_cached_this_frame_ = 0;
TraceStatsToTimeline();
}

void RasterCache::Clear() {
Expand All @@ -259,4 +259,35 @@ void RasterCache::SetCheckboardCacheImages(bool checkerboard) {
Clear();
}

void RasterCache::TraceStatsToTimeline() const {
#if FLUTTER_RUNTIME_MODE != FLUTTER_RUNTIME_MODE_RELEASE

size_t layer_cache_count = 0;
size_t layer_cache_bytes = 0;
size_t picture_cache_count = 0;
size_t picture_cache_bytes = 0;

for (const auto& item : layer_cache_) {
const auto dimensions = item.second.image.image_dimensions();
layer_cache_count++;
layer_cache_bytes += dimensions.width() * dimensions.height() * 4;
}

for (const auto& item : picture_cache_) {
const auto dimensions = item.second.image.image_dimensions();
picture_cache_count++;
picture_cache_bytes += dimensions.width() * dimensions.height() * 4;
}

FML_TRACE_COUNTER("flutter", "RasterCache",
reinterpret_cast<int64_t>(this), //
"LayerCount", layer_cache_count, //
"LayerMBytes", layer_cache_bytes * 1e-6, //
"PictureCount", picture_cache_count, //
"PictureMBytes", picture_cache_bytes * 1e-6 //
);

#endif // FLUTTER_RUNTIME_MODE != FLUTTER_RUNTIME_MODE_RELEASE
}

} // namespace flow
7 changes: 7 additions & 0 deletions flow/raster_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class RasterCacheResult {

void draw(SkCanvas& canvas, const SkPaint* paint = nullptr) const;

SkISize image_dimensions() const {
return image_ ? image_->dimensions() : SkISize::Make(0, 0);
};

private:
sk_sp<SkImage> image_;
SkRect logical_rect_;
Expand Down Expand Up @@ -87,6 +91,7 @@ class RasterCache {
void Prepare(PrerollContext* context, Layer* layer, const SkMatrix& ctm);

RasterCacheResult Get(const SkPicture& picture, const SkMatrix& ctm) const;

RasterCacheResult Get(Layer* layer, const SkMatrix& ctm) const;

void SweepAfterFrame();
Expand Down Expand Up @@ -127,6 +132,8 @@ class RasterCache {
bool checkerboard_images_;
fml::WeakPtrFactory<RasterCache> weak_factory_;

void TraceStatsToTimeline() const;

FML_DISALLOW_COPY_AND_ASSIGN(RasterCache);
};

Expand Down
38 changes: 26 additions & 12 deletions fml/trace_event.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,36 @@

#include "flutter/fml/trace_event.h"

#include "third_party/dart/runtime/include/dart_tools_api.h"
#include <algorithm>

#include "flutter/fml/logging.h"

namespace fml {
namespace tracing {

void TraceCounter(TraceArg category_group, TraceArg name, TraceIDArg count) {
auto count_string = std::to_string(count);
const char* arg_names[] = {name};
const char* arg_values[] = {count_string.c_str()};
Dart_TimelineEvent(name, // label
Dart_TimelineGetMicros(), // timestamp0
0, // timestamp1_or_async_id
Dart_Timeline_Event_Counter, // event type
1, // argument_count
arg_names, // argument_names
arg_values // argument_values
void TraceTimelineEvent(TraceArg category_group,
TraceArg name,
TraceIDArg identifier,
Dart_Timeline_Event_Type type,
const std::vector<const char*>& c_names,
const std::vector<std::string>& values) {
const auto argument_count = std::min(c_names.size(), values.size());

std::vector<const char*> c_values;
c_values.resize(argument_count, nullptr);

for (size_t i = 0; i < argument_count; i++) {
c_values[i] = values[i].c_str();
}

Dart_TimelineEvent(
name, // label
Dart_TimelineGetMicros(), // timestamp0
identifier, // timestamp1_or_async_id
type, // event type
argument_count, // argument_count
const_cast<const char**>(c_names.data()), // argument_names
c_values.data() // argument_values
);
}

Expand Down
68 changes: 63 additions & 5 deletions fml/trace_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

#include <trace/event.h>

#define FML_TRACE_COUNTER(category_group, name, count) \
TRACE_COUNTER(category_group, name, 0u, name, count)
#define TRACE_EVENT0(a, b) TRACE_DURATION(a, b)
#define TRACE_EVENT1(a, b, c, d) TRACE_DURATION(a, b, c, d)
#define TRACE_EVENT2(a, b, c, d, e, f) TRACE_DURATION(a, b, c, d, e, f)
Expand All @@ -28,8 +26,11 @@
#include <cstddef>
#include <cstdint>
#include <string>
#include <type_traits>
#include <vector>

#include "flutter/fml/macros.h"
#include "third_party/dart/runtime/include/dart_tools_api.h"

#if !defined(OS_FUCHSIA)

Expand All @@ -45,8 +46,9 @@
// from trace/event.h on Fuchsia.
//
// TODO(chinmaygarde): All macros here should have the FML prefix.
#define FML_TRACE_COUNTER(category_group, name, count) \
::fml::tracing::TraceCounter(category_group, name, count);
#define FML_TRACE_COUNTER(category_group, name, counter_id, arg1, ...) \
::fml::tracing::TraceCounter((category_group), (name), (counter_id), (arg1), \
__VA_ARGS__);

#define TRACE_EVENT0(category_group, name) \
::fml::tracing::TraceEvent0(category_group, name); \
Expand Down Expand Up @@ -99,7 +101,63 @@ namespace tracing {
using TraceArg = const char*;
using TraceIDArg = int64_t;

void TraceCounter(TraceArg category_group, TraceArg name, TraceIDArg count);
void TraceTimelineEvent(TraceArg category_group,
TraceArg name,
TraceIDArg id,
Dart_Timeline_Event_Type type,
const std::vector<const char*>& names,
const std::vector<std::string>& values);

inline std::string TraceToString(const char* string) {
return std::string{string};
}

inline std::string TraceToString(std::string string) {
return string;
}

template <typename T, typename = std::enable_if_t<std::is_arithmetic<T>::value>>
std::string TraceToString(T string) {
return std::to_string(string);
}

inline void SplitArgumentsCollect(std::vector<const char*>& keys,
std::vector<std::string>& values) {}

template <typename Key, typename Value, typename... Args>
void SplitArgumentsCollect(std::vector<const char*>& keys,
std::vector<std::string>& values,
Key key,
Value value,
Args... args) {
keys.emplace_back(key);
values.emplace_back(TraceToString(value));
SplitArgumentsCollect(keys, values, args...);
}

inline std::pair<std::vector<const char*>, std::vector<std::string>>
SplitArguments() {
return {};
}

template <typename Key, typename Value, typename... Args>
std::pair<std::vector<const char*>, std::vector<std::string>>
SplitArguments(Key key, Value value, Args... args) {
std::vector<const char*> keys;
std::vector<std::string> values;
SplitArgumentsCollect(keys, values, key, value, args...);
return std::make_pair(std::move(keys), std::move(values));
}

template <typename... Args>
void TraceCounter(TraceArg category,
TraceArg name,
TraceIDArg identifier,
Args... args) {
auto split = SplitArguments(args...);
TraceTimelineEvent(category, name, identifier, Dart_Timeline_Event_Counter,
split.first, split.second);
}

void TraceEvent0(TraceArg category_group, TraceArg name);

Expand Down
2 changes: 0 additions & 2 deletions tools/gn
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ def to_gn_args(args):

if not sys.platform.startswith(('cygwin', 'win')):
gn_args['use_clang_static_analyzer'] = args.clang_static_analyzer
else:
gn_args['use_clang_static_analyzer'] = False

gn_args['embedder_for_target'] = args.embedder_for_target

Expand Down

0 comments on commit 906d684

Please sign in to comment.