1 /* <lambda>null2 * Copyright 2024 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 androidx.tracing.driver 18 19 /** [Track] representing a numerical value that can change over the duration of the trace. */ 20 public open class CounterTrack( 21 /** The name of the counter track */ 22 private val name: String, 23 /** The parent track the counter belongs to. */ 24 private val parent: Track 25 ) : Track(context = parent.context, uuid = monotonicId()) { 26 internal val packetLock = Any() 27 28 init { 29 synchronized(packetLock) { 30 emitTraceEvent(immediateDispatch = true) { event -> 31 event.setPreamble( 32 TrackDescriptor( 33 name = name, 34 uuid = uuid, 35 parentUuid = parent.uuid, 36 type = TRACK_DESCRIPTOR_TYPE_COUNTER, 37 pid = INVALID_INT, 38 tid = INVALID_INT 39 ) 40 ) 41 } 42 } 43 } 44 45 public fun setCounter(value: Long) { 46 if (context.isEnabled) { 47 synchronized(packetLock) { 48 emitTraceEvent { packet -> packet.setCounterLong(uuid, value) } 49 } 50 } 51 } 52 53 public fun setCounter(value: Double) { 54 if (context.isEnabled) { 55 synchronized(packetLock) { 56 emitTraceEvent { packet -> packet.setCounterDouble(uuid, value) } 57 } 58 } 59 } 60 } 61 62 // An empty counter track when tracing is disabled 63 64 private const val EMPTY_COUNTER_NAME = "Empty Counter" 65 66 internal class EmptyCounterTrack(process: EmptyProcessTrack) : 67 CounterTrack(name = EMPTY_COUNTER_NAME, parent = process) {} 68