memory_reports.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # Copyright 2024 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. def _convert_bytes_to_kilobytes(number_bytes):
  15. """
  16. Convert the input from bytes into kilobytes
  17. :param number_bytes: the number of bytes to convert
  18. :return: the input value converted to kilobytes
  19. """
  20. NUMBER_BYTES_IN_KBYTE = 1024
  21. return int(number_bytes) / NUMBER_BYTES_IN_KBYTE
  22. def app_memory_report(platform_name, bin_type, app_size, max_ram, free_ram, resource_size=None,
  23. max_resource_size=None):
  24. """
  25. This method provides a formatted string for printing the memory usage of this binary to the
  26. console.
  27. :param platform_name: the name of the current HW platform being targeted
  28. :param bin_type: the type of binary being built (app, lib, worker)
  29. :param app_size: the size of the binary
  30. :param max_ram: the maximum allowed size of the binary
  31. :param free_ram: the amount of remaining memory
  32. :param resource_size: the size of the resource pack
  33. :param max_resource_size: the maximum allowed size of the resource pack
  34. :return: a tuple containing the color for the string print, and the string to print
  35. """
  36. LABEL = "-------------------------------------------------------\n{} {} MEMORY USAGE\n"
  37. RESOURCE_SIZE = "Total size of resources: {} bytes / {}KB\n"
  38. MEMORY_USAGE = ("Total footprint in RAM: {} bytes / {}KB\n"
  39. "Free RAM available (heap): {} bytes\n"
  40. "-------------------------------------------------------")
  41. if resource_size and max_resource_size:
  42. report = (LABEL.format(platform_name.upper(), bin_type.upper()) +
  43. RESOURCE_SIZE.format(resource_size,
  44. _convert_bytes_to_kilobytes(max_resource_size)) +
  45. MEMORY_USAGE.format(app_size, _convert_bytes_to_kilobytes(max_ram), free_ram))
  46. else:
  47. report = (LABEL.format(platform_name.upper(), bin_type.upper()) +
  48. MEMORY_USAGE.format(app_size, _convert_bytes_to_kilobytes(max_ram), free_ram))
  49. return 'YELLOW', report
  50. def app_resource_memory_error(platform_name, resource_size, max_resource_size):
  51. """
  52. This method provides a formatted error message for printing to the console when the resource
  53. size exceeds the maximum resource size supported by the Pebble firmware.
  54. :param platform_name: the name of the current HW platform being targeted
  55. :param resource_size: the size of the resource pack
  56. :param max_resource_size: the maximum allowed size of the resource pack
  57. :return: a tuple containing the color for the string print, and the string to print
  58. """
  59. report = ("======================================================\n"
  60. "Build failed: {}\n"
  61. "Error: Resource pack is too large ({}KB / {}KB)\n"
  62. "======================================================\n".
  63. format(platform_name,
  64. _convert_bytes_to_kilobytes(resource_size),
  65. _convert_bytes_to_kilobytes(max_resource_size)))
  66. return 'RED', report
  67. def app_appstore_resource_memory_error(platform_name, resource_size, max_appstore_resource_size):
  68. """
  69. This method provides a formatted warning message for printing to the console when the resource
  70. pack size exceeds the maximum allowed resource size for the appstore.
  71. :param platform_name: the name of the current HW platform being targeted
  72. :param resource_size: the size of the resource pack
  73. :param max_appstore_resource_size: the maximum appstore-allowed size of the resource pack
  74. :return: a tuple containing the color for the string print, and the string to print
  75. """
  76. report = ("WARNING: Your {} app resources are too large ({}KB / {}KB). You will not be "
  77. "able "
  78. "to publish your app.\n".
  79. format(platform_name,
  80. _convert_bytes_to_kilobytes(resource_size),
  81. _convert_bytes_to_kilobytes(max_appstore_resource_size)))
  82. return 'RED', report
  83. def bytecode_memory_report(platform_name, bytecode_size, bytecode_max):
  84. """
  85. This method provides a formatted string for printing the memory usage for this Rocky bytecode
  86. file to the console.
  87. :param platform_name: the name of the current HW platform being targeted
  88. :param bytecode_size: the size of the bytecode file, in bytes
  89. :param bytecode_max: the max allowed size of the bytecode file, in bytes
  90. :return: a tuple containing the color for the string print, and the string to print
  91. """
  92. LABEL = "-------------------------------------------------------\n{} MEMORY USAGE\n"
  93. BYTECODE_USAGE = ("Total size of snapshot: {}KB / {}KB\n"
  94. "-------------------------------------------------------")
  95. report = (LABEL.format(platform_name.upper()) +
  96. BYTECODE_USAGE.format(_convert_bytes_to_kilobytes(bytecode_size),
  97. _convert_bytes_to_kilobytes(bytecode_max)))
  98. return 'YELLOW', report
  99. def simple_memory_report(platform_name, bin_size, resource_size=None):
  100. """
  101. This method provides a formatted string for printing the memory usage for this binary to the
  102. console.
  103. :param platform_name: the name of the current HW platform being targeted
  104. :param bin_size: the size of the binary
  105. :param resource_size: the size of the resource pack
  106. :return: a tuple containing the color for the string print, and the string to print
  107. """
  108. LABEL = "-------------------------------------------------------\n{} MEMORY USAGE\n"
  109. RESOURCE_SIZE = "Total size of resources: {} bytes\n"
  110. MEMORY_USAGE = ("Total footprint in RAM: {} bytes\n"
  111. "-------------------------------------------------------")
  112. if resource_size:
  113. report = (LABEL.format(platform_name.upper()) +
  114. RESOURCE_SIZE.format(resource_size) +
  115. MEMORY_USAGE.format(bin_size))
  116. else:
  117. report = (LABEL.format(platform_name.upper()) +
  118. MEMORY_USAGE.format(bin_size))
  119. return 'YELLOW', report