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