storaged.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #ifndef _STORAGED_H_
  17. #define _STORAGED_H_
  18. #include <semaphore.h>
  19. #include <stdint.h>
  20. #include <time.h>
  21. #include <queue>
  22. #include <string>
  23. #include <unordered_map>
  24. #include <vector>
  25. #include <utils/Mutex.h>
  26. #include <android/hardware/health/2.0/IHealth.h>
  27. #define FRIEND_TEST(test_case_name, test_name) \
  28. friend class test_case_name##_##test_name##_Test
  29. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  30. #define IS_ALIGNED(x, align) (!((x) & ((align) - 1)))
  31. #define ROUND_UP(x, align) (((x) + ((align) - 1)) & ~((align) - 1))
  32. #define SECTOR_SIZE ( 512 )
  33. #define SEC_TO_MSEC ( 1000 )
  34. #define MSEC_TO_USEC ( 1000 )
  35. #define USEC_TO_NSEC ( 1000 )
  36. #define SEC_TO_USEC ( 1000000 )
  37. #define HOUR_TO_SEC ( 3600 )
  38. #define DAY_TO_SEC ( 3600 * 24 )
  39. #define WEEK_TO_DAYS ( 7 )
  40. #define YEAR_TO_WEEKS ( 52 )
  41. #include "storaged_diskstats.h"
  42. #include "storaged_info.h"
  43. #include "storaged_uid_monitor.h"
  44. #include "storaged.pb.h"
  45. #include "uid_info.h"
  46. using namespace std;
  47. using namespace android;
  48. // Periodic chores intervals in seconds
  49. #define DEFAULT_PERIODIC_CHORES_INTERVAL_UNIT ( 60 )
  50. #define DEFAULT_PERIODIC_CHORES_INTERVAL_DISK_STATS_PUBLISH ( 3600 )
  51. #define DEFAULT_PERIODIC_CHORES_INTERVAL_UID_IO ( 3600 )
  52. #define DEFAULT_PERIODIC_CHORES_INTERVAL_UID_IO_LIMIT ( 300 )
  53. #define DEFAULT_PERIODIC_CHORES_INTERVAL_FLUSH_PROTO ( 3600 )
  54. // UID IO threshold in bytes
  55. #define DEFAULT_PERIODIC_CHORES_UID_IO_THRESHOLD ( 1024 * 1024 * 1024ULL )
  56. struct storaged_config {
  57. int periodic_chores_interval_unit;
  58. int periodic_chores_interval_disk_stats_publish;
  59. int periodic_chores_interval_uid_io;
  60. int periodic_chores_interval_flush_proto;
  61. int event_time_check_usec; // check how much cputime spent in event loop
  62. };
  63. class storaged_t : public android::hardware::health::V2_0::IHealthInfoCallback,
  64. public android::hardware::hidl_death_recipient {
  65. private:
  66. time_t mTimer;
  67. storaged_config mConfig;
  68. unique_ptr<disk_stats_monitor> mDsm;
  69. uid_monitor mUidm;
  70. time_t mStarttime;
  71. sp<android::hardware::health::V2_0::IHealth> health;
  72. unique_ptr<storage_info_t> storage_info;
  73. static const uint32_t current_version;
  74. unordered_map<userid_t, bool> proto_loaded;
  75. void load_proto(userid_t user_id);
  76. char* prepare_proto(userid_t user_id, StoragedProto* proto);
  77. void flush_proto(userid_t user_id, StoragedProto* proto);
  78. void flush_proto_data(userid_t user_id, const char* data, ssize_t size);
  79. string proto_path(userid_t user_id) {
  80. return string("/data/misc_ce/") + to_string(user_id) +
  81. "/storaged/storaged.proto";
  82. }
  83. void init_health_service();
  84. public:
  85. storaged_t(void);
  86. void init(void);
  87. void event(void);
  88. void event_checked(void);
  89. void pause(void) {
  90. sleep(mConfig.periodic_chores_interval_unit);
  91. }
  92. time_t get_starttime(void) {
  93. return mStarttime;
  94. }
  95. unordered_map<uint32_t, uid_info> get_uids(void) {
  96. return mUidm.get_uid_io_stats();
  97. }
  98. vector<int> get_perf_history(void) {
  99. return storage_info->get_perf_history();
  100. }
  101. uint32_t get_recent_perf(void) { return storage_info->get_recent_perf(); }
  102. map<uint64_t, struct uid_records> get_uid_records(
  103. double hours, uint64_t threshold, bool force_report) {
  104. return mUidm.dump(hours, threshold, force_report);
  105. }
  106. void update_uid_io_interval(int interval) {
  107. if (interval >= DEFAULT_PERIODIC_CHORES_INTERVAL_UID_IO_LIMIT) {
  108. mConfig.periodic_chores_interval_uid_io = interval;
  109. }
  110. }
  111. void add_user_ce(userid_t user_id);
  112. void remove_user_ce(userid_t user_id);
  113. virtual ::android::hardware::Return<void> healthInfoChanged(
  114. const ::android::hardware::health::V2_0::HealthInfo& info);
  115. void serviceDied(uint64_t cookie, const wp<::android::hidl::base::V1_0::IBase>& who);
  116. void report_storage_info();
  117. void flush_protos(unordered_map<int, StoragedProto>* protos);
  118. };
  119. // Eventlog tag
  120. // The content must match the definition in EventLogTags.logtags
  121. #define EVENTLOGTAG_DISKSTATS ( 2732 )
  122. #define EVENTLOGTAG_EMMCINFO ( 2733 )
  123. #define EVENTLOGTAG_UID_IO_ALERT ( 2734 )
  124. #endif /* _STORAGED_H_ */