Merge "Reduce the memory usage of the ProtoLogToFile"

This commit is contained in:
Treehugger Robot
2023-03-03 15:37:47 +00:00
committed by Gerrit Code Review
3 changed files with 19 additions and 7 deletions

View File

@@ -72,11 +72,13 @@ public class BaseProtoLogImpl {
private static final String TAG = "ProtoLog"; private static final String TAG = "ProtoLog";
private static final long MAGIC_NUMBER_VALUE = ((long) MAGIC_NUMBER_H << 32) | MAGIC_NUMBER_L; private static final long MAGIC_NUMBER_VALUE = ((long) MAGIC_NUMBER_H << 32) | MAGIC_NUMBER_L;
static final String PROTOLOG_VERSION = "1.0.0"; static final String PROTOLOG_VERSION = "1.0.0";
private static final int DEFAULT_PER_CHUNK_SIZE = 0;
private final File mLogFile; private final File mLogFile;
private final String mViewerConfigFilename; private final String mViewerConfigFilename;
private final TraceBuffer mBuffer; private final TraceBuffer mBuffer;
protected final ProtoLogViewerConfigReader mViewerConfig; protected final ProtoLogViewerConfigReader mViewerConfig;
private final int mPerChunkSize;
private boolean mProtoLogEnabled; private boolean mProtoLogEnabled;
private boolean mProtoLogEnabledLockFree; private boolean mProtoLogEnabledLockFree;
@@ -156,7 +158,7 @@ public class BaseProtoLogImpl {
return; return;
} }
try { try {
ProtoOutputStream os = new ProtoOutputStream(); ProtoOutputStream os = new ProtoOutputStream(mPerChunkSize);
long token = os.start(LOG); long token = os.start(LOG);
os.write(MESSAGE_HASH, messageHash); os.write(MESSAGE_HASH, messageHash);
os.write(ELAPSED_REALTIME_NANOS, SystemClock.elapsedRealtimeNanos()); os.write(ELAPSED_REALTIME_NANOS, SystemClock.elapsedRealtimeNanos());
@@ -215,10 +217,16 @@ public class BaseProtoLogImpl {
public BaseProtoLogImpl(File file, String viewerConfigFilename, int bufferCapacity, public BaseProtoLogImpl(File file, String viewerConfigFilename, int bufferCapacity,
ProtoLogViewerConfigReader viewerConfig) { ProtoLogViewerConfigReader viewerConfig) {
this(file, viewerConfigFilename, bufferCapacity, viewerConfig, DEFAULT_PER_CHUNK_SIZE);
}
public BaseProtoLogImpl(File file, String viewerConfigFilename, int bufferCapacity,
ProtoLogViewerConfigReader viewerConfig, int perChunkSize) {
mLogFile = file; mLogFile = file;
mBuffer = new TraceBuffer(bufferCapacity); mBuffer = new TraceBuffer(bufferCapacity);
mViewerConfigFilename = viewerConfigFilename; mViewerConfigFilename = viewerConfigFilename;
mViewerConfig = viewerConfig; mViewerConfig = viewerConfig;
mPerChunkSize = perChunkSize;
} }
/** /**
@@ -364,7 +372,7 @@ public class BaseProtoLogImpl {
try { try {
long offset = long offset =
(System.currentTimeMillis() - (SystemClock.elapsedRealtimeNanos() / 1000000)); (System.currentTimeMillis() - (SystemClock.elapsedRealtimeNanos() / 1000000));
ProtoOutputStream proto = new ProtoOutputStream(); ProtoOutputStream proto = new ProtoOutputStream(mPerChunkSize);
proto.write(MAGIC_NUMBER, MAGIC_NUMBER_VALUE); proto.write(MAGIC_NUMBER, MAGIC_NUMBER_VALUE);
proto.write(VERSION, PROTOLOG_VERSION); proto.write(VERSION, PROTOLOG_VERSION);
proto.write(REAL_TIME_TO_ELAPSED_TIME_OFFSET_MILLIS, offset); proto.write(REAL_TIME_TO_ELAPSED_TIME_OFFSET_MILLIS, offset);

View File

@@ -30,6 +30,7 @@ public class ProtoLogImpl extends BaseProtoLogImpl {
private static final int BUFFER_CAPACITY = 1024 * 1024; private static final int BUFFER_CAPACITY = 1024 * 1024;
private static final String LOG_FILENAME = "/data/misc/wmtrace/wm_log.winscope"; private static final String LOG_FILENAME = "/data/misc/wmtrace/wm_log.winscope";
private static final String VIEWER_CONFIG_FILENAME = "/system/etc/protolog.conf.json.gz"; private static final String VIEWER_CONFIG_FILENAME = "/system/etc/protolog.conf.json.gz";
private static final int PER_CHUNK_SIZE = 1024;
private static ProtoLogImpl sServiceInstance = null; private static ProtoLogImpl sServiceInstance = null;
@@ -94,7 +95,10 @@ public class ProtoLogImpl extends BaseProtoLogImpl {
public static synchronized ProtoLogImpl getSingleInstance() { public static synchronized ProtoLogImpl getSingleInstance() {
if (sServiceInstance == null) { if (sServiceInstance == null) {
sServiceInstance = new ProtoLogImpl( sServiceInstance = new ProtoLogImpl(
new File(LOG_FILENAME), BUFFER_CAPACITY, new ProtoLogViewerConfigReader()); new File(LOG_FILENAME)
, BUFFER_CAPACITY
, new ProtoLogViewerConfigReader()
, PER_CHUNK_SIZE);
} }
return sServiceInstance; return sServiceInstance;
} }
@@ -105,8 +109,8 @@ public class ProtoLogImpl extends BaseProtoLogImpl {
} }
public ProtoLogImpl(File logFile, int bufferCapacity, public ProtoLogImpl(File logFile, int bufferCapacity,
ProtoLogViewerConfigReader viewConfigReader) { ProtoLogViewerConfigReader viewConfigReader, int perChunkSize) {
super(logFile, VIEWER_CONFIG_FILENAME, bufferCapacity, viewConfigReader); super(logFile, VIEWER_CONFIG_FILENAME, bufferCapacity, viewConfigReader, perChunkSize);
} }
} }

View File

@@ -86,7 +86,7 @@ public class ProtoLogImplTest {
mFile = testContext.getFileStreamPath("tracing_test.dat"); mFile = testContext.getFileStreamPath("tracing_test.dat");
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
mFile.delete(); mFile.delete();
mProtoLog = new ProtoLogImpl(mFile, 1024 * 1024, mReader); mProtoLog = new ProtoLogImpl(mFile, 1024 * 1024, mReader, 1024);
} }
@After @After