1 /* 2 * Copyright (C) 2015 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 package android.os; 18 19 import android.util.Log; 20 import android.util.Size; 21 import com.android.internal.util.FastPrintWriter; 22 23 import java.io.FileOutputStream; 24 import java.io.PrintWriter; 25 import java.util.HashMap; 26 import java.util.Map; 27 28 /** 29 * Class used to track binder transactions. It indexes the transactions by the stack trace. 30 * 31 * @hide 32 */ 33 public class TransactionTracker { 34 private Map<String, Long> mTraces; 35 resetTraces()36 private void resetTraces() { 37 synchronized (this) { 38 mTraces = new HashMap<String, Long>(); 39 } 40 } 41 TransactionTracker()42 TransactionTracker() { 43 resetTraces(); 44 } 45 addTrace(Throwable tr)46 public void addTrace(Throwable tr) { 47 String trace = Log.getStackTraceString(tr); 48 synchronized (this) { 49 if (mTraces.containsKey(trace)) { 50 mTraces.put(trace, mTraces.get(trace) + 1); 51 } else { 52 mTraces.put(trace, Long.valueOf(1)); 53 } 54 } 55 } 56 writeTracesToFile(ParcelFileDescriptor fd)57 public void writeTracesToFile(ParcelFileDescriptor fd) { 58 if (mTraces.isEmpty()) { 59 return; 60 } 61 62 PrintWriter pw = new FastPrintWriter(new FileOutputStream(fd.getFileDescriptor())); 63 synchronized (this) { 64 for (String trace : mTraces.keySet()) { 65 pw.println("Count: " + mTraces.get(trace)); 66 pw.println("Trace: " + trace); 67 pw.println(); 68 } 69 } 70 pw.flush(); 71 } 72 clearTraces()73 public void clearTraces(){ 74 resetTraces(); 75 } 76 } 77