1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
12 */
13
14 #include "stream_statistics.h"
15 #include <sstream>
16
17 namespace OHOS::Camera {
StreamStatistics()18 StreamStatistics::StreamStatistics()
19 {
20 Clear();
21 }
22
StreamStatistics(int32_t streamId)23 StreamStatistics::StreamStatistics(int32_t streamId)
24 {
25 streamId_ = streamId;
26 Clear();
27 }
28
~StreamStatistics()29 StreamStatistics::~StreamStatistics() {}
30
SetStreamId(int32_t streamId)31 void StreamStatistics::SetStreamId(int32_t streamId)
32 {
33 streamId_ = streamId;
34 }
35
RequestBufferSuccess()36 void StreamStatistics::RequestBufferSuccess()
37 {
38 requestBufferSuccessCount_++;
39 }
40
RequestBufferFail()41 void StreamStatistics::RequestBufferFail()
42 {
43 requestBufferFailCount_++;
44 }
45
FlushBufferSuccess()46 void StreamStatistics::FlushBufferSuccess()
47 {
48 flushBufferSuccessCount_++;
49 }
50
FlushBufferFail()51 void StreamStatistics::FlushBufferFail()
52 {
53 flushBufferFailCount_++;
54 }
55
CancelBufferSuccess()56 void StreamStatistics::CancelBufferSuccess()
57 {
58 cancelBufferSuccessCount_++;
59 }
60
CancelBufferFail()61 void StreamStatistics::CancelBufferFail()
62 {
63 cancelBufferFailCount_++;
64 }
65
Clear()66 void StreamStatistics::Clear()
67 {
68 requestBufferSuccessCount_ = 0;
69 requestBufferFailCount_ = 0;
70 flushBufferSuccessCount_ = 0;
71 flushBufferFailCount_ = 0;
72 cancelBufferSuccessCount_ = 0;
73 cancelBufferFailCount_ = 0;
74 }
75
DumpStats(int interval)76 void StreamStatistics::DumpStats(int interval)
77 {
78 if (clock_gettime(CLOCK_MONOTONIC, ×tamp_) != 0) {
79 CAMERA_LOGE("clock_gettime error");
80 return;
81 }
82
83 if (lastOutputTime_ == 0) {
84 lastOutputTime_ = timestamp_.tv_sec;
85 return;
86 }
87
88 if (timestamp_.tv_sec - lastOutputTime_ > interval) {
89 std::stringstream ss;
90 ss << "streamId:" << streamId_ << ", requestBufferSuccessCount:" << requestBufferSuccessCount_ <<
91 ", requestBufferFailCount:" << requestBufferFailCount_ << ", flushBufferSuccessCount:" <<
92 flushBufferSuccessCount_ << ", flushBufferFailCount:" << flushBufferFailCount_ <<
93 ", cancelBufferSuccessCount:" << cancelBufferSuccessCount_ << ", cancelBufferFailCount:" <<
94 cancelBufferFailCount_;
95 std::string dumpInfo = ss.str();
96
97 CAMERA_LOGE("%{public}s", dumpInfo.c_str());
98 lastOutputTime_ = timestamp_.tv_sec;
99 }
100 }
101
CancelBufferResult(int32_t ret)102 void StreamStatistics::CancelBufferResult(int32_t ret)
103 {
104 if (ret != 0) {
105 CancelBufferFail();
106 } else {
107 CancelBufferSuccess();
108 }
109 }
110
FlushBufferResult(int32_t ret)111 void StreamStatistics::FlushBufferResult(int32_t ret)
112 {
113 if (ret != 0) {
114 FlushBufferFail();
115 } else {
116 FlushBufferSuccess();
117 }
118 }
119
RequestBufferResult(OHOS::SurfaceError sfError)120 void StreamStatistics::RequestBufferResult(OHOS::SurfaceError sfError)
121 {
122 if (sfError != OHOS::SURFACE_ERROR_OK) {
123 RequestBufferFail();
124 } else {
125 RequestBufferSuccess();
126 }
127 }
128
RequestBufferResult(OHOS::SurfaceBuffer * sbuffer)129 void StreamStatistics::RequestBufferResult(OHOS::SurfaceBuffer *sbuffer)
130 {
131 if (sbuffer == nullptr) {
132 RequestBufferFail();
133 } else {
134 RequestBufferSuccess();
135 }
136 }
137 } // end namespace OHOS::Camera
138