cordz_handle.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2019 The Abseil Authors.
  2. //
  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. // https://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. #ifndef ABSL_STRINGS_CORDZ_HANDLE_H_
  15. #define ABSL_STRINGS_CORDZ_HANDLE_H_
  16. #include <atomic>
  17. #include <vector>
  18. #include "absl/base/config.h"
  19. #include "absl/base/internal/raw_logging.h"
  20. #include "absl/base/internal/spinlock.h"
  21. #include "absl/synchronization/mutex.h"
  22. namespace absl {
  23. ABSL_NAMESPACE_BEGIN
  24. namespace cord_internal {
  25. // This base class allows multiple types of object (CordzInfo and
  26. // CordzSampleToken) to exist simultaneously on the delete queue (pointed to by
  27. // global_dq_tail and traversed using dq_prev_ and dq_next_). The
  28. // delete queue guarantees that once a profiler creates a CordzSampleToken and
  29. // has gained visibility into a CordzInfo object, that CordzInfo object will not
  30. // be deleted prematurely. This allows the profiler to inspect all CordzInfo
  31. // objects that are alive without needing to hold a global lock.
  32. class CordzHandle {
  33. public:
  34. CordzHandle() : CordzHandle(false) {}
  35. bool is_snapshot() const { return is_snapshot_; }
  36. // Returns true if this instance is safe to be deleted because it is either a
  37. // snapshot, which is always safe to delete, or not included in the global
  38. // delete queue and thus not included in any snapshot.
  39. // Callers are responsible for making sure this instance can not be newly
  40. // discovered by other threads. For example, CordzInfo instances first de-list
  41. // themselves from the global CordzInfo list before determining if they are
  42. // safe to be deleted directly.
  43. // If SafeToDelete returns false, callers MUST use the Delete() method to
  44. // safely queue CordzHandle instances for deletion.
  45. bool SafeToDelete() const;
  46. // Deletes the provided instance, or puts it on the delete queue to be deleted
  47. // once there are no more sample tokens (snapshot) instances potentially
  48. // referencing the instance. `handle` should not be null.
  49. static void Delete(CordzHandle* handle);
  50. // Returns the current entries in the delete queue in LIFO order.
  51. static std::vector<const CordzHandle*> DiagnosticsGetDeleteQueue();
  52. // Returns true if the provided handle is nullptr or guarded by this handle.
  53. // Since the CordzSnapshot token is itself a CordzHandle, this method will
  54. // allow tests to check if that token is keeping an arbitrary CordzHandle
  55. // alive.
  56. bool DiagnosticsHandleIsSafeToInspect(const CordzHandle* handle) const;
  57. // Returns the current entries in the delete queue, in LIFO order, that are
  58. // protected by this. CordzHandle objects are only placed on the delete queue
  59. // after CordzHandle::Delete is called with them as an argument. Only
  60. // CordzHandle objects that are not also CordzSnapshot objects will be
  61. // included in the return vector. For each of the handles in the return
  62. // vector, the earliest that their memory can be freed is when this
  63. // CordzSnapshot object is deleted.
  64. std::vector<const CordzHandle*> DiagnosticsGetSafeToInspectDeletedHandles();
  65. protected:
  66. explicit CordzHandle(bool is_snapshot);
  67. virtual ~CordzHandle();
  68. private:
  69. // Global queue data. CordzHandle stores a pointer to the global queue
  70. // instance to harden against ODR violations.
  71. struct Queue {
  72. constexpr explicit Queue(absl::ConstInitType)
  73. : mutex(absl::kConstInit,
  74. absl::base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL) {}
  75. absl::base_internal::SpinLock mutex;
  76. std::atomic<CordzHandle*> dq_tail ABSL_GUARDED_BY(mutex){nullptr};
  77. // Returns true if this delete queue is empty. This method does not acquire
  78. // the lock, but does a 'load acquire' observation on the delete queue tail.
  79. // It is used inside Delete() to check for the presence of a delete queue
  80. // without holding the lock. The assumption is that the caller is in the
  81. // state of 'being deleted', and can not be newly discovered by a concurrent
  82. // 'being constructed' snapshot instance. Practically, this means that any
  83. // such discovery (`find`, 'first' or 'next', etc) must have proper 'happens
  84. // before / after' semantics and atomic fences.
  85. bool IsEmpty() const ABSL_NO_THREAD_SAFETY_ANALYSIS {
  86. return dq_tail.load(std::memory_order_acquire) == nullptr;
  87. }
  88. };
  89. void ODRCheck() const {
  90. #ifndef NDEBUG
  91. ABSL_RAW_CHECK(queue_ == &global_queue_, "ODR violation in Cord");
  92. #endif
  93. }
  94. ABSL_CONST_INIT static Queue global_queue_;
  95. Queue* const queue_ = &global_queue_;
  96. const bool is_snapshot_;
  97. // dq_prev_ and dq_next_ require the global queue mutex to be held.
  98. // Unfortunately we can't use thread annotations such that the thread safety
  99. // analysis understands that queue_ and global_queue_ are one and the same.
  100. CordzHandle* dq_prev_ = nullptr;
  101. CordzHandle* dq_next_ = nullptr;
  102. };
  103. class CordzSnapshot : public CordzHandle {
  104. public:
  105. CordzSnapshot() : CordzHandle(true) {}
  106. };
  107. } // namespace cord_internal
  108. ABSL_NAMESPACE_END
  109. } // namespace absl
  110. #endif // ABSL_STRINGS_CORDZ_HANDLE_H_