Apply clang-format to folly/experimental/exception_tracer/
[folly.git] / folly / experimental / exception_tracer / ExceptionCounterLib.cpp
1 /*
2  * Copyright 2017 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  */
16
17 #include <folly/experimental/exception_tracer/ExceptionCounterLib.h>
18
19 #include <iosfwd>
20 #include <unordered_map>
21
22 #include <folly/RWSpinLock.h>
23 #include <folly/Range.h>
24 #include <folly/Synchronized.h>
25 #include <folly/ThreadLocal.h>
26 #include <folly/hash/SpookyHashV2.h>
27
28 #include <folly/experimental/exception_tracer/ExceptionTracerLib.h>
29 #include <folly/experimental/exception_tracer/StackTrace.h>
30 #include <folly/experimental/symbolizer/Symbolizer.h>
31
32 using namespace folly::exception_tracer;
33
34 namespace {
35
36 // We use the hash of stack trace and exception type to uniquely
37 // identify the exception.
38 using ExceptionId = uint64_t;
39
40 using ExceptionStatsHolderType =
41     std::unordered_map<ExceptionId, ExceptionStats>;
42
43 struct ExceptionStatsStorage {
44   void appendTo(ExceptionStatsHolderType& data) {
45     ExceptionStatsHolderType tempHolder;
46     statsHolder->swap(tempHolder);
47
48     for (const auto& myData : tempHolder) {
49       auto inserted = data.insert(myData);
50       if (!inserted.second) {
51         inserted.first->second.count += myData.second.count;
52       }
53     }
54   }
55
56   folly::Synchronized<ExceptionStatsHolderType, folly::RWSpinLock> statsHolder;
57 };
58
59 class Tag {};
60
61 folly::ThreadLocal<ExceptionStatsStorage, Tag> gExceptionStats;
62
63 } // namespace
64
65 namespace folly {
66 namespace exception_tracer {
67
68 std::vector<ExceptionStats> getExceptionStatistics() {
69   ExceptionStatsHolderType accumulator;
70   for (auto& threadStats : gExceptionStats.accessAllThreads()) {
71     threadStats.appendTo(accumulator);
72   }
73
74   std::vector<ExceptionStats> result;
75   result.reserve(accumulator.size());
76   for (auto& item : accumulator) {
77     result.push_back(std::move(item.second));
78   }
79
80   std::sort(
81       result.begin(),
82       result.end(),
83       [](const ExceptionStats& lhs, const ExceptionStats& rhs) {
84         return lhs.count > rhs.count;
85       });
86
87   return result;
88 }
89
90 std::ostream& operator<<(std::ostream& out, const ExceptionStats& stats) {
91   out << "Exception report: \n"
92       << "Exception count: " << stats.count << "\n"
93       << stats.info;
94
95   return out;
96 }
97
98 } // namespace exception_tracer
99 } // namespace folly
100
101 namespace {
102
103 /*
104  * This handler gathers statistics on all exceptions thrown by the program
105  * Information is being stored in thread local storage.
106  */
107 void throwHandler(void*, std::type_info* exType, void (*)(void*)) noexcept {
108   // This array contains the exception type and the stack frame
109   // pointers so they get all hashed together.
110   uintptr_t frames[kMaxFrames + 1];
111   frames[0] = reinterpret_cast<uintptr_t>(exType);
112   auto n = folly::symbolizer::getStackTrace(frames + 1, kMaxFrames);
113
114   if (n == -1) {
115     // If we fail to collect the stack trace for this exception we
116     // just log it under empty stack trace.
117     n = 0;
118   }
119
120   auto exceptionId =
121       folly::hash::SpookyHashV2::Hash64(frames, (n + 1) * sizeof(frames[0]), 0);
122
123   SYNCHRONIZED(holder, gExceptionStats->statsHolder) {
124     auto it = holder.find(exceptionId);
125     if (it != holder.end()) {
126       ++it->second.count;
127     } else {
128       ExceptionInfo info;
129       info.type = exType;
130       info.frames.assign(frames + 1, frames + 1 + n);
131       holder.emplace(exceptionId, ExceptionStats{1, std::move(info)});
132     }
133   }
134 }
135
136 struct Initializer {
137   Initializer() {
138     registerCxaThrowCallback(throwHandler);
139   }
140 };
141
142 Initializer initializer;
143
144 } // namespace