1 /* 2 * Copyright (C) 2024 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 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_ANDROID_BUGREPORT_ANDROID_BATTERY_STATS_HISTORY_STRING_TRACKER_H_ 18 #define SRC_TRACE_PROCESSOR_IMPORTERS_ANDROID_BUGREPORT_ANDROID_BATTERY_STATS_HISTORY_STRING_TRACKER_H_ 19 20 #include <bitset> 21 #include <cstdint> 22 #include <optional> 23 24 #include "perfetto/base/logging.h" 25 #include "perfetto/ext/base/string_view.h" 26 #include "perfetto/public/compiler.h" 27 #include "src/trace_processor/storage/trace_storage.h" 28 #include "src/trace_processor/tables/metadata_tables_py.h" 29 #include "src/trace_processor/types/destructible.h" 30 #include "src/trace_processor/types/trace_processor_context.h" 31 32 namespace perfetto::trace_processor { 33 34 // This class is used to track the history string pool (hsp) items emitted by 35 // the battery stats checkin reader and consumed by AndroidDumpStateParserImpl 36 // 37 // The history string pool items are stored in a vector and not internerned in 38 // the trace processor storage, due to these strings needing further processing 39 // after being associated with a timestampped event post-sort. The final 40 // processed string will be interned there post-sort instead. 41 // 42 // The string pool items are added to the vector using the SetStringPoolItem 43 // method. The items are later retrieved using the GetUid and GetString 44 // methods. 45 class AndroidBatteryStatsHistoryStringTracker : public Destructible { 46 public: 47 ~AndroidBatteryStatsHistoryStringTracker() override; 48 GetOrCreate(TraceProcessorContext * context)49 static AndroidBatteryStatsHistoryStringTracker* GetOrCreate( 50 TraceProcessorContext* context) { 51 if (!context->android_battery_stats_history_tracker) { 52 context->android_battery_stats_history_tracker.reset( 53 new AndroidBatteryStatsHistoryStringTracker()); 54 } 55 return static_cast<AndroidBatteryStatsHistoryStringTracker*>( 56 context->android_battery_stats_history_tracker.get()); 57 } 58 59 // Returns the Uid (user ID) associated with the given HSP index. GetUid(int64_t index)60 int32_t GetUid(int64_t index) { 61 return index >= 0 ? hsp_items_[static_cast<uint64_t>(index)].uid : -1; 62 } 63 64 // Gets the string associated with the given HSP index. GetString(int64_t index)65 const std::string& GetString(int64_t index) { 66 return index >= 0 ? hsp_items_[static_cast<uint64_t>(index)].str : invalid_string_; 67 } 68 69 // Associate the given uid and string with the given HSP index. 70 base::Status SetStringPoolItem(int64_t index, 71 int32_t uid, 72 const std::string& str); 73 74 // Set the current version of the battery stats file being parsed. battery_stats_version(uint32_t ver)75 void battery_stats_version(uint32_t ver) { battery_stats_version_ = ver; } 76 77 // Get the current version of the battery stats file being parsed. battery_stats_version()78 uint32_t battery_stats_version() { return battery_stats_version_; } 79 80 private: 81 struct HistoryStringPoolItem { 82 // The linux User ID (UID) associated with the item. 83 // max linux uid is 2^31 - 2. Battery stats sometimes gives us a uid of -1, 84 // so make this signed. 85 int32_t uid; 86 // An arbirary string associated with the HSP i 87 std::string str; 88 }; 89 90 const std::string invalid_string_ = ""; 91 92 // Use a vector to store the hsp items internally since these strings indices 93 // start from zero and are consecutive. 94 std::vector<HistoryStringPoolItem> hsp_items_; 95 uint32_t battery_stats_version_ = 0; 96 }; 97 98 } // namespace perfetto::trace_processor 99 100 #endif // SRC_TRACE_PROCESSOR_IMPORTERS_ANDROID_BUGREPORT_ANDROID_BATTERY_STATS_HISTORY_STRING_TRACKER_H_ 101