joule-profiler-core 2.1.0

Core library for joule-profiler, handling orchestration and energy measurements
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
//! This module contains the core logic of profiling and is the entrypoint of Joule Profiler.
//!
//! It provides the main [`JouleProfiler`] struct, orchestrating
//! the execution of commands, collecting metrics from various sources (e.g. RAPL, `perf_event`, NVML, etc.),
//! and aggregate them into a clean common structure.

use log::{debug, info, trace};
use regex::Regex;
use std::io::BufWriter;
use std::os::unix::process::CommandExt;
use std::process::{Child, ChildStdout, Command};
use std::{
    io::{BufRead, BufReader, ErrorKind, Write},
    process::{self, Stdio},
};
use tokio::sync::oneshot;

pub mod error;

use crate::config::ProfileConfig;
use crate::orchestrator::Orchestrator;
use crate::phase::{PhaseInfo, PhaseToken};
use crate::profiler::types::{MeasureData, Phase, ProfilerResults, ReaderResult, Result};
use crate::sensor::{Sensor, Sensors};
use crate::source::{MetricReader, MetricSource, MetricSourceError};
use crate::util::fs::create_file_with_user_permissions;
use crate::util::sys::{get_uid_from_username, is_root, signal};
use crate::util::time::get_timestamp_micros;
pub use error::JouleProfilerError;

pub mod types;

/// Orchestrates program profiling and metric collection.
///
/// `JouleProfiler` runs a command, collects energy metrics from registered
/// sources (e.g. RAPL, `perf_event`, NVML), and aggregates them into a
/// structured result organized by phases.
///
/// It is also responsible for the detection of phases (parts of a program execution)
/// through the standard output.
///
/// # Examples
///
/// ```no_run
/// use joule_profiler_core::{JouleProfiler, config::ProfileConfig};
///
/// # tokio_test::block_on(async {
/// let mut profiler = JouleProfiler::new();
///
/// // Add sources using profiler.add_source(source)
///
/// let config = ProfileConfig {
///     cmd: vec!["echo".to_string(), "hello".to_string()],
///     token_pattern: "__PHASE__".to_string(),
///     stdout_file: None,
///     use_root: false,
///     init_timeout: std::time::Duration::from_secs(1),
/// };
///
/// let results = profiler.profile(&config).await.unwrap();
/// # });
/// ```
#[derive(Default)]
pub struct JouleProfiler {
    /// The different metric sources.
    sources: Vec<Box<dyn MetricSource>>,
}

impl JouleProfiler {
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds a custom metric source to the profiler.
    ///
    /// A source must implement the [`MetricReader`] trait.
    pub fn add_source<T>(&mut self, reader: T)
    where
        T: MetricReader,
    {
        debug!("Registering additional metric source: {}", T::get_name());
        trace!("MetricReader type: {}", std::any::type_name::<T>());
        self.sources.push(reader.into());
    }

    /// List the sensors of the provided sources.
    pub fn list_sensors(&mut self) -> Result<Sensors> {
        debug!("Listing sensors from {} source(s)", self.sources.len());

        let sensors: Vec<Sensor> = self
            .sources
            .iter()
            .enumerate()
            .map(|(i, source)| {
                trace!("Querying sensors from source {i}");
                source.list_sensors().map_err(MetricSourceError::into)
            })
            .collect::<Result<Vec<Sensors>>>()?
            .into_iter()
            .flatten()
            .collect();

        info!("Discovered {} sensor(s)", sensors.len());
        Ok(sensors)
    }

    /// Profiles a program spawned with the configured command and return the aggregated results.
    ///
    /// It spawns the program, initializes the metric sources with its pid, starts the
    /// orchestrator, and profiles the program.
    pub async fn profile(&mut self, config: &ProfileConfig) -> Result<ProfilerResults> {
        info!("Running phase-based profiling");
        debug!("Phase regex: {}", config.token_pattern);

        let regex = Regex::new(&config.token_pattern)?;

        let sources = std::mem::take(&mut self.sources);
        if sources.is_empty() {
            return Err(JouleProfilerError::NoSourceConfigured);
        }
        let mut orchestrator = Orchestrator::new(sources);

        orchestrator.pre_init().await?;

        debug!("Spawning command: {:?}", config.cmd);
        let mut child = spawn_profiled_command(config)?;
        let pid = child.id().cast_signed();

        pause_process(pid)?;

        orchestrator.init(pid, config.init_timeout).await?;
        orchestrator.run();

        let child_stdout = child
            .stdout
            .take()
            .ok_or(JouleProfilerError::StdOutCaptureFail)?;

        info!("Starting measurements");
        let reader_result_rx = spawn_reader_thread(
            orchestrator,
            child_stdout,
            regex,
            config.stdout_file.clone(),
            pid,
        )?;

        let (mut orchestrator, measure_result) = reader_result_rx
            .await
            .map_err(|_| JouleProfilerError::ReaderThreadPanicked)?;

        let MeasureData {
            phases: detected_phases,
            begin_timestamp,
            end_timestamp,
        } = match measure_result {
            Ok(data) => data,
            Err(err) => {
                return Err(match orchestrator.finalize().await {
                    Err(source_err) => source_err.into(),
                    Ok(_) => err,
                });
            }
        };

        let command_duration_ms = (end_timestamp - begin_timestamp) / 1000;
        let timestamp = begin_timestamp;

        let exit_code = tokio::task::spawn_blocking(move || wait_for_child_exit(&mut child))
            .await
            .map_err(|_| {
                JouleProfilerError::ProcessControlFailed("wait thread panicked".to_string())
            })??;
        info!("Command finished: duration={command_duration_ms} ms exit_code={exit_code}");

        let (sources_results, sources) = orchestrator.finalize().await?;
        self.sources = sources;

        let mut phases: Vec<_> = detected_phases
            .windows(2)
            .enumerate()
            .zip(&sources_results.phases)
            .map(|((index, window), real_phase)| {
                let (d1, d2) = (&window[0], &window[1]);
                let mut phase_metrics = real_phase.metrics.clone();
                phase_metrics.sort_by(|a, b| a.name.cmp(&b.name));
                Phase {
                    index,
                    metrics: phase_metrics,
                    start_token: d1.token.clone(),
                    end_token: d2.token.clone(),
                    timestamp: d1.timestamp,
                    duration_ms: (d2.timestamp - d1.timestamp) / 1000,
                    start_token_line: d1.line_number,
                    end_token_line: d2.line_number,
                }
            })
            .collect();

        if phases.is_empty()
            && let Some(end_phase) = sources_results.phases.into_iter().last()
        {
            phases.push(Phase {
                index: 0,
                metrics: end_phase.metrics,
                start_token: PhaseToken::Start,
                end_token: PhaseToken::End,
                timestamp,
                duration_ms: command_duration_ms,
                start_token_line: None,
                end_token_line: None,
            });
        }

        debug!("Collected {} sensor phase(s)", phases.len());
        Ok(ProfilerResults {
            timestamp,
            duration_ms: command_duration_ms,
            exit_code,
            phases,
        })
    }
}

/// Profiles the already spawned and paused command, on the reader thread:
/// first measure, resume the process, then a blocking measure and new phase
/// for each detected token, until the stdout closes.
fn measure_phases_blocking(
    orchestrator: &mut Orchestrator,
    child_stdout: ChildStdout,
    regex: &Regex,
    stdout_file: Option<&String>,
    pid: i32,
) -> Result<MeasureData> {
    let sink = create_output_sink(stdout_file)?;
    let reader = BufReader::new(child_stdout);

    let mut detected_phases = Vec::with_capacity(2);

    let begin_timestamp = get_timestamp_micros();
    trace!("Begin timestamp: {begin_timestamp}");

    orchestrator.measure_blocking()?;
    resume_process(pid)?;
    detected_phases.push(PhaseInfo::start(begin_timestamp));

    read_and_detect_phases(orchestrator, &mut detected_phases, reader, regex, sink)?;

    let end_timestamp = get_timestamp_micros();
    trace!("End timestamp: {end_timestamp}");

    orchestrator.measure_blocking()?;
    orchestrator.new_phase_blocking()?;
    detected_phases.push(PhaseInfo::end(end_timestamp));

    Ok(MeasureData {
        phases: detected_phases,
        begin_timestamp,
        end_timestamp,
    })
}

/// Spawns the dedicated reader thread, which owns the orchestrator during the
/// run and sends it back with the measurement outcome once done.
fn spawn_reader_thread(
    mut orchestrator: Orchestrator,
    child_stdout: ChildStdout,
    regex: Regex,
    stdout_file: Option<String>,
    pid: i32,
) -> Result<oneshot::Receiver<ReaderResult>> {
    let (tx, rx) = oneshot::channel();

    std::thread::Builder::new()
        .name("phase-reader".to_string())
        .spawn(move || {
            let result = measure_phases_blocking(
                &mut orchestrator,
                child_stdout,
                &regex,
                stdout_file.as_ref(),
                pid,
            );
            let _ = tx.send((orchestrator, result));
        })
        .map_err(|err| JouleProfilerError::ReaderThreadSpawnFailed(err.to_string()))?;

    Ok(rx)
}

/// Reads the child's stdout line by line; every line matching the regex
/// triggers a blocking measure and new phase, and records a [`PhaseInfo`].
fn read_and_detect_phases<R, W>(
    orchestrator: &mut Orchestrator,
    phases: &mut Vec<PhaseInfo>,
    mut reader: R,
    regex: &Regex,
    mut sink: W,
) -> Result<()>
where
    R: BufRead,
    W: Write,
{
    let mut line = String::new();
    let mut line_number: usize = 0;

    loop {
        line.clear();

        match reader.read_line(&mut line) {
            Ok(0) => break,
            Ok(_) => {}
            Err(e) if e.kind() == ErrorKind::InvalidData => {
                trace!("Skipping invalid UTF-8 output at line {line_number}");
                line_number += 1;
                continue;
            }
            Err(e) => return Err(e.into()),
        }

        if line.ends_with('\n') {
            line.pop();
            if line.ends_with('\r') {
                line.pop();
            }
        }

        writeln!(sink, "{line}")?;

        if let Some(token) = phase_token_in_line(regex, &line) {
            let phase_timestamp = get_timestamp_micros();
            debug!("Detected phase at line {line_number}, token '{token}'");

            orchestrator.measure_blocking()?;
            orchestrator.new_phase_blocking()?;

            phases.push(PhaseInfo {
                token: PhaseToken::Token(token.to_owned()),
                timestamp: phase_timestamp,
                line_number: Some(line_number),
            });
        }

        line_number += 1;
    }

    sink.flush()?;
    Ok(())
}

/// Checks whether a line matches the specified regular expression.
pub fn phase_token_in_line<'a>(regex: &Regex, line: &'a str) -> Option<&'a str> {
    regex.find(line).map(|mat| mat.as_str())
}

/// Spawns a sub-process with the specified command and arguments.
///
/// Returns the attached sub-process on success. If an error occur, a [`JouleProfilerError::CommandNotFound`]
/// error is returned if the specified program cannot be found, or a [`JouleProfilerError::CommandExecutionFailed`] otherwise.
///
/// The standard output is piped to be analyzed for phases detection.
fn spawn_profiled_command(config: &ProfileConfig) -> Result<Child> {
    let mut command = init_command(&config.cmd, config.use_root)?;

    command.spawn().map_err(|err| {
        if err.kind() == ErrorKind::NotFound {
            JouleProfilerError::CommandNotFound(config.cmd[0].clone())
        } else {
            JouleProfilerError::CommandExecutionFailed(err.to_string())
        }
    })
}

/// Initializes the command used to spawn the profiled process and handles it's privileges.
///
/// If the current user is root and the parameter `use_root` is true, then the command
/// is spawned with root privileges, else the real user id is retrieved and the process
/// runs with user privileges. If Joule Profiler is not launched with root privileges,
/// then the program is spawned with default user privileges.
///
/// An error can occur if:
/// - The `SUDO_USER` environment variable cannot be retrieved, even so the user is root.
/// - The user uid cannot be retrieved with it's username provided by the environment variable.  
pub fn init_command(cmd: &[String], use_root: bool) -> Result<Command> {
    let mut command = if let Some(program) = cmd.first() {
        process::Command::new(program)
    } else {
        return Err(JouleProfilerError::EmptyCommand);
    };

    if cmd.len() > 1 {
        command.args(&cmd[1..]);
    }

    if is_root() && !use_root {
        let username =
            std::env::var("SUDO_USER").map_err(|_| JouleProfilerError::CannotRetrieveSudoUser)?;
        let uid = get_uid_from_username(&username)?;
        command.uid(uid);
    }

    command.stdout(Stdio::piped());
    command.stderr(Stdio::inherit());

    Ok(command)
}

/// Waits for the sub-process termination, returns the status code of the child.
///
/// If the child cannot be terminated, its associated error will be forwarded, also if the
/// exit status code cannot be retrieved, 1 is returned, signifying that an error occured.
fn wait_for_child_exit(child: &mut process::Child) -> Result<i32> {
    let status = child.wait()?;
    Ok(status.code().unwrap_or(1))
}

/// Creates a sink to be able to write the program output into either the process output file, either the standard output of the profiler.
///
/// A buffered writer is used to limit the system calls made, thus reducing the overhead introduced by the profiler.
fn create_output_sink(path: Option<&String>) -> Result<Box<dyn Write>> {
    if let Some(path) = path {
        let file = create_file_with_user_permissions(path).map_err(|err| {
            JouleProfilerError::OutputFileCreationFailed(format!("{path:?}: {err}"))
        })?;

        Ok(Box::new(BufWriter::new(file)))
    } else {
        Ok(Box::new(BufWriter::new(std::io::stdout().lock())))
    }
}

/// Sends `SIGSTOP` to a child process to pause its execution.
///
/// # Preconditions
///
/// - 'pid' must refer to a valid process identifier obtained from
///   '`std::process::Child::id()`' immediately after spawning.
/// - The process must be owned by the current user (we only signal
///   child processes we created).
///
/// Returns [`JouleProfilerError::ProcessControlFailed`] if the
/// signal delivery fails.
fn pause_process(pid: i32) -> Result<()> {
    signal(pid, libc::SIGSTOP)
}

/// Sends `SIGCONT` to resume a previously paused process.
///
/// # Preconditions
///
/// - `pid` must refer to a valid, running or stopped child process.
/// - The process must still exist when the signal is sent.
///
/// Returns [`JouleProfilerError::ProcessControlFailed`] if the
/// signal delivery fails.
fn resume_process(pid: i32) -> Result<()> {
    signal(pid, libc::SIGCONT)
}

#[cfg(test)]
mod tests {
    use crate::config::ProfileConfig;
    use crate::orchestrator::Orchestrator;
    use crate::phase::{PhaseInfo, PhaseToken};
    use crate::profiler::{
        create_output_sink, phase_token_in_line, read_and_detect_phases, spawn_profiled_command,
        wait_for_child_exit,
    };
    use crate::sensor::Sensors;
    use crate::source::MetricReader;
    use crate::types::Metrics;
    use crate::{JouleProfiler, JouleProfilerError};
    use mockall::mock;
    use regex::Regex;
    use std::fs;
    use std::io::{BufReader, Cursor, Read, Write};
    use std::time::Duration;
    use tempfile::TempDir;

    fn joule_profiler() -> JouleProfiler {
        JouleProfiler {
            sources: Vec::new(),
        }
    }

    /// Drives [`read_and_detect_phases`] to completion over an in-memory reader
    /// with a sourceless orchestrator, and collects the recorded [`PhaseInfo`]s.
    fn collect_phases<R, W>(
        reader: R,
        regex: &Regex,
        sink: W,
    ) -> crate::profiler::types::Result<Vec<PhaseInfo>>
    where
        R: std::io::BufRead,
        W: Write,
    {
        let mut orchestrator = Orchestrator::new(Vec::new());
        let mut phases = Vec::new();
        read_and_detect_phases(&mut orchestrator, &mut phases, reader, regex, sink)?;
        Ok(phases)
    }

    fn create_test_config(cmd: Vec<String>) -> ProfileConfig {
        ProfileConfig {
            cmd,
            token_pattern: "__PHASE__".to_string(),
            stdout_file: None,
            use_root: false,
            init_timeout: Duration::from_secs(1),
        }
    }

    #[derive(Debug)]
    pub struct MockError;

    impl std::fmt::Display for MockError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "mock error")
        }
    }

    impl std::error::Error for MockError {}

    mock! {
        pub MetricReader {}

        impl MetricReader for MetricReader {
            type Type = ();
            type Error = MockError;
            type Config = ();
            fn from_config(config: ()) -> Result<Self, MockError>;
            async fn init(&mut self, pid: i32) -> Result<(), MockError>;
            async fn join(&mut self) -> Result<(), MockError>;
            async fn measure(&mut self) -> Result<(), MockError>;
            async fn retrieve(&mut self) -> Result<(), MockError>;
            fn get_sensors(&self) -> Result<Sensors, MockError>;
            fn to_metrics(&self, v: ()) -> Result<Metrics, MockError>;
            fn get_name() -> &'static str;
            fn get_id() -> &'static str;
        }
    }

    #[test]
    fn detect_multiple_phases() {
        let regex = Regex::new("__[A-Z0-9_]+__").unwrap();
        let cursor = Cursor::new("__PHASE1__\n__PHASE2__\n__PHASE3__");
        let reader = BufReader::new(cursor);
        let sink: Vec<u8> = Vec::new();

        let phases = collect_phases(reader, &regex, sink).unwrap();

        assert_eq!(3, phases.len());
        assert_eq!(PhaseToken::Token("__PHASE1__".to_string()), phases[0].token);
        assert_eq!(PhaseToken::Token("__PHASE2__".to_string()), phases[1].token);
        assert_eq!(PhaseToken::Token("__PHASE3__".to_string()), phases[2].token);
    }

    #[test]
    fn detect_no_phases() {
        let regex = Regex::new("__[A-Z0-9_]+__").unwrap();
        let cursor = Cursor::new("hello\nworld\nno phases here");
        let reader = BufReader::new(cursor);
        let sink: Vec<u8> = Vec::new();

        let phases = collect_phases(reader, &regex, sink).unwrap();

        assert!(phases.is_empty());
    }

    #[test]
    fn detect_empty_output() {
        let regex = Regex::new("__PHASE__").unwrap();
        let cursor = Cursor::new("");
        let reader = BufReader::new(cursor);
        let sink: Vec<u8> = Vec::new();

        let phases = collect_phases(reader, &regex, sink).unwrap();

        assert_eq!(phases.len(), 0);
    }

    #[test]
    fn detect_phase_in_middle_of_line() {
        let regex = Regex::new("__PHASE[0-9]+__").unwrap();
        let cursor = Cursor::new("start __PHASE1__ end");
        let reader = BufReader::new(cursor);
        let sink: Vec<u8> = Vec::new();

        let phases = collect_phases(reader, &regex, sink).unwrap();

        assert_eq!(phases.len(), 1);
        assert_eq!(phases[0].token, PhaseToken::Token("__PHASE1__".to_string()));
        assert_eq!(phases[0].line_number, Some(0));
    }

    #[test]
    fn detect_correct_line_numbers() {
        let regex = Regex::new("__PHASE[0-9]+__").unwrap();
        let cursor = Cursor::new("a\nb\n__PHASE1__\nc\n__PHASE2__");
        let reader = BufReader::new(cursor);
        let sink: Vec<u8> = Vec::new();

        let phases = collect_phases(reader, &regex, sink).unwrap();

        assert_eq!(phases.len(), 2);
        assert_eq!(phases[0].line_number, Some(2));
        assert_eq!(phases[1].line_number, Some(4));
    }

    #[test]
    fn writes_stdout_to_file() {
        use std::fs;
        use tempfile::NamedTempFile;

        let regex = Regex::new("__PHASE__").unwrap();
        let cursor = Cursor::new("hello\n__PHASE__\nworld");
        let reader = BufReader::new(cursor);

        let mut temp_file = NamedTempFile::new().unwrap();

        collect_phases(reader, &regex, temp_file.as_file_mut()).unwrap();

        let content = fs::read_to_string(temp_file.path()).unwrap();
        assert!(content.contains("hello"));
        assert!(content.contains("__PHASE__"));
        assert!(content.contains("world"));
    }

    #[test]
    fn skips_invalid_utf8_lines() {
        let regex = Regex::new("__PHASE__").unwrap();

        let bytes = vec![
            0xff, 0xfe, b'\n', b'_', b'_', b'P', b'H', b'A', b'S', b'E', b'_', b'_',
        ];
        let cursor = Cursor::new(bytes);
        let reader = BufReader::new(cursor);
        let sink: Vec<u8> = Vec::new();

        let phases = collect_phases(reader, &regex, sink).unwrap();

        assert_eq!(phases.len(), 1);
    }

    #[tokio::test]
    async fn reader_channel_dropped_without_send_maps_to_reader_thread_panicked() {
        let (tx, rx) = tokio::sync::oneshot::channel::<()>();
        drop(tx);

        let result: crate::profiler::types::Result<()> = rx
            .await
            .map_err(|_| JouleProfilerError::ReaderThreadPanicked);

        assert!(matches!(
            result,
            Err(JouleProfilerError::ReaderThreadPanicked)
        ));
    }

    #[test]
    fn phase_token_in_line_returns_none_when_no_match() {
        let regex = Regex::new("X").unwrap();
        assert_eq!(phase_token_in_line(&regex, "abc"), None);
    }

    #[test]
    fn phase_token_in_line_returns_some_when_match_exists() {
        let regex = Regex::new("X").unwrap();
        assert_eq!(phase_token_in_line(&regex, "aXc"), Some("X"));
    }

    #[test]
    fn phase_token_in_line_returns_first_match_only() {
        let regex = Regex::new("X").unwrap();
        assert_eq!(phase_token_in_line(&regex, "XX"), Some("X"));
    }

    #[test]
    fn phase_token_in_line_does_not_trim_or_modify_input() {
        let regex = Regex::new("X").unwrap();
        assert_eq!(phase_token_in_line(&regex, "  X  "), Some("X"));
    }

    #[test]
    fn phase_token_in_line_returns_slice_from_input() {
        let regex = Regex::new("X").unwrap();
        let line = String::from("aXc");

        let token = phase_token_in_line(&regex, &line).unwrap();

        let line_ptr = line.as_ptr() as usize;
        let tok_ptr = token.as_ptr() as usize;
        assert!(tok_ptr >= line_ptr && tok_ptr < line_ptr + line.len());
    }

    #[test]
    fn phase_token_in_line_empty_line_returns_none() {
        let regex = Regex::new("X").unwrap();
        assert_eq!(phase_token_in_line(&regex, ""), None);
    }

    #[test]
    fn phase_token_in_line_full_line_match() {
        let regex = Regex::new(".*").unwrap();
        assert_eq!(phase_token_in_line(&regex, "abc"), Some("abc"));
    }

    #[tokio::test]
    async fn profile_invalid_regex_returns_error() {
        let mut profiler = joule_profiler();
        let config = ProfileConfig {
            cmd: vec!["echo".to_string()],
            token_pattern: "[[invalid[[[regex[[".to_string(),
            stdout_file: None,
            use_root: false,
            init_timeout: Duration::from_secs(1),
        };
        profiler.add_source(MockMetricReader::new());
        let result = profiler.profile(&config).await;
        assert!(matches!(result, Err(JouleProfilerError::InvalidPattern(_))));
    }

    #[test]
    fn create_output_sink_none_returns_stdout_sink() {
        assert!(create_output_sink(None).is_ok());
    }

    #[test]
    fn create_output_sink_with_path_creates_file() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("out.txt").to_str().unwrap().to_owned();

        let result = create_output_sink(Some(&path));
        assert!(result.is_ok());
        assert!(fs::metadata(&path).is_ok());
    }

    #[test]
    fn create_output_sink_invalid_path_returns_error() {
        let result = create_output_sink(Some(&"/nonexistent/dir/out.txt".to_string()));
        assert!(result.is_err());
        assert!(matches!(
            result.err().unwrap(),
            JouleProfilerError::OutputFileCreationFailed(_)
        ));
    }

    #[test]
    fn spawn_profiled_command_with_valid_command() {
        let config = create_test_config(vec!["echo".to_string(), "hello".to_string()]);

        let result = spawn_profiled_command(&config);

        assert!(result.is_ok());
        let mut child = result.unwrap();

        assert!(child.stdout.is_some());

        let _ = child.kill();
        let _ = child.wait();
    }

    #[test]
    fn spawn_profiled_command_with_nonexistent_command() {
        let config = create_test_config(vec!["mais_t_es_pas_la_mais_t_es_ou".to_string()]);

        let result = spawn_profiled_command(&config);

        assert!(result.is_err());
        match result.unwrap_err() {
            JouleProfilerError::CommandNotFound(cmd) => {
                assert_eq!(cmd, "mais_t_es_pas_la_mais_t_es_ou");
            }
            _ => panic!("Expected CommandNotFound error"),
        }
    }

    #[test]
    fn spawn_profiled_command_with_single_arg() {
        let config = create_test_config(vec!["echo".to_string()]);

        let result = spawn_profiled_command(&config);

        assert!(result.is_ok());
        let mut child = result.unwrap();
        let _ = child.kill();
        let _ = child.wait();
    }

    #[test]
    fn spawn_profiled_command_with_multiple_args() {
        let config = create_test_config(vec![
            "echo".to_string(),
            "help".to_string(),
            "me".to_string(),
            "plz".to_string(),
        ]);

        let result = spawn_profiled_command(&config);

        assert!(result.is_ok());
        let mut child = result.unwrap();

        let mut output = String::new();
        if let Some(mut stdout) = child.stdout.take() {
            stdout.read_to_string(&mut output).unwrap();
        }

        assert!(output.contains("help"));
        assert!(output.contains("me"));
        assert!(output.contains("plz"));

        let _ = child.wait();
    }

    #[cfg(unix)]
    #[test]
    fn spawn_profiled_command_permission_denied() {
        use std::os::unix::fs::PermissionsExt;

        let temp_dir = TempDir::new().unwrap();
        let script_path = temp_dir.path().join("no_exec.sh");

        fs::write(&script_path, "#!/bin/sh\necho test").unwrap();
        let mut perms = fs::metadata(&script_path).unwrap().permissions();
        perms.set_mode(0o644); // rw-r--r--
        fs::set_permissions(&script_path, perms).unwrap();

        let config = create_test_config(vec![script_path.to_string_lossy().to_string()]);

        let result = spawn_profiled_command(&config);

        assert!(result.is_err());
        match result.unwrap_err() {
            JouleProfilerError::CommandExecutionFailed(_) => (),
            _ => panic!("Expected CommandExecutionFailed error"),
        }
    }

    #[test]
    fn wait_for_child_exit_zero_on_success() {
        let config = create_test_config(vec!["true".to_string()]);
        let mut child = spawn_profiled_command(&config).unwrap();
        assert_eq!(wait_for_child_exit(&mut child).unwrap(), 0);
    }

    #[test]
    fn wait_for_child_exit_nonzero_on_failure() {
        let config = create_test_config(vec!["false".to_string()]);
        let mut child = spawn_profiled_command(&config).unwrap();
        assert_ne!(wait_for_child_exit(&mut child).unwrap(), 0);
    }

    #[test]
    fn list_sensors_no_sources_returns_empty() {
        let mut profiler = joule_profiler();
        let sensors = profiler.list_sensors().unwrap();
        assert!(sensors.is_empty());
    }

    #[tokio::test]
    async fn run_with_no_source_returns_no_source_configured_error() {
        let config = create_test_config(vec!["test".to_string()]);
        assert!(matches!(
            joule_profiler().profile(&config).await,
            Err(JouleProfilerError::NoSourceConfigured)
        ));
    }
}