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
13 from mozparsers
import parse_histograms
15 banner
= """/* This file is auto-generated, see gen_histogram_phf.py. */
19 #ifndef mozilla_TelemetryHistogramNameMap_h
20 #define mozilla_TelemetryHistogramNameMap_h
22 #include "mozilla/PerfectHash.h"
29 } // namespace mozilla
30 } // namespace Telemetry
31 #endif // mozilla_TelemetryHistogramNameMap_h
35 def main(output
, *filenames
):
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.
45 histograms
= list(parse_histograms
.from_files(filenames
))
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")
54 (bytearray(hist
.name(), "ascii"), idx
) for (idx
, hist
) in enumerate(histograms
)
56 name_phf
= PerfectHash(histograms
, PHFSIZE
)
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()",
72 if __name__
== "__main__":
73 main(sys
.stdout
, *sys
.argv
[1:])