[getStats] Reduce blocking thread-invokes from 2 to 1.

pc_->GetCallStats() does a blocking-invoke if not already on the worker
thread. By moving this call into one of the lambdas that is already
executing on the worker thread, we can "piggy-back" on it and reduce
the number of blocking-invokes by one.

No change in behavior is intended with this CL, other than performance
improvements.

Bug: webrtc:11767
Change-Id: I04eaf990be946720353adca82e87b739ec6614f2
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/193060
Reviewed-by: Philipp Hancke <philipp.hancke@googlemail.com>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Henrik Boström <hbos@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#32602}
This commit is contained in:
Henrik Boström
2020-11-13 09:05:21 +01:00
committed by Commit Bot
parent 90e958b4cd
commit 180faebe88
2 changed files with 15 additions and 24 deletions

View File

@ -1074,24 +1074,14 @@ void RTCStatsCollector::GetStatsReportInternal(
num_pending_partial_reports_ = 2; num_pending_partial_reports_ = 2;
partial_report_timestamp_us_ = cache_now_us; partial_report_timestamp_us_ = cache_now_us;
// Prepare |transceiver_stats_infos_| for use in // Prepare |transceiver_stats_infos_| and |call_stats_| for use in
// |ProducePartialResultsOnNetworkThread| and // |ProducePartialResultsOnNetworkThread| and
// |ProducePartialResultsOnSignalingThread|. // |ProducePartialResultsOnSignalingThread|.
transceiver_stats_infos_ = PrepareTransceiverStatsInfos_s_w(); PrepareTransceiverStatsInfosAndCallStats_s_w();
// Prepare |transport_names_| for use in // Prepare |transport_names_| for use in
// |ProducePartialResultsOnNetworkThread|. // |ProducePartialResultsOnNetworkThread|.
transport_names_ = PrepareTransportNames_s(); transport_names_ = PrepareTransportNames_s();
// Prepare |call_stats_| here since GetCallStats() will hop to the worker
// thread.
// TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
// network thread, where it more naturally belongs.
// TODO(https://crbug.com/webrtc/11767): In the meantime we can piggyback on
// the blocking-invoke that is already performed in
// PrepareTransceiverStatsInfos_s_w() so that we can call GetCallStats()
// without additional blocking-invokes.
call_stats_ = pc_->GetCallStats();
// Don't touch |network_report_| on the signaling thread until // Don't touch |network_report_| on the signaling thread until
// ProducePartialResultsOnNetworkThread() has signaled the // ProducePartialResultsOnNetworkThread() has signaled the
// |network_report_event_|. // |network_report_event_|.
@ -1898,11 +1888,10 @@ RTCStatsCollector::PrepareTransportCertificateStats_n(
return transport_cert_stats; return transport_cert_stats;
} }
std::vector<RTCStatsCollector::RtpTransceiverStatsInfo> void RTCStatsCollector::PrepareTransceiverStatsInfosAndCallStats_s_w() {
RTCStatsCollector::PrepareTransceiverStatsInfos_s_w() const {
RTC_DCHECK(signaling_thread_->IsCurrent()); RTC_DCHECK(signaling_thread_->IsCurrent());
std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos; transceiver_stats_infos_.clear();
// These are used to invoke GetStats for all the media channels together in // These are used to invoke GetStats for all the media channels together in
// one worker thread hop. // one worker thread hop.
std::map<cricket::VoiceMediaChannel*, std::map<cricket::VoiceMediaChannel*,
@ -1920,8 +1909,8 @@ RTCStatsCollector::PrepareTransceiverStatsInfos_s_w() const {
// Prepare stats entry. The TrackMediaInfoMap will be filled in after the // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
// stats have been fetched on the worker thread. // stats have been fetched on the worker thread.
transceiver_stats_infos.emplace_back(); transceiver_stats_infos_.emplace_back();
RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back(); RtpTransceiverStatsInfo& stats = transceiver_stats_infos_.back();
stats.transceiver = transceiver->internal(); stats.transceiver = transceiver->internal();
stats.media_type = media_type; stats.media_type = media_type;
@ -1952,9 +1941,10 @@ RTCStatsCollector::PrepareTransceiverStatsInfos_s_w() const {
} }
} }
// We jump to the worker thread and call GetStats() on each media channel. At // We jump to the worker thread and call GetStats() on each media channel as
// the same time we construct the TrackMediaInfoMaps, which also needs info // well as GetCallStats(). At the same time we construct the
// from the worker thread. This minimizes the number of thread jumps. // TrackMediaInfoMaps, which also needs info from the worker thread. This
// minimizes the number of thread jumps.
worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls; rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
@ -1971,7 +1961,7 @@ RTCStatsCollector::PrepareTransceiverStatsInfos_s_w() const {
} }
// Create the TrackMediaInfoMap for each transceiver stats object. // Create the TrackMediaInfoMap for each transceiver stats object.
for (auto& stats : transceiver_stats_infos) { for (auto& stats : transceiver_stats_infos_) {
auto transceiver = stats.transceiver; auto transceiver = stats.transceiver;
std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info; std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
std::unique_ptr<cricket::VideoMediaInfo> video_media_info; std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
@ -2003,9 +1993,9 @@ RTCStatsCollector::PrepareTransceiverStatsInfos_s_w() const {
std::move(voice_media_info), std::move(video_media_info), senders, std::move(voice_media_info), std::move(video_media_info), senders,
receivers); receivers);
} }
});
return transceiver_stats_infos; call_stats_ = pc_->GetCallStats();
});
} }
std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const { std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const {

View File

@ -215,7 +215,8 @@ class RTCStatsCollector : public virtual rtc::RefCountInterface,
PrepareTransportCertificateStats_n( PrepareTransportCertificateStats_n(
const std::map<std::string, cricket::TransportStats>& const std::map<std::string, cricket::TransportStats>&
transport_stats_by_name) const; transport_stats_by_name) const;
std::vector<RtpTransceiverStatsInfo> PrepareTransceiverStatsInfos_s_w() const; // The results are stored in |transceiver_stats_infos_| and |call_stats_|.
void PrepareTransceiverStatsInfosAndCallStats_s_w();
std::set<std::string> PrepareTransportNames_s() const; std::set<std::string> PrepareTransportNames_s() const;
// Stats gathering on a particular thread. // Stats gathering on a particular thread.