iai_callgrind/client_requests/callgrind.rs
1// Copyright (C) 2003-2017 Josef Weidendorfer. All rights reserved.
2//
3// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions
5// are met:
6//
7// 1. Redistributions of source code must retain the above copyright notice, this list of conditions
8// and the following disclaimer.
9//
10// 2. The origin of this software must not be misrepresented; you must not claim that you wrote the
11// original software. If you use this software in a product, an acknowledgment in the product
12// documentation would be appreciated but is not required.
13//
14// 3. Altered source versions must be plainly marked as such, and must not be misrepresented as
15// being the original software.
16//
17// 4. The name of the author may not be used to endorse or promote products derived from this
18// software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS`` AND ANY EXPRESS
21// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
24// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31//
32// ----------------------------------------------------------------
33//
34// We're using a lot of the original documentation from the `callgrind.h` header file with some
35// small adjustments, so above is the original license from `callgrind.h` file.
36//
37// This file is distributed under the same License as the rest of `iai-callgrind`.
38//
39// ----------------------------------------------------------------
40//
41//! All client requests from the `callgrind.h` header file
42//!
43//! See also [Callgrind specific client
44//! requests](https://valgrind.org/docs/manual/cl-manual.html#cl-manual.clientrequests)
45
46use std::ffi::CStr;
47
48use super::{bindings, fatal_error, valgrind_do_client_request_stmt};
49
50/// Dump current state of cost centers, and zero them afterward
51///
52/// Force generation of a profile dump at specified position in code, for the current thread only.
53/// Written counters will be reset to zero.
54#[inline(always)]
55pub fn dump_stats() {
56 do_client_request!(
57 "callgrind::dump_stats",
58 bindings::IC_CallgrindClientRequest::IC_DUMP_STATS,
59 0,
60 0,
61 0,
62 0,
63 0
64 );
65}
66
67/// Dump current state of cost centers, and zero them afterward stating a reason
68///
69/// Same as [`dump_stats`], but allows to specify a string to be able to distinguish profile dumps.
70///
71/// The argument is appended to a string stating the reason which triggered the dump. This string is
72/// written as a description field into the profile data dump.
73#[inline(always)]
74pub fn dump_stats_at<T>(c_str: T)
75where
76 T: AsRef<CStr>,
77{
78 do_client_request!(
79 "callgrind::dump_stats_at",
80 bindings::IC_CallgrindClientRequest::IC_DUMP_STATS_AT,
81 c_str.as_ref().as_ptr() as usize,
82 0,
83 0,
84 0,
85 0
86 );
87}
88
89/// Zero cost centers
90///
91/// Reset the profile counters for the current thread to zero.
92#[inline(always)]
93pub fn zero_stats() {
94 do_client_request!(
95 "callgrind::zero_stats",
96 bindings::IC_CallgrindClientRequest::IC_ZERO_STATS,
97 0,
98 0,
99 0,
100 0,
101 0
102 );
103}
104
105/// Toggles collection state.
106///
107/// The collection state specifies whether the happening of events should be noted or if they are to
108/// be ignored. Events are noted by increment of counters in a cost center
109///
110/// This allows to ignore events with regard to profile counters. See also valgrind command line
111/// options `--collect-atstart` and `--toggle-collect`.
112#[inline(always)]
113pub fn toggle_collect() {
114 do_client_request!(
115 "callgrind::toggle_collect",
116 bindings::IC_CallgrindClientRequest::IC_TOGGLE_COLLECT,
117 0,
118 0,
119 0,
120 0,
121 0
122 );
123}
124
125/// Start full callgrind instrumentation if not already switched on
126///
127/// When cache simulation is done, it will flush the simulated cache; this will lead to an
128/// artificial cache warmup phase afterward with cache misses which would not have happened in
129/// reality.
130#[inline(always)]
131pub fn start_instrumentation() {
132 do_client_request!(
133 "callgrind::start_instrumentation",
134 bindings::IC_CallgrindClientRequest::IC_START_INSTRUMENTATION,
135 0,
136 0,
137 0,
138 0,
139 0
140 );
141}
142
143/// Stop full callgrind instrumentation if not already switched off
144///
145/// This flushes Valgrind's translation cache, and does no additional instrumentation afterward,
146/// which effectively will run at the same speed as the "none" tool (i.e. at minimal slowdown). Use
147/// this to bypass Callgrind aggregation for uninteresting code parts. To start Callgrind in this
148/// mode to ignore the setup phase, use the valgrind command line option `--instr-atstart=no`.
149#[inline(always)]
150pub fn stop_instrumentation() {
151 do_client_request!(
152 "callgrind::stop_instrumentation",
153 bindings::IC_CallgrindClientRequest::IC_STOP_INSTRUMENTATION,
154 0,
155 0,
156 0,
157 0,
158 0
159 );
160}