1 /*
2  * Copyright (C) 2017 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_INFO_H_
18 #define _STORAGED_INFO_H_
19 
20 #include <string.h>
21 
22 #include <chrono>
23 
24 #include <android/hardware/health/2.0/IHealth.h>
25 #include <utils/Mutex.h>
26 
27 #include "storaged.h"
28 #include "storaged.pb.h"
29 
30 #define FRIEND_TEST(test_case_name, test_name) \
31 friend class test_case_name##_##test_name##_Test
32 
33 using namespace std;
34 using namespace android;
35 using namespace chrono;
36 using namespace storaged_proto;
37 
38 class storage_info_t {
39   protected:
40     FRIEND_TEST(storaged_test, storage_info_t);
41     FRIEND_TEST(storaged_test, storage_info_t_proto);
42     // emmc lifetime
43     uint16_t eol;                   // pre-eol (end of life) information
44     uint16_t lifetime_a;            // device life time estimation (type A)
45     uint16_t lifetime_b;            // device life time estimation (type B)
46     string version;                 // version string
47     // free space
48     const string userdata_path = "/data";
49     uint64_t userdata_total_kb;
50     uint64_t userdata_free_kb;
51     // io perf history
52     time_point<system_clock> day_start_tp;
53     vector<uint32_t> recent_perf;
54     uint32_t nr_samples;
55     vector<uint32_t> daily_perf;
56     uint32_t nr_days;
57     vector<uint32_t> weekly_perf;
58     uint32_t nr_weeks;
59     Mutex si_mutex;
60 
storage_info_t()61     storage_info_t() : eol(0), lifetime_a(0), lifetime_b(0),
62         userdata_total_kb(0), userdata_free_kb(0), nr_samples(0),
63         daily_perf(WEEK_TO_DAYS, 0), nr_days(0),
64         weekly_perf(YEAR_TO_WEEKS, 0), nr_weeks(0) {
65             day_start_tp = system_clock::now();
66             day_start_tp -= chrono::seconds(duration_cast<chrono::seconds>(
67                 day_start_tp.time_since_epoch()).count() % DAY_TO_SEC);
68     }
69     void publish();
70     storage_info_t* s_info;
71 
72   public:
73     static storage_info_t* get_storage_info(
74         const sp<android::hardware::health::V2_0::IHealth>& healthService);
~storage_info_t()75     virtual ~storage_info_t() {};
report()76     virtual void report() {};
77     void load_perf_history_proto(const IOPerfHistory& perf_history);
78     void refresh(IOPerfHistory* perf_history);
79     void update_perf_history(uint32_t bw,
80                              const time_point<system_clock>& tp);
81     vector<int> get_perf_history();
82     uint32_t get_recent_perf();
83 };
84 
85 class emmc_info_t : public storage_info_t {
86 private:
87     bool report_sysfs();
88     bool report_debugfs();
89 public:
90     static const string emmc_sysfs;
91     static const string emmc_debugfs;
92     static const char* emmc_ver_str[];
93 
~emmc_info_t()94     virtual ~emmc_info_t() {}
95     virtual void report();
96 };
97 
98 class ufs_info_t : public storage_info_t {
99 public:
100     static const string health_file;
101 
~ufs_info_t()102     virtual ~ufs_info_t() {}
103     virtual void report();
104 };
105 
106 class health_storage_info_t : public storage_info_t {
107   private:
108     using IHealth = hardware::health::V2_0::IHealth;
109     using StorageInfo = hardware::health::V2_0::StorageInfo;
110 
111     sp<IHealth> mHealth;
112     void set_values_from_hal_storage_info(const StorageInfo& halInfo);
113 
114   public:
health_storage_info_t(const sp<IHealth> & service)115     health_storage_info_t(const sp<IHealth>& service) : mHealth(service){};
~health_storage_info_t()116     virtual ~health_storage_info_t() {}
117     virtual void report();
118 };
119 
120 #endif /* _STORAGED_INFO_H_ */
121