Home | History | Annotate | Download | only in tuningfork
      1 /*
      2  * Copyright 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #pragma once
     18 
     19 #include "tuningfork_internal.h"
     20 
     21 #include <inttypes.h>
     22 #include <vector>
     23 #include <string>
     24 
     25 namespace tuningfork {
     26 
     27 class Histogram {
     28     typedef double Sample;
     29     static constexpr int kAutoSizeNumStdDev = 3;
     30     static constexpr double kAutoSizeMinBucketSizeMs = 0.1;
     31     Sample start_ms_, end_ms_, bucket_dt_ms_;
     32     uint32_t num_buckets_;
     33     std::vector<uint32_t> buckets_;
     34     std::vector<Sample> samples_;
     35     bool auto_range_;
     36     size_t count_;
     37 public:
     38     static constexpr int kDefaultNumBuckets = 30;
     39 
     40     explicit Histogram(float start_ms = 0, float end_ms = 0, int num_buckets_between = kDefaultNumBuckets);
     41     explicit Histogram(const Settings::Histogram&);
     42 
     43     // Add a sample delta time
     44     void Add(Sample dt_ms);
     45 
     46     // Reset the histogram
     47     void Clear(bool autorange = false);
     48 
     49     // Get the total number of samples added so far
     50     size_t Count() const { return count_; }
     51 
     52     // Get the histogram as a JSON object, for testing
     53     std::string ToJSON() const;
     54 
     55     // Use the data we have to construct the bucket ranges. This is called automatically after
     56     //  sizeAtWhichToRange samples have been collected, if we are auto-ranging.
     57     void CalcBucketsFromSamples();
     58 
     59     friend class ClearcutSerializer;
     60 };
     61 
     62 } // namespace tuningfork {
     63