1 /* 2 * Copyright (C) 2020 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.util; 18 19 import android.annotation.NonNull; 20 import android.annotation.SystemApi; 21 import android.annotation.UptimeMillisLong; 22 import android.os.SystemClock; 23 24 import com.android.internal.annotations.VisibleForTesting; 25 26 /** 27 * Writes an EventLog event capturing the performance of system config file writes. 28 * The event log entry is formatted like this: 29 * <code>525000 commit_sys_config_file (name|3),(time|2|3)</code>, where <code>name</code> is 30 * a short unique name representing the type of configuration file and <code>time</code> is 31 * duration in the {@link SystemClock#uptimeMillis()} time base. 32 * 33 * @hide 34 */ 35 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) 36 public class SystemConfigFileCommitEventLogger { 37 private final String mName; 38 private long mStartTime; 39 40 /** 41 * @param name The short name of the config file that is included in the event log event, 42 * e.g. "jobs", "appops", "uri-grants" etc. 43 */ SystemConfigFileCommitEventLogger(@onNull String name)44 public SystemConfigFileCommitEventLogger(@NonNull String name) { 45 mName = name; 46 } 47 48 /** 49 * Override the start timestamp. Use this method when it's desired to include the time 50 * taken by the preparation of the configuration data in the overall duration of the 51 * "commitSysConfigFile" event. 52 * 53 * @param startTime Overridden start time, in system uptime milliseconds 54 */ setStartTime(@ptimeMillisLong long startTime)55 public void setStartTime(@UptimeMillisLong long startTime) { 56 mStartTime = startTime; 57 } 58 59 /** 60 * Invoked just before the configuration file writing begins. 61 * @hide 62 */ onStartWrite()63 public void onStartWrite() { 64 if (mStartTime == 0) { 65 mStartTime = SystemClock.uptimeMillis(); 66 } 67 } 68 69 /** 70 * Invoked just after the configuration file writing ends. 71 * @hide 72 */ onFinishWrite()73 public void onFinishWrite() { 74 writeLogRecord(SystemClock.uptimeMillis() - mStartTime); 75 } 76 77 /** 78 * The actual write of the log record. 79 * @hide 80 */ 81 @VisibleForTesting writeLogRecord(long durationMs)82 public void writeLogRecord(long durationMs) { 83 com.android.internal.logging.EventLogTags.writeCommitSysConfigFile(mName, durationMs); 84 } 85 } 86