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.
5 #ifndef BASE_WIN_SAMPLING_PROFILER_H_
6 #define BASE_WIN_SAMPLING_PROFILER_H_
10 #include "base/basictypes.h"
11 #include "base/time.h"
12 #include "base/win/scoped_handle.h"
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
{
24 // Create an uninitialized sampling profiler.
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
,
39 size_t log2_bucket_size
);
41 // Start this profiler, which must be initialized and not started.
43 // Stop this profiler, which must be started.
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
);
52 bool is_started() const { return is_started_
; }
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
58 const std::vector
<ULONG
>& buckets() const { return buckets_
; }
61 // Handle to the corresponding kernel object.
62 ScopedHandle profile_handle_
;
63 // True iff this profiler is started.
65 std::vector
<ULONG
> buckets_
;
67 DISALLOW_COPY_AND_ASSIGN(SamplingProfiler
);
73 #endif // BASE_WIN_SAMPLING_PROFILER_H_