1 /*
2  * Copyright (C) 2024 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 #define MLOG_TAG "DfxTimer"
16 
17 #include "dfx_timer.h"
18 
19 #include "media_file_utils.h"
20 #include "media_log.h"
21 #include "dfx_manager.h"
22 #include "medialibrary_bundle_manager.h"
23 
24 namespace OHOS {
25 namespace Media {
DfxTimer(int32_t type,int32_t object,int64_t timeOut,bool isReport)26 DfxTimer::DfxTimer(int32_t type, int32_t object, int64_t timeOut, bool isReport)
27 {
28     type_ = type;
29     object_ = object;
30     start_ = MediaFileUtils::UTCTimeMilliSeconds();
31     timeOut_ = timeOut;
32     isReport_ = isReport;
33     isEnd_ = false;
34 }
35 
~DfxTimer()36 DfxTimer::~DfxTimer()
37 {
38     if (isEnd_) {
39         return;
40     }
41     timeCost_ = MediaFileUtils::UTCTimeMilliSeconds() - start_;
42     if (timeCost_ > timeOut_) {
43         if (isReport_) {
44             std::string bundleName = MediaLibraryBundleManager::GetInstance()->GetClientBundleName();
45             MEDIA_WARN_LOG("timeout! bundleName: %{public}s, type: %{public}d, object: %{public}d, cost %{public}d",
46                 bundleName.c_str(), type_, object_, (int) (timeCost_));
47             if (timeCost_ > TO_MILLION) {
48                 DfxManager::GetInstance()->HandleTimeOutOperation(bundleName, type_, object_, (int) (timeCost_));
49             }
50         } else {
51             MEDIA_WARN_LOG("timeout! type: %{public}d, object: %{public}d, cost %{public}lld ms", type_,
52                 object_, (long long) (timeCost_));
53         }
54     }
55     if (isReport_) {
56         DfxManager::GetInstance()->HandleCommonBehavior(MediaLibraryBundleManager::GetInstance()->GetClientBundleName(),
57             type_);
58     }
59 }
60 
End()61 void DfxTimer::End()
62 {
63     timeCost_ = MediaFileUtils::UTCTimeMilliSeconds() - start_;
64     if (timeCost_ > timeOut_) {
65         MEDIA_WARN_LOG("timeout! type: %{public}d, object: %{public}d, cost %{public}d ms", type_, object_,
66             (int) (timeCost_));
67     }
68     isEnd_ = true;
69 }
70 
71 } // namespace Media
72 } // namespace OHOS