diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /toolkit/components/telemetry/build_scripts/gen_histogram_phf.py | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/telemetry/build_scripts/gen_histogram_phf.py')
-rw-r--r-- | toolkit/components/telemetry/build_scripts/gen_histogram_phf.py | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/toolkit/components/telemetry/build_scripts/gen_histogram_phf.py b/toolkit/components/telemetry/build_scripts/gen_histogram_phf.py new file mode 100644 index 0000000000..b817695d87 --- /dev/null +++ b/toolkit/components/telemetry/build_scripts/gen_histogram_phf.py @@ -0,0 +1,74 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +from __future__ import print_function +from mozparsers.shared_telemetry_utils import ParserError +from perfecthash import PerfectHash + +PHFSIZE = 1024 + +from mozparsers import parse_histograms +import sys +import buildconfig + + +banner = """/* This file is auto-generated, see gen_histogram_phf.py. */ +""" + +header = """ +#ifndef mozilla_TelemetryHistogramNameMap_h +#define mozilla_TelemetryHistogramNameMap_h + +#include "mozilla/PerfectHash.h" + +namespace mozilla { +namespace Telemetry { +""" + +footer = """ +} // namespace mozilla +} // namespace Telemetry +#endif // mozilla_TelemetryHistogramNameMap_h +""" + + +def main(output, *filenames): + """ + Generate a Perfect Hash Table for the Histogram name -> Histogram ID lookup. + The table is immutable once generated and we can avoid any dynamic memory allocation. + """ + + output.write(banner) + output.write(header) + + try: + histograms = list(parse_histograms.from_files(filenames)) + histograms = [ + h for h in histograms if h.record_on_os(buildconfig.substs["OS_TARGET"]) + ] + except ParserError as ex: + print("\nError processing histograms:\n" + str(ex) + "\n") + sys.exit(1) + + histograms = [ + (bytearray(hist.name(), "ascii"), idx) for (idx, hist) in enumerate(histograms) + ] + name_phf = PerfectHash(histograms, PHFSIZE) + + output.write( + name_phf.cxx_codegen( + name="HistogramIDByNameLookup", + entry_type="uint32_t", + lower_entry=lambda x: str(x[1]), + key_type="const nsACString&", + key_bytes="aKey.BeginReading()", + key_length="aKey.Length()", + ) + ) + + output.write(footer) + + +if __name__ == "__main__": + main(sys.stdout, *sys.argv[1:]) |