8333775: Small improvement to outputStream auto-indentation mode

Reviewed-by: jsjolen, mbaesken
This commit is contained in:
Thomas Stuefe 2024-06-10 12:33:32 +00:00
parent 7b43a8cd7c
commit e22fc121ae
5 changed files with 28 additions and 15 deletions
src/hotspot/share
test/hotspot/gtest/utilities

@ -51,13 +51,7 @@ static ssize_t counter_diff(size_t c1, size_t c2) {
} }
MemReporterBase::MemReporterBase(outputStream* out, size_t scale) : MemReporterBase::MemReporterBase(outputStream* out, size_t scale) :
_scale(scale), _output(out) { _scale(scale), _output(out), _auto_indentor(out) {}
_output->set_autoindent(true);
}
MemReporterBase::~MemReporterBase() {
_output->set_autoindent(false);
}
size_t MemReporterBase::reserved_total(const MallocMemory* malloc, const VirtualMemory* vm) { size_t MemReporterBase::reserved_total(const MallocMemory* malloc, const VirtualMemory* vm) {
return malloc->malloc_size() + malloc->arena_size() + vm->reserved(); return malloc->malloc_size() + malloc->arena_size() + vm->reserved();

@ -38,6 +38,7 @@ class MemReporterBase : public StackObj {
private: private:
const size_t _scale; // report in this scale const size_t _scale; // report in this scale
outputStream* const _output; // destination outputStream* const _output; // destination
StreamAutoIndentor _auto_indentor;
public: public:
@ -45,7 +46,6 @@ class MemReporterBase : public StackObj {
static const size_t default_scale = K; static const size_t default_scale = K;
MemReporterBase(outputStream* out, size_t scale = default_scale); MemReporterBase(outputStream* out, size_t scale = default_scale);
~MemReporterBase();
// Helper functions // Helper functions
// Calculate total reserved and committed amount // Calculate total reserved and committed amount

@ -277,6 +277,12 @@ outputStream& outputStream::indent() {
return *this; return *this;
} }
bool outputStream::set_autoindent(bool value) {
const bool old = _autoindent;
_autoindent = value;
return old;
}
void outputStream::print_jlong(jlong value) { void outputStream::print_jlong(jlong value) {
print(JLONG_FORMAT, value); print(JLONG_FORMAT, value);
} }

@ -109,7 +109,8 @@ class outputStream : public CHeapObjBase {
// line starts depending on the current indentation level: // line starts depending on the current indentation level:
// print(), print_cr(), print_raw(), print_raw_cr() // print(), print_cr(), print_raw(), print_raw_cr()
// Other APIs are unaffected // Other APIs are unaffected
void set_autoindent(bool value) { _autoindent = value; } // Returns old autoindent state.
bool set_autoindent(bool value);
// sizing // sizing
int position() const { return _position; } int position() const { return _position; }
@ -175,17 +176,26 @@ class outputStream : public CHeapObjBase {
extern outputStream* tty; // tty output extern outputStream* tty; // tty output
class streamIndentor : public StackObj { class streamIndentor : public StackObj {
private: outputStream* const _str;
outputStream* _str; const int _amount;
int _amount; NONCOPYABLE(streamIndentor);
public:
public:
streamIndentor(outputStream* str, int amt = 2) : _str(str), _amount(amt) { streamIndentor(outputStream* str, int amt = 2) : _str(str), _amount(amt) {
_str->inc(_amount); _str->inc(_amount);
} }
~streamIndentor() { _str->dec(_amount); } ~streamIndentor() { _str->dec(_amount); }
}; };
class StreamAutoIndentor : public StackObj {
outputStream* const _os;
const bool _old;
NONCOPYABLE(StreamAutoIndentor);
public:
StreamAutoIndentor(outputStream* os) :
_os(os), _old(os->set_autoindent(true)) {}
~StreamAutoIndentor() { _os->set_autoindent(_old); }
};
// advisory locking for the shared tty stream: // advisory locking for the shared tty stream:
class ttyLocker: StackObj { class ttyLocker: StackObj {
friend class ttyUnlocker; friend class ttyUnlocker;

@ -106,7 +106,8 @@ TEST_VM(ostream, bufferedStream_dynamic_small) {
static void test_autoindent(bool on) { static void test_autoindent(bool on) {
stringStream ss; stringStream ss;
ss.set_autoindent(on); const bool prior = ss.set_autoindent(on);
EXPECT_FALSE(prior);
{ {
streamIndentor si(&ss, 5); streamIndentor si(&ss, 5);
ss.print("ABC"); ss.print("ABC");
@ -146,6 +147,8 @@ static void test_autoindent(bool on) {
"end" "end"
); );
} }
bool prior2 = ss.set_autoindent(prior);
EXPECT_EQ(prior2, on);
} }
TEST_VM(ostream, autoindent_on) { test_autoindent(true); } TEST_VM(ostream, autoindent_on) { test_autoindent(true); }