Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/logging/test_logDecorations.cpp
41144 views
1
/*
2
* Copyright (c) 2016, 2020, Oracle and/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 "logging/logDecorations.hpp"
26
#include "logging/logTagSet.hpp"
27
#include "runtime/os.hpp"
28
#include "unittest.hpp"
29
#include "utilities/globalDefinitions.hpp"
30
31
static const LogTagSet& tagset = LogTagSetMapping<LOG_TAGS(logging, safepoint)>::tagset();
32
static const LogDecorators default_decorators;
33
34
TEST_VM(LogDecorations, level) {
35
char buf[LogDecorations::max_decoration_size + 1];
36
for (uint l = LogLevel::First; l <= LogLevel::Last; l++) {
37
LogLevelType level = static_cast<LogLevelType>(l);
38
// Create a decorations object for the current level
39
LogDecorations decorations(level, tagset, default_decorators);
40
// Verify that the level decoration matches the specified level
41
EXPECT_STREQ(LogLevel::name(level), decorations.decoration(LogDecorators::level_decorator, buf, sizeof(buf)));
42
43
// Test changing level after object creation time
44
LogLevelType other_level;
45
if (l != LogLevel::Last) {
46
other_level = static_cast<LogLevelType>(l + 1);
47
} else {
48
other_level = static_cast<LogLevelType>(LogLevel::First);
49
}
50
decorations.set_level(other_level);
51
EXPECT_STREQ(LogLevel::name(other_level), decorations.decoration(LogDecorators::level_decorator, buf, sizeof(buf)))
52
<< "Decoration reports incorrect value after changing the level";
53
}
54
}
55
56
TEST_VM(LogDecorations, uptime) {
57
char buf[LogDecorations::max_decoration_size + 1];
58
// Verify the format of the decoration
59
int a, b;
60
char decimal_point;
61
LogDecorations decorations(LogLevel::Info, tagset, default_decorators);
62
const char* uptime = decorations.decoration(LogDecorators::uptime_decorator, buf, sizeof(buf));
63
int read = sscanf(uptime, "%d%c%ds", &a, &decimal_point, &b);
64
EXPECT_EQ(3, read) << "Invalid uptime decoration: " << uptime;
65
EXPECT_TRUE(decimal_point == '.' || decimal_point == ',') << "Invalid uptime decoration: " << uptime;
66
67
// Verify that uptime increases
68
double prev = 0;
69
for (int i = 0; i < 3; i++) {
70
os::naked_short_sleep(10);
71
LogDecorations d(LogLevel::Info, tagset, default_decorators);
72
double cur = strtod(d.decoration(LogDecorators::uptime_decorator, buf, sizeof(buf)), NULL);
73
ASSERT_LT(prev, cur);
74
prev = cur;
75
}
76
}
77
78
TEST_VM(LogDecorations, tags) {
79
char buf[LogDecorations::max_decoration_size + 1];
80
char expected_tags[1 * K];
81
tagset.label(expected_tags, sizeof(expected_tags));
82
// Verify that the expected tags are included in the tags decoration
83
LogDecorations decorations(LogLevel::Info, tagset, default_decorators);
84
EXPECT_STREQ(expected_tags, decorations.decoration(LogDecorators::tags_decorator, buf, sizeof(buf)));
85
}
86
87
// Test each variation of the different timestamp decorations (ms, ns, uptime ms, uptime ns)
88
TEST_VM(LogDecorations, timestamps) {
89
char buf[LogDecorations::max_decoration_size + 1];
90
struct {
91
const LogDecorators::Decorator decorator;
92
const char* suffix;
93
const char* desc;
94
} test_decorator[] = {
95
{ LogDecorators::timemillis_decorator, "ms", "timemillis" },
96
{ LogDecorators::uptimemillis_decorator, "ms", "uptimemillis" },
97
{ LogDecorators::timenanos_decorator, "ns", "timenanos" },
98
{ LogDecorators::uptimenanos_decorator, "ns", "uptimenanos" }
99
};
100
101
for (uint i = 0; i < ARRAY_SIZE(test_decorator); i++) {
102
tty->print_cr("Processing Decorator %s", test_decorator[i].desc);
103
LogDecorators::Decorator decorator = test_decorator[i].decorator;
104
LogDecorators decorator_selection;
105
ASSERT_TRUE(decorator_selection.parse(LogDecorators::name(decorator)));
106
107
// Create decorations with the decorator we want to test included
108
LogDecorations decorations(LogLevel::Info, tagset, decorator_selection);
109
const char* decoration = decorations.decoration(decorator, buf, sizeof(buf));
110
111
// Verify format of timestamp
112
const char* suffix;
113
for (suffix = decoration; isdigit(*suffix); suffix++) {
114
// Skip over digits
115
}
116
EXPECT_STREQ(test_decorator[i].suffix, suffix);
117
118
// Verify timestamp values
119
julong prev = 0;
120
for (int i = 0; i < 3; i++) {
121
// The sleep needs to be longer than the timer resolution to ensure
122
// we see updates to 'timemillis'. Windows has the lowest resolution
123
// at 15-16ms, so we use 20.
124
os::naked_short_sleep(20);
125
LogDecorations d(LogLevel::Info, tagset, decorator_selection);
126
julong val = strtoull(d.decoration(decorator, buf, sizeof(buf)), NULL, 10);
127
tty->print_cr("Read value: " UINT64_FORMAT, val);
128
ASSERT_LT(prev, val);
129
prev = val;
130
}
131
}
132
}
133
134
// Test the time decoration
135
TEST(LogDecorations, iso8601_time) {
136
char buf[LogDecorations::max_decoration_size + 1];
137
LogDecorators decorator_selection;
138
ASSERT_TRUE(decorator_selection.parse("time"));
139
LogDecorations decorations(LogLevel::Info, tagset, decorator_selection);
140
141
const char *timestr = decorations.decoration(LogDecorators::time_decorator, buf, sizeof(buf));
142
time_t expected_ts = time(NULL);
143
144
// Verify format
145
int y, M, d, h, m, s, ms;
146
int read = sscanf(timestr, "%d-%d-%dT%d:%d:%d.%d", &y, &M, &d, &h, &m, &s, &ms);
147
ASSERT_EQ(7, read) << "Invalid format: " << timestr;
148
149
// Verify reported time & date
150
struct tm reported_time = {0};
151
reported_time.tm_year = y - 1900;
152
reported_time.tm_mon = M - 1;
153
reported_time.tm_mday = d;
154
reported_time.tm_hour = h;
155
reported_time.tm_min = m;
156
reported_time.tm_sec = s;
157
reported_time.tm_isdst = -1; // let mktime deduce DST settings
158
time_t reported_ts = mktime(&reported_time);
159
expected_ts = mktime(localtime(&expected_ts));
160
time_t diff = reported_ts - expected_ts;
161
if (diff < 0) {
162
diff = -diff;
163
}
164
// Allow up to 10 seconds in difference
165
ASSERT_LE(diff, 10) << "Reported time: " << reported_ts << " (" << timestr << ")"
166
<< ", expected time: " << expected_ts;
167
}
168
169
// Test the utctime decoration
170
TEST(LogDecorations, iso8601_utctime) {
171
char buf[LogDecorations::max_decoration_size + 1];
172
LogDecorators decorator_selection;
173
ASSERT_TRUE(decorator_selection.parse("utctime"));
174
LogDecorations decorations(LogLevel::Info, tagset, decorator_selection);
175
176
const char *timestr = decorations.decoration(LogDecorators::utctime_decorator, buf, sizeof(buf));
177
time_t expected_ts = time(NULL);
178
179
// Verify format
180
char trailing_character;
181
int y, M, d, h, m, s, ms, offset;
182
183
int read = sscanf(timestr, "%d-%d-%dT%d:%d:%d.%d%c%d", &y, &M, &d, &h, &m, &s, &ms, &trailing_character, &offset);
184
185
ASSERT_EQ(9, read) << "Invalid format: " << timestr;
186
187
// Ensure time is UTC (no offset)
188
ASSERT_EQ('+', trailing_character) << "Invalid trailing character for UTC: "
189
<< trailing_character;
190
ASSERT_EQ(0, offset) << "Invalid offset: " << timestr;
191
192
struct tm reported_time = {0};
193
reported_time.tm_year = y - 1900;
194
reported_time.tm_mon = M - 1;
195
reported_time.tm_mday = d;
196
reported_time.tm_hour = h;
197
reported_time.tm_min = m;
198
reported_time.tm_sec = s;
199
reported_time.tm_isdst = 0; // No DST for UTC timestamps
200
time_t reported_ts = mktime(&reported_time);
201
expected_ts = mktime(gmtime(&expected_ts));
202
time_t diff = reported_ts - expected_ts;
203
if (diff < 0) {
204
diff = -diff;
205
}
206
// Allow up to 10 seconds in difference
207
ASSERT_LE(diff, 10) << "Reported time: " << reported_ts << " (" << timestr << ")"
208
<< ", expected time: " << expected_ts;
209
}
210
211
// Test the pid and tid decorations
212
TEST(LogDecorations, identifiers) {
213
char buf[LogDecorations::max_decoration_size + 1];
214
LogDecorators decorator_selection;
215
ASSERT_TRUE(decorator_selection.parse("pid,tid"));
216
LogDecorations decorations(LogLevel::Info, tagset, decorator_selection);
217
218
struct {
219
intx expected;
220
LogDecorators::Decorator decorator;
221
} ids[] = {
222
{ os::current_process_id(), LogDecorators::pid_decorator },
223
{ os::current_thread_id(), LogDecorators::tid_decorator },
224
};
225
226
for (uint i = 0; i < ARRAY_SIZE(ids); i++) {
227
const char* reported = decorations.decoration(ids[i].decorator, buf, sizeof(buf));
228
229
// Verify format
230
const char* str;
231
for (str = reported; isdigit(*str); str++) {
232
// Skip over digits
233
}
234
EXPECT_EQ('\0', *str) << "Should only contain digits";
235
236
// Verify value
237
EXPECT_EQ(ids[i].expected, strtol(reported, NULL, 10));
238
}
239
}
240
241