summaryrefslogtreecommitdiffstats
path: root/remoting/protocol/performance_tracker.h
blob: c31ca91eb6774d750737130d62dc53674287d3ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// 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 REMOTING_PROTOCOL_PERFORMANCE_TRACKER_H_
#define REMOTING_PROTOCOL_PERFORMANCE_TRACKER_H_

#include <stdint.h>

#include <map>

#include "base/callback.h"
#include "base/macros.h"
#include "base/timer/timer.h"
#include "remoting/base/rate_counter.h"
#include "remoting/base/running_samples.h"

namespace remoting {

class VideoPacket;

namespace protocol {

// PerformanceTracker defines a bundle of performance counters and statistics
// for chromoting.
class PerformanceTracker {
 public:
  // Callback that updates UMA custom counts or custom times histograms.
  typedef base::Callback<void(const std::string& histogram_name,
                              int64_t value,
                              int histogram_min,
                              int histogram_max,
                              int histogram_buckets)>
      UpdateUmaCustomHistogramCallback;

  // Callback that updates UMA enumeration histograms.
  typedef base::Callback<
      void(const std::string& histogram_name, int64_t value, int histogram_max)>
      UpdateUmaEnumHistogramCallback;

  PerformanceTracker();
  virtual ~PerformanceTracker();

  // Constant used to calculate the average for rate metrics and used by the
  // plugin for the frequency at which stats should be updated.
  static const int kStatsUpdatePeriodSeconds = 1;

  // Return rates and running-averages for different metrics.
  double video_bandwidth() { return video_bandwidth_.Rate(); }
  double video_frame_rate() { return video_frame_rate_.Rate(); }
  double video_packet_rate() { return video_packet_rate_.Rate(); }
  const RunningSamples& video_capture_ms() { return video_capture_ms_; }
  const RunningSamples& video_encode_ms() { return video_encode_ms_; }
  const RunningSamples& video_decode_ms() { return video_decode_ms_; }
  const RunningSamples& video_paint_ms() { return video_paint_ms_; }
  const RunningSamples& round_trip_ms() { return round_trip_ms_; }

  // Record stats for a video-packet.
  void RecordVideoPacketStats(const VideoPacket& packet);

  // Helpers to track decode and paint time. If the render drops some frames
  // before they are painted then OnFramePainted() records paint time when the
  // following frame is rendered. OnFramePainted() may be called multiple times,
  // in which case all calls after the first one are ignored.
  void OnFrameDecoded(int32_t frame_id);
  void OnFramePainted(int32_t frame_id);

  // Sets callbacks in ChromotingInstance to update a UMA custom counts, custom
  // times or enum histogram.
  void SetUpdateUmaCallbacks(
      UpdateUmaCustomHistogramCallback update_uma_custom_counts_callback,
      UpdateUmaCustomHistogramCallback update_uma_custom_times_callback,
      UpdateUmaEnumHistogramCallback update_uma_enum_histogram_callback);

  void OnPauseStateChanged(bool paused);

 private:
  struct FrameTimestamps {
    FrameTimestamps();
    ~FrameTimestamps();

    // Set to null for frames that were not sent after a fresh input event.
    base::TimeTicks latest_event_timestamp;

    // Set to TimeDelta::Max() when unknown.
    base::TimeDelta total_host_latency;

    base::TimeTicks time_received;
    base::TimeTicks time_decoded;
  };
  typedef std::map<int32_t, FrameTimestamps> FramesTimestampsMap;

  // Helper to record input roundtrip latency after a frame has been painted.
  void RecordRoundTripLatency(const FrameTimestamps& timestamps);

  // Updates frame-rate, packet-rate and bandwidth UMA statistics.
  void UploadRateStatsToUma();

  // The video and packet rate metrics below are updated per video packet
  // received and then, for reporting, averaged over a 1s time-window.
  // Bytes per second for non-empty video-packets.
  RateCounter video_bandwidth_;

  // Frames per second for non-empty video-packets.
  RateCounter video_frame_rate_;

  // Video packets per second, including empty video-packets.
  // This will be greater than the frame rate, as individual frames are
  // contained in packets, some of which might be empty (e.g. when there are no
  // screen changes).
  RateCounter video_packet_rate_;

  // The following running-averages are uploaded to UMA per video packet and
  // also used for display to users, averaged over the N most recent samples.
  // N = kLatencySampleSize.
  RunningSamples video_capture_ms_;
  RunningSamples video_encode_ms_;
  RunningSamples video_decode_ms_;
  RunningSamples video_paint_ms_;
  RunningSamples round_trip_ms_;

  // Used to update UMA stats, if set.
  UpdateUmaCustomHistogramCallback uma_custom_counts_updater_;
  UpdateUmaCustomHistogramCallback uma_custom_times_updater_;
  UpdateUmaEnumHistogramCallback uma_enum_histogram_updater_;

  // The latest event timestamp that a VideoPacket was seen annotated with.
  base::TimeTicks latest_event_timestamp_;

  // Stores timestamps for the frames that are currently being processed.
  FramesTimestampsMap frame_timestamps_;

  bool is_paused_ = false;

  base::RepeatingTimer upload_uma_stats_timer_;

  DISALLOW_COPY_AND_ASSIGN(PerformanceTracker);
};

}  // namespace protocol
}  // namespace remoting

#endif  // REMOTING_PROTOCOL_PERFORMANCE_TRACKER_H_