• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *
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 "player_xcollie.h"
17 #include <unistd.h>
18 #include "media_errors.h"
19 #include "media_log.h"
20 #include "param_wrapper.h"
21 #ifdef HICOLLIE_ENABLE
22 #include "xcollie/xcollie.h"
23 #include "xcollie/xcollie_define.h"
24 #endif
25 
26 namespace {
27 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "PlayerXCollie"};
28 }
29 
30 namespace OHOS {
31 namespace Media {
GetInstance()32 PlayerXCollie &PlayerXCollie::GetInstance()
33 {
34     static PlayerXCollie instance;
35     return instance;
36 }
37 
TimerCallback(void * data)38 void PlayerXCollie::TimerCallback(void *data)
39 {
40     std::lock_guard<std::mutex> lock(mutex_);
41     threadDeadlockCount_++;
42     MEDIA_LOGE("threadDeadlockCount: %{public}d", threadDeadlockCount_);
43     static constexpr uint32_t threshold = 5; // >5 Restart service
44     if (threadDeadlockCount_ >= threshold) {
45         _exit(-1);
46     }
47 }
48 
Dump(int32_t fd)49 int32_t PlayerXCollie::Dump(int32_t fd)
50 {
51     std::lock_guard<std::mutex> lock(mutex_);
52     std::string dumpString = "------------------XCollieDfx------------------\n";
53     for (const auto &iter : dfxDumper_) {
54         dumpString += "WaitTask-----";
55         dumpString += iter.second;
56         dumpString += "-----\n";
57     }
58     if (fd != -1) {
59         write(fd, dumpString.c_str(), dumpString.size());
60         dumpString.clear();
61     }
62     return MSERR_OK;
63 }
64 
SetTimer(const std::string & name,bool recovery,uint32_t timeout)65 int32_t PlayerXCollie::SetTimer(const std::string &name, bool recovery, uint32_t timeout)
66 {
67 #ifdef HICOLLIE_ENABLE
68     auto func = [this](void *data) {
69         this->TimerCallback(data);
70     };
71 
72     unsigned int flag = HiviewDFX::XCOLLIE_FLAG_LOG | HiviewDFX::XCOLLIE_FLAG_NOOP;
73     if (recovery) {
74         flag |= HiviewDFX::XCOLLIE_FLAG_RECOVERY;
75     }
76     int32_t id = HiviewDFX::XCollie::GetInstance().SetTimer(name, timeout, func, this, flag);
77     if (id != HiviewDFX::INVALID_ID) {
78         std::lock_guard<std::mutex> lock(mutex_);
79         dfxDumper_.emplace(id, name);
80     }
81     return id;
82 #else
83     return -1;
84 #endif
85 }
86 
SetTimerByLog(const std::string & name,uint32_t timeout)87 int32_t PlayerXCollie::SetTimerByLog(const std::string &name, uint32_t timeout)
88 {
89 #ifdef HICOLLIE_ENABLE
90     unsigned int flag = HiviewDFX::XCOLLIE_FLAG_LOG;
91     int32_t id = HiviewDFX::XCollie::GetInstance().SetTimer(name, timeout, nullptr, this, flag);
92     if (id != HiviewDFX::INVALID_ID) {
93         std::lock_guard<std::mutex> lock(mutex_);
94         dfxDumper_.emplace(id, name);
95     }
96     return id;
97 #else
98     return -1;
99 #endif
100 }
101 
CancelTimer(int32_t id)102 void PlayerXCollie::CancelTimer(int32_t id)
103 {
104 #ifdef HICOLLIE_ENABLE
105     if (id != HiviewDFX::INVALID_ID) {
106         std::lock_guard<std::mutex> lock(mutex_);
107         auto it = dfxDumper_.find(id);
108         if (it != dfxDumper_.end()) {
109             dfxDumper_.erase(it);
110             return HiviewDFX::XCollie::GetInstance().CancelTimer(id);
111         }
112     }
113 #else
114     (void)id;
115     return;
116 #endif
117 }
118 } // namespace Media
119 } // namespace OHOS