Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/gc_verbose_old/VerboseStandardStreamOutput.cpp
5985 views
1
2
/*******************************************************************************
3
* Copyright (c) 1991, 2014 IBM Corp. and others
4
*
5
* This program and the accompanying materials are made available under
6
* the terms of the Eclipse Public License 2.0 which accompanies this
7
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
8
* or the Apache License, Version 2.0 which accompanies this distribution and
9
* is available at https://www.apache.org/licenses/LICENSE-2.0.
10
*
11
* This Source Code may also be made available under the following
12
* Secondary Licenses when the conditions for such availability set
13
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
14
* General Public License, version 2 with the GNU Classpath
15
* Exception [1] and GNU General Public License, version 2 with the
16
* OpenJDK Assembly Exception [2].
17
*
18
* [1] https://www.gnu.org/software/classpath/license.html
19
* [2] http://openjdk.java.net/legal/assembly-exception.html
20
*
21
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
22
*******************************************************************************/
23
24
#include "VerboseStandardStreamOutput.hpp"
25
#include "VerboseEvent.hpp"
26
#include "VerboseBuffer.hpp"
27
#include "EnvironmentBase.hpp"
28
#include "GCExtensions.hpp"
29
#include <string.h>
30
31
#define INPUT_STRING_SIZE 236
32
#define INDENT_SPACER " "
33
34
/**
35
* Formats and output data.
36
* Called by events, formats the data passed into verbose form and outputs it.
37
* @param indent The current level of indentation.
38
* @param format String to format the data into.
39
*/
40
void
41
MM_VerboseStandardStreamOutput::formatAndOutput(J9VMThread *vmThread, UDATA indent, const char *format, ...)
42
{
43
char inputString[INPUT_STRING_SIZE];
44
char localBuf[256];
45
UDATA length;
46
va_list args;
47
MM_EnvironmentBase* env = MM_EnvironmentBase::getEnvironment((OMR_VMThread *)vmThread->omrVMThread);
48
PORT_ACCESS_FROM_ENVIRONMENT(env);
49
50
localBuf[0] = '\0';
51
for(UDATA i=0; i < indent; i++) {
52
strcat(localBuf, INDENT_SPACER);
53
}
54
55
va_start(args, format);
56
j9str_vprintf(inputString, INPUT_STRING_SIZE, format, args);
57
va_end(args);
58
59
strcat(localBuf, inputString);
60
strcat(localBuf, "\n");
61
length = strlen(localBuf);
62
63
if(NULL != _buffer) {
64
if(_buffer->add(env, localBuf)) {
65
/* Added successfully - return */
66
return;
67
}
68
}
69
70
/* If there's a problem with the buffering, we just write straight to the tty */
71
if(STDERR == _currentStream){
72
j9file_write_text(J9PORT_TTY_ERR, localBuf, length);
73
} else {
74
j9file_write_text(J9PORT_TTY_OUT, localBuf, length);
75
}
76
}
77
78
/**
79
* Create a new MM_VerboseStandardStreamOutput instance.
80
*/
81
MM_VerboseStandardStreamOutput *
82
MM_VerboseStandardStreamOutput::newInstance(MM_EnvironmentBase *env, const char *filename)
83
{
84
MM_GCExtensions *extensions = MM_GCExtensions::getExtensions(env->getOmrVM());
85
86
MM_VerboseStandardStreamOutput *agent = (MM_VerboseStandardStreamOutput *)extensions->getForge()->allocate(sizeof(MM_VerboseStandardStreamOutput), MM_AllocationCategory::DIAGNOSTIC, J9_GET_CALLSITE());
87
if(agent) {
88
new(agent) MM_VerboseStandardStreamOutput(env);
89
if(!agent->initialize(env, filename)) {
90
agent->kill(env);
91
agent = NULL;
92
}
93
}
94
return agent;
95
}
96
97
/**
98
* Initializes the MM_VerboseStandardStreamOutput instance.
99
*/
100
bool
101
MM_VerboseStandardStreamOutput::initialize(MM_EnvironmentBase *env, const char *filename)
102
{
103
J9JavaVM* javaVM = (J9JavaVM *)env->getOmrVM()->_language_vm;
104
PORT_ACCESS_FROM_JAVAVM(javaVM);
105
const char* version = javaVM->memoryManagerFunctions->omrgc_get_version(env->getOmrVM());
106
107
setStream(getStreamID(env, filename));
108
109
if(STDERR == _currentStream){
110
j9file_printf(PORTLIB, J9PORT_TTY_ERR, "\n" VERBOSEGC_HEADER_TEXT_ALL, version);
111
} else {
112
j9file_printf(PORTLIB, J9PORT_TTY_OUT, "\n" VERBOSEGC_HEADER_TEXT_ALL, version);
113
}
114
115
if(NULL == (_buffer = MM_VerboseBuffer::newInstance(env, INITIAL_BUFFER_SIZE))) {
116
return false;
117
}
118
119
return true;
120
}
121
122
/**
123
* Closes the agent's output stream.
124
*/
125
void
126
MM_VerboseStandardStreamOutput::closeStream(MM_EnvironmentBase *env)
127
{
128
UDATA length = strlen(VERBOSEGC_FOOTER_TEXT);
129
PORT_ACCESS_FROM_ENVIRONMENT(env);
130
131
if(STDERR == _currentStream){
132
j9file_write_text(J9PORT_TTY_ERR, VERBOSEGC_FOOTER_TEXT "\n", length + 1);
133
} else {
134
j9file_write_text(J9PORT_TTY_OUT, VERBOSEGC_FOOTER_TEXT "\n", length + 1);
135
}
136
}
137
138
/**
139
* Tear down the structures managed by the MM_VerboseStandardStreamOutput.
140
* Tears down the verbose buffer.
141
*/
142
void
143
MM_VerboseStandardStreamOutput::tearDown(MM_EnvironmentBase *env)
144
{
145
if(NULL != _buffer) {
146
_buffer->kill(env);
147
}
148
}
149
150
/**
151
* Flushes the verbose buffer to the output stream.
152
*/
153
void
154
MM_VerboseStandardStreamOutput::endOfCycle(J9VMThread *vmThread)
155
{
156
PORT_ACCESS_FROM_VMC(vmThread);
157
158
if(NULL != _buffer){
159
if(STDERR == _currentStream){
160
j9file_write_text(J9PORT_TTY_ERR, _buffer->contents(), _buffer->currentSize());
161
j9file_write_text(J9PORT_TTY_ERR, "\n", 1);
162
} else {
163
j9file_write_text(J9PORT_TTY_OUT, _buffer->contents(), _buffer->currentSize());
164
j9file_write_text(J9PORT_TTY_OUT, "\n", 1);
165
}
166
_buffer->reset();
167
}
168
}
169
170
bool
171
MM_VerboseStandardStreamOutput::reconfigure(MM_EnvironmentBase *env, const char *filename, UDATA fileCount, UDATA iterations)
172
{
173
setStream(getStreamID(env, filename));
174
return true;
175
}
176
177
MM_VerboseStandardStreamOutput::StreamID
178
MM_VerboseStandardStreamOutput::getStreamID(MM_EnvironmentBase *env, const char *string)
179
{
180
if(NULL == string) {
181
return STDERR;
182
}
183
184
if(!strcmp(string, "stdout")) {
185
return STDOUT;
186
}
187
188
return STDERR;
189
}
190
191