Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 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 | |
Adam Pardyl | 0f1b3d4 | 2019-08-19 15:24:11 +0200 | [diff] [blame] | 17 | package com.android.server.utils; |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 18 | |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 19 | import android.util.proto.ProtoOutputStream; |
| 20 | |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 21 | import com.android.internal.annotations.VisibleForTesting; |
| 22 | |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 23 | import java.io.File; |
| 24 | import java.io.FileOutputStream; |
| 25 | import java.io.IOException; |
| 26 | import java.io.OutputStream; |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 27 | import java.util.ArrayDeque; |
| 28 | import java.util.Arrays; |
| 29 | import java.util.Queue; |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 30 | |
| 31 | /** |
Adam Pardyl | 0f1b3d4 | 2019-08-19 15:24:11 +0200 | [diff] [blame] | 32 | * Buffer used for tracing and logging. |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 33 | */ |
Adam Pardyl | 0f1b3d4 | 2019-08-19 15:24:11 +0200 | [diff] [blame] | 34 | public class TraceBuffer { |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 35 | private final Object mBufferLock = new Object(); |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 36 | |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 37 | private final Queue<ProtoOutputStream> mBuffer = new ArrayDeque<>(); |
| 38 | private int mBufferUsedSize; |
| 39 | private int mBufferCapacity; |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 40 | |
Adam Pardyl | 0f1b3d4 | 2019-08-19 15:24:11 +0200 | [diff] [blame] | 41 | public TraceBuffer(int bufferCapacity) { |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 42 | mBufferCapacity = bufferCapacity; |
| 43 | resetBuffer(); |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Adam Pardyl | 0f1b3d4 | 2019-08-19 15:24:11 +0200 | [diff] [blame] | 46 | public int getAvailableSpace() { |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 47 | return mBufferCapacity - mBufferUsedSize; |
| 48 | } |
| 49 | |
Adam Pardyl | 0f1b3d4 | 2019-08-19 15:24:11 +0200 | [diff] [blame] | 50 | /** |
| 51 | * Returns buffer size. |
| 52 | */ |
| 53 | public int size() { |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 54 | return mBuffer.size(); |
| 55 | } |
| 56 | |
Adam Pardyl | 0f1b3d4 | 2019-08-19 15:24:11 +0200 | [diff] [blame] | 57 | public void setCapacity(int capacity) { |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 58 | mBufferCapacity = capacity; |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Inserts the specified element into this buffer. |
| 63 | * |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 64 | * @param proto the element to add |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 65 | * @throws IllegalStateException if the element cannot be added because it is larger |
| 66 | * than the buffer size. |
| 67 | */ |
Adam Pardyl | 0f1b3d4 | 2019-08-19 15:24:11 +0200 | [diff] [blame] | 68 | public void add(ProtoOutputStream proto) { |
Nataniel Borges | 023ecb5 | 2019-01-16 14:15:43 -0800 | [diff] [blame] | 69 | int protoLength = proto.getRawSize(); |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 70 | if (protoLength > mBufferCapacity) { |
| 71 | throw new IllegalStateException("Trace object too large for the buffer. Buffer size:" |
| 72 | + mBufferCapacity + " Object size: " + protoLength); |
| 73 | } |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 74 | synchronized (mBufferLock) { |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 75 | discardOldest(protoLength); |
| 76 | mBuffer.add(proto); |
| 77 | mBufferUsedSize += protoLength; |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 78 | mBufferLock.notify(); |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 82 | boolean contains(byte[] other) { |
| 83 | return mBuffer.stream() |
Nataniel Borges | 023ecb5 | 2019-01-16 14:15:43 -0800 | [diff] [blame] | 84 | .anyMatch(p -> Arrays.equals(p.getBytes(), other)); |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 87 | /** |
Adam Pardyl | 0f1b3d4 | 2019-08-19 15:24:11 +0200 | [diff] [blame] | 88 | * Writes the trace buffer to disk inside the encapsulatingProto.. |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 89 | */ |
Adam Pardyl | 0f1b3d4 | 2019-08-19 15:24:11 +0200 | [diff] [blame] | 90 | public void writeTraceToFile(File traceFile, ProtoOutputStream encapsulatingProto) |
| 91 | throws IOException { |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 92 | synchronized (mBufferLock) { |
| 93 | traceFile.delete(); |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 94 | try (OutputStream os = new FileOutputStream(traceFile)) { |
Nataniel Borges | 8c074c6 | 2019-03-12 15:16:46 -0700 | [diff] [blame] | 95 | traceFile.setReadable(true /* readable */, false /* ownerOnly */); |
Adam Pardyl | 0f1b3d4 | 2019-08-19 15:24:11 +0200 | [diff] [blame] | 96 | os.write(encapsulatingProto.getBytes()); |
Nataniel Borges | b2476220 | 2019-02-22 14:39:18 -0800 | [diff] [blame] | 97 | for (ProtoOutputStream protoOutputStream : mBuffer) { |
Adam Pardyl | 0f1b3d4 | 2019-08-19 15:24:11 +0200 | [diff] [blame] | 98 | encapsulatingProto = protoOutputStream; |
| 99 | byte[] protoBytes = encapsulatingProto.getBytes(); |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 100 | os.write(protoBytes); |
| 101 | } |
| 102 | os.flush(); |
| 103 | } |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Checks if the element can be added to the buffer. The element is already certain to be |
| 109 | * smaller than the overall buffer size. |
| 110 | * |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 111 | * @param protoLength byte array representation of the Proto object to add |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 112 | */ |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 113 | private void discardOldest(int protoLength) { |
| 114 | long availableSpace = getAvailableSpace(); |
| 115 | |
| 116 | while (availableSpace < protoLength) { |
| 117 | |
| 118 | ProtoOutputStream item = mBuffer.poll(); |
| 119 | if (item == null) { |
| 120 | throw new IllegalStateException("No element to discard from buffer"); |
| 121 | } |
| 122 | mBufferUsedSize -= item.getRawSize(); |
| 123 | availableSpace = getAvailableSpace(); |
| 124 | } |
| 125 | } |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 126 | |
| 127 | /** |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 128 | * Removes all elements form the buffer |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 129 | */ |
Adam Pardyl | 0f1b3d4 | 2019-08-19 15:24:11 +0200 | [diff] [blame] | 130 | public void resetBuffer() { |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 131 | synchronized (mBufferLock) { |
| 132 | mBuffer.clear(); |
| 133 | mBufferUsedSize = 0; |
Nataniel Borges | 98d92aa | 2019-01-03 14:22:44 -0800 | [diff] [blame] | 134 | } |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 135 | } |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 136 | |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 137 | @VisibleForTesting |
| 138 | int getBufferSize() { |
| 139 | return mBufferUsedSize; |
| 140 | } |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 141 | |
Adam Pardyl | 0f1b3d4 | 2019-08-19 15:24:11 +0200 | [diff] [blame] | 142 | /** |
| 143 | * Returns the buffer status in human-readable form. |
| 144 | */ |
| 145 | public String getStatus() { |
Nataniel Borges | 7d37ce2 | 2019-02-05 10:07:02 -0800 | [diff] [blame] | 146 | synchronized (mBufferLock) { |
| 147 | return "Buffer size: " |
| 148 | + mBufferCapacity |
| 149 | + " bytes" |
| 150 | + "\n" |
| 151 | + "Buffer usage: " |
| 152 | + mBufferUsedSize |
| 153 | + " bytes" |
| 154 | + "\n" |
| 155 | + "Elements in the buffer: " |
| 156 | + mBuffer.size(); |
Nataniel Borges | 5a38bad | 2019-01-03 10:32:03 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | } |