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 package com.android.car.storagemonitoring; 17 18 import android.annotation.Nullable; 19 import android.car.storagemonitoring.UidIoRecord; 20 import android.util.Slog; 21 import android.util.SparseArray; 22 23 import com.android.car.CarLog; 24 import com.android.internal.annotations.VisibleForTesting; 25 26 import java.io.File; 27 import java.io.IOException; 28 import java.nio.file.Files; 29 import java.nio.file.Path; 30 import java.util.List; 31 import java.util.Objects; 32 import java.util.StringTokenizer; 33 34 /** 35 * Loads I/O stats from procfs 36 * 37 * The Android kernel can be configured to provide uid I/O stats in /proc/uid_io/stats. 38 */ 39 public class ProcfsUidIoStatsProvider implements UidIoStatsProvider { 40 private static Path DEFAULT_STATS_FILE = new File("/proc/uid_io/stats").toPath(); 41 42 private final Path mStatsFile; 43 ProcfsUidIoStatsProvider()44 public ProcfsUidIoStatsProvider() { 45 this(DEFAULT_STATS_FILE); 46 } 47 48 @VisibleForTesting ProcfsUidIoStatsProvider(Path statsFile)49 ProcfsUidIoStatsProvider(Path statsFile) { 50 mStatsFile = Objects.requireNonNull(statsFile); 51 } 52 53 @Nullable 54 @Override load()55 public SparseArray<UidIoRecord> load() { 56 List<String> lines; 57 SparseArray<UidIoRecord> result = new SparseArray<>(); 58 try { 59 lines = Files.readAllLines(mStatsFile); 60 } catch (IOException e) { 61 Slog.w(CarLog.TAG_STORAGE, "can't read I/O stats from " + mStatsFile, e); 62 return null; 63 } 64 65 for (String line : lines) { 66 StringTokenizer tokenizer = new StringTokenizer(line); 67 if (tokenizer.countTokens() != 11) { 68 Slog.w(CarLog.TAG_STORAGE, "malformed I/O stats entry: " + line); 69 return null; 70 } 71 72 try { 73 int uid = Integer.valueOf(tokenizer.nextToken()); 74 long foreground_rchar = Long.valueOf(tokenizer.nextToken()); 75 long foreground_wchar = Long.valueOf(tokenizer.nextToken()); 76 long foreground_read_bytes = Long.valueOf(tokenizer.nextToken()); 77 long foreground_write_bytes = Long.valueOf(tokenizer.nextToken()); 78 long background_rchar = Long.valueOf(tokenizer.nextToken()); 79 long background_wchar = Long.valueOf(tokenizer.nextToken()); 80 long background_read_bytes = Long.valueOf(tokenizer.nextToken()); 81 long background_write_bytes = Long.valueOf(tokenizer.nextToken()); 82 long foreground_fsync = Long.valueOf(tokenizer.nextToken()); 83 long background_fsync = Long.valueOf(tokenizer.nextToken()); 84 85 result.append(uid, new UidIoRecord(uid, 86 foreground_rchar, 87 foreground_wchar, 88 foreground_read_bytes, 89 foreground_write_bytes, 90 foreground_fsync, 91 background_rchar, 92 background_wchar, 93 background_read_bytes, 94 background_write_bytes, 95 background_fsync)); 96 97 } catch (NumberFormatException e) { 98 Slog.w(CarLog.TAG_STORAGE, "malformed I/O stats entry: " + line, e); 99 return null; 100 } 101 } 102 103 return result; 104 } 105 } 106 107 108