Path: blob/master/src/hotspot/share/jfr/support/jfrAdaptiveSampler.hpp
41152 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2020, Datadog, 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#ifndef SHARE_JFR_SUPPORT_JFRADAPTIVESAMPLER_HPP26#define SHARE_JFR_SUPPORT_JFRADAPTIVESAMPLER_HPP2728#include "jfr/utilities/jfrAllocation.hpp"29#include "jfr/utilities/jfrRandom.hpp"3031/*32* The terminology is mostly from the domain of statistics:33*34* Population - a set of elements of interest.35* Sample - a subset of elements from a population selected by a defined procedure.36* Sample point - an element of a sample (sub)set.37* Sampling interval - the distance between which measurements are taken, also referred to as 'nth selection'38* Debt - an error term, signifying the deviation from a configured set point.39* Amortization - a projection or strategy to recover accumulated debt.40* Window - as in time window or time frame. The sampler sees the evolution of the system in time slices, i.e. in windows.41* Rotate - the process of retiring an expired window and installing a new window with updated parameters.42*43* The adaptive sampler will guarantee a maximum number of sample points selected from a populuation44* during a certain time interval. It is using fixed size time windows and adjusts the sampling interval for the next45* window based on what it learned in the past. Each window has a set point, which is the target number of sample points46* to select. The sampler keeps a cumulative error term, called 'accumulated debt', which is a measure47* for how much the sampler is deviating from the set point over time. The maximum number of sample points selected48* during an individual window is the set point + the accumulated debt.49* The 'accumulated debt' also works as a 'spike damper', smoothing out the extremes in a way that the overall50* target rate is obeyed without highly over- or under-sampled windows.51*52* Sample point selection is defined by a sampling interval, which gives instructions for selecting the 'nth' element53* in a population. Which 'nth' to select is a random variable from a geometric distribution, recalculated for each window.54*55* Each window is configured individually, by an instance of the JfrSamplerParams struct. On window expiration,56* but before switching in the next window, the sampler calls a subclass with the just expired window as an argument.57.* A subclass can inspect the window to study the history of the system and also get an overview of how the sampler58* is performing to help draw inferences. Based on what it learned, it can choose to let the sampler re-apply an updated59* set of parameters to the next, upcoming, window. This is a basic feedback control loop to be developed further,60* perhaps evolving more elaborate sampling schemes in the future.61*62* Using the JfrAdaptiveSampler, we can let a user specify at a high level, for example that he/she would like a63* maximum rate of n sample points per second. Naturally, lower rates will be reported if the system does not produce64* a population to sustain the requested rate, but n per second is respected as a maximum limit, hence it will never65* report a rate higher than n per second.66*67* One good use of the sampler is to employ it as a throttler, or regulator, to help shape large data sets into smaller,68* more managable subsets while still keeping the data somewhat representative.69*70*/7172struct JfrSamplerParams {73size_t sample_points_per_window; // The number of sample points to target per window.74size_t window_duration_ms;75size_t window_lookback_count; // The number of data points (windows) to include when calculating a moving average for the population size.76mutable bool reconfigure; // The sampler should issue a reconfiguration because some parameter changed.77};7879class JfrSamplerWindow : public JfrCHeapObj {80friend class JfrAdaptiveSampler;81private:82JfrSamplerParams _params;83volatile int64_t _end_ticks;84size_t _sampling_interval;85size_t _projected_population_size;86mutable volatile size_t _measured_population_size;8788JfrSamplerWindow();89void initialize(const JfrSamplerParams& params);90size_t max_sample_size() const;91bool is_expired(int64_t timestamp) const;92bool sample() const;93bool sample(int64_t timestamp, bool* is_expired) const;9495public:96size_t population_size() const;97size_t sample_size() const;98intptr_t debt() const;99intptr_t accumulated_debt() const;100const JfrSamplerParams& params() const {101return _params;102}103};104105class JfrAdaptiveSampler : public JfrCHeapObj {106private:107JfrPRNG _prng;108JfrSamplerWindow* _window_0;109JfrSamplerWindow* _window_1;110const JfrSamplerWindow* _active_window;111double _avg_population_size;112double _ewma_population_size_alpha;113size_t _acc_debt_carry_limit;114size_t _acc_debt_carry_count;115116void rotate_window(int64_t timestamp);117void rotate(const JfrSamplerWindow* expired);118const JfrSamplerWindow* active_window() const;119JfrSamplerWindow* next_window(const JfrSamplerWindow* expired) const;120void install(const JfrSamplerWindow* next);121122size_t amortize_debt(const JfrSamplerWindow* expired);123size_t derive_sampling_interval(double sample_size, const JfrSamplerWindow* expired);124size_t project_population_size(const JfrSamplerWindow* expired);125size_t project_sample_size(const JfrSamplerParams& params, const JfrSamplerWindow* expired);126JfrSamplerWindow* set_rate(const JfrSamplerParams& params, const JfrSamplerWindow* expired);127128void configure(const JfrSamplerParams& params);129const JfrSamplerWindow* configure(const JfrSamplerParams& params, const JfrSamplerWindow* expired);130131protected:132volatile int _lock;133JfrAdaptiveSampler();134virtual ~JfrAdaptiveSampler();135virtual bool initialize();136virtual const JfrSamplerParams& next_window_params(const JfrSamplerWindow* expired) = 0;137void reconfigure();138139public:140bool sample(int64_t timestamp = 0);141};142143/* GTEST support */144class JfrGTestFixedRateSampler : public JfrAdaptiveSampler {145private:146JfrSamplerParams _params;147double _sample_size_ewma;148public:149JfrGTestFixedRateSampler(size_t sample_points_per_window, size_t window_duration_ms, size_t lookback_count);150virtual bool initialize();151const JfrSamplerParams& next_window_params(const JfrSamplerWindow* expired);152};153154#endif // SHARE_JFR_SUPPORT_JFRADAPTIVESAMPLER_HPP155156157