• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "permission_utils.h"
24 
25 namespace OHOS {
26 namespace Media {
DfxTimer(int32_t type,int32_t object,int64_t timeOut,bool isReport)27 DfxTimer::DfxTimer(int32_t type, int32_t object, int64_t timeOut, bool isReport)
28 {
29     type_ = type;
30     object_ = object;
31     start_ = MediaFileUtils::UTCTimeMilliSeconds();
32     timeOut_ = timeOut;
33     isReport_ = isReport;
34     isEnd_ = false;
35     uid_ = -1;
36 }
37 
~DfxTimer()38 DfxTimer::~DfxTimer()
39 {
40     if (isEnd_) {
41         return;
42     }
43 
44     timeCost_ = MediaFileUtils::UTCTimeMilliSeconds() - start_;
45     if (!isReport_) {
46         if (timeCost_ > timeOut_)
47             MEDIA_WARN_LOG("timeout! type: %{public}d, object: %{public}d, cost %{public}lld ms",
48                 type_, object_, (long long) (timeCost_));
49         return;
50     }
51 
52     std::string bundleName;
53     if (uid_ > 0) {
54         PermissionUtils::GetClientBundle(uid_, bundleName);
55     } else {
56         bundleName = MediaLibraryBundleManager::GetInstance()->GetClientBundleName();
57     }
58 
59     if (timeCost_ > timeOut_) {
60         std::string caller = (bundleName == "") ? "uid=" + std::to_string(IPCSkeleton::GetCallingUid()) : bundleName;
61         MEDIA_WARN_LOG("timeout! caller: %{public}s, type: %{public}d, object: %{public}d, cost %{public}d",
62             caller.c_str(), type_, object_, (int) (timeCost_));
63 
64         if (timeCost_ > TO_MILLION)
65             DfxManager::GetInstance()->HandleTimeOutOperation(bundleName, type_, object_, (int) (timeCost_));
66     }
67 
68     DfxManager::GetInstance()->HandleCommonBehavior(bundleName, type_);
69 }
70 
End()71 void DfxTimer::End()
72 {
73     timeCost_ = MediaFileUtils::UTCTimeMilliSeconds() - start_;
74     if (timeCost_ > timeOut_) {
75         MEDIA_WARN_LOG("timeout! type: %{public}d, object: %{public}d, cost %{public}d ms", type_, object_,
76             (int) (timeCost_));
77     }
78     isEnd_ = true;
79 }
80 
SetCallerUid(int32_t uid)81 void DfxTimer::SetCallerUid(int32_t uid)
82 {
83     uid_ = uid;
84 }
85 
86 } // namespace Media
87 } // namespace OHOS