1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_RUNTIME_JAVAHEAPPROF_JAVAHEAPSAMPLER_H_
18 #define ART_RUNTIME_JAVAHEAPPROF_JAVAHEAPSAMPLER_H_
19 
20 #include <random>
21 #include "base/locks.h"
22 #include "base/mutex.h"
23 #include "mirror/object.h"
24 
25 namespace art {
26 
27 class HeapSampler {
28  public:
HeapSampler()29   HeapSampler() : rng_(/*seed=*/std::minstd_rand::default_seed),
30                   geo_dist_(1.0 / /*expected value=4KB*/ 4096),
31                   geo_dist_rng_lock_("Heap Sampler RNG Geometric Dist lock",
32                                      art::LockLevel::kGenericBottomLock) {}
33 
34   // Set the bytes until sample.
SetBytesUntilSample(size_t bytes)35   void SetBytesUntilSample(size_t bytes) {
36     *GetBytesUntilSample() = bytes;
37   }
38   // Get the bytes until sample.
GetBytesUntilSample()39   size_t* GetBytesUntilSample() {
40     // Initialization should happen only once the first time the function is called.
41     // However there will always be a slot allocated for it at thread creation.
42     thread_local size_t bytes_until_sample = 0;
43     return &bytes_until_sample;
44   }
SetHeapID(uint32_t heap_id)45   void SetHeapID(uint32_t heap_id) {
46     perfetto_heap_id_ = heap_id;
47   }
EnableHeapSampler()48   void EnableHeapSampler() {
49     enabled_.store(true, std::memory_order_release);
50   }
DisableHeapSampler()51   void DisableHeapSampler() {
52     enabled_.store(false, std::memory_order_release);
53   }
54   // Report a sample to Perfetto.
55   void ReportSample(art::mirror::Object* obj, size_t allocation_size);
56   // Check whether we should take a sample or not at this allocation, and return the
57   // number of bytes from current pos to the next sample to use in the expand Tlab
58   // calculation.
59   // Update state of both take_sample and temp_bytes_until_sample.
60   // tlab_used = pos - start
61   // Note: we do not update bytes until sample here. It will be saved after the allocation
62   // happens. This function can be called before the actual allocation happens.
63   size_t GetSampleOffset(size_t alloc_size,
64                          size_t tlab_used,
65                          bool* take_sample,
66                          size_t* temp_bytes_until_sample) REQUIRES(!geo_dist_rng_lock_);
67   // Adjust the sample offset value with the adjustment usually (pos - start)
68   // of new Tlab after Reset.
69   void AdjustSampleOffset(size_t adjustment);
70   // Is heap sampler enabled?
71   bool IsEnabled();
72   // Set the sampling interval.
73   void SetSamplingInterval(int sampling_interval) REQUIRES(!geo_dist_rng_lock_);
74   // Return the sampling interval.
75   int GetSamplingInterval();
76 
77  private:
78   size_t NextGeoDistRandSample() REQUIRES(!geo_dist_rng_lock_);
79   // Choose, save, and return the number of bytes until the next sample,
80   // possibly decreasing sample intervals by sample_adj_bytes.
81   size_t PickAndAdjustNextSample(size_t sample_adj_bytes = 0) REQUIRES(!geo_dist_rng_lock_);
82 
83   std::atomic<bool> enabled_;
84   // Default sampling interval is 4kb.
85   // Writes guarded by geo_dist_rng_lock_.
86   std::atomic<int> p_sampling_interval_{4 * 1024};
87   uint32_t perfetto_heap_id_ = 0;
88   // std random number generator.
89   std::minstd_rand rng_ GUARDED_BY(geo_dist_rng_lock_);  // Holds the state
90   // std geometric distribution
91   std::geometric_distribution</*result_type=*/size_t> geo_dist_ GUARDED_BY(geo_dist_rng_lock_);
92   // Multiple threads can access the geometric distribution and the random number
93   // generator concurrently and thus geo_dist_rng_lock_ is used for thread safety.
94   art::Mutex geo_dist_rng_lock_;
95 };
96 
97 }  // namespace art
98 
99 #endif  // ART_RUNTIME_JAVAHEAPPROF_JAVAHEAPSAMPLER_H_
100