orderfile_generator_backend.py 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. #!/usr/bin/env vpython3
  2. # Copyright (c) 2013 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """ A utility to generate an up-to-date orderfile.
  6. The orderfile is used by the linker to order text sections such that the
  7. sections are placed consecutively in the order specified. This allows us
  8. to page in less code during start-up.
  9. Example usage:
  10. tools/cygprofile/orderfile_generator_backend.py --use-goma --target-arch=arm
  11. """
  12. import argparse
  13. import csv
  14. import hashlib
  15. import json
  16. import glob
  17. import logging
  18. import os
  19. import shutil
  20. import subprocess
  21. import sys
  22. import tempfile
  23. import time
  24. import cluster
  25. import cyglog_to_orderfile
  26. import patch_orderfile
  27. import process_profiles
  28. import profile_android_startup
  29. _SRC_PATH = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)
  30. sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'catapult', 'devil'))
  31. from devil.android import device_utils
  32. from devil.android.sdk import version_codes
  33. _SRC_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)),
  34. os.pardir, os.pardir)
  35. sys.path.append(os.path.join(_SRC_PATH, 'build', 'android'))
  36. import devil_chromium
  37. from pylib import constants
  38. # Needs to happen early for GetBuildType()/GetOutDirectory() to work correctly
  39. constants.SetBuildType('Release')
  40. # Architecture specific GN args. Trying to build an orderfile for an
  41. # architecture not listed here will eventually throw.
  42. _ARCH_GN_ARGS = {
  43. 'arm': ['target_cpu = "arm"'],
  44. 'arm64': ['target_cpu = "arm64"', 'android_64bit_browser = true'],
  45. 'x86': ['target_cpu = "x86"'],
  46. }
  47. class CommandError(Exception):
  48. """Indicates that a dispatched shell command exited with a non-zero status."""
  49. def __init__(self, value):
  50. super().__init__()
  51. self.value = value
  52. def __str__(self):
  53. return repr(self.value)
  54. def _GenerateHash(file_path):
  55. """Calculates and returns the hash of the file at file_path."""
  56. sha1 = hashlib.sha1()
  57. with open(file_path, 'rb') as f:
  58. while True:
  59. # Read in 1mb chunks, so it doesn't all have to be loaded into memory.
  60. chunk = f.read(1024 * 1024)
  61. if not chunk:
  62. break
  63. sha1.update(chunk)
  64. return sha1.hexdigest()
  65. def _GetFileExtension(file_name):
  66. """Calculates the file extension from a file name.
  67. Args:
  68. file_name: The source file name.
  69. Returns:
  70. The part of file_name after the dot (.) or None if the file has no
  71. extension.
  72. Examples: /home/user/foo.bar -> bar
  73. /home/user.name/foo -> None
  74. /home/user/.foo -> None
  75. /home/user/foo.bar.baz -> baz
  76. """
  77. file_name_parts = os.path.basename(file_name).split('.')
  78. if len(file_name_parts) > 1:
  79. return file_name_parts[-1]
  80. return None
  81. def _StashOutputDirectory(buildpath):
  82. """Takes the output directory and stashes it in the default output directory.
  83. This allows it to be used for incremental builds next time (after unstashing)
  84. by keeping it in a place that isn't deleted normally, while also ensuring
  85. that it is properly clobbered when appropriate.
  86. This is a dirty hack to deal with the needs of clobbering while also handling
  87. incremental builds and the hardcoded relative paths used in some of the
  88. project files.
  89. Args:
  90. buildpath: The path where the building happens. If this corresponds to the
  91. default output directory, no action is taken.
  92. """
  93. if os.path.abspath(buildpath) == os.path.abspath(os.path.dirname(
  94. constants.GetOutDirectory())):
  95. return
  96. name = os.path.basename(buildpath)
  97. stashpath = os.path.join(constants.GetOutDirectory(), name)
  98. if not os.path.exists(buildpath):
  99. return
  100. if os.path.exists(stashpath):
  101. shutil.rmtree(stashpath, ignore_errors=True)
  102. shutil.move(buildpath, stashpath)
  103. def _UnstashOutputDirectory(buildpath):
  104. """Inverse of _StashOutputDirectory.
  105. Moves the output directory stashed within the default output directory
  106. (out/Release) to the position where the builds can actually happen.
  107. This is a dirty hack to deal with the needs of clobbering while also handling
  108. incremental builds and the hardcoded relative paths used in some of the
  109. project files.
  110. Args:
  111. buildpath: The path where the building happens. If this corresponds to the
  112. default output directory, no action is taken.
  113. """
  114. if os.path.abspath(buildpath) == os.path.abspath(os.path.dirname(
  115. constants.GetOutDirectory())):
  116. return
  117. name = os.path.basename(buildpath)
  118. stashpath = os.path.join(constants.GetOutDirectory(), name)
  119. if not os.path.exists(stashpath):
  120. return
  121. if os.path.exists(buildpath):
  122. shutil.rmtree(buildpath, ignore_errors=True)
  123. shutil.move(stashpath, buildpath)
  124. class StepRecorder:
  125. """Records steps and timings."""
  126. def __init__(self, buildbot):
  127. self.timings = []
  128. self._previous_step = ('', 0.0)
  129. self._buildbot = buildbot
  130. self._error_recorded = False
  131. def BeginStep(self, name):
  132. """Marks a beginning of the next step in the script.
  133. On buildbot, this prints a specially formatted name that will show up
  134. in the waterfall. Otherwise, just prints the step name.
  135. Args:
  136. name: The name of the step.
  137. """
  138. self.EndStep()
  139. self._previous_step = (name, time.time())
  140. print('Running step: ', name)
  141. def EndStep(self):
  142. """Records successful completion of the current step.
  143. This is optional if the step is immediately followed by another BeginStep.
  144. """
  145. if self._previous_step[0]:
  146. elapsed = time.time() - self._previous_step[1]
  147. print('Step %s took %f seconds' % (self._previous_step[0], elapsed))
  148. self.timings.append((self._previous_step[0], elapsed))
  149. self._previous_step = ('', 0.0)
  150. def FailStep(self, message=None):
  151. """Marks that a particular step has failed.
  152. On buildbot, this will mark the current step as failed on the waterfall.
  153. Otherwise we will just print an optional failure message.
  154. Args:
  155. message: An optional explanation as to why the step failed.
  156. """
  157. print('STEP FAILED!!')
  158. if message:
  159. print(message)
  160. self._error_recorded = True
  161. self.EndStep()
  162. def ErrorRecorded(self):
  163. """True if FailStep has been called."""
  164. return self._error_recorded
  165. def RunCommand(self, cmd, cwd=constants.DIR_SOURCE_ROOT, raise_on_error=True,
  166. stdout=None):
  167. """Execute a shell command.
  168. Args:
  169. cmd: A list of command strings.
  170. cwd: Directory in which the command should be executed, defaults to build
  171. root of script's location if not specified.
  172. raise_on_error: If true will raise a CommandError if the call doesn't
  173. succeed and mark the step as failed.
  174. stdout: A file to redirect stdout for the command to.
  175. Returns:
  176. The process's return code.
  177. Raises:
  178. CommandError: An error executing the specified command.
  179. """
  180. print('Executing %s in %s' % (' '.join(cmd), cwd))
  181. process = subprocess.Popen(cmd, stdout=stdout, cwd=cwd, env=os.environ)
  182. process.wait()
  183. if raise_on_error and process.returncode != 0:
  184. self.FailStep()
  185. raise CommandError('Exception executing command %s' % ' '.join(cmd))
  186. return process.returncode
  187. class ClankCompiler:
  188. """Handles compilation of clank."""
  189. def __init__(self, out_dir, step_recorder, arch, use_goma, goma_dir,
  190. system_health_profiling, monochrome, public, orderfile_location):
  191. self._out_dir = out_dir
  192. self._step_recorder = step_recorder
  193. self._arch = arch
  194. self._use_goma = use_goma
  195. self._goma_dir = goma_dir
  196. self._system_health_profiling = system_health_profiling
  197. self._public = public
  198. self._orderfile_location = orderfile_location
  199. if monochrome:
  200. self._apk = 'Monochrome.apk'
  201. self._apk_target = 'monochrome_apk'
  202. self._libname = 'libmonochrome'
  203. self._libchrome_target = 'libmonochrome'
  204. else:
  205. self._apk = 'Chrome.apk'
  206. self._apk_target = 'chrome_apk'
  207. self._libname = 'libchrome'
  208. self._libchrome_target = 'libchrome'
  209. if public:
  210. self._apk = self._apk.replace('.apk', 'Public.apk')
  211. self._apk_target = self._apk_target.replace('_apk', '_public_apk')
  212. self.obj_dir = os.path.join(self._out_dir, 'Release', 'obj')
  213. self.lib_chrome_so = os.path.join(
  214. self._out_dir, 'Release', 'lib.unstripped',
  215. '{}.so'.format(self._libname))
  216. self.chrome_apk = os.path.join(self._out_dir, 'Release', 'apks', self._apk)
  217. def Build(self, instrumented, use_call_graph, target):
  218. """Builds the provided ninja target with or without order_profiling on.
  219. Args:
  220. instrumented: (bool) Whether we want to build an instrumented binary.
  221. use_call_graph: (bool) Whether to use the call graph instrumentation.
  222. target: (str) The name of the ninja target to build.
  223. """
  224. self._step_recorder.BeginStep('Compile %s' % target)
  225. assert not use_call_graph or instrumented, ('You can not enable call graph '
  226. 'without instrumentation!')
  227. # Set the "Release Official" flavor, the parts affecting performance.
  228. args = [
  229. 'enable_resource_allowlist_generation=false',
  230. 'is_chrome_branded=' + str(not self._public).lower(),
  231. 'is_debug=false',
  232. 'is_official_build=true',
  233. 'symbol_level=1', # to fit 30 GiB RAM on the bot when LLD is running
  234. 'target_os="android"',
  235. 'use_goma=' + str(self._use_goma).lower(),
  236. 'use_order_profiling=' + str(instrumented).lower(),
  237. 'use_call_graph=' + str(use_call_graph).lower(),
  238. ]
  239. args += _ARCH_GN_ARGS[self._arch]
  240. if self._goma_dir:
  241. args += ['goma_dir="%s"' % self._goma_dir]
  242. if self._system_health_profiling:
  243. args += ['devtools_instrumentation_dumping = ' +
  244. str(instrumented).lower()]
  245. if self._public and os.path.exists(self._orderfile_location):
  246. # GN needs the orderfile path to be source-absolute.
  247. src_abs_orderfile = os.path.relpath(self._orderfile_location,
  248. constants.DIR_SOURCE_ROOT)
  249. args += ['chrome_orderfile="//{}"'.format(src_abs_orderfile)]
  250. self._step_recorder.RunCommand(
  251. ['gn', 'gen', os.path.join(self._out_dir, 'Release'),
  252. '--args=' + ' '.join(args)])
  253. self._step_recorder.RunCommand(
  254. ['autoninja', '-C',
  255. os.path.join(self._out_dir, 'Release'), target])
  256. def ForceRelink(self):
  257. """Forces libchrome.so or libmonochrome.so to be re-linked.
  258. With partitioned libraries enabled, deleting these library files does not
  259. guarantee they'll be recreated by the linker (they may simply be
  260. re-extracted from a combined library). To be safe, touch a source file
  261. instead. See http://crbug.com/972701 for more explanation.
  262. """
  263. file_to_touch = os.path.join(constants.DIR_SOURCE_ROOT, 'chrome', 'browser',
  264. 'chrome_browser_main_android.cc')
  265. assert os.path.exists(file_to_touch)
  266. self._step_recorder.RunCommand(['touch', file_to_touch])
  267. def CompileChromeApk(self, instrumented, use_call_graph, force_relink=False):
  268. """Builds a Chrome.apk either with or without order_profiling on.
  269. Args:
  270. instrumented: (bool) Whether to build an instrumented apk.
  271. use_call_graph: (bool) Whether to use the call graph instrumentation.
  272. force_relink: Whether libchromeview.so should be re-created.
  273. """
  274. if force_relink:
  275. self.ForceRelink()
  276. self.Build(instrumented, use_call_graph, self._apk_target)
  277. def CompileLibchrome(self, instrumented, use_call_graph, force_relink=False):
  278. """Builds a libchrome.so either with or without order_profiling on.
  279. Args:
  280. instrumented: (bool) Whether to build an instrumented apk.
  281. use_call_graph: (bool) Whether to use the call graph instrumentation.
  282. force_relink: (bool) Whether libchrome.so should be re-created.
  283. """
  284. if force_relink:
  285. self.ForceRelink()
  286. self.Build(instrumented, use_call_graph, self._libchrome_target)
  287. class OrderfileUpdater:
  288. """Handles uploading and committing a new orderfile in the repository.
  289. Only used for testing or on a bot.
  290. """
  291. _CLOUD_STORAGE_BUCKET_FOR_DEBUG = None
  292. _CLOUD_STORAGE_BUCKET = None
  293. _UPLOAD_TO_CLOUD_COMMAND = 'upload_to_google_storage.py'
  294. def __init__(self, repository_root, step_recorder):
  295. """Constructor.
  296. Args:
  297. repository_root: (str) Root of the target repository.
  298. step_recorder: (StepRecorder) Step recorder, for logging.
  299. """
  300. self._repository_root = repository_root
  301. self._step_recorder = step_recorder
  302. def CommitStashedFileHashes(self, files):
  303. """Commits unpatched and patched orderfiles hashes if changed.
  304. The files are committed only if their associated sha1 hash files match, and
  305. are modified in git. In normal operations the hash files are changed only
  306. when a file is uploaded to cloud storage. If the hash file is not modified
  307. in git, the file is skipped.
  308. Args:
  309. files: [str or None] specifies file paths. None items are ignored.
  310. Raises:
  311. Exception if the hash file does not match the file.
  312. NotImplementedError when the commit logic hasn't been overridden.
  313. """
  314. files_to_commit = [_f for _f in files if _f]
  315. if files_to_commit:
  316. self._CommitStashedFiles(files_to_commit)
  317. def UploadToCloudStorage(self, filename, use_debug_location):
  318. """Uploads a file to cloud storage.
  319. Args:
  320. filename: (str) File to upload.
  321. use_debug_location: (bool) Whether to use the debug location.
  322. """
  323. bucket = (self._CLOUD_STORAGE_BUCKET_FOR_DEBUG if use_debug_location
  324. else self._CLOUD_STORAGE_BUCKET)
  325. extension = _GetFileExtension(filename)
  326. cmd = [self._UPLOAD_TO_CLOUD_COMMAND, '--bucket', bucket]
  327. if extension:
  328. cmd.extend(['-z', extension])
  329. cmd.append(filename)
  330. self._step_recorder.RunCommand(cmd)
  331. print('Download: https://sandbox.google.com/storage/%s/%s' %
  332. (bucket, _GenerateHash(filename)))
  333. def _GetHashFilePathAndContents(self, filename):
  334. """Gets the name and content of the hash file created from uploading the
  335. given file.
  336. Args:
  337. filename: (str) The file that was uploaded to cloud storage.
  338. Returns:
  339. A tuple of the hash file name, relative to the reository root, and the
  340. content, which should be the sha1 hash of the file
  341. ('base_file.sha1', hash)
  342. """
  343. abs_hash_filename = filename + '.sha1'
  344. rel_hash_filename = os.path.relpath(
  345. abs_hash_filename, self._repository_root)
  346. with open(abs_hash_filename, 'r') as f:
  347. return (rel_hash_filename, f.read())
  348. def _GitStash(self):
  349. """Git stash the current clank tree.
  350. Raises:
  351. NotImplementedError when the stash logic hasn't been overridden.
  352. """
  353. raise NotImplementedError
  354. def _CommitStashedFiles(self, expected_files_in_stash):
  355. """Commits stashed files.
  356. The local repository is updated and then the files to commit are taken from
  357. modified files from the git stash. The modified files should be a subset of
  358. |expected_files_in_stash|. If there are unexpected modified files, this
  359. function may raise. This is meant to be paired with _GitStash().
  360. Args:
  361. expected_files_in_stash: [str] paths to a possible superset of files
  362. expected to be stashed & committed.
  363. Raises:
  364. NotImplementedError when the commit logic hasn't been overridden.
  365. """
  366. raise NotImplementedError
  367. class OrderfileGenerator:
  368. """A utility for generating a new orderfile for Clank.
  369. Builds an instrumented binary, profiles a run of the application, and
  370. generates an updated orderfile.
  371. """
  372. _CHECK_ORDERFILE_SCRIPT = os.path.join(
  373. constants.DIR_SOURCE_ROOT, 'tools', 'cygprofile', 'check_orderfile.py')
  374. _BUILD_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(
  375. constants.GetOutDirectory()))) # Normally /path/to/src
  376. # Previous orderfile_generator debug files would be overwritten.
  377. _DIRECTORY_FOR_DEBUG_FILES = '/tmp/orderfile_generator_debug_files'
  378. _CLOUD_STORAGE_BUCKET_FOR_DEBUG = None
  379. def _PrepareOrderfilePaths(self):
  380. if self._options.public:
  381. self._clank_dir = os.path.join(constants.DIR_SOURCE_ROOT,
  382. '')
  383. if not os.path.exists(os.path.join(self._clank_dir, 'orderfiles')):
  384. os.makedirs(os.path.join(self._clank_dir, 'orderfiles'))
  385. else:
  386. self._clank_dir = os.path.join(constants.DIR_SOURCE_ROOT,
  387. 'clank')
  388. self._unpatched_orderfile_filename = os.path.join(
  389. self._clank_dir, 'orderfiles', 'unpatched_orderfile.%s')
  390. self._path_to_orderfile = os.path.join(
  391. self._clank_dir, 'orderfiles', 'orderfile.%s.out')
  392. def _GetPathToOrderfile(self):
  393. """Gets the path to the architecture-specific orderfile."""
  394. # Build GN files use the ".arm" orderfile irrespective of the actual
  395. # architecture. Fake it, otherwise the orderfile we generate here is not
  396. # going to be picked up by builds.
  397. orderfile_fake_arch = 'arm'
  398. return self._path_to_orderfile % orderfile_fake_arch
  399. def _GetUnpatchedOrderfileFilename(self):
  400. """Gets the path to the architecture-specific unpatched orderfile."""
  401. return self._unpatched_orderfile_filename % self._options.arch
  402. def _SetDevice(self):
  403. """ Selects the device to be used by the script.
  404. Returns:
  405. (Device with given serial ID) : if the --device flag is set.
  406. (Device running Android[K,L]) : if --use-legacy-chrome-apk flag is set or
  407. no device running Android N+ was found.
  408. (Device running Android N+) : Otherwise.
  409. Raises Error:
  410. If no device meeting the requirements has been found.
  411. """
  412. devices = None
  413. if self._options.device:
  414. devices = [device_utils.DeviceUtils(self._options.device)]
  415. else:
  416. devices = device_utils.DeviceUtils.HealthyDevices()
  417. assert devices, 'Expected at least one connected device'
  418. if self._options.use_legacy_chrome_apk:
  419. self._monochrome = False
  420. for device in devices:
  421. device_version = device.build_version_sdk
  422. if (version_codes.KITKAT <= device_version <=
  423. version_codes.LOLLIPOP_MR1):
  424. return device
  425. assert not self._options.use_legacy_chrome_apk, \
  426. 'No device found running suitable android version for Chrome.apk.'
  427. preferred_device = None
  428. for device in devices:
  429. if device.build_version_sdk >= version_codes.NOUGAT:
  430. preferred_device = device
  431. break
  432. self._monochrome = preferred_device is not None
  433. return preferred_device if preferred_device else devices[0]
  434. def __init__(self, options, orderfile_updater_class):
  435. self._options = options
  436. self._instrumented_out_dir = os.path.join(
  437. self._BUILD_ROOT, self._options.arch + '_instrumented_out')
  438. if self._options.use_call_graph:
  439. self._instrumented_out_dir += '_call_graph'
  440. self._uninstrumented_out_dir = os.path.join(
  441. self._BUILD_ROOT, self._options.arch + '_uninstrumented_out')
  442. self._no_orderfile_out_dir = os.path.join(
  443. self._BUILD_ROOT, self._options.arch + '_no_orderfile_out')
  444. self._PrepareOrderfilePaths()
  445. if options.profile:
  446. output_directory = os.path.join(self._instrumented_out_dir, 'Release')
  447. host_profile_dir = os.path.join(output_directory, 'profile_data')
  448. urls = [profile_android_startup.AndroidProfileTool.TEST_URL]
  449. use_wpr = True
  450. simulate_user = False
  451. urls = options.urls
  452. use_wpr = not options.no_wpr
  453. simulate_user = options.simulate_user
  454. device = self._SetDevice()
  455. self._profiler = profile_android_startup.AndroidProfileTool(
  456. output_directory, host_profile_dir, use_wpr, urls, simulate_user,
  457. device, debug=self._options.streamline_for_debugging)
  458. if options.pregenerated_profiles:
  459. self._profiler.SetPregeneratedProfiles(
  460. glob.glob(options.pregenerated_profiles))
  461. else:
  462. assert not options.pregenerated_profiles, (
  463. '--pregenerated-profiles cannot be used with --skip-profile')
  464. assert not options.profile_save_dir, (
  465. '--profile-save-dir cannot be used with --skip-profile')
  466. self._monochrome = not self._options.use_legacy_chrome_apk
  467. # Outlined function handling enabled by default for all architectures.
  468. self._order_outlined_functions = not options.noorder_outlined_functions
  469. self._output_data = {}
  470. self._step_recorder = StepRecorder(options.buildbot)
  471. self._compiler = None
  472. if orderfile_updater_class is None:
  473. orderfile_updater_class = OrderfileUpdater
  474. assert issubclass(orderfile_updater_class, OrderfileUpdater)
  475. self._orderfile_updater = orderfile_updater_class(self._clank_dir,
  476. self._step_recorder)
  477. assert os.path.isdir(constants.DIR_SOURCE_ROOT), 'No src directory found'
  478. @staticmethod
  479. def _RemoveBlanks(src_file, dest_file):
  480. """A utility to remove blank lines from a file.
  481. Args:
  482. src_file: The name of the file to remove the blanks from.
  483. dest_file: The name of the file to write the output without blanks.
  484. """
  485. assert src_file != dest_file, 'Source and destination need to be distinct'
  486. try:
  487. src = open(src_file, 'r')
  488. dest = open(dest_file, 'w')
  489. for line in src:
  490. if line and not line.isspace():
  491. dest.write(line)
  492. finally:
  493. src.close()
  494. dest.close()
  495. def _GenerateAndProcessProfile(self):
  496. """Invokes a script to merge the per-thread traces into one file.
  497. The produced list of offsets is saved in
  498. self._GetUnpatchedOrderfileFilename().
  499. """
  500. self._step_recorder.BeginStep('Generate Profile Data')
  501. files = []
  502. logging.getLogger().setLevel(logging.DEBUG)
  503. if self._options.profile_save_dir:
  504. # The directory must not preexist, to ensure purity of data. Check
  505. # before profiling to save time.
  506. if os.path.exists(self._options.profile_save_dir):
  507. raise Exception('Profile save directory must not pre-exist')
  508. os.makedirs(self._options.profile_save_dir)
  509. if self._options.system_health_orderfile:
  510. files = self._profiler.CollectSystemHealthProfile(
  511. self._compiler.chrome_apk)
  512. self._MaybeSaveProfile(files)
  513. try:
  514. self._ProcessPhasedOrderfile(files)
  515. except Exception:
  516. for f in files:
  517. self._SaveForDebugging(f)
  518. self._SaveForDebugging(self._compiler.lib_chrome_so)
  519. raise
  520. finally:
  521. self._profiler.Cleanup()
  522. else:
  523. self._CollectLegacyProfile()
  524. logging.getLogger().setLevel(logging.INFO)
  525. def _ProcessPhasedOrderfile(self, files):
  526. """Process the phased orderfiles produced by system health benchmarks.
  527. The offsets will be placed in _GetUnpatchedOrderfileFilename().
  528. Args:
  529. file: Profile files pulled locally.
  530. """
  531. self._step_recorder.BeginStep('Process Phased Orderfile')
  532. profiles = process_profiles.ProfileManager(files)
  533. processor = process_profiles.SymbolOffsetProcessor(
  534. self._compiler.lib_chrome_so)
  535. ordered_symbols = cluster.ClusterOffsets(profiles, processor,
  536. call_graph=self._options.use_call_graph)
  537. if not ordered_symbols:
  538. raise Exception('Failed to get ordered symbols')
  539. for sym in ordered_symbols:
  540. assert not sym.startswith('OUTLINED_FUNCTION_'), (
  541. 'Outlined function found in instrumented function, very likely '
  542. 'something has gone very wrong!')
  543. self._output_data['offsets_kib'] = processor.SymbolsSize(
  544. ordered_symbols) / 1024
  545. with open(self._GetUnpatchedOrderfileFilename(), 'w') as orderfile:
  546. orderfile.write('\n'.join(ordered_symbols))
  547. def _CollectLegacyProfile(self):
  548. files = []
  549. try:
  550. files = self._profiler.CollectProfile(
  551. self._compiler.chrome_apk,
  552. constants.PACKAGE_INFO['chrome'])
  553. self._MaybeSaveProfile(files)
  554. self._step_recorder.BeginStep('Process profile')
  555. assert os.path.exists(self._compiler.lib_chrome_so)
  556. offsets = process_profiles.GetReachedOffsetsFromDumpFiles(
  557. files, self._compiler.lib_chrome_so)
  558. if not offsets:
  559. raise Exception('No profiler offsets found in {}'.format(
  560. '\n'.join(files)))
  561. processor = process_profiles.SymbolOffsetProcessor(
  562. self._compiler.lib_chrome_so)
  563. ordered_symbols = processor.GetOrderedSymbols(offsets)
  564. if not ordered_symbols:
  565. raise Exception('No symbol names from offsets found in {}'.format(
  566. '\n'.join(files)))
  567. with open(self._GetUnpatchedOrderfileFilename(), 'w') as orderfile:
  568. orderfile.write('\n'.join(ordered_symbols))
  569. except Exception:
  570. for f in files:
  571. self._SaveForDebugging(f)
  572. raise
  573. finally:
  574. self._profiler.Cleanup()
  575. def _MaybeSaveProfile(self, files):
  576. if self._options.profile_save_dir:
  577. logging.info('Saving profiles to %s', self._options.profile_save_dir)
  578. for f in files:
  579. shutil.copy(f, self._options.profile_save_dir)
  580. logging.info('Saved profile %s', f)
  581. def _PatchOrderfile(self):
  582. """Patches the orderfile using clean version of libchrome.so."""
  583. self._step_recorder.BeginStep('Patch Orderfile')
  584. patch_orderfile.GeneratePatchedOrderfile(
  585. self._GetUnpatchedOrderfileFilename(), self._compiler.lib_chrome_so,
  586. self._GetPathToOrderfile(), self._order_outlined_functions)
  587. def _VerifySymbolOrder(self):
  588. self._step_recorder.BeginStep('Verify Symbol Order')
  589. return_code = self._step_recorder.RunCommand([
  590. self._CHECK_ORDERFILE_SCRIPT, self._compiler.lib_chrome_so,
  591. self._GetPathToOrderfile()
  592. ],
  593. constants.DIR_SOURCE_ROOT,
  594. raise_on_error=False)
  595. if return_code:
  596. self._step_recorder.FailStep('Orderfile check returned %d.' % return_code)
  597. def _RecordHash(self, file_name):
  598. """Records the hash of the file into the output_data dictionary."""
  599. self._output_data[os.path.basename(file_name) + '.sha1'] = _GenerateHash(
  600. file_name)
  601. def _SaveFileLocally(self, file_name, file_sha1):
  602. """Saves the file to a temporary location and prints the sha1sum."""
  603. if not os.path.exists(self._DIRECTORY_FOR_DEBUG_FILES):
  604. os.makedirs(self._DIRECTORY_FOR_DEBUG_FILES)
  605. shutil.copy(file_name, self._DIRECTORY_FOR_DEBUG_FILES)
  606. print('File: %s, saved in: %s, sha1sum: %s' %
  607. (file_name, self._DIRECTORY_FOR_DEBUG_FILES, file_sha1))
  608. def _SaveForDebugging(self, filename):
  609. """Uploads the file to cloud storage or saves to a temporary location."""
  610. file_sha1 = _GenerateHash(filename)
  611. if not self._options.buildbot:
  612. self._SaveFileLocally(filename, file_sha1)
  613. else:
  614. print('Uploading file for debugging: ' + filename)
  615. self._orderfile_updater.UploadToCloudStorage(
  616. filename, use_debug_location=True)
  617. def _SaveForDebuggingWithOverwrite(self, file_name):
  618. """Uploads and overwrites the file in cloud storage or copies locally.
  619. Should be used for large binaries like lib_chrome_so.
  620. Args:
  621. file_name: (str) File to upload.
  622. """
  623. file_sha1 = _GenerateHash(file_name)
  624. if not self._options.buildbot:
  625. self._SaveFileLocally(file_name, file_sha1)
  626. else:
  627. print('Uploading file for debugging: %s, sha1sum: %s' % (file_name,
  628. file_sha1))
  629. upload_location = '%s/%s' % (
  630. self._CLOUD_STORAGE_BUCKET_FOR_DEBUG, os.path.basename(file_name))
  631. self._step_recorder.RunCommand([
  632. 'gsutil.py', 'cp', file_name, 'gs://' + upload_location])
  633. print('Uploaded to: https://sandbox.google.com/storage/' +
  634. upload_location)
  635. def _MaybeArchiveOrderfile(self, filename):
  636. """In buildbot configuration, uploads the generated orderfile to
  637. Google Cloud Storage.
  638. Args:
  639. filename: (str) Orderfile to upload.
  640. """
  641. # First compute hashes so that we can download them later if we need to.
  642. self._step_recorder.BeginStep('Compute hash for ' + filename)
  643. self._RecordHash(filename)
  644. if self._options.buildbot:
  645. self._step_recorder.BeginStep('Archive ' + filename)
  646. self._orderfile_updater.UploadToCloudStorage(
  647. filename, use_debug_location=False)
  648. def UploadReadyOrderfiles(self):
  649. self._step_recorder.BeginStep('Upload Ready Orderfiles')
  650. for file_name in [self._GetUnpatchedOrderfileFilename(),
  651. self._GetPathToOrderfile()]:
  652. self._orderfile_updater.UploadToCloudStorage(
  653. file_name, use_debug_location=False)
  654. def _NativeCodeMemoryBenchmark(self, apk):
  655. """Runs system_health.memory_mobile to assess native code memory footprint.
  656. Args:
  657. apk: (str) Path to the apk.
  658. Returns:
  659. results: ([int]) Values of native code memory footprint in bytes from the
  660. benchmark results.
  661. """
  662. self._step_recorder.BeginStep("Running orderfile.memory_mobile")
  663. try:
  664. out_dir = tempfile.mkdtemp()
  665. self._profiler._RunCommand(['tools/perf/run_benchmark',
  666. '--device={}'.format(
  667. self._profiler._device.serial),
  668. '--browser=exact',
  669. '--output-format=csv',
  670. '--output-dir={}'.format(out_dir),
  671. '--reset-results',
  672. '--browser-executable={}'.format(apk),
  673. 'orderfile.memory_mobile'])
  674. out_file_path = os.path.join(out_dir, 'results.csv')
  675. if not os.path.exists(out_file_path):
  676. raise Exception('Results file not found!')
  677. results = {}
  678. with open(out_file_path, 'r') as f:
  679. reader = csv.DictReader(f)
  680. for row in reader:
  681. if not row['name'].endswith('NativeCodeResidentMemory'):
  682. continue
  683. # Note: NativeCodeResidentMemory records a single sample from each
  684. # story run, so this average (reported as 'avg') is exactly the value
  685. # of that one sample. Each story is run multiple times, so this loop
  686. # will accumulate into a list all values for all runs of each story.
  687. results.setdefault(row['name'], {}).setdefault(
  688. row['stories'], []).append(row['avg'])
  689. if not results:
  690. raise Exception('Could not find relevant results')
  691. return results
  692. except Exception as e:
  693. return 'Error: ' + str(e)
  694. finally:
  695. shutil.rmtree(out_dir)
  696. def _PerformanceBenchmark(self, apk):
  697. """Runs Speedometer2.0 to assess performance.
  698. Args:
  699. apk: (str) Path to the apk.
  700. Returns:
  701. results: ([float]) Speedometer2.0 results samples in milliseconds.
  702. """
  703. self._step_recorder.BeginStep("Running Speedometer2.0.")
  704. try:
  705. out_dir = tempfile.mkdtemp()
  706. self._profiler._RunCommand(['tools/perf/run_benchmark',
  707. '--device={}'.format(
  708. self._profiler._device.serial),
  709. '--browser=exact',
  710. '--output-format=histograms',
  711. '--output-dir={}'.format(out_dir),
  712. '--reset-results',
  713. '--browser-executable={}'.format(apk),
  714. 'speedometer2'])
  715. out_file_path = os.path.join(out_dir, 'histograms.json')
  716. if not os.path.exists(out_file_path):
  717. raise Exception('Results file not found!')
  718. with open(out_file_path, 'r') as f:
  719. results = json.load(f)
  720. if not results:
  721. raise Exception('Results file is empty.')
  722. for el in results:
  723. if 'name' in el and el['name'] == 'Total' and 'sampleValues' in el:
  724. return el['sampleValues']
  725. raise Exception('Unexpected results format.')
  726. except Exception as e:
  727. return 'Error: ' + str(e)
  728. finally:
  729. shutil.rmtree(out_dir)
  730. def RunBenchmark(self, out_directory, no_orderfile=False):
  731. """Builds chrome apk and runs performance and memory benchmarks.
  732. Builds a non-instrumented version of chrome.
  733. Installs chrome apk on the device.
  734. Runs Speedometer2.0 benchmark to assess performance.
  735. Runs system_health.memory_mobile to evaluate memory footprint.
  736. Args:
  737. out_directory: (str) Path to out directory for this build.
  738. no_orderfile: (bool) True if chrome to be built without orderfile.
  739. Returns:
  740. benchmark_results: (dict) Results extracted from benchmarks.
  741. """
  742. benchmark_results = {}
  743. try:
  744. _UnstashOutputDirectory(out_directory)
  745. self._compiler = ClankCompiler(out_directory, self._step_recorder,
  746. self._options.arch, self._options.use_goma,
  747. self._options.goma_dir,
  748. self._options.system_health_orderfile,
  749. self._monochrome, self._options.public,
  750. self._GetPathToOrderfile())
  751. if no_orderfile:
  752. orderfile_path = self._GetPathToOrderfile()
  753. backup_orderfile = orderfile_path + '.backup'
  754. shutil.move(orderfile_path, backup_orderfile)
  755. open(orderfile_path, 'w').close()
  756. # Build APK to be installed on the device.
  757. self._compiler.CompileChromeApk(instrumented=False,
  758. use_call_graph=False,
  759. force_relink=True)
  760. benchmark_results['Speedometer2.0'] = self._PerformanceBenchmark(
  761. self._compiler.chrome_apk)
  762. benchmark_results['orderfile.memory_mobile'] = (
  763. self._NativeCodeMemoryBenchmark(self._compiler.chrome_apk))
  764. except Exception as e:
  765. benchmark_results['Error'] = str(e)
  766. finally:
  767. if no_orderfile and os.path.exists(backup_orderfile):
  768. shutil.move(backup_orderfile, orderfile_path)
  769. _StashOutputDirectory(out_directory)
  770. return benchmark_results
  771. def Generate(self):
  772. """Generates and maybe upload an order."""
  773. assert (bool(self._options.profile) ^
  774. bool(self._options.manual_symbol_offsets))
  775. if self._options.system_health_orderfile and not self._options.profile:
  776. raise AssertionError('--system_health_orderfile must be not be used '
  777. 'with --skip-profile')
  778. if (self._options.manual_symbol_offsets and
  779. not self._options.system_health_orderfile):
  780. raise AssertionError('--manual-symbol-offsets must be used with '
  781. '--system_health_orderfile.')
  782. if self._options.profile:
  783. try:
  784. _UnstashOutputDirectory(self._instrumented_out_dir)
  785. self._compiler = ClankCompiler(
  786. self._instrumented_out_dir, self._step_recorder, self._options.arch,
  787. self._options.use_goma, self._options.goma_dir,
  788. self._options.system_health_orderfile, self._monochrome,
  789. self._options.public, self._GetPathToOrderfile())
  790. if not self._options.pregenerated_profiles:
  791. # If there are pregenerated profiles, the instrumented build should
  792. # not be changed to avoid invalidating the pregenerated profile
  793. # offsets.
  794. self._compiler.CompileChromeApk(instrumented=True,
  795. use_call_graph=
  796. self._options.use_call_graph)
  797. self._GenerateAndProcessProfile()
  798. self._MaybeArchiveOrderfile(self._GetUnpatchedOrderfileFilename())
  799. finally:
  800. _StashOutputDirectory(self._instrumented_out_dir)
  801. elif self._options.manual_symbol_offsets:
  802. assert self._options.manual_libname
  803. assert self._options.manual_objdir
  804. with open(self._options.manual_symbol_offsets) as f:
  805. symbol_offsets = [int(x) for x in f]
  806. processor = process_profiles.SymbolOffsetProcessor(
  807. self._options.manual_libname)
  808. generator = cyglog_to_orderfile.OffsetOrderfileGenerator(
  809. processor, cyglog_to_orderfile.ObjectFileProcessor(
  810. self._options.manual_objdir))
  811. ordered_sections = generator.GetOrderedSections(symbol_offsets)
  812. if not ordered_sections: # Either None or empty is a problem.
  813. raise Exception('Failed to get ordered sections')
  814. with open(self._GetUnpatchedOrderfileFilename(), 'w') as orderfile:
  815. orderfile.write('\n'.join(ordered_sections))
  816. if self._options.patch:
  817. if self._options.profile:
  818. self._RemoveBlanks(self._GetUnpatchedOrderfileFilename(),
  819. self._GetPathToOrderfile())
  820. try:
  821. _UnstashOutputDirectory(self._uninstrumented_out_dir)
  822. self._compiler = ClankCompiler(
  823. self._uninstrumented_out_dir, self._step_recorder,
  824. self._options.arch, self._options.use_goma, self._options.goma_dir,
  825. self._options.system_health_orderfile, self._monochrome,
  826. self._options.public, self._GetPathToOrderfile())
  827. self._compiler.CompileLibchrome(instrumented=False,
  828. use_call_graph=False)
  829. self._PatchOrderfile()
  830. # Because identical code folding is a bit different with and without
  831. # the orderfile build, we need to re-patch the orderfile with code
  832. # folding as close to the final version as possible.
  833. self._compiler.CompileLibchrome(instrumented=False,
  834. use_call_graph=False, force_relink=True)
  835. self._PatchOrderfile()
  836. self._compiler.CompileLibchrome(instrumented=False,
  837. use_call_graph=False, force_relink=True)
  838. self._VerifySymbolOrder()
  839. self._MaybeArchiveOrderfile(self._GetPathToOrderfile())
  840. finally:
  841. _StashOutputDirectory(self._uninstrumented_out_dir)
  842. if self._options.benchmark:
  843. self._output_data['orderfile_benchmark_results'] = self.RunBenchmark(
  844. self._uninstrumented_out_dir)
  845. self._output_data['no_orderfile_benchmark_results'] = self.RunBenchmark(
  846. self._no_orderfile_out_dir, no_orderfile=True)
  847. if self._options.buildbot:
  848. self._orderfile_updater._GitStash()
  849. self._step_recorder.EndStep()
  850. return not self._step_recorder.ErrorRecorded()
  851. def GetReportingData(self):
  852. """Get a dictionary of reporting data (timings, output hashes)"""
  853. self._output_data['timings'] = self._step_recorder.timings
  854. return self._output_data
  855. def CommitStashedOrderfileHashes(self):
  856. """Commit any orderfile hash files in the current checkout.
  857. Only possible if running on the buildbot.
  858. Returns: true on success.
  859. """
  860. if not self._options.buildbot:
  861. logging.error('Trying to commit when not running on the buildbot')
  862. return False
  863. self._orderfile_updater._CommitStashedFiles([
  864. filename + '.sha1'
  865. for filename in (self._GetUnpatchedOrderfileFilename(),
  866. self._GetPathToOrderfile())])
  867. return True
  868. def CreateArgumentParser():
  869. """Creates and returns the argument parser."""
  870. parser = argparse.ArgumentParser()
  871. parser.add_argument('--no-benchmark', action='store_false', dest='benchmark',
  872. default=True, help='Disables running benchmarks.')
  873. parser.add_argument(
  874. '--buildbot', action='store_true',
  875. help='If true, the script expects to be run on a buildbot')
  876. parser.add_argument(
  877. '--device', default=None, type=str,
  878. help='Device serial number on which to run profiling.')
  879. parser.add_argument(
  880. '--verify', action='store_true',
  881. help='If true, the script only verifies the current orderfile')
  882. parser.add_argument('--target-arch',
  883. action='store',
  884. dest='arch',
  885. default='arm',
  886. choices=list(_ARCH_GN_ARGS.keys()),
  887. help='The target architecture for which to build.')
  888. parser.add_argument('--output-json', action='store', dest='json_file',
  889. help='Location to save stats in json format')
  890. parser.add_argument(
  891. '--skip-profile', action='store_false', dest='profile', default=True,
  892. help='Don\'t generate a profile on the device. Only patch from the '
  893. 'existing profile.')
  894. parser.add_argument(
  895. '--skip-patch', action='store_false', dest='patch', default=True,
  896. help='Only generate the raw (unpatched) orderfile, don\'t patch it.')
  897. parser.add_argument('--goma-dir', help='GOMA directory.')
  898. parser.add_argument(
  899. '--use-goma', action='store_true', help='Enable GOMA.', default=False)
  900. parser.add_argument('--adb-path', help='Path to the adb binary.')
  901. parser.add_argument('--public',
  902. action='store_true',
  903. help='Build non-internal APK and change the orderfile '
  904. 'location. Required if your checkout is non-internal.',
  905. default=False)
  906. parser.add_argument('--nosystem-health-orderfile', action='store_false',
  907. dest='system_health_orderfile', default=True,
  908. help=('Create an orderfile based on an about:blank '
  909. 'startup benchmark instead of system health '
  910. 'benchmarks.'))
  911. parser.add_argument(
  912. '--use-legacy-chrome-apk', action='store_true', default=False,
  913. help=('Compile and instrument chrome for [L, K] devices.'))
  914. parser.add_argument('--manual-symbol-offsets', default=None, type=str,
  915. help=('File of list of ordered symbol offsets generated '
  916. 'by manual profiling. Must set other --manual* '
  917. 'flags if this is used, and must --skip-profile.'))
  918. parser.add_argument('--manual-libname', default=None, type=str,
  919. help=('Library filename corresponding to '
  920. '--manual-symbol-offsets.'))
  921. parser.add_argument('--manual-objdir', default=None, type=str,
  922. help=('Root of object file directory corresponding to '
  923. '--manual-symbol-offsets.'))
  924. parser.add_argument('--noorder-outlined-functions', action='store_true',
  925. help='Disable outlined functions in the orderfile.')
  926. parser.add_argument('--pregenerated-profiles', default=None, type=str,
  927. help=('Pregenerated profiles to use instead of running '
  928. 'profile step. Cannot be used with '
  929. '--skip-profiles.'))
  930. parser.add_argument('--profile-save-dir', default=None, type=str,
  931. help=('Directory to save any profiles created. These can '
  932. 'be used with --pregenerated-profiles. Cannot be '
  933. 'used with --skip-profiles.'))
  934. parser.add_argument('--upload-ready-orderfiles', action='store_true',
  935. help=('Skip orderfile generation and manually upload '
  936. 'orderfiles (both patched and unpatched) from '
  937. 'their normal location in the tree to the cloud '
  938. 'storage. DANGEROUS! USE WITH CARE!'))
  939. parser.add_argument('--streamline-for-debugging', action='store_true',
  940. help=('Streamline where possible the run for faster '
  941. 'iteration while debugging. The orderfile '
  942. 'generated will be valid and nontrivial, but '
  943. 'may not be based on a representative profile '
  944. 'or other such considerations. Use with caution.'))
  945. parser.add_argument('--commit-hashes', action='store_true',
  946. help=('Commit any orderfile hash files in the current '
  947. 'checkout; performs no other action'))
  948. parser.add_argument('--use-call-graph', action='store_true', default=False,
  949. help='Use call graph instrumentation.')
  950. profile_android_startup.AddProfileCollectionArguments(parser)
  951. return parser
  952. def CreateOrderfile(options, orderfile_updater_class=None):
  953. """Creates an orderfile.
  954. Args:
  955. options: As returned from optparse.OptionParser.parse_args()
  956. orderfile_updater_class: (OrderfileUpdater) subclass of OrderfileUpdater.
  957. Returns:
  958. True iff success.
  959. """
  960. logging.basicConfig(level=logging.INFO)
  961. devil_chromium.Initialize(adb_path=options.adb_path)
  962. # Since we generate a ".arm" orderfile irrespective of the architecture (see
  963. # comment in _GetPathToOrderfile()), make sure that we don't commit it.
  964. if options.arch != 'arm':
  965. assert not options.buildbot, (
  966. 'ARM is the only supported architecture on bots')
  967. assert not options.upload_ready_orderfiles, (
  968. 'ARM is the only supported architecture on bots')
  969. generator = OrderfileGenerator(options, orderfile_updater_class)
  970. try:
  971. if options.verify:
  972. generator._VerifySymbolOrder()
  973. elif options.commit_hashes:
  974. return generator.CommitStashedOrderfileHashes()
  975. elif options.upload_ready_orderfiles:
  976. return generator.UploadReadyOrderfiles()
  977. else:
  978. return generator.Generate()
  979. finally:
  980. json_output = json.dumps(generator.GetReportingData(),
  981. indent=2) + '\n'
  982. if options.json_file:
  983. with open(options.json_file, 'w') as f:
  984. f.write(json_output)
  985. print(json_output)
  986. return False
  987. def main():
  988. parser = CreateArgumentParser()
  989. options = parser.parse_args()
  990. return 0 if CreateOrderfile(options) else 1
  991. if __name__ == '__main__':
  992. sys.exit(main())