Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """The Package Size Reporting CLI entry point.""" |
| 6 | |
Alex Klein | 3271bc7 | 2022-09-14 16:05:06 -0600 | [diff] [blame^] | 7 | import os |
| 8 | |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 9 | from chromite.lib import commandline |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame] | 10 | from chromite.lib import metrics_lib |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 11 | from chromite.lib import portage_util |
Lizzy Presland | 1754b60 | 2022-06-01 23:47:35 +0000 | [diff] [blame] | 12 | from chromite.utils import pformat |
| 13 | |
| 14 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 15 | _PARTITION_NAMES = ["rootfs", "stateful"] |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 16 | |
| 17 | |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 18 | def _get_parser(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 19 | """Create an argument parser for this script.""" |
| 20 | parser = commandline.ArgumentParser(description=__doc__) |
| 21 | parser.add_argument( |
| 22 | "--root", |
| 23 | required=True, |
| 24 | type="path", |
| 25 | help="Specify the rootfs to investigate.", |
| 26 | ) |
| 27 | parser.add_argument( |
| 28 | "--image-type", |
| 29 | help="Specify the type of image being investigated. " |
| 30 | "e.g. [base, dev, test]", |
| 31 | ) |
| 32 | parser.add_argument( |
| 33 | "--partition-name", |
| 34 | choices=_PARTITION_NAMES, |
| 35 | help="Specify the partition name. " "e.g. [rootfs, stateful]", |
| 36 | ) |
| 37 | parser.add_argument( |
| 38 | "packages", |
| 39 | nargs="*", |
| 40 | help="Names of packages to investigate. Must be " |
| 41 | "specified as category/package-version.", |
| 42 | ) |
| 43 | return parser |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 44 | |
| 45 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 46 | def generate_package_size_report( |
| 47 | db, root, image_type, partition_name, installed_packages |
| 48 | ): |
| 49 | """Collect package sizes and generate a report.""" |
| 50 | results = {} |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 51 | package_sizes = portage_util.GeneratePackageSizes( |
| 52 | db, root, installed_packages |
| 53 | ) |
| 54 | timestamp = metrics_lib.current_milli_time() |
| 55 | for package_cpv, size in package_sizes: |
| 56 | results[package_cpv] = size |
| 57 | metrics_lib.append_metrics_log( |
| 58 | timestamp, |
| 59 | "package_size.%s.%s.%s" % (image_type, partition_name, package_cpv), |
| 60 | metrics_lib.OP_GAUGE, |
| 61 | arg=size, |
| 62 | ) |
Alex Klein | 3271bc7 | 2022-09-14 16:05:06 -0600 | [diff] [blame^] | 63 | |
| 64 | rootfs_stat = os.statvfs(root) |
| 65 | block_size = rootfs_stat.f_bsize |
| 66 | blocks_used = rootfs_stat.f_blocks - rootfs_stat.f_bfree |
| 67 | total_size = block_size * blocks_used |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 68 | |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame] | 69 | metrics_lib.append_metrics_log( |
Alex Klein | 8718c03 | 2022-04-19 14:06:03 -0600 | [diff] [blame] | 70 | timestamp, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 71 | "total_size.%s.%s" % (image_type, partition_name), |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame] | 72 | metrics_lib.OP_GAUGE, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 73 | arg=total_size, |
| 74 | ) |
| 75 | return { |
| 76 | "root": root, |
| 77 | "package_sizes": results, |
| 78 | "total_size": total_size, |
| 79 | "package_database_path": db.db_path, |
| 80 | "package_install_path": db.package_install_path, |
| 81 | } |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 82 | |
| 83 | |
| 84 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 85 | """Find and report approximate size info for a particular built package.""" |
| 86 | commandline.RunInsideChroot() |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 87 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 88 | parser = _get_parser() |
| 89 | opts = parser.parse_args(argv) |
| 90 | opts.Freeze() |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 91 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 92 | vdb = package_install_path = "" |
| 93 | if opts.partition_name == "stateful": |
| 94 | vdb = "var_overlay/db/pkg" |
| 95 | package_install_path = "dev_image" |
| 96 | db = portage_util.PortageDB( |
| 97 | root=opts.root, vdb=vdb, package_install_path=package_install_path |
| 98 | ) |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 99 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 100 | if opts.packages: |
| 101 | installed_packages = portage_util.GenerateInstalledPackages( |
| 102 | db, opts.root, opts.packages |
| 103 | ) |
| 104 | else: |
| 105 | installed_packages = db.InstalledPackages() |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 106 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 107 | results = generate_package_size_report( |
| 108 | db, opts.root, opts.image_type, opts.partition_name, installed_packages |
| 109 | ) |
| 110 | print(pformat.json(results)) |