oe-build-perf-test 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/python3
  2. #
  3. # Build performance test script
  4. #
  5. # Copyright (c) 2016, Intel Corporation.
  6. #
  7. # This program is free software; you can redistribute it and/or modify it
  8. # under the terms and conditions of the GNU General Public License,
  9. # version 2, as published by the Free Software Foundation.
  10. #
  11. # This program is distributed in the hope it will be useful, but WITHOUT
  12. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. # more details.
  15. #
  16. """Build performance test script"""
  17. import argparse
  18. import errno
  19. import fcntl
  20. import logging
  21. import os
  22. import sys
  23. from datetime import datetime
  24. sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '/lib')
  25. import scriptpath
  26. scriptpath.add_oe_lib_path()
  27. from oeqa.buildperf import BuildPerfTestRunner, KernelDropCaches
  28. from oeqa.utils.commands import runCmd
  29. # Set-up logging
  30. LOG_FORMAT = '[%(asctime)s] %(levelname)s: %(message)s'
  31. logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
  32. log = logging.getLogger()
  33. def acquire_lock(lock_f):
  34. """Acquire flock on file"""
  35. log.debug("Acquiring lock %s", os.path.abspath(lock_f.name))
  36. try:
  37. fcntl.flock(lock_f, fcntl.LOCK_EX | fcntl.LOCK_NB)
  38. except IOError as err:
  39. if err.errno == errno.EAGAIN:
  40. return False
  41. raise
  42. log.debug("Lock acquired")
  43. return True
  44. def pre_run_sanity_check():
  45. """Sanity check of build environment"""
  46. build_dir = os.environ.get("BUILDDIR")
  47. if not build_dir:
  48. log.error("BUILDDIR not set. Please run the build environmnent setup "
  49. "script.")
  50. return False
  51. if os.getcwd() != build_dir:
  52. log.error("Please run this script under BUILDDIR (%s)", build_dir)
  53. return False
  54. ret = runCmd('which bitbake', ignore_status=True)
  55. if ret.status:
  56. log.error("bitbake command not found")
  57. return False
  58. return True
  59. def setup_file_logging(log_file):
  60. """Setup loggin to file"""
  61. log_dir = os.path.dirname(log_file)
  62. if not os.path.exists(log_dir):
  63. os.makedirs(log_dir)
  64. formatter = logging.Formatter(LOG_FORMAT)
  65. handler = logging.FileHandler(log_file)
  66. handler.setFormatter(formatter)
  67. log.addHandler(handler)
  68. def parse_args(argv):
  69. """Parse command line arguments"""
  70. parser = argparse.ArgumentParser(
  71. formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  72. parser.add_argument('-D', '--debug', action='store_true',
  73. help='Enable debug level logging')
  74. parser.add_argument('--globalres-file',
  75. help="Append results to 'globalres' csv file")
  76. parser.add_argument('--lock-file', default='./oe-build-perf.lock',
  77. metavar='FILENAME',
  78. help="Lock file to use")
  79. return parser.parse_args(argv)
  80. def main(argv=None):
  81. """Script entry point"""
  82. args = parse_args(argv)
  83. if args.debug:
  84. log.setLevel(logging.DEBUG)
  85. lock_f = open(args.lock_file, 'w')
  86. if not acquire_lock(lock_f):
  87. log.error("Another instance of this script is running, exiting...")
  88. return 1
  89. if not pre_run_sanity_check():
  90. return 1
  91. # Check our capability to drop caches and ask pass if needed
  92. KernelDropCaches.check()
  93. # Set-up log file
  94. out_dir = 'results-{}'.format(datetime.now().strftime('%Y%m%d%H%M%S'))
  95. setup_file_logging(os.path.join(out_dir, 'output.log'))
  96. # Run actual tests
  97. runner = BuildPerfTestRunner(out_dir)
  98. ret = runner.run_tests()
  99. if not ret:
  100. if args.globalres_file:
  101. runner.update_globalres_file(args.globalres_file)
  102. return ret
  103. if __name__ == '__main__':
  104. sys.exit(main())