ysr@777: /* ysr@777: * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved. ysr@777: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ysr@777: * ysr@777: * This code is free software; you can redistribute it and/or modify it ysr@777: * under the terms of the GNU General Public License version 2 only, as ysr@777: * published by the Free Software Foundation. ysr@777: * ysr@777: * This code is distributed in the hope that it will be useful, but WITHOUT ysr@777: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ysr@777: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ysr@777: * version 2 for more details (a copy is included in the LICENSE file that ysr@777: * accompanied this code). ysr@777: * ysr@777: * You should have received a copy of the GNU General Public License version ysr@777: * 2 along with this work; if not, write to the Free Software Foundation, ysr@777: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ysr@777: * ysr@777: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, ysr@777: * CA 95054 USA or visit www.sun.com if you need additional information or ysr@777: * have any questions. ysr@777: * ysr@777: */ ysr@777: ysr@777: // COTracker keeps track of the concurrent overhead of a GC thread. ysr@777: ysr@777: // A thread that needs to be tracked must, itself, start up its ysr@777: // tracker with the start() method and then call the update() method ysr@777: // at regular intervals. What the tracker does is to calculate the ysr@777: // concurrent overhead of a process at a given update period. The ysr@777: // tracker starts and when is detects that it has exceeded the given ysr@777: // period, it calculates the duration of the period in wall-clock time ysr@777: // and the duration of the period in vtime (i.e. how much time the ysr@777: // concurrent processes really took up during this period). The ratio ysr@777: // of the latter over the former is the concurrent overhead of that ysr@777: // process for that period over a single CPU. This overhead is stored ysr@777: // on the tracker, "timestamped" with the wall-clock time of the end ysr@777: // of the period. When the concurrent overhead of this process needs ysr@777: // to be queried, this last "reading" provides a good approximation ysr@777: // (we assume that the concurrent overhead of a particular thread ysr@777: // stays largely constant over time). The timestamp is necessary to ysr@777: // detect when the process has stopped working and the recorded ysr@777: // reading hasn't been updated for some time. ysr@777: ysr@777: // Each concurrent GC thread is considered to be part of a "group" ysr@777: // (i.e. any available concurrent marking threads are part of the ysr@777: // "concurrent marking thread group"). A COTracker is associated with ysr@777: // a single group at construction-time. It's up to each collector to ysr@777: // decide how groups will be mapped to such an id (ids should start ysr@777: // from 0 and be consecutive; there's a hardcoded max group num ysr@777: // defined on the GCOverheadTracker class). The notion of a group has ysr@777: // been introduced to be able to identify how much overhead was ysr@777: // imposed by each group, instead of getting a single value that ysr@777: // covers all concurrent overhead. ysr@777: ysr@777: class COTracker { ysr@777: private: ysr@777: // It indicates whether this tracker is enabled or not. When the ysr@777: // tracker is disabled, then it returns 0.0 as the latest concurrent ysr@777: // overhead and several methods (reset, start, and update) are not ysr@777: // supposed to be called on it. This enabling / disabling facility ysr@777: // is really provided to make a bit more explicit in the code when a ysr@777: // particulary tracker of a processes that doesn't run all the time ysr@777: // (e.g. concurrent marking) is supposed to be used and not it's not. ysr@777: bool _enabled; ysr@777: ysr@777: // The ID of the group associated with this tracker. ysr@777: int _group; ysr@777: ysr@777: // The update period of the tracker. A new value for the concurrent ysr@777: // overhead of the associated process will be made at intervals no ysr@777: // smaller than this. ysr@777: double _update_period_sec; ysr@777: ysr@777: // The start times (both wall-block time and vtime) of the current ysr@777: // interval. ysr@777: double _period_start_time_sec; ysr@777: double _period_start_vtime_sec; ysr@777: ysr@777: // Number seq of the concurrent overhead readings within a period ysr@777: NumberSeq _conc_overhead_seq; ysr@777: ysr@777: // The latest reading of the concurrent overhead (over a single CPU) ysr@777: // imposed by the associated concurrent thread, made available at ysr@777: // the indicated wall-clock time. ysr@777: double _conc_overhead; ysr@777: double _time_stamp_sec; ysr@777: ysr@777: // The number of CPUs that the host machine has (for convenience ysr@777: // really, as we'd have to keep translating it into a double) ysr@777: static double _cpu_number; ysr@777: ysr@777: // Fields that keep a list of all trackers created. This is useful, ysr@777: // since it allows us to sum up the concurrent overhead without ysr@777: // having to write code for a specific collector to broadcast a ysr@777: // request to all its concurrent processes. ysr@777: COTracker* _next; ysr@777: static COTracker* _head; ysr@777: ysr@777: // It indicates that a new period is starting by updating the ysr@777: // _period_start_time_sec and _period_start_vtime_sec fields. ysr@777: void resetPeriod(double now_sec, double vnow_sec); ysr@777: // It updates the latest concurrent overhead reading, taken at a ysr@777: // given wall-clock time. ysr@777: void setConcOverhead(double time_stamp_sec, double conc_overhead); ysr@777: ysr@777: // It determines whether the time stamp of the latest concurrent ysr@777: // overhead reading is out of date or not. ysr@777: bool outOfDate(double now_sec) { ysr@777: // The latest reading is considered out of date, if it was taken ysr@777: // 1.2x the update period. ysr@777: return (now_sec - _time_stamp_sec) > 1.2 * _update_period_sec; ysr@777: } ysr@777: ysr@777: public: ysr@777: // The constructor which associates the tracker with a group ID. ysr@777: COTracker(int group); ysr@777: ysr@777: // Methods to enable / disable the tracker and query whether it is enabled. ysr@777: void enable() { _enabled = true; } ysr@777: void disable() { _enabled = false; } ysr@777: bool enabled() { return _enabled; } ysr@777: ysr@777: // It resets the tracker and sets concurrent overhead reading to be ysr@777: // the given parameter and the associated time stamp to be now. ysr@777: void reset(double starting_conc_overhead = 0.0); ysr@777: // The tracker starts tracking. IT should only be called from the ysr@777: // concurrent thread that is tracked by this tracker. ysr@777: void start(); ysr@777: // It updates the tracker and, if the current period is longer than ysr@777: // the update period, the concurrent overhead reading will be ysr@777: // updated. force_end being true indicates that it's the last call ysr@777: // to update() by this process before the tracker is disabled (the ysr@777: // tracker can be re-enabled later if necessary). It should only be ysr@777: // called from the concurrent thread that is tracked by this tracker ysr@777: // and while the thread has joined the STS. ysr@777: void update(bool force_end = false); ysr@777: // It adjusts the contents of the tracker to take into account a STW ysr@777: // pause. ysr@777: void updateForSTW(double start_sec, double end_sec); ysr@777: ysr@777: // It returns the last concurrent overhead reading over a single ysr@777: // CPU. If the reading is out of date, or the tracker is disabled, ysr@777: // it returns 0.0. ysr@777: double concCPUOverhead(double now_sec) { ysr@777: if (!_enabled || outOfDate(now_sec)) ysr@777: return 0.0; ysr@777: else ysr@777: return _conc_overhead; ysr@777: } ysr@777: ysr@777: // It returns the last concurrent overhead reading over all CPUs ysr@777: // that the host machine has. If the reading is out of date, or the ysr@777: // tracker is disabled, it returns 0.0. ysr@777: double concOverhead(double now_sec) { ysr@777: return concCPUOverhead(now_sec) / _cpu_number; ysr@777: } ysr@777: ysr@777: double predConcOverhead(); ysr@777: ysr@777: void resetPred(); ysr@777: ysr@777: // statics ysr@777: ysr@777: // It notifies all trackers about a STW pause. ysr@777: static void updateAllForSTW(double start_sec, double end_sec); ysr@777: ysr@777: // It returns the sum of the concurrent overhead readings of all ysr@777: // available (and enabled) trackers for the given time stamp. The ysr@777: // overhead is over all the CPUs of the host machine. ysr@777: ysr@777: static double totalConcOverhead(double now_sec); ysr@777: // Like the previous method, but it also sums up the overheads per ysr@777: // group number. The length of the co_per_group array must be at ysr@777: // least as large group_num ysr@777: static double totalConcOverhead(double now_sec, ysr@777: size_t group_num, ysr@777: double* co_per_group); ysr@777: ysr@777: static double totalPredConcOverhead(); ysr@777: };