forked from GoogleCloudPlatform/cloud-debug-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_queue.cc
More file actions
126 lines (95 loc) · 3.06 KB
/
format_queue.cc
File metadata and controls
126 lines (95 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* 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
*
* http://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 "format_queue.h"
#include "statistician.h"
#include "stopwatch.h"
namespace devtools {
namespace cdbg {
FormatQueue::~FormatQueue() {
if (!queue_.empty()) {
LOG(WARNING) << "Pending breakpoint hit reports are abandoned";
}
}
bool FormatQueue::IsEmpty() const {
MutexLock lock(&mu_);
return queue_.empty();
}
void FormatQueue::RemoveAll() {
MutexLock lock(&mu_);
for (Item& item : queue_) {
if (item.collector != nullptr) {
item.collector->ReleaseRefs();
}
}
queue_.clear();
}
void FormatQueue::Enqueue(
std::unique_ptr<BreakpointModel> breakpoint,
std::unique_ptr<CaptureDataCollector> collector) {
{
Item item;
item.breakpoint = std::move(breakpoint);
item.collector = std::move(collector);
if (item.breakpoint == nullptr) {
DCHECK(item.breakpoint != nullptr);
return;
}
MutexLock lock(&mu_);
// Replace pending non-final updates and ignore repeated final updates.
for (Item& existing_item : queue_) {
if (existing_item.breakpoint->id == item.breakpoint->id) {
if (!existing_item.breakpoint->is_final_state) {
std::swap(existing_item, item);
}
return;
}
}
if (queue_.size() < kMaxFormatQueueSize) {
queue_.push_back(std::move(item));
}
}
on_item_enqueued_.Fire();
}
std::unique_ptr<BreakpointModel> FormatQueue::FormatAndPop() {
Stopwatch stopwatch;
MutexLock lock(&mu_);
if (queue_.empty()) {
return nullptr;
}
// TODO(vlif): refactor to avoid keeping mutex locked during "Format".
Item& front = queue_.front();
std::unique_ptr<BreakpointModel> breakpoint = std::move(front.breakpoint);
if (front.collector != nullptr) {
front.collector->Format(breakpoint.get());
front.collector->ReleaseRefs();
}
queue_.pop_front();
// Copy "expressions" to "evaluated_expressions". The size of "expressions"
// and "evaluated_expressions" is expected to be the same if breakpoint was
// evaluated. Otherwise "evaluated_expressions" will be empty. Use "std::min"
// just to be on the safe side.
const int expressions_count =
std::min(
breakpoint->expressions.size(),
breakpoint->evaluated_expressions.size());
for (int i = 0; i < expressions_count; ++i) {
breakpoint->evaluated_expressions[i]->name = breakpoint->expressions[i];
}
statFormattingTime->add(stopwatch.GetElapsedMicros());
return breakpoint;
}
} // namespace cdbg
} // namespace devtools