Backed out changeset f594e6f00208 (bug 1940883) for causing crashes in bug 1941164.
[gecko.git] / toolkit / components / telemetry / build_scripts / gen_histogram_phf.py
blob38c724550675eacce767a8540e6afc4a258a4093
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 from mozparsers.shared_telemetry_utils import ParserError
6 from perfecthash import PerfectHash
8 PHFSIZE = 1024
10 import sys
12 import buildconfig
13 from mozparsers import parse_histograms
15 banner = """/* This file is auto-generated, see gen_histogram_phf.py. */
16 """
18 header = """
19 #ifndef mozilla_TelemetryHistogramNameMap_h
20 #define mozilla_TelemetryHistogramNameMap_h
22 #include "mozilla/PerfectHash.h"
24 namespace mozilla {
25 namespace Telemetry {
26 """
28 footer = """
29 } // namespace mozilla
30 } // namespace Telemetry
31 #endif // mozilla_TelemetryHistogramNameMap_h
32 """
35 def main(output, *filenames):
36 """
37 Generate a Perfect Hash Table for the Histogram name -> Histogram ID lookup.
38 The table is immutable once generated and we can avoid any dynamic memory allocation.
39 """
41 output.write(banner)
42 output.write(header)
44 try:
45 histograms = list(parse_histograms.from_files(filenames))
46 histograms = [
47 h for h in histograms if h.record_on_os(buildconfig.substs["OS_TARGET"])
49 except ParserError as ex:
50 print("\nError processing histograms:\n" + str(ex) + "\n")
51 sys.exit(1)
53 histograms = [
54 (bytearray(hist.name(), "ascii"), idx) for (idx, hist) in enumerate(histograms)
56 name_phf = PerfectHash(histograms, PHFSIZE)
58 output.write(
59 name_phf.cxx_codegen(
60 name="HistogramIDByNameLookup",
61 entry_type="uint32_t",
62 lower_entry=lambda x: str(x[1]),
63 key_type="const nsACString&",
64 key_bytes="aKey.BeginReading()",
65 key_length="aKey.Length()",
69 output.write(footer)
72 if __name__ == "__main__":
73 main(sys.stdout, *sys.argv[1:])