ExceptionCounterTest.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. */
  16. #include <condition_variable>
  17. #include <mutex>
  18. #include <sstream>
  19. #include <stdexcept>
  20. #include <thread>
  21. #include <folly/experimental/exception_tracer/ExceptionCounterLib.h>
  22. #include <folly/portability/GTest.h>
  23. struct MyException {};
  24. // clang-format off
  25. [[noreturn]] void bar() {
  26. throw std::runtime_error("hello");
  27. }
  28. [[noreturn]] void foo() {
  29. throw MyException();
  30. }
  31. [[noreturn]] void baz() {
  32. foo();
  33. }
  34. // clang-format on
  35. using namespace folly::exception_tracer;
  36. template <typename F>
  37. void throwAndCatch(F f) {
  38. try {
  39. f();
  40. } catch (...) {
  41. // ignore
  42. }
  43. }
  44. TEST(ExceptionCounter, oneThread) {
  45. throwAndCatch(foo);
  46. // Use volatile to prevent loop unrolling (it screws up stack frame grouping).
  47. for (volatile int i = 0; i < 10; ++i) {
  48. throwAndCatch(bar);
  49. }
  50. auto stats = getExceptionStatistics();
  51. EXPECT_EQ(stats.size(), 2);
  52. EXPECT_EQ(stats[0].count, 10);
  53. EXPECT_EQ(stats[1].count, 1);
  54. EXPECT_EQ(*(stats[0].info.type), typeid(std::runtime_error));
  55. EXPECT_EQ(*(stats[1].info.type), typeid(MyException));
  56. }
  57. TEST(ExceptionCounter, testClearExceptionStatistics) {
  58. throwAndCatch(foo);
  59. auto stats = getExceptionStatistics();
  60. EXPECT_EQ(stats.size(), 1);
  61. stats = getExceptionStatistics();
  62. EXPECT_EQ(stats.size(), 0);
  63. }
  64. TEST(ExceptionCounter, testDifferentStacks) {
  65. throwAndCatch(foo);
  66. throwAndCatch(baz);
  67. auto stats = getExceptionStatistics();
  68. EXPECT_EQ(stats.size(), 2);
  69. }
  70. TEST(ExceptionCounter, multyThreads) {
  71. constexpr size_t kNumIterations = 10000;
  72. constexpr size_t kNumThreads = 10;
  73. std::vector<std::thread> threads;
  74. threads.resize(kNumThreads);
  75. std::mutex preparedMutex;
  76. std::mutex finishedMutex;
  77. std::condition_variable preparedBarrier;
  78. std::condition_variable finishedBarrier;
  79. int preparedThreads = 0;
  80. bool finished = false;
  81. for (auto& t : threads) {
  82. t = std::thread([&]() {
  83. for (size_t i = 0; i < kNumIterations; ++i) {
  84. throwAndCatch(foo);
  85. }
  86. {
  87. std::unique_lock<std::mutex> lock(preparedMutex);
  88. ++preparedThreads;
  89. preparedBarrier.notify_one();
  90. }
  91. std::unique_lock<std::mutex> lock(finishedMutex);
  92. finishedBarrier.wait(lock, [&]() { return finished; });
  93. });
  94. }
  95. {
  96. std::unique_lock<std::mutex> lock(preparedMutex);
  97. preparedBarrier.wait(
  98. lock, [&]() { return preparedThreads == kNumThreads; });
  99. }
  100. auto stats = getExceptionStatistics();
  101. EXPECT_EQ(stats.size(), 1);
  102. EXPECT_EQ(stats[0].count, kNumIterations * kNumThreads);
  103. {
  104. std::unique_lock<std::mutex> lock(finishedMutex);
  105. finished = true;
  106. finishedBarrier.notify_all();
  107. }
  108. for (auto& t : threads) {
  109. t.join();
  110. }
  111. }