blob: 4fd4bc9dc699717e179508706878955acb64e4d9 [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#define LOG_TAG "storaged"
18
ynwang62cb3722016-06-17 14:30:48 -070019#include <dirent.h>
ynwangaf49d972016-06-17 14:30:48 -070020#include <fcntl.h>
Yang Jin3906c892017-06-22 15:18:21 -070021#include <inttypes.h>
ynwangaf49d972016-06-17 14:30:48 -070022#include <linux/time.h>
ynwang62cb3722016-06-17 14:30:48 -070023#include <stdint.h>
ynwangaf49d972016-06-17 14:30:48 -070024#include <stdio.h>
ynwang62cb3722016-06-17 14:30:48 -070025#include <stdlib.h>
26#include <string.h>
ynwangaf49d972016-06-17 14:30:48 -070027#include <sys/stat.h>
28#include <time.h>
ynwang62cb3722016-06-17 14:30:48 -070029#include <unistd.h>
30
Jin Qian535ddbe2017-01-11 17:19:21 -080031#include <iomanip>
ynwang62cb3722016-06-17 14:30:48 -070032#include <sstream>
33#include <string>
34#include <unordered_map>
35
36#include <android-base/file.h>
ynwangaf49d972016-06-17 14:30:48 -070037#include <android-base/logging.h>
ynwang62cb3722016-06-17 14:30:48 -070038#include <android-base/stringprintf.h>
39#include <android-base/strings.h>
ynwang62cb3722016-06-17 14:30:48 -070040#include <log/log_event_list.h>
ynwang62cb3722016-06-17 14:30:48 -070041
42#include <storaged.h>
43#include <storaged_utils.h>
44
Jin Qianb049d182017-10-12 17:02:17 -070045bool cmp_uid_info(const UidInfo& l, const UidInfo& r) {
Jin Qianbcd6e3b2016-12-28 15:43:51 -080046 // Compare background I/O first.
Jin Qian5b962c62017-01-30 14:48:38 -080047 for (int i = UID_STATS - 1; i >= 0; i--) {
Jin Qianbcd6e3b2016-12-28 15:43:51 -080048 uint64_t l_bytes = l.io[i].read_bytes + l.io[i].write_bytes;
49 uint64_t r_bytes = r.io[i].read_bytes + r.io[i].write_bytes;
50 uint64_t l_chars = l.io[i].rchar + l.io[i].wchar;
51 uint64_t r_chars = r.io[i].rchar + r.io[i].wchar;
52
53 if (l_bytes != r_bytes) {
54 return l_bytes > r_bytes;
55 }
56 if (l_chars != r_chars) {
57 return l_chars > r_chars;
58 }
59 }
60
61 return l.name < r.name;
62}
63
Jin Qianb049d182017-10-12 17:02:17 -070064void sort_running_uids_info(std::vector<UidInfo> &uids) {
Jin Qianbcd6e3b2016-12-28 15:43:51 -080065 std::sort(uids.begin(), uids.end(), cmp_uid_info);
66}
67
68// Logging functions
Jin Qianb049d182017-10-12 17:02:17 -070069void log_console_running_uids_info(const std::vector<UidInfo>& uids, bool flag_dump_task) {
Jin Qian4bcc8b62017-03-14 12:40:06 -070070 printf("name/uid fg_rchar fg_wchar fg_rbytes fg_wbytes "
71 "bg_rchar bg_wchar bg_rbytes bg_wbytes fg_fsync bg_fsync\n");
Jin Qianbcd6e3b2016-12-28 15:43:51 -080072
73 for (const auto& uid : uids) {
Yang Jin3906c892017-06-22 15:18:21 -070074 printf("%s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
75 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
76 uid.name.c_str(),
Jin Qianbcd6e3b2016-12-28 15:43:51 -080077 uid.io[0].rchar, uid.io[0].wchar, uid.io[0].read_bytes, uid.io[0].write_bytes,
Jin Qiane83a6102017-03-02 16:16:54 -080078 uid.io[1].rchar, uid.io[1].wchar, uid.io[1].read_bytes, uid.io[1].write_bytes,
79 uid.io[0].fsync, uid.io[1].fsync);
Yang Jin3906c892017-06-22 15:18:21 -070080 if (flag_dump_task) {
81 for (const auto& task_it : uid.tasks) {
Jin Qianb049d182017-10-12 17:02:17 -070082 const task_info& task = task_it.second;
Yang Jin3906c892017-06-22 15:18:21 -070083 printf("-> %s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
84 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
85 task.comm.c_str(),
86 task.io[0].rchar, task.io[0].wchar, task.io[0].read_bytes, task.io[0].write_bytes,
87 task.io[1].rchar, task.io[1].wchar, task.io[1].read_bytes, task.io[1].write_bytes,
88 task.io[0].fsync, task.io[1].fsync);
89 }
90 }
Jin Qianbcd6e3b2016-12-28 15:43:51 -080091 }
92 fflush(stdout);
93}
Jin Qiand691d6e2017-09-28 16:02:22 -070094
Jin Qianb049d182017-10-12 17:02:17 -070095void log_console_perf_history(const vector<int>& perf_history) {
96 if (perf_history.size() < 3 ||
97 perf_history.size() != perf_history[0] +
98 perf_history[1] +
99 perf_history[2] + (size_t)3) {
Jin Qiand691d6e2017-09-28 16:02:22 -0700100 return;
101 }
102
103 printf("\nI/O perf history (KB/s) : most_recent <--------- least_recent \n");
104
105 std::stringstream line;
Jin Qianb049d182017-10-12 17:02:17 -0700106 int start = 3;
107 int end = 3 + perf_history[0];
108 std::copy(perf_history.begin() + start, perf_history.begin() + end,
Jin Qiand691d6e2017-09-28 16:02:22 -0700109 std::ostream_iterator<int>(line, " "));
110 printf("last 24 hours : %s\n", line.str().c_str());
111
112 line.str("");
Jin Qianb049d182017-10-12 17:02:17 -0700113 start = end;
114 end += perf_history[1];
115 std::copy(perf_history.begin() + start, perf_history.begin() + end,
Jin Qiand691d6e2017-09-28 16:02:22 -0700116 std::ostream_iterator<int>(line, " "));
117 printf("last 7 days : %s\n", line.str().c_str());
118
119 line.str("");
Jin Qianb049d182017-10-12 17:02:17 -0700120 start = end;
121 std::copy(perf_history.begin() + start, perf_history.end(),
Jin Qiand691d6e2017-09-28 16:02:22 -0700122 std::ostream_iterator<int>(line, " "));
123 printf("last 52 weeks : %s\n", line.str().c_str());
Jin Qian6df3bc62017-10-18 17:52:14 -0700124}
125
126map<string, io_usage> merge_io_usage(const vector<uid_record>& entries) {
127 map<string, io_usage> merged_entries;
128 for (const auto& record : entries) {
129 merged_entries[record.name] += record.ios.uid_ios;
130 }
131 return merged_entries;
132}