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 #include "prong.h"
18 
19 #define LOG_TAG "TuningFork"
20 #include "Log.h"
21 
22 #include <string>
23 #include <sstream>
24 
25 namespace tuningfork {
26 
27 // Allocate all the prongs up front
ProngCache(size_t size,int max_num_instrumentation_keys,const std::vector<Settings::Histogram> & histogram_settings,const std::function<SerializedAnnotation (uint64_t)> & seralizeId)28 ProngCache::ProngCache(size_t size, int max_num_instrumentation_keys,
29                        const std::vector<Settings::Histogram> &histogram_settings,
30                        const std::function<SerializedAnnotation(uint64_t)> &seralizeId)
31     : prongs_(size) {
32     // Allocate all the prongs
33     InstrumentationKey ikey = 0;
34     for (int i = 0; i < size; ++i) {
35         auto &p = prongs_[i];
36         SerializedAnnotation annotation = seralizeId(i);
37         auto& h = histogram_settings[ikey<histogram_settings.size()?ikey:0];
38         p = std::make_unique<Prong>(ikey, annotation, h);
39         ++ikey;
40         if (ikey >= max_num_instrumentation_keys)
41             ikey = 0;
42     }
43 }
44 
Get(uint64_t compound_id)45 Prong *ProngCache::Get(uint64_t compound_id) {
46     if (compound_id >= prongs_.size()) {
47         ALOGW("You have overrun the number of histograms (are your "
48               "Settings correct?)");
49         return nullptr;
50     }
51     return prongs_[compound_id].get();
52 }
53 
Clear()54 void ProngCache::Clear() {
55     for (auto &p: prongs_) {
56         if (p->histogram_.Count() > 0)
57             p->histogram_.Clear();
58     }
59 }
60 
61 }
62