|
|
|
// 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.
|
|
|
|
#include "absl/strings/internal/cordz_handle.h"
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
#include "absl/base/internal/raw_logging.h" // For ABSL_RAW_CHECK
|
|
|
|
#include "absl/base/internal/spinlock.h"
|
|
|
|
|
|
|
|
namespace absl {
|
|
|
|
ABSL_NAMESPACE_BEGIN
|
|
|
|
namespace cord_internal {
|
|
|
|
|
|
|
|
using ::absl::base_internal::SpinLockHolder;
|
|
|
|
|
|
|
|
ABSL_CONST_INIT CordzHandle::Queue CordzHandle::global_queue_(absl::kConstInit);
|
|
|
|
|
|
|
|
CordzHandle::CordzHandle(bool is_snapshot) : is_snapshot_(is_snapshot) {
|
|
|
|
if (is_snapshot) {
|
|
|
|
SpinLockHolder lock(&queue_->mutex);
|
|
|
|
CordzHandle* dq_tail = queue_->dq_tail.load(std::memory_order_acquire);
|
|
|
|
if (dq_tail != nullptr) {
|
|
|
|
dq_prev_ = dq_tail;
|
|
|
|
dq_tail->dq_next_ = this;
|
|
|
|
}
|
|
|
|
queue_->dq_tail.store(this, std::memory_order_release);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CordzHandle::~CordzHandle() {
|
|
|
|
ODRCheck();
|
|
|
|
if (is_snapshot_) {
|
|
|
|
std::vector<CordzHandle*> to_delete;
|
|
|
|
{
|
|
|
|
SpinLockHolder lock(&queue_->mutex);
|
|
|
|
CordzHandle* next = dq_next_;
|
|
|
|
if (dq_prev_ == nullptr) {
|
|
|
|
// We were head of the queue, delete every CordzHandle until we reach
|
|
|
|
// either the end of the list, or a snapshot handle.
|
|
|
|
while (next && !next->is_snapshot_) {
|
|
|
|
to_delete.push_back(next);
|
|
|
|
next = next->dq_next_;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Another CordzHandle existed before this one, don't delete anything.
|
|
|
|
dq_prev_->dq_next_ = next;
|
|
|
|
}
|
|
|
|
if (next) {
|
|
|
|
next->dq_prev_ = dq_prev_;
|
|
|
|
} else {
|
|
|
|
queue_->dq_tail.store(dq_prev_, std::memory_order_release);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (CordzHandle* handle : to_delete) {
|
|
|
|
delete handle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
bool CordzHandle::SafeToDelete() const {
|
|
|
|
return is_snapshot_ || queue_->IsEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CordzHandle::Delete(CordzHandle* handle) {
|
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
|
|
|
assert(handle);
|
|
|
|
if (handle) {
|
|
|
|
handle->ODRCheck();
|
|
|
|
Queue* const queue = handle->queue_;
|
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
|
|
|
if (!handle->SafeToDelete()) {
|
|
|
|
SpinLockHolder lock(&queue->mutex);
|
|
|
|
CordzHandle* dq_tail = queue->dq_tail.load(std::memory_order_acquire);
|
|
|
|
if (dq_tail != nullptr) {
|
|
|
|
handle->dq_prev_ = dq_tail;
|
|
|
|
dq_tail->dq_next_ = handle;
|
|
|
|
queue->dq_tail.store(handle, std::memory_order_release);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete handle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const CordzHandle*> CordzHandle::DiagnosticsGetDeleteQueue() {
|
|
|
|
std::vector<const CordzHandle*> handles;
|
|
|
|
SpinLockHolder lock(&global_queue_.mutex);
|
|
|
|
CordzHandle* dq_tail = global_queue_.dq_tail.load(std::memory_order_acquire);
|
|
|
|
for (const CordzHandle* p = dq_tail; p; p = p->dq_prev_) {
|
|
|
|
handles.push_back(p);
|
|
|
|
}
|
|
|
|
return handles;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CordzHandle::DiagnosticsHandleIsSafeToInspect(
|
|
|
|
const CordzHandle* handle) const {
|
|
|
|
ODRCheck();
|
|
|
|
if (!is_snapshot_) return false;
|
|
|
|
if (handle == nullptr) return true;
|
|
|
|
if (handle->is_snapshot_) return false;
|
|
|
|
bool snapshot_found = false;
|
|
|
|
SpinLockHolder lock(&queue_->mutex);
|
|
|
|
for (const CordzHandle* p = queue_->dq_tail; p; p = p->dq_prev_) {
|
|
|
|
if (p == handle) return !snapshot_found;
|
|
|
|
if (p == this) snapshot_found = true;
|
|
|
|
}
|
|
|
|
ABSL_ASSERT(snapshot_found); // Assert that 'this' is in delete queue.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const CordzHandle*>
|
|
|
|
CordzHandle::DiagnosticsGetSafeToInspectDeletedHandles() {
|
|
|
|
ODRCheck();
|
|
|
|
std::vector<const CordzHandle*> handles;
|
|
|
|
if (!is_snapshot()) {
|
|
|
|
return handles;
|
|
|
|
}
|
|
|
|
|
|
|
|
SpinLockHolder lock(&queue_->mutex);
|
|
|
|
for (const CordzHandle* p = dq_next_; p != nullptr; p = p->dq_next_) {
|
|
|
|
if (!p->is_snapshot()) {
|
|
|
|
handles.push_back(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return handles;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cord_internal
|
|
|
|
ABSL_NAMESPACE_END
|
|
|
|
} // namespace absl
|