1 /*
   2  * Copyright (c) 2001, 2010, 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 #ifndef SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPTHREAD_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPTHREAD_HPP
  27 
  28 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.hpp"
  29 #include "gc_implementation/shared/concurrentGCThread.hpp"
  30 #ifdef TARGET_OS_FAMILY_linux
  31 # include "thread_linux.inline.hpp"
  32 #endif
  33 #ifdef TARGET_OS_FAMILY_solaris
  34 # include "thread_solaris.inline.hpp"
  35 #endif
  36 #ifdef TARGET_OS_FAMILY_windows
  37 # include "thread_windows.inline.hpp"
  38 #endif
  39 
  40 class ConcurrentMarkSweepGeneration;
  41 class CMSCollector;
  42 
  43 // The Concurrent Mark Sweep GC Thread (could be several in the future).
  44 class ConcurrentMarkSweepThread: public ConcurrentGCThread {
  45   friend class VMStructs;
  46   friend class ConcurrentMarkSweepGeneration;   // XXX should remove friendship
  47   friend class CMSCollector;
  48  public:
  49   virtual void run();
  50 
  51  private:
  52   static ConcurrentMarkSweepThread*     _cmst;
  53   static CMSCollector*                  _collector;
  54   static SurrogateLockerThread*         _slt;
  55   static SurrogateLockerThread::SLT_msg_type _sltBuffer;
  56   static Monitor*                       _sltMonitor;
  57 
  58   ConcurrentMarkSweepThread*            _next;
  59 
  60   static bool _should_terminate;
  61 
  62   enum CMS_flag_type {
  63     CMS_nil             = NoBits,
  64     CMS_cms_wants_token = nth_bit(0),
  65     CMS_cms_has_token   = nth_bit(1),
  66     CMS_vm_wants_token  = nth_bit(2),
  67     CMS_vm_has_token    = nth_bit(3)
  68   };
  69 
  70   static int _CMS_flag;
  71 
  72   static bool CMS_flag_is_set(int b)        { return (_CMS_flag & b) != 0;   }
  73   static bool set_CMS_flag(int b)           { return (_CMS_flag |= b) != 0;  }
  74   static bool clear_CMS_flag(int b)         { return (_CMS_flag &= ~b) != 0; }
  75   void sleepBeforeNextCycle();
  76 
  77   // CMS thread should yield for a young gen collection, direct allocation,
  78   // and iCMS activity.
  79   static char _pad_1[64 - sizeof(jint)];    // prevent cache-line sharing
  80   static volatile jint _pending_yields;
  81   static volatile jint _pending_decrements; // decrements to _pending_yields
  82   static char _pad_2[64 - sizeof(jint)];    // prevent cache-line sharing
  83 
  84   // Tracing messages, enabled by CMSTraceThreadState.
  85   static inline void trace_state(const char* desc);
  86 
  87   static volatile bool _icms_enabled;   // iCMS enabled?
  88   static volatile bool _should_run;     // iCMS may run
  89   static volatile bool _should_stop;    // iCMS should stop
  90 
  91   // debugging
  92   void verify_ok_to_terminate() const PRODUCT_RETURN;
  93 
  94  public:
  95   // Constructor
  96   ConcurrentMarkSweepThread(CMSCollector* collector);
  97 
  98   static void makeSurrogateLockerThread(TRAPS);
  99   static SurrogateLockerThread* slt() { return _slt; }
 100 
 101   // Tester
 102   bool is_ConcurrentGC_thread() const { return true;       }
 103 
 104   static void threads_do(ThreadClosure* tc);
 105 
 106   // Printing
 107   void print_on(outputStream* st) const;
 108   void print() const                                  { print_on(tty); }
 109   static void print_all_on(outputStream* st);
 110   static void print_all()                             { print_all_on(tty); }
 111 
 112   // Returns the CMS Thread
 113   static ConcurrentMarkSweepThread* cmst()    { return _cmst; }
 114   static CMSCollector*         collector()    { return _collector;  }
 115 
 116   // Create and start the CMS Thread, or stop it on shutdown
 117   static ConcurrentMarkSweepThread* start(CMSCollector* collector);
 118   static void stop();
 119   static bool should_terminate() { return _should_terminate; }
 120 
 121   // Synchronization using CMS token
 122   static void synchronize(bool is_cms_thread);
 123   static void desynchronize(bool is_cms_thread);
 124   static bool vm_thread_has_cms_token() {
 125     return CMS_flag_is_set(CMS_vm_has_token);
 126   }
 127   static bool cms_thread_has_cms_token() {
 128     return CMS_flag_is_set(CMS_cms_has_token);
 129   }
 130   static bool vm_thread_wants_cms_token() {
 131     return CMS_flag_is_set(CMS_vm_wants_token);
 132   }
 133   static bool cms_thread_wants_cms_token() {
 134     return CMS_flag_is_set(CMS_cms_wants_token);
 135   }
 136 
 137   // Wait on CMS lock until the next synchronous GC
 138   // or given timeout, whichever is earlier.
 139   void    wait_on_cms_lock(long t); // milliseconds
 140 
 141   // The CMS thread will yield during the work portion of it's cycle
 142   // only when requested to.  Both synchronous and asychronous requests
 143   // are provided.  A synchronous request is used for young gen
 144   // collections and direct allocations.  The requesting thread increments
 145   // pending_yields at the beginning of an operation, and decrements it when
 146   // the operation is completed.  The CMS thread yields when pending_yields
 147   // is positive.  An asynchronous request is used by iCMS in the stop_icms()
 148   // operation. A single yield satisfies the outstanding asynch yield requests.
 149   // The requesting thread increments both pending_yields and pending_decrements.
 150   // After yielding, the CMS thread decrements both by the amount in
 151   // pending_decrements.
 152   // Note that, while "_pending_yields >= _pending_decrements" is an invariant,
 153   // we cannot easily test that invariant, since the counters are manipulated via
 154   // atomic instructions without explicit locking and we cannot read
 155   // the two counters atomically together: one suggestion is to
 156   // use (for example) 16-bit counters so as to be able to read the
 157   // two counters atomically even on 32-bit platforms. Notice that
 158   // the second assert in acknowledge_yield_request() does indeed
 159   // check a form of the above invariant, albeit indirectly.
 160 
 161   static void increment_pending_yields()   {
 162     Atomic::inc(&_pending_yields);
 163     assert(_pending_yields >= 0, "can't be negative");
 164   }
 165   static void decrement_pending_yields()   {
 166     Atomic::dec(&_pending_yields);
 167     assert(_pending_yields >= 0, "can't be negative");
 168   }
 169   static void asynchronous_yield_request() {
 170     increment_pending_yields();
 171     Atomic::inc(&_pending_decrements);
 172     assert(_pending_decrements >= 0, "can't be negative");
 173   }
 174   static void acknowledge_yield_request() {
 175     jint decrement = _pending_decrements;
 176     if (decrement > 0) {
 177       // Order important to preserve: _pending_yields >= _pending_decrements
 178       Atomic::add(-decrement, &_pending_decrements);
 179       Atomic::add(-decrement, &_pending_yields);
 180       assert(_pending_decrements >= 0, "can't be negative");
 181       assert(_pending_yields >= 0, "can't be negative");
 182     }
 183   }
 184   static bool should_yield()   { return _pending_yields > 0; }
 185 
 186   // CMS incremental mode.
 187   static void start_icms(); // notify thread to start a quantum of work
 188   static void stop_icms();  // request thread to stop working
 189   void icms_wait();         // if asked to stop, wait until notified to start
 190 
 191   // Incremental mode is enabled globally by the flag CMSIncrementalMode.  It
 192   // must also be enabled/disabled dynamically to allow foreground collections.
 193   static inline void enable_icms()              { _icms_enabled = true; }
 194   static inline void disable_icms()             { _icms_enabled = false; }
 195   static inline void set_icms_enabled(bool val) { _icms_enabled = val; }
 196   static inline bool icms_enabled()             { return _icms_enabled; }
 197 };
 198 
 199 inline void ConcurrentMarkSweepThread::trace_state(const char* desc) {
 200   if (CMSTraceThreadState) {
 201     char buf[128];
 202     TimeStamp& ts = gclog_or_tty->time_stamp();
 203     if (!ts.is_updated()) {
 204       ts.update();
 205     }
 206     jio_snprintf(buf, sizeof(buf), " [%.3f:  CMSThread %s] ",
 207                  ts.seconds(), desc);
 208     buf[sizeof(buf) - 1] = '\0';
 209     gclog_or_tty->print(buf);
 210   }
 211 }
 212 
 213 // For scoped increment/decrement of yield requests
 214 class CMSSynchronousYieldRequest: public StackObj {
 215  public:
 216   CMSSynchronousYieldRequest() {
 217     ConcurrentMarkSweepThread::increment_pending_yields();
 218   }
 219   ~CMSSynchronousYieldRequest() {
 220     ConcurrentMarkSweepThread::decrement_pending_yields();
 221   }
 222 };
 223 
 224 // Used to emit a warning in case of unexpectedly excessive
 225 // looping (in "apparently endless loops") in CMS code.
 226 class CMSLoopCountWarn: public StackObj {
 227  private:
 228   const char* _src;
 229   const char* _msg;
 230   const intx  _threshold;
 231   intx        _ticks;
 232 
 233  public:
 234   inline CMSLoopCountWarn(const char* src, const char* msg,
 235                           const intx threshold) :
 236     _src(src), _msg(msg), _threshold(threshold), _ticks(0) { }
 237 
 238   inline void tick() {
 239     _ticks++;
 240     if (CMSLoopWarn && _ticks % _threshold == 0) {
 241       warning("%s has looped %d times %s", _src, _ticks, _msg);
 242     }
 243   }
 244 };
 245 
 246 #endif // SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPTHREAD_HPP