below_dump/
system.rs

1// Copyright (c) Facebook, Inc. and its affiliates.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use super::*;
16
17pub struct System {
18    opts: GeneralOpt,
19    fields: Vec<SystemField>,
20}
21
22impl System {
23    pub fn new(opts: &GeneralOpt, fields: Vec<SystemField>) -> Self {
24        Self {
25            opts: opts.to_owned(),
26            fields,
27        }
28    }
29}
30
31impl Dumper for System {
32    fn dump_model(
33        &self,
34        ctx: &CommonFieldContext,
35        model: &model::Model,
36        output: &mut dyn Write,
37        round: &mut usize,
38        comma_flag: bool,
39    ) -> Result<IterExecResult> {
40        let mut fields = self.fields.clone();
41
42        if self.opts.detail || self.opts.everything {
43            // If detail is set, add per-cpu fields.
44            // The fields need to be added at runtime because we cannot know the number of CPUs in the model statically.
45            for key in model.system.cpus.keys() {
46                for subquery_id in
47                    &enum_iterator::all::<model::SingleCpuModelFieldId>().collect::<Vec<_>>()
48                {
49                    let value = subquery_id.clone();
50                    fields.push(DumpField::FieldId(model::SystemModelFieldId::Cpus(
51                        model::BTreeMapFieldId::new(Some(*key), value),
52                    )));
53                }
54            }
55        }
56
57        match self.opts.output_format {
58            Some(OutputFormat::Raw) | None => write!(
59                output,
60                "{}",
61                print::dump_raw(
62                    &fields,
63                    ctx,
64                    &model.system,
65                    *round,
66                    self.opts.repeat_title,
67                    self.opts.disable_title,
68                    self.opts.raw
69                )
70            )?,
71            Some(OutputFormat::Csv) => write!(
72                output,
73                "{}",
74                print::dump_csv(
75                    &fields,
76                    ctx,
77                    &model.system,
78                    *round,
79                    self.opts.disable_title,
80                    self.opts.raw
81                )
82            )?,
83            Some(OutputFormat::Tsv) => write!(
84                output,
85                "{}",
86                print::dump_tsv(
87                    &fields,
88                    ctx,
89                    &model.system,
90                    *round,
91                    self.opts.disable_title,
92                    self.opts.raw
93                )
94            )?,
95            Some(OutputFormat::KeyVal) => write!(
96                output,
97                "{}",
98                print::dump_kv(&fields, ctx, &model.system, self.opts.raw)
99            )?,
100            Some(OutputFormat::Json) => {
101                let par = print::dump_json(&fields, ctx, &model.system, self.opts.raw);
102                if comma_flag {
103                    write!(output, ",{}", par)?;
104                } else {
105                    write!(output, "{}", par)?;
106                }
107            }
108            Some(OutputFormat::OpenMetrics) => write!(
109                output,
110                "{}",
111                print::dump_openmetrics(&fields, ctx, &model.system)
112            )?,
113        };
114
115        *round += 1;
116
117        Ok(IterExecResult::Success)
118    }
119}