1 /*
   2  * Copyright (c) 2001, 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 
  25 #include "precompiled.hpp"
  26 #include "gc/shared/taskqueue.hpp"
  27 #include "gc/shared/owstTaskTerminator.hpp"
  28 #include "oops/oop.inline.hpp"
  29 #include "logging/log.hpp"
  30 #include "runtime/atomic.hpp"
  31 #include "runtime/os.hpp"
  32 #include "runtime/thread.inline.hpp"
  33 #include "utilities/debug.hpp"
  34 #include "utilities/stack.inline.hpp"
  35 
  36 #if TASKQUEUE_STATS
  37 const char * const TaskQueueStats::_names[last_stat_id] = {
  38   "qpush", "qpop", "qpop-s", "qattempt", "qsteal", "opush", "omax"
  39 };
  40 
  41 TaskQueueStats & TaskQueueStats::operator +=(const TaskQueueStats & addend)
  42 {
  43   for (unsigned int i = 0; i < last_stat_id; ++i) {
  44     _stats[i] += addend._stats[i];
  45   }
  46   return *this;
  47 }
  48 
  49 void TaskQueueStats::print_header(unsigned int line, outputStream* const stream,
  50                                   unsigned int width)
  51 {
  52   // Use a width w: 1 <= w <= max_width
  53   const unsigned int max_width = 40;
  54   const unsigned int w = clamp(width, 1u, max_width);
  55 
  56   if (line == 0) { // spaces equal in width to the header
  57     const unsigned int hdr_width = w * last_stat_id + last_stat_id - 1;
  58     stream->print("%*s", hdr_width, " ");
  59   } else if (line == 1) { // labels
  60     stream->print("%*s", w, _names[0]);
  61     for (unsigned int i = 1; i < last_stat_id; ++i) {
  62       stream->print(" %*s", w, _names[i]);
  63     }
  64   } else if (line == 2) { // dashed lines
  65     char dashes[max_width + 1];
  66     memset(dashes, '-', w);
  67     dashes[w] = '\0';
  68     stream->print("%s", dashes);
  69     for (unsigned int i = 1; i < last_stat_id; ++i) {
  70       stream->print(" %s", dashes);
  71     }
  72   }
  73 }
  74 
  75 void TaskQueueStats::print(outputStream* stream, unsigned int width) const
  76 {
  77   #define FMT SIZE_FORMAT_W(*)
  78   stream->print(FMT, width, _stats[0]);
  79   for (unsigned int i = 1; i < last_stat_id; ++i) {
  80     stream->print(" " FMT, width, _stats[i]);
  81   }
  82   #undef FMT
  83 }
  84 
  85 #ifdef ASSERT
  86 // Invariants which should hold after a TaskQueue has been emptied and is
  87 // quiescent; they do not hold at arbitrary times.
  88 void TaskQueueStats::verify() const
  89 {
  90   assert(get(push) == get(pop) + get(steal),
  91          "push=" SIZE_FORMAT " pop=" SIZE_FORMAT " steal=" SIZE_FORMAT,
  92          get(push), get(pop), get(steal));
  93   assert(get(pop_slow) <= get(pop),
  94          "pop_slow=" SIZE_FORMAT " pop=" SIZE_FORMAT,
  95          get(pop_slow), get(pop));
  96   assert(get(steal) <= get(steal_attempt),
  97          "steal=" SIZE_FORMAT " steal_attempt=" SIZE_FORMAT,
  98          get(steal), get(steal_attempt));
  99   assert(get(overflow) == 0 || get(push) != 0,
 100          "overflow=" SIZE_FORMAT " push=" SIZE_FORMAT,
 101          get(overflow), get(push));
 102   assert(get(overflow_max_len) == 0 || get(overflow) != 0,
 103          "overflow_max_len=" SIZE_FORMAT " overflow=" SIZE_FORMAT,
 104          get(overflow_max_len), get(overflow));
 105 }
 106 #endif // ASSERT
 107 #endif // TASKQUEUE_STATS
 108 
 109 #ifdef ASSERT
 110 bool ObjArrayTask::is_valid() const {
 111   return _obj != NULL && _obj->is_objArray() && _index >= 0 &&
 112       _index < objArrayOop(_obj)->length();
 113 }
 114 #endif // ASSERT