proxygen
ExceptionCounterLib.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
17 
18 #include <iosfwd>
19 #include <unordered_map>
20 
21 #include <folly/Range.h>
22 #include <folly/Synchronized.h>
23 #include <folly/ThreadLocal.h>
26 
30 
31 using namespace folly::exception_tracer;
32 
33 namespace {
34 
35 // We use the hash of stack trace and exception type to uniquely
36 // identify the exception.
37 using ExceptionId = uint64_t;
38 
39 using ExceptionStatsHolderType =
40  std::unordered_map<ExceptionId, ExceptionStats>;
41 
42 struct ExceptionStatsStorage {
43  void appendTo(ExceptionStatsHolderType& data) {
44  ExceptionStatsHolderType tempHolder;
45  statsHolder->swap(tempHolder);
46 
47  for (const auto& myData : tempHolder) {
48  auto inserted = data.insert(myData);
49  if (!inserted.second) {
50  inserted.first->second.count += myData.second.count;
51  }
52  }
53  }
54 
56 };
57 
58 class Tag {};
59 
61 
62 } // namespace
63 
64 namespace folly {
65 namespace exception_tracer {
66 
67 std::vector<ExceptionStats> getExceptionStatistics() {
68  ExceptionStatsHolderType accumulator;
69  for (auto& threadStats : gExceptionStats.accessAllThreads()) {
70  threadStats.appendTo(accumulator);
71  }
72 
73  std::vector<ExceptionStats> result;
74  result.reserve(accumulator.size());
75  for (auto& item : accumulator) {
76  result.push_back(std::move(item.second));
77  }
78 
79  std::sort(
80  result.begin(),
81  result.end(),
82  [](const ExceptionStats& lhs, const ExceptionStats& rhs) {
83  return lhs.count > rhs.count;
84  });
85 
86  return result;
87 }
88 
89 std::ostream& operator<<(std::ostream& out, const ExceptionStats& stats) {
90  out << "Exception report: \n"
91  << "Exception count: " << stats.count << "\n"
92  << stats.info;
93 
94  return out;
95 }
96 
97 } // namespace exception_tracer
98 } // namespace folly
99 
100 namespace {
101 
102 /*
103  * This handler gathers statistics on all exceptions thrown by the program
104  * Information is being stored in thread local storage.
105  */
106 void throwHandler(void*, std::type_info* exType, void (*)(void*)) noexcept {
107  // This array contains the exception type and the stack frame
108  // pointers so they get all hashed together.
109  uintptr_t frames[kMaxFrames + 1];
110  frames[0] = reinterpret_cast<uintptr_t>(exType);
111  auto n = folly::symbolizer::getStackTrace(frames + 1, kMaxFrames);
112 
113  if (n == -1) {
114  // If we fail to collect the stack trace for this exception we
115  // just log it under empty stack trace.
116  n = 0;
117  }
118 
119  auto exceptionId =
120  folly::hash::SpookyHashV2::Hash64(frames, (n + 1) * sizeof(frames[0]), 0);
121 
122  gExceptionStats->statsHolder.withWLock([&](auto& holder) {
123  auto it = holder.find(exceptionId);
124  if (it != holder.end()) {
125  ++it->second.count;
126  } else {
128  info.type = exType;
129  info.frames.assign(frames + 1, frames + 1 + n);
130  holder.emplace(exceptionId, ExceptionStats{1, std::move(info)});
131  }
132  });
133 }
134 
135 struct Initializer {
136  Initializer() {
137  registerCxaThrowCallback(throwHandler);
138  }
139 };
140 
141 Initializer initializer;
142 
143 } // namespace
def info()
Definition: deadlock.py:447
constexpr size_t kMaxFrames
Definition: StackTrace.h:26
void registerCxaThrowCallback(CxaThrowType callback)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
FOLLY_PUSH_WARNING RHS rhs
Definition: Traits.h:649
static uint64_t Hash64(const void *message, size_t length, uint64_t seed)
Definition: SpookyHashV2.h:71
ssize_t getStackTrace(uintptr_t *addresses, size_t maxAddresses)
Definition: StackTrace.cpp:25
std::ostream & operator<<(std::ostream &out, const ExceptionStats &stats)
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
Append appendTo(Collection &collection)
Definition: Base.h:824
std::vector< ExceptionStats > getExceptionStatistics()