1 /*
2  * Copyright (C) 2015 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.camera.stats.profiler;
18 
19 /**
20  * A guarding profile will only write messages to a writer if the
21  * time exceeds a given threshold. For mark() calls, it uses the
22  * time since last event, and for stop() calls it uses the total
23  * elapsed duration of the profile.
24  */
25 public class GuardingProfile extends ProfileBase {
26     private final Writer mGuardWriter;
27     private final Writer mVerboseWriter;
28     private final int mMaxMillis;
29 
GuardingProfile(Writer writer, Writer verbose, String name,int maxDuration)30     public GuardingProfile(Writer writer, Writer verbose, String name,int maxDuration) {
31         super(name);
32         mGuardWriter = writer;
33         mVerboseWriter = verbose;
34         mMaxMillis = maxDuration;
35     }
36 
37     @Override
onStart()38     protected void onStart() {
39         mVerboseWriter.write(format(0, "GUARD", "START"));
40     }
41 
42     @Override
onMark(double totalMillis, double lastMillis, String reason)43     protected void onMark(double totalMillis, double lastMillis, String reason) {
44         if (lastMillis > mMaxMillis) {
45             mGuardWriter.write(format(totalMillis, "GUARD", lastMillis, reason));
46         } else {
47             mVerboseWriter.write(format(totalMillis, "GUARD", lastMillis, reason));
48         }
49     }
50 
51     @Override
onStop(double totalMillis, double lastMillis)52     protected void onStop(double totalMillis, double lastMillis) {
53         if (totalMillis > mMaxMillis) {
54             mGuardWriter.write(format(totalMillis, "GUARD", "STOP"));
55         } else {
56             mVerboseWriter.write(format(totalMillis, "GUARD",  "STOP"));
57         }
58     }
59 
60     @Override
onStop(double totalMillis, double lastMillis, String reason)61     protected void onStop(double totalMillis, double lastMillis, String reason) {
62         onMark(totalMillis, lastMillis, reason);
63         onStop(totalMillis, lastMillis);
64     }
65 }
66