1 /*
2  * Copyright (C) 2020 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 "WakeupList.h"
18 
19 #include <android-base/logging.h>
20 #include <android-base/strings.h>
21 
22 namespace android {
23 namespace system {
24 namespace suspend {
25 namespace V1_0 {
26 
WakeupList(size_t capacity)27 WakeupList::WakeupList(size_t capacity) : mCapacity(capacity) {}
28 
getWakeupStats(std::vector<WakeupInfo> * wakeups) const29 void WakeupList::getWakeupStats(std::vector<WakeupInfo>* wakeups) const {
30     std::scoped_lock lock(mLock);
31 
32     for (const WakeupInfo& w : mWakeups) {
33         wakeups->push_back(w);
34     }
35 }
36 
update(const std::vector<std::string> & wakeupReasons)37 void WakeupList::update(const std::vector<std::string>& wakeupReasons) {
38     if (wakeupReasons.empty()) {
39         LOG(ERROR) << "WakeupList: empty wakeup reasons";
40         return;
41     }
42     std::string key = ::android::base::Join(wakeupReasons, ";");
43 
44     std::scoped_lock lock(mLock);
45 
46     auto it = mLookupTable.find(key);
47     if (it == mLookupTable.end()) {
48         // Create a new entry
49         WakeupInfo w;
50         w.name = key;
51         w.count = 1;
52 
53         insert(std::move(w));
54         evict();
55     } else {
56         // Entry found. Increment the count
57         auto staleEntry = it->second;
58         WakeupInfo updatedEntry = *staleEntry;
59         updatedEntry.count++;
60 
61         erase(staleEntry);
62         insert(std::move(updatedEntry));
63     }
64 }
65 
evict()66 void WakeupList::evict() {
67     if (mWakeups.size() > mCapacity) {
68         erase(std::prev(mWakeups.end()));
69         LOG(ERROR) << "WakeupList: Capacity met, consider adjusting capacity to "
70                       "avoid stats eviction.";
71     }
72 }
73 
insert(WakeupInfo entry)74 void WakeupList::insert(WakeupInfo entry) {
75     mWakeups.push_front(entry);
76     mLookupTable.insert_or_assign(entry.name, mWakeups.begin());
77 }
78 
erase(std::list<WakeupInfo>::iterator entry)79 void WakeupList::erase(std::list<WakeupInfo>::iterator entry) {
80     mLookupTable.erase(entry->name);
81     mWakeups.erase(entry);
82 }
83 
84 }  // namespace V1_0
85 }  // namespace suspend
86 }  // namespace system
87 }  // namespace android