Commit a0687c71 authored by Clemens Backes's avatar Clemens Backes Committed by Commit Bot

[utils] Synchronize across StdoutStream instances

We constantly fight against scrambled output with --print-wasm-code and
other flags. Passing --single-threaded only partially mitigates this,
because there could still be multiple isolates (e.g. Workers), and we
sometimes failed to really execute in a single thread if that flag was
set.
Hence this CL solves the problem in a more fundamental way: Whenever a
{StdoutStream} is constructed, it implicitly takes a global recursive
mutex. The recursive mutex is needed because we still have some printing
methods that don't take a stream as parameter, and instead create their
own instance of {StdoutStream}, which should not crash of course.

The overhead of taking a mutex should be acceptable, since output to
stdout mostly happens if special tracing flags have been passed, and is
slow anyway.

This CL ensures that the {StdoutStream} is used at least for
--print-code, --print-wasm-code, and --trace-turbo-graph.
More flags can later be ported on demand.

The {JSHeapBroker} class was modified to not contain a {StdoutStream},
but instead create one on demand.

R=mlippautz@chromium.org, tebbi@chromium.org
CC=ahaas@chromium.org

Bug: v8:10506
Change-Id: Ib9cf8d76aa79553b4215bb7775e6d47a8179aafa
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2201767Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#67855}
parent f87539b4
...@@ -2407,9 +2407,11 @@ JSHeapBroker::JSHeapBroker(Isolate* isolate, Zone* broker_zone, ...@@ -2407,9 +2407,11 @@ JSHeapBroker::JSHeapBroker(Isolate* isolate, Zone* broker_zone,
TRACE(this, "Constructing heap broker"); TRACE(this, "Constructing heap broker");
} }
std::ostream& JSHeapBroker::Trace() const { std::string JSHeapBroker::Trace() const {
return trace_out_ << "[" << this << "] " std::ostringstream oss;
<< std::string(trace_indentation_ * 2, ' '); oss << "[" << this << "] ";
for (unsigned i = 0; i < trace_indentation_ * 2; ++i) oss.put(' ');
return oss.str();
} }
void JSHeapBroker::StopSerializing() { void JSHeapBroker::StopSerializing() {
......
...@@ -33,20 +33,20 @@ std::ostream& operator<<(std::ostream& os, const ObjectRef& ref); ...@@ -33,20 +33,20 @@ std::ostream& operator<<(std::ostream& os, const ObjectRef& ref);
#define TRACE_BROKER(broker, x) \ #define TRACE_BROKER(broker, x) \
do { \ do { \
if (broker->tracing_enabled() && FLAG_trace_heap_broker_verbose) \ if (broker->tracing_enabled() && FLAG_trace_heap_broker_verbose) \
broker->Trace() << x << '\n'; \ StdoutStream{} << broker->Trace() << x << '\n'; \
} while (false) } while (false)
#define TRACE_BROKER_MEMORY(broker, x) \ #define TRACE_BROKER_MEMORY(broker, x) \
do { \ do { \
if (broker->tracing_enabled() && FLAG_trace_heap_broker_memory) \ if (broker->tracing_enabled() && FLAG_trace_heap_broker_memory) \
broker->Trace() << x << std::endl; \ StdoutStream{} << broker->Trace() << x << std::endl; \
} while (false) } while (false)
#define TRACE_BROKER_MISSING(broker, x) \ #define TRACE_BROKER_MISSING(broker, x) \
do { \ do { \
if (broker->tracing_enabled()) \ if (broker->tracing_enabled()) \
broker->Trace() << "Missing " << x << " (" << __FILE__ << ":" \ StdoutStream{} << broker->Trace() << "Missing " << x << " (" << __FILE__ \
<< __LINE__ << ")" << std::endl; \ << ":" << __LINE__ << ")" << std::endl; \
} while (false) } while (false)
struct PropertyAccessTarget { struct PropertyAccessTarget {
...@@ -193,7 +193,7 @@ class V8_EXPORT_PRIVATE JSHeapBroker { ...@@ -193,7 +193,7 @@ class V8_EXPORT_PRIVATE JSHeapBroker {
bool IsSerializedForCompilation(const SharedFunctionInfoRef& shared, bool IsSerializedForCompilation(const SharedFunctionInfoRef& shared,
const FeedbackVectorRef& feedback) const; const FeedbackVectorRef& feedback) const;
std::ostream& Trace() const; std::string Trace() const;
void IncrementTracingIndentation(); void IncrementTracingIndentation();
void DecrementTracingIndentation(); void DecrementTracingIndentation();
...@@ -242,7 +242,6 @@ class V8_EXPORT_PRIVATE JSHeapBroker { ...@@ -242,7 +242,6 @@ class V8_EXPORT_PRIVATE JSHeapBroker {
BrokerMode mode_ = kDisabled; BrokerMode mode_ = kDisabled;
bool const tracing_enabled_; bool const tracing_enabled_;
bool const is_concurrent_inlining_; bool const is_concurrent_inlining_;
mutable StdoutStream trace_out_;
unsigned trace_indentation_ = 0; unsigned trace_indentation_ = 0;
PerIsolateCompilerCache* compiler_cache_ = nullptr; PerIsolateCompilerCache* compiler_cache_ = nullptr;
ZoneUnorderedMap<FeedbackSource, ProcessedFeedback const*, ZoneUnorderedMap<FeedbackSource, ProcessedFeedback const*,
......
This diff is collapsed.
...@@ -5,9 +5,11 @@ ...@@ -5,9 +5,11 @@
#ifndef V8_DIAGNOSTICS_CODE_TRACER_H_ #ifndef V8_DIAGNOSTICS_CODE_TRACER_H_
#define V8_DIAGNOSTICS_CODE_TRACER_H_ #define V8_DIAGNOSTICS_CODE_TRACER_H_
#include "src/base/optional.h"
#include "src/common/globals.h" #include "src/common/globals.h"
#include "src/flags/flags.h" #include "src/flags/flags.h"
#include "src/utils/allocation.h" #include "src/utils/allocation.h"
#include "src/utils/ostreams.h"
#include "src/utils/utils.h" #include "src/utils/utils.h"
#include "src/utils/vector.h" #include "src/utils/vector.h"
...@@ -45,6 +47,28 @@ class CodeTracer final : public Malloced { ...@@ -45,6 +47,28 @@ class CodeTracer final : public Malloced {
CodeTracer* tracer_; CodeTracer* tracer_;
}; };
class StreamScope : public Scope {
public:
explicit StreamScope(CodeTracer* tracer) : Scope(tracer) {
FILE* file = this->file();
if (file == stdout) {
stdout_stream_.emplace();
} else {
file_stream_.emplace(file);
}
}
std::ostream& stream() {
if (stdout_stream_.has_value()) return stdout_stream_.value();
return file_stream_.value();
}
private:
// Exactly one of these two will be initialized.
base::Optional<StdoutStream> stdout_stream_;
base::Optional<OFStream> file_stream_;
};
void OpenFile() { void OpenFile() {
if (!ShouldRedirect()) { if (!ShouldRedirect()) {
return; return;
......
...@@ -783,7 +783,6 @@ DEFINE_BOOL(wasm_fuzzer_gen_test, false, ...@@ -783,7 +783,6 @@ DEFINE_BOOL(wasm_fuzzer_gen_test, false,
"generate a test case when running a wasm fuzzer") "generate a test case when running a wasm fuzzer")
DEFINE_IMPLICATION(wasm_fuzzer_gen_test, single_threaded) DEFINE_IMPLICATION(wasm_fuzzer_gen_test, single_threaded)
DEFINE_BOOL(print_wasm_code, false, "Print WebAssembly code") DEFINE_BOOL(print_wasm_code, false, "Print WebAssembly code")
DEFINE_IMPLICATION(print_wasm_code, single_threaded)
DEFINE_BOOL(print_wasm_stub_code, false, "Print WebAssembly stub code") DEFINE_BOOL(print_wasm_stub_code, false, "Print WebAssembly stub code")
DEFINE_BOOL(asm_wasm_lazy_compilation, false, DEFINE_BOOL(asm_wasm_lazy_compilation, false,
"enable lazy compilation for asm-wasm modules") "enable lazy compilation for asm-wasm modules")
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <cinttypes> #include <cinttypes>
#include "src/base/lazy-instance.h"
#include "src/objects/objects.h" #include "src/objects/objects.h"
#include "src/objects/string.h" #include "src/objects/string.h"
...@@ -114,6 +115,9 @@ std::streamsize AndroidLogStream::xsputn(const char* s, std::streamsize n) { ...@@ -114,6 +115,9 @@ std::streamsize AndroidLogStream::xsputn(const char* s, std::streamsize n) {
} }
#endif #endif
DEFINE_LAZY_LEAKY_OBJECT_GETTER(base::RecursiveMutex,
StdoutStream::GetStdoutMutex)
namespace { namespace {
// Locale-independent predicates. // Locale-independent predicates.
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "include/v8config.h" #include "include/v8config.h"
#include "src/base/macros.h" #include "src/base/macros.h"
#include "src/base/platform/mutex.h"
#include "src/common/globals.h" #include "src/common/globals.h"
namespace v8 { namespace v8 {
...@@ -80,12 +81,20 @@ class StdoutStream : public std::ostream { ...@@ -80,12 +81,20 @@ class StdoutStream : public std::ostream {
StdoutStream() : std::ostream(&stream_) {} StdoutStream() : std::ostream(&stream_) {}
private: private:
static V8_EXPORT_PRIVATE base::RecursiveMutex* GetStdoutMutex();
AndroidLogStream stream_; AndroidLogStream stream_;
base::RecursiveMutexGuard mutex_guard_{GetStdoutMutex()};
}; };
#else #else
class StdoutStream : public OFStream { class StdoutStream : public OFStream {
public: public:
StdoutStream() : OFStream(stdout) {} StdoutStream() : OFStream(stdout) {}
private:
static V8_EXPORT_PRIVATE base::RecursiveMutex* GetStdoutMutex();
base::RecursiveMutexGuard mutex_guard_{GetStdoutMutex()};
}; };
#endif #endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment