blob: 8210ae5c1985c294e5e92ed162c0f92ad95dcacf [file] [log] [blame]
// Copyright 2013 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 <stdint.h>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/containers/small_map.h"
#include "base/time/time.h"
#include "ui/events/events_base_export.h"
#include "ui/gfx/geometry/point_f.h"
#if !defined(OS_IOS)
#include "ipc/ipc_param_traits.h" // nogncheck
#include "mojo/public/cpp/bindings/struct_traits.h" // nogncheck
namespace base {
namespace trace_event {
class ConvertableToTraceFormat;
namespace ui {
#if !defined(OS_IOS)
namespace mojom {
class LatencyInfoDataView;
// When adding new components, or new metrics based on LatencyInfo,
// please update
enum LatencyComponentType {
// ---------------------------BEGIN COMPONENT-------------------------------
// BEGIN COMPONENT is when we show the latency begin in chrome://tracing.
// Timestamp when the input event is sent from RenderWidgetHost to renderer.
// In threaded scrolling, main thread scroll listener update is async to
// scroll processing in impl thread. This is the timestamp when we consider
// the main thread scroll listener update is begun.
// ---------------------------NORMAL COMPONENT-------------------------------
// The original timestamp of the touch event which converts to scroll update.
// The original timestamp of the touch event which converts to the *first*
// scroll update in a scroll gesture sequence.
// Original timestamp for input event (e.g. timestamp from kernel).
// Timestamp when the UI event is created.
// Timestamp when the event is dispatched on the main thread of the renderer.
// This is special component indicating there is rendering scheduled for
// the event associated with this LatencyInfo on main thread.
// This is special component indicating there is rendering scheduled for
// the event associated with this LatencyInfo on impl thread.
// Timestamp when a scroll update is forwarded to the main thread.
// Timestamp when the event's ack is received by the RWH.
// Frame number when a window snapshot was requested. The snapshot
// is taken when the rendering results actually reach the screen.
// Timestamp when a tab is requested to be shown.
// Timestamp when the frame is swapped in renderer.
// Timestamp of when the browser process receives a buffer swap notification
// from the renderer.
// Timestamp of when the gpu service began swap buffers, unlike
// Timestamp of when the gesture scroll update is generated from a mouse wheel
// event.
// ---------------------------TERMINAL COMPONENT-----------------------------
// Timestamp when the event is acked from renderer when it does not
// cause any rendering to be scheduled.
// Timestamp when the frame is swapped (i.e. when the rendering caused by
// input event actually takes effect).
// This component indicates that the input causes a commit to be scheduled
// but the commit failed.
// This component indicates that the input causes a commit to be scheduled
// but the commit was aborted since it carried no new information.
// This component indicates that the input causes a swap to be scheduled
// but the swap failed.
enum SourceEventType {
class EVENTS_BASE_EXPORT LatencyInfo {
struct LatencyComponent {
// Nondecreasing number that can be used to determine what events happened
// in the component at the time this struct was sent on to the next
// component.
int64_t sequence_number;
// Average time of events that happened in this component.
base::TimeTicks event_time;
// Count of events that happened in this component
uint32_t event_count;
// Time of the oldest event that happened in this component.
base::TimeTicks first_event_time;
// Time of the most recent event that happened in this component.
base::TimeTicks last_event_time;
// Empirically determined constant based on a typical scroll sequence.
enum { kTypicalMaxComponentsPerLatencyInfo = 10 };
enum : size_t { kMaxInputCoordinates = 2 };
// Map a Latency Component (with a component-specific int64_t id) to a
// component info.
typedef base::SmallMap<
std::map<std::pair<LatencyComponentType, int64_t>, LatencyComponent>,
kTypicalMaxComponentsPerLatencyInfo> LatencyMap;
LatencyInfo(const LatencyInfo& other);
LatencyInfo(SourceEventType type);
// For test only.
LatencyInfo(int64_t trace_id, bool terminated);
// Returns true if the vector |latency_info| is valid. Returns false
// if it is not valid and log the |referring_msg|.
// This function is mainly used to check the latency_info vector that
// is passed between processes using IPC message has reasonable size
// so that we are confident the IPC message is not corrupted/compromised.
// This check will go away once the IPC system has better built-in scheme
// for corruption/compromise detection.
static bool Verify(const std::vector<LatencyInfo>& latency_info,
const char* referring_msg);
// Copy LatencyComponents with type |type| from |other| into |this|.
void CopyLatencyFrom(const LatencyInfo& other, LatencyComponentType type);
// Add LatencyComponents that are in |other| but not in |this|.
void AddNewLatencyFrom(const LatencyInfo& other);
// Modifies the current sequence number for a component, and adds a new
// sequence number with the current timestamp.
void AddLatencyNumber(LatencyComponentType component,
int64_t id,
int64_t component_sequence_number);
// Similar to |AddLatencyNumber|, and also appends |trace_name_str| to
// the trace event's name.
// This function should only be called when adding a BEGIN component.
void AddLatencyNumberWithTraceName(LatencyComponentType component,
int64_t id,
int64_t component_sequence_number,
const char* trace_name_str);
// Modifies the current sequence number and adds a certain number of events
// for a specific component.
void AddLatencyNumberWithTimestamp(LatencyComponentType component,
int64_t id,
int64_t component_sequence_number,
base::TimeTicks time,
uint32_t event_count);
// Returns true if the a component with |type| and |id| is found in
// the latency_components and the component is stored to |output| if
// |output| is not NULL. Returns false if no such component is found.
bool FindLatency(LatencyComponentType type,
int64_t id,
LatencyComponent* output) const;
void RemoveLatency(LatencyComponentType type);
// Returns true if there is still room for keeping the |input_coordinate|,
// false otherwise.
bool AddInputCoordinate(const gfx::PointF& input_coordinate);
uint32_t input_coordinates_size() const { return input_coordinates_size_; }
const gfx::PointF* input_coordinates() const { return input_coordinates_; }
const LatencyMap& latency_components() const { return latency_components_; }
const SourceEventType& source_event_type() const {
return source_event_type_;
void set_source_event_type(SourceEventType type) {
source_event_type_ = type;
bool terminated() const { return terminated_; }
void set_coalesced() { coalesced_ = true; }
bool coalesced() const { return coalesced_; }
int64_t trace_id() const { return trace_id_; }
void AddLatencyNumberWithTimestampImpl(LatencyComponentType component,
int64_t id,
int64_t component_sequence_number,
base::TimeTicks time,
uint32_t event_count,
const char* trace_name_str);
// Converts latencyinfo into format that can be dumped into trace buffer.
// Shown as part of the name of the trace event for this LatencyInfo.
// String is empty if no tracing is enabled.
std::string trace_name_;
LatencyMap latency_components_;
// These coordinates represent window coordinates of the original input event.
uint32_t input_coordinates_size_;
gfx::PointF input_coordinates_[kMaxInputCoordinates];
// The unique id for matching the ASYNC_BEGIN/END trace event.
int64_t trace_id_;
// Whether this event has been coalesced into another event.
bool coalesced_;
// Whether a terminal component has been added.
bool terminated_;
// Stores the type of the first source event.
SourceEventType source_event_type_;
#if !defined(OS_IOS)
friend struct IPC::ParamTraits<ui::LatencyInfo>;
friend struct mojo::StructTraits<ui::mojom::LatencyInfoDataView,
} // namespace ui