blob: e1c14c9df73ce900c67ba18258a6560dfbc2e38d [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Will Bradley333d22c2019-09-11 15:04:59 -06002# 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 Klein3271bc72022-09-14 16:05:06 -06007import os
8
Will Bradley333d22c2019-09-11 15:04:59 -06009from chromite.lib import commandline
Alex Kleinaef41942022-04-19 14:13:17 -060010from chromite.lib import metrics_lib
Will Bradley333d22c2019-09-11 15:04:59 -060011from chromite.lib import portage_util
Lizzy Presland1754b602022-06-01 23:47:35 +000012from chromite.utils import pformat
13
14
Alex Klein1699fab2022-09-08 08:46:06 -060015_PARTITION_NAMES = ["rootfs", "stateful"]
Will Bradley333d22c2019-09-11 15:04:59 -060016
17
Will Bradley333d22c2019-09-11 15:04:59 -060018def _get_parser():
Alex Klein1699fab2022-09-08 08:46:06 -060019 """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 Bradley333d22c2019-09-11 15:04:59 -060044
45
Alex Klein1699fab2022-09-08 08:46:06 -060046def 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 Klein1699fab2022-09-08 08:46:06 -060051 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 Klein3271bc72022-09-14 16:05:06 -060063
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 Klein1699fab2022-09-08 08:46:06 -060068
Alex Kleinaef41942022-04-19 14:13:17 -060069 metrics_lib.append_metrics_log(
Alex Klein8718c032022-04-19 14:06:03 -060070 timestamp,
Alex Klein1699fab2022-09-08 08:46:06 -060071 "total_size.%s.%s" % (image_type, partition_name),
Alex Kleinaef41942022-04-19 14:13:17 -060072 metrics_lib.OP_GAUGE,
Alex Klein1699fab2022-09-08 08:46:06 -060073 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 Bradley333d22c2019-09-11 15:04:59 -060082
83
84def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060085 """Find and report approximate size info for a particular built package."""
86 commandline.RunInsideChroot()
Will Bradley333d22c2019-09-11 15:04:59 -060087
Alex Klein1699fab2022-09-08 08:46:06 -060088 parser = _get_parser()
89 opts = parser.parse_args(argv)
90 opts.Freeze()
Will Bradley333d22c2019-09-11 15:04:59 -060091
Alex Klein1699fab2022-09-08 08:46:06 -060092 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 Bradley333d22c2019-09-11 15:04:59 -060099
Alex Klein1699fab2022-09-08 08:46:06 -0600100 if opts.packages:
101 installed_packages = portage_util.GenerateInstalledPackages(
102 db, opts.root, opts.packages
103 )
104 else:
105 installed_packages = db.InstalledPackages()
Will Bradley333d22c2019-09-11 15:04:59 -0600106
Alex Klein1699fab2022-09-08 08:46:06 -0600107 results = generate_package_size_report(
108 db, opts.root, opts.image_type, opts.partition_name, installed_packages
109 )
110 print(pformat.json(results))