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_PLAYER, "PlayerXCollie"};
28 }
29 
30 namespace OHOS {
31 namespace Media {
GetInstance()32 PlayerXCollie &PlayerXCollie::GetInstance()
33 {
34     static PlayerXCollie instance;
35     return instance;
36 }
37 
~PlayerXCollie()38 PlayerXCollie::~PlayerXCollie()
39 {
40     MEDIA_LOGI("~PlayerXCollie()");
41     std::lock_guard<std::mutex> lock(mutex_);
42 #ifdef HICOLLIE_ENABLE
43     for (const auto &iter : dfxDumper_) {
44         HiviewDFX::XCollie::GetInstance().CancelTimer(iter.first);
45     }
46 #endif
47     dfxDumper_.clear();
48 }
49 
TimerCallback(void * data)50 void PlayerXCollie::TimerCallback(void *data)
51 {
52     std::lock_guard<std::mutex> lock(mutex_);
53     threadDeadlockCount_++;
54     MEDIA_LOGE("threadDeadlockCount: %{public}d", threadDeadlockCount_);
55     static constexpr uint32_t threshold = 5; // >5 Restart service
56     if (threadDeadlockCount_ >= threshold) {
57         _exit(-1);
58     }
59 }
60 
Dump(int32_t fd)61 int32_t PlayerXCollie::Dump(int32_t fd)
62 {
63     std::lock_guard<std::mutex> lock(mutex_);
64     std::string dumpString = "------------------XCollieDfx------------------\n";
65     for (const auto &iter : dfxDumper_) {
66         dumpString += "WaitTask-----";
67         dumpString += iter.second;
68         dumpString += "-----\n";
69     }
70     if (fd != -1) {
71         write(fd, dumpString.c_str(), dumpString.size());
72         dumpString.clear();
73     }
74     return MSERR_OK;
75 }
76 
SetTimer(const std::string & name,bool recovery,uint32_t timeout)77 int32_t PlayerXCollie::SetTimer(const std::string &name, bool recovery, uint32_t timeout)
78 {
79 #ifdef HICOLLIE_ENABLE
80     auto func = [this](void *data) {
81         this->TimerCallback(data);
82     };
83 
84     unsigned int flag = HiviewDFX::XCOLLIE_FLAG_LOG | HiviewDFX::XCOLLIE_FLAG_NOOP;
85     if (recovery) {
86         flag |= HiviewDFX::XCOLLIE_FLAG_RECOVERY;
87     }
88     int32_t id = HiviewDFX::XCollie::GetInstance().SetTimer(name, timeout, func, this, flag);
89     if (id != HiviewDFX::INVALID_ID) {
90         std::lock_guard<std::mutex> lock(mutex_);
91         dfxDumper_.emplace(id, name);
92     }
93     return id;
94 #else
95     return -1;
96 #endif
97 }
98 
SetTimerByLog(const std::string & name,uint32_t timeout)99 int32_t PlayerXCollie::SetTimerByLog(const std::string &name, uint32_t timeout)
100 {
101 #ifdef HICOLLIE_ENABLE
102     unsigned int flag = HiviewDFX::XCOLLIE_FLAG_LOG;
103     int32_t id = HiviewDFX::XCollie::GetInstance().SetTimer(name, timeout, nullptr, this, flag);
104     if (id != HiviewDFX::INVALID_ID) {
105         std::lock_guard<std::mutex> lock(mutex_);
106         dfxDumper_.emplace(id, name);
107     }
108     return id;
109 #else
110     return -1;
111 #endif
112 }
113 
CancelTimer(int32_t id)114 void PlayerXCollie::CancelTimer(int32_t id)
115 {
116 #ifdef HICOLLIE_ENABLE
117     if (id != HiviewDFX::INVALID_ID) {
118         std::lock_guard<std::mutex> lock(mutex_);
119         if (dfxDumper_.size() == 0) {
120             return;
121         }
122         auto it = dfxDumper_.find(id);
123         if (it != dfxDumper_.end()) {
124             dfxDumper_.erase(it);
125             return HiviewDFX::XCollie::GetInstance().CancelTimer(id);
126         }
127     }
128 #else
129     (void)id;
130     return;
131 #endif
132 }
133 } // namespace Media
134 } // namespace OHOS