1 /*
   2  * Copyright (c) 2017, Red Hat Inc. 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 "runtime/highLatencyKiller.hpp"
  27 #include "runtime/task.hpp"
  28 #include "runtime/safepoint.hpp"
  29 
  30 // PeriodicTasks do not participate in safepoint protocol, and therefore
  31 // can fire when application threads are stopped. This task samples the
  32 // time spent in a single safepoint, and aborts VM if timeout was reached.
  33 class HighLatencyKillerTask : public PeriodicTask {
  34 public:
  35   HighLatencyKillerTask(size_t interval_time) :
  36           PeriodicTask(interval_time), _last_safepoint(-1), _first_time(0), _last_time(0) {}
  37 
  38   jlong _first_time;
  39   jlong _last_time;
  40   int _last_safepoint;
  41 
  42   virtual void task() {
  43     if (SafepointSynchronize::is_synchronizing() ||
  44             SafepointSynchronize::is_at_safepoint()) {
  45       jlong ct = os::javaTimeMillis();
  46 
  47       // TODO: Make sure this operation is safe without acquiring Thread_lock
  48       int current = SafepointSynchronize::safepoint_counter();
  49       if (current == _last_safepoint) {
  50         _last_time = ct;
  51 
  52         size_t delay = (size_t)(_last_time - _first_time);
  53         if (delay > HighLatencyKillerTimeout) {
  54           // TODO: Print VM operation in progress
  55           report_fatal("", -1, "High Latency detected: " SIZE_FORMAT " ms (actual) > " SIZE_FORMAT " ms (threshold). Killing VM.",
  56                        delay, HighLatencyKillerTimeout);
  57         }
  58       } else {
  59         _first_time = ct;
  60         _last_time = ct;
  61         _last_safepoint = current;
  62       }
  63     }
  64   }
  65 };
  66 
  67 HighLatencyKiller::HighLatencyKiller() {
  68   // TODO: Handle HighLatencyKillerPeriod > HighLatencyKillerTimeout
  69   HighLatencyKillerTask* task = new HighLatencyKillerTask(HighLatencyKillerPeriod);
  70   task->enroll();
  71 }
  72 
  73 void HighLatencyKiller::create() {
  74   _killer = new HighLatencyKiller();
  75 };
  76 
  77 // TODO: Destructors and proper cleanup
  78 
  79 HighLatencyKiller *HighLatencyKiller::_killer = NULL;
  80