| 1 | /* |
| 2 | * Copyright (c) 2015, 2018, 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 "jvm.h" |
| 26 | #include "logging/log.hpp" |
| 27 | #include "logging/logFileStreamOutput.hpp" |
| 28 | #include "logging/logOutput.hpp" |
| 29 | #include "logging/logSelection.hpp" |
| 30 | #include "logging/logTagSet.hpp" |
| 31 | #include "memory/allocation.inline.hpp" |
| 32 | #include "runtime/mutexLocker.hpp" |
| 33 | #include "runtime/os.inline.hpp" |
| 34 | |
| 35 | LogOutput::~LogOutput() { |
| 36 | os::free(_config_string); |
| 37 | } |
| 38 | |
| 39 | void LogOutput::describe(outputStream *out) { |
| 40 | out->print("%s " , name()); |
| 41 | out->print_raw(config_string()); // raw printed because length might exceed O_BUFLEN |
| 42 | |
| 43 | bool has_decorator = false; |
| 44 | char delimiter = ' '; |
| 45 | for (size_t d = 0; d < LogDecorators::Count; d++) { |
| 46 | LogDecorators::Decorator decorator = static_cast<LogDecorators::Decorator>(d); |
| 47 | if (decorators().is_decorator(decorator)) { |
| 48 | has_decorator = true; |
| 49 | out->print("%c%s" , delimiter, LogDecorators::name(decorator)); |
| 50 | delimiter = ','; |
| 51 | } |
| 52 | } |
| 53 | if (!has_decorator) { |
| 54 | out->print(" none" ); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void LogOutput::set_config_string(const char* string) { |
| 59 | os::free(_config_string); |
| 60 | _config_string = os::strdup(string, mtLogging); |
| 61 | _config_string_buffer_size = strlen(_config_string) + 1; |
| 62 | } |
| 63 | |
| 64 | void LogOutput::add_to_config_string(const LogSelection& selection) { |
| 65 | if (_config_string_buffer_size < InitialConfigBufferSize) { |
| 66 | _config_string_buffer_size = InitialConfigBufferSize; |
| 67 | _config_string = REALLOC_C_HEAP_ARRAY(char, _config_string, _config_string_buffer_size, mtLogging); |
| 68 | } |
| 69 | |
| 70 | size_t offset = strlen(_config_string); |
| 71 | if (offset > 0) { |
| 72 | // Add commas in-between tag and level combinations in the config string |
| 73 | _config_string[offset++] = ','; |
| 74 | } |
| 75 | |
| 76 | for (;;) { |
| 77 | int ret = selection.describe(_config_string + offset, |
| 78 | _config_string_buffer_size - offset); |
| 79 | if (ret == -1) { |
| 80 | // Double the buffer size and retry |
| 81 | _config_string_buffer_size *= 2; |
| 82 | _config_string = REALLOC_C_HEAP_ARRAY(char, _config_string, _config_string_buffer_size, mtLogging); |
| 83 | continue; |
| 84 | } |
| 85 | break; |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | static int tag_cmp(const void *a, const void *b) { |
| 91 | return static_cast<const LogTagType*>(a) - static_cast<const LogTagType*>(b); |
| 92 | } |
| 93 | |
| 94 | static void sort_tags(LogTagType tags[LogTag::MaxTags]) { |
| 95 | size_t ntags = 0; |
| 96 | while (tags[ntags] != LogTag::__NO_TAG) { |
| 97 | ntags++; |
| 98 | } |
| 99 | qsort(tags, ntags, sizeof(*tags), tag_cmp); |
| 100 | } |
| 101 | |
| 102 | static const size_t MaxSubsets = 1 << LogTag::MaxTags; |
| 103 | |
| 104 | // Fill result with all possible subsets of the given tag set. Empty set not included. |
| 105 | // For example, if tags is {gc, heap} then the result is {{gc}, {heap}, {gc, heap}}. |
| 106 | // (Arguments with default values are intended exclusively for recursive calls.) |
| 107 | static void generate_all_subsets_of(LogTagType result[MaxSubsets][LogTag::MaxTags], |
| 108 | size_t* result_size, |
| 109 | const LogTagType tags[LogTag::MaxTags], |
| 110 | LogTagType subset[LogTag::MaxTags] = NULL, |
| 111 | const size_t subset_size = 0, |
| 112 | const size_t depth = 0) { |
| 113 | assert(subset_size <= LogTag::MaxTags, "subset must never have more than MaxTags tags" ); |
| 114 | assert(depth <= LogTag::MaxTags, "recursion depth overflow" ); |
| 115 | |
| 116 | if (subset == NULL) { |
| 117 | assert(*result_size == 0, "outer (non-recursive) call expects result_size to be 0" ); |
| 118 | // Make subset the first element in the result array initially |
| 119 | subset = result[0]; |
| 120 | } |
| 121 | assert((void*) subset >= &result[0] && (void*) subset <= &result[MaxSubsets - 1], |
| 122 | "subset should always point to element in result" ); |
| 123 | |
| 124 | if (depth == LogTag::MaxTags || tags[depth] == LogTag::__NO_TAG) { |
| 125 | if (subset_size == 0) { |
| 126 | // Ignore empty subset |
| 127 | return; |
| 128 | } |
| 129 | if (subset_size != LogTag::MaxTags) { |
| 130 | subset[subset_size] = LogTag::__NO_TAG; |
| 131 | } |
| 132 | assert(*result_size < MaxSubsets, "subsets overflow" ); |
| 133 | *result_size += 1; |
| 134 | |
| 135 | // Bump subset and copy over current state |
| 136 | memcpy(result[*result_size], subset, sizeof(*subset) * LogTag::MaxTags); |
| 137 | subset = result[*result_size]; |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | // Recurse, excluding the tag of the current depth |
| 142 | generate_all_subsets_of(result, result_size, tags, subset, subset_size, depth + 1); |
| 143 | // ... and with it included |
| 144 | subset[subset_size] = tags[depth]; |
| 145 | generate_all_subsets_of(result, result_size, tags, subset, subset_size + 1, depth + 1); |
| 146 | } |
| 147 | |
| 148 | // Generate all possible selections (for the given level) based on the given tag set, |
| 149 | // and add them to the selections array (growing it as necessary). |
| 150 | static void add_selections(LogSelection** selections, |
| 151 | size_t* n_selections, |
| 152 | size_t* selections_cap, |
| 153 | const LogTagSet& tagset, |
| 154 | LogLevelType level) { |
| 155 | LogTagType tags[LogTag::MaxTags] = { LogTag::__NO_TAG }; |
| 156 | for (size_t i = 0; i < tagset.ntags(); i++) { |
| 157 | tags[i] = tagset.tag(i); |
| 158 | } |
| 159 | |
| 160 | size_t n_subsets = 0; |
| 161 | LogTagType subsets[MaxSubsets][LogTag::MaxTags]; |
| 162 | generate_all_subsets_of(subsets, &n_subsets, tags); |
| 163 | |
| 164 | for (size_t i = 0; i < n_subsets; i++) { |
| 165 | // Always keep tags sorted |
| 166 | sort_tags(subsets[i]); |
| 167 | |
| 168 | // Ignore subsets already represented in selections |
| 169 | bool unique = true; |
| 170 | for (size_t sel = 0; sel < *n_selections; sel++) { |
| 171 | if (level == (*selections)[sel].level() && (*selections)[sel].consists_of(subsets[i])) { |
| 172 | unique = false; |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | if (!unique) { |
| 177 | continue; |
| 178 | } |
| 179 | |
| 180 | LogSelection exact_selection(subsets[i], false, level); |
| 181 | LogSelection wildcard_selection(subsets[i], true, level); |
| 182 | |
| 183 | // Check if the two selections match any tag sets |
| 184 | bool wildcard_match = false; |
| 185 | bool exact_match = false; |
| 186 | for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) { |
| 187 | if (!wildcard_selection.selects(*ts)) { |
| 188 | continue; |
| 189 | } |
| 190 | |
| 191 | wildcard_match = true; |
| 192 | if (exact_selection.selects(*ts)) { |
| 193 | exact_match = true; |
| 194 | } |
| 195 | if (exact_match) { |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if (!wildcard_match && !exact_match) { |
| 201 | continue; |
| 202 | } |
| 203 | |
| 204 | // Ensure there's enough room for both wildcard_match and exact_match |
| 205 | if (*n_selections + 2 > *selections_cap) { |
| 206 | *selections_cap *= 2; |
| 207 | *selections = REALLOC_C_HEAP_ARRAY(LogSelection, *selections, *selections_cap, mtLogging); |
| 208 | } |
| 209 | |
| 210 | // Add found matching selections to the result array |
| 211 | if (exact_match) { |
| 212 | (*selections)[(*n_selections)++] = exact_selection; |
| 213 | } |
| 214 | if (wildcard_match) { |
| 215 | (*selections)[(*n_selections)++] = wildcard_selection; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | void LogOutput::update_config_string(const size_t on_level[LogLevel::Count]) { |
| 221 | // Find the most common level (MCL) |
| 222 | LogLevelType mcl = LogLevel::Off; |
| 223 | size_t max = on_level[LogLevel::Off]; |
| 224 | for (LogLevelType l = LogLevel::First; l <= LogLevel::Last; l = static_cast<LogLevelType>(l + 1)) { |
| 225 | if (on_level[l] > max) { |
| 226 | mcl = l; |
| 227 | max = on_level[l]; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // Always let the first part of each output's config string be "all=<MCL>" |
| 232 | { |
| 233 | char buf[64]; |
| 234 | jio_snprintf(buf, sizeof(buf), "all=%s" , LogLevel::name(mcl)); |
| 235 | set_config_string(buf); |
| 236 | } |
| 237 | |
| 238 | // If there are no deviating tag sets, we're done |
| 239 | size_t deviating_tagsets = LogTagSet::ntagsets() - max; |
| 240 | if (deviating_tagsets == 0) { |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | size_t n_selections = 0; |
| 245 | size_t selections_cap = 4 * MaxSubsets; // Start with some reasonably large initial capacity |
| 246 | LogSelection* selections = NEW_C_HEAP_ARRAY(LogSelection, selections_cap, mtLogging); |
| 247 | |
| 248 | size_t n_deviates = 0; |
| 249 | const LogTagSet** deviates = NEW_C_HEAP_ARRAY(const LogTagSet*, deviating_tagsets, mtLogging); |
| 250 | |
| 251 | // Generate all possible selections involving the deviating tag sets |
| 252 | for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) { |
| 253 | LogLevelType level = ts->level_for(this); |
| 254 | if (level == mcl) { |
| 255 | continue; |
| 256 | } |
| 257 | deviates[n_deviates++] = ts; |
| 258 | add_selections(&selections, &n_selections, &selections_cap, *ts, level); |
| 259 | } |
| 260 | |
| 261 | // Reduce deviates greedily, using the "best" selection at each step to reduce the number of deviating tag sets |
| 262 | while (n_deviates > 0) { |
| 263 | size_t prev_deviates = n_deviates; |
| 264 | int max_score = 0; |
| 265 | |
| 266 | guarantee(n_selections > 0, "Cannot find maximal selection." ); |
| 267 | const LogSelection* best_selection = &selections[0]; |
| 268 | for (size_t i = 0; i < n_selections; i++) { |
| 269 | |
| 270 | // Give the selection a score based on how many deviating tag sets it selects (with correct level) |
| 271 | int score = 0; |
| 272 | for (size_t d = 0; d < n_deviates; d++) { |
| 273 | if (selections[i].selects(*deviates[d]) && deviates[d]->level_for(this) == selections[i].level()) { |
| 274 | score++; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | // Ignore selections with lower score than the current best even before subtracting mismatched selections |
| 279 | if (score < max_score) { |
| 280 | continue; |
| 281 | } |
| 282 | |
| 283 | // Subtract from the score the number of tag sets it selects with an incorrect level |
| 284 | for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) { |
| 285 | if (selections[i].selects(*ts) && ts->level_for(this) != selections[i].level()) { |
| 286 | score--; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // Pick the selection with the best score, or in the case of a tie, the one with fewest tags |
| 291 | if (score > max_score || |
| 292 | (score == max_score && selections[i].ntags() < best_selection->ntags())) { |
| 293 | max_score = score; |
| 294 | best_selection = &selections[i]; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | add_to_config_string(*best_selection); |
| 299 | |
| 300 | // Remove all deviates that this selection covered |
| 301 | for (size_t d = 0; d < n_deviates;) { |
| 302 | if (deviates[d]->level_for(this) == best_selection->level() && best_selection->selects(*deviates[d])) { |
| 303 | deviates[d] = deviates[--n_deviates]; |
| 304 | continue; |
| 305 | } |
| 306 | d++; |
| 307 | } |
| 308 | |
| 309 | // Add back any new deviates that this selection added (no array growth since removed > added) |
| 310 | for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) { |
| 311 | if (ts->level_for(this) == best_selection->level() || !best_selection->selects(*ts)) { |
| 312 | continue; |
| 313 | } |
| 314 | |
| 315 | bool already_added = false; |
| 316 | for (size_t dev = 0; dev < n_deviates; dev++) { |
| 317 | if (deviates[dev] == ts) { |
| 318 | already_added = true; |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | if (already_added) { |
| 323 | continue; |
| 324 | } |
| 325 | |
| 326 | deviates[n_deviates++] = ts; |
| 327 | } |
| 328 | |
| 329 | // Reset the selections and generate a new ones based on the updated deviating tag sets |
| 330 | n_selections = 0; |
| 331 | for (size_t d = 0; d < n_deviates; d++) { |
| 332 | add_selections(&selections, &n_selections, &selections_cap, *deviates[d], deviates[d]->level_for(this)); |
| 333 | } |
| 334 | |
| 335 | assert(n_deviates < deviating_tagsets, "deviating tag set array overflow" ); |
| 336 | assert(prev_deviates > n_deviates, "number of deviating tag sets must never grow" ); |
| 337 | } |
| 338 | FREE_C_HEAP_ARRAY(LogTagSet*, deviates); |
| 339 | FREE_C_HEAP_ARRAY(Selection, selections); |
| 340 | } |
| 341 | |
| 342 | |