sync_point_impl.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #include "test_util/sync_point.h"
  6. #include <assert.h>
  7. #include <atomic>
  8. #include <condition_variable>
  9. #include <functional>
  10. #include <mutex>
  11. #include <string>
  12. #include <thread>
  13. #include <unordered_map>
  14. #include <unordered_set>
  15. #include "port/port.h"
  16. #include "util/random.h"
  17. #pragma once
  18. #ifndef NDEBUG
  19. namespace ROCKSDB_NAMESPACE {
  20. struct SyncPoint::Data {
  21. Data() : enabled_(false) {}
  22. // Enable proper deletion by subclasses
  23. virtual ~Data() {}
  24. // successor/predecessor map loaded from LoadDependency
  25. std::unordered_map<std::string, std::vector<std::string>> successors_;
  26. std::unordered_map<std::string, std::vector<std::string>> predecessors_;
  27. std::unordered_map<std::string, std::function<void(void*)> > callbacks_;
  28. std::unordered_map<std::string, std::vector<std::string> > markers_;
  29. std::unordered_map<std::string, std::thread::id> marked_thread_id_;
  30. std::mutex mutex_;
  31. std::condition_variable cv_;
  32. // sync points that have been passed through
  33. std::unordered_set<std::string> cleared_points_;
  34. std::atomic<bool> enabled_;
  35. int num_callbacks_running_ = 0;
  36. void LoadDependency(const std::vector<SyncPointPair>& dependencies);
  37. void LoadDependencyAndMarkers(const std::vector<SyncPointPair>& dependencies,
  38. const std::vector<SyncPointPair>& markers);
  39. bool PredecessorsAllCleared(const std::string& point);
  40. void SetCallBack(const std::string& point,
  41. const std::function<void(void*)>& callback) {
  42. std::lock_guard<std::mutex> lock(mutex_);
  43. callbacks_[point] = callback;
  44. }
  45. void ClearCallBack(const std::string& point);
  46. void ClearAllCallBacks();
  47. void EnableProcessing() {
  48. enabled_ = true;
  49. }
  50. void DisableProcessing() {
  51. enabled_ = false;
  52. }
  53. void ClearTrace() {
  54. std::lock_guard<std::mutex> lock(mutex_);
  55. cleared_points_.clear();
  56. }
  57. bool DisabledByMarker(const std::string& point,
  58. std::thread::id thread_id) {
  59. auto marked_point_iter = marked_thread_id_.find(point);
  60. return marked_point_iter != marked_thread_id_.end() &&
  61. thread_id != marked_point_iter->second;
  62. }
  63. void Process(const std::string& point, void* cb_arg);
  64. };
  65. } // namespace ROCKSDB_NAMESPACE
  66. #endif // NDEBUG