1 /*
2  * Copyright (C) 2019 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 #define LOG_TAG "pwrstats_util"
17 
18 #include "DataProviderHelper.h"
19 #include <android-base/logging.h>
20 
StateResidencyInterval(const StateResidency_Residency & startResidency,StateResidency_Residency * intervalResidency)21 int StateResidencyInterval(const StateResidency_Residency& startResidency,
22                            StateResidency_Residency* intervalResidency) {
23     // If start and interval are not the same size then they cannot have matching data
24     if (startResidency.size() != intervalResidency->size()) {
25         LOG(ERROR) << __func__ << ": mismatched data";
26         return 1;
27     }
28 
29     for (int i = 0; i < startResidency.size(); ++i) {
30         // Check and make sure each entry matches. Data are in sorted order so if there is a
31         // mismatch then we will bail.
32         if (startResidency.Get(i).entity_name() != intervalResidency->Get(i).entity_name() ||
33             startResidency.Get(i).state_name() != intervalResidency->Get(i).state_name()) {
34             LOG(ERROR) << __func__ << ": mismatched data";
35             return 1;
36         }
37 
38         auto delta = intervalResidency->Get(i).time_ms() - startResidency.Get(i).time_ms();
39         intervalResidency->Mutable(i)->set_time_ms(delta);
40     }
41     return 0;
42 }
43 
StateResidencyDump(const StateResidency_Residency & stateResidency,std::ostream * output)44 void StateResidencyDump(const StateResidency_Residency& stateResidency, std::ostream* output) {
45     for (auto const& residency : stateResidency) {
46         *output << residency.entity_name() << ":" << residency.state_name() << "="
47                 << residency.time_ms() << std::endl;
48     }
49     *output << std::endl;
50 }
51