1 /* 2 * Copyright (C) 2019 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 com.android.systemui.tracing; 18 19 import static com.android.systemui.tracing.nano.SystemUiTraceFileProto.MAGIC_NUMBER_H; 20 import static com.android.systemui.tracing.nano.SystemUiTraceFileProto.MAGIC_NUMBER_L; 21 22 import android.content.Context; 23 import android.os.SystemClock; 24 25 import androidx.annotation.NonNull; 26 27 import com.android.systemui.Dumpable; 28 import com.android.systemui.dagger.SysUISingleton; 29 import com.android.systemui.dump.DumpManager; 30 import com.android.systemui.shared.tracing.FrameProtoTracer; 31 import com.android.systemui.shared.tracing.FrameProtoTracer.ProtoTraceParams; 32 import com.android.systemui.shared.tracing.ProtoTraceable; 33 import com.android.systemui.tracing.nano.SystemUiTraceEntryProto; 34 import com.android.systemui.tracing.nano.SystemUiTraceFileProto; 35 import com.android.systemui.tracing.nano.SystemUiTraceProto; 36 37 import com.google.protobuf.nano.MessageNano; 38 39 import java.io.File; 40 import java.io.PrintWriter; 41 import java.util.ArrayList; 42 import java.util.Queue; 43 44 import javax.inject.Inject; 45 46 /** 47 * Controller for coordinating winscope proto tracing. 48 */ 49 @SysUISingleton 50 public class ProtoTracer implements 51 Dumpable, 52 ProtoTraceParams< 53 MessageNano, 54 SystemUiTraceFileProto, 55 SystemUiTraceEntryProto, 56 SystemUiTraceProto> { 57 58 private static final String TAG = "ProtoTracer"; 59 private static final long MAGIC_NUMBER_VALUE = ((long) MAGIC_NUMBER_H << 32) | MAGIC_NUMBER_L; 60 61 private final Context mContext; 62 private final FrameProtoTracer<MessageNano, SystemUiTraceFileProto, SystemUiTraceEntryProto, 63 SystemUiTraceProto> mProtoTracer; 64 65 @Inject ProtoTracer(Context context, DumpManager dumpManager)66 public ProtoTracer(Context context, DumpManager dumpManager) { 67 mContext = context; 68 mProtoTracer = new FrameProtoTracer<>(this); 69 dumpManager.registerDumpable(this); 70 } 71 72 @Override getTraceFile()73 public File getTraceFile() { 74 return new File(mContext.getFilesDir(), "sysui_trace.pb"); 75 } 76 77 @Override getEncapsulatingTraceProto()78 public SystemUiTraceFileProto getEncapsulatingTraceProto() { 79 return new SystemUiTraceFileProto(); 80 } 81 82 @Override updateBufferProto(SystemUiTraceEntryProto reuseObj, ArrayList<ProtoTraceable<SystemUiTraceProto>> traceables)83 public SystemUiTraceEntryProto updateBufferProto(SystemUiTraceEntryProto reuseObj, 84 ArrayList<ProtoTraceable<SystemUiTraceProto>> traceables) { 85 SystemUiTraceEntryProto proto = reuseObj != null 86 ? reuseObj 87 : new SystemUiTraceEntryProto(); 88 proto.elapsedRealtimeNanos = SystemClock.elapsedRealtimeNanos(); 89 proto.systemUi = proto.systemUi != null ? proto.systemUi : new SystemUiTraceProto(); 90 for (ProtoTraceable t : traceables) { 91 t.writeToProto(proto.systemUi); 92 } 93 return proto; 94 } 95 96 @Override serializeEncapsulatingProto(SystemUiTraceFileProto encapsulatingProto, Queue<SystemUiTraceEntryProto> buffer)97 public byte[] serializeEncapsulatingProto(SystemUiTraceFileProto encapsulatingProto, 98 Queue<SystemUiTraceEntryProto> buffer) { 99 encapsulatingProto.magicNumber = MAGIC_NUMBER_VALUE; 100 encapsulatingProto.entry = buffer.toArray(new SystemUiTraceEntryProto[0]); 101 return MessageNano.toByteArray(encapsulatingProto); 102 } 103 104 @Override getProtoBytes(MessageNano proto)105 public byte[] getProtoBytes(MessageNano proto) { 106 return MessageNano.toByteArray(proto); 107 } 108 109 @Override getProtoSize(MessageNano proto)110 public int getProtoSize(MessageNano proto) { 111 return proto.getCachedSize(); 112 } 113 start()114 public void start() { 115 mProtoTracer.start(); 116 } 117 stop()118 public void stop() { 119 mProtoTracer.stop(); 120 } 121 isEnabled()122 public boolean isEnabled() { 123 return mProtoTracer.isEnabled(); 124 } 125 add(ProtoTraceable<SystemUiTraceProto> traceable)126 public void add(ProtoTraceable<SystemUiTraceProto> traceable) { 127 mProtoTracer.add(traceable); 128 } 129 remove(ProtoTraceable<SystemUiTraceProto> traceable)130 public void remove(ProtoTraceable<SystemUiTraceProto> traceable) { 131 mProtoTracer.remove(traceable); 132 } 133 scheduleFrameUpdate()134 public void scheduleFrameUpdate() { 135 mProtoTracer.scheduleFrameUpdate(); 136 } 137 update()138 public void update() { 139 mProtoTracer.update(); 140 } 141 142 @Override dump(@onNull PrintWriter pw, @NonNull String[] args)143 public void dump(@NonNull PrintWriter pw, @NonNull String[] args) { 144 pw.println("ProtoTracer:"); 145 pw.print(" "); pw.println("enabled: " + mProtoTracer.isEnabled()); 146 pw.print(" "); pw.println("usagePct: " + mProtoTracer.getBufferUsagePct()); 147 pw.print(" "); pw.println("file: " + getTraceFile()); 148 } 149 } 150