code-mesh-core 0.1.0

High-performance, WASM-powered distributed swarm intelligence core library for concurrent code execution and neural mesh computing
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
//! Advanced profiling capabilities for performance analysis

use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use std::thread;

/// Advanced profiler for detailed performance analysis
pub struct Profiler {
    sessions: Arc<Mutex<HashMap<String, ProfilingSession>>>,
    global_stats: Arc<Mutex<GlobalProfilingStats>>,
}

impl Profiler {
    pub fn new() -> Self {
        Self {
            sessions: Arc::new(Mutex::new(HashMap::new())),
            global_stats: Arc::new(Mutex::new(GlobalProfilingStats::new())),
        }
    }

    /// Start a profiling session
    pub fn start_session(&self, session_id: &str, config: ProfilingConfig) -> ProfilingSession {
        let session = ProfilingSession::new(session_id.to_string(), config);
        
        {
            let mut sessions = self.sessions.lock().unwrap();
            sessions.insert(session_id.to_string(), session.clone());
        }

        session
    }

    /// End a profiling session and get results
    pub fn end_session(&self, session_id: &str) -> Option<ProfilingResults> {
        let mut sessions = self.sessions.lock().unwrap();
        if let Some(mut session) = sessions.remove(session_id) {
            session.end();
            let results = session.get_results();
            
            // Update global stats
            {
                let mut global_stats = self.global_stats.lock().unwrap();
                global_stats.update_from_session(&results);
            }

            Some(results)
        } else {
            None
        }
    }

    /// Get all active sessions
    pub fn get_active_sessions(&self) -> Vec<String> {
        let sessions = self.sessions.lock().unwrap();
        sessions.keys().cloned().collect()
    }

    /// Get global profiling statistics
    pub fn get_global_stats(&self) -> GlobalProfilingStats {
        self.global_stats.lock().unwrap().clone()
    }

    /// Profile a function execution
    pub fn profile_function<T, F>(&self, name: &str, func: F) -> (T, FunctionProfile)
    where
        F: FnOnce() -> T,
    {
        let start_time = Instant::now();
        let start_memory = get_current_memory_usage();
        
        let result = func();
        
        let end_time = Instant::now();
        let end_memory = get_current_memory_usage();
        
        let profile = FunctionProfile {
            name: name.to_string(),
            execution_time: end_time - start_time,
            memory_allocated: end_memory.saturating_sub(start_memory),
            cpu_cycles: estimate_cpu_cycles(end_time - start_time),
            call_count: 1,
        };

        (result, profile)
    }

    /// Profile an async function execution
    pub async fn profile_async_function<T, F, Fut>(&self, name: &str, func: F) -> (T, FunctionProfile)
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = T>,
    {
        let start_time = Instant::now();
        let start_memory = get_current_memory_usage();
        
        let result = func().await;
        
        let end_time = Instant::now();
        let end_memory = get_current_memory_usage();
        
        let profile = FunctionProfile {
            name: name.to_string(),
            execution_time: end_time - start_time,
            memory_allocated: end_memory.saturating_sub(start_memory),
            cpu_cycles: estimate_cpu_cycles(end_time - start_time),
            call_count: 1,
        };

        (result, profile)
    }

    /// Generate flame graph data
    pub fn generate_flame_graph(&self, session_id: &str) -> Option<FlameGraphData> {
        let sessions = self.sessions.lock().unwrap();
        if let Some(session) = sessions.get(session_id) {
            Some(session.generate_flame_graph())
        } else {
            None
        }
    }

    /// Export profiling data in various formats
    pub fn export_data(&self, session_id: &str, format: ExportFormat) -> Option<String> {
        let sessions = self.sessions.lock().unwrap();
        if let Some(session) = sessions.get(session_id) {
            match format {
                ExportFormat::Json => Some(session.export_json()),
                ExportFormat::Csv => Some(session.export_csv()),
                ExportFormat::FlameGraph => Some(session.export_flame_graph()),
            }
        } else {
            None
        }
    }
}

/// Profiling session for tracking performance during execution
#[derive(Debug, Clone)]
pub struct ProfilingSession {
    id: String,
    config: ProfilingConfig,
    start_time: Instant,
    end_time: Option<Instant>,
    function_profiles: Arc<Mutex<Vec<FunctionProfile>>>,
    memory_snapshots: Arc<Mutex<Vec<MemorySnapshot>>>,
    call_stack: Arc<Mutex<Vec<StackFrame>>>,
}

impl ProfilingSession {
    fn new(id: String, config: ProfilingConfig) -> Self {
        Self {
            id,
            config,
            start_time: Instant::now(),
            end_time: None,
            function_profiles: Arc::new(Mutex::new(Vec::new())),
            memory_snapshots: Arc::new(Mutex::new(Vec::new())),
            call_stack: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Record a function profile
    pub fn record_function(&self, profile: FunctionProfile) {
        if self.config.profile_functions {
            let mut profiles = self.function_profiles.lock().unwrap();
            profiles.push(profile);
        }
    }

    /// Take a memory snapshot
    pub fn take_memory_snapshot(&self, label: &str) {
        if self.config.profile_memory {
            let snapshot = MemorySnapshot {
                timestamp: Instant::now(),
                label: label.to_string(),
                memory_usage: get_current_memory_usage(),
                heap_size: get_heap_size(),
                allocated_objects: count_allocated_objects(),
            };

            let mut snapshots = self.memory_snapshots.lock().unwrap();
            snapshots.push(snapshot);
        }
    }

    /// Push to call stack
    pub fn push_call(&self, function_name: &str) {
        if self.config.track_call_stack {
            let frame = StackFrame {
                function_name: function_name.to_string(),
                entry_time: Instant::now(),
                file: String::new(), // Would be filled by macro
                line: 0,
            };

            let mut stack = self.call_stack.lock().unwrap();
            stack.push(frame);
        }
    }

    /// Pop from call stack
    pub fn pop_call(&self) -> Option<Duration> {
        if self.config.track_call_stack {
            let mut stack = self.call_stack.lock().unwrap();
            if let Some(frame) = stack.pop() {
                return Some(Instant::now() - frame.entry_time);
            }
        }
        None
    }

    fn end(&mut self) {
        self.end_time = Some(Instant::now());
    }

    fn get_results(&self) -> ProfilingResults {
        let total_duration = self.end_time.unwrap_or_else(Instant::now) - self.start_time;
        let function_profiles = self.function_profiles.lock().unwrap().clone();
        let memory_snapshots = self.memory_snapshots.lock().unwrap().clone();

        ProfilingResults {
            session_id: self.id.clone(),
            total_duration,
            function_profiles,
            memory_snapshots,
            call_stack_depth: self.call_stack.lock().unwrap().len(),
            peak_memory_usage: memory_snapshots.iter()
                .map(|s| s.memory_usage)
                .max()
                .unwrap_or(0),
        }
    }

    fn generate_flame_graph(&self) -> FlameGraphData {
        let function_profiles = self.function_profiles.lock().unwrap();
        let mut flame_data = HashMap::new();

        for profile in function_profiles.iter() {
            let entry = flame_data.entry(profile.name.clone())
                .or_insert(FlameGraphEntry {
                    name: profile.name.clone(),
                    self_time: Duration::default(),
                    total_time: Duration::default(),
                    call_count: 0,
                    children: HashMap::new(),
                });

            entry.self_time += profile.execution_time;
            entry.total_time += profile.execution_time;
            entry.call_count += profile.call_count;
        }

        FlameGraphData { entries: flame_data }
    }

    fn export_json(&self) -> String {
        let results = self.get_results();
        serde_json::to_string_pretty(&results).unwrap_or_else(|_| "{}".to_string())
    }

    fn export_csv(&self) -> String {
        let function_profiles = self.function_profiles.lock().unwrap();
        let mut csv = String::from("function_name,execution_time_ms,memory_allocated,call_count\n");

        for profile in function_profiles.iter() {
            csv.push_str(&format!(
                "{},{},{},{}\n",
                profile.name,
                profile.execution_time.as_millis(),
                profile.memory_allocated,
                profile.call_count
            ));
        }

        csv
    }

    fn export_flame_graph(&self) -> String {
        let flame_data = self.generate_flame_graph();
        // Convert to flame graph format (simplified)
        let mut output = String::new();
        
        for (name, entry) in flame_data.entries {
            output.push_str(&format!(
                "{} {}\n",
                name,
                entry.self_time.as_millis()
            ));
        }

        output
    }
}

/// Profiling configuration
#[derive(Debug, Clone)]
pub struct ProfilingConfig {
    pub profile_functions: bool,
    pub profile_memory: bool,
    pub track_call_stack: bool,
    pub sampling_interval: Duration,
    pub max_stack_depth: usize,
}

impl Default for ProfilingConfig {
    fn default() -> Self {
        Self {
            profile_functions: true,
            profile_memory: true,
            track_call_stack: true,
            sampling_interval: Duration::from_millis(10),
            max_stack_depth: 100,
        }
    }
}

/// Function profiling information
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct FunctionProfile {
    pub name: String,
    pub execution_time: Duration,
    pub memory_allocated: usize,
    pub cpu_cycles: u64,
    pub call_count: u64,
}

/// Memory snapshot
#[derive(Debug, Clone)]
pub struct MemorySnapshot {
    pub timestamp: Instant,
    pub label: String,
    pub memory_usage: usize,
    pub heap_size: usize,
    pub allocated_objects: usize,
}

/// Call stack frame
#[derive(Debug, Clone)]
pub struct StackFrame {
    pub function_name: String,
    pub entry_time: Instant,
    pub file: String,
    pub line: u32,
}

/// Profiling results
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ProfilingResults {
    pub session_id: String,
    pub total_duration: Duration,
    pub function_profiles: Vec<FunctionProfile>,
    pub memory_snapshots: Vec<MemorySnapshot>,
    pub call_stack_depth: usize,
    pub peak_memory_usage: usize,
}

/// Global profiling statistics
#[derive(Debug, Clone)]
pub struct GlobalProfilingStats {
    pub total_sessions: usize,
    pub total_execution_time: Duration,
    pub average_session_duration: Duration,
    pub most_expensive_functions: Vec<FunctionProfile>,
    pub memory_usage_trend: Vec<usize>,
}

impl GlobalProfilingStats {
    fn new() -> Self {
        Self {
            total_sessions: 0,
            total_execution_time: Duration::default(),
            average_session_duration: Duration::default(),
            most_expensive_functions: Vec::new(),
            memory_usage_trend: Vec::new(),
        }
    }

    fn update_from_session(&mut self, results: &ProfilingResults) {
        self.total_sessions += 1;
        self.total_execution_time += results.total_duration;
        self.average_session_duration = self.total_execution_time / self.total_sessions as u32;

        // Update most expensive functions
        for profile in &results.function_profiles {
            self.update_expensive_functions(profile.clone());
        }

        // Update memory trend
        self.memory_usage_trend.push(results.peak_memory_usage);
        if self.memory_usage_trend.len() > 100 {
            self.memory_usage_trend.remove(0);
        }
    }

    fn update_expensive_functions(&mut self, profile: FunctionProfile) {
        // Insert or update function profile in most expensive list
        if let Some(existing) = self.most_expensive_functions.iter_mut()
            .find(|p| p.name == profile.name) {
            existing.execution_time += profile.execution_time;
            existing.call_count += profile.call_count;
        } else {
            self.most_expensive_functions.push(profile);
        }

        // Keep only top 20 most expensive functions
        self.most_expensive_functions.sort_by(|a, b| b.execution_time.cmp(&a.execution_time));
        self.most_expensive_functions.truncate(20);
    }
}

/// Flame graph data structure
#[derive(Debug, Clone)]
pub struct FlameGraphData {
    pub entries: HashMap<String, FlameGraphEntry>,
}

#[derive(Debug, Clone)]
pub struct FlameGraphEntry {
    pub name: String,
    pub self_time: Duration,
    pub total_time: Duration,
    pub call_count: u64,
    pub children: HashMap<String, FlameGraphEntry>,
}

/// Export formats for profiling data
#[derive(Debug, Clone)]
pub enum ExportFormat {
    Json,
    Csv,
    FlameGraph,
}

// Helper functions for system metrics

fn get_current_memory_usage() -> usize {
    use memory_stats::memory_stats;
    memory_stats().map(|s| s.physical_mem).unwrap_or(0)
}

fn get_heap_size() -> usize {
    // Platform-specific heap size implementation
    0
}

fn count_allocated_objects() -> usize {
    // Platform-specific object count implementation
    0
}

fn estimate_cpu_cycles(duration: Duration) -> u64 {
    // Rough estimation: assume 3GHz CPU
    (duration.as_nanos() as u64 * 3) / 1000
}

/// Profiling macros for convenient usage
#[macro_export]
macro_rules! profile_function {
    ($profiler:expr, $name:expr, $block:block) => {{
        let (result, profile) = $profiler.profile_function($name, || $block);
        result
    }};
}

#[macro_export]
macro_rules! profile_async_function {
    ($profiler:expr, $name:expr, $async_block:expr) => {{
        let (result, profile) = $profiler.profile_async_function($name, || $async_block).await;
        result
    }};
}