Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / cbuildbot / stages / afdo_stages.py
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.
4
5 """Module containing the AFDO stages."""
6
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
14
15
16 class AFDODataGenerateStage(generic_stages.BoardSpecificBuilderStage,
17                             generic_stages.ForgivingBuilderStage):
18   """Stage that generates AFDO profile data from a perf profile."""
19
20   def _GetCurrentArch(self):
21     """Get architecture for the current board being built."""
22     return self._GetPortageEnvVar('ARCH', self._current_board)
23
24   def PerformStage(self):
25     """After collecting a 'perf' profile, generate the profile in AFDO format.
26     """
27     super(AFDODataGenerateStage, self).PerformStage()
28
29     board = self._current_board
30     if not afdo.CanGenerateAFDOData(board):
31       cros_build_lib.Warning('Board %s cannot generate its own AFDO profile.',
32                              board)
33       return
34
35     arch = self._GetCurrentArch()
36     buildroot = self._build_root
37     gs_context = gs.GSContext()
38     cpv = portage_utilities.BestVisible(constants.CHROME_CP,
39                                         buildroot=buildroot)
40     afdo_file = None
41
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.
45     try:
46       if afdo.WaitForAFDOPerfData(cpv, arch, buildroot, gs_context):
47         afdo_file = afdo.GenerateAFDOData(cpv, arch, board,
48                                           buildroot, gs_context)
49         assert afdo_file
50         cros_build_lib.Info('Generated %s AFDO profile %s',
51                             arch, afdo_file)
52       else:
53         raise afdo.MissingAFDOData('Could not find current "perf" profile. '
54                                    'Master PFQ builder will try to use stale '
55                                    'AFDO profile.')
56     # Will let system-exiting exceptions through.
57     except Exception:
58       cros_build_lib.PrintBuildbotStepWarnings()
59       cros_build_lib.Warning('AFDO profile generation failed with exception ',
60                              exc_info=True)
61
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,
70                           message=alert_msg)
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
73       # generated.
74       raise
75
76
77 class AFDOUpdateEbuildStage(generic_stages.BuilderStage):
78   """Updates the Chrome ebuild with the names of the AFDO profiles."""
79
80   def PerformStage(self):
81     buildroot = self._build_root
82     gs_context = gs.GSContext()
83     cpv = portage_utilities.BestVisible(constants.CHROME_CP,
84                                         buildroot=buildroot)
85     version_number = cpv.version
86
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
92     arch_profiles = {}
93     for arch in afdo.AFDO_ARCH_GENERATORS:
94       afdo_file = afdo.GetLatestAFDOFile(cpv, arch, buildroot, gs_context)
95       if not afdo_file:
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
101
102     # Now update the Chrome ebuild file with the AFDO profiles we found
103     # for each architecture.
104     afdo.UpdateChromeEbuildAFDOFile(board, arch_profiles)