|
|
|
// Copyright 2019 The Abseil Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#ifndef ABSL_STRINGS_CORDZ_HANDLE_H_
|
|
|
|
#define ABSL_STRINGS_CORDZ_HANDLE_H_
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "absl/base/config.h"
|
|
|
|
#include "absl/base/internal/raw_logging.h"
|
|
|
|
#include "absl/base/internal/spinlock.h"
|
|
|
|
#include "absl/synchronization/mutex.h"
|
|
|
|
|
|
|
|
namespace absl {
|
|
|
|
ABSL_NAMESPACE_BEGIN
|
|
|
|
namespace cord_internal {
|
|
|
|
|
|
|
|
// This base class allows multiple types of object (CordzInfo and
|
|
|
|
// CordzSampleToken) to exist simultaneously on the delete queue (pointed to by
|
|
|
|
// global_dq_tail and traversed using dq_prev_ and dq_next_). The
|
|
|
|
// delete queue guarantees that once a profiler creates a CordzSampleToken and
|
|
|
|
// has gained visibility into a CordzInfo object, that CordzInfo object will not
|
|
|
|
// be deleted prematurely. This allows the profiler to inspect all CordzInfo
|
|
|
|
// objects that are alive without needing to hold a global lock.
|
|
|
|
class CordzHandle {
|
|
|
|
public:
|
|
|
|
CordzHandle() : CordzHandle(false) {}
|
|
|
|
|
|
|
|
bool is_snapshot() const { return is_snapshot_; }
|
|
|
|
|
Export of internal Abseil changes
--
60b8e77be4bab1bbd3b4c3b70054879229634511 by Derek Mauro <dmauro@google.com>:
Use _MSVC_LANG for some C++ dialect checks since MSVC doesn't
set __cplusplus accurately by default.
https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/
See GitHub #722.
PiperOrigin-RevId: 371362181
--
5d736accdff04db0e722f377c0d79f2d3ed53263 by Martijn Vels <mvels@google.com>:
Fix the estimated memory size for CordRepExternal
PiperOrigin-RevId: 371350380
--
eaaa1d8a167aeca67a2aa3a098a2b61a9d72172f by Martijn Vels <mvels@google.com>:
Remove flakes by not enforcing re-allocated pointers do never match original
Tests that do multiple updates could end up with the original allocated pointer on a 2nd resize, so the 'EqIfPrivate' should not assume that if we do 'not' have the capacity that all following relocations will never match the original. We only care about 'pointer unchanged if private and there is capacity', trying to establish 'pointer changed at some point due to re-allocation; is pointless.
PiperOrigin-RevId: 371338965
--
d1837bee6bade1902b095c1cbf64231668bb84c5 by Martijn Vels <mvels@google.com>:
Undo inline of small data copy in cord
This leads to a performance regression as the code is not inlined (absent hard FDO inputs), and there are no suitable tail call options.
PiperOrigin-RevId: 371332332
--
06dc64b833069efc7d18b11df607c8c22be690da by Martijn Vels <mvels@google.com>:
Add final instrumentation for Cordz and remove 'old' cordz logic.
This change instruments the last cord function for cordz. It removes the 'old' functions: set_tree, replace_tree, UpdateCordzStatistics and RecordMetrics.
PiperOrigin-RevId: 371219909
--
a5e0be538579c603052feec03e6d9910c43ea787 by Martijn Vels <mvels@google.com>:
Extend the life of CordRep* if inside a snapshot
If a snapshot (potentially) includes the current CordzInfo, we need to extent the lifetime of the CordRep*, as the snapshot 'point in time' observation of the cord should ideally be preserved.
PiperOrigin-RevId: 371146151
--
74d77a89774cd6c8ecdeebee0193b294a39383d6 by Martijn Vels <mvels@google.com>:
Instrument std::string consuming methods: ctor, operator=, Append and Prepend
This change moves the 'steal into CordRep' logic into a separate function so we can use it directly in the ctor, operator assign and append and prepend, allowing Cordz instrumentation with the proper method attributes.
The assign operator is implemented in AssignLargeString leaving the dispatch inlined in cord.h (which as a side effects also allows clean tail calls in the AssignLargeString method)
PiperOrigin-RevId: 371094756
--
b39effc45266b7ce2e7f96caa3b16cb6e3acc2dd by Martijn Vels <mvels@google.com>:
Add Cordz instrumentation to CordReader
PiperOrigin-RevId: 370990181
GitOrigin-RevId: 60b8e77be4bab1bbd3b4c3b70054879229634511
Change-Id: I96af62e6f1a643e8b1228ae01e6c84e33706bb05
4 years ago
|
|
|
// Returns true if this instance is safe to be deleted because it is either a
|
|
|
|
// snapshot, which is always safe to delete, or not included in the global
|
|
|
|
// delete queue and thus not included in any snapshot.
|
|
|
|
// Callers are responsible for making sure this instance can not be newly
|
|
|
|
// discovered by other threads. For example, CordzInfo instances first de-list
|
|
|
|
// themselves from the global CordzInfo list before determining if they are
|
|
|
|
// safe to be deleted directly.
|
|
|
|
// If SafeToDelete returns false, callers MUST use the Delete() method to
|
|
|
|
// safely queue CordzHandle instances for deletion.
|
|
|
|
bool SafeToDelete() const;
|
|
|
|
|
|
|
|
// Deletes the provided instance, or puts it on the delete queue to be deleted
|
|
|
|
// once there are no more sample tokens (snapshot) instances potentially
|
Export of internal Abseil changes
--
60b8e77be4bab1bbd3b4c3b70054879229634511 by Derek Mauro <dmauro@google.com>:
Use _MSVC_LANG for some C++ dialect checks since MSVC doesn't
set __cplusplus accurately by default.
https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/
See GitHub #722.
PiperOrigin-RevId: 371362181
--
5d736accdff04db0e722f377c0d79f2d3ed53263 by Martijn Vels <mvels@google.com>:
Fix the estimated memory size for CordRepExternal
PiperOrigin-RevId: 371350380
--
eaaa1d8a167aeca67a2aa3a098a2b61a9d72172f by Martijn Vels <mvels@google.com>:
Remove flakes by not enforcing re-allocated pointers do never match original
Tests that do multiple updates could end up with the original allocated pointer on a 2nd resize, so the 'EqIfPrivate' should not assume that if we do 'not' have the capacity that all following relocations will never match the original. We only care about 'pointer unchanged if private and there is capacity', trying to establish 'pointer changed at some point due to re-allocation; is pointless.
PiperOrigin-RevId: 371338965
--
d1837bee6bade1902b095c1cbf64231668bb84c5 by Martijn Vels <mvels@google.com>:
Undo inline of small data copy in cord
This leads to a performance regression as the code is not inlined (absent hard FDO inputs), and there are no suitable tail call options.
PiperOrigin-RevId: 371332332
--
06dc64b833069efc7d18b11df607c8c22be690da by Martijn Vels <mvels@google.com>:
Add final instrumentation for Cordz and remove 'old' cordz logic.
This change instruments the last cord function for cordz. It removes the 'old' functions: set_tree, replace_tree, UpdateCordzStatistics and RecordMetrics.
PiperOrigin-RevId: 371219909
--
a5e0be538579c603052feec03e6d9910c43ea787 by Martijn Vels <mvels@google.com>:
Extend the life of CordRep* if inside a snapshot
If a snapshot (potentially) includes the current CordzInfo, we need to extent the lifetime of the CordRep*, as the snapshot 'point in time' observation of the cord should ideally be preserved.
PiperOrigin-RevId: 371146151
--
74d77a89774cd6c8ecdeebee0193b294a39383d6 by Martijn Vels <mvels@google.com>:
Instrument std::string consuming methods: ctor, operator=, Append and Prepend
This change moves the 'steal into CordRep' logic into a separate function so we can use it directly in the ctor, operator assign and append and prepend, allowing Cordz instrumentation with the proper method attributes.
The assign operator is implemented in AssignLargeString leaving the dispatch inlined in cord.h (which as a side effects also allows clean tail calls in the AssignLargeString method)
PiperOrigin-RevId: 371094756
--
b39effc45266b7ce2e7f96caa3b16cb6e3acc2dd by Martijn Vels <mvels@google.com>:
Add Cordz instrumentation to CordReader
PiperOrigin-RevId: 370990181
GitOrigin-RevId: 60b8e77be4bab1bbd3b4c3b70054879229634511
Change-Id: I96af62e6f1a643e8b1228ae01e6c84e33706bb05
4 years ago
|
|
|
// referencing the instance. `handle` should not be null.
|
|
|
|
static void Delete(CordzHandle* handle);
|
|
|
|
|
|
|
|
// Returns the current entries in the delete queue in LIFO order.
|
|
|
|
static std::vector<const CordzHandle*> DiagnosticsGetDeleteQueue();
|
|
|
|
|
|
|
|
// Returns true if the provided handle is nullptr or guarded by this handle.
|
|
|
|
// Since the CordzSnapshot token is itself a CordzHandle, this method will
|
|
|
|
// allow tests to check if that token is keeping an arbitrary CordzHandle
|
|
|
|
// alive.
|
|
|
|
bool DiagnosticsHandleIsSafeToInspect(const CordzHandle* handle) const;
|
|
|
|
|
|
|
|
// Returns the current entries in the delete queue, in LIFO order, that are
|
|
|
|
// protected by this. CordzHandle objects are only placed on the delete queue
|
|
|
|
// after CordzHandle::Delete is called with them as an argument. Only
|
|
|
|
// CordzHandle objects that are not also CordzSnapshot objects will be
|
|
|
|
// included in the return vector. For each of the handles in the return
|
|
|
|
// vector, the earliest that their memory can be freed is when this
|
|
|
|
// CordzSnapshot object is deleted.
|
|
|
|
std::vector<const CordzHandle*> DiagnosticsGetSafeToInspectDeletedHandles();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
explicit CordzHandle(bool is_snapshot);
|
|
|
|
virtual ~CordzHandle();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Global queue data. CordzHandle stores a pointer to the global queue
|
|
|
|
// instance to harden against ODR violations.
|
|
|
|
struct Queue {
|
|
|
|
constexpr explicit Queue(absl::ConstInitType)
|
|
|
|
: mutex(absl::kConstInit,
|
|
|
|
absl::base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL) {}
|
|
|
|
|
|
|
|
absl::base_internal::SpinLock mutex;
|
|
|
|
std::atomic<CordzHandle*> dq_tail ABSL_GUARDED_BY(mutex){nullptr};
|
|
|
|
|
|
|
|
// Returns true if this delete queue is empty. This method does not acquire
|
|
|
|
// the lock, but does a 'load acquire' observation on the delete queue tail.
|
|
|
|
// It is used inside Delete() to check for the presence of a delete queue
|
|
|
|
// without holding the lock. The assumption is that the caller is in the
|
|
|
|
// state of 'being deleted', and can not be newly discovered by a concurrent
|
|
|
|
// 'being constructed' snapshot instance. Practically, this means that any
|
|
|
|
// such discovery (`find`, 'first' or 'next', etc) must have proper 'happens
|
|
|
|
// before / after' semantics and atomic fences.
|
|
|
|
bool IsEmpty() const ABSL_NO_THREAD_SAFETY_ANALYSIS {
|
|
|
|
return dq_tail.load(std::memory_order_acquire) == nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void ODRCheck() const {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
ABSL_RAW_CHECK(queue_ == &global_queue_, "ODR violation in Cord");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
ABSL_CONST_INIT static Queue global_queue_;
|
|
|
|
Queue* const queue_ = &global_queue_;
|
|
|
|
const bool is_snapshot_;
|
|
|
|
|
|
|
|
// dq_prev_ and dq_next_ require the global queue mutex to be held.
|
|
|
|
// Unfortunately we can't use thread annotations such that the thread safety
|
|
|
|
// analysis understands that queue_ and global_queue_ are one and the same.
|
|
|
|
CordzHandle* dq_prev_ = nullptr;
|
|
|
|
CordzHandle* dq_next_ = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CordzSnapshot : public CordzHandle {
|
|
|
|
public:
|
|
|
|
CordzSnapshot() : CordzHandle(true) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace cord_internal
|
|
|
|
ABSL_NAMESPACE_END
|
|
|
|
} // namespace absl
|
|
|
|
|
|
|
|
#endif // ABSL_STRINGS_CORDZ_HANDLE_H_
|