-
Notifications
You must be signed in to change notification settings - Fork 392
/
VerboseWriterFileLogging.cpp
327 lines (282 loc) · 9.17 KB
/
VerboseWriterFileLogging.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*******************************************************************************
* Copyright (c) 1991, 2016 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* 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
*******************************************************************************/
#include "modronapicore.hpp"
#include "VerboseManager.hpp"
#include "VerboseWriterFileLogging.hpp"
#include "GCExtensionsBase.hpp"
#include "EnvironmentBase.hpp"
#include <string.h>
enum {
single_file = 0,
rotating_files
};
MM_VerboseWriterFileLogging::MM_VerboseWriterFileLogging(MM_EnvironmentBase *env, MM_VerboseManager *manager, WriterType type)
:MM_VerboseWriter(type)
,_filename(NULL)
,_mode(single_file)
,_currentFile(0)
,_currentCycle(0)
,_tokens(NULL)
,_manager(manager)
{
/* No implementation */
}
/**
* Initializes the MM_VerboseWriterFileLogging instance.
* @return true on success, false otherwise
*/
bool
MM_VerboseWriterFileLogging::initialize(MM_EnvironmentBase *env, const char *filename, uintptr_t numFiles, uintptr_t numCycles)
{
MM_VerboseWriter::initialize(env);
_numFiles = numFiles;
_numCycles = numCycles;
if((_numFiles > 0) && (_numCycles > 0)) {
_mode = rotating_files;
} else {
_mode = single_file;
}
if (!initializeTokens(env)) {
return false;
}
if (!initializeFilename(env, filename)) {
return false;
}
intptr_t initialFile = findInitialFile(env);
if (initialFile < 0) {
return false;
}
_currentFile = initialFile;
if(!openFile(env)) {
return false;
}
return true;
}
/**
* Tear down the structures managed by the MM_VerboseWriterFileLogging.
* Tears down the verbose buffer.
*/
void
MM_VerboseWriterFileLogging::tearDown(MM_EnvironmentBase *env)
{
OMRPORT_ACCESS_FROM_OMRPORT(env->getPortLibrary());
MM_GCExtensionsBase *extensions = MM_GCExtensionsBase::getExtensions(env->getOmrVM());
omrstr_free_tokens(_tokens);
_tokens = NULL;
extensions->getForge()->free(_filename);
_filename = NULL;
MM_VerboseWriter::tearDown(env);
}
/**
* Initialize the _tokens field.
* JTCJAZZ 36600 alias %p to be the same as %pid
*/
bool
MM_VerboseWriterFileLogging::initializeTokens(MM_EnvironmentBase *env)
{
OMRPORT_ACCESS_FROM_OMRPORT(env->getPortLibrary());
char pidBuffer[64];
_tokens = omrstr_create_tokens(omrtime_current_time_millis());
if (_tokens == NULL) {
return false;
}
if (sizeof(pidBuffer) < omrstr_subst_tokens(pidBuffer, sizeof(pidBuffer), "%pid", _tokens)) {
return false;
}
if (omrstr_set_token(_tokens, "p", "%s", pidBuffer)) {
return false;
}
return true;
}
/**
* Initialize the _filename field based on filename.
*
* Since token substitution only supports tokens starting with %, all # characters in the
* filename will be replaced with %seq (unless the # is already preceded by an odd number
* of % signs, in which case it is replaced with seq).
*
* e.g. foo# --> foo%seq
* foo%# --> foo%seq
* foo%%# --> foo%%%seq
*
* If %seq or %# is not specified, and if rotating logs have been requested, ".%seq" is
* appended to the log name.
*
* If the resulting filename is too large to fit in the buffer, it is truncated.
*
* @param[in] env the current environment
* @param[in] filename the user specified filename
*
* @return true on success, false on failure
*/
bool
MM_VerboseWriterFileLogging::initializeFilename(MM_EnvironmentBase *env, const char *filename)
{
MM_GCExtensionsBase *extensions = MM_GCExtensionsBase::getExtensions(env->getOmrVM());
if (_mode == rotating_files) {
const char* read = filename;
/* count the number of hashes in the source string */
uintptr_t hashCount = 0;
for (read = filename; '\0' != *read; read++) {
if ('#' == *read) {
hashCount++;
}
}
/* allocate memory for the copied template filename */
uintptr_t nameLen = strlen(filename) + 1;
if (hashCount > 0) {
/* each # expands into %seq, so for each # add 3 to len */
nameLen += hashCount * (sizeof("seq") - 1);
} else {
/* if there are no hashes we may append .%seq to the end */
nameLen += sizeof(".%seq") - 1;
}
_filename = (char*)extensions->getForge()->allocate(nameLen, OMR::GC::AllocationCategory::DIAGNOSTIC, OMR_GET_CALLSITE());
if (NULL == _filename) {
return false;
}
/* copy the original filename into the allocated memory, expanding #s to %seq */
bool foundSeq = false;
bool oddPercents = false;
char* write = _filename;
for (read = filename; '\0' != *read; read++) {
/* check to see if %seq appears in the source filename */
if (oddPercents && (0 == strncmp(read, "seq", 3))) {
foundSeq = true;
}
if ('#' == *read) {
strcpy(write, oddPercents ? "seq" : "%seq");
write += strlen(write);
} else {
*write++ = *read;
}
oddPercents = ('%' == *read) ? !oddPercents : false;
}
*write = '\0';
if ( (false == foundSeq) && (0 == hashCount) ) {
strcpy(write, ".%seq");
}
} else {
_filename = (char*)extensions->getForge()->allocate(strlen(filename) + 1, OMR::GC::AllocationCategory::DIAGNOSTIC, OMR_GET_CALLSITE());
if (NULL == _filename) {
return false;
}
strcpy(_filename, filename);
}
return true;
}
/**
* Generate an expanded filename based on currentFile.
* The caller is responsible for freeing the returned memory.
*
* @param env the current thread
* @param currentFile the current file number to substitute into the filename template
*
* @return NULL on failure, allocated memory on success
*/
char*
MM_VerboseWriterFileLogging::expandFilename(MM_EnvironmentBase *env, uintptr_t currentFile)
{
OMRPORT_ACCESS_FROM_OMRPORT(env->getPortLibrary());
MM_GCExtensionsBase *extensions = MM_GCExtensionsBase::getExtensions(env->getOmrVM());
if (_mode == rotating_files) {
omrstr_set_token(_tokens, "seq", "%03zu", currentFile + 1); /* plus one so the filenames start from .001 instead of .000 */
}
uintptr_t len = omrstr_subst_tokens(NULL, 0, _filename, _tokens);
char *filenameToOpen = (char*)extensions->getForge()->allocate(len, OMR::GC::AllocationCategory::DIAGNOSTIC, OMR_GET_CALLSITE());
if (NULL != filenameToOpen) {
omrstr_subst_tokens(filenameToOpen, len, _filename, _tokens);
}
return filenameToOpen;
}
/**
* Probe the file system for existing files. Determine
* the first number which is unused, or the number of the oldest
* file if all numbers are used.
* @return the first file number to use (starting at 0), or -1 on failure
*/
intptr_t
MM_VerboseWriterFileLogging::findInitialFile(MM_EnvironmentBase *env)
{
OMRPORT_ACCESS_FROM_OMRPORT(env->getPortLibrary());
MM_GCExtensionsBase *extensions = MM_GCExtensionsBase::getExtensions(env->getOmrVM());
int64_t oldestTime = J9CONST64(0x7FFFFFFFFFFFFFFF); /* the highest possible time. */
intptr_t oldestFile = 0;
if (_mode != rotating_files) {
/* nothing to do */
return 0;
}
for (uintptr_t currentFile = 0; currentFile < _numFiles; currentFile++) {
char *filenameToOpen = expandFilename(env, currentFile);
if (NULL == filenameToOpen) {
return -1;
}
int64_t thisTime = omrfile_lastmod(filenameToOpen);
extensions->getForge()->free(filenameToOpen);
if (thisTime < 0) {
/* file doesn't exist, or some other problem reading the file */
oldestFile = currentFile;
break;
} else if (thisTime < oldestTime) {
oldestTime = thisTime;
oldestFile = currentFile;
}
}
return oldestFile;
}
/**
* Closes the agent's output stream.
*/
void
MM_VerboseWriterFileLogging::closeStream(MM_EnvironmentBase *env)
{
closeFile(env);
}
/**
* Flushes the verbose buffer to the output stream.
* Also cycles the output files if necessary.
*/
void
MM_VerboseWriterFileLogging::endOfCycle(MM_EnvironmentBase *env)
{
if(rotating_files == _mode) {
_currentCycle = (_currentCycle + 1) % _numCycles;
if(0 == _currentCycle) {
closeFile(env);
_currentFile = (_currentFile + 1) % _numFiles;
openFile(env, true);
}
}
}
/**
* Reconfigures the agent according to the parameters passed.
* Required for Dynamic verbose gc configuration.
* @param filename The name of the file or output stream to log to.
* @param fileCount The number of files to log to.
* @param iterations The number of gc cycles to log to each file.
*/
bool
MM_VerboseWriterFileLogging::reconfigure(MM_EnvironmentBase *env, const char *filename, uintptr_t numFiles, uintptr_t numCycles)
{
closeFile(env);
return initialize(env, filename, numFiles, numCycles);
}