Skip to content

Commit bf7f1c4

Browse files
committedJun 7, 2024
8333211: NMT Reports: replace manual indentation handling with auto indent
Reviewed-by: jsjolen, asmehra
1 parent 8ffc35d commit bf7f1c4

File tree

9 files changed

+215
-136
lines changed

9 files changed

+215
-136
lines changed
 

‎src/hotspot/share/memory/arena.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class Chunk {
8787
class Arena : public CHeapObjBase {
8888
public:
8989

90-
enum class Tag {
90+
enum class Tag : uint8_t {
9191
tag_other = 0,
9292
tag_ra, // resource area
9393
tag_ha, // handle area
@@ -101,6 +101,7 @@ class Arena : public CHeapObjBase {
101101

102102
MEMFLAGS _flags; // Memory tracking flags
103103
const Tag _tag;
104+
uint32_t _init_size;
104105
Chunk* _first; // First chunk
105106
Chunk* _chunk; // current chunk
106107
char* _hwm; // High water mark

‎src/hotspot/share/nmt/memReporter.cpp

+133-104
Large diffs are not rendered by default.

‎src/hotspot/share/nmt/memReporter.hpp

+3-7
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ class MemReporterBase : public StackObj {
4444
// Default scale to use if no scale given.
4545
static const size_t default_scale = K;
4646

47-
MemReporterBase(outputStream* out, size_t scale = default_scale) :
48-
_scale(scale), _output(out)
49-
{}
47+
MemReporterBase(outputStream* out, size_t scale = default_scale);
48+
~MemReporterBase();
5049

5150
// Helper functions
5251
// Calculate total reserved and committed amount
@@ -109,10 +108,7 @@ class MemReporterBase : public StackObj {
109108
void print_total(size_t reserved, size_t committed, size_t peak = 0) const;
110109
void print_malloc(const MemoryCounter* c, MEMFLAGS flag = mtNone) const;
111110
void print_virtual_memory(size_t reserved, size_t committed, size_t peak) const;
112-
113-
void print_malloc_line(const MemoryCounter* c) const;
114-
void print_virtual_memory_line(size_t reserved, size_t committed, size_t peak) const;
115-
void print_arena_line(const MemoryCounter* c) const;
111+
void print_arena(const MemoryCounter* c) const;
116112

117113
void print_virtual_memory_region(const char* type, address base, size_t size) const;
118114
};

‎src/hotspot/share/nmt/memoryFileTracker.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ void MemoryFileTracker::print_report_on(const MemoryFile* file, outputStream* st
9292
NMTUtil::amount_in_scale(end_addr - start_addr, scale),
9393
NMTUtil::scale_name(scale),
9494
NMTUtil::flag_to_name(prev->val().out.flag()));
95-
_stack_storage.get(prev->val().out.stack()).print_on(stream, 4);
95+
{
96+
streamIndentor si(stream, 4);
97+
_stack_storage.get(prev->val().out.stack()).print_on(stream);
98+
}
9699
stream->cr();
97100
}
98101
prev = current;

‎src/hotspot/share/utilities/nativeCallStack.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,18 @@ int NativeCallStack::frames() const {
7171
return index;
7272
}
7373

74-
void NativeCallStack::print_on(outputStream* out) const {
75-
print_on(out, 0);
76-
}
77-
7874
// Decode and print this call path
79-
void NativeCallStack::print_on(outputStream* out, int indent) const {
75+
void NativeCallStack::print_on(outputStream* out) const {
8076
DEBUG_ONLY(assert_not_fake();)
8177
address pc;
8278
char buf[1024];
8379
int offset;
8480
if (is_empty()) {
85-
out->fill_to(indent);
8681
out->print("[BOOTSTRAP]");
8782
} else {
8883
for (int frame = 0; frame < NMT_TrackingStackDepth; frame ++) {
8984
pc = get_frame(frame);
9085
if (pc == nullptr) break;
91-
out->fill_to(indent);
9286
out->print("[" PTR_FORMAT "]", p2i(pc));
9387
// Print function and library; shorten library name to just its last component
9488
// for brevity, and omit it completely for libjvm.so

‎src/hotspot/share/utilities/nativeCallStack.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ class NativeCallStack : public StackObj {
124124
}
125125

126126
void print_on(outputStream* out) const;
127-
void print_on(outputStream* out, int indent) const;
128127
};
129128

130129
#define FAKE_CALLSTACK NativeCallStack(NativeCallStack::FakeMarker::its_fake)

‎src/hotspot/share/utilities/ostream.cpp

+11-8
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,11 @@
4444
extern "C" void jio_print(const char* s, size_t len);
4545
extern "C" int jio_printf(const char *fmt, ...);
4646

47-
outputStream::outputStream() {
48-
_position = 0;
49-
_precount = 0;
50-
_indentation = 0;
51-
_scratch = nullptr;
52-
_scratch_len = 0;
53-
}
54-
5547
outputStream::outputStream(bool has_time_stamps) {
5648
_position = 0;
5749
_precount = 0;
5850
_indentation = 0;
51+
_autoindent = false;
5952
_scratch = nullptr;
6053
_scratch_len = 0;
6154
if (has_time_stamps) _stamp.update();
@@ -159,6 +152,9 @@ void outputStream::do_vsnprintf_and_write_with_scratch_buffer(const char* format
159152
}
160153

161154
void outputStream::do_vsnprintf_and_write(const char* format, va_list ap, bool add_cr) {
155+
if (_autoindent && _position == 0) {
156+
indent();
157+
}
162158
if (_scratch) {
163159
do_vsnprintf_and_write_with_scratch_buffer(format, ap, add_cr);
164160
} else {
@@ -188,6 +184,13 @@ void outputStream::vprint_cr(const char* format, va_list argptr) {
188184
do_vsnprintf_and_write(format, argptr, true);
189185
}
190186

187+
void outputStream::print_raw(const char* str, size_t len) {
188+
if (_autoindent && _position == 0) {
189+
indent();
190+
}
191+
write(str, len);
192+
}
193+
191194
void outputStream::fill_to(int col) {
192195
int need_fill = col - position();
193196
sp(need_fill);

‎src/hotspot/share/utilities/ostream.hpp

+14-7
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ DEBUG_ONLY(class ResourceMark;)
4646
class outputStream : public CHeapObjBase {
4747
private:
4848
NONCOPYABLE(outputStream);
49+
int _indentation; // current indentation
50+
bool _autoindent; // if true, every line starts with indentation
4951

5052
protected:
51-
int _indentation; // current indentation
5253
int _position; // visual position on the current line
5354
uint64_t _precount; // number of chars output, less than _position
5455
TimeStamp _stamp; // for time stamps
@@ -90,8 +91,7 @@ class outputStream : public CHeapObjBase {
9091
class TestSupport; // Unit test support
9192

9293
// creation
93-
outputStream();
94-
outputStream(bool has_time_stamps);
94+
outputStream(bool has_time_stamps = false);
9595

9696
// indentation
9797
outputStream& indent();
@@ -104,6 +104,13 @@ class outputStream : public CHeapObjBase {
104104
void fill_to(int col);
105105
void move_to(int col, int slop = 6, int min_space = 2);
106106

107+
// Automatic indentation:
108+
// If autoindent mode is on, the following APIs will automatically indent
109+
// line starts depending on the current indentation level:
110+
// print(), print_cr(), print_raw(), print_raw_cr()
111+
// Other APIs are unaffected
112+
void set_autoindent(bool value) { _autoindent = value; }
113+
107114
// sizing
108115
int position() const { return _position; }
109116
julong count() const { return _precount + _position; }
@@ -119,10 +126,10 @@ class outputStream : public CHeapObjBase {
119126
void print_cr(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
120127
void vprint(const char *format, va_list argptr) ATTRIBUTE_PRINTF(2, 0);
121128
void vprint_cr(const char* format, va_list argptr) ATTRIBUTE_PRINTF(2, 0);
122-
void print_raw(const char* str) { write(str, strlen(str)); }
123-
void print_raw(const char* str, size_t len) { write(str, len); }
124-
void print_raw_cr(const char* str) { write(str, strlen(str)); cr(); }
125-
void print_raw_cr(const char* str, size_t len){ write(str, len); cr(); }
129+
void print_raw(const char* str) { print_raw(str, strlen(str)); }
130+
void print_raw(const char* str, size_t len);
131+
void print_raw_cr(const char* str) { print_raw(str); cr(); }
132+
void print_raw_cr(const char* str, size_t len) { print_raw(str, len); cr(); }
126133
void print_data(void* data, size_t len, bool with_ascii, bool rel_addr=true);
127134
void put(char ch);
128135
void sp(int count = 1);

‎test/hotspot/gtest/utilities/test_ostream.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,53 @@ TEST_VM(ostream, bufferedStream_dynamic_small) {
104104
}
105105
}
106106

107+
static void test_autoindent(bool on) {
108+
stringStream ss;
109+
ss.set_autoindent(on);
110+
{
111+
streamIndentor si(&ss, 5);
112+
ss.print("ABC");
113+
ss.print("DEF");
114+
ss.cr();
115+
ss.print_cr("0123");
116+
{
117+
streamIndentor si(&ss, 5);
118+
ss.print_cr("4567");
119+
ss.print_raw("89AB");
120+
ss.print_raw("CDEXXXX", 3);
121+
ss.print_raw_cr("XYZ");
122+
}
123+
ss.print("%u", 100);
124+
ss.print_raw("KB");
125+
ss.cr();
126+
}
127+
ss.print("end");
128+
129+
if (on) {
130+
EXPECT_STREQ(ss.base(),
131+
" ABCDEF\n"
132+
" 0123\n"
133+
" 4567\n"
134+
" 89ABCDEXYZ\n"
135+
" 100KB\n"
136+
"end"
137+
);
138+
} else {
139+
// no autoindent: calls should work as always without indentation
140+
EXPECT_STREQ(ss.base(),
141+
"ABCDEF\n"
142+
"0123\n"
143+
"4567\n"
144+
"89ABCDEXYZ\n"
145+
"100KB\n"
146+
"end"
147+
);
148+
}
149+
}
150+
151+
TEST_VM(ostream, autoindent_on) { test_autoindent(true); }
152+
TEST_VM(ostream, autoindent_off) { test_autoindent(false); }
153+
107154
/* Activate to manually test bufferedStream dynamic cap.
108155
109156
TEST_VM(ostream, bufferedStream_dynamic_large) {

0 commit comments

Comments
 (0)
Please sign in to comment.