Path: blob/master/test/hotspot/gtest/logging/test_asynclog.cpp
41145 views
/*1* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/23#include "precompiled.hpp"24#include "jvm.h"25#include "logging/log.hpp"26#include "logging/logAsyncWriter.hpp"27#include "logging/logMessage.hpp"28#include "logTestFixture.hpp"29#include "logTestUtils.inline.hpp"30#include "unittest.hpp"3132class AsyncLogTest : public LogTestFixture {33public:34AsyncLogTest() {35if(!LogConfiguration::is_async_mode()) {36fprintf(stderr, "Warning: asynclog is OFF.\n");37}38}3940void test_asynclog_ls() {41LogStream ls(Log(logging)::info());42outputStream* os = &ls;43os->print_cr("LogStreamWithAsyncLogImpl");44os->print_cr("LogStreamWithAsyncLogImpl secondline");4546//multi-lines47os->print("logStream msg1-");48os->print("msg2-");49os->print("msg3\n");50os->print_cr("logStream newline");51}5253void test_asynclog_raw() {54Log(logging) logger;55#define LOG_LEVEL(level, name) logger.name("1" #level);56LOG_LEVEL_LIST57#undef LOG_LEVEL5859LogTarget(Trace, logging) t;60LogTarget(Debug, logging) d;61EXPECT_FALSE(t.is_enabled());62EXPECT_TRUE(d.is_enabled());6364d.print("AsyncLogTarget.print = %d", 1);65log_trace(logging)("log_trace-test");66log_debug(logging)("log_debug-test");67}68};6970TEST_VM(AsyncLogBufferTest, fifo) {71LinkedListDeque<int, mtLogging> fifo;72LinkedListImpl<int, ResourceObj::C_HEAP, mtLogging> result;7374fifo.push_back(1);75EXPECT_EQ((size_t)1, fifo.size());76EXPECT_EQ(1, *(fifo.back()));7778fifo.pop_all(&result);79EXPECT_EQ((size_t)0, fifo.size());80EXPECT_EQ(NULL, fifo.back());81EXPECT_EQ((size_t)1, result.size());82EXPECT_EQ(1, *(result.head()->data()));83result.clear();8485fifo.push_back(2);86fifo.push_back(1);87fifo.pop_all(&result);88EXPECT_EQ((size_t)2, result.size());89EXPECT_EQ(2, *(result.head()->data()));90EXPECT_EQ(1, *(result.head()->next()->data()));91result.clear();92const int N = 1000;93for (int i=0; i<N; ++i) {94fifo.push_back(i);95}96fifo.pop_all(&result);9798EXPECT_EQ((size_t)N, result.size());99LinkedListIterator<int> it(result.head());100for (int i=0; i<N; ++i) {101int* e = it.next();102EXPECT_EQ(i, *e);103}104}105106TEST_VM(AsyncLogBufferTest, deque) {107LinkedListDeque<int, mtLogging> deque;108const int N = 10;109110EXPECT_EQ(NULL, deque.front());111EXPECT_EQ(NULL, deque.back());112for (int i = 0; i < N; ++i) {113deque.push_back(i);114}115116EXPECT_EQ(0, *(deque.front()));117EXPECT_EQ(N-1, *(deque.back()));118EXPECT_EQ((size_t)N, deque.size());119120deque.pop_front();121EXPECT_EQ((size_t)(N - 1), deque.size());122EXPECT_EQ(1, *(deque.front()));123EXPECT_EQ(N - 1, *(deque.back()));124125deque.pop_front();126EXPECT_EQ((size_t)(N - 2), deque.size());127EXPECT_EQ(2, *(deque.front()));128EXPECT_EQ(N - 1, *(deque.back()));129130131for (int i=2; i < N-1; ++i) {132deque.pop_front();133}134EXPECT_EQ((size_t)1, deque.size());135EXPECT_EQ(N - 1, *(deque.back()));136EXPECT_EQ(deque.back(), deque.front());137138deque.pop_front();139EXPECT_EQ((size_t)0, deque.size());140}141142TEST_VM_F(AsyncLogTest, asynclog) {143set_log_config(TestLogFileName, "logging=debug");144145test_asynclog_ls();146test_asynclog_raw();147AsyncLogWriter::flush();148149EXPECT_TRUE(file_contains_substring(TestLogFileName, "LogStreamWithAsyncLogImpl"));150EXPECT_TRUE(file_contains_substring(TestLogFileName, "logStream msg1-msg2-msg3"));151EXPECT_TRUE(file_contains_substring(TestLogFileName, "logStream newline"));152153EXPECT_TRUE(file_contains_substring(TestLogFileName, "1Debug"));154EXPECT_TRUE(file_contains_substring(TestLogFileName, "1Info"));155EXPECT_TRUE(file_contains_substring(TestLogFileName, "1Warning"));156EXPECT_TRUE(file_contains_substring(TestLogFileName, "1Error"));157EXPECT_FALSE(file_contains_substring(TestLogFileName, "1Trace")); // trace message is masked out158159EXPECT_TRUE(file_contains_substring(TestLogFileName, "AsyncLogTarget.print = 1"));160EXPECT_FALSE(file_contains_substring(TestLogFileName, "log_trace-test")); // trace message is masked out161EXPECT_TRUE(file_contains_substring(TestLogFileName, "log_debug-test"));162}163164TEST_VM_F(AsyncLogTest, logMessage) {165set_log_config(TestLogFileName, "logging=debug");166167const int MULTI_LINES = 20;168{169170LogMessage(logging) msg;171Log(logging) logger;172173for (int i = 0; i < MULTI_LINES; ++i) {174msg.debug("nonbreakable log message line-%02d", i);175176if (0 == (i % 4)) {177logger.debug("a noisy message from other logger");178}179}180logger.debug("a noisy message from other logger");181}182AsyncLogWriter::flush();183184ResourceMark rm;185LogMessageBuffer buffer;186const char* strs[MULTI_LINES + 1];187strs[MULTI_LINES] = NULL;188for (int i = 0; i < MULTI_LINES; ++i) {189stringStream ss;190ss.print_cr("nonbreakable log message line-%02d", i);191strs[i] = ss.as_string();192}193// check nonbreakable log messages are consecutive194EXPECT_TRUE(file_contains_substrings_in_order(TestLogFileName, strs));195EXPECT_TRUE(file_contains_substring(TestLogFileName, "a noisy message from other logger"));196}197198TEST_VM_F(AsyncLogTest, droppingMessage) {199set_log_config(TestLogFileName, "logging=debug");200const size_t sz = 100;201202if (AsyncLogWriter::instance() != nullptr) {203// shrink async buffer.204AutoModifyRestore<size_t> saver(AsyncLogBufferSize, sz * 1024 /*in byte*/);205LogMessage(logging) lm;206207// write 100x more messages than its capacity in burst208for (size_t i = 0; i < sz * 100; ++i) {209lm.debug("a lot of log...");210}211lm.flush();212AsyncLogWriter::flush();213EXPECT_TRUE(file_contains_substring(TestLogFileName, "messages dropped due to async logging"));214}215}216217218