ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 _STORAGED_H_ |
| 18 | #define _STORAGED_H_ |
| 19 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 20 | #include <semaphore.h> |
| 21 | #include <stdint.h> |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 22 | #include <time.h> |
| 23 | |
| 24 | #include <queue> |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 25 | #include <string> |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 26 | #include <unordered_map> |
| 27 | #include <vector> |
| 28 | |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 29 | #include <batteryservice/IBatteryPropertiesListener.h> |
Jin Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 30 | #include <utils/Mutex.h> |
Yifan Hong | bf2dcb2 | 2017-09-27 14:01:30 -0700 | [diff] [blame] | 31 | |
| 32 | #include <android/hardware/health/2.0/IHealth.h> |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 33 | |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 34 | #define FRIEND_TEST(test_case_name, test_name) \ |
| 35 | friend class test_case_name##_##test_name##_Test |
| 36 | |
Jin Qian | 4fc338e | 2017-03-15 19:03:06 -0700 | [diff] [blame] | 37 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 38 | |
Jin Qian | 4c23c45 | 2017-12-12 18:38:25 -0800 | [diff] [blame] | 39 | #define IS_ALIGNED(x, align) (!((x) & ((align) - 1))) |
| 40 | #define ROUND_UP(x, align) (((x) + ((align) - 1)) & ~((align) - 1)) |
| 41 | |
Jin Qian | 3790f5b | 2017-01-23 14:38:47 -0800 | [diff] [blame] | 42 | #define SECTOR_SIZE ( 512 ) |
| 43 | #define SEC_TO_MSEC ( 1000 ) |
| 44 | #define MSEC_TO_USEC ( 1000 ) |
| 45 | #define USEC_TO_NSEC ( 1000 ) |
| 46 | #define SEC_TO_USEC ( 1000000 ) |
Jin Qian | 9cdfdd3 | 2017-01-31 17:33:20 -0800 | [diff] [blame] | 47 | #define HOUR_TO_SEC ( 3600 ) |
| 48 | #define DAY_TO_SEC ( 3600 * 24 ) |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 49 | #define WEEK_TO_DAYS ( 7 ) |
| 50 | #define YEAR_TO_WEEKS ( 52 ) |
| 51 | |
| 52 | #include "storaged_diskstats.h" |
| 53 | #include "storaged_info.h" |
| 54 | #include "storaged_uid_monitor.h" |
| 55 | #include "storaged.pb.h" |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 56 | #include "uid_info.h" |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 57 | |
| 58 | using namespace std; |
| 59 | using namespace android; |
Jin Qian | 3790f5b | 2017-01-23 14:38:47 -0800 | [diff] [blame] | 60 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 61 | // Periodic chores intervals in seconds |
Jin Qian | 0c77270 | 2017-01-17 11:14:22 -0800 | [diff] [blame] | 62 | #define DEFAULT_PERIODIC_CHORES_INTERVAL_UNIT ( 60 ) |
| 63 | #define DEFAULT_PERIODIC_CHORES_INTERVAL_DISK_STATS_PUBLISH ( 3600 ) |
Jin Qian | 9cdfdd3 | 2017-01-31 17:33:20 -0800 | [diff] [blame] | 64 | #define DEFAULT_PERIODIC_CHORES_INTERVAL_UID_IO ( 3600 ) |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 65 | #define DEFAULT_PERIODIC_CHORES_INTERVAL_UID_IO_LIMIT ( 300 ) |
| 66 | #define DEFAULT_PERIODIC_CHORES_INTERVAL_FLUSH_PROTO ( 3600 ) |
Jin Qian | 3790f5b | 2017-01-23 14:38:47 -0800 | [diff] [blame] | 67 | |
| 68 | // UID IO threshold in bytes |
| 69 | #define DEFAULT_PERIODIC_CHORES_UID_IO_THRESHOLD ( 1024 * 1024 * 1024ULL ) |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 70 | |
| 71 | struct storaged_config { |
| 72 | int periodic_chores_interval_unit; |
| 73 | int periodic_chores_interval_disk_stats_publish; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 74 | int periodic_chores_interval_uid_io; |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 75 | int periodic_chores_interval_flush_proto; |
Jin Qian | 3790f5b | 2017-01-23 14:38:47 -0800 | [diff] [blame] | 76 | int event_time_check_usec; // check how much cputime spent in event loop |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Yifan Hong | bf2dcb2 | 2017-09-27 14:01:30 -0700 | [diff] [blame] | 79 | class storaged_t : public android::hardware::health::V2_0::IHealthInfoCallback, |
| 80 | public android::hardware::hidl_death_recipient { |
| 81 | private: |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 82 | time_t mTimer; |
| 83 | storaged_config mConfig; |
Yifan Hong | c4b46e0 | 2018-01-16 15:49:08 -0800 | [diff] [blame] | 84 | unique_ptr<disk_stats_monitor> mDsm; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 85 | uid_monitor mUidm; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 86 | time_t mStarttime; |
Yifan Hong | bf2dcb2 | 2017-09-27 14:01:30 -0700 | [diff] [blame] | 87 | sp<android::hardware::health::V2_0::IHealth> health; |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 88 | unique_ptr<storage_info_t> storage_info; |
Jin Qian | 4c23c45 | 2017-12-12 18:38:25 -0800 | [diff] [blame] | 89 | static const uint32_t current_version; |
Mark Salyzyn | fc8e987 | 2020-11-20 09:30:29 -0800 | [diff] [blame] | 90 | Mutex proto_lock; |
Jin Qian | 94b64ef | 2017-11-09 15:07:18 -0800 | [diff] [blame] | 91 | unordered_map<userid_t, bool> proto_loaded; |
| 92 | void load_proto(userid_t user_id); |
Jin Qian | 4c23c45 | 2017-12-12 18:38:25 -0800 | [diff] [blame] | 93 | char* prepare_proto(userid_t user_id, StoragedProto* proto); |
Jin Qian | 94b64ef | 2017-11-09 15:07:18 -0800 | [diff] [blame] | 94 | void flush_proto(userid_t user_id, StoragedProto* proto); |
Jin Qian | 4c23c45 | 2017-12-12 18:38:25 -0800 | [diff] [blame] | 95 | void flush_proto_data(userid_t user_id, const char* data, ssize_t size); |
Jin Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 96 | string proto_path(userid_t user_id) { |
| 97 | return string("/data/misc_ce/") + to_string(user_id) + |
| 98 | "/storaged/storaged.proto"; |
| 99 | } |
Yifan Hong | 4a43bdc | 2018-01-16 17:20:32 -0800 | [diff] [blame] | 100 | void init_health_service(); |
| 101 | |
| 102 | public: |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 103 | storaged_t(void); |
Yifan Hong | 4a43bdc | 2018-01-16 17:20:32 -0800 | [diff] [blame] | 104 | void init(void); |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 105 | void event(void); |
Jin Qian | 3790f5b | 2017-01-23 14:38:47 -0800 | [diff] [blame] | 106 | void event_checked(void); |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 107 | void pause(void) { |
| 108 | sleep(mConfig.periodic_chores_interval_unit); |
| 109 | } |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 110 | |
| 111 | time_t get_starttime(void) { |
| 112 | return mStarttime; |
| 113 | } |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 114 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 115 | unordered_map<uint32_t, uid_info> get_uids(void) { |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 116 | return mUidm.get_uid_io_stats(); |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 117 | } |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 118 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 119 | vector<int> get_perf_history(void) { |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 120 | return storage_info->get_perf_history(); |
| 121 | } |
| 122 | |
Michael Wachenschwanz | 37b912b | 2017-12-14 18:20:26 -0800 | [diff] [blame] | 123 | uint32_t get_recent_perf(void) { return storage_info->get_recent_perf(); } |
| 124 | |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 125 | map<uint64_t, struct uid_records> get_uid_records( |
Jin Qian | dd41d6b | 2017-02-10 17:23:16 -0800 | [diff] [blame] | 126 | double hours, uint64_t threshold, bool force_report) { |
Jin Qian | 94b64ef | 2017-11-09 15:07:18 -0800 | [diff] [blame] | 127 | return mUidm.dump(hours, threshold, force_report); |
Jin Qian | e5ea17c | 2017-02-10 10:50:03 -0800 | [diff] [blame] | 128 | } |
Jin Qian | a853332 | 2017-10-13 18:15:34 -0700 | [diff] [blame] | 129 | |
Jin Qian | e5ea17c | 2017-02-10 10:50:03 -0800 | [diff] [blame] | 130 | void update_uid_io_interval(int interval) { |
| 131 | if (interval >= DEFAULT_PERIODIC_CHORES_INTERVAL_UID_IO_LIMIT) { |
| 132 | mConfig.periodic_chores_interval_uid_io = interval; |
| 133 | } |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 134 | } |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 135 | |
Jin Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 136 | void add_user_ce(userid_t user_id); |
| 137 | void remove_user_ce(userid_t user_id); |
Jin Qian | a853332 | 2017-10-13 18:15:34 -0700 | [diff] [blame] | 138 | |
Yifan Hong | bf2dcb2 | 2017-09-27 14:01:30 -0700 | [diff] [blame] | 139 | virtual ::android::hardware::Return<void> healthInfoChanged( |
Hridya Valsaraju | 79d38eb | 2018-01-17 23:02:56 -0800 | [diff] [blame] | 140 | const ::android::hardware::health::V2_0::HealthInfo& info); |
Yifan Hong | bf2dcb2 | 2017-09-27 14:01:30 -0700 | [diff] [blame] | 141 | void serviceDied(uint64_t cookie, const wp<::android::hidl::base::V1_0::IBase>& who); |
Jin Qian | 8847c62 | 2017-07-17 15:06:11 -0700 | [diff] [blame] | 142 | |
| 143 | void report_storage_info(); |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 144 | |
Jin Qian | 94b64ef | 2017-11-09 15:07:18 -0800 | [diff] [blame] | 145 | void flush_protos(unordered_map<int, StoragedProto>* protos); |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | // Eventlog tag |
| 149 | // The content must match the definition in EventLogTags.logtags |
| 150 | #define EVENTLOGTAG_DISKSTATS ( 2732 ) |
| 151 | #define EVENTLOGTAG_EMMCINFO ( 2733 ) |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 152 | #define EVENTLOGTAG_UID_IO_ALERT ( 2734 ) |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 153 | |
| 154 | #endif /* _STORAGED_H_ */ |