1 /*
2 * Copyright (c) 2025 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 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "audio_dfx.h"
17 #ifdef AUDIO_HITRACE_ENABLE
18 #include <hitrace_meter.h>
19 #endif
20 #ifdef AUDIO_HICOLLIE_ENABLE
21 #include "xcollie/xcollie.h"
22 #include "xcollie/xcollie_define.h"
23 #endif
24 #ifdef AUDIO_HISYSEVENT_ENABLE
25 #include "hisysevent.h"
26 #endif
27
28 using namespace OHOS::HiviewDFX;
29
30 #define HICOLLIE_TIMEOUT 8
31
HdfAudioStartTrace(const char * value,int valueLen)32 void HdfAudioStartTrace(const char* value, int valueLen)
33 {
34 (void) valueLen;
35 #ifdef AUDIO_HITRACE_ENABLE
36 StartTrace(HITRACE_TAG_HDF, value);
37 #else
38 (void) value;
39 #endif
40 }
41
HdfAudioFinishTrace(void)42 void HdfAudioFinishTrace(void)
43 {
44 #ifdef AUDIO_HITRACE_ENABLE
45 FinishTrace(HITRACE_TAG_HDF);
46 #endif
47 }
48
SetTimer(const char * name)49 int32_t SetTimer(const char* name)
50 {
51 int32_t id = 0;
52 #ifdef AUDIO_HICOLLIE_ENABLE
53 id = OHOS::HiviewDFX::XCollie::GetInstance().SetTimer(name, HICOLLIE_TIMEOUT, nullptr, nullptr,
54 OHOS::HiviewDFX::XCOLLIE_FLAG_LOG | OHOS::HiviewDFX::XCOLLIE_FLAG_RECOVERY);
55 #else
56 (void)name;
57 #endif
58 return id;
59 }
60
CancelTimer(int32_t id)61 void CancelTimer(int32_t id)
62 {
63 #ifdef AUDIO_HICOLLIE_ENABLE
64 if (id != 0) {
65 OHOS::HiviewDFX::XCollie::GetInstance().CancelTimer(id);
66 }
67 #else
68 (void)id;
69 #endif
70 }
71
AudioDfxSysEventGetTimeStamp(void)72 struct timeval AudioDfxSysEventGetTimeStamp(void)
73 {
74 struct timeval startTime;
75 gettimeofday(&startTime, nullptr);
76 return startTime;
77 }
78
AudioDfxSysEventError(const char * desc,struct timeval startTime,int timeThreshold,int err)79 int32_t AudioDfxSysEventError(const char* desc, struct timeval startTime, int timeThreshold, int err)
80 {
81 if (err != HDF_SUCCESS) {
82 #ifdef AUDIO_HISYSEVENT_ENABLE
83 HiSysEventWrite(HiSysEvent::Domain::AUDIO, "HDF_AUDIO_ERROR_EVENT", HiSysEvent::EventType::FAULT,
84 "ERROR_DESC", desc, "ERROR_CODE", err, "OVER_TIME", 0);
85 #endif
86 }
87 struct timeval endTime;
88 gettimeofday(&endTime, nullptr);
89 int32_t runTime = (int32_t)((endTime.tv_sec - startTime.tv_sec) * TIME_1000 +
90 (endTime.tv_usec - startTime.tv_usec) / TIME_1000);
91 if (runTime > timeThreshold) {
92 AUDIO_FUNC_LOGE("%{public}s, ovet time [%{public}d]", desc, runTime);
93 #ifdef AUDIO_HISYSEVENT_ENABLE
94 HiSysEventWrite(HiSysEvent::Domain::AUDIO, "HDF_AUDIO_ERROR_EVENT", HiSysEvent::EventType::FAULT,
95 "ERROR_DESC", desc, "ERROR_CODE", err, "OVER_TIME", runTime);
96 #endif
97 }
98 return HDF_SUCCESS;
99 }
100