alumet 0.8.0

Modular framework for hardware and software measurement (including energy consumption and more).
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
//! Building a set of plugins with their configuration options.

use std::collections::BTreeMap;

use anyhow::{anyhow, Context};

use crate::plugin::PluginMetadata;

/// Creates a [`Vec`] containing [`PluginMetadata`] for static plugins.
///
/// Each argument must be a _type_ that implements the [`AlumetPlugin`](crate::plugin::rust::AlumetPlugin) trait.
///
/// # Example
/// ```ignore
/// use alumet::plugin::PluginMetadata;
///
/// let plugins: Vec<PluginMetadata> = static_plugins![PluginA, PluginB];
/// ```
///
/// Attributes are supported:
/// ```ignore
/// use alumet::plugin::PluginMetadata;
///
/// let plugins = static_plugins![
///     #[cfg(feature = "some-feature")]
///     ConditionalPlugin
/// ];
/// ```
#[macro_export]
macro_rules! static_plugins {
    // ```
    // static_plugins![MyPluginA, ...];
    // ```
    //
    // desugars to:
    // ```
    // let plugins = vec![PluginMetadata::from_static::<MyPlugin>(), ...]
    // ```
    [] => {
        Vec::<$crate::plugin::PluginMetadata>::new()
    };
    [$( $(#[$m:meta])* $x:path ),+ $(,)?] => {
    //  ^^^^^^^^^^^^^^ accepts zero or more #[attribute]
        {
            vec![
                $(
                    $(#[$m])* // expands the attributes, if any
                    $crate::plugin::PluginMetadata::from_static::<$x>(),
                )*
            ] as Vec<$crate::plugin::PluginMetadata>
        }
    }
}

/// Information about a plugin that has not been created yet
/// (i.e. [`PluginMetadata::init`] has not been called).
pub struct PluginInfo {
    pub metadata: PluginMetadata,
    pub enabled: bool,
    pub config: Option<toml::Table>,
}

/// A set of non-created plugins, with their metadata and configuration.
///
/// The order of the plugins is preserved: they are stored in the same order as they are added to the set.
pub struct PluginSet(BTreeMap<String, PluginInfo>);

/// Filters plugins based on their status.
pub enum PluginFilter {
    /// Matches enabled plugins.
    Enabled,
    /// Matches disabled plugins.
    Disabled,
    /// Matches all plugins.
    Any,
}

/// How to react when the config contains an unknown plugin.
pub enum UnknownPluginInConfigPolicy {
    /// Logs a warning message and continues.
    LogWarn,
    /// Returns an error.
    Error,
    /// Ignores the plugin config and continues.
    Ignore,
}

impl PluginInfo {
    fn new(metadata: PluginMetadata) -> Self {
        Self {
            metadata,
            enabled: true,
            config: None,
        }
    }
}

impl From<Vec<PluginMetadata>> for PluginSet {
    /// Creates a new plugin set from their metadata.
    ///
    /// Every plugin is marked as enabled. No configuration is attached to the plugins.
    fn from(metadata: Vec<PluginMetadata>) -> Self {
        let map = BTreeMap::from_iter(metadata.into_iter().map(|m| (m.name.clone(), PluginInfo::new(m))));
        Self(map)
    }
}

impl PluginSet {
    /// Creates a new empty plugin set.
    pub fn new() -> Self {
        Self(BTreeMap::new())
    }

    /// Enables the specified plugins and disables all the others.
    ///
    /// Plugins that are not in the set are ignored.
    pub fn enable_only(&mut self, plugin_names: &[impl AsRef<str>]) {
        // We disable every plugin and re-enable only the ones we are interested in.
        // Cost: O(P+E) where P is the number of plugins in the set and E the size of `plugin_names`,
        // assuming that the cost of one lookup in the set is 1.
        for p in self.0.values_mut() {
            p.enabled = false;
        }
        for p in plugin_names {
            self.set_plugin_enabled(p.as_ref(), true);
        }
    }

    /// Extracts the config of each plugin.
    ///
    /// Use `on_unknown` to choose what to do when the config mentions a plugin that is not in the plugin set.
    ///
    /// # Enabling/disabling plugins
    /// If `update_status` is true, enable/disable the plugins according to the configuration.
    /// Plugins that are present in the config are enabled, those that are not present are disabled.
    /// If the configuration of a plugin contains an `enabled` or `enable` boolean key, its value determines
    /// whether the plugin is enabled or not.
    pub fn extract_config(
        &mut self,
        global_config: &mut toml::Table,
        update_status: bool,
        on_unknown: UnknownPluginInConfigPolicy,
    ) -> anyhow::Result<()> {
        // Disable every plugin first.
        if update_status {
            for plugin_info in self.0.values_mut() {
                plugin_info.enabled = false;
            }
        }

        // Extract the config and enable the plugins that it contains.
        let extracted = super::config::extract_plugins_config(global_config).context("invalid config")?;
        for (plugin_name, (enabled, config)) in extracted {
            if let Some(plugin_info) = self.0.get_mut(&plugin_name) {
                if update_status {
                    plugin_info.enabled = enabled;
                }
                plugin_info.config = Some(config);
            } else {
                match on_unknown {
                    UnknownPluginInConfigPolicy::LogWarn => {
                        log::warn!("unknown plugin '{plugin_name}' in configuration")
                    }
                    UnknownPluginInConfigPolicy::Error => {
                        return Err(anyhow!("unknown plugin '{plugin_name}' in configuration"))
                    }
                    UnknownPluginInConfigPolicy::Ignore => {
                        // do nothing
                    }
                }
            }
        }
        Ok(())
    }

    /// Gets the information about a non-initialized plugin.
    pub fn get_plugin(&self, plugin_name: &str) -> Option<&PluginInfo> {
        self.0.get(plugin_name)
    }

    /// Gets the information about a non-initialized plugin.
    pub fn get_plugin_mut(&mut self, plugin_name: &str) -> Option<&mut PluginInfo> {
        self.0.get_mut(plugin_name)
    }

    /// Checks if a plugin is enabled.
    ///
    /// If the plugin is not in the set, returns `false`.
    pub fn is_plugin_enabled(&self, plugin_name: &str) -> bool {
        self.0.get(plugin_name).map(|p| p.enabled).unwrap_or(false)
    }

    /// Enables or disables a plugin.
    pub fn set_plugin_enabled(&mut self, plugin_name: &str, enabled: bool) {
        if let Some(plugin) = self.0.get_mut(plugin_name) {
            plugin.enabled = enabled;
        }
    }

    /// Adds a new plugin to the set.
    ///
    /// The plugin is not initialized yet.
    pub fn add_plugin(&mut self, plugin: PluginInfo) {
        self.0.insert(plugin.metadata.name.clone(), plugin);
    }

    /// Adds multiple un-initialized plugins to the set.
    pub fn add_plugins(&mut self, plugins: Vec<PluginInfo>) {
        self.0.extend(plugins.into_iter().map(|p| (p.metadata.name.clone(), p)));
    }

    /// Iterates on the metadata of the plugins that match the given status filter.
    pub fn metadata(&self, filter: PluginFilter) -> impl Iterator<Item = &PluginMetadata> {
        self.0
            .values()
            .filter_map(move |p| if filter.accept(p) { Some(&p.metadata) } else { None })
    }

    /// Consumes the set and returns two lists: the enabled plugins,
    /// and the disabled plugins.
    pub fn into_partition(self) -> (Vec<PluginInfo>, Vec<PluginInfo>) {
        // (enabled, disabled)
        self.0.into_values().partition(|p| p.enabled)
    }

    /// Collects the plugins to a `Vec<PluginMetadata>`, filtered by status.
    pub fn into_metadata(self, filter: PluginFilter) -> Vec<PluginMetadata> {
        self.0
            .into_values()
            .filter(|p| filter.accept(p))
            .map(|p| p.metadata)
            .collect()
    }
}

impl PluginFilter {
    /// Checks if a plugin matches this filter.
    fn accept(&self, p: &PluginInfo) -> bool {
        match self {
            PluginFilter::Enabled => p.enabled,
            PluginFilter::Disabled => !p.enabled,
            PluginFilter::Any => true,
        }
    }
}

#[cfg(test)]
mod tests {
    use serde::Serialize;

    use crate::plugin::{
        rust::{serialize_config, AlumetPlugin},
        AlumetPluginStart, ConfigTable,
    };

    mod macros {
        use super::MyPlugin;

        #[test]
        fn static_plugins_macro() {
            let a = static_plugins![MyPlugin];
            let b = static_plugins![MyPlugin,];
            let empty = static_plugins![];
            assert_eq!(1, a.len());
            assert_eq!(1, b.len());
            assert_eq!(a[0].name, b[0].name);
            assert_eq!(a[0].version, b[0].version);
            assert!(empty.is_empty());
        }

        #[test]
        fn static_plugins_macro_with_attributes() {
            let single = static_plugins![
                #[cfg(test)]
                MyPlugin,
            ];
            assert_eq!(1, single.len());

            let empty = static_plugins![
                #[cfg(not(test))]
                MyPlugin
            ];
            assert_eq!(0, empty.len());

            let multiple = static_plugins![
                #[cfg(test)]
                MyPlugin,
                #[cfg(not(test))]
                MyPlugin,
                #[cfg(test)]
                MyPlugin
            ];
            assert_eq!(2, multiple.len());
        }
    }

    mod plugin_set {
        use toml::toml;

        use crate::plugin::rust::AlumetPlugin;

        use super::super::{PluginInfo, PluginMetadata, PluginSet, UnknownPluginInConfigPolicy};
        use super::MyPlugin;

        fn plugin_set() -> PluginSet {
            let mut set = PluginSet::new();
            set.add_plugin(PluginInfo {
                metadata: PluginMetadata::from_static::<MyPlugin>(),
                enabled: false,
                config: None,
            });
            assert_eq!(set.0.len(), 1);
            assert!(set.get_plugin(MyPlugin::name()).is_some());
            set
        }

        #[test]
        fn extract_config_no_update() {
            let mut set = plugin_set();
            let mut global_config = toml! {
                global = 0

                [plugins.name]
                n = 123
            };

            // extract with `update_status=false`, the plugin should not be enabled yet.
            set.extract_config(&mut global_config, false, UnknownPluginInConfigPolicy::Error)
                .expect("config should be valid");
            // check that the plugin's config has been moved to its PluginInfo
            let plugin_info = set.get_plugin("name").unwrap();
            assert_eq!(
                plugin_info
                    .config
                    .as_ref()
                    .expect("plugin config should be set")
                    .get("n"),
                Some(&toml::Value::Integer(123))
            );
            // check that the plugin is NOT enabled
            assert!(!plugin_info.enabled);
            assert!(!set.is_plugin_enabled("name"));
            // check that the plugins' configs are no longer in the global config
            assert!(global_config.get("plugins").is_none());
        }

        #[test]
        fn extract_config_update_status_implicitly_enabled() {
            let mut set = plugin_set();
            let mut global_config = toml! {
                global = 0

                [plugins.name]
                n = 123
            };

            // extract with `update_status=true`, the plugin should be enabled
            set.extract_config(&mut global_config, true, UnknownPluginInConfigPolicy::Error)
                .expect("config should be valid");
            let plugin_info = set.get_plugin("name").unwrap();
            assert_eq!(
                plugin_info
                    .config
                    .as_ref()
                    .expect("plugin config should be set")
                    .get("n"),
                Some(&toml::Value::Integer(123))
            );
            assert!(plugin_info.enabled);
            assert!(set.is_plugin_enabled("name"));
        }

        #[test]
        fn extract_config_update_status_implicitly_disabled() {
            let mut set = plugin_set();
            let mut global_config = toml! {
                global = 0
            };

            // extract with `update_status=true`, the plugin should be enabled
            set.extract_config(&mut global_config, true, UnknownPluginInConfigPolicy::Error)
                .expect("config should be valid");
            let plugin_info = set.get_plugin("name").unwrap();
            assert!(plugin_info.config.is_none(), "plugin should have no config");
            assert!(!plugin_info.enabled);
            assert!(!set.is_plugin_enabled("name"));
        }

        #[test]
        fn extract_config_update_status_explicitly_disabled() {
            let mut set = plugin_set();
            let mut global_config = toml! {
                global = 0

                [plugins.name]
                enabled = false
                n = 123
            };

            // extract with `update_status=true`, the plugin should be enabled
            set.extract_config(&mut global_config, true, UnknownPluginInConfigPolicy::Error)
                .expect("config should be valid");
            let plugin_info = set.get_plugin("name").unwrap();
            assert_eq!(
                plugin_info
                    .config
                    .as_ref()
                    .expect("plugin config should be set")
                    .get("n"),
                Some(&toml::Value::Integer(123))
            );
            assert!(!plugin_info.enabled);
            assert!(!set.is_plugin_enabled("name"));
        }

        #[test]
        fn extract_config_update_status_explicitly_enabled() {
            let mut set = plugin_set();
            let mut global_config = toml! {
                global = 0

                [plugins.name]
                enabled = true
                n = 123
            };

            // extract with `update_status=true`, the plugin should be enabled
            set.extract_config(&mut global_config, true, UnknownPluginInConfigPolicy::Error)
                .expect("config should be valid");
            let plugin_info = set.get_plugin("name").unwrap();
            assert_eq!(
                plugin_info
                    .config
                    .as_ref()
                    .expect("plugin config should be set")
                    .get("n"),
                Some(&toml::Value::Integer(123))
            );
            assert!(plugin_info.enabled);
            assert!(set.is_plugin_enabled("name"));
        }
    }

    struct MyPlugin;
    impl AlumetPlugin for MyPlugin {
        fn name() -> &'static str {
            "name"
        }

        fn version() -> &'static str {
            "version"
        }

        fn init(_config: ConfigTable) -> anyhow::Result<Box<Self>> {
            todo!()
        }

        fn start(&mut self, _alumet: &mut AlumetPluginStart) -> anyhow::Result<()> {
            todo!()
        }

        fn stop(&mut self) -> anyhow::Result<()> {
            todo!()
        }

        fn default_config() -> anyhow::Result<Option<ConfigTable>> {
            let config = serialize_config(MyPluginConfig::default())?;
            Ok(Some(config))
        }
    }

    #[derive(Serialize)]
    struct MyPluginConfig {
        list: Vec<String>,
        count: u32,
    }

    impl Default for MyPluginConfig {
        fn default() -> Self {
            Self {
                list: vec![String::from("default-item")],
                count: 42,
            }
        }
    }
}