Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some more performance improvements #4431

Open
wants to merge 7 commits into
base: 25.lts.1+
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions base/task/sequence_manager/task_queue_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,9 @@ void TaskQueueImpl::ActivateDelayedFenceIfNeeded(const Task& task) {
main_thread_only().delayed_fence = absl::nullopt;
}

#if !defined(STARBOARD)
// We disable the "lifecycles" tracing group in Cobalt for performance
// reasons.
void TaskQueueImpl::MaybeReportIpcTaskQueuedFromMainThread(
const Task& pending_task) {
if (!pending_task.ipc_hash)
Expand Down Expand Up @@ -1502,6 +1505,7 @@ void TaskQueueImpl::ReportIpcTaskQueued(
&ctx, pending_task.posted_from));
});
}
#endif

void TaskQueueImpl::OnQueueUnblocked() {
DCHECK(IsQueueEnabled());
Expand Down
17 changes: 17 additions & 0 deletions base/task/sequence_manager/task_queue_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,22 @@ class BASE_EXPORT TaskQueueImpl {

// Reports the task if it was due to IPC and was posted to a disabled queue.
// This should be called after WillQueueTask has been called for the task.
#if defined(STARBOARD)
// We disable the "lifecycles" tracing group in Cobalt for performance
// reasons.
void MaybeReportIpcTaskQueuedFromMainThread(const Task& pending_task) {};
bool ShouldReportIpcTaskQueuedFromAnyThreadLocked(
base::TimeDelta* time_since_disabled)
EXCLUSIVE_LOCKS_REQUIRED(any_thread_lock_) {
return false;
}
void MaybeReportIpcTaskQueuedFromAnyThreadLocked(const Task& pending_task)
EXCLUSIVE_LOCKS_REQUIRED(any_thread_lock_) {}
void MaybeReportIpcTaskQueuedFromAnyThreadUnlocked(const Task& pending_task) {
}
void ReportIpcTaskQueued(const Task& pending_task,
const base::TimeDelta& time_since_disabled) {}
#else // !defined(STARBOARD)
void MaybeReportIpcTaskQueuedFromMainThread(const Task& pending_task);
bool ShouldReportIpcTaskQueuedFromAnyThreadLocked(
base::TimeDelta* time_since_disabled)
Expand All @@ -545,6 +561,7 @@ class BASE_EXPORT TaskQueueImpl {
void MaybeReportIpcTaskQueuedFromAnyThreadUnlocked(const Task& pending_task);
void ReportIpcTaskQueued(const Task& pending_task,
const base::TimeDelta& time_since_disabled);
#endif // !defined(STARBOARD)

// Invoked when the queue becomes enabled and not blocked by a fence.
void OnQueueUnblocked();
Expand Down
9 changes: 9 additions & 0 deletions base/task/sequenced_task_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,20 @@ bool SequencedTaskRunner::PostDelayedTaskAt(
: delayed_run_time - TimeTicks::Now());
}

#if defined(STARBOARD)
// static
scoped_refptr<SequencedTaskRunner>*
SequencedTaskRunner::null_sequenced_task_runner_(
new scoped_refptr<SequencedTaskRunner>);
#endif

// static
const scoped_refptr<SequencedTaskRunner>&
SequencedTaskRunner::GetCurrentDefault() {
#if defined(STARBOARD)
auto current_default_handle = GetCurrentDefaultHandle();
return (!current_default_handle ? *null_sequenced_task_runner_
: current_default_handle->task_runner_);
#endif
CHECK(current_default_handle)
<< "Error: This caller requires a sequenced context (i.e. the current "
Expand Down
4 changes: 4 additions & 0 deletions base/task/sequenced_task_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ class BASE_EXPORT SequencedTaskRunner : public TaskRunner {
virtual bool DeleteOrReleaseSoonInternal(const Location& from_here,
void (*deleter)(const void*),
const void* object);

#if defined(STARBOARD)
static scoped_refptr<SequencedTaskRunner>* null_sequenced_task_runner_;
#endif
};

// Sample usage with std::unique_ptr :
Expand Down
15 changes: 15 additions & 0 deletions base/threading/post_task_and_reply_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,20 @@ bool PostTaskAndReplyImpl::PostTaskAndReply(const Location& from_here,
DCHECK(task) << from_here.ToString();
DCHECK(reply) << from_here.ToString();

#if defined(STARBOARD)
// This is a slight performance optimization for Starboard.
// With Starboard, HasCurrentDefault() and GetCurrentDefault() are quite
// expensive, and GetCurrentDefault() is safe to call and will return
// nullptr when needed.
const auto& current_context = SequencedTaskRunner::GetCurrentDefault();
const bool has_sequenced_context = !!current_context;
const bool post_task_success = PostTask(
from_here,
BindOnce(&PostTaskAndReplyRelay::RunTaskAndPostReply,
PostTaskAndReplyRelay(
from_here, std::move(task), std::move(reply),
has_sequenced_context ? current_context : nullptr)));
#else
const bool has_sequenced_context = SequencedTaskRunner::HasCurrentDefault();

const bool post_task_success = PostTask(
Expand All @@ -150,6 +164,7 @@ bool PostTaskAndReplyImpl::PostTaskAndReply(const Location& from_here,
has_sequenced_context
? SequencedTaskRunner::GetCurrentDefault()
: nullptr)));
#endif

// PostTaskAndReply() requires a SequencedTaskRunner::CurrentDefaultHandle to
// post the reply. Having no SequencedTaskRunner::CurrentDefaultHandle is
Expand Down
22 changes: 22 additions & 0 deletions net/quic/platform/impl/quic_chromium_clock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

namespace quic {

#if defined(STARBOARD)
namespace {
QuicTime s_approximate_now{QuicTime::Zero()};
} // namespace
#endif

QuicChromiumClock* QuicChromiumClock::GetInstance() {
static base::NoDestructor<QuicChromiumClock> instance;
return instance.get();
Expand All @@ -19,16 +25,32 @@ QuicChromiumClock::QuicChromiumClock() = default;

QuicChromiumClock::~QuicChromiumClock() = default;

#if defined(STARBOARD)
void QuicChromiumClock::ZeroApproximateNow() {
s_approximate_now = QuicTime::Zero();
};
#endif

QuicTime QuicChromiumClock::ApproximateNow() const {
// At the moment, Chrome does not have a distinct notion of ApproximateNow().
// We should consider implementing this using MessageLoop::recent_time_.
#if defined(STARBOARD)
if (s_approximate_now.IsInitialized()) {
return s_approximate_now;
}
#endif
return Now();
}

QuicTime QuicChromiumClock::Now() const {
int64_t ticks = (base::TimeTicks::Now() - base::TimeTicks()).InMicroseconds();
DCHECK_GE(ticks, 0);
#if defined(STARBOARD)
s_approximate_now = CreateTimeFromMicroseconds(ticks);
return s_approximate_now;
#else
return CreateTimeFromMicroseconds(ticks);
#endif
}

QuicWallTime QuicChromiumClock::WallNow() const {
Expand Down
4 changes: 4 additions & 0 deletions net/quic/platform/impl/quic_chromium_clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class NET_EXPORT_PRIVATE QuicChromiumClock : public QuicClock {

~QuicChromiumClock() override;

#if defined(STARBOARD)
void ZeroApproximateNow();
#endif

// QuicClock implementation:
QuicTime ApproximateNow() const override;
QuicTime Now() const override;
Expand Down
15 changes: 14 additions & 1 deletion net/quic/quic_chromium_packet_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#include "net/quic/address_utils.h"
#include "net/third_party/quiche/src/quiche/quic/core/quic_clock.h"

#if defined(STARBOARD)
#include "net/quic/platform/impl/quic_chromium_clock.h"
#endif

namespace net {

namespace {
Expand Down Expand Up @@ -92,6 +96,7 @@ int QuicChromiumPacketReader::StartReadingMultiplePackets() {
}

bool QuicChromiumPacketReader::ProcessMultiplePacketReadResult(int result) {
quic::QuicChromiumClock::GetInstance()->ZeroApproximateNow();
read_pending_ = false;
if (result <= 0 && net_log_.IsCapturing()) {
net_log_.AddEventWithIntParams(NetLogEventType::QUIC_READ_ERROR,
Expand Down Expand Up @@ -129,7 +134,7 @@ bool QuicChromiumPacketReader::ProcessMultiplePacketReadResult(int result) {
continue;
}
quic::QuicReceivedPacket packet(read_packet->buffer, read_packet->result,
clock_->Now());
clock_->ApproximateNow());
if (!(visitor_->OnPacket(packet, quick_local_address, quick_peer_address) &&
self)) {
return false;
Expand Down Expand Up @@ -202,6 +207,9 @@ void QuicChromiumPacketReader::StartReading() {
}

bool QuicChromiumPacketReader::ProcessReadResult(int result) {
#if defined(STARBOARD)
quic::QuicChromiumClock::GetInstance()->ZeroApproximateNow();
#endif
read_pending_ = false;
if (result <= 0 && net_log_.IsCapturing()) {
net_log_.AddEventWithIntParams(NetLogEventType::QUIC_READ_ERROR,
Expand All @@ -221,7 +229,12 @@ bool QuicChromiumPacketReader::ProcessReadResult(int result) {
return visitor_->OnReadError(result, socket_);
}

#if defined(STARBOARD)
quic::QuicReceivedPacket packet(read_buffer_->data(), result,
clock_->ApproximateNow());
#else
quic::QuicReceivedPacket packet(read_buffer_->data(), result, clock_->Now());
#endif
IPEndPoint local_address;
IPEndPoint peer_address;
socket_->GetLocalAddress(&local_address);
Expand Down
6 changes: 6 additions & 0 deletions net/quic/quic_chromium_packet_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ void QuicChromiumPacketWriter::WritePacketToSocket(
}

quic::WriteResult QuicChromiumPacketWriter::WritePacketToSocketImpl() {
#if !defined(STARBOARD)
// Tracking the histogram takes 25% of the CPU time on some devices.
base::TimeTicks now = base::TimeTicks::Now();
#endif

int rv = socket_->Write(packet_.get(), packet_->size(), write_callback_,
kTrafficAnnotation);
Expand All @@ -168,12 +171,15 @@ quic::WriteResult QuicChromiumPacketWriter::WritePacketToSocketImpl() {
}
}

#if !defined(STARBOARD)
// Tracking the histogram here takes 25% of the CPU time on some devices.
base::TimeDelta delta = base::TimeTicks::Now() - now;
if (status == quic::WRITE_STATUS_OK) {
UMA_HISTOGRAM_TIMES("Net.QuicSession.PacketWriteTime.Synchronous", delta);
} else if (quic::IsWriteBlockedStatus(status)) {
UMA_HISTOGRAM_TIMES("Net.QuicSession.PacketWriteTime.Asynchronous", delta);
}
#endif

return quic::WriteResult(status, rv);
}
Expand Down
14 changes: 14 additions & 0 deletions net/quic/quic_connection_logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ void QuicConnectionLogger::OnPacketSent(
const quic::QuicFrames& retransmittable_frames,
const quic::QuicFrames& nonretransmittable_frames,
quic::QuicTime sent_time) {
#if !defined(STARBOARD)
// 4.4.1.4. Minimum Packet Size
// The payload of a UDP datagram carrying the Initial packet MUST be
// expanded to at least 1200 octets
Expand Down Expand Up @@ -211,6 +212,7 @@ void QuicConnectionLogger::OnPacketSent(
NOTREACHED();
break;
}
#endif

event_logger_.OnPacketSent(packet_number, packet_length, has_crypto_handshake,
transmission_type, encryption_level,
Expand Down Expand Up @@ -241,13 +243,17 @@ void QuicConnectionLogger::OnPacketReceived(
const quic::QuicSocketAddress& self_address,
const quic::QuicSocketAddress& peer_address,
const quic::QuicEncryptedPacket& packet) {
#if !defined(STARBOARD)
// We disable the packet receiving histogram in Cobalt for performance
// reasons.
if (local_address_from_self_.GetFamily() == ADDRESS_FAMILY_UNSPECIFIED) {
local_address_from_self_ = ToIPEndPoint(self_address);
UMA_HISTOGRAM_ENUMERATION(
"Net.QuicSession.ConnectionTypeFromSelf",
GetRealAddressFamily(ToIPEndPoint(self_address).address()),
ADDRESS_FAMILY_LAST);
}
#endif

previous_received_packet_size_ = last_received_packet_size_;
last_received_packet_size_ = packet.length();
Expand Down Expand Up @@ -301,7 +307,10 @@ void QuicConnectionLogger::OnPacketHeader(const quic::QuicPacketHeader& header,
if (!largest_received_packet_number_.IsInitialized()) {
largest_received_packet_number_ = header.packet_number;
} else if (largest_received_packet_number_ < header.packet_number) {
#if !defined(STARBOARD)
uint64_t delta = header.packet_number - largest_received_packet_number_;
// We disable the packet header histograms in Cobalt for performance
// reasons.
if (delta > 1) {
// There is a gap between the largest packet previously received and
// the current packet. This indicates either loss, or out-of-order
Expand All @@ -310,8 +319,12 @@ void QuicConnectionLogger::OnPacketHeader(const quic::QuicPacketHeader& header,
"Net.QuicSession.PacketGapReceived",
static_cast<base::HistogramBase::Sample>(delta - 1));
}
#endif
largest_received_packet_number_ = header.packet_number;
}
#if !defined(STARBOARD)
// We disable the packet header histograms in Cobalt for performance
// reasons.
if (header.packet_number - first_received_packet_number_ <
received_packets_.size()) {
received_packets_[header.packet_number - first_received_packet_number_] =
Expand All @@ -335,6 +348,7 @@ void QuicConnectionLogger::OnPacketHeader(const quic::QuicPacketHeader& header,
}
no_packet_received_after_ping_ = false;
}
#endif
last_received_packet_number_ = header.packet_number;
event_logger_.OnPacketHeader(header, receive_time, level);
}
Expand Down
14 changes: 14 additions & 0 deletions net/third_party/quiche/src/quiche/quic/core/quic_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3304,7 +3304,14 @@ bool QuicConnection::CanWrite(HasRetransmittableData retransmittable) {
return false;
}

#if defined(STARBOARD)
// Not reading the precise clock is a significant CPU usage reduction.
// Either this clock is accurate enough, or we are already throttling by more
// than the difference due to the reading task taking a long time.
QuicTime now = clock_->ApproximateNow();
#else
QuicTime now = clock_->Now();
#endif
QuicTime::Delta delay = sent_packet_manager_.TimeUntilSend(now);
if (delay.IsInfinite()) {
send_alarm_->Cancel();
Expand All @@ -3327,7 +3334,14 @@ bool QuicConnection::CanWrite(HasRetransmittableData retransmittable) {
}

QuicTime QuicConnection::CalculatePacketSentTime() {
#if defined(STARBOARD)
// Not reading the precise clock is a significant CPU usage reduction.
// Either this clock is accurate enough, or we are already throttling by more
// than the difference due to the reading task taking a long time.
const QuicTime now = clock_->ApproximateNow();
#else
const QuicTime now = clock_->Now();
#endif
if (!supports_release_time_ || per_packet_options_ == nullptr) {
// Don't change the release delay.
return now;
Expand Down
Loading