• 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 
16 #include <sstream>
17 
18 #include "deadlock_helpers.h"
19 
20 namespace Commonlibrary::Concurrent::LocksModule {
21 
CreateDeadlockWarningMessage(DeadlockInfo && deadlock)22 std::string CreateDeadlockWarningMessage(DeadlockInfo &&deadlock)
23 {
24     std::stringstream s;
25     auto vertexPrinter = [](tid_t tid) { return "TID " + std::to_string(tid); };
26     auto edgePrinter = [](const AsyncLockDependency *edata) {
27         if (edata == nullptr) {
28             return std::string(" <-- lock {NULL} -- WAITED BY ");
29         }
30         return " <-- lock {" + edata->name + "} -- WAITED BY ";
31     };
32     s << "!!! DEADLOCK WARNING !!!\n"
33       << LockGraph::CycleAsString(deadlock, "Possible deadlock: ", "\n", vertexPrinter, edgePrinter);
34     return s.str();
35 }
36 
CreateFullLockInfosMessage(tid_t targetTid,std::vector<AsyncLockDependency> && dependencies,DeadlockInfo && deadlock)37 std::string CreateFullLockInfosMessage(tid_t targetTid, std::vector<AsyncLockDependency> &&dependencies,
38                                        DeadlockInfo &&deadlock)
39 {
40     std::stringstream s;
41     s << "\nThread's async locks information:\n";
42     for (auto &maybeHeld : dependencies) {
43         if ((maybeHeld.holderTid == targetTid) && (maybeHeld.waiterTid == INVALID_TID)) {
44             s << "HELD: lock {" + maybeHeld.name + "} , taken at:\n" + maybeHeld.creationStacktrace + "\n";
45         }
46     }
47     for (auto &maybeWaiting : dependencies) {
48         if (maybeWaiting.waiterTid == targetTid) {
49             s << "WAITING: lock {" + maybeWaiting.name + "} , taken at:\n" + maybeWaiting.creationStacktrace + "\n";
50         }
51     }
52     if (!deadlock.IsEmpty()) {
53         s << CreateDeadlockWarningMessage(std::move(deadlock));
54     } else {
55         s << "No deadlocks detected.\n";
56     }
57     return s.str();
58 }
59 
CheckDeadlocks(const std::vector<AsyncLockDependency> & dependencies)60 DeadlockInfo CheckDeadlocks(const std::vector<AsyncLockDependency> &dependencies)
61 {
62     LockGraph::AdjacencyList adjlist;
63     for (auto &dep : dependencies) {
64         if (dep.waiterTid == INVALID_TID) {
65             // this is the "held only" edge, skip
66             continue;
67         }
68         adjlist.push_back(std::make_tuple(dep.waiterTid, dep.holderTid, &dep));
69     }
70     LockGraph g(std::move(adjlist));
71     return g.FindFirstCycle();
72 }
73 
74 }  // namespace Commonlibrary::Concurrent::LocksModule