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 #include "histogram.h" 21 22 #include <inttypes.h> 23 #include <vector> 24 #include <map> 25 #include <string> 26 #include <memory> 27 28 namespace tuningfork { 29 30 typedef ProtobufSerialization SerializedAnnotation; 31 32 // A prong holds a histogram for a given annotation and instrument key 33 class Prong { 34 public: 35 InstrumentationKey instrumentation_key_; 36 SerializedAnnotation annotation_; 37 Histogram histogram_; 38 TimePoint last_time_ns_; 39 40 Prong(InstrumentationKey instrumentation_key = 0, 41 const SerializedAnnotation &annotation = {}, 42 const Settings::Histogram& histogram_settings = {}) instrumentation_key_(instrumentation_key)43 : instrumentation_key_(instrumentation_key), annotation_(annotation), 44 last_time_ns_(std::chrono::steady_clock::time_point::min()), 45 histogram_(histogram_settings) {} 46 Tick(TimePoint t_ns)47 void Tick(TimePoint t_ns) { 48 if (last_time_ns_ != std::chrono::steady_clock::time_point::min()) 49 Trace(t_ns - last_time_ns_); 50 last_time_ns_ = t_ns; 51 } 52 Trace(Duration dt_ns)53 void Trace(Duration dt_ns) { 54 // The histogram stores millisecond values as doubles 55 histogram_.Add( 56 double(std::chrono::duration_cast<std::chrono::nanoseconds>(dt_ns).count()) / 1000000); 57 } 58 Clear()59 void Clear() { 60 last_time_ns_ = std::chrono::steady_clock::time_point::min(); 61 histogram_.Clear(); 62 } 63 Count()64 size_t Count() const { 65 return histogram_.Count(); 66 } 67 68 friend class ClearcutSerializer; 69 }; 70 71 // Simple fixed-size cache 72 class ProngCache { 73 std::vector<std::unique_ptr<Prong>> prongs_; 74 public: 75 ProngCache(size_t size, int max_num_instrumentation_keys, 76 const std::vector<Settings::Histogram>& histogram_settings, 77 const std::function<SerializedAnnotation(uint64_t)>& seralizeId); 78 79 Prong *Get(uint64_t compound_id); 80 81 void Clear(); 82 83 friend class ClearcutSerializer; 84 85 }; 86 87 } // namespace tuningfork { 88