Order the trace writer while commiting in startup registry
If the trace buffer size is smaller than the size in startup registry
then we end up with events with a few random threads. To avoid this,
bind the trace writer in order of creation, and keep all writers
waiting if any bind call fails. This would make it more likely to get
chunks from writers in order.
Ideally we should order chunks in order of commit, but it is lot of
information to keep track.
Change-Id: I9bb492aa286d663df7f7de32688aec412023ef6f
diff --git a/include/perfetto/ext/tracing/core/startup_trace_writer_registry.h b/include/perfetto/ext/tracing/core/startup_trace_writer_registry.h
index 1f0dd4a..febb55c 100644
--- a/include/perfetto/ext/tracing/core/startup_trace_writer_registry.h
+++ b/include/perfetto/ext/tracing/core/startup_trace_writer_registry.h
@@ -20,7 +20,6 @@
#include <functional>
#include <memory>
#include <mutex>
-#include <set>
#include <vector>
#include "perfetto/base/export.h"
@@ -122,7 +121,7 @@
// Unbound writers that we handed out to writer threads. These writers may be
// concurrently written to by the writer threads.
- std::set<StartupTraceWriter*> unbound_writers_;
+ std::vector<StartupTraceWriter*> unbound_writers_;
// Unbound writers that writer threads returned to the registry by calling
// ReturnUnboundTraceWriter(). Writers are removed from |unbound_writers_|
diff --git a/src/tracing/core/startup_trace_writer_registry.cc b/src/tracing/core/startup_trace_writer_registry.cc
index cb02231..79a6813 100644
--- a/src/tracing/core/startup_trace_writer_registry.cc
+++ b/src/tracing/core/startup_trace_writer_registry.cc
@@ -57,7 +57,7 @@
std::lock_guard<std::mutex> lock(lock_);
PERFETTO_DCHECK(!arbiter_); // Should only be called while unbound.
std::unique_ptr<StartupTraceWriter> writer(new StartupTraceWriter(handle_));
- unbound_writers_.insert(writer.get());
+ unbound_writers_.push_back(writer.get());
return writer;
}
@@ -65,10 +65,11 @@
std::unique_ptr<StartupTraceWriter> trace_writer) {
std::lock_guard<std::mutex> lock(lock_);
PERFETTO_DCHECK(!trace_writer->write_in_progress_);
+ auto it = std::find(unbound_writers_.begin(), unbound_writers_.end(),
+ trace_writer.get());
// If the registry is already bound, but the writer wasn't, bind it now.
if (arbiter_) {
- auto it = unbound_writers_.find(trace_writer.get());
if (it == unbound_writers_.end()) {
// Nothing to do, the writer was already bound.
return;
@@ -85,8 +86,8 @@
}
// If the registry was not bound yet, keep the writer alive until it is.
- PERFETTO_DCHECK(unbound_writers_.count(trace_writer.get()));
- unbound_writers_.erase(trace_writer.get());
+ PERFETTO_DCHECK(it != unbound_writers_.end());
+ unbound_writers_.erase(it);
unbound_owned_writers_.push_back(std::move(trace_writer));
}
@@ -151,7 +152,7 @@
if ((*it)->BindToArbiter(arbiter_, target_buffer_, chunks_per_batch_)) {
it = unbound_writers_.erase(it);
} else {
- it++;
+ break;
}
}
if (!unbound_writers_.empty()) {