src/share/vm/utilities/taskqueue.cpp

Sat, 18 May 2013 20:41:01 -0700

author
iklam
date
Sat, 18 May 2013 20:41:01 -0700
changeset 5144
a5d6f0c3585f
parent 4465
203f64878aab
child 6680
78bbf4d43a14
permissions
-rw-r--r--

8014262: PrintStringTableStatistics should include more footprint info
Summary: Added info for the string/symbol objects and the hash entries
Reviewed-by: coleenp, rbackman

     1 /*
     2  * Copyright (c) 2001, 2013, 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  */
    25 #include "precompiled.hpp"
    26 #include "oops/oop.inline.hpp"
    27 #include "runtime/os.hpp"
    28 #include "runtime/thread.inline.hpp"
    29 #include "utilities/debug.hpp"
    30 #include "utilities/stack.inline.hpp"
    31 #include "utilities/taskqueue.hpp"
    33 #ifdef TRACESPINNING
    34 uint ParallelTaskTerminator::_total_yields = 0;
    35 uint ParallelTaskTerminator::_total_spins = 0;
    36 uint ParallelTaskTerminator::_total_peeks = 0;
    37 #endif
    39 #if TASKQUEUE_STATS
    40 const char * const TaskQueueStats::_names[last_stat_id] = {
    41   "qpush", "qpop", "qpop-s", "qattempt", "qsteal", "opush", "omax"
    42 };
    44 TaskQueueStats & TaskQueueStats::operator +=(const TaskQueueStats & addend)
    45 {
    46   for (unsigned int i = 0; i < last_stat_id; ++i) {
    47     _stats[i] += addend._stats[i];
    48   }
    49   return *this;
    50 }
    52 void TaskQueueStats::print_header(unsigned int line, outputStream* const stream,
    53                                   unsigned int width)
    54 {
    55   // Use a width w: 1 <= w <= max_width
    56   const unsigned int max_width = 40;
    57   const unsigned int w = MAX2(MIN2(width, max_width), 1U);
    59   if (line == 0) { // spaces equal in width to the header
    60     const unsigned int hdr_width = w * last_stat_id + last_stat_id - 1;
    61     stream->print("%*s", hdr_width, " ");
    62   } else if (line == 1) { // labels
    63     stream->print("%*s", w, _names[0]);
    64     for (unsigned int i = 1; i < last_stat_id; ++i) {
    65       stream->print(" %*s", w, _names[i]);
    66     }
    67   } else if (line == 2) { // dashed lines
    68     char dashes[max_width + 1];
    69     memset(dashes, '-', w);
    70     dashes[w] = '\0';
    71     stream->print("%s", dashes);
    72     for (unsigned int i = 1; i < last_stat_id; ++i) {
    73       stream->print(" %s", dashes);
    74     }
    75   }
    76 }
    78 void TaskQueueStats::print(outputStream* stream, unsigned int width) const
    79 {
    80   #define FMT SIZE_FORMAT_W(*)
    81   stream->print(FMT, width, _stats[0]);
    82   for (unsigned int i = 1; i < last_stat_id; ++i) {
    83     stream->print(" " FMT, width, _stats[i]);
    84   }
    85   #undef FMT
    86 }
    88 #ifdef ASSERT
    89 // Invariants which should hold after a TaskQueue has been emptied and is
    90 // quiescent; they do not hold at arbitrary times.
    91 void TaskQueueStats::verify() const
    92 {
    93   assert(get(push) == get(pop) + get(steal),
    94          err_msg("push=" SIZE_FORMAT " pop=" SIZE_FORMAT " steal=" SIZE_FORMAT,
    95                  get(push), get(pop), get(steal)));
    96   assert(get(pop_slow) <= get(pop),
    97          err_msg("pop_slow=" SIZE_FORMAT " pop=" SIZE_FORMAT,
    98                  get(pop_slow), get(pop)));
    99   assert(get(steal) <= get(steal_attempt),
   100          err_msg("steal=" SIZE_FORMAT " steal_attempt=" SIZE_FORMAT,
   101                  get(steal), get(steal_attempt)));
   102   assert(get(overflow) == 0 || get(push) != 0,
   103          err_msg("overflow=" SIZE_FORMAT " push=" SIZE_FORMAT,
   104                  get(overflow), get(push)));
   105   assert(get(overflow_max_len) == 0 || get(overflow) != 0,
   106          err_msg("overflow_max_len=" SIZE_FORMAT " overflow=" SIZE_FORMAT,
   107                  get(overflow_max_len), get(overflow)));
   108 }
   109 #endif // ASSERT
   110 #endif // TASKQUEUE_STATS
   112 int TaskQueueSetSuper::randomParkAndMiller(int *seed0) {
   113   const int a =      16807;
   114   const int m = 2147483647;
   115   const int q =     127773;  /* m div a */
   116   const int r =       2836;  /* m mod a */
   117   assert(sizeof(int) == 4, "I think this relies on that");
   118   int seed = *seed0;
   119   int hi   = seed / q;
   120   int lo   = seed % q;
   121   int test = a * lo - r * hi;
   122   if (test > 0)
   123     seed = test;
   124   else
   125     seed = test + m;
   126   *seed0 = seed;
   127   return seed;
   128 }
   130 ParallelTaskTerminator::
   131 ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set) :
   132   _n_threads(n_threads),
   133   _queue_set(queue_set),
   134   _offered_termination(0) {}
   136 bool ParallelTaskTerminator::peek_in_queue_set() {
   137   return _queue_set->peek();
   138 }
   140 void ParallelTaskTerminator::yield() {
   141   assert(_offered_termination <= _n_threads, "Invariant");
   142   os::yield();
   143 }
   145 void ParallelTaskTerminator::sleep(uint millis) {
   146   assert(_offered_termination <= _n_threads, "Invariant");
   147   os::sleep(Thread::current(), millis, false);
   148 }
   150 bool
   151 ParallelTaskTerminator::offer_termination(TerminatorTerminator* terminator) {
   152   assert(_n_threads > 0, "Initialization is incorrect");
   153   assert(_offered_termination < _n_threads, "Invariant");
   154   Atomic::inc(&_offered_termination);
   156   uint yield_count = 0;
   157   // Number of hard spin loops done since last yield
   158   uint hard_spin_count = 0;
   159   // Number of iterations in the hard spin loop.
   160   uint hard_spin_limit = WorkStealingHardSpins;
   162   // If WorkStealingSpinToYieldRatio is 0, no hard spinning is done.
   163   // If it is greater than 0, then start with a small number
   164   // of spins and increase number with each turn at spinning until
   165   // the count of hard spins exceeds WorkStealingSpinToYieldRatio.
   166   // Then do a yield() call and start spinning afresh.
   167   if (WorkStealingSpinToYieldRatio > 0) {
   168     hard_spin_limit = WorkStealingHardSpins >> WorkStealingSpinToYieldRatio;
   169     hard_spin_limit = MAX2(hard_spin_limit, 1U);
   170   }
   171   // Remember the initial spin limit.
   172   uint hard_spin_start = hard_spin_limit;
   174   // Loop waiting for all threads to offer termination or
   175   // more work.
   176   while (true) {
   177     assert(_offered_termination <= _n_threads, "Invariant");
   178     // Are all threads offering termination?
   179     if (_offered_termination == _n_threads) {
   180       return true;
   181     } else {
   182       // Look for more work.
   183       // Periodically sleep() instead of yield() to give threads
   184       // waiting on the cores the chance to grab this code
   185       if (yield_count <= WorkStealingYieldsBeforeSleep) {
   186         // Do a yield or hardspin.  For purposes of deciding whether
   187         // to sleep, count this as a yield.
   188         yield_count++;
   190         // Periodically call yield() instead spinning
   191         // After WorkStealingSpinToYieldRatio spins, do a yield() call
   192         // and reset the counts and starting limit.
   193         if (hard_spin_count > WorkStealingSpinToYieldRatio) {
   194           yield();
   195           hard_spin_count = 0;
   196           hard_spin_limit = hard_spin_start;
   197 #ifdef TRACESPINNING
   198           _total_yields++;
   199 #endif
   200         } else {
   201           // Hard spin this time
   202           // Increase the hard spinning period but only up to a limit.
   203           hard_spin_limit = MIN2(2*hard_spin_limit,
   204                                  (uint) WorkStealingHardSpins);
   205           for (uint j = 0; j < hard_spin_limit; j++) {
   206             SpinPause();
   207           }
   208           hard_spin_count++;
   209 #ifdef TRACESPINNING
   210           _total_spins++;
   211 #endif
   212         }
   213       } else {
   214         if (PrintGCDetails && Verbose) {
   215          gclog_or_tty->print_cr("ParallelTaskTerminator::offer_termination() "
   216            "thread %d sleeps after %d yields",
   217            Thread::current(), yield_count);
   218         }
   219         yield_count = 0;
   220         // A sleep will cause this processor to seek work on another processor's
   221         // runqueue, if it has nothing else to run (as opposed to the yield
   222         // which may only move the thread to the end of the this processor's
   223         // runqueue).
   224         sleep(WorkStealingSleepMillis);
   225       }
   227 #ifdef TRACESPINNING
   228       _total_peeks++;
   229 #endif
   230       if (peek_in_queue_set() ||
   231           (terminator != NULL && terminator->should_exit_termination())) {
   232         Atomic::dec(&_offered_termination);
   233         assert(_offered_termination < _n_threads, "Invariant");
   234         return false;
   235       }
   236     }
   237   }
   238 }
   240 #ifdef TRACESPINNING
   241 void ParallelTaskTerminator::print_termination_counts() {
   242   gclog_or_tty->print_cr("ParallelTaskTerminator Total yields: " UINT32_FORMAT
   243     " Total spins: " UINT32_FORMAT " Total peeks: " UINT32_FORMAT,
   244     total_yields(),
   245     total_spins(),
   246     total_peeks());
   247 }
   248 #endif
   250 void ParallelTaskTerminator::reset_for_reuse() {
   251   if (_offered_termination != 0) {
   252     assert(_offered_termination == _n_threads,
   253            "Terminator may still be in use");
   254     _offered_termination = 0;
   255   }
   256 }
   258 #ifdef ASSERT
   259 bool ObjArrayTask::is_valid() const {
   260   return _obj != NULL && _obj->is_objArray() && _index > 0 &&
   261     _index < objArrayOop(_obj)->length();
   262 }
   263 #endif // ASSERT
   265 void ParallelTaskTerminator::reset_for_reuse(int n_threads) {
   266   reset_for_reuse();
   267   _n_threads = n_threads;
   268 }

mercurial