Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: webrtc/logging/rtc_event_log/rtc_event_log.cc

Issue 3007473002: Make RtcEventLogImpl to use a TaskQueue instead of a helper-thread (Closed)
Patch Set: Response to comments. Created 3 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/logging/rtc_event_log/rtc_event_log.h" 11 #include "webrtc/logging/rtc_event_log/rtc_event_log.h"
12 12
13 #include <atomic>
14 #include <deque>
15 #include <functional>
13 #include <limits> 16 #include <limits>
17 #include <memory>
14 #include <utility> 18 #include <utility>
15 #include <vector> 19 #include <vector>
16 20
17 #include "webrtc/logging/rtc_event_log/rtc_event_log_helper_thread.h"
18 #include "webrtc/modules/audio_coding/audio_network_adaptor/include/audio_networ k_adaptor.h" 21 #include "webrtc/modules/audio_coding/audio_network_adaptor/include/audio_networ k_adaptor.h"
19 #include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h" 22 #include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h"
20 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" 23 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
21 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" 24 #include "webrtc/modules/rtp_rtcp/source/byte_io.h"
22 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/app.h" 25 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/app.h"
23 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/bye.h" 26 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/bye.h"
24 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/common_header.h" 27 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/common_header.h"
25 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/extended_jitter_report.h" 28 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/extended_jitter_report.h"
26 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/extended_reports.h" 29 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
27 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/psfb.h" 30 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/psfb.h"
28 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/receiver_report.h" 31 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
29 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/rtpfb.h" 32 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/rtpfb.h"
30 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sdes.h" 33 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sdes.h"
31 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sender_report.h" 34 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
32 #include "webrtc/rtc_base/atomicops.h"
33 #include "webrtc/rtc_base/checks.h" 35 #include "webrtc/rtc_base/checks.h"
34 #include "webrtc/rtc_base/constructormagic.h" 36 #include "webrtc/rtc_base/constructormagic.h"
35 #include "webrtc/rtc_base/event.h" 37 #include "webrtc/rtc_base/event.h"
38 #include "webrtc/rtc_base/ignore_wundef.h"
36 #include "webrtc/rtc_base/logging.h" 39 #include "webrtc/rtc_base/logging.h"
37 #include "webrtc/rtc_base/protobuf_utils.h" 40 #include "webrtc/rtc_base/protobuf_utils.h"
38 #include "webrtc/rtc_base/swap_queue.h" 41 #include "webrtc/rtc_base/ptr_util.h"
39 #include "webrtc/rtc_base/thread_checker.h" 42 #include "webrtc/rtc_base/sequenced_task_checker.h"
43 #include "webrtc/rtc_base/task_queue.h"
44 #include "webrtc/rtc_base/thread_annotations.h"
40 #include "webrtc/rtc_base/timeutils.h" 45 #include "webrtc/rtc_base/timeutils.h"
41 #include "webrtc/system_wrappers/include/file_wrapper.h" 46 #include "webrtc/system_wrappers/include/file_wrapper.h"
47 #include "webrtc/typedefs.h"
42 48
43 #ifdef ENABLE_RTC_EVENT_LOG 49 #ifdef ENABLE_RTC_EVENT_LOG
44 // *.pb.h files are generated at build-time by the protobuf compiler. 50 // *.ph.h files are generated at build-time by the protobuf compiler.
51 RTC_PUSH_IGNORING_WUNDEF()
45 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD 52 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD
46 #include "external/webrtc/webrtc/logging/rtc_event_log/rtc_event_log.pb.h" 53 #include "external/webrtc/webrtc/logging/rtc_event_log/rtc_event_log.pb.h"
47 #else 54 #else
48 #include "webrtc/logging/rtc_event_log/rtc_event_log.pb.h" 55 #include "webrtc/logging/rtc_event_log/rtc_event_log.pb.h"
49 #endif 56 #endif
57 RTC_POP_IGNORING_WUNDEF()
50 #endif 58 #endif
51 59
52 namespace webrtc { 60 namespace webrtc {
53 61
54 #ifdef ENABLE_RTC_EVENT_LOG 62 #ifdef ENABLE_RTC_EVENT_LOG
55 63
64 namespace {
65 const int kEventsInHistory = 10000;
66
67 bool IsConfigEvent(const rtclog::Event& event) {
68 rtclog::Event_EventType event_type = event.type();
69 return event_type == rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT ||
70 event_type == rtclog::Event::VIDEO_SENDER_CONFIG_EVENT ||
71 event_type == rtclog::Event::AUDIO_RECEIVER_CONFIG_EVENT ||
72 event_type == rtclog::Event::AUDIO_SENDER_CONFIG_EVENT;
73 }
74
75 // This class exists because C++11 doesn't allow transferring a unique_ptr to
76 // a lambda (a copy constructor is required).
77 class RtcEventLogTask final : public rtc::QueuedTask {
78 public:
79 RtcEventLogTask(std::unique_ptr<rtclog::Event> event,
80 std::function<void(std::unique_ptr<rtclog::Event>)> handler)
81 : event_(std::move(event)), handler_(handler) {}
82
83 bool Run() override {
84 handler_(std::move(event_));
85 return true;
86 }
87
88 private:
89 std::unique_ptr<rtclog::Event> event_;
90 std::function<void(std::unique_ptr<rtclog::Event>)> handler_;
91 };
92
93 // This class exists because C++11 doesn't allow transferring a unique_ptr to
94 // a lambda (a copy constructor is required).
95 class RtcEventLogFileTask final : public rtc::QueuedTask {
96 public:
97 RtcEventLogFileTask(std::unique_ptr<FileWrapper> file,
98 std::function<void(std::unique_ptr<FileWrapper>)> handler)
99 : file_(std::move(file)),
100 handler_(handler) {}
101
102 bool Run() override {
103 handler_(std::move(file_));
104 return true;
105 }
106
107 private:
108 std::unique_ptr<FileWrapper> file_;
109 std::function<void(std::unique_ptr<FileWrapper>)> handler_;
110 };
111 } // namespace
112
56 class RtcEventLogImpl final : public RtcEventLog { 113 class RtcEventLogImpl final : public RtcEventLog {
57 friend std::unique_ptr<RtcEventLog> RtcEventLog::Create(); 114 friend std::unique_ptr<RtcEventLog> RtcEventLog::Create();
58 115
59 public: 116 public:
60 ~RtcEventLogImpl() override; 117 ~RtcEventLogImpl() override;
61 118
62 bool StartLogging(const std::string& file_name, 119 bool StartLogging(const std::string& file_name,
63 int64_t max_size_bytes) override; 120 int64_t max_size_bytes) override;
64 bool StartLogging(rtc::PlatformFile platform_file, 121 bool StartLogging(rtc::PlatformFile platform_file,
65 int64_t max_size_bytes) override; 122 int64_t max_size_bytes) override;
(...skipping 22 matching lines...) Expand all
88 const AudioEncoderRuntimeConfig& config) override; 145 const AudioEncoderRuntimeConfig& config) override;
89 void LogProbeClusterCreated(int id, 146 void LogProbeClusterCreated(int id,
90 int bitrate_bps, 147 int bitrate_bps,
91 int min_probes, 148 int min_probes,
92 int min_bytes) override; 149 int min_bytes) override;
93 void LogProbeResultSuccess(int id, int bitrate_bps) override; 150 void LogProbeResultSuccess(int id, int bitrate_bps) override;
94 void LogProbeResultFailure(int id, 151 void LogProbeResultFailure(int id,
95 ProbeFailureReason failure_reason) override; 152 ProbeFailureReason failure_reason) override;
96 153
97 private: 154 private:
98 // Private constructor to ensure that creation is done by RtcEventLog::Create. 155 void StartLoggingInternal(std::unique_ptr<FileWrapper> file,
99 RtcEventLogImpl(); 156 int64_t max_size_bytes);
157
158 RtcEventLogImpl(); // Creation is done by RtcEventLog::Create.
100 159
101 void StoreEvent(std::unique_ptr<rtclog::Event> event); 160 void StoreEvent(std::unique_ptr<rtclog::Event> event);
161 void LogEvent(std::unique_ptr<rtclog::Event> rtclog_event);
102 void LogProbeResult(int id, 162 void LogProbeResult(int id,
103 rtclog::BweProbeResult::ResultType result, 163 rtclog::BweProbeResult::ResultType result,
104 int bitrate_bps); 164 int bitrate_bps);
105 165
106 static volatile int log_count_; 166 // Attempts to append an event to the output protobuf string, while observing
167 // the (potential) limitation on file size. (False is returned when observing
168 // the limitation disallows appending the event.)
169 bool AppendEventToString(rtclog::Event* event,
170 ProtoString* output_string) RTC_WARN_UNUSED_RESULT;
107 171
108 // Message queue for passing control messages to the logging thread. 172 void LogToMemory(std::unique_ptr<rtclog::Event> event);
109 SwapQueue<RtcEventLogHelperThread::ControlMessage> message_queue_;
110 173
111 // Message queue for passing events to the logging thread. 174 void StartLogFile();
112 SwapQueue<std::unique_ptr<rtclog::Event> > event_queue_; 175 void LogToFile(std::unique_ptr<rtclog::Event> event);
176 void StopLogFile(int64_t stop_time);
113 177
114 RtcEventLogHelperThread helper_thread_; 178 // TODO(eladalon): Known issue - there's a race over |log_count_|.
115 rtc::ThreadChecker thread_checker_; 179 static std::atomic<int> log_count_;
180
181 rtc::SequencedTaskChecker worker_sequence_checker_;
182 rtc::SequencedTaskChecker owner_sequence_checker_;
183
184 // History containing all past configuration events.
185 std::vector<std::unique_ptr<rtclog::Event>> config_history_
186 GUARDED_BY(worker_sequence_checker_);
187
188 // History containing the most recent (non-configuration) events (~ 10 s).
189 std::deque<std::unique_ptr<rtclog::Event>> history_
190 GUARDED_BY(worker_sequence_checker_);
191
192 std::unique_ptr<FileWrapper> file_ GUARDED_BY(worker_sequence_checker_);
193
194 size_t max_size_bytes_ GUARDED_BY(worker_sequence_checker_);
195 size_t written_bytes_ GUARDED_BY(worker_sequence_checker_);
196
197 // Keep this last to ensure it destructs first, or else tasks living on the
198 // queue might access other members after they've been torn down.
199 rtc::TaskQueue task_queue_;
116 200
117 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogImpl); 201 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogImpl);
118 }; 202 };
119 203
120 namespace { 204 namespace {
121 // The functions in this namespace convert enums from the runtime format 205 // The functions in this namespace convert enums from the runtime format
122 // that the rest of the WebRtc project can use, to the corresponding 206 // that the rest of the WebRtc project can use, to the corresponding
123 // serialized enum which is defined by the protobuf. 207 // serialized enum which is defined by the protobuf.
124 208
125 rtclog::VideoReceiveConfig_RtcpMode ConvertRtcpMode(RtcpMode rtcp_mode) { 209 rtclog::VideoReceiveConfig_RtcpMode ConvertRtcpMode(RtcpMode rtcp_mode) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 return rtclog::BweProbeResult::INVALID_SEND_RECEIVE_INTERVAL; 241 return rtclog::BweProbeResult::INVALID_SEND_RECEIVE_INTERVAL;
158 case kInvalidSendReceiveRatio: 242 case kInvalidSendReceiveRatio:
159 return rtclog::BweProbeResult::INVALID_SEND_RECEIVE_RATIO; 243 return rtclog::BweProbeResult::INVALID_SEND_RECEIVE_RATIO;
160 case kTimeout: 244 case kTimeout:
161 return rtclog::BweProbeResult::TIMEOUT; 245 return rtclog::BweProbeResult::TIMEOUT;
162 } 246 }
163 RTC_NOTREACHED(); 247 RTC_NOTREACHED();
164 return rtclog::BweProbeResult::SUCCESS; 248 return rtclog::BweProbeResult::SUCCESS;
165 } 249 }
166 250
167 // The RTP and RTCP buffers reserve space for twice the expected number of
168 // sent packets because they also contain received packets.
169 static const int kEventsPerSecond = 1000;
170 static const int kControlMessagesPerSecond = 10;
171 } // namespace 251 } // namespace
172 252
173 volatile int RtcEventLogImpl::log_count_ = 0; 253 std::atomic<int> RtcEventLogImpl::log_count_(0);
174 254
175 // RtcEventLogImpl member functions.
176 RtcEventLogImpl::RtcEventLogImpl() 255 RtcEventLogImpl::RtcEventLogImpl()
177 // Allocate buffers for roughly one second of history. 256 : file_(FileWrapper::Create()),
178 : message_queue_(kControlMessagesPerSecond), 257 max_size_bytes_(std::numeric_limits<decltype(max_size_bytes_)>::max()),
179 event_queue_(kEventsPerSecond), 258 written_bytes_(0),
180 helper_thread_(&message_queue_, &event_queue_), 259 task_queue_("rtc_event_log") {
181 thread_checker_() { 260 worker_sequence_checker_.Detach();
182 thread_checker_.DetachFromThread();
183 } 261 }
184 262
185 RtcEventLogImpl::~RtcEventLogImpl() { 263 RtcEventLogImpl::~RtcEventLogImpl() {
186 // The RtcEventLogHelperThread destructor closes the file 264 RTC_DCHECK_CALLED_SEQUENTIALLY(&owner_sequence_checker_);
187 // and waits for the thread to terminate. 265
188 int count = rtc::AtomicOps::Decrement(&RtcEventLogImpl::log_count_); 266 // If we're logging to the file, this will stop that. Blocking function.
267 StopLogging();
268
269 int count = std::atomic_fetch_sub(&RtcEventLogImpl::log_count_, 1) - 1;
189 RTC_DCHECK_GE(count, 0); 270 RTC_DCHECK_GE(count, 0);
190 } 271 }
191 272
192 bool RtcEventLogImpl::StartLogging(const std::string& file_name, 273 bool RtcEventLogImpl::StartLogging(const std::string& file_name,
193 int64_t max_size_bytes) { 274 int64_t max_size_bytes) {
194 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 275 RTC_DCHECK_CALLED_SEQUENTIALLY(&owner_sequence_checker_);
195 RtcEventLogHelperThread::ControlMessage message; 276
196 message.message_type = RtcEventLogHelperThread::ControlMessage::START_FILE; 277 auto file = rtc::WrapUnique<FileWrapper>(FileWrapper::Create());
197 message.max_size_bytes = max_size_bytes <= 0 278 if (!file->OpenFile(file_name.c_str(), false)) {
198 ? std::numeric_limits<int64_t>::max()
199 : max_size_bytes;
200 message.start_time = rtc::TimeMicros();
201 message.stop_time = std::numeric_limits<int64_t>::max();
202 message.file.reset(FileWrapper::Create());
203 if (!message.file->OpenFile(file_name.c_str(), false)) {
204 LOG(LS_ERROR) << "Can't open file. WebRTC event log not started."; 279 LOG(LS_ERROR) << "Can't open file. WebRTC event log not started.";
205 return false; 280 return false;
206 } 281 }
207 if (!message_queue_.Insert(&message)) { 282
208 LOG(LS_ERROR) << "Message queue full. Can't start logging."; 283 StartLoggingInternal(std::move(file), max_size_bytes);
209 return false; 284
210 }
211 helper_thread_.SignalNewEvent();
212 LOG(LS_INFO) << "Starting WebRTC event log.";
213 return true; 285 return true;
214 } 286 }
215 287
216 bool RtcEventLogImpl::StartLogging(rtc::PlatformFile platform_file, 288 bool RtcEventLogImpl::StartLogging(rtc::PlatformFile platform_file,
217 int64_t max_size_bytes) { 289 int64_t max_size_bytes) {
218 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 290 RTC_DCHECK_CALLED_SEQUENTIALLY(&owner_sequence_checker_);
219 RtcEventLogHelperThread::ControlMessage message; 291
220 message.message_type = RtcEventLogHelperThread::ControlMessage::START_FILE; 292 auto file = rtc::WrapUnique<FileWrapper>(FileWrapper::Create());
221 message.max_size_bytes = max_size_bytes <= 0
222 ? std::numeric_limits<int64_t>::max()
223 : max_size_bytes;
224 message.start_time = rtc::TimeMicros();
225 message.stop_time = std::numeric_limits<int64_t>::max();
226 message.file.reset(FileWrapper::Create());
227 FILE* file_handle = rtc::FdopenPlatformFileForWriting(platform_file); 293 FILE* file_handle = rtc::FdopenPlatformFileForWriting(platform_file);
228 if (!file_handle) { 294 if (!file_handle) {
229 LOG(LS_ERROR) << "Can't open file. WebRTC event log not started."; 295 LOG(LS_ERROR) << "Can't open file. WebRTC event log not started.";
230 // Even though we failed to open a FILE*, the platform_file is still open 296 // Even though we failed to open a FILE*, the platform_file is still open
231 // and needs to be closed. 297 // and needs to be closed.
232 if (!rtc::ClosePlatformFile(platform_file)) { 298 if (!rtc::ClosePlatformFile(platform_file)) {
233 LOG(LS_ERROR) << "Can't close file."; 299 LOG(LS_ERROR) << "Can't close file.";
234 } 300 }
235 return false; 301 return false;
236 } 302 }
237 if (!message.file->OpenFromFileHandle(file_handle)) { 303 if (!file->OpenFromFileHandle(file_handle)) {
238 LOG(LS_ERROR) << "Can't open file. WebRTC event log not started."; 304 LOG(LS_ERROR) << "Can't open file. WebRTC event log not started.";
239 return false; 305 return false;
240 } 306 }
241 if (!message_queue_.Insert(&message)) { 307
242 LOG(LS_ERROR) << "Message queue full. Can't start logging."; 308 StartLoggingInternal(std::move(file), max_size_bytes);
243 return false; 309
244 }
245 helper_thread_.SignalNewEvent();
246 LOG(LS_INFO) << "Starting WebRTC event log.";
247 return true; 310 return true;
248 } 311 }
249 312
250 void RtcEventLogImpl::StopLogging() { 313 void RtcEventLogImpl::StopLogging() {
251 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 314 RTC_DCHECK_CALLED_SEQUENTIALLY(&owner_sequence_checker_);
252 RtcEventLogHelperThread::ControlMessage message; 315
253 message.message_type = RtcEventLogHelperThread::ControlMessage::STOP_FILE;
254 message.stop_time = rtc::TimeMicros();
255 while (!message_queue_.Insert(&message)) {
256 // TODO(terelius): We would like to have a blocking Insert function in the
257 // SwapQueue, but for the time being we will just clear any previous
258 // messages.
259 // Since StopLogging waits for the thread, it is essential that we don't
260 // clear any STOP_FILE messages. To ensure that there is only one call at a
261 // time, we require that all calls to StopLogging are made on the same
262 // thread.
263 LOG(LS_ERROR) << "Message queue full. Clearing queue to stop logging.";
264 message_queue_.Clear();
265 }
266 LOG(LS_INFO) << "Stopping WebRTC event log."; 316 LOG(LS_INFO) << "Stopping WebRTC event log.";
267 helper_thread_.WaitForFileFinished(); 317
318 const int64_t stop_time = rtc::TimeMicros();
319
320 task_queue_.PostTask([this, stop_time]() {
321 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
322 if (file_->is_open()) {
323 StopLogFile(stop_time);
324 }
325 });
326
327 // Wait for an indication that the previous task, which stops the log file,
328 // has been executed to completion.
329 rtc::Event file_finished(true, false);
330 task_queue_.PostTask([this, &file_finished]() {
331 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
332 file_finished.Set();
333 });
334 file_finished.Wait(rtc::Event::kForever);
335
336 LOG(LS_INFO) << "WebRTC event log successfully stopped.";
268 } 337 }
269 338
270 void RtcEventLogImpl::LogVideoReceiveStreamConfig( 339 void RtcEventLogImpl::LogVideoReceiveStreamConfig(
271 const rtclog::StreamConfig& config) { 340 const rtclog::StreamConfig& config) {
272 std::unique_ptr<rtclog::Event> event(new rtclog::Event()); 341 std::unique_ptr<rtclog::Event> event(new rtclog::Event());
273 event->set_timestamp_us(rtc::TimeMicros()); 342 event->set_timestamp_us(rtc::TimeMicros());
274 event->set_type(rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT); 343 event->set_type(rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT);
275 344
276 rtclog::VideoReceiveConfig* receiver_config = 345 rtclog::VideoReceiveConfig* receiver_config =
277 event->mutable_video_receiver_config(); 346 event->mutable_video_receiver_config();
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 event->set_type(rtclog::Event::BWE_PROBE_RESULT_EVENT); 627 event->set_type(rtclog::Event::BWE_PROBE_RESULT_EVENT);
559 628
560 auto probe_result = event->mutable_probe_result(); 629 auto probe_result = event->mutable_probe_result();
561 probe_result->set_id(id); 630 probe_result->set_id(id);
562 probe_result->set_result(result); 631 probe_result->set_result(result);
563 if (result == rtclog::BweProbeResult::SUCCESS) 632 if (result == rtclog::BweProbeResult::SUCCESS)
564 probe_result->set_bitrate_bps(bitrate_bps); 633 probe_result->set_bitrate_bps(bitrate_bps);
565 StoreEvent(std::move(event)); 634 StoreEvent(std::move(event));
566 } 635 }
567 636
637 void RtcEventLogImpl::StartLoggingInternal(std::unique_ptr<FileWrapper> file,
638 int64_t max_size_bytes) {
639 LOG(LS_INFO) << "Starting WebRTC event log.";
640
641 max_size_bytes = (max_size_bytes <= 0) ? std::numeric_limits<int64_t>::max()
642 : max_size_bytes;
643 auto file_handler = [this, max_size_bytes](
644 std::unique_ptr<FileWrapper> file) {
645 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
646 if (!file_->is_open()) {
647 max_size_bytes_ = max_size_bytes;
648 file_= std::move(file);
649 StartLogFile();
650 } else {
651 // Already started. Ignore message and close file handle.
652 file->CloseFile();
653 }
654 };
655 auto task = rtc::WrapUnique<rtc::QueuedTask>(
656 new RtcEventLogFileTask(std::move(file), file_handler));
657 task_queue_.PostTask(std::move(task));
658 }
659
568 void RtcEventLogImpl::StoreEvent(std::unique_ptr<rtclog::Event> event) { 660 void RtcEventLogImpl::StoreEvent(std::unique_ptr<rtclog::Event> event) {
569 RTC_DCHECK(event.get() != nullptr); 661 RTC_DCHECK(event);
570 if (!event_queue_.Insert(&event)) { 662 // LogEvent is necessary because lambdas in C++11 need their captured
571 LOG(LS_ERROR) << "WebRTC event log queue full. Dropping event."; 663 // arguments to have a copy-constructor.
572 } 664 auto event_handler = [this](std::unique_ptr<rtclog::Event> rtclog_event) {
573 helper_thread_.SignalNewEvent(); 665 LogEvent(std::move(rtclog_event));
666 };
667 auto task = rtc::WrapUnique<rtc::QueuedTask>(
668 new RtcEventLogTask(std::move(event), event_handler));
669 task_queue_.PostTask(std::move(task));
670 }
671
672 void RtcEventLogImpl::LogEvent(std::unique_ptr<rtclog::Event> event) {
terelius 2017/08/30 11:26:19 nit: Could the name be made clearer and less simil
eladalon 2017/08/30 11:43:33 I'll move this code directly into where the functi
673 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
674 if (file_->is_open()) {
675 LogToFile(std::move(event));
676 } else {
677 LogToMemory(std::move(event));
678 }
679 }
680
681 bool RtcEventLogImpl::AppendEventToString(rtclog::Event* event,
682 ProtoString* output_string) {
683 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
684
685 // Even though we're only serializing a single event during this call, what
686 // we intend to get is a list of events, with a tag and length preceding
687 // each actual event. To produce that, we serialize a list of a single event.
688 // If we later serialize additional events, the resulting ProtoString will
689 // be a proper concatenation of all those events.
690
691 rtclog::EventStream event_stream;
692 event_stream.add_stream();
693
694 // As a tweak, we swap the new event into the event-stream, write that to
695 // file, then swap back. This saves on some copying.
696 rtclog::Event* output_event = event_stream.mutable_stream(0);
697 output_event->Swap(event);
698
699 bool appended;
700 size_t potential_new_size =
701 written_bytes_ + output_string->size() + event_stream.ByteSize();
702 if (potential_new_size <= max_size_bytes_) {
703 event_stream.AppendToString(output_string);
704 appended = true;
705 } else {
706 appended = false;
707 }
708
709 // When the function returns, the original Event will be unchanged.
710 output_event->Swap(event);
711
712 return appended;
713 }
714
715 void RtcEventLogImpl::LogToMemory(std::unique_ptr<rtclog::Event> event) {
716 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
717 RTC_DCHECK(!file_->is_open());
718
719 if (IsConfigEvent(*event.get())) {
720 config_history_.push_back(std::move(event));
721 } else {
722 history_.push_back(std::move(event));
723 if (history_.size() > kEventsInHistory) {
724 history_.pop_front();
725 }
726 }
727 }
728
729 void RtcEventLogImpl::StartLogFile() {
730 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
731 RTC_DCHECK(file_->is_open());
732
733 ProtoString output_string;
734
735 // Create and serialize the LOG_START event.
736 // The timestamp used will correspond to when logging has started. The log
737 // may contain events earlier than the LOG_START event. (In general, the
738 // timestamps in the log are not monotonic.)
739 rtclog::Event start_event;
740 start_event.set_timestamp_us(rtc::TimeMicros());
741 start_event.set_type(rtclog::Event::LOG_START);
742 bool appended = AppendEventToString(&start_event, &output_string);
743
744 // Serialize the config information for all old streams, including streams
745 // which were already logged to previous files.
746 for (auto& event : config_history_) {
747 if (!appended) {
748 break;
749 }
750 appended = AppendEventToString(event.get(), &output_string);
751 }
752
753 // Serialize the events in the event queue.
754 while (appended && !history_.empty()) {
755 appended = AppendEventToString(history_.front().get(), &output_string);
756 if (appended) {
757 // Known issue - if writing to the file fails, these events will have
758 // been lost. If we try to open a new file, these events will be missing
759 // from it.
760 history_.pop_front();
761 }
762 }
763
764 // Write to file.
765 if (!file_->Write(output_string.data(), output_string.size())) {
766 LOG(LS_ERROR) << "FileWrapper failed to write WebRtcEventLog file.";
767 // The current FileWrapper implementation closes the file on error.
768 RTC_DCHECK(!file_->is_open());
769 return;
770 }
771 written_bytes_ += output_string.size();
772
773 if (!appended) {
774 RTC_DCHECK(file_->is_open());
775 StopLogFile(rtc::TimeMicros());
776 }
777 }
778
779 void RtcEventLogImpl::LogToFile(std::unique_ptr<rtclog::Event> event) {
780 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
781 RTC_DCHECK(file_->is_open());
782
783 ProtoString output_string;
784
785 bool appended = AppendEventToString(event.get(), &output_string);
786
787 if (IsConfigEvent(*event.get())) {
788 config_history_.push_back(std::move(event));
789 }
790
791 if (!appended) {
792 RTC_DCHECK(file_->is_open());
793 StopLogFile(rtc::TimeMicros());
794 return;
795 }
796
797 // Write string to file.
798 if (file_->Write(output_string.data(), output_string.size())) {
799 written_bytes_ += output_string.size();
800 } else {
801 LOG(LS_ERROR) << "FileWrapper failed to write WebRtcEventLog file.";
802 // The current FileWrapper implementation closes the file on error.
803 RTC_DCHECK(!file_->is_open());
804 }
805 }
806
807 void RtcEventLogImpl::StopLogFile(int64_t stop_time) {
808 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
809 RTC_DCHECK(file_->is_open());
810
811 ProtoString output_string;
812
813 rtclog::Event end_event;
814 end_event.set_timestamp_us(stop_time);
815 end_event.set_type(rtclog::Event::LOG_END);
816 bool appended = AppendEventToString(&end_event, &output_string);
817
818 if (appended) {
819 if (!file_->Write(output_string.data(), output_string.size())) {
820 LOG(LS_ERROR) << "FileWrapper failed to write WebRtcEventLog file.";
821 // The current FileWrapper implementation closes the file on error.
822 RTC_DCHECK(!file_->is_open());
823 }
824 written_bytes_ += output_string.size();
825 }
826
827 max_size_bytes_ = std::numeric_limits<int64_t>::max();
828 written_bytes_ = 0;
829
830 file_->CloseFile();
831 RTC_DCHECK(!file_->is_open());
574 } 832 }
575 833
576 bool RtcEventLog::ParseRtcEventLog(const std::string& file_name, 834 bool RtcEventLog::ParseRtcEventLog(const std::string& file_name,
577 rtclog::EventStream* result) { 835 rtclog::EventStream* result) {
578 char tmp_buffer[1024]; 836 char tmp_buffer[1024];
579 int bytes_read = 0; 837 int bytes_read = 0;
580 std::unique_ptr<FileWrapper> dump_file(FileWrapper::Create()); 838 std::unique_ptr<FileWrapper> dump_file(FileWrapper::Create());
581 if (!dump_file->OpenFile(file_name.c_str(), true)) { 839 if (!dump_file->OpenFile(file_name.c_str(), true)) {
582 return false; 840 return false;
583 } 841 }
584 ProtoString dump_buffer; 842 ProtoString dump_buffer;
585 while ((bytes_read = dump_file->Read(tmp_buffer, sizeof(tmp_buffer))) > 0) { 843 while ((bytes_read = dump_file->Read(tmp_buffer, sizeof(tmp_buffer))) > 0) {
586 dump_buffer.append(tmp_buffer, bytes_read); 844 dump_buffer.append(tmp_buffer, bytes_read);
587 } 845 }
588 dump_file->CloseFile(); 846 dump_file->CloseFile();
589 return result->ParseFromString(dump_buffer); 847 return result->ParseFromString(dump_buffer);
590 } 848 }
591 849
592 #endif // ENABLE_RTC_EVENT_LOG 850 #endif // ENABLE_RTC_EVENT_LOG
593 851
594 // RtcEventLog member functions. 852 // RtcEventLog member functions.
595 std::unique_ptr<RtcEventLog> RtcEventLog::Create() { 853 std::unique_ptr<RtcEventLog> RtcEventLog::Create() {
596 #ifdef ENABLE_RTC_EVENT_LOG 854 #ifdef ENABLE_RTC_EVENT_LOG
855 // TODO(eladalon): Known issue - there's a race over |log_count_| here.
597 constexpr int kMaxLogCount = 5; 856 constexpr int kMaxLogCount = 5;
598 int count = rtc::AtomicOps::Increment(&RtcEventLogImpl::log_count_); 857 int count = 1 + std::atomic_fetch_add(&RtcEventLogImpl::log_count_, 1);
599 if (count > kMaxLogCount) { 858 if (count > kMaxLogCount) {
600 LOG(LS_WARNING) << "Denied creation of additional WebRTC event logs. " 859 LOG(LS_WARNING) << "Denied creation of additional WebRTC event logs. "
601 << count - 1 << " logs open already."; 860 << count - 1 << " logs open already.";
602 rtc::AtomicOps::Decrement(&RtcEventLogImpl::log_count_); 861 std::atomic_fetch_sub(&RtcEventLogImpl::log_count_, 1);
603 return std::unique_ptr<RtcEventLog>(new RtcEventLogNullImpl()); 862 return std::unique_ptr<RtcEventLog>(new RtcEventLogNullImpl());
604 } 863 }
605 return std::unique_ptr<RtcEventLog>(new RtcEventLogImpl()); 864 return std::unique_ptr<RtcEventLog>(new RtcEventLogImpl());
606 #else 865 #else
607 return std::unique_ptr<RtcEventLog>(new RtcEventLogNullImpl()); 866 return CreateNull();
608 #endif // ENABLE_RTC_EVENT_LOG 867 #endif // ENABLE_RTC_EVENT_LOG
609 } 868 }
610 869
611 std::unique_ptr<RtcEventLog> RtcEventLog::CreateNull() { 870 std::unique_ptr<RtcEventLog> RtcEventLog::CreateNull() {
612 return std::unique_ptr<RtcEventLog>(new RtcEventLogNullImpl()); 871 return std::unique_ptr<RtcEventLog>(new RtcEventLogNullImpl());
613 } 872 }
614 873
615 } // namespace webrtc 874 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/logging/rtc_event_log/rtc_event_log.h ('k') | webrtc/logging/rtc_event_log/rtc_event_log_helper_thread.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698