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
pub mod gpu_controller;
pub mod hw_mon;
pub mod sysfs;

#[cfg(all(test, feature = "mock"))]
mod tests {
    use crate::gpu_controller::{GpuController, PerformanceLevel};
    use crate::sysfs::SysFS;
    use tempfile::tempdir;
    use tokio::fs;

    #[tokio::test]
    async fn mock() {
        let mockfs = MockSysFS::init().await;

        let gpu_controller = GpuController::new_from_path(mockfs.get_path().to_path_buf())
            .await
            .expect("Failed to create GPU controller");

        assert_eq!(gpu_controller.get_driver().await, "mock");

        assert_eq!(gpu_controller.get_pci_id(), Some(("1002", "67DF")));

        assert_eq!(gpu_controller.get_pci_subsys_id(), Some(("1DA2", "E387")));

        assert_eq!(gpu_controller.get_busy_percent().await, Some(100));

        assert_eq!(
            gpu_controller.get_total_vram().await,
            Some(512 * 1024 * 1024)
        );

        assert_eq!(
            gpu_controller.get_used_vram().await,
            Some(256 * 1024 * 1024)
        );

        assert_eq!(
            gpu_controller.get_vbios_version().await,
            Some("MOCKFS-VBIOS".to_string())
        );

        assert_eq!(
            gpu_controller.get_power_force_performance_level().await,
            Some(PerformanceLevel::Auto)
        );

        let hw_mon = gpu_controller.hw_monitors.first().unwrap();

        assert_eq!(hw_mon.get_fan_pwm().await, Some(255));

        assert_eq!(hw_mon.get_fan_current().await, Some(1600));
        assert_eq!(hw_mon.get_fan_target().await, Some(1600));

        assert_eq!(hw_mon.get_fan_max().await, Some(3200));
        assert_eq!(hw_mon.get_fan_min().await, Some(0));

        let temperatures = hw_mon.get_temps().await;

        assert_eq!(temperatures["edge"].current, Some(44.0));
        assert_eq!(temperatures["edge"].crit, Some(94.0));
        assert_eq!(temperatures["edge"].crit_hyst, Some(-273.150));
    }

    #[derive(Debug)]
    struct MockSysFS {
        temp_dir: tempfile::TempDir,
    }

    impl SysFS for MockSysFS {
        fn get_path(&self) -> &std::path::Path {
            self.temp_dir.path()
        }
    }

    impl MockSysFS {
        pub async fn init() -> Self {
            let temp_dir = tempdir().expect("Failed to create temp dir");

            let path = temp_dir.path().to_path_buf();

            std::fs::create_dir_all(&path).expect("Failed to create mock path");

            let mock = Self { temp_dir };

            mock.write_file("uevent", "DRIVER=mock\nPCI_ID=1002:67DF\nPCI_SUBSYS_ID=1DA2:E387")
                .await
                .unwrap();

            mock.write_file("gpu_busy_percent", "100").await.unwrap();

            mock.write_file("mem_info_vram_total", (512 * 1024 * 1024).to_string())
                .await
                .unwrap();

            mock.write_file("mem_info_vram_used", (256 * 1024 * 1024).to_string())
                .await
                .unwrap();

            mock.write_file("vbios_version", "MOCKFS-VBIOS")
                .await
                .unwrap();

            mock.write_file("power_dpm_force_performance_level", "auto")
                .await
                .unwrap();

            let hw_mon_path = path.join("hwmon/hwmon1");

            fs::create_dir_all(hw_mon_path).await.unwrap();

            mock.write_file("hwmon/hwmon1/name", "mock").await.unwrap();

            mock.write_file("hwmon/hwmon1/pwm1", "255").await.unwrap();

            mock.write_file("hwmon/hwmon1/fan1_max", "3200")
                .await
                .unwrap();
            mock.write_file("hwmon/hwmon1/fan1_min", "0").await.unwrap();

            mock.write_file("hwmon/hwmon1/fan1_input", "1600")
                .await
                .unwrap();
            mock.write_file("hwmon/hwmon1/fan1_target", "1600")
                .await
                .unwrap();

            mock.write_file("hwmon/hwmon1/temp1_label", "edge")
                .await
                .unwrap();

            mock.write_file("hwmon/hwmon1/temp1_input", "44000")
                .await
                .unwrap();
            mock.write_file("hwmon/hwmon1/temp1_crit", "94000")
                .await
                .unwrap();
            mock.write_file("hwmon/hwmon1/temp1_crit_hyst", "-273150")
                .await
                .unwrap();

            mock
        }
    }
}