Skip to content

Commit d066f2b

Browse files
committed
8260030: Improve stringStream buffer handling
Reviewed-by: iklam, kbarrett
1 parent 1452280 commit d066f2b

File tree

3 files changed

+96
-67
lines changed

3 files changed

+96
-67
lines changed

src/hotspot/share/utilities/ostream.cpp

Lines changed: 65 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -310,46 +310,72 @@ void outputStream::print_data(void* data, size_t len, bool with_ascii) {
310310
}
311311
}
312312

313-
stringStream::stringStream(size_t initial_size) : outputStream() {
314-
buffer_length = initial_size;
315-
buffer = NEW_C_HEAP_ARRAY(char, buffer_length, mtInternal);
316-
buffer_pos = 0;
317-
buffer_fixed = false;
313+
stringStream::stringStream(size_t initial_capacity) :
314+
outputStream(),
315+
_buffer(_small_buffer),
316+
_written(0),
317+
_capacity(sizeof(_small_buffer)),
318+
_is_fixed(false)
319+
{
320+
if (initial_capacity > _capacity) {
321+
grow(initial_capacity);
322+
}
318323
zero_terminate();
319324
}
320325

321326
// useful for output to fixed chunks of memory, such as performance counters
322-
stringStream::stringStream(char* fixed_buffer, size_t fixed_buffer_size) : outputStream() {
323-
buffer_length = fixed_buffer_size;
324-
buffer = fixed_buffer;
325-
buffer_pos = 0;
326-
buffer_fixed = true;
327+
stringStream::stringStream(char* fixed_buffer, size_t fixed_buffer_size) :
328+
outputStream(),
329+
_buffer(fixed_buffer),
330+
_written(0),
331+
_capacity(fixed_buffer_size),
332+
_is_fixed(true)
333+
{
327334
zero_terminate();
328335
}
329336

337+
// Grow backing buffer to desired capacity. Don't call for fixed buffers
338+
void stringStream::grow(size_t new_capacity) {
339+
assert(!_is_fixed, "Don't call for caller provided buffers");
340+
assert(new_capacity > _capacity, "Sanity");
341+
assert(new_capacity > sizeof(_small_buffer), "Sanity");
342+
if (_buffer == _small_buffer) {
343+
_buffer = NEW_C_HEAP_ARRAY(char, new_capacity, mtInternal);
344+
_capacity = new_capacity;
345+
if (_written > 0) {
346+
::memcpy(_buffer, _small_buffer, _written);
347+
}
348+
zero_terminate();
349+
} else {
350+
_buffer = REALLOC_C_HEAP_ARRAY(char, _buffer, new_capacity, mtInternal);
351+
_capacity = new_capacity;
352+
}
353+
}
354+
330355
void stringStream::write(const char* s, size_t len) {
331-
size_t write_len = len; // number of non-null bytes to write
332-
size_t end = buffer_pos + len + 1; // position after write and final '\0'
333-
if (end > buffer_length) {
334-
if (buffer_fixed) {
335-
// if buffer cannot resize, silently truncate
336-
end = buffer_length;
337-
write_len = end - buffer_pos - 1; // leave room for the final '\0'
338-
} else {
339-
// For small overruns, double the buffer. For larger ones,
340-
// increase to the requested size.
341-
if (end < buffer_length * 2) {
342-
end = buffer_length * 2;
343-
}
344-
buffer = REALLOC_C_HEAP_ARRAY(char, buffer, end, mtInternal);
345-
buffer_length = end;
356+
assert(_capacity >= _written + 1, "Sanity");
357+
if (len == 0) {
358+
return;
359+
}
360+
const size_t reasonable_max_len = 1 * G;
361+
if (len >= reasonable_max_len) {
362+
assert(false, "bad length? (" SIZE_FORMAT ")", len);
363+
return;
364+
}
365+
size_t write_len = 0;
366+
if (_is_fixed) {
367+
write_len = MIN2(len, _capacity - _written - 1);
368+
} else {
369+
write_len = len;
370+
size_t needed = _written + len + 1;
371+
if (needed > _capacity) {
372+
grow(MAX2(needed, _capacity * 2));
346373
}
347374
}
348-
// invariant: buffer is always null-terminated
349-
guarantee(buffer_pos + write_len + 1 <= buffer_length, "stringStream oob");
375+
assert(_written + write_len + 1 <= _capacity, "stringStream oob");
350376
if (write_len > 0) {
351-
memcpy(buffer + buffer_pos, s, write_len);
352-
buffer_pos += write_len;
377+
::memcpy(_buffer + _written, s, write_len);
378+
_written += write_len;
353379
zero_terminate();
354380
}
355381

@@ -360,21 +386,22 @@ void stringStream::write(const char* s, size_t len) {
360386
}
361387

362388
void stringStream::zero_terminate() {
363-
assert(buffer != NULL &&
364-
buffer_pos < buffer_length, "sanity");
365-
buffer[buffer_pos] = '\0';
389+
assert(_buffer != NULL &&
390+
_written < _capacity, "sanity");
391+
_buffer[_written] = '\0';
366392
}
367393

368394
void stringStream::reset() {
369-
buffer_pos = 0; _precount = 0; _position = 0;
395+
_written = 0; _precount = 0; _position = 0;
396+
_newlines = 0;
370397
zero_terminate();
371398
}
372399

373400
char* stringStream::as_string(bool c_heap) const {
374401
char* copy = c_heap ?
375-
NEW_C_HEAP_ARRAY(char, buffer_pos + 1, mtInternal) : NEW_RESOURCE_ARRAY(char, buffer_pos + 1);
376-
strncpy(copy, buffer, buffer_pos);
377-
copy[buffer_pos] = 0; // terminating null
402+
NEW_C_HEAP_ARRAY(char, _written + 1, mtInternal) : NEW_RESOURCE_ARRAY(char, _written + 1);
403+
::memcpy(copy, _buffer, _written);
404+
copy[_written] = 0; // terminating null
378405
if (c_heap) {
379406
// Need to ensure our content is written to memory before we return
380407
// the pointer to it.
@@ -384,8 +411,8 @@ char* stringStream::as_string(bool c_heap) const {
384411
}
385412

386413
stringStream::~stringStream() {
387-
if (buffer_fixed == false && buffer != NULL) {
388-
FREE_C_HEAP_ARRAY(char, buffer);
414+
if (!_is_fixed && _buffer != _small_buffer) {
415+
FREE_C_HEAP_ARRAY(char, _buffer);
389416
}
390417
}
391418

src/hotspot/share/utilities/ostream.hpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ class outputStream : public ResourceObj {
8787
// sizing
8888
int width() const { return _width; }
8989
int position() const { return _position; }
90-
int newlines() const { return _newlines; }
9190
julong count() const { return _precount + _position; }
9291
void set_count(julong count) { _precount = count - _position; }
9392
void set_position(int pos) { _position = pos; }
@@ -192,28 +191,31 @@ class ttyUnlocker: StackObj {
192191
// for writing to strings; buffer will expand automatically.
193192
// Buffer will always be zero-terminated.
194193
class stringStream : public outputStream {
195-
protected:
196-
char* buffer;
197-
size_t buffer_pos;
198-
size_t buffer_length;
199-
bool buffer_fixed;
194+
char* _buffer;
195+
size_t _written; // Number of characters written, excluding termin. zero
196+
size_t _capacity;
197+
const bool _is_fixed;
198+
char _small_buffer[48];
199+
200+
// Grow backing buffer to desired capacity.
201+
void grow(size_t new_capacity);
200202

201203
// zero terminate at buffer_pos.
202204
void zero_terminate();
203205

204206
public:
205207
// Create a stringStream using an internal buffer of initially initial_bufsize size;
206208
// will be enlarged on demand. There is no maximum cap.
207-
stringStream(size_t initial_bufsize = 256);
209+
stringStream(size_t initial_capacity = 0);
208210
// Creates a stringStream using a caller-provided buffer. Will truncate silently if
209211
// it overflows.
210212
stringStream(char* fixed_buffer, size_t fixed_buffer_size);
211213
~stringStream();
212214
virtual void write(const char* c, size_t len);
213215
// Return number of characters written into buffer, excluding terminating zero and
214216
// subject to truncation in static buffer mode.
215-
size_t size() const { return buffer_pos; }
216-
const char* base() const { return buffer; }
217+
size_t size() const { return _written; }
218+
const char* base() const { return _buffer; }
217219
void reset();
218220
// copy to a resource, or C-heap, array as requested
219221
char* as_string(bool c_heap = false) const;

test/hotspot/gtest/utilities/test_ostream.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,17 @@
3131

3232
#include "unittest.hpp"
3333

34-
static size_t print_lorem(outputStream* st, bool short_len) {
34+
static size_t print_lorem(outputStream* st) {
3535
// Create a ResourceMark just to make sure the stream does not use ResourceArea
3636
ResourceMark rm;
3737
static const char* const lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, "
3838
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lacinia at quis "
3939
"risus sed vulputate odio ut enim blandit. Amet risus nullam eget felis eget. Viverra "
4040
"orci sagittis eu volutpat odio facilisis mauris sit. Erat velit scelerisque in dictum non.";
4141
static const size_t len_lorem = strlen(lorem);
42-
size_t len;
43-
if (short_len) {
44-
len = os::random() % 10;
45-
} else {
46-
len = MAX2(1, (int)(os::random() % len_lorem));
47-
}
42+
// Randomly alternate between short and long writes at a ratio of 9:1.
43+
const bool short_write = (os::random() % 10) > 0;
44+
const size_t len = os::random() % (short_write ? 10 : len_lorem);
4845
st->write(lorem, len);
4946
return len;
5047
}
@@ -53,12 +50,11 @@ static void test_stringStream_is_zero_terminated(const stringStream* ss) {
5350
ASSERT_EQ(ss->base()[ss->size()], '\0');
5451
}
5552

56-
57-
static void do_test_stringStream(stringStream* ss, bool short_len, size_t expected_cap) {
53+
static void do_test_stringStream(stringStream* ss, size_t expected_cap) {
5854
test_stringStream_is_zero_terminated(ss);
5955
size_t written = 0;
6056
for (int i = 0; i < 1000; i ++) {
61-
written += print_lorem(ss, short_len);
57+
written += print_lorem(ss);
6258
if (expected_cap > 0 && written >= expected_cap) {
6359
ASSERT_EQ(ss->size(), expected_cap - 1);
6460
} else {
@@ -73,14 +69,18 @@ static void do_test_stringStream(stringStream* ss, bool short_len, size_t expect
7369
test_stringStream_is_zero_terminated(ss);
7470
}
7571

76-
TEST_VM(ostream, stringStream_dynamic_realloc_1) {
77-
stringStream ss(2); // dynamic buffer with very small starting size
78-
do_test_stringStream(&ss, false, 0);
72+
TEST_VM(ostream, stringStream_dynamic_start_with_internal_buffer) {
73+
stringStream ss;
74+
do_test_stringStream(&ss, 0);
75+
ss.reset();
76+
do_test_stringStream(&ss, 0);
7977
}
8078

81-
TEST_VM(ostream, stringStream_dynamic_realloc_2) {
82-
stringStream ss(2); // dynamic buffer with very small starting size
83-
do_test_stringStream(&ss, true, 0);
79+
TEST_VM(ostream, stringStream_dynamic_start_with_malloced_buffer) {
80+
stringStream ss(128);
81+
do_test_stringStream(&ss, 0);
82+
ss.reset();
83+
do_test_stringStream(&ss, 0);
8484
}
8585

8686
TEST_VM(ostream, stringStream_static) {
@@ -89,7 +89,7 @@ TEST_VM(ostream, stringStream_static) {
8989
*canary_at = 'X';
9090
size_t stream_buf_size = sizeof(buffer) - 1;
9191
stringStream ss(buffer, stream_buf_size);
92-
do_test_stringStream(&ss, false, stream_buf_size);
92+
do_test_stringStream(&ss, stream_buf_size);
9393
ASSERT_EQ(*canary_at, 'X'); // canary
9494
}
9595

@@ -101,7 +101,7 @@ TEST_VM(ostream, bufferedStream_static) {
101101
bufferedStream bs(buf, stream_buf_size);
102102
size_t written = 0;
103103
for (int i = 0; i < 100; i ++) {
104-
written += print_lorem(&bs, true);
104+
written += print_lorem(&bs);
105105
if (written < stream_buf_size) {
106106
ASSERT_EQ(bs.size(), written);
107107
} else {
@@ -116,7 +116,7 @@ TEST_VM(ostream, bufferedStream_dynamic_small) {
116116
size_t written = 0;
117117
// The max cap imposed is 100M, we should be safely below this in this test.
118118
for (int i = 0; i < 10; i ++) {
119-
written += print_lorem(&bs, true);
119+
written += print_lorem(&bs);
120120
ASSERT_EQ(bs.size(), written);
121121
}
122122
}
@@ -130,7 +130,7 @@ TEST_VM(ostream, bufferedStream_dynamic_large) {
130130
// Note that this will assert in debug builds which is the expected behavior.
131131
size_t expected_cap_at = 100 * M;
132132
for (int i = 0; i < 10000000; i ++) {
133-
written += print_lorem(&bs, false);
133+
written += print_lorem(&bs);
134134
if (written < expected_cap_at) {
135135
ASSERT_EQ(bs.size(), written);
136136
} else {

0 commit comments

Comments
 (0)