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 
17 package com.android.car.stats;
18 
19 import android.car.vms.VmsLayer;
20 
21 import com.android.internal.annotations.GuardedBy;
22 
23 /**
24  * Java representation of VmsClientStats statsd atom.
25  *
26  * All access to this class is synchronized through VmsClientLog.
27  */
28 class VmsClientStats {
29     private final Object mLock = new Object();
30 
31     private final int mUid;
32 
33     private final int mLayerType;
34     private final int mLayerChannel;
35     private final int mLayerVersion;
36 
37     @GuardedBy("mLock")
38     private long mTxBytes;
39     @GuardedBy("mLock")
40     private long mTxPackets;
41 
42     @GuardedBy("mLock")
43     private long mRxBytes;
44     @GuardedBy("mLock")
45     private long mRxPackets;
46 
47     @GuardedBy("mLock")
48     private long mDroppedBytes;
49     @GuardedBy("mLock")
50     private long mDroppedPackets;
51 
52     /**
53      * Constructor for a VmsClientStats entry.
54      *
55      * @param uid UID of client package.
56      * @param layer Vehicle Maps Service layer.
57      */
VmsClientStats(int uid, VmsLayer layer)58     VmsClientStats(int uid, VmsLayer layer) {
59         mUid = uid;
60 
61         mLayerType = layer.getType();
62         mLayerChannel = layer.getSubtype();
63         mLayerVersion = layer.getVersion();
64     }
65 
66     /**
67      * Copy constructor for entries exported from {@link VmsClientLogger}.
68      */
VmsClientStats(VmsClientStats other)69     VmsClientStats(VmsClientStats other) {
70         synchronized (other.mLock) {
71             this.mUid = other.mUid;
72 
73             this.mLayerType = other.mLayerType;
74             this.mLayerChannel = other.mLayerChannel;
75             this.mLayerVersion = other.mLayerVersion;
76 
77             this.mTxBytes = other.mTxBytes;
78             this.mTxPackets = other.mTxPackets;
79             this.mRxBytes = other.mRxBytes;
80             this.mRxPackets = other.mRxPackets;
81             this.mDroppedBytes = other.mDroppedBytes;
82             this.mDroppedPackets = other.mDroppedPackets;
83         }
84     }
85 
86     /**
87      * Records that a packet was sent by a publisher client.
88      *
89      * @param size Size of packet.
90      */
packetSent(long size)91     void packetSent(long size) {
92         synchronized (mLock) {
93             mTxBytes += size;
94             mTxPackets++;
95         }
96     }
97 
98     /**
99      * Records that a packet was successfully received by a subscriber client.
100      *
101      * @param size Size of packet.
102      */
packetReceived(long size)103     void packetReceived(long size) {
104         synchronized (mLock) {
105             mRxBytes += size;
106             mRxPackets++;
107         }
108     }
109 
110     /**
111      * Records that a packet was dropped while being delivered to a subscriber client.
112      *
113      * @param size Size of packet.
114      */
packetDropped(long size)115     void packetDropped(long size) {
116         synchronized (mLock) {
117             mDroppedBytes += size;
118             mDroppedPackets++;
119         }
120     }
121 
getUid()122     int getUid() {
123         return mUid;
124     }
125 
getLayerType()126     int getLayerType() {
127         return mLayerType;
128     }
129 
getLayerChannel()130     int getLayerChannel() {
131         return mLayerChannel;
132     }
133 
getLayerVersion()134     int getLayerVersion() {
135         return mLayerVersion;
136     }
137 
getTxBytes()138     long getTxBytes() {
139         synchronized (mLock) {
140             return mTxBytes;
141         }
142     }
143 
getTxPackets()144     long getTxPackets() {
145         synchronized (mLock) {
146             return mTxPackets;
147         }
148     }
149 
getRxBytes()150     long getRxBytes() {
151         synchronized (mLock) {
152             return mRxBytes;
153         }
154     }
155 
getRxPackets()156     long getRxPackets() {
157         synchronized (mLock) {
158             return mRxPackets;
159         }
160     }
161 
getDroppedBytes()162     long getDroppedBytes() {
163         synchronized (mLock) {
164             return mDroppedBytes;
165         }
166     }
167 
getDroppedPackets()168     long getDroppedPackets() {
169         synchronized (mLock) {
170             return mDroppedPackets;
171         }
172     }
173 }
174 
175