1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_WIN_SAMPLING_PROFILER_H_ 6 #define BASE_WIN_SAMPLING_PROFILER_H_ 7 8 #include <vector> 9 10 #include "base/basictypes.h" 11 #include "base/time/time.h" 12 #include "base/win/scoped_handle.h" 13 14 namespace base { 15 namespace win { 16 17 // This class exposes the functionality of Window's built-in sampling profiler. 18 // Each profiler instance covers a range of memory, and while the profiler is 19 // running, its buckets will count the number of times the instruction counter 20 // lands in the associated range of memory on a sample. 21 // The sampling interval is settable, but the setting is system-wide. 22 class BASE_EXPORT SamplingProfiler { 23 public: 24 // Create an uninitialized sampling profiler. 25 SamplingProfiler(); 26 ~SamplingProfiler(); 27 28 // Initializes the profiler to cover the memory range |start| through 29 // |start| + |size|, in the process |process_handle| with bucket size 30 // |2^log2_bucket_size|, |log2_bucket_size| must be in the range 2-31, 31 // for bucket sizes of 4 bytes to 2 gigabytes. 32 // The process handle must grant at least PROCESS_QUERY_INFORMATION. 33 // The memory range should be exectuable code, like e.g. the text segment 34 // of an exectuable (whether DLL or EXE). 35 // Returns true on success. 36 bool Initialize(HANDLE process_handle, 37 void* start, 38 size_t size, 39 size_t log2_bucket_size); 40 41 // Start this profiler, which must be initialized and not started. 42 bool Start(); 43 // Stop this profiler, which must be started. 44 bool Stop(); 45 46 // Get and set the sampling interval. 47 // Note that this is a system-wide setting. 48 static bool SetSamplingInterval(base::TimeDelta sampling_interval); 49 static bool GetSamplingInterval(base::TimeDelta* sampling_interval); 50 51 // Accessors. 52 bool is_started() const { return is_started_; } 53 54 // It is safe to read the counts in the sampling buckets at any time. 55 // Note however that there's no guarantee that you'll read consistent counts 56 // until the profiler has been stopped, as the counts may be updating on other 57 // CPU cores. 58 const std::vector<ULONG>& buckets() const { return buckets_; } 59 60 private: 61 // Handle to the corresponding kernel object. 62 ScopedHandle profile_handle_; 63 // True iff this profiler is started. 64 bool is_started_; 65 std::vector<ULONG> buckets_; 66 67 DISALLOW_COPY_AND_ASSIGN(SamplingProfiler); 68 }; 69 70 } // namespace win 71 } // namespace base 72 73 #endif // BASE_WIN_SAMPLING_PROFILER_H_ 74