energy_model_config/
lib.rs

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
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use anyhow::{ensure, Context, Error};
use serde_derive::Deserialize;
use std::fs::File;
use std::io::Read as _;
use std::path::Path;
use zx_types::{
    zx_cpu_set_t, zx_processor_power_domain_t, zx_processor_power_level_t,
    zx_processor_power_level_transition_t, ZX_CPU_SET_BITS_PER_WORD, ZX_CPU_SET_MAX_CPUS,
    ZX_MAX_NAME_LEN, ZX_PROCESSOR_POWER_CONTROL_ARM_PSCI, ZX_PROCESSOR_POWER_CONTROL_ARM_WFI,
    ZX_PROCESSOR_POWER_CONTROL_CPU_DRIVER, ZX_PROCESSOR_POWER_CONTROL_RISCV_SBI,
    ZX_PROCESSOR_POWER_CONTROL_RISCV_WFI, ZX_PROCESSOR_POWER_LEVEL_OPTIONS_DOMAIN_INDEPENDENT,
};

/// This library is used to parse a processor energy model JSON file into a data structure which
/// also implements some convenience methods for accessing and consuming the data.
///
/// The intended usage is that `EnergyModel::new()` is called with an energy model JSON file path.
/// If successful, the function returns an EnergyModel instance containing the parsed config data.
///
/// The parser expects a JSON5 file of the following format:
///     [
///         {
///             power_domain: {
///                 cpu_set: [
///                     1,
///                     2,
///                     3,
///                 ],
///                 domain_id: 0,
///             },
///             power_levels: [
///                 {
///                     processing_rate: 200,
///                     power_coefficient_nw: 100,
///                     control_interface: 'CpuDriver',
///                     control_argument: 0,
///                     diagnostic_name: 'Pstate 0',
///                 },
///                 {
///                     processing_rate: 100,
///                     power_coefficient_nw: 50,
///                     control_interface: 'CpuDriver',
///                     control_argument: 0,
///                     diagnostic_name: 'Pstate 1',
///                 },
///             ],
///             power_level_transitions: [
///                 {
///                     from: 0,
///                     to: 1,
///                     duration_ns: 100,
///                     energy_nj: 100,
///                 },
///             ],
///         },
///         {
///             power_domain: {
///                 cpu_set: [
///                     0,
///                     4,
///                 ],
///                 domain_id: 1,
///             },
///             power_levels: {
///                 option: 'DomainIndependent',
///                 processing_rate: 200,
///                 power_coefficient_nw: 200,
///                 control_interface: 'CpuDriver',
///                 control_argument: 0,
///                 diagnostic_name: 'Pstate 0',
///             },
///             power_level_transitions: [
///             ],
///         },
///     ]

/// Defines a Json compatible energy model struct of a specific set of CPUs.
#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct PowerLevelDomainJson {
    // Represents CPUs that are a subset of a domain.
    pub power_domain: PowerDomain,

    // Power levels are implicitly enumerated as the indices of `power_levels`.
    pub power_levels: Vec<PowerLevel>,

    // There are no ordering restrictions on `power_level_transitions` and the
    // power levels they encode correspond to those defined by `power_levels`.
    // An absent transition between levels is assumed to indicate that there
    // is no energy or latency cost borne by performing it.
    pub power_level_transitions: Vec<PowerLevelTransition>,
}

#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct PowerDomain {
    // A vector of CPU indexes.
    pub cpu_set: Vec<u16>,
    // A unique identifier for all CPUs in the cpu_set.
    pub domain_id: u32,
}

#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct PowerLevel {
    pub option: Option<PowerLevelOption>,
    pub processing_rate: u64,
    pub power_coefficient_nw: u64,
    pub control_interface: ControlInterface,
    pub control_argument: u64,
    pub diagnostic_name: String,
}

#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct PowerLevelTransition {
    pub from: u8,
    pub to: u8,
    pub duration_ns: i64,
    pub energy_nj: u64,
}

#[derive(Deserialize, Debug, Clone, PartialEq)]
pub enum ControlInterface {
    CpuDriver,
    ArmPsci,
    ArmWfi,
    RiscvSbi,
    RiscvWfi,
}

#[derive(Deserialize, Debug, Clone, PartialEq)]
pub enum PowerLevelOption {
    DomainIndependent,
}

impl PowerLevelDomainJson {
    pub fn validate(&self) -> Result<(), Error> {
        let power_level_num = self.power_levels.len();
        for power_level_transition in self.power_level_transitions.iter() {
            let from_index = power_level_transition.from as usize;
            let to_index = power_level_transition.to as usize;
            ensure!(
                from_index != to_index
                    && from_index < power_level_num
                    && to_index < power_level_num,
                "Power level transition need to be between two different valid indexes",
            );
            let from_processing_rate = self.power_levels[from_index].processing_rate;
            let to_processing_rate = self.power_levels[to_index].processing_rate;
            ensure!(
                from_processing_rate != 0 || to_processing_rate != 0,
                "Transitions directly from one idle power level to another idle power level are invalid",
            );
        }
        Ok(())
    }
}

/// Represents the top level of an energy model structure.
#[derive(Debug, Clone, PartialEq)]
pub struct EnergyModel(pub Vec<PowerLevelDomain>);

/// Zircon compatible version of PowerLevelDomainJson.
#[derive(Debug, Clone, PartialEq)]
pub struct PowerLevelDomain {
    pub power_domain: zx_processor_power_domain_t,
    pub power_levels: Vec<zx_processor_power_level_t>,
    pub power_level_transitions: Vec<zx_processor_power_level_transition_t>,
}

impl EnergyModel {
    pub fn new(json_file_path: &Path) -> Result<EnergyModel, Error> {
        let mut buffer = String::new();
        File::open(&json_file_path)?.read_to_string(&mut buffer)?;

        let power_level_domains = serde_json5::from_str::<Vec<PowerLevelDomainJson>>(&buffer)?;

        // Iterate and validate each underlying PowerLevelDomain instance.
        for power_level_domain in power_level_domains.iter() {
            power_level_domain.validate().context(format!(
                "Validation failed for power_domain {}",
                power_level_domain.power_domain.domain_id
            ))?;
        }

        Ok(Self(
            power_level_domains
                .into_iter()
                .map(|s| s.try_into())
                .collect::<Result<Vec<PowerLevelDomain>, _>>()?,
        ))
    }
}

impl TryFrom<PowerLevelDomainJson> for PowerLevelDomain {
    type Error = anyhow::Error;
    fn try_from(e: PowerLevelDomainJson) -> Result<Self, Error> {
        Ok(Self {
            power_domain: e.power_domain.try_into()?,
            power_levels: e
                .power_levels
                .into_iter()
                .map(|s| s.try_into())
                .collect::<Result<Vec<zx_processor_power_level_t>, _>>()?,
            power_level_transitions: e
                .power_level_transitions
                .into_iter()
                .map(|s| s.into())
                .collect(),
        })
    }
}

impl TryFrom<PowerDomain> for zx_processor_power_domain_t {
    type Error = anyhow::Error;
    fn try_from(p: PowerDomain) -> Result<Self, Error> {
        let mut ret = Self::default();
        ret.cpus = get_cpu_mask(p.cpu_set)?;
        ret.domain_id = p.domain_id;
        Ok(ret)
    }
}

impl From<PowerLevelTransition> for zx_processor_power_level_transition_t {
    fn from(p: PowerLevelTransition) -> Self {
        let mut ret = Self::default();
        ret.from = p.from;
        ret.to = p.to;
        ret.latency = p.duration_ns;
        ret.energy = p.energy_nj;
        ret
    }
}

impl TryFrom<PowerLevel> for zx_processor_power_level_t {
    type Error = anyhow::Error;
    fn try_from(p: PowerLevel) -> Result<Self, Error> {
        let control_interface = match p.control_interface {
            ControlInterface::CpuDriver => ZX_PROCESSOR_POWER_CONTROL_CPU_DRIVER,
            ControlInterface::ArmPsci => ZX_PROCESSOR_POWER_CONTROL_ARM_PSCI,
            ControlInterface::ArmWfi => ZX_PROCESSOR_POWER_CONTROL_ARM_WFI,
            ControlInterface::RiscvSbi => ZX_PROCESSOR_POWER_CONTROL_RISCV_SBI,
            ControlInterface::RiscvWfi => ZX_PROCESSOR_POWER_CONTROL_RISCV_WFI,
        };

        let options = match p.option {
            Some(PowerLevelOption::DomainIndependent) => {
                ZX_PROCESSOR_POWER_LEVEL_OPTIONS_DOMAIN_INDEPENDENT
            }
            _ => 0,
        };

        let bytes = p.diagnostic_name.as_bytes();
        ensure!(
            bytes.len() <= ZX_MAX_NAME_LEN,
            "diagnostic_name {:?} must be shorter than {:?} bytes",
            p.diagnostic_name,
            ZX_MAX_NAME_LEN,
        );
        let mut diagnostic_name = [0u8; ZX_MAX_NAME_LEN];
        diagnostic_name[..bytes.len()].copy_from_slice(bytes);

        let mut ret = Self::default();
        ret.options = options;
        ret.processing_rate = p.processing_rate;
        ret.power_coefficient_nw = p.power_coefficient_nw;
        ret.control_interface = control_interface;
        ret.control_argument = p.control_argument;
        ret.diagnostic_name = diagnostic_name;
        Ok(ret)
    }
}

// Convert from a list of CPU indexes to CPU mask.
//
// Zircon definition:
// The |N|'th CPU is considered in the CPU set if the bit:
//
//   cpu_mask[N / ZX_CPU_SET_BITS_PER_WORD]
//       & (1 << (N % ZX_CPU_SET_BITS_PER_WORD))
//
// is set.
fn get_cpu_mask(cpu_set: Vec<u16>) -> Result<zx_cpu_set_t, anyhow::Error> {
    let mut cpu_mask = [0 as u64; ZX_CPU_SET_MAX_CPUS / ZX_CPU_SET_BITS_PER_WORD];
    for cpu in cpu_set {
        let cpu_index = cpu as usize;
        ensure!(
            cpu_index < ZX_CPU_SET_MAX_CPUS,
            "CPU index must be smaller than {:?}",
            ZX_CPU_SET_MAX_CPUS,
        );

        let i = cpu_index / ZX_CPU_SET_BITS_PER_WORD;
        cpu_mask[i] = cpu_mask[i] | (1 << (cpu_index % ZX_CPU_SET_BITS_PER_WORD));
    }
    Ok(zx_cpu_set_t { mask: cpu_mask })
}

#[cfg(test)]
mod tests {
    use crate::*;
    use assert_matches::assert_matches;

    #[test]
    fn test_convert_cpu_mask() {
        assert_eq!(
            get_cpu_mask(vec![0u16, 1u16, 2u16, 3u16]).unwrap().mask,
            [15u64, 0u64, 0u64, 0u64, 0u64, 0u64, 0u64, 0u64]
        );

        fn is_nth_set(
            cpu_mask: [u64; ZX_CPU_SET_MAX_CPUS / ZX_CPU_SET_BITS_PER_WORD],
            n: usize,
        ) -> bool {
            cpu_mask[n / ZX_CPU_SET_BITS_PER_WORD] & (1 << (n % ZX_CPU_SET_BITS_PER_WORD)) != 0
        }

        let mut cpu_set = vec![15u16];
        let mut cpu_mask = get_cpu_mask(cpu_set).unwrap().mask;
        assert!(is_nth_set(cpu_mask, 15));

        cpu_set = vec![32u16];
        cpu_mask = get_cpu_mask(cpu_set).unwrap().mask;
        assert!(is_nth_set(cpu_mask, 32));

        // CPU index must be smaller than ZX_CPU_SET_MAX_CPUS.
        cpu_set = vec![ZX_CPU_SET_MAX_CPUS as u16];
        assert_matches!(get_cpu_mask(cpu_set), Err(_));
    }

    #[test]
    fn test_processor_power_level() {
        let power_level = PowerLevel {
            option: Some(PowerLevelOption::DomainIndependent),
            processing_rate: 0,
            power_coefficient_nw: 0,
            control_interface: ControlInterface::ArmPsci,
            control_argument: 0,
            diagnostic_name:
                "This is a very very very long diagnositc name which exceeds 32 bytes.".to_string(),
        };
        assert_matches!(zx_processor_power_level_t::try_from(power_level), Err(_));

        let mut zx_power_level = zx_processor_power_level_t::default();
        zx_power_level.control_interface = ZX_PROCESSOR_POWER_CONTROL_CPU_DRIVER;
        let power_level = PowerLevel {
            option: None,
            processing_rate: 0,
            power_coefficient_nw: 0,
            control_interface: ControlInterface::CpuDriver,
            control_argument: 0,
            diagnostic_name: "".to_string(),
        };

        assert!(zx_processor_power_level_t::try_from(power_level).unwrap() == zx_power_level);
    }
}