1 # Copyright 2014 The Chromium OS Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Module containing the AFDO stages."""
7 from chromite.cbuildbot import afdo
8 from chromite.cbuildbot import constants
9 from chromite.cbuildbot import portage_utilities
10 from chromite.lib import alerts
11 from chromite.lib import cros_build_lib
12 from chromite.lib import gs
13 from chromite.cbuildbot.stages import generic_stages
16 class AFDODataGenerateStage(generic_stages.BoardSpecificBuilderStage,
17 generic_stages.ForgivingBuilderStage):
18 """Stage that generates AFDO profile data from a perf profile."""
20 def _GetCurrentArch(self):
21 """Get architecture for the current board being built."""
22 return self._GetPortageEnvVar('ARCH', self._current_board)
24 def PerformStage(self):
25 """After collecting a 'perf' profile, generate the profile in AFDO format.
27 super(AFDODataGenerateStage, self).PerformStage()
29 board = self._current_board
30 if not afdo.CanGenerateAFDOData(board):
31 cros_build_lib.Warning('Board %s cannot generate its own AFDO profile.',
35 arch = self._GetCurrentArch()
36 buildroot = self._build_root
37 gs_context = gs.GSContext()
38 cpv = portage_utilities.BestVisible(constants.CHROME_CP,
42 # Generation of AFDO could fail for different reasons.
43 # We will ignore the failures and let the master PFQ builder try
44 # to find an older AFDO profile.
46 if afdo.WaitForAFDOPerfData(cpv, arch, buildroot, gs_context):
47 afdo_file = afdo.GenerateAFDOData(cpv, arch, board,
48 buildroot, gs_context)
50 cros_build_lib.Info('Generated %s AFDO profile %s',
53 raise afdo.MissingAFDOData('Could not find current "perf" profile. '
54 'Master PFQ builder will try to use stale '
56 # Will let system-exiting exceptions through.
58 cros_build_lib.PrintBuildbotStepWarnings()
59 cros_build_lib.Warning('AFDO profile generation failed with exception ',
62 alert_msg = ('Please triage. This will become a fatal error.\n\n'
63 'arch=%s buildroot=%s\n\nURL=%s' %
64 (arch, buildroot, self._run.ConstructDashboardURL()))
65 subject_msg = ('Failure in generation of AFDO Data for builder %s' %
66 self._run.config.name)
67 alerts.SendEmailLog(subject_msg,
68 afdo.AFDO_ALERT_RECIPIENTS,
69 smtp_server=constants.GOLO_SMTP_SERVER,
71 # Re-raise whatever exception we got here. This stage will only
72 # generate a warning but we want to make sure the warning is
77 class AFDOUpdateEbuildStage(generic_stages.BuilderStage):
78 """Updates the Chrome ebuild with the names of the AFDO profiles."""
80 def PerformStage(self):
81 buildroot = self._build_root
82 gs_context = gs.GSContext()
83 cpv = portage_utilities.BestVisible(constants.CHROME_CP,
85 version_number = cpv.version
87 # We need the name of one board that has been setup in this
88 # builder to find the Chrome ebuild. The chrome ebuild should be
89 # the same for all the boards, so just use the first one.
90 # If we don't have any boards, leave the called function to guess.
91 board = self._boards[0] if self._boards else None
93 for arch in afdo.AFDO_ARCH_GENERATORS:
94 afdo_file = afdo.GetLatestAFDOFile(cpv, arch, buildroot, gs_context)
96 raise afdo.MissingAFDOData('Could not find appropriate AFDO profile')
97 state = 'current' if version_number in afdo_file else 'previous'
98 cros_build_lib.Info('Found %s %s AFDO profile %s',
99 state, arch, afdo_file)
100 arch_profiles[arch] = afdo_file
102 # Now update the Chrome ebuild file with the AFDO profiles we found
103 # for each architecture.
104 afdo.UpdateChromeEbuildAFDOFile(board, arch_profiles)