shared_telemetry_utils.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/.
  4. # This file contains utility functions shared by the scalars and the histogram generation
  5. # scripts.
  6. from __future__ import print_function
  7. import re
  8. class StringTable:
  9. """Manages a string table and allows C style serialization to a file."""
  10. def __init__(self):
  11. self.current_index = 0;
  12. self.table = {}
  13. def c_strlen(self, string):
  14. """The length of a string including the null terminating character.
  15. :param string: the input string.
  16. """
  17. return len(string) + 1
  18. def stringIndex(self, string):
  19. """Returns the index in the table of the provided string. Adds the string to
  20. the table if it's not there.
  21. :param string: the input string.
  22. """
  23. if string in self.table:
  24. return self.table[string]
  25. else:
  26. result = self.current_index
  27. self.table[string] = result
  28. self.current_index += self.c_strlen(string)
  29. return result
  30. def stringIndexes(self, strings):
  31. """ Returns a list of indexes for the provided list of strings.
  32. Adds the strings to the table if they are not in it yet.
  33. :param strings: list of strings to put into the table.
  34. """
  35. return [self.stringIndex(s) for s in strings]
  36. def writeDefinition(self, f, name):
  37. """Writes the string table to a file as a C const char array.
  38. This writes out the string table as one single C char array for memory
  39. size reasons, separating the individual strings with '\0' characters.
  40. This way we can index directly into the string array and avoid the additional
  41. storage costs for the pointers to them (and potential extra relocations for those).
  42. :param f: the output stream.
  43. :param name: the name of the output array.
  44. """
  45. entries = self.table.items()
  46. entries.sort(key=lambda x:x[1])
  47. # Avoid null-in-string warnings with GCC and potentially
  48. # overlong string constants; write everything out the long way.
  49. def explodeToCharArray(string):
  50. def toCChar(s):
  51. if s == "'":
  52. return "'\\''"
  53. else:
  54. return "'%s'" % s
  55. return ", ".join(map(toCChar, string))
  56. f.write("const char %s[] = {\n" % name)
  57. for (string, offset) in entries:
  58. if "*/" in string:
  59. raise ValueError, "String in string table contains unexpected sequence '*/': %s" % string
  60. e = explodeToCharArray(string)
  61. if e:
  62. f.write(" /* %5d - \"%s\" */ %s, '\\0',\n"
  63. % (offset, string, explodeToCharArray(string)))
  64. else:
  65. f.write(" /* %5d - \"%s\" */ '\\0',\n" % (offset, string))
  66. f.write("};\n\n")
  67. def static_assert(output, expression, message):
  68. """Writes a C++ compile-time assertion expression to a file.
  69. :param output: the output stream.
  70. :param expression: the expression to check.
  71. :param message: the string literal that will appear if the expression evaluates to
  72. false.
  73. """
  74. print("static_assert(%s, \"%s\");" % (expression, message), file=output)
  75. def add_expiration_postfix(expiration):
  76. """ Formats the expiration version and adds a version postfix if needed.
  77. :param expiration: the expiration version string.
  78. :return: the modified expiration string.
  79. """
  80. if re.match(r'^[1-9][0-9]*$', expiration):
  81. return expiration + ".0a1"
  82. if re.match(r'^[1-9][0-9]*\.0$', expiration):
  83. return expiration + "a1"
  84. return expiration