1 /*
2 * Copyright 2023 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 #include <dump/pixel_dump.h>
18 #include <android-base/file.h>
19 #include <android-base/stringprintf.h>
20 #include <android-base/properties.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <log/log.h>
24 #include <regex>
25 #include <fstream>
26 #include <map>
27
28 #define F2FS_FSCK_TIME_PROPERTY "ro.boottime.init.fsck.data"
29 #define F2FS_MNT_TIME_PROPERTY "ro.boottime.init.mount.data"
30 #define BOOTDEVICE_PROPERTY "ro.boot.bootdevice"
31 #define BUILD_TYPE_PROPERTY "ro.build.type"
32
read_buffer(int buf_id,int total_len,const char * path)33 void read_buffer(int buf_id, int total_len, const char* path)
34 {
35 int i, len;
36 system("mkdir -m 0770 -p /data/vendor/storage/");
37 auto cmd = android::base::StringPrintf("rm -f /data/vendor/storage/%s\n", path);
38 system(cmd.c_str());
39
40 for (i=0;i<=total_len;i+=524288) {
41 if (total_len-i < 524288) {
42 len = total_len-i;
43 printf("/vendor/bin/sg_read_buffer -m 0x1c -i %d -l %d -o %d -r /dev/sg3 >> /data/vendor/storage/%s\n",
44 buf_id, len, i, path);
45 auto cmd = android::base::StringPrintf(
46 "/vendor/bin/sg_read_buffer -m 0x1c -i %d -l %d -o %d -r /dev/sg3 >> /data/vendor/storage/%s\n",
47 buf_id, len, i, path);
48 system(cmd.c_str());
49 break;
50 }
51 len = 524288;
52 printf("/vendor/bin/sg_read_buffer -m 0x1c -i %d -l %d -o %d -r /dev/sg3 >> /data/vendor/storage/%s\n",
53 buf_id, len, i, path);
54 auto cmd = android::base::StringPrintf(
55 "/vendor/bin/sg_read_buffer -m 0x1c -i %d -l %d -o %d -r /dev/sg3 >> /data/vendor/storage/%s\n",
56 buf_id, len, i, path);
57 system(cmd.c_str());
58 }
59 }
60
main()61 int main() {
62 //F2FS
63 dumpFileContent("F2FS status", "/sys/kernel/debug/f2fs/status");
64 dumpFileContent("F2FS fsck", "/dev/fscklogs/fsck");
65
66 int fsck_time = android::base::GetIntProperty(F2FS_FSCK_TIME_PROPERTY, 0);
67 printf("--- F2FS - fsck time (ms) ---\n%d\n\n", fsck_time);
68 int mnt_time = android::base::GetIntProperty(F2FS_MNT_TIME_PROPERTY, 0);
69 printf("--- F2FS - checkpoint=disable time (ms) ---\n%d\n\n", mnt_time);
70
71 //UFS
72 dumpFileContent("UFS model", "/sys/block/sda/device/model");
73 dumpFileContent("UFS rev", "/sys/block/sda/device/rev");
74 dumpFileContent("UFS size", "/sys/block/sda/size");
75
76 dumpFileContent("UFS Slow IO Read",
77 "/dev/sys/block/bootdevice/slowio_read_cnt");
78 dumpFileContent("UFS Slow IO Write",
79 "/dev/sys/block/bootdevice/slowio_write_cnt");
80 dumpFileContent("UFS Slow IO Unmap",
81 "/dev/sys/block/bootdevice/slowio_unmap_cnt");
82 dumpFileContent("UFS Slow IO Sync",
83 "/dev/sys/block/bootdevice/slowio_sync_cnt");
84
85 printf("------ UFS err_stats ------\n");
86 const std::string ufs_err_stats_path(
87 "/dev/sys/block/bootdevice/err_stats/");
88 std::unique_ptr<DIR, decltype(&closedir)> statdir(
89 opendir(ufs_err_stats_path.c_str()), closedir);
90 if (statdir) {
91 dirent *stat_entry;
92 while ((stat_entry = readdir(statdir.get())) != nullptr) {
93 std::string ufs_err_stats_path(stat_entry->d_name);
94 if (!strcmp(ufs_err_stats_path.c_str(), ".")
95 || !strcmp(ufs_err_stats_path.c_str(), ".."))
96 continue;
97 std::string bootdevice = android::base::GetProperty(
98 BOOTDEVICE_PROPERTY, "");
99 std::string err_stat_path = "/sys/devices/platform/";
100 err_stat_path.append(bootdevice.c_str());
101 err_stat_path.append("/err_stats/");
102 err_stat_path.append(ufs_err_stats_path.c_str());
103 std::ifstream err_stat_file(err_stat_path);
104 if (err_stat_file.is_open()) {
105 std::string err_stat_atom;
106 err_stat_file >> err_stat_atom;
107 printf("%s:%s\n", ufs_err_stats_path.c_str(),
108 err_stat_atom.c_str());
109 err_stat_file.close();
110 }
111 }
112 }
113
114 printf("\n------ UFS io_stats ------\n");
115 printf("\t\t%-12s %-12s %-12s %-12s %-12s %-12s\n", "ReadCnt", "ReadBytes",
116 "WriteCnt", "WriteBytes", "RWCnt", "RWBytes");
117 const std::string ufs_io_stats = "/dev/sys/block/bootdevice/io_stats/";
118 const std::string iostats_seq[3] = {"_start", "_complete", "_maxdiff"};
119 const std::string iostats[6] = {"rcnt", "rbyte", "wcnt", "wbyte", "rwcnt",
120 "rwbyte"};
121
122 for (int i = 0; i < 3; i++) {
123 const std::string header[3] = {"Started: \t", "Completed: \t",
124 "MaxDiff: \t"};
125 printf("%s", header[i].c_str());
126 for (int j = 0; j < 6; j++) {
127 std::string iostat_path = ufs_io_stats.c_str();
128 iostat_path.append(iostats[j].c_str());
129 iostat_path.append(iostats_seq[i].c_str());
130 std::ifstream io_stats_file(iostat_path.c_str());
131 unsigned long io_stat_atom = 0;
132 if (io_stats_file.is_open()) {
133 io_stats_file >> io_stat_atom;
134 printf("%-12lu ", io_stat_atom);
135 io_stats_file.close();
136 }
137 }
138 printf("\n");
139 }
140
141 printf("\n------ UFS req_stats ------\n");
142 printf("\t%-12s %-12s %-12s %-12s %-12s %-12s\n", "All", "Write", "Read",
143 "Security", "Flush", "Discard");
144 const std::string ufs_req_stats = "/dev/sys/block/bootdevice/req_stats/";
145 const std::string reqstats_seq[4] = {"_min", "_max", "_avg", "_sum"};
146 const std::string reqstats[6] = {"all", "write", "read", "security",
147 "flush", "discard"};
148
149 for (int i = 0; i < 4; i++) {
150 const std::string header[4] = {"Min:\t", "Max: \t", "Avg: \t",
151 "Count: \t"};
152 printf("%s", header[i].c_str());
153 for (int j = 0; j < 6; j++) {
154 std::string reqstat_path = ufs_req_stats.c_str();
155 reqstat_path.append(reqstats[j].c_str());
156 reqstat_path.append(reqstats_seq[i].c_str());
157 std::ifstream req_stats_file(reqstat_path.c_str());
158 unsigned long req_stat_atom = 0;
159 if (req_stats_file.is_open()) {
160 req_stats_file >> req_stat_atom;
161 printf("%-12lu ", req_stat_atom);
162 req_stats_file.close();
163 }
164 }
165 printf("\n");
166 }
167
168 printf("\n------ UFS health ------\n");
169 dumpFileContent(
170 "",
171 "/dev/sys/block/bootdevice/health_descriptor/life_time_estimation_a");
172 dumpFileContent(
173 "",
174 "/dev/sys/block/bootdevice/health_descriptor/life_time_estimation_b");
175 dumpFileContent("", "/dev/sys/block/bootdevice/health_descriptor/eol_info");
176 dumpFileContent(
177 "",
178 "/dev/sys/block/bootdevice/health_descriptor/life_time_estimation_c");
179
180 printf("\n------ UFS error history ------\n");
181 std::string build_type = android::base::GetProperty(BUILD_TYPE_PROPERTY, "");
182 if (build_type == "userdebug") {
183 std::string sg_read_buffer = "/vendor/bin/sg_read_buffer";
184 std::ifstream sg_read_buffer_file(sg_read_buffer.c_str());
185 if (sg_read_buffer_file.is_open()) {
186 const std::string ufs_ver_path(
187 "/dev/sys/block/bootdevice/device_descriptor/specification_version");
188 std::ifstream ufs_ver_file(ufs_ver_path);
189 std::string ufs_ver;
190 if (ufs_ver_file.is_open()) {
191 ufs_ver_file >> ufs_ver;
192 ufs_ver_file.close();
193 }
194 if (strcmp(ufs_ver.c_str(), "0x0210")) {
195 const std::string ufs_brand_path("/sys/block/sda/device/vendor");
196 std::ifstream ufs_brand_file(ufs_brand_path);
197 std::string ufs_brand;
198 if (ufs_brand_file.is_open()) {
199 ufs_brand_file >> ufs_brand;
200 ufs_brand_file.close();
201 }
202
203 std::map<std::string, int> const table =
204 {
205 {"MICRON", 0x12C},
206 {"KIOXIA", 0x198},
207 {"SKhynix", 0x1AD},
208 {"SAMSUNG", 0x1CE}
209 };
210 auto ufs_vendor_id = table.find(ufs_brand);
211 if (ufs_vendor_id != table.end()) {
212 switch(ufs_vendor_id->second){
213 case 0x12C: //MICRON
214 read_buffer(16, 2097152, "micron_10_ufs_err_history.dat");
215 read_buffer(18, 10485760, "micron_12_ufs_err_history.dat");
216 read_buffer(19, 10485760, "micron_13_ufs_err_history.dat");
217 break;
218 case 0x198: //KIOXIA
219 read_buffer(16, 16773120, "kioxia_10_ufs_err_history.dat");
220 read_buffer(17, 2097152, "kioxia_11_ufs_err_history.dat");
221 read_buffer(18, 131072, "kioxia_12_ufs_err_history.dat");
222 break;
223 case 0x1AD: //SKhynix
224 read_buffer(0, 4096, "hynix_00_ufs_err_history.dat");
225 read_buffer(16, 131072, "skhynix_10_ufs_err_history.dat");
226 read_buffer(17, 131072, "skhynix_11_ufs_err_history.dat");
227 read_buffer(18, 131072, "skhynix_12_ufs_err_history.dat");
228 read_buffer(19, 131072, "skhynix_13_ufs_err_history.dat");
229 break;
230 case 0x1CE: //SAMSUNG
231 read_buffer(16, 8404992, "samsung_10_ufs_err_history.dat");
232 break;
233 default:
234 break;
235 }
236 }
237 sg_read_buffer_file.close();
238 }
239 } else
240 printf("sg_read_buffer does not exist\n");
241 }
242
243 return 0;
244 }
245