зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1276190 - Add a script to generate headers with scalar data from Scalars.yaml. r=gfritzsche
This commit is contained in:
Родитель
4c8bdc2f7f
Коммит
4bccd54fad
|
@ -0,0 +1,26 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
|
||||
/* 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/. */
|
||||
|
||||
#ifndef TelemetryScalarInfo_h__
|
||||
#define TelemetryScalarInfo_h__
|
||||
|
||||
// This module is internal to Telemetry. It defines a structure that holds the
|
||||
// scalar info. It should only be used by TelemetryScalarData.h automatically
|
||||
// generated file and TelemetryScalar.cpp. This should not be used anywhere else.
|
||||
// For the public interface to Telemetry functionality, see Telemetry.h.
|
||||
|
||||
namespace {
|
||||
struct ScalarInfo {
|
||||
uint32_t kind;
|
||||
uint32_t name_offset;
|
||||
uint32_t expiration_offset;
|
||||
uint32_t dataset;
|
||||
|
||||
const char *name() const;
|
||||
const char *expiration() const;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
#endif // TelemetryScalarInfo_h__
|
|
@ -0,0 +1,63 @@
|
|||
# This file contains a definition of the scalar probes that are recorded in Telemetry.
|
||||
# They are submitted with the "main" pings and can be inspected in about:telemetry.
|
||||
|
||||
# The following section is for probes testing the Telemetry system. They will not be
|
||||
# submitted in pings and are only used for testing.
|
||||
telemetry.test:
|
||||
unsigned_int_kind:
|
||||
bug_numbers:
|
||||
- 1276190
|
||||
description: >
|
||||
This is a test uint type with a really long description, maybe spanning even multiple
|
||||
lines, to just prove a point: everything works just fine.
|
||||
expires: never
|
||||
kind: uint
|
||||
notification_emails:
|
||||
- telemetry-client-dev@mozilla.com
|
||||
|
||||
string_kind:
|
||||
bug_numbers:
|
||||
- 1276190
|
||||
description: A string test type with a one line comment that works just fine!
|
||||
expires: never
|
||||
kind: string
|
||||
notification_emails:
|
||||
- telemetry-client-dev@mozilla.com
|
||||
|
||||
expired:
|
||||
bug_numbers:
|
||||
- 1276190
|
||||
description: This is an expired testing scalar; not meant to be touched.
|
||||
expires: 4.0a1
|
||||
kind: uint
|
||||
notification_emails:
|
||||
- telemetry-client-dev@mozilla.com
|
||||
|
||||
unexpired:
|
||||
bug_numbers:
|
||||
- 1276190
|
||||
description: This is an unexpired testing scalar; not meant to be touched.
|
||||
expires: "375.0"
|
||||
kind: uint
|
||||
notification_emails:
|
||||
- telemetry-client-dev@mozilla.com
|
||||
|
||||
release_optin:
|
||||
bug_numbers:
|
||||
- 1276190
|
||||
description: A testing scalar; not meant to be touched.
|
||||
expires: never
|
||||
kind: uint
|
||||
notification_emails:
|
||||
- telemetry-client-dev@mozilla.com
|
||||
release_channel_collection: opt-in
|
||||
|
||||
release_optout:
|
||||
bug_numbers:
|
||||
- 1276190
|
||||
description: A testing scalar; not meant to be touched.
|
||||
expires: never
|
||||
kind: uint
|
||||
notification_emails:
|
||||
- telemetry-client-dev@mozilla.com
|
||||
release_channel_collection: opt-out
|
|
@ -6,6 +6,7 @@
|
|||
# in a file provided as a command-line argument.
|
||||
|
||||
from __future__ import print_function
|
||||
from shared_telemetry_utils import StringTable, static_assert
|
||||
|
||||
import sys
|
||||
import histogram_tools
|
||||
|
@ -13,48 +14,6 @@ import itertools
|
|||
|
||||
banner = """/* This file is auto-generated, see gen-histogram-data.py. */
|
||||
"""
|
||||
|
||||
# Write out the gHistograms array.
|
||||
|
||||
class StringTable:
|
||||
def __init__(self):
|
||||
self.current_index = 0;
|
||||
self.table = {}
|
||||
|
||||
def c_strlen(self, string):
|
||||
return len(string) + 1
|
||||
|
||||
def stringIndex(self, string):
|
||||
if string in self.table:
|
||||
return self.table[string]
|
||||
else:
|
||||
result = self.current_index
|
||||
self.table[string] = result
|
||||
self.current_index += self.c_strlen(string)
|
||||
return result
|
||||
|
||||
def writeDefinition(self, f, name):
|
||||
entries = self.table.items()
|
||||
entries.sort(key=lambda x:x[1])
|
||||
# Avoid null-in-string warnings with GCC and potentially
|
||||
# overlong string constants; write everything out the long way.
|
||||
def explodeToCharArray(string):
|
||||
def toCChar(s):
|
||||
if s == "'":
|
||||
return "'\\''"
|
||||
else:
|
||||
return "'%s'" % s
|
||||
return ", ".join(map(toCChar, string))
|
||||
f.write("const char %s[] = {\n" % name)
|
||||
for (string, offset) in entries[:-1]:
|
||||
e = explodeToCharArray(string)
|
||||
if e:
|
||||
f.write(" /* %5d */ %s, '\\0',\n"
|
||||
% (offset, explodeToCharArray(string)))
|
||||
else:
|
||||
f.write(" /* %5d */ '\\0',\n" % offset)
|
||||
f.write(" /* %5d */ %s, '\\0' };\n\n"
|
||||
% (entries[-1][1], explodeToCharArray(entries[-1][0])))
|
||||
|
||||
def print_array_entry(output, histogram, name_index, exp_index):
|
||||
cpp_guard = histogram.cpp_guard()
|
||||
|
@ -82,15 +41,12 @@ def write_histogram_table(output, histograms):
|
|||
table.writeDefinition(output, strtab_name)
|
||||
static_assert(output, "sizeof(%s) <= UINT32_MAX" % strtab_name,
|
||||
"index overflow")
|
||||
|
||||
|
||||
# Write out static asserts for histogram data. We'd prefer to perform
|
||||
# these checks in this script itself, but since several histograms
|
||||
# (generally enumerated histograms) use compile-time constants for
|
||||
# their upper bounds, we have to let the compiler do the checking.
|
||||
|
||||
def static_assert(output, expression, message):
|
||||
print("static_assert(%s, \"%s\");" % (expression, message), file=output)
|
||||
|
||||
def static_asserts_for_boolean(output, histogram):
|
||||
pass
|
||||
|
||||
|
|
|
@ -0,0 +1,89 @@
|
|||
# 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/.
|
||||
|
||||
# Write out scalar information for C++. The scalars are defined
|
||||
# in a file provided as a command-line argument.
|
||||
|
||||
from __future__ import print_function
|
||||
from shared_telemetry_utils import StringTable, static_assert
|
||||
|
||||
import parse_scalars
|
||||
import sys
|
||||
|
||||
# The banner/text at the top of the generated file.
|
||||
banner = """/* This file is auto-generated, only for internal use in TelemetryScalar.h,
|
||||
see gen-scalar-data.py. */
|
||||
"""
|
||||
|
||||
file_header = """\
|
||||
#ifndef mozilla_TelemetryScalarData_h
|
||||
#define mozilla_TelemetryScalarData_h
|
||||
#include "ScalarInfo.h"
|
||||
namespace {
|
||||
"""
|
||||
|
||||
file_footer = """\
|
||||
} // namespace
|
||||
#endif // mozilla_TelemetryScalarData_h
|
||||
"""
|
||||
|
||||
def write_scalar_info(scalar, output, name_index, expiration_index):
|
||||
"""Writes a scalar entry to the output file.
|
||||
|
||||
:param scalar: a ScalarType instance describing the scalar.
|
||||
:param output: the output stream.
|
||||
:param name_index: the index of the scalar name in the strings table.
|
||||
:param expiration_index: the index of the expiration version in the strings table.
|
||||
"""
|
||||
cpp_guard = scalar.cpp_guard
|
||||
if cpp_guard:
|
||||
print("#if defined(%s)" % cpp_guard, file=output)
|
||||
|
||||
print(" {{ {}, {}, {}, {} }},"\
|
||||
.format(scalar.nsITelemetry_kind,
|
||||
name_index,
|
||||
expiration_index,
|
||||
scalar.dataset),
|
||||
file=output)
|
||||
|
||||
if cpp_guard:
|
||||
print("#endif", file=output)
|
||||
|
||||
def write_scalar_tables(scalars, output):
|
||||
"""Writes the scalar and strings tables to an header file.
|
||||
|
||||
:param scalars: a list of ScalarType instances describing the scalars.
|
||||
:param output: the output stream.
|
||||
"""
|
||||
string_table = StringTable()
|
||||
|
||||
print("const ScalarInfo gScalars[] = {", file=output)
|
||||
for s in scalars:
|
||||
# We add both the scalar label and the expiration string to the strings
|
||||
# table.
|
||||
name_index = string_table.stringIndex(s.label)
|
||||
exp_index = string_table.stringIndex(s.expires)
|
||||
# Write the scalar info entry.
|
||||
write_scalar_info(s, output, name_index, exp_index)
|
||||
print("};", file=output)
|
||||
|
||||
string_table_name = "gScalarsStringTable"
|
||||
string_table.writeDefinition(output, string_table_name)
|
||||
static_assert(output, "sizeof(%s) <= UINT32_MAX" % string_table_name,
|
||||
"index overflow")
|
||||
|
||||
def main(output, *filenames):
|
||||
# Load the scalars first.
|
||||
if len(filenames) > 1:
|
||||
raise Exception('We don\'t support loading from more than one file.')
|
||||
scalars = parse_scalars.load_scalars(filenames[0])
|
||||
|
||||
# Write the scalar data file.
|
||||
print(banner, file=output)
|
||||
print(file_header, file=output)
|
||||
write_scalar_tables(scalars, output)
|
||||
print(file_footer, file=output)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(sys.stdout, *sys.argv[1:])
|
|
@ -0,0 +1,56 @@
|
|||
# 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/.
|
||||
|
||||
# Write out a C++ enum definition whose members are the names of
|
||||
# scalar types.
|
||||
#
|
||||
# The scalars are defined in files provided as command-line arguments.
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import sys
|
||||
import parse_scalars
|
||||
|
||||
banner = """/* This file is auto-generated, see gen-scalar-enum.py. */
|
||||
"""
|
||||
|
||||
file_header = """\
|
||||
#ifndef mozilla_TelemetryScalarEnums_h
|
||||
#define mozilla_TelemetryScalarEnums_h
|
||||
namespace mozilla {
|
||||
namespace Telemetry {
|
||||
enum class ScalarID : uint32_t {\
|
||||
"""
|
||||
|
||||
file_footer = """\
|
||||
};
|
||||
} // namespace mozilla
|
||||
} // namespace Telemetry
|
||||
#endif // mozilla_TelemetryScalarEnums_h
|
||||
"""
|
||||
|
||||
def main(output, *filenames):
|
||||
# Load the scalars first.
|
||||
if len(filenames) > 1:
|
||||
raise Exception('We don\'t support loading from more than one file.')
|
||||
scalars = parse_scalars.load_scalars(filenames[0])
|
||||
|
||||
# Write the enum file.
|
||||
print(banner, file=output)
|
||||
print(file_header, file=output);
|
||||
|
||||
for s in scalars:
|
||||
cpp_guard = s.cpp_guard
|
||||
if cpp_guard:
|
||||
print("#if defined(%s)" % cpp_guard, file=output)
|
||||
print(" %s," % s.enum_label, file=output)
|
||||
if cpp_guard:
|
||||
print("#endif", file=output)
|
||||
|
||||
print(" ScalarCount,", file=output)
|
||||
|
||||
print(file_footer, file=output)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(sys.stdout, *sys.argv[1:])
|
|
@ -16,6 +16,7 @@ XPIDL_MODULE = 'telemetry'
|
|||
|
||||
EXPORTS.mozilla += [
|
||||
'!TelemetryHistogramEnums.h',
|
||||
'!TelemetryScalarEnums.h',
|
||||
'ProcessedStack.h',
|
||||
'Telemetry.h',
|
||||
'ThreadHangStats.h',
|
||||
|
@ -59,6 +60,8 @@ FINAL_LIBRARY = 'xul'
|
|||
GENERATED_FILES = [
|
||||
'TelemetryHistogramData.inc',
|
||||
'TelemetryHistogramEnums.h',
|
||||
'TelemetryScalarData.h',
|
||||
'TelemetryScalarEnums.h',
|
||||
]
|
||||
|
||||
histogram_files = [
|
||||
|
@ -75,6 +78,19 @@ enums = GENERATED_FILES['TelemetryHistogramEnums.h']
|
|||
enums.script = 'gen-histogram-enum.py'
|
||||
enums.inputs = histogram_files
|
||||
|
||||
# Generate Scalars
|
||||
scalar_files = [
|
||||
'Scalars.yaml',
|
||||
]
|
||||
|
||||
scalar_data = GENERATED_FILES['TelemetryScalarData.h']
|
||||
scalar_data.script = 'gen-scalar-data.py'
|
||||
scalar_data.inputs = scalar_files
|
||||
|
||||
scalar_enums = GENERATED_FILES['TelemetryScalarEnums.h']
|
||||
scalar_enums.script = 'gen-scalar-enum.py'
|
||||
scalar_enums.inputs = scalar_files
|
||||
|
||||
DEFINES['MOZ_APP_VERSION'] = '"%s"' % CONFIG['MOZ_APP_VERSION']
|
||||
|
||||
LOCAL_INCLUDES += [
|
||||
|
|
|
@ -0,0 +1,255 @@
|
|||
# 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/.
|
||||
|
||||
import re
|
||||
import yaml
|
||||
from shared_telemetry_utils import add_expiration_postfix
|
||||
|
||||
# The map of containing the allowed scalar types and their mapping to
|
||||
# nsITelemetry::SCALAR_* type constants.
|
||||
SCALAR_TYPES_MAP = {
|
||||
'uint': 'nsITelemetry::SCALAR_COUNT',
|
||||
'string': 'nsITelemetry::SCALAR_STRING'
|
||||
}
|
||||
|
||||
class ScalarType:
|
||||
"""A class for representing a scalar definition."""
|
||||
|
||||
def __init__(self, group_name, probe_name, definition):
|
||||
# Validate and set the name, so we don't need to pass it to the other
|
||||
# validation functions.
|
||||
self.validate_names(group_name, probe_name)
|
||||
self._name = probe_name
|
||||
self._group_name = group_name
|
||||
|
||||
# Validating the scalar definition.
|
||||
self.validate_types(definition)
|
||||
self.validate_values(definition)
|
||||
|
||||
# Everything is ok, set the rest of the data.
|
||||
self._definition = definition
|
||||
definition['expires'] = add_expiration_postfix(definition['expires'])
|
||||
|
||||
def validate_names(self, group_name, probe_name):
|
||||
"""Validate the group and probe name:
|
||||
- Group name must be alpha-numeric + '.', no leading/trailing digit or '.'.
|
||||
- Probe name must be alpha-numeric + '_', no leading/trailing digit or '_'.
|
||||
|
||||
:param group_name: the name of the group the probe is in.
|
||||
:param probe_name: the name of the scalar probe.
|
||||
:raises ValueError: if the length of the names exceeds the limit or they don't
|
||||
conform our name specification.
|
||||
"""
|
||||
|
||||
# Enforce a maximum length on group and probe names.
|
||||
MAX_NAME_LENGTH = 40
|
||||
for n in [group_name, probe_name]:
|
||||
if len(n) > MAX_NAME_LENGTH:
|
||||
raise ValueError("Name '{}' exceeds maximum name length of {} characters."\
|
||||
.format(n, MAX_NAME_LENGTH))
|
||||
|
||||
def check_name(name, error_msg_prefix, allowed_char_regexp):
|
||||
# Check if we only have the allowed characters.
|
||||
chars_regxp = r'^[a-zA-Z0-9' + allowed_char_regexp + r']+$'
|
||||
if not re.search(chars_regxp, name):
|
||||
raise ValueError(error_msg_prefix + " name must be alpha-numeric. Got: '{}'".format(name))
|
||||
|
||||
# Don't allow leading/trailing digits, '.' or '_'.
|
||||
if re.search(r'(^[\d\._])|([\d\._])$', name):
|
||||
raise ValueError(error_msg_prefix +
|
||||
" name must not have a leading/trailing digit, a dot or underscore. Got: '{}'"\
|
||||
.format(name))
|
||||
|
||||
check_name(group_name, 'Group', r'\.')
|
||||
check_name(probe_name, 'Probe', r'_')
|
||||
|
||||
def validate_types(self, definition):
|
||||
"""This function performs some basic sanity checks on the scalar definition:
|
||||
- Checks that all the required fields are available.
|
||||
- Checks that all the fields have the expected types.
|
||||
|
||||
:param definition: the dictionary containing the scalar properties.
|
||||
:raises TypeError: if a scalar definition field is of the wrong type.
|
||||
:raise KeyError: if a required field is missing or unknown fields are present.
|
||||
"""
|
||||
|
||||
# The required and optional fields in a scalar type definition.
|
||||
REQUIRED_FIELDS = {
|
||||
'bug_numbers': list, # This contains ints. See LIST_FIELDS_CONTENT.
|
||||
'description': basestring,
|
||||
'expires': basestring,
|
||||
'kind': basestring,
|
||||
'notification_emails': list # This contains strings. See LIST_FIELDS_CONTENT.
|
||||
}
|
||||
|
||||
OPTIONAL_FIELDS = {
|
||||
'cpp_guard': basestring,
|
||||
'release_channel_collection': basestring
|
||||
}
|
||||
|
||||
# The types for the data within the fields that hold lists.
|
||||
LIST_FIELDS_CONTENT = {
|
||||
'bug_numbers': int,
|
||||
'notification_emails': basestring
|
||||
}
|
||||
|
||||
# Concatenate the required and optional field definitions.
|
||||
ALL_FIELDS = REQUIRED_FIELDS.copy()
|
||||
ALL_FIELDS.update(OPTIONAL_FIELDS)
|
||||
|
||||
# Checks that all the required fields are available.
|
||||
missing_fields = [f for f in REQUIRED_FIELDS.keys() if f not in definition]
|
||||
if len(missing_fields) > 0:
|
||||
raise KeyError(self._name + ' - missing required fields: ' + ', '.join(missing_fields))
|
||||
|
||||
# Do we have any unknown field?
|
||||
unknown_fields = [f for f in definition.keys() if f not in ALL_FIELDS]
|
||||
if len(unknown_fields) > 0:
|
||||
raise KeyError(self._name + ' - unknown fields: ' + ', '.join(unknown_fields))
|
||||
|
||||
# Checks the type for all the fields.
|
||||
wrong_type_names = ['{} must be {}'.format(f, ALL_FIELDS[f].__name__) \
|
||||
for f in definition.keys() if not isinstance(definition[f], ALL_FIELDS[f])]
|
||||
if len(wrong_type_names) > 0:
|
||||
raise TypeError(self._name + ' - ' + ', '.join(wrong_type_names))
|
||||
|
||||
# Check that the lists are not empty and that data in the lists
|
||||
# have the correct types.
|
||||
list_fields = [f for f in definition if isinstance(definition[f], list)]
|
||||
for field in list_fields:
|
||||
# Check for empty lists.
|
||||
if len(definition[field]) == 0:
|
||||
raise TypeError("Field '{}' for probe '{}' must not be empty."
|
||||
.format(field, self._name))
|
||||
# Check the type of the list content.
|
||||
broken_types =\
|
||||
[not isinstance(v, LIST_FIELDS_CONTENT[field]) for v in definition[field]]
|
||||
if any(broken_types):
|
||||
raise TypeError("Field '{}' for probe '{}' must only contain values of type {}"
|
||||
.format(field, self._name, LIST_FIELDS_CONTENT[field].__name__))
|
||||
|
||||
def validate_values(self, definition):
|
||||
"""This function checks that the fields have the correct values.
|
||||
|
||||
:param definition: the dictionary containing the scalar properties.
|
||||
:raises ValueError: if a scalar definition field contains an unexpected value.
|
||||
"""
|
||||
|
||||
# Validate the scalar kind.
|
||||
scalar_kind = definition.get('kind')
|
||||
if scalar_kind not in SCALAR_TYPES_MAP.keys():
|
||||
raise ValueError(self._name + ' - unknown scalar kind: ' + scalar_kind)
|
||||
|
||||
# Validate the collection policy.
|
||||
collection_policy = definition.get('release_channel_collection', None)
|
||||
if collection_policy and collection_policy not in ['opt-in', 'opt-out']:
|
||||
raise ValueError(self._name + ' - unknown collection policy: ' + collection_policy)
|
||||
|
||||
# Validate the cpp_guard.
|
||||
cpp_guard = definition.get('cpp_guard')
|
||||
if cpp_guard and re.match(r'\W', cpp_guard):
|
||||
raise ValueError(self._name + ' - invalid cpp_guard: ' + cpp_guard)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Get the scalar name"""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def label(self):
|
||||
"""Get the scalar label generated from the scalar and group names."""
|
||||
return self._group_name + '.' + self._name
|
||||
|
||||
@property
|
||||
def enum_label(self):
|
||||
"""Get the enum label generated from the scalar and group names. This is used to
|
||||
generate the enum tables."""
|
||||
|
||||
# The scalar name can contain informations about its hierarchy (e.g. 'a.b.scalar').
|
||||
# We can't have dots in C++ enums, replace them with an underscore. Also, make the
|
||||
# label upper case for consistency with the histogram enums.
|
||||
return self.label.replace('.', '_').upper()
|
||||
|
||||
@property
|
||||
def bug_numbers(self):
|
||||
"""Get the list of related bug numbers"""
|
||||
return self._definition['bug_numbers']
|
||||
|
||||
@property
|
||||
def description(self):
|
||||
"""Get the scalar description"""
|
||||
return self._definition['description']
|
||||
|
||||
@property
|
||||
def expires(self):
|
||||
"""Get the scalar expiration"""
|
||||
return self._definition['expires']
|
||||
|
||||
@property
|
||||
def kind(self):
|
||||
"""Get the scalar kind"""
|
||||
return self._definition['kind']
|
||||
|
||||
@property
|
||||
def nsITelemetry_kind(self):
|
||||
"""Get the scalar kind constant defined in nsITelemetry"""
|
||||
return SCALAR_TYPES_MAP.get(self.kind)
|
||||
|
||||
@property
|
||||
def notification_emails(self):
|
||||
"""Get the list of notification emails"""
|
||||
return self._definition['notification_emails']
|
||||
|
||||
@property
|
||||
def dataset(self):
|
||||
"""Get the nsITelemetry constant equivalent to the chose release channel collection
|
||||
policy for the scalar.
|
||||
"""
|
||||
# The collection policy is optional, but we still define a default
|
||||
# behaviour for it.
|
||||
release_channel_collection = \
|
||||
self._definition.get('release_channel_collection', 'opt-in')
|
||||
return 'nsITelemetry::' + ('DATASET_RELEASE_CHANNEL_OPTOUT' \
|
||||
if release_channel_collection == 'opt-out' else 'DATASET_RELEASE_CHANNEL_OPTIN')
|
||||
|
||||
@property
|
||||
def cpp_guard(self):
|
||||
"""Get the cpp guard for this scalar"""
|
||||
return self._definition.get('cpp_guard')
|
||||
|
||||
def load_scalars(filename):
|
||||
"""Parses a YAML file containing the scalar definition.
|
||||
|
||||
:param filename: the YAML file containing the scalars definition.
|
||||
:raises Exception: if the scalar file cannot be opened or parsed.
|
||||
"""
|
||||
|
||||
# Parse the scalar definitions from the YAML file.
|
||||
scalars = None
|
||||
try:
|
||||
with open(filename, 'r') as f:
|
||||
scalars = yaml.safe_load(f)
|
||||
except IOError, e:
|
||||
raise Exception('Error opening ' + filename + ': ' + e.message)
|
||||
except ValueError, e:
|
||||
raise Exception('Error parsing scalars in ' + filename + ': ' + e.message)
|
||||
|
||||
scalar_list = []
|
||||
|
||||
# Scalars are defined in a fixed two-level hierarchy within the definition file.
|
||||
# The first level contains the group name, while the second level contains the
|
||||
# probe name (e.g. "group.name: probe: ...").
|
||||
for group_name in scalars:
|
||||
group = scalars[group_name]
|
||||
|
||||
# Make sure that the group has at least one probe in it.
|
||||
if not group or len(group) == 0:
|
||||
raise ValueError(group_name + ' must have at least a probe in it')
|
||||
|
||||
for probe_name in group:
|
||||
# We found a scalar type. Go ahead and parse it.
|
||||
scalar_info = group[probe_name]
|
||||
scalar_list.append(ScalarType(group_name, probe_name, scalar_info))
|
||||
|
||||
return scalar_list
|
|
@ -0,0 +1,86 @@
|
|||
# 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/.
|
||||
|
||||
# This file contains utility functions shared by the scalars and the histogram generation
|
||||
# scripts.
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import re
|
||||
|
||||
class StringTable:
|
||||
"""Manages a string table and allows C style serialization to a file."""
|
||||
|
||||
def __init__(self):
|
||||
self.current_index = 0;
|
||||
self.table = {}
|
||||
|
||||
def c_strlen(self, string):
|
||||
"""The length of a string including the null terminating character.
|
||||
:param string: the input string.
|
||||
"""
|
||||
return len(string) + 1
|
||||
|
||||
def stringIndex(self, string):
|
||||
"""Returns the index in the table of the provided string. Adds the string to
|
||||
the table if it's not there.
|
||||
:param string: the input string.
|
||||
"""
|
||||
if string in self.table:
|
||||
return self.table[string]
|
||||
else:
|
||||
result = self.current_index
|
||||
self.table[string] = result
|
||||
self.current_index += self.c_strlen(string)
|
||||
return result
|
||||
|
||||
def writeDefinition(self, f, name):
|
||||
"""Writes the string table to a file as a C const char array.
|
||||
:param f: the output stream.
|
||||
:param name: the name of the output array.
|
||||
"""
|
||||
entries = self.table.items()
|
||||
entries.sort(key=lambda x:x[1])
|
||||
# Avoid null-in-string warnings with GCC and potentially
|
||||
# overlong string constants; write everything out the long way.
|
||||
def explodeToCharArray(string):
|
||||
def toCChar(s):
|
||||
if s == "'":
|
||||
return "'\\''"
|
||||
else:
|
||||
return "'%s'" % s
|
||||
return ", ".join(map(toCChar, string))
|
||||
f.write("const char %s[] = {\n" % name)
|
||||
for (string, offset) in entries[:-1]:
|
||||
e = explodeToCharArray(string)
|
||||
if e:
|
||||
f.write(" /* %5d */ %s, '\\0',\n"
|
||||
% (offset, explodeToCharArray(string)))
|
||||
else:
|
||||
f.write(" /* %5d */ '\\0',\n" % offset)
|
||||
f.write(" /* %5d */ %s, '\\0' };\n\n"
|
||||
% (entries[-1][1], explodeToCharArray(entries[-1][0])))
|
||||
|
||||
def static_assert(output, expression, message):
|
||||
"""Writes a C++ compile-time assertion expression to a file.
|
||||
:param output: the output stream.
|
||||
:param expression: the expression to check.
|
||||
:param message: the string literal that will appear if the expression evaluates to
|
||||
false.
|
||||
"""
|
||||
print("static_assert(%s, \"%s\");" % (expression, message), file=output)
|
||||
|
||||
def add_expiration_postfix(expiration):
|
||||
""" Formats the expiration version and adds a version postfix if needed.
|
||||
|
||||
:param expiration: the expiration version string.
|
||||
:return: the modified expiration string.
|
||||
"""
|
||||
if re.match(r'^[1-9][0-9]*$', expiration):
|
||||
return expiration + ".0a1"
|
||||
|
||||
if re.match(r'^[1-9][0-9]*\.0$', expiration):
|
||||
return expiration + "a1"
|
||||
|
||||
return expiration
|
Загрузка…
Ссылка в новой задаче