OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 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/modules/remote_bitrate_estimator/test/bwe_test_logging.h" | 11 #include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h" |
12 | 12 |
13 #if BWE_TEST_LOGGING_COMPILE_TIME_ENABLE | 13 #if BWE_TEST_LOGGING_COMPILE_TIME_ENABLE |
14 | 14 |
15 #include <stdarg.h> | 15 #include <stdarg.h> |
16 #include <stdio.h> | 16 #include <stdio.h> |
17 | 17 |
18 #include <algorithm> | 18 #include <algorithm> |
19 #include <sstream> | 19 #include <sstream> |
20 | 20 |
21 #include "webrtc/base/checks.h" | 21 #include "webrtc/base/checks.h" |
22 #include "webrtc/base/format_macros.h" | 22 #include "webrtc/base/format_macros.h" |
23 #include "webrtc/base/platform_thread.h" | 23 #include "webrtc/base/platform_thread.h" |
24 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | |
25 | 24 |
26 namespace webrtc { | 25 namespace webrtc { |
27 namespace testing { | 26 namespace testing { |
28 namespace bwe { | 27 namespace bwe { |
29 | 28 |
30 Logging Logging::g_Logging; | 29 Logging Logging::g_Logging; |
31 | 30 |
32 static std::string ToString(uint32_t v) { | 31 static std::string ToString(uint32_t v) { |
33 std::stringstream ss; | 32 std::stringstream ss; |
34 ss << v; | 33 ss << v; |
(...skipping 16 matching lines...) Expand all Loading... |
51 | 50 |
52 Logging::Context::~Context() { | 51 Logging::Context::~Context() { |
53 Logging::GetInstance()->PopState(); | 52 Logging::GetInstance()->PopState(); |
54 } | 53 } |
55 | 54 |
56 Logging* Logging::GetInstance() { | 55 Logging* Logging::GetInstance() { |
57 return &g_Logging; | 56 return &g_Logging; |
58 } | 57 } |
59 | 58 |
60 void Logging::SetGlobalContext(uint32_t name) { | 59 void Logging::SetGlobalContext(uint32_t name) { |
61 CriticalSectionScoped cs(crit_sect_.get()); | 60 rtc::CritScope cs(&crit_sect_); |
62 thread_map_[rtc::CurrentThreadId()].global_state.tag = ToString(name); | 61 thread_map_[rtc::CurrentThreadId()].global_state.tag = ToString(name); |
63 } | 62 } |
64 | 63 |
65 void Logging::SetGlobalContext(const std::string& name) { | 64 void Logging::SetGlobalContext(const std::string& name) { |
66 CriticalSectionScoped cs(crit_sect_.get()); | 65 rtc::CritScope cs(&crit_sect_); |
67 thread_map_[rtc::CurrentThreadId()].global_state.tag = name; | 66 thread_map_[rtc::CurrentThreadId()].global_state.tag = name; |
68 } | 67 } |
69 | 68 |
70 void Logging::SetGlobalContext(const char* name) { | 69 void Logging::SetGlobalContext(const char* name) { |
71 CriticalSectionScoped cs(crit_sect_.get()); | 70 rtc::CritScope cs(&crit_sect_); |
72 thread_map_[rtc::CurrentThreadId()].global_state.tag = name; | 71 thread_map_[rtc::CurrentThreadId()].global_state.tag = name; |
73 } | 72 } |
74 | 73 |
75 void Logging::SetGlobalEnable(bool enabled) { | 74 void Logging::SetGlobalEnable(bool enabled) { |
76 CriticalSectionScoped cs(crit_sect_.get()); | 75 rtc::CritScope cs(&crit_sect_); |
77 thread_map_[rtc::CurrentThreadId()].global_state.enabled = enabled; | 76 thread_map_[rtc::CurrentThreadId()].global_state.enabled = enabled; |
78 } | 77 } |
79 | 78 |
80 void Logging::Log(const char format[], ...) { | 79 void Logging::Log(const char format[], ...) { |
81 CriticalSectionScoped cs(crit_sect_.get()); | 80 rtc::CritScope cs(&crit_sect_); |
82 ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId()); | 81 ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId()); |
83 RTC_DCHECK(it != thread_map_.end()); | 82 RTC_DCHECK(it != thread_map_.end()); |
84 const State& state = it->second.stack.top(); | 83 const State& state = it->second.stack.top(); |
85 if (state.enabled) { | 84 if (state.enabled) { |
86 printf("%s\t", state.tag.c_str()); | 85 printf("%s\t", state.tag.c_str()); |
87 va_list args; | 86 va_list args; |
88 va_start(args, format); | 87 va_start(args, format); |
89 vprintf(format, args); | 88 vprintf(format, args); |
90 va_end(args); | 89 va_end(args); |
91 printf("\n"); | 90 printf("\n"); |
(...skipping 16 matching lines...) Expand all Loading... |
108 double value, | 107 double value, |
109 const std::string& alg_name) { | 108 const std::string& alg_name) { |
110 Plot(figure, name, value, 0, alg_name); | 109 Plot(figure, name, value, 0, alg_name); |
111 } | 110 } |
112 | 111 |
113 void Logging::Plot(int figure, | 112 void Logging::Plot(int figure, |
114 const std::string& name, | 113 const std::string& name, |
115 double value, | 114 double value, |
116 uint32_t ssrc, | 115 uint32_t ssrc, |
117 const std::string& alg_name) { | 116 const std::string& alg_name) { |
118 CriticalSectionScoped cs(crit_sect_.get()); | 117 rtc::CritScope cs(&crit_sect_); |
119 ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId()); | 118 ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId()); |
120 RTC_DCHECK(it != thread_map_.end()); | 119 RTC_DCHECK(it != thread_map_.end()); |
121 const State& state = it->second.stack.top(); | 120 const State& state = it->second.stack.top(); |
122 if (state.enabled) { | 121 if (state.enabled) { |
123 printf("PLOT\t%d\t%s:%" PRIu32 "@%s\t%f\t%f\n", figure, name.c_str(), ssrc, | 122 printf("PLOT\t%d\t%s:%" PRIu32 "@%s\t%f\t%f\n", figure, name.c_str(), ssrc, |
124 alg_name.c_str(), state.timestamp_ms * 0.001, value); | 123 alg_name.c_str(), state.timestamp_ms * 0.001, value); |
125 } | 124 } |
126 } | 125 } |
127 | 126 |
128 void Logging::PlotBar(int figure, | 127 void Logging::PlotBar(int figure, |
129 const std::string& name, | 128 const std::string& name, |
130 double value, | 129 double value, |
131 int flow_id) { | 130 int flow_id) { |
132 CriticalSectionScoped cs(crit_sect_.get()); | 131 rtc::CritScope cs(&crit_sect_); |
133 ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId()); | 132 ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId()); |
134 RTC_DCHECK(it != thread_map_.end()); | 133 RTC_DCHECK(it != thread_map_.end()); |
135 const State& state = it->second.stack.top(); | 134 const State& state = it->second.stack.top(); |
136 if (state.enabled) { | 135 if (state.enabled) { |
137 printf("BAR\t%d\t%s_%d\t%f\n", figure, name.c_str(), flow_id, value); | 136 printf("BAR\t%d\t%s_%d\t%f\n", figure, name.c_str(), flow_id, value); |
138 } | 137 } |
139 } | 138 } |
140 | 139 |
141 void Logging::PlotBaselineBar(int figure, | 140 void Logging::PlotBaselineBar(int figure, |
142 const std::string& name, | 141 const std::string& name, |
143 double value, | 142 double value, |
144 int flow_id) { | 143 int flow_id) { |
145 CriticalSectionScoped cs(crit_sect_.get()); | 144 rtc::CritScope cs(&crit_sect_); |
146 ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId()); | 145 ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId()); |
147 RTC_DCHECK(it != thread_map_.end()); | 146 RTC_DCHECK(it != thread_map_.end()); |
148 const State& state = it->second.stack.top(); | 147 const State& state = it->second.stack.top(); |
149 if (state.enabled) { | 148 if (state.enabled) { |
150 printf("BASELINE\t%d\t%s_%d\t%f\n", figure, name.c_str(), flow_id, value); | 149 printf("BASELINE\t%d\t%s_%d\t%f\n", figure, name.c_str(), flow_id, value); |
151 } | 150 } |
152 } | 151 } |
153 | 152 |
154 void Logging::PlotErrorBar(int figure, | 153 void Logging::PlotErrorBar(int figure, |
155 const std::string& name, | 154 const std::string& name, |
156 double value, | 155 double value, |
157 double ylow, | 156 double ylow, |
158 double yhigh, | 157 double yhigh, |
159 const std::string& error_title, | 158 const std::string& error_title, |
160 int flow_id) { | 159 int flow_id) { |
161 CriticalSectionScoped cs(crit_sect_.get()); | 160 rtc::CritScope cs(&crit_sect_); |
162 ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId()); | 161 ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId()); |
163 RTC_DCHECK(it != thread_map_.end()); | 162 RTC_DCHECK(it != thread_map_.end()); |
164 const State& state = it->second.stack.top(); | 163 const State& state = it->second.stack.top(); |
165 if (state.enabled) { | 164 if (state.enabled) { |
166 printf("ERRORBAR\t%d\t%s_%d\t%f\t%f\t%f\t%s\n", figure, name.c_str(), | 165 printf("ERRORBAR\t%d\t%s_%d\t%f\t%f\t%f\t%s\n", figure, name.c_str(), |
167 flow_id, value, ylow, yhigh, error_title.c_str()); | 166 flow_id, value, ylow, yhigh, error_title.c_str()); |
168 } | 167 } |
169 } | 168 } |
170 | 169 |
171 void Logging::PlotLimitErrorBar(int figure, | 170 void Logging::PlotLimitErrorBar(int figure, |
172 const std::string& name, | 171 const std::string& name, |
173 double value, | 172 double value, |
174 double ylow, | 173 double ylow, |
175 double yhigh, | 174 double yhigh, |
176 const std::string& error_title, | 175 const std::string& error_title, |
177 double ymax, | 176 double ymax, |
178 const std::string& limit_title, | 177 const std::string& limit_title, |
179 int flow_id) { | 178 int flow_id) { |
180 CriticalSectionScoped cs(crit_sect_.get()); | 179 rtc::CritScope cs(&crit_sect_); |
181 ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId()); | 180 ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId()); |
182 RTC_DCHECK(it != thread_map_.end()); | 181 RTC_DCHECK(it != thread_map_.end()); |
183 const State& state = it->second.stack.top(); | 182 const State& state = it->second.stack.top(); |
184 if (state.enabled) { | 183 if (state.enabled) { |
185 printf("LIMITERRORBAR\t%d\t%s_%d\t%f\t%f\t%f\t%s\t%f\t%s\n", figure, | 184 printf("LIMITERRORBAR\t%d\t%s_%d\t%f\t%f\t%f\t%s\t%f\t%s\n", figure, |
186 name.c_str(), flow_id, value, ylow, yhigh, error_title.c_str(), ymax, | 185 name.c_str(), flow_id, value, ylow, yhigh, error_title.c_str(), ymax, |
187 limit_title.c_str()); | 186 limit_title.c_str()); |
188 } | 187 } |
189 } | 188 } |
190 | 189 |
191 void Logging::PlotLabel(int figure, | 190 void Logging::PlotLabel(int figure, |
192 const std::string& title, | 191 const std::string& title, |
193 const std::string& y_label, | 192 const std::string& y_label, |
194 int num_flows) { | 193 int num_flows) { |
195 CriticalSectionScoped cs(crit_sect_.get()); | 194 rtc::CritScope cs(&crit_sect_); |
196 ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId()); | 195 ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId()); |
197 RTC_DCHECK(it != thread_map_.end()); | 196 RTC_DCHECK(it != thread_map_.end()); |
198 const State& state = it->second.stack.top(); | 197 const State& state = it->second.stack.top(); |
199 if (state.enabled) { | 198 if (state.enabled) { |
200 printf("LABEL\t%d\t%s\t%s\t%d\n", figure, title.c_str(), y_label.c_str(), | 199 printf("LABEL\t%d\t%s\t%s\t%d\n", figure, title.c_str(), y_label.c_str(), |
201 num_flows); | 200 num_flows); |
202 } | 201 } |
203 } | 202 } |
204 | 203 |
205 Logging::Logging() | 204 Logging::Logging() |
206 : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), | 205 : thread_map_() { |
207 thread_map_() { | |
208 } | 206 } |
209 | 207 |
210 Logging::State::State() : tag(""), timestamp_ms(0), enabled(true) {} | 208 Logging::State::State() : tag(""), timestamp_ms(0), enabled(true) {} |
211 | 209 |
212 Logging::State::State(const std::string& tag, int64_t timestamp_ms, | 210 Logging::State::State(const std::string& tag, int64_t timestamp_ms, |
213 bool enabled) | 211 bool enabled) |
214 : tag(tag), | 212 : tag(tag), |
215 timestamp_ms(timestamp_ms), | 213 timestamp_ms(timestamp_ms), |
216 enabled(enabled) { | 214 enabled(enabled) { |
217 } | 215 } |
218 | 216 |
219 void Logging::State::MergePrevious(const State& previous) { | 217 void Logging::State::MergePrevious(const State& previous) { |
220 if (tag.empty()) { | 218 if (tag.empty()) { |
221 tag = previous.tag; | 219 tag = previous.tag; |
222 } else if (!previous.tag.empty()) { | 220 } else if (!previous.tag.empty()) { |
223 tag = previous.tag + "_" + tag; | 221 tag = previous.tag + "_" + tag; |
224 } | 222 } |
225 timestamp_ms = std::max(previous.timestamp_ms, timestamp_ms); | 223 timestamp_ms = std::max(previous.timestamp_ms, timestamp_ms); |
226 enabled = previous.enabled && enabled; | 224 enabled = previous.enabled && enabled; |
227 } | 225 } |
228 | 226 |
229 void Logging::PushState(const std::string& append_to_tag, int64_t timestamp_ms, | 227 void Logging::PushState(const std::string& append_to_tag, int64_t timestamp_ms, |
230 bool enabled) { | 228 bool enabled) { |
231 CriticalSectionScoped cs(crit_sect_.get()); | 229 rtc::CritScope cs(&crit_sect_); |
232 State new_state(append_to_tag, timestamp_ms, enabled); | 230 State new_state(append_to_tag, timestamp_ms, enabled); |
233 ThreadState* thread_state = &thread_map_[rtc::CurrentThreadId()]; | 231 ThreadState* thread_state = &thread_map_[rtc::CurrentThreadId()]; |
234 std::stack<State>* stack = &thread_state->stack; | 232 std::stack<State>* stack = &thread_state->stack; |
235 if (stack->empty()) { | 233 if (stack->empty()) { |
236 new_state.MergePrevious(thread_state->global_state); | 234 new_state.MergePrevious(thread_state->global_state); |
237 } else { | 235 } else { |
238 new_state.MergePrevious(stack->top()); | 236 new_state.MergePrevious(stack->top()); |
239 } | 237 } |
240 stack->push(new_state); | 238 stack->push(new_state); |
241 } | 239 } |
242 | 240 |
243 void Logging::PopState() { | 241 void Logging::PopState() { |
244 CriticalSectionScoped cs(crit_sect_.get()); | 242 rtc::CritScope cs(&crit_sect_); |
245 ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId()); | 243 ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId()); |
246 RTC_DCHECK(it != thread_map_.end()); | 244 RTC_DCHECK(it != thread_map_.end()); |
247 std::stack<State>* stack = &it->second.stack; | 245 std::stack<State>* stack = &it->second.stack; |
248 int64_t newest_timestamp_ms = stack->top().timestamp_ms; | 246 int64_t newest_timestamp_ms = stack->top().timestamp_ms; |
249 stack->pop(); | 247 stack->pop(); |
250 if (!stack->empty()) { | 248 if (!stack->empty()) { |
251 State* state = &stack->top(); | 249 State* state = &stack->top(); |
252 // Update time so that next log/plot will use the latest time seen so far | 250 // Update time so that next log/plot will use the latest time seen so far |
253 // in this call tree. | 251 // in this call tree. |
254 state->timestamp_ms = std::max(state->timestamp_ms, newest_timestamp_ms); | 252 state->timestamp_ms = std::max(state->timestamp_ms, newest_timestamp_ms); |
255 } | 253 } |
256 } | 254 } |
257 } // namespace bwe | 255 } // namespace bwe |
258 } // namespace testing | 256 } // namespace testing |
259 } // namespace webrtc | 257 } // namespace webrtc |
260 | 258 |
261 #endif // BWE_TEST_LOGGING_COMPILE_TIME_ENABLE | 259 #endif // BWE_TEST_LOGGING_COMPILE_TIME_ENABLE |
OLD | NEW |