Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/logging/test_asynclog.cpp
41145 views
1
/*
2
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
#include "precompiled.hpp"
25
#include "jvm.h"
26
#include "logging/log.hpp"
27
#include "logging/logAsyncWriter.hpp"
28
#include "logging/logMessage.hpp"
29
#include "logTestFixture.hpp"
30
#include "logTestUtils.inline.hpp"
31
#include "unittest.hpp"
32
33
class AsyncLogTest : public LogTestFixture {
34
public:
35
AsyncLogTest() {
36
if(!LogConfiguration::is_async_mode()) {
37
fprintf(stderr, "Warning: asynclog is OFF.\n");
38
}
39
}
40
41
void test_asynclog_ls() {
42
LogStream ls(Log(logging)::info());
43
outputStream* os = &ls;
44
os->print_cr("LogStreamWithAsyncLogImpl");
45
os->print_cr("LogStreamWithAsyncLogImpl secondline");
46
47
//multi-lines
48
os->print("logStream msg1-");
49
os->print("msg2-");
50
os->print("msg3\n");
51
os->print_cr("logStream newline");
52
}
53
54
void test_asynclog_raw() {
55
Log(logging) logger;
56
#define LOG_LEVEL(level, name) logger.name("1" #level);
57
LOG_LEVEL_LIST
58
#undef LOG_LEVEL
59
60
LogTarget(Trace, logging) t;
61
LogTarget(Debug, logging) d;
62
EXPECT_FALSE(t.is_enabled());
63
EXPECT_TRUE(d.is_enabled());
64
65
d.print("AsyncLogTarget.print = %d", 1);
66
log_trace(logging)("log_trace-test");
67
log_debug(logging)("log_debug-test");
68
}
69
};
70
71
TEST_VM(AsyncLogBufferTest, fifo) {
72
LinkedListDeque<int, mtLogging> fifo;
73
LinkedListImpl<int, ResourceObj::C_HEAP, mtLogging> result;
74
75
fifo.push_back(1);
76
EXPECT_EQ((size_t)1, fifo.size());
77
EXPECT_EQ(1, *(fifo.back()));
78
79
fifo.pop_all(&result);
80
EXPECT_EQ((size_t)0, fifo.size());
81
EXPECT_EQ(NULL, fifo.back());
82
EXPECT_EQ((size_t)1, result.size());
83
EXPECT_EQ(1, *(result.head()->data()));
84
result.clear();
85
86
fifo.push_back(2);
87
fifo.push_back(1);
88
fifo.pop_all(&result);
89
EXPECT_EQ((size_t)2, result.size());
90
EXPECT_EQ(2, *(result.head()->data()));
91
EXPECT_EQ(1, *(result.head()->next()->data()));
92
result.clear();
93
const int N = 1000;
94
for (int i=0; i<N; ++i) {
95
fifo.push_back(i);
96
}
97
fifo.pop_all(&result);
98
99
EXPECT_EQ((size_t)N, result.size());
100
LinkedListIterator<int> it(result.head());
101
for (int i=0; i<N; ++i) {
102
int* e = it.next();
103
EXPECT_EQ(i, *e);
104
}
105
}
106
107
TEST_VM(AsyncLogBufferTest, deque) {
108
LinkedListDeque<int, mtLogging> deque;
109
const int N = 10;
110
111
EXPECT_EQ(NULL, deque.front());
112
EXPECT_EQ(NULL, deque.back());
113
for (int i = 0; i < N; ++i) {
114
deque.push_back(i);
115
}
116
117
EXPECT_EQ(0, *(deque.front()));
118
EXPECT_EQ(N-1, *(deque.back()));
119
EXPECT_EQ((size_t)N, deque.size());
120
121
deque.pop_front();
122
EXPECT_EQ((size_t)(N - 1), deque.size());
123
EXPECT_EQ(1, *(deque.front()));
124
EXPECT_EQ(N - 1, *(deque.back()));
125
126
deque.pop_front();
127
EXPECT_EQ((size_t)(N - 2), deque.size());
128
EXPECT_EQ(2, *(deque.front()));
129
EXPECT_EQ(N - 1, *(deque.back()));
130
131
132
for (int i=2; i < N-1; ++i) {
133
deque.pop_front();
134
}
135
EXPECT_EQ((size_t)1, deque.size());
136
EXPECT_EQ(N - 1, *(deque.back()));
137
EXPECT_EQ(deque.back(), deque.front());
138
139
deque.pop_front();
140
EXPECT_EQ((size_t)0, deque.size());
141
}
142
143
TEST_VM_F(AsyncLogTest, asynclog) {
144
set_log_config(TestLogFileName, "logging=debug");
145
146
test_asynclog_ls();
147
test_asynclog_raw();
148
AsyncLogWriter::flush();
149
150
EXPECT_TRUE(file_contains_substring(TestLogFileName, "LogStreamWithAsyncLogImpl"));
151
EXPECT_TRUE(file_contains_substring(TestLogFileName, "logStream msg1-msg2-msg3"));
152
EXPECT_TRUE(file_contains_substring(TestLogFileName, "logStream newline"));
153
154
EXPECT_TRUE(file_contains_substring(TestLogFileName, "1Debug"));
155
EXPECT_TRUE(file_contains_substring(TestLogFileName, "1Info"));
156
EXPECT_TRUE(file_contains_substring(TestLogFileName, "1Warning"));
157
EXPECT_TRUE(file_contains_substring(TestLogFileName, "1Error"));
158
EXPECT_FALSE(file_contains_substring(TestLogFileName, "1Trace")); // trace message is masked out
159
160
EXPECT_TRUE(file_contains_substring(TestLogFileName, "AsyncLogTarget.print = 1"));
161
EXPECT_FALSE(file_contains_substring(TestLogFileName, "log_trace-test")); // trace message is masked out
162
EXPECT_TRUE(file_contains_substring(TestLogFileName, "log_debug-test"));
163
}
164
165
TEST_VM_F(AsyncLogTest, logMessage) {
166
set_log_config(TestLogFileName, "logging=debug");
167
168
const int MULTI_LINES = 20;
169
{
170
171
LogMessage(logging) msg;
172
Log(logging) logger;
173
174
for (int i = 0; i < MULTI_LINES; ++i) {
175
msg.debug("nonbreakable log message line-%02d", i);
176
177
if (0 == (i % 4)) {
178
logger.debug("a noisy message from other logger");
179
}
180
}
181
logger.debug("a noisy message from other logger");
182
}
183
AsyncLogWriter::flush();
184
185
ResourceMark rm;
186
LogMessageBuffer buffer;
187
const char* strs[MULTI_LINES + 1];
188
strs[MULTI_LINES] = NULL;
189
for (int i = 0; i < MULTI_LINES; ++i) {
190
stringStream ss;
191
ss.print_cr("nonbreakable log message line-%02d", i);
192
strs[i] = ss.as_string();
193
}
194
// check nonbreakable log messages are consecutive
195
EXPECT_TRUE(file_contains_substrings_in_order(TestLogFileName, strs));
196
EXPECT_TRUE(file_contains_substring(TestLogFileName, "a noisy message from other logger"));
197
}
198
199
TEST_VM_F(AsyncLogTest, droppingMessage) {
200
set_log_config(TestLogFileName, "logging=debug");
201
const size_t sz = 100;
202
203
if (AsyncLogWriter::instance() != nullptr) {
204
// shrink async buffer.
205
AutoModifyRestore<size_t> saver(AsyncLogBufferSize, sz * 1024 /*in byte*/);
206
LogMessage(logging) lm;
207
208
// write 100x more messages than its capacity in burst
209
for (size_t i = 0; i < sz * 100; ++i) {
210
lm.debug("a lot of log...");
211
}
212
lm.flush();
213
AsyncLogWriter::flush();
214
EXPECT_TRUE(file_contains_substring(TestLogFileName, "messages dropped due to async logging"));
215
}
216
}
217
218