| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.util; |
| 18 |
|
| 19 |
import android.util.proto.ProtoOutputStream; |
| 20 |
|
| 21 |
import com.android.internal.annotations.VisibleForTesting; |
| 22 |
|
| 23 |
import java.io.File; |
| 24 |
import java.io.FileOutputStream; |
| 25 |
import java.io.IOException; |
| 26 |
import java.io.OutputStream; |
| 27 |
import java.util.ArrayDeque; |
| 28 |
import java.util.Arrays; |
| 29 |
import java.util.Queue; |
| 30 |
import java.util.function.Consumer; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class TraceBuffer<P, S extends P, T extends P> { |
| 42 |
private final Object mBufferLock = new Object(); |
| 43 |
|
| 44 |
private final ProtoProvider<P, S, T> mProtoProvider; |
| 45 |
private final Queue<T> mBuffer = new ArrayDeque<>(); |
| 46 |
private final Consumer mProtoDequeuedCallback; |
| 47 |
private int mBufferUsedSize; |
| 48 |
private int mBufferCapacity; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public interface ProtoProvider<P, S extends P, T extends P> { |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
int getItemSize(P proto); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
byte[] getBytes(P proto); |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
void write(S encapsulatingProto, Queue<T> buffer, OutputStream os) throws IOException; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
private static class ProtoOutputStreamProvider implements |
| 80 |
ProtoProvider<ProtoOutputStream, ProtoOutputStream, ProtoOutputStream> { |
| 81 |
@Override |
| 82 |
public int getItemSize(ProtoOutputStream proto) { |
| 83 |
return proto.getRawSize(); |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public byte[] getBytes(ProtoOutputStream proto) { |
| 88 |
return proto.getBytes(); |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public void write(ProtoOutputStream encapsulatingProto, Queue<ProtoOutputStream> buffer, |
| 93 |
OutputStream os) throws IOException { |
| 94 |
os.write(encapsulatingProto.getBytes()); |
| 95 |
for (ProtoOutputStream protoOutputStream : buffer) { |
| 96 |
byte[] protoBytes = protoOutputStream.getBytes(); |
| 97 |
os.write(protoBytes); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
public TraceBuffer(int bufferCapacity) { |
| 103 |
this(bufferCapacity, new ProtoOutputStreamProvider(), null); |
| 104 |
} |
| 105 |
|
| 106 |
public TraceBuffer(int bufferCapacity, ProtoProvider protoProvider, |
| 107 |
Consumer<T> protoDequeuedCallback) { |
| 108 |
mBufferCapacity = bufferCapacity; |
| 109 |
mProtoProvider = protoProvider; |
| 110 |
mProtoDequeuedCallback = protoDequeuedCallback; |
| 111 |
resetBuffer(); |
| 112 |
} |
| 113 |
|
| 114 |
public int getAvailableSpace() { |
| 115 |
return mBufferCapacity - mBufferUsedSize; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public int size() { |
| 122 |
return mBuffer.size(); |
| 123 |
} |
| 124 |
|
| 125 |
public void setCapacity(int capacity) { |
| 126 |
mBufferCapacity = capacity; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public void add(T proto) { |
| 137 |
int protoLength = mProtoProvider.getItemSize(proto); |
| 138 |
if (protoLength > mBufferCapacity) { |
| 139 |
throw new IllegalStateException("Trace object too large for the buffer. Buffer size:" |
| 140 |
+ mBufferCapacity + " Object size: " + protoLength); |
| 141 |
} |
| 142 |
synchronized (mBufferLock) { |
| 143 |
discardOldest(protoLength); |
| 144 |
mBuffer.add(proto); |
| 145 |
mBufferUsedSize += protoLength; |
| 146 |
mBufferLock.notify(); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
@VisibleForTesting |
| 151 |
public boolean contains(byte[] other) { |
| 152 |
return mBuffer.stream() |
| 153 |
.anyMatch(p -> Arrays.equals(mProtoProvider.getBytes(p), other)); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public void writeTraceToFile(File traceFile, S encapsulatingProto) |
| 160 |
throws IOException { |
| 161 |
synchronized (mBufferLock) { |
| 162 |
traceFile.delete(); |
| 163 |
try (OutputStream os = new FileOutputStream(traceFile)) { |
| 164 |
traceFile.setReadable(true , false ); |
| 165 |
mProtoProvider.write(encapsulatingProto, mBuffer, os); |
| 166 |
os.flush(); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
private void discardOldest(int protoLength) { |
| 178 |
long availableSpace = getAvailableSpace(); |
| 179 |
|
| 180 |
while (availableSpace < protoLength) { |
| 181 |
|
| 182 |
P item = mBuffer.poll(); |
| 183 |
if (item == null) { |
| 184 |
throw new IllegalStateException("No element to discard from buffer"); |
| 185 |
} |
| 186 |
mBufferUsedSize -= mProtoProvider.getItemSize(item); |
| 187 |
availableSpace = getAvailableSpace(); |
| 188 |
|
| 189 |
if (mProtoDequeuedCallback != null) { |
| 190 |
mProtoDequeuedCallback.accept(item); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public void resetBuffer() { |
| 199 |
synchronized (mBufferLock) { |
| 200 |
if (mProtoDequeuedCallback != null) { |
| 201 |
for (T item : mBuffer) { |
| 202 |
mProtoDequeuedCallback.accept(item); |
| 203 |
} |
| 204 |
} |
| 205 |
mBuffer.clear(); |
| 206 |
mBufferUsedSize = 0; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
@VisibleForTesting |
| 211 |
public int getBufferSize() { |
| 212 |
return mBufferUsedSize; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public String getStatus() { |
| 219 |
synchronized (mBufferLock) { |
| 220 |
return "Buffer size: " + mBufferCapacity + " bytes" + "\n" |
| 221 |
+ "Buffer usage: " + mBufferUsedSize + " bytes" + "\n" |
| 222 |
+ "Elements in the buffer: " + mBuffer.size(); |
| 223 |
} |
| 224 |
} |
| 225 |
} |