storaged_utils.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #define LOG_TAG "storaged"
  17. #include <dirent.h>
  18. #include <fcntl.h>
  19. #include <inttypes.h>
  20. #include <linux/time.h>
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/stat.h>
  26. #include <time.h>
  27. #include <unistd.h>
  28. #include <iomanip>
  29. #include <sstream>
  30. #include <string>
  31. #include <unordered_map>
  32. #include <android-base/file.h>
  33. #include <android-base/logging.h>
  34. #include <android-base/stringprintf.h>
  35. #include <android-base/strings.h>
  36. #include <log/log_event_list.h>
  37. #include <storaged.h>
  38. #include <storaged_utils.h>
  39. bool cmp_uid_info(const UidInfo& l, const UidInfo& r) {
  40. // Compare background I/O first.
  41. for (int i = UID_STATS - 1; i >= 0; i--) {
  42. uint64_t l_bytes = l.io[i].read_bytes + l.io[i].write_bytes;
  43. uint64_t r_bytes = r.io[i].read_bytes + r.io[i].write_bytes;
  44. uint64_t l_chars = l.io[i].rchar + l.io[i].wchar;
  45. uint64_t r_chars = r.io[i].rchar + r.io[i].wchar;
  46. if (l_bytes != r_bytes) {
  47. return l_bytes > r_bytes;
  48. }
  49. if (l_chars != r_chars) {
  50. return l_chars > r_chars;
  51. }
  52. }
  53. return l.name < r.name;
  54. }
  55. void sort_running_uids_info(std::vector<UidInfo> &uids) {
  56. std::sort(uids.begin(), uids.end(), cmp_uid_info);
  57. }
  58. // Logging functions
  59. void log_console_running_uids_info(const std::vector<UidInfo>& uids, bool flag_dump_task) {
  60. printf("name/uid fg_rchar fg_wchar fg_rbytes fg_wbytes "
  61. "bg_rchar bg_wchar bg_rbytes bg_wbytes fg_fsync bg_fsync\n");
  62. for (const auto& uid : uids) {
  63. printf("%s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
  64. " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
  65. uid.name.c_str(),
  66. uid.io[0].rchar, uid.io[0].wchar, uid.io[0].read_bytes, uid.io[0].write_bytes,
  67. uid.io[1].rchar, uid.io[1].wchar, uid.io[1].read_bytes, uid.io[1].write_bytes,
  68. uid.io[0].fsync, uid.io[1].fsync);
  69. if (flag_dump_task) {
  70. for (const auto& task_it : uid.tasks) {
  71. const task_info& task = task_it.second;
  72. printf("-> %s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
  73. " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
  74. task.comm.c_str(),
  75. task.io[0].rchar, task.io[0].wchar, task.io[0].read_bytes, task.io[0].write_bytes,
  76. task.io[1].rchar, task.io[1].wchar, task.io[1].read_bytes, task.io[1].write_bytes,
  77. task.io[0].fsync, task.io[1].fsync);
  78. }
  79. }
  80. }
  81. fflush(stdout);
  82. }
  83. void log_console_perf_history(const vector<int>& perf_history) {
  84. if (perf_history.size() < 3 ||
  85. perf_history.size() != perf_history[0] +
  86. perf_history[1] +
  87. perf_history[2] + (size_t)3) {
  88. return;
  89. }
  90. printf("\nI/O perf history (KB/s) : most_recent <--------- least_recent \n");
  91. std::stringstream line;
  92. int start = 3;
  93. int end = 3 + perf_history[0];
  94. std::copy(perf_history.begin() + start, perf_history.begin() + end,
  95. std::ostream_iterator<int>(line, " "));
  96. printf("last 24 hours : %s\n", line.str().c_str());
  97. line.str("");
  98. start = end;
  99. end += perf_history[1];
  100. std::copy(perf_history.begin() + start, perf_history.begin() + end,
  101. std::ostream_iterator<int>(line, " "));
  102. printf("last 7 days : %s\n", line.str().c_str());
  103. line.str("");
  104. start = end;
  105. std::copy(perf_history.begin() + start, perf_history.end(),
  106. std::ostream_iterator<int>(line, " "));
  107. printf("last 52 weeks : %s\n", line.str().c_str());
  108. }
  109. map<string, io_usage> merge_io_usage(const vector<uid_record>& entries) {
  110. map<string, io_usage> merged_entries;
  111. for (const auto& record : entries) {
  112. merged_entries[record.name] += record.ios.uid_ios;
  113. }
  114. return merged_entries;
  115. }