blob: a3d05e5d4ca5048c4c763b24eb930c9215460113 [file] [log] [blame]
ynwang62cb3722016-06-17 14:30:48 -07001/*
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
ynwang62cb3722016-06-17 14:30:48 -070020#include <semaphore.h>
21#include <stdint.h>
Jin Qianbcd6e3b2016-12-28 15:43:51 -080022#include <time.h>
23
24#include <queue>
ynwang62cb3722016-06-17 14:30:48 -070025#include <string>
ynwang62cb3722016-06-17 14:30:48 -070026#include <unordered_map>
27#include <vector>
28
Jin Qian5b962c62017-01-30 14:48:38 -080029#include <batteryservice/IBatteryPropertiesListener.h>
Jin Qian6df3bc62017-10-18 17:52:14 -070030#include <utils/Mutex.h>
Yifan Hongbf2dcb22017-09-27 14:01:30 -070031
32#include <android/hardware/health/2.0/IHealth.h>
Jin Qian5b962c62017-01-30 14:48:38 -080033
Jin Qian65dea712017-08-29 16:48:20 -070034#define FRIEND_TEST(test_case_name, test_name) \
35friend class test_case_name##_##test_name##_Test
36
Jin Qian4fc338e2017-03-15 19:03:06 -070037#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
38
Jin Qian4c23c452017-12-12 18:38:25 -080039#define IS_ALIGNED(x, align) (!((x) & ((align) - 1)))
40#define ROUND_UP(x, align) (((x) + ((align) - 1)) & ~((align) - 1))
41
Jin Qian3790f5b2017-01-23 14:38:47 -080042#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 Qian9cdfdd32017-01-31 17:33:20 -080047#define HOUR_TO_SEC ( 3600 )
48#define DAY_TO_SEC ( 3600 * 24 )
Jin Qiand691d6e2017-09-28 16:02:22 -070049#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 Qianb049d182017-10-12 17:02:17 -070056#include "uid_info.h"
Jin Qiand691d6e2017-09-28 16:02:22 -070057
58using namespace std;
59using namespace android;
Jin Qian3790f5b2017-01-23 14:38:47 -080060
ynwang62cb3722016-06-17 14:30:48 -070061// Periodic chores intervals in seconds
Jin Qian0c772702017-01-17 11:14:22 -080062#define DEFAULT_PERIODIC_CHORES_INTERVAL_UNIT ( 60 )
63#define DEFAULT_PERIODIC_CHORES_INTERVAL_DISK_STATS_PUBLISH ( 3600 )
Jin Qian9cdfdd32017-01-31 17:33:20 -080064#define DEFAULT_PERIODIC_CHORES_INTERVAL_UID_IO ( 3600 )
Jin Qianb049d182017-10-12 17:02:17 -070065#define DEFAULT_PERIODIC_CHORES_INTERVAL_UID_IO_LIMIT ( 300 )
66#define DEFAULT_PERIODIC_CHORES_INTERVAL_FLUSH_PROTO ( 3600 )
Jin Qian3790f5b2017-01-23 14:38:47 -080067
68// UID IO threshold in bytes
69#define DEFAULT_PERIODIC_CHORES_UID_IO_THRESHOLD ( 1024 * 1024 * 1024ULL )
ynwang62cb3722016-06-17 14:30:48 -070070
71struct storaged_config {
72 int periodic_chores_interval_unit;
73 int periodic_chores_interval_disk_stats_publish;
Jin Qianbcd6e3b2016-12-28 15:43:51 -080074 int periodic_chores_interval_uid_io;
Jin Qiand691d6e2017-09-28 16:02:22 -070075 int periodic_chores_interval_flush_proto;
Jin Qian3790f5b2017-01-23 14:38:47 -080076 int event_time_check_usec; // check how much cputime spent in event loop
ynwang62cb3722016-06-17 14:30:48 -070077};
78
Yifan Hongbf2dcb22017-09-27 14:01:30 -070079class storaged_t : public android::hardware::health::V2_0::IHealthInfoCallback,
80 public android::hardware::hidl_death_recipient {
81 private:
ynwang62cb3722016-06-17 14:30:48 -070082 time_t mTimer;
83 storaged_config mConfig;
Yifan Hongc4b46e02018-01-16 15:49:08 -080084 unique_ptr<disk_stats_monitor> mDsm;
Jin Qianbcd6e3b2016-12-28 15:43:51 -080085 uid_monitor mUidm;
ynwang62cb3722016-06-17 14:30:48 -070086 time_t mStarttime;
Yifan Hongbf2dcb22017-09-27 14:01:30 -070087 sp<android::hardware::health::V2_0::IHealth> health;
Jin Qiand691d6e2017-09-28 16:02:22 -070088 unique_ptr<storage_info_t> storage_info;
Jin Qian4c23c452017-12-12 18:38:25 -080089 static const uint32_t current_version;
Mark Salyzynfc8e9872020-11-20 09:30:29 -080090 Mutex proto_lock;
Jin Qian94b64ef2017-11-09 15:07:18 -080091 unordered_map<userid_t, bool> proto_loaded;
92 void load_proto(userid_t user_id);
Jin Qian4c23c452017-12-12 18:38:25 -080093 char* prepare_proto(userid_t user_id, StoragedProto* proto);
Jin Qian94b64ef2017-11-09 15:07:18 -080094 void flush_proto(userid_t user_id, StoragedProto* proto);
Jin Qian4c23c452017-12-12 18:38:25 -080095 void flush_proto_data(userid_t user_id, const char* data, ssize_t size);
Jin Qian6df3bc62017-10-18 17:52:14 -070096 string proto_path(userid_t user_id) {
97 return string("/data/misc_ce/") + to_string(user_id) +
98 "/storaged/storaged.proto";
99 }
Yifan Hong4a43bdc2018-01-16 17:20:32 -0800100 void init_health_service();
101
102 public:
ynwang62cb3722016-06-17 14:30:48 -0700103 storaged_t(void);
Yifan Hong4a43bdc2018-01-16 17:20:32 -0800104 void init(void);
ynwang62cb3722016-06-17 14:30:48 -0700105 void event(void);
Jin Qian3790f5b2017-01-23 14:38:47 -0800106 void event_checked(void);
ynwang62cb3722016-06-17 14:30:48 -0700107 void pause(void) {
108 sleep(mConfig.periodic_chores_interval_unit);
109 }
ynwang62cb3722016-06-17 14:30:48 -0700110
111 time_t get_starttime(void) {
112 return mStarttime;
113 }
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800114
Jin Qianb049d182017-10-12 17:02:17 -0700115 unordered_map<uint32_t, uid_info> get_uids(void) {
Jin Qian5b962c62017-01-30 14:48:38 -0800116 return mUidm.get_uid_io_stats();
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800117 }
Jin Qiand691d6e2017-09-28 16:02:22 -0700118
Jin Qianb049d182017-10-12 17:02:17 -0700119 vector<int> get_perf_history(void) {
Jin Qiand691d6e2017-09-28 16:02:22 -0700120 return storage_info->get_perf_history();
121 }
122
Michael Wachenschwanz37b912b2017-12-14 18:20:26 -0800123 uint32_t get_recent_perf(void) { return storage_info->get_recent_perf(); }
124
Jin Qiand691d6e2017-09-28 16:02:22 -0700125 map<uint64_t, struct uid_records> get_uid_records(
Jin Qiandd41d6b2017-02-10 17:23:16 -0800126 double hours, uint64_t threshold, bool force_report) {
Jin Qian94b64ef2017-11-09 15:07:18 -0800127 return mUidm.dump(hours, threshold, force_report);
Jin Qiane5ea17c2017-02-10 10:50:03 -0800128 }
Jin Qiana8533322017-10-13 18:15:34 -0700129
Jin Qiane5ea17c2017-02-10 10:50:03 -0800130 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 Qiana2e5bd12017-01-24 16:23:13 -0800134 }
Jin Qian5b962c62017-01-30 14:48:38 -0800135
Jin Qian6df3bc62017-10-18 17:52:14 -0700136 void add_user_ce(userid_t user_id);
137 void remove_user_ce(userid_t user_id);
Jin Qiana8533322017-10-13 18:15:34 -0700138
Yifan Hongbf2dcb22017-09-27 14:01:30 -0700139 virtual ::android::hardware::Return<void> healthInfoChanged(
Hridya Valsaraju79d38eb2018-01-17 23:02:56 -0800140 const ::android::hardware::health::V2_0::HealthInfo& info);
Yifan Hongbf2dcb22017-09-27 14:01:30 -0700141 void serviceDied(uint64_t cookie, const wp<::android::hidl::base::V1_0::IBase>& who);
Jin Qian8847c622017-07-17 15:06:11 -0700142
143 void report_storage_info();
Jin Qiand691d6e2017-09-28 16:02:22 -0700144
Jin Qian94b64ef2017-11-09 15:07:18 -0800145 void flush_protos(unordered_map<int, StoragedProto>* protos);
ynwang62cb3722016-06-17 14:30:48 -0700146};
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 Qianbcd6e3b2016-12-28 15:43:51 -0800152#define EVENTLOGTAG_UID_IO_ALERT ( 2734 )
ynwang62cb3722016-06-17 14:30:48 -0700153
154#endif /* _STORAGED_H_ */