Home | History | Annotate | Download | only in base
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "ui/base/latency_info.h"
      6 
      7 #include <algorithm>
      8 
      9 namespace ui {
     10 
     11 LatencyInfo::LatencyInfo() {
     12 }
     13 
     14 LatencyInfo::~LatencyInfo() {
     15 }
     16 
     17 void LatencyInfo::MergeWith(const LatencyInfo& other) {
     18   for (LatencyMap::const_iterator it = other.latency_components.begin();
     19        it != other.latency_components.end();
     20        ++it) {
     21     AddLatencyNumberWithTimestamp(it->first.first,
     22                                   it->first.second,
     23                                   it->second.sequence_number,
     24                                   it->second.event_time,
     25                                   it->second.event_count);
     26   }
     27 }
     28 
     29 void LatencyInfo::AddNewLatencyFrom(const LatencyInfo& other) {
     30     for (LatencyMap::const_iterator it = other.latency_components.begin();
     31          it != other.latency_components.end();
     32          ++it) {
     33       if (!FindLatency(it->first.first, it->first.second, NULL)) {
     34         AddLatencyNumberWithTimestamp(it->first.first,
     35                                       it->first.second,
     36                                       it->second.sequence_number,
     37                                       it->second.event_time,
     38                                       it->second.event_count);
     39       }
     40     }
     41 }
     42 
     43 void LatencyInfo::AddLatencyNumber(LatencyComponentType component,
     44                                    int64 id,
     45                                    int64 component_sequence_number) {
     46   AddLatencyNumberWithTimestamp(component, id, component_sequence_number,
     47                                 base::TimeTicks::HighResNow(), 1);
     48 }
     49 
     50 void LatencyInfo::AddLatencyNumberWithTimestamp(LatencyComponentType component,
     51                                                 int64 id,
     52                                                 int64 component_sequence_number,
     53                                                 base::TimeTicks time,
     54                                                 uint32 event_count) {
     55   LatencyMap::key_type key = std::make_pair(component, id);
     56   LatencyMap::iterator it = latency_components.find(key);
     57   if (it == latency_components.end()) {
     58     LatencyComponent info = {component_sequence_number, time, event_count};
     59     latency_components[key] = info;
     60     return;
     61   }
     62   it->second.sequence_number = std::max(component_sequence_number,
     63                                         it->second.sequence_number);
     64   uint32 new_count = event_count + it->second.event_count;
     65   if (event_count > 0 && new_count != 0) {
     66     // Do a weighted average, so that the new event_time is the average of
     67     // the times of events currently in this structure with the time passed
     68     // into this method.
     69     it->second.event_time += (time - it->second.event_time) * event_count /
     70         new_count;
     71     it->second.event_count = new_count;
     72   }
     73 }
     74 
     75 bool LatencyInfo::FindLatency(LatencyComponentType type,
     76                               int64 id,
     77                               LatencyComponent* output) const {
     78   LatencyMap::const_iterator it = latency_components.find(
     79       std::make_pair(type, id));
     80   if (it == latency_components.end())
     81     return false;
     82   if (output)
     83     *output = it->second;
     84   return true;
     85 }
     86 
     87 void LatencyInfo::Clear() {
     88   latency_components.clear();
     89 }
     90 
     91 }  // namespace ui
     92