torsh-profiler 0.1.2

Performance profiling and monitoring for ToRSh
Documentation
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//! Custom tool APIs for profiler integration
//!
//! This module provides extensible APIs for integrating custom profiling tools
//! and frameworks into the torsh-profiler ecosystem.

use crate::ProfileEvent;
use anyhow::Result;
use once_cell::sync::Lazy;
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Instant, SystemTime};

/// Global registry for custom profiling tools
static CUSTOM_TOOL_REGISTRY: Lazy<Mutex<CustomToolRegistry>> =
    Lazy::new(|| Mutex::new(CustomToolRegistry::new()));

/// Custom tool integration trait
pub trait CustomTool: Send + Sync {
    /// Initialize the custom tool
    fn initialize(&mut self, config: &ToolConfig) -> Result<()>;

    /// Start profiling session
    fn start_session(&mut self, session_id: &str) -> Result<()>;

    /// Record a profiling event
    fn record_event(&mut self, event: &ProfileEvent) -> Result<()>;

    /// Stop profiling session
    fn stop_session(&mut self, session_id: &str) -> Result<()>;

    /// Export collected data
    fn export_data(&self, format: &ExportFormat) -> Result<Vec<u8>>;

    /// Get tool statistics
    fn get_statistics(&self) -> Result<ToolStatistics>;

    /// Cleanup resources
    fn cleanup(&mut self) -> Result<()>;

    /// Get tool name
    fn name(&self) -> &str;

    /// Get tool version
    fn version(&self) -> &str;
}

/// Configuration for custom tools
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolConfig {
    pub name: String,
    pub version: String,
    pub enabled: bool,
    pub sampling_rate: f64,
    pub buffer_size: usize,
    pub output_path: Option<String>,
    pub custom_options: HashMap<String, String>,
}

impl Default for ToolConfig {
    fn default() -> Self {
        Self {
            name: "custom_tool".to_string(),
            version: "1.0.0".to_string(),
            enabled: true,
            sampling_rate: 1.0,
            buffer_size: 1024,
            output_path: None,
            custom_options: HashMap::new(),
        }
    }
}

/// Export format for custom tools
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ExportFormat {
    Json,
    Csv,
    Binary,
    Custom(String),
}

/// Tool statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolStatistics {
    pub events_recorded: u64,
    pub sessions_completed: u64,
    pub total_duration_ns: u64,
    pub memory_usage_bytes: u64,
    pub error_count: u64,
    pub last_updated: SystemTime,
}

impl Default for ToolStatistics {
    fn default() -> Self {
        Self {
            events_recorded: 0,
            sessions_completed: 0,
            total_duration_ns: 0,
            memory_usage_bytes: 0,
            error_count: 0,
            last_updated: SystemTime::now(),
        }
    }
}

/// Registry for managing custom tools
#[derive(Default)]
pub struct CustomToolRegistry {
    tools: HashMap<String, Box<dyn CustomTool>>,
    configs: HashMap<String, ToolConfig>,
}

impl CustomToolRegistry {
    pub fn new() -> Self {
        Self {
            tools: HashMap::new(),
            configs: HashMap::new(),
        }
    }

    /// Register a custom tool
    pub fn register_tool(&mut self, tool: Box<dyn CustomTool>, config: ToolConfig) -> Result<()> {
        let name = tool.name().to_string();
        self.configs.insert(name.clone(), config);
        self.tools.insert(name, tool);
        Ok(())
    }

    /// Unregister a custom tool
    pub fn unregister_tool(&mut self, name: &str) -> Result<()> {
        if let Some(mut tool) = self.tools.remove(name) {
            tool.cleanup()?;
        }
        self.configs.remove(name);
        Ok(())
    }

    /// Get tool by name
    pub fn get_tool(&mut self, name: &str) -> Option<&mut Box<dyn CustomTool>> {
        self.tools.get_mut(name)
    }

    /// Initialize all tools
    pub fn initialize_all(&mut self) -> Result<()> {
        for (name, tool) in &mut self.tools {
            if let Some(config) = self.configs.get(name) {
                tool.initialize(config)?;
            }
        }
        Ok(())
    }

    /// Start session on all tools
    pub fn start_session_all(&mut self, session_id: &str) -> Result<()> {
        for tool in self.tools.values_mut() {
            tool.start_session(session_id)?;
        }
        Ok(())
    }

    /// Record event on all tools
    pub fn record_event_all(&mut self, event: &ProfileEvent) -> Result<()> {
        for tool in self.tools.values_mut() {
            tool.record_event(event)?;
        }
        Ok(())
    }

    /// Stop session on all tools
    pub fn stop_session_all(&mut self, session_id: &str) -> Result<()> {
        for tool in self.tools.values_mut() {
            tool.stop_session(session_id)?;
        }
        Ok(())
    }

    /// Get all tool statistics
    pub fn get_all_statistics(&self) -> HashMap<String, ToolStatistics> {
        let mut stats = HashMap::new();
        for (name, tool) in &self.tools {
            if let Ok(tool_stats) = tool.get_statistics() {
                stats.insert(name.clone(), tool_stats);
            }
        }
        stats
    }

    /// List all registered tools
    pub fn list_tools(&self) -> Vec<String> {
        self.tools.keys().cloned().collect()
    }
}

/// Example custom tool implementation
pub struct ExampleCustomTool {
    name: String,
    version: String,
    statistics: ToolStatistics,
    events: Vec<ProfileEvent>,
    active_sessions: Vec<String>,
}

impl ExampleCustomTool {
    pub fn new(name: String, version: String) -> Self {
        Self {
            name,
            version,
            statistics: ToolStatistics::default(),
            events: Vec::new(),
            active_sessions: Vec::new(),
        }
    }
}

impl CustomTool for ExampleCustomTool {
    fn initialize(&mut self, config: &ToolConfig) -> Result<()> {
        println!("Initializing custom tool: {}", config.name);
        Ok(())
    }

    fn start_session(&mut self, session_id: &str) -> Result<()> {
        self.active_sessions.push(session_id.to_string());
        Ok(())
    }

    fn record_event(&mut self, event: &ProfileEvent) -> Result<()> {
        self.events.push(event.clone());
        self.statistics.events_recorded += 1;
        self.statistics.last_updated = SystemTime::now();
        Ok(())
    }

    fn stop_session(&mut self, session_id: &str) -> Result<()> {
        self.active_sessions.retain(|s| s != session_id);
        self.statistics.sessions_completed += 1;
        Ok(())
    }

    fn export_data(&self, format: &ExportFormat) -> Result<Vec<u8>> {
        match format {
            ExportFormat::Json => {
                let json = serde_json::to_string_pretty(&self.events)?;
                Ok(json.into_bytes())
            }
            ExportFormat::Csv => {
                let mut csv = String::new();
                csv.push_str("timestamp,name,duration_ns,thread_id\n");
                for event in &self.events {
                    csv.push_str(&format!(
                        "{},{},{},{}\n",
                        event.start_us,
                        event.name,
                        event.duration_us * 1000, // Convert to ns
                        event.thread_id as u64
                    ));
                }
                Ok(csv.into_bytes())
            }
            _ => Err(anyhow::anyhow!("Unsupported export format")),
        }
    }

    fn get_statistics(&self) -> Result<ToolStatistics> {
        Ok(self.statistics.clone())
    }

    fn cleanup(&mut self) -> Result<()> {
        self.events.clear();
        self.active_sessions.clear();
        Ok(())
    }

    fn name(&self) -> &str {
        &self.name
    }

    fn version(&self) -> &str {
        &self.version
    }
}

/// External tool bridge for integrating third-party tools
pub struct ExternalToolBridge {
    name: String,
    version: String,
    command: String,
    args: Vec<String>,
    statistics: ToolStatistics,
}

impl ExternalToolBridge {
    pub fn new(name: String, version: String, command: String, args: Vec<String>) -> Self {
        Self {
            name,
            version,
            command,
            args,
            statistics: ToolStatistics::default(),
        }
    }
}

impl CustomTool for ExternalToolBridge {
    fn initialize(&mut self, config: &ToolConfig) -> Result<()> {
        // Initialize external tool process
        std::process::Command::new(&self.command)
            .args(&self.args)
            .arg("--initialize")
            .output()?;
        Ok(())
    }

    fn start_session(&mut self, session_id: &str) -> Result<()> {
        std::process::Command::new(&self.command)
            .args(&self.args)
            .arg("--start-session")
            .arg(session_id)
            .output()?;
        Ok(())
    }

    fn record_event(&mut self, event: &ProfileEvent) -> Result<()> {
        let event_json = serde_json::to_string(event)?;
        std::process::Command::new(&self.command)
            .args(&self.args)
            .arg("--record-event")
            .arg(&event_json)
            .output()?;
        self.statistics.events_recorded += 1;
        Ok(())
    }

    fn stop_session(&mut self, session_id: &str) -> Result<()> {
        std::process::Command::new(&self.command)
            .args(&self.args)
            .arg("--stop-session")
            .arg(session_id)
            .output()?;
        self.statistics.sessions_completed += 1;
        Ok(())
    }

    fn export_data(&self, format: &ExportFormat) -> Result<Vec<u8>> {
        let format_str = match format {
            ExportFormat::Json => "json",
            ExportFormat::Csv => "csv",
            ExportFormat::Binary => "binary",
            ExportFormat::Custom(fmt) => fmt,
        };

        let output = std::process::Command::new(&self.command)
            .args(&self.args)
            .arg("--export")
            .arg(format_str)
            .output()?;

        Ok(output.stdout)
    }

    fn get_statistics(&self) -> Result<ToolStatistics> {
        Ok(self.statistics.clone())
    }

    fn cleanup(&mut self) -> Result<()> {
        std::process::Command::new(&self.command)
            .args(&self.args)
            .arg("--cleanup")
            .output()?;
        Ok(())
    }

    fn name(&self) -> &str {
        &self.name
    }

    fn version(&self) -> &str {
        &self.version
    }
}

/// Public API functions for custom tool integration
/// Register a custom tool with the global registry
pub fn register_custom_tool(tool: Box<dyn CustomTool>, config: ToolConfig) -> Result<()> {
    CUSTOM_TOOL_REGISTRY.lock().register_tool(tool, config)
}

/// Unregister a custom tool from the global registry
pub fn unregister_custom_tool(name: &str) -> Result<()> {
    CUSTOM_TOOL_REGISTRY.lock().unregister_tool(name)
}

/// Initialize all registered custom tools
pub fn initialize_custom_tools() -> Result<()> {
    CUSTOM_TOOL_REGISTRY.lock().initialize_all()
}

/// Start profiling session on all custom tools
pub fn start_custom_tool_session(session_id: &str) -> Result<()> {
    CUSTOM_TOOL_REGISTRY.lock().start_session_all(session_id)
}

/// Record event on all custom tools
pub fn record_custom_tool_event(event: &ProfileEvent) -> Result<()> {
    CUSTOM_TOOL_REGISTRY.lock().record_event_all(event)
}

/// Stop profiling session on all custom tools
pub fn stop_custom_tool_session(session_id: &str) -> Result<()> {
    CUSTOM_TOOL_REGISTRY.lock().stop_session_all(session_id)
}

/// Get statistics from all custom tools
pub fn get_custom_tool_statistics() -> HashMap<String, ToolStatistics> {
    CUSTOM_TOOL_REGISTRY.lock().get_all_statistics()
}

/// List all registered custom tools
pub fn list_custom_tools() -> Vec<String> {
    CUSTOM_TOOL_REGISTRY.lock().list_tools()
}

/// Create an example custom tool
pub fn create_example_tool(name: String, version: String) -> Box<dyn CustomTool> {
    Box::new(ExampleCustomTool::new(name, version))
}

/// Create an external tool bridge
pub fn create_external_tool_bridge(
    name: String,
    version: String,
    command: String,
    args: Vec<String>,
) -> Box<dyn CustomTool> {
    Box::new(ExternalToolBridge::new(name, version, command, args))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::SystemTime;

    #[test]
    fn test_custom_tool_registration() {
        let mut registry = CustomToolRegistry::new();
        let tool = create_example_tool("test_tool".to_string(), "1.0.0".to_string());
        let config = ToolConfig::default();

        assert!(registry.register_tool(tool, config).is_ok());
        assert_eq!(registry.list_tools(), vec!["test_tool"]);
    }

    #[test]
    fn test_tool_lifecycle() {
        let mut registry = CustomToolRegistry::new();
        let tool = create_example_tool("test_tool".to_string(), "1.0.0".to_string());
        let config = ToolConfig::default();

        registry.register_tool(tool, config).unwrap();
        registry.initialize_all().unwrap();
        registry.start_session_all("test_session").unwrap();

        let event = ProfileEvent {
            name: "test_event".to_string(),
            category: "test".to_string(),
            start_us: 0,
            duration_us: 1,
            thread_id: 0,
            operation_count: Some(1),
            flops: Some(100),
            bytes_transferred: Some(1024),
            stack_trace: Some("test trace".to_string()),
        };

        registry.record_event_all(&event).unwrap();
        registry.stop_session_all("test_session").unwrap();

        let stats = registry.get_all_statistics();
        assert_eq!(stats.get("test_tool").unwrap().events_recorded, 1);
        assert_eq!(stats.get("test_tool").unwrap().sessions_completed, 1);
    }

    #[test]
    fn test_example_tool_export() {
        let mut tool = ExampleCustomTool::new("test".to_string(), "1.0.0".to_string());
        let config = ToolConfig::default();

        tool.initialize(&config).unwrap();
        tool.start_session("test").unwrap();

        let event = ProfileEvent {
            name: "test_event".to_string(),
            category: "test".to_string(),
            start_us: 0,
            duration_us: 1,
            thread_id: 0,
            operation_count: Some(1),
            flops: Some(100),
            bytes_transferred: Some(1024),
            stack_trace: Some("test trace".to_string()),
        };

        tool.record_event(&event).unwrap();

        let json_data = tool.export_data(&ExportFormat::Json).unwrap();
        assert!(!json_data.is_empty());

        let csv_data = tool.export_data(&ExportFormat::Csv).unwrap();
        assert!(!csv_data.is_empty());

        tool.stop_session("test").unwrap();
        tool.cleanup().unwrap();
    }

    #[test]
    fn test_tool_config_serialization() {
        let config = ToolConfig {
            name: "test_tool".to_string(),
            version: "1.0.0".to_string(),
            enabled: true,
            sampling_rate: 0.5,
            buffer_size: 2048,
            output_path: Some(std::env::temp_dir().join("test").display().to_string()),
            custom_options: {
                let mut opts = HashMap::new();
                opts.insert("key1".to_string(), "value1".to_string());
                opts.insert("key2".to_string(), "value2".to_string());
                opts
            },
        };

        let json = serde_json::to_string(&config).unwrap();
        let deserialized: ToolConfig = serde_json::from_str(&json).unwrap();

        assert_eq!(config.name, deserialized.name);
        assert_eq!(config.version, deserialized.version);
        assert_eq!(config.enabled, deserialized.enabled);
        assert_eq!(config.sampling_rate, deserialized.sampling_rate);
        assert_eq!(config.buffer_size, deserialized.buffer_size);
        assert_eq!(config.output_path, deserialized.output_path);
        assert_eq!(config.custom_options, deserialized.custom_options);
    }

    #[test]
    fn test_global_api_functions() {
        let tool = create_example_tool("global_test".to_string(), "1.0.0".to_string());
        let config = ToolConfig::default();

        assert!(register_custom_tool(tool, config).is_ok());
        assert!(list_custom_tools().contains(&"global_test".to_string()));

        assert!(initialize_custom_tools().is_ok());
        assert!(start_custom_tool_session("global_session").is_ok());

        let event = ProfileEvent {
            name: "global_event".to_string(),
            category: "test".to_string(),
            start_us: 0,
            duration_us: 1,
            thread_id: 0,
            operation_count: Some(1),
            flops: Some(100),
            bytes_transferred: Some(1024),
            stack_trace: Some("test trace".to_string()),
        };

        assert!(record_custom_tool_event(&event).is_ok());
        assert!(stop_custom_tool_session("global_session").is_ok());

        let stats = get_custom_tool_statistics();
        assert!(stats.contains_key("global_test"));

        assert!(unregister_custom_tool("global_test").is_ok());
    }
}