1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#![allow(clippy::module_name_repetitions)]
use proc_macro::TokenStream;
use quote::quote;
use syn::parse::{Parse, ParseStream};
use syn::{parse_macro_input, ItemFn};
use syn::{Attribute, FnArg, Generics, ReturnType, Visibility, WhereClause};
/// Configuration for `profiled` attribute macro
#[allow(clippy::struct_excessive_bools)]
#[derive(Default)]
struct ProfileArgs {
// imp parameter removed - not needed
/// Flag for time profiling
time: bool,
/// Flag for memory summary profiling
mem_summary: bool,
/// Flag for detailed memory profiling
mem_detail: bool,
/// Flag for both time and memory profiling
both: bool,
/// Flag for using global profiling settings
global: bool,
/// Flag for creating profile clone for testing
test: bool,
}
impl Parse for ProfileArgs {
fn parse(input: ParseStream) -> syn::Result<Self> {
let mut args = Self::default();
// Handle empty case
if input.is_empty() {
args.global = true; // Default to global if no args specified
return Ok(args);
}
// Parse as a list of flags
let mut first = true;
while !input.is_empty() {
if !first {
let _: syn::Token![,] = input.parse()?;
}
first = false;
// Parse as flag
let flag: syn::Ident = input.parse()?;
match flag.to_string().as_str() {
"time" => args.time = true,
"mem_summary" => args.mem_summary = true,
"mem_detail" => args.mem_detail = true,
"both" => args.both = true,
"global" => args.global = true,
"test" => args.test = true,
_ => {
return Err(syn::Error::new(
flag.span(),
format!("unknown flag: {flag}"),
));
}
}
}
// If no profiling type was specified, default to global
if !args.time && !args.mem_summary && !args.mem_detail && !args.both && !args.global {
args.global = true;
}
Ok(args)
}
}
#[allow(clippy::needless_pass_by_value)]
pub fn profiled_impl(attr: TokenStream, item: TokenStream) -> TokenStream {
// Print the raw attribute TokenStream to help debug
// eprintln!("Raw attribute tokens: {attr}");
let args = match syn::parse::<ProfileArgs>(attr) {
Ok(args) => {
// Print parsed arguments for debugging
// eprintln!("Parsed attributes - time: {}, mem_summary: {}, mem_detail: {}, both: {}, global: {}, test: {}",
// args.time, args.mem_summary, args.mem_detail, args.both, args.global, args.test);
args
}
Err(e) => {
eprintln!("Error parsing profiled attributes: {e}");
// eprintln!("Raw attribute tokens: {attr}");
// Return the original function without any changes
return item;
}
};
let item_clone = item.clone();
let input = parse_macro_input!(item_clone as ItemFn);
let fn_name = &input.sig.ident;
let fn_name_str = fn_name.to_string();
if fn_name_str == "main" {
eprintln!("`main` function may only be profiled through #[enable_function] attribute - ignoring #[profiled] attribute");
return item;
}
let is_async = input.sig.asyncness.is_some();
// Print detailed flags for debugging
// eprintln!(
// "Profile type determination - both: {}, time: {}, mem_summary: {}, mem_detail: {}",
// args.both, args.time, args.mem_summary, args.mem_detail
// );
// Determine profile type based on flags
#[cfg(feature = "full_profiling")]
let profile_type = if args.both || (args.time && (args.mem_summary || args.mem_detail)) {
quote! { ::thag_profiler::ProfileType::Both }
} else if args.time {
quote! { ::thag_profiler::ProfileType::Time }
} else if args.mem_summary || args.mem_detail {
quote! { ::thag_profiler::ProfileType::Memory }
} else {
// Default to global
quote! { ::thag_profiler::get_global_profile_type() }
};
// When not using full_profiling, always use Time regardless of memory settings
#[cfg(not(feature = "full_profiling"))]
let profile_type = quote! { ::thag_profiler::ProfileType::Time };
// Determine if detailed memory profiling is enabled
let detailed_memory = args.mem_detail;
// Check if this is a test function by name or by explicit flag
let is_test_fn = args.test || fn_name_str.ends_with("_test");
#[cfg(not(feature = "full_profiling"))]
let profile_new = quote! {
::thag_profiler::Profile::new(None, Some(#fn_name_str), #profile_type, #is_async, #detailed_memory, file!(), None, None)
};
#[cfg(feature = "full_profiling")]
let profile_new = quote! {
::thag_profiler::safe_alloc! {
::thag_profiler::Profile::new(None, Some(#fn_name_str), #profile_type, #is_async, #detailed_memory, file!(), None, None)
}
};
#[cfg(not(feature = "full_profiling"))]
let profile_drop = quote! {
drop(profile);
};
#[cfg(feature = "full_profiling")]
let profile_drop = quote! {
::thag_profiler::safe_alloc! {
drop(profile);
};
};
#[cfg(not(feature = "full_profiling"))]
let import_mem_tracking = quote! {};
#[cfg(feature = "full_profiling")]
let import_mem_tracking = quote! {
use thag_profiler::mem_tracking::{self, compare_exchange_using_system, set_using_system};
};
let ctx = FunctionContext {
vis: &input.vis,
fn_name,
generics: &input.sig.generics,
inputs: &input.sig.inputs,
output: &input.sig.output,
where_clause: input.sig.generics.where_clause.as_ref(),
body: &input.block,
attrs: &input.attrs,
profile_new,
profile_drop,
is_test_fn,
import_mem_tracking,
};
if is_async {
generate_async_wrapper(&ctx)
} else {
generate_sync_wrapper(&ctx)
}
.into()
}
fn generate_sync_wrapper(ctx: &FunctionContext) -> proc_macro2::TokenStream {
let FunctionContext {
vis,
fn_name,
generics,
inputs,
output,
where_clause,
body,
attrs,
profile_new,
profile_drop,
is_test_fn: _,
import_mem_tracking,
}: &FunctionContext<'_> = ctx;
quote! {
#(#attrs)*
#vis fn #fn_name #generics (#inputs) #output #where_clause {
#import_mem_tracking
// We pass None for the name as we rely on the backtrace to identify the function
let profile = #profile_new;
let result = { #body };
#profile_drop
result
}
}
}
fn generate_async_wrapper(ctx: &FunctionContext) -> proc_macro2::TokenStream {
let FunctionContext {
vis,
fn_name,
generics,
inputs,
output,
where_clause,
body,
attrs,
profile_new,
profile_drop,
is_test_fn,
import_mem_tracking,
} = ctx;
// For test functions or functions with _test suffix, create a clone
// to make profile available inside the function body
let profile_setup = if *is_test_fn {
quote! {
let profile = #profile_new;
let profile_for_future = profile.clone();
}
} else {
quote! {
let profile = #profile_new;
}
};
// Choose the right profile for the future
let future_profile = if *is_test_fn {
quote! { profile_for_future }
} else {
quote! { profile }
};
// If this is a test function, add a debug message
let fn_name_str = fn_name.to_string();
let debug_msg = if *is_test_fn {
// Use a simple println instead of debug_log to avoid any import issues
quote! {
println!("Using cloned profile for test function: {}", #fn_name_str);
}
} else {
quote! {}
};
quote! {
#(#attrs)*
#vis async fn #fn_name #generics (#inputs) #output #where_clause {
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
#import_mem_tracking;
struct ProfiledFuture<F> {
inner: F,
_profile: Option<::thag_profiler::Profile>,
}
impl<F: Future> Future for ProfiledFuture<F> {
type Output = F::Output;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = unsafe { self.as_mut().get_unchecked_mut() };
let result = unsafe { Pin::new_unchecked(&mut this.inner) }.poll(cx);
if result.is_ready() {
// Take the profile out so we can explicitly drop it with the System allocator
if let Some(profile) = this._profile.take() {
#profile_drop
}
}
result
}
}
#debug_msg
#profile_setup
let future = async #body;
ProfiledFuture {
inner: future,
_profile: #future_profile,
}.await
}
}
}
/// Context for generating profiled function wrappers
///
/// This struct contains all the necessary components to generate either a synchronous
/// or asynchronous function wrapper with profiling capabilities.
#[derive(Debug)]
struct FunctionContext<'a> {
/// Function visibility (pub, pub(crate), etc.)
vis: &'a Visibility,
/// Function name identifier
fn_name: &'a syn::Ident,
/// Generic parameters including lifetimes and type parameters
generics: &'a Generics,
/// Function parameters
inputs: &'a syn::punctuated::Punctuated<FnArg, syn::Token![,]>,
/// Function return type
output: &'a ReturnType,
/// Optional where clause for generic constraints
where_clause: Option<&'a WhereClause>,
/// Function body
body: &'a syn::Block,
attrs: &'a Vec<Attribute>,
/// Profile instantiation, avoiding allocation tracking if memory profiling
profile_new: proc_macro2::TokenStream,
/// Profile drop, avoiding allocation tracking if memory profiling
profile_drop: proc_macro2::TokenStream,
/// Is this a test function (either by name convention or explicit flag)
is_test_fn: bool,
/// Import `mem_tracking` for `safe_alloc` if memory profiling
import_mem_tracking: proc_macro2::TokenStream,
}