flowey_lib_hvlite/_jobs/
check_openvmm_hcl_size.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Compares the size of the OpenHCL binary in the current PR with the size of the binary from the last successful merge to main.
5
6use crate::artifact_openhcl_igvm_from_recipe_extras;
7use crate::build_openhcl_igvm_from_recipe;
8use crate::build_openhcl_igvm_from_recipe::OpenhclIgvmRecipe;
9use crate::build_openvmm_hcl;
10use crate::build_openvmm_hcl::OpenvmmHclBuildParams;
11use crate::build_openvmm_hcl::OpenvmmHclBuildProfile::OpenvmmHclShip;
12use crate::run_cargo_build::common::CommonArch;
13use crate::run_cargo_build::common::CommonTriple;
14use flowey::node::prelude::*;
15use flowey_lib_common::download_gh_artifact;
16use flowey_lib_common::gh_workflow_id;
17use flowey_lib_common::git_merge_commit;
18
19flowey_request! {
20    pub struct Request {
21        pub target: CommonTriple,
22        pub done: WriteVar<SideEffect>,
23        pub pipeline_name: String,
24    }
25}
26
27new_simple_flow_node!(struct Node);
28
29impl SimpleFlowNode for Node {
30    type Request = Request;
31
32    fn imports(ctx: &mut ImportCtx<'_>) {
33        ctx.import::<crate::build_xtask::Node>();
34        ctx.import::<crate::git_checkout_openvmm_repo::Node>();
35        ctx.import::<download_gh_artifact::Node>();
36        ctx.import::<git_merge_commit::Node>();
37        ctx.import::<gh_workflow_id::Node>();
38        ctx.import::<build_openhcl_igvm_from_recipe::Node>();
39        ctx.import::<build_openvmm_hcl::Node>();
40        ctx.import::<artifact_openhcl_igvm_from_recipe_extras::publish::Node>();
41    }
42
43    fn process_request(request: Self::Request, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
44        let Request {
45            target,
46            done,
47            pipeline_name,
48        } = request;
49
50        let xtask = ctx.reqv(|v| crate::build_xtask::Request {
51            target: target.clone(),
52            xtask: v,
53        });
54        let openvmm_repo_path = ctx.reqv(crate::git_checkout_openvmm_repo::req::GetRepoDir);
55
56        let built_openvmm_hcl = ctx.reqv(|v| build_openvmm_hcl::Request {
57            build_params: OpenvmmHclBuildParams {
58                target: target.clone(),
59                profile: OpenvmmHclShip,
60                features: (OpenhclIgvmRecipe::X64)
61                    .recipe_details(true)
62                    .openvmm_hcl_features,
63                no_split_dbg_info: false,
64            },
65            openvmm_hcl_output: v,
66        });
67
68        let file_name = match target.common_arch().unwrap() {
69            CommonArch::X86_64 => "x64-openhcl-baseline",
70            CommonArch::Aarch64 => "aarch64-openhcl-baseline",
71        };
72
73        let merge_commit = ctx.reqv(|v| git_merge_commit::Request {
74            repo_path: openvmm_repo_path.clone(),
75            merge_commit: v,
76            base_branch: "main".into(),
77        });
78
79        let merge_run = ctx.reqv(|v| gh_workflow_id::Request {
80            repo_path: openvmm_repo_path.clone(),
81            github_commit_hash: merge_commit,
82            gh_workflow: v,
83            pipeline_name,
84        });
85
86        let run_id = merge_run.map(ctx, |r| r.id);
87        let merge_head_artifact = ctx.reqv(|old_openhcl| download_gh_artifact::Request {
88            repo_owner: "microsoft".into(),
89            repo_name: "openvmm".into(),
90            file_name: file_name.into(),
91            path: old_openhcl,
92            run_id,
93        });
94
95        // Publish the built binary as an artifact for offline analysis.
96        //
97        // FUTURE: Flowey should have a general mechanism for this. We cannot
98        // use the existing artifact support because all artifacts are only
99        // published at the end of the job, if everything else succeeds.
100        let publish_artifact = if ctx.backend() == FlowBackend::Github {
101            let dir = ctx.emit_rust_stepv("collect openvmm_hcl files for analysis", |ctx| {
102                let built_openvmm_hcl = built_openvmm_hcl.clone().claim(ctx);
103                move |rt| {
104                    let built_openvmm_hcl = rt.read(built_openvmm_hcl);
105                    let path = Path::new("artifact");
106                    fs_err::create_dir_all(path)?;
107                    fs_err::copy(built_openvmm_hcl.bin, path.join("openvmm_hcl"))?;
108                    if let Some(dbg) = built_openvmm_hcl.dbg {
109                        fs_err::copy(dbg, path.join("openvmm_hcl.dbg"))?;
110                    }
111                    Ok(path
112                        .absolute()?
113                        .into_os_string()
114                        .into_string()
115                        .ok()
116                        .unwrap())
117                }
118            });
119            let name = format!(
120                "{}_openvmm_hcl_for_size_analysis",
121                target.common_arch().unwrap().as_arch()
122            );
123            Some(
124                ctx.emit_gh_step(
125                    "publish openvmm_hcl for analysis",
126                    "actions/upload-artifact@v4",
127                )
128                .with("name", name)
129                .with("path", dir)
130                .finish(ctx),
131            )
132        } else {
133            None
134        };
135
136        let comparison = ctx.emit_rust_step("binary size comparison", |ctx| {
137            // Ensure the artifact is published before the analysis since this step may fail.
138            let _publish_artifact = publish_artifact.claim(ctx);
139            let xtask = xtask.claim(ctx);
140            let openvmm_repo_path = openvmm_repo_path.claim(ctx);
141            let old_openhcl = merge_head_artifact.claim(ctx);
142            let new_openhcl = built_openvmm_hcl.claim(ctx);
143            let merge_run = merge_run.claim(ctx);
144
145            move |rt| {
146                let xtask = match rt.read(xtask) {
147                    crate::build_xtask::XtaskOutput::LinuxBin { bin, .. } => bin,
148                    crate::build_xtask::XtaskOutput::WindowsBin { exe, .. } => exe,
149                };
150
151                let old_openhcl = rt.read(old_openhcl);
152                let new_openhcl = rt.read(new_openhcl);
153                let merge_run = rt.read(merge_run);
154
155                let old_path = old_openhcl.join(file_name).join("openhcl");
156                let new_path = new_openhcl.bin;
157
158                println!(
159                    "comparing HEAD to merge commit {} and workflow {}",
160                    merge_run.commit, merge_run.id
161                );
162
163                let sh = xshell::Shell::new()?;
164                sh.change_dir(rt.read(openvmm_repo_path));
165                xshell::cmd!(
166                    sh,
167                    "{xtask} verify-size --original {old_path} --new {new_path}"
168                )
169                .run()?;
170
171                Ok(())
172            }
173        });
174
175        ctx.emit_side_effect_step(vec![comparison], [done]);
176
177        Ok(())
178    }
179}