bootgraph.py 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. #!/usr/bin/python2
  2. #
  3. # Tool for analyzing boot timing
  4. # Copyright (c) 2013, Intel Corporation.
  5. #
  6. # This program is free software; you can redistribute it and/or modify it
  7. # under the terms and conditions of the GNU General Public License,
  8. # version 2, as published by the Free Software Foundation.
  9. #
  10. # This program is distributed in the hope it will be useful, but WITHOUT
  11. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. # more details.
  14. #
  15. # Authors:
  16. # Todd Brandt <todd.e.brandt@linux.intel.com>
  17. #
  18. # Description:
  19. # This tool is designed to assist kernel and OS developers in optimizing
  20. # their linux stack's boot time. It creates an html representation of
  21. # the kernel boot timeline up to the start of the init process.
  22. #
  23. # ----------------- LIBRARIES --------------------
  24. import sys
  25. import time
  26. import os
  27. import string
  28. import re
  29. import platform
  30. import shutil
  31. from datetime import datetime, timedelta
  32. from subprocess import call, Popen, PIPE
  33. import sleepgraph as aslib
  34. def pprint(msg):
  35. print(msg)
  36. sys.stdout.flush()
  37. # ----------------- CLASSES --------------------
  38. # Class: SystemValues
  39. # Description:
  40. # A global, single-instance container used to
  41. # store system values and test parameters
  42. class SystemValues(aslib.SystemValues):
  43. title = 'BootGraph'
  44. version = '2.2'
  45. hostname = 'localhost'
  46. testtime = ''
  47. kernel = ''
  48. dmesgfile = ''
  49. ftracefile = ''
  50. htmlfile = 'bootgraph.html'
  51. testdir = ''
  52. kparams = ''
  53. result = ''
  54. useftrace = False
  55. usecallgraph = False
  56. suspendmode = 'boot'
  57. max_graph_depth = 2
  58. graph_filter = 'do_one_initcall'
  59. reboot = False
  60. manual = False
  61. iscronjob = False
  62. timeformat = '%.6f'
  63. bootloader = 'grub'
  64. blexec = []
  65. def __init__(self):
  66. self.hostname = platform.node()
  67. self.testtime = datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
  68. if os.path.exists('/proc/version'):
  69. fp = open('/proc/version', 'r')
  70. val = fp.read().strip()
  71. fp.close()
  72. self.kernel = self.kernelVersion(val)
  73. else:
  74. self.kernel = 'unknown'
  75. self.testdir = datetime.now().strftime('boot-%y%m%d-%H%M%S')
  76. def kernelVersion(self, msg):
  77. return msg.split()[2]
  78. def checkFtraceKernelVersion(self):
  79. val = tuple(map(int, self.kernel.split('-')[0].split('.')))
  80. if val >= (4, 10, 0):
  81. return True
  82. return False
  83. def kernelParams(self):
  84. cmdline = 'initcall_debug log_buf_len=32M'
  85. if self.useftrace:
  86. if self.cpucount > 0:
  87. bs = min(self.memtotal / 2, 2*1024*1024) / self.cpucount
  88. else:
  89. bs = 131072
  90. cmdline += ' trace_buf_size=%dK trace_clock=global '\
  91. 'trace_options=nooverwrite,funcgraph-abstime,funcgraph-cpu,'\
  92. 'funcgraph-duration,funcgraph-proc,funcgraph-tail,'\
  93. 'nofuncgraph-overhead,context-info,graph-time '\
  94. 'ftrace=function_graph '\
  95. 'ftrace_graph_max_depth=%d '\
  96. 'ftrace_graph_filter=%s' % \
  97. (bs, self.max_graph_depth, self.graph_filter)
  98. return cmdline
  99. def setGraphFilter(self, val):
  100. master = self.getBootFtraceFilterFunctions()
  101. fs = ''
  102. for i in val.split(','):
  103. func = i.strip()
  104. if func == '':
  105. doError('badly formatted filter function string')
  106. if '[' in func or ']' in func:
  107. doError('loadable module functions not allowed - "%s"' % func)
  108. if ' ' in func:
  109. doError('spaces found in filter functions - "%s"' % func)
  110. if func not in master:
  111. doError('function "%s" not available for ftrace' % func)
  112. if not fs:
  113. fs = func
  114. else:
  115. fs += ','+func
  116. if not fs:
  117. doError('badly formatted filter function string')
  118. self.graph_filter = fs
  119. def getBootFtraceFilterFunctions(self):
  120. self.rootCheck(True)
  121. fp = open(self.tpath+'available_filter_functions')
  122. fulllist = fp.read().split('\n')
  123. fp.close()
  124. list = []
  125. for i in fulllist:
  126. if not i or ' ' in i or '[' in i or ']' in i:
  127. continue
  128. list.append(i)
  129. return list
  130. def myCronJob(self, line):
  131. if '@reboot' not in line:
  132. return False
  133. if 'bootgraph' in line or 'analyze_boot.py' in line or '-cronjob' in line:
  134. return True
  135. return False
  136. def cronjobCmdString(self):
  137. cmdline = '%s -cronjob' % os.path.abspath(sys.argv[0])
  138. args = iter(sys.argv[1:])
  139. for arg in args:
  140. if arg in ['-h', '-v', '-cronjob', '-reboot', '-verbose']:
  141. continue
  142. elif arg in ['-o', '-dmesg', '-ftrace', '-func']:
  143. args.next()
  144. continue
  145. elif arg == '-result':
  146. cmdline += ' %s "%s"' % (arg, os.path.abspath(args.next()))
  147. continue
  148. elif arg == '-cgskip':
  149. file = self.configFile(args.next())
  150. cmdline += ' %s "%s"' % (arg, os.path.abspath(file))
  151. continue
  152. cmdline += ' '+arg
  153. if self.graph_filter != 'do_one_initcall':
  154. cmdline += ' -func "%s"' % self.graph_filter
  155. cmdline += ' -o "%s"' % os.path.abspath(self.testdir)
  156. return cmdline
  157. def manualRebootRequired(self):
  158. cmdline = self.kernelParams()
  159. pprint('To generate a new timeline manually, follow these steps:\n\n'\
  160. '1. Add the CMDLINE string to your kernel command line.\n'\
  161. '2. Reboot the system.\n'\
  162. '3. After reboot, re-run this tool with the same arguments but no command (w/o -reboot or -manual).\n\n'\
  163. 'CMDLINE="%s"' % cmdline)
  164. sys.exit()
  165. def blGrub(self):
  166. blcmd = ''
  167. for cmd in ['update-grub', 'grub-mkconfig', 'grub2-mkconfig']:
  168. if blcmd:
  169. break
  170. blcmd = self.getExec(cmd)
  171. if not blcmd:
  172. doError('[GRUB] missing update command')
  173. if not os.path.exists('/etc/default/grub'):
  174. doError('[GRUB] missing /etc/default/grub')
  175. if 'grub2' in blcmd:
  176. cfg = '/boot/grub2/grub.cfg'
  177. else:
  178. cfg = '/boot/grub/grub.cfg'
  179. if not os.path.exists(cfg):
  180. doError('[GRUB] missing %s' % cfg)
  181. if 'update-grub' in blcmd:
  182. self.blexec = [blcmd]
  183. else:
  184. self.blexec = [blcmd, '-o', cfg]
  185. def getBootLoader(self):
  186. if self.bootloader == 'grub':
  187. self.blGrub()
  188. else:
  189. doError('unknown boot loader: %s' % self.bootloader)
  190. def writeDatafileHeader(self, filename):
  191. self.kparams = open('/proc/cmdline', 'r').read().strip()
  192. fp = open(filename, 'w')
  193. fp.write(self.teststamp+'\n')
  194. fp.write(self.sysstamp+'\n')
  195. fp.write('# command | %s\n' % self.cmdline)
  196. fp.write('# kparams | %s\n' % self.kparams)
  197. fp.close()
  198. sysvals = SystemValues()
  199. # Class: Data
  200. # Description:
  201. # The primary container for test data.
  202. class Data(aslib.Data):
  203. dmesg = {} # root data structure
  204. start = 0.0 # test start
  205. end = 0.0 # test end
  206. dmesgtext = [] # dmesg text file in memory
  207. testnumber = 0
  208. idstr = ''
  209. html_device_id = 0
  210. valid = False
  211. tUserMode = 0.0
  212. boottime = ''
  213. phases = ['kernel', 'user']
  214. do_one_initcall = False
  215. def __init__(self, num):
  216. self.testnumber = num
  217. self.idstr = 'a'
  218. self.dmesgtext = []
  219. self.dmesg = {
  220. 'kernel': {'list': dict(), 'start': -1.0, 'end': -1.0, 'row': 0,
  221. 'order': 0, 'color': 'linear-gradient(to bottom, #fff, #bcf)'},
  222. 'user': {'list': dict(), 'start': -1.0, 'end': -1.0, 'row': 0,
  223. 'order': 1, 'color': '#fff'}
  224. }
  225. def deviceTopology(self):
  226. return ''
  227. def newAction(self, phase, name, pid, start, end, ret, ulen):
  228. # new device callback for a specific phase
  229. self.html_device_id += 1
  230. devid = '%s%d' % (self.idstr, self.html_device_id)
  231. list = self.dmesg[phase]['list']
  232. length = -1.0
  233. if(start >= 0 and end >= 0):
  234. length = end - start
  235. i = 2
  236. origname = name
  237. while(name in list):
  238. name = '%s[%d]' % (origname, i)
  239. i += 1
  240. list[name] = {'name': name, 'start': start, 'end': end,
  241. 'pid': pid, 'length': length, 'row': 0, 'id': devid,
  242. 'ret': ret, 'ulen': ulen }
  243. return name
  244. def deviceMatch(self, pid, cg):
  245. if cg.end - cg.start == 0:
  246. return ''
  247. for p in data.phases:
  248. list = self.dmesg[p]['list']
  249. for devname in list:
  250. dev = list[devname]
  251. if pid != dev['pid']:
  252. continue
  253. if cg.name == 'do_one_initcall':
  254. if(cg.start <= dev['start'] and cg.end >= dev['end'] and dev['length'] > 0):
  255. dev['ftrace'] = cg
  256. self.do_one_initcall = True
  257. return devname
  258. else:
  259. if(cg.start > dev['start'] and cg.end < dev['end']):
  260. if 'ftraces' not in dev:
  261. dev['ftraces'] = []
  262. dev['ftraces'].append(cg)
  263. return devname
  264. return ''
  265. def printDetails(self):
  266. sysvals.vprint('Timeline Details:')
  267. sysvals.vprint(' Host: %s' % sysvals.hostname)
  268. sysvals.vprint(' Kernel: %s' % sysvals.kernel)
  269. sysvals.vprint(' Test time: %s' % sysvals.testtime)
  270. sysvals.vprint(' Boot time: %s' % self.boottime)
  271. for phase in self.phases:
  272. dc = len(self.dmesg[phase]['list'])
  273. sysvals.vprint('%9s mode: %.3f - %.3f (%d initcalls)' % (phase,
  274. self.dmesg[phase]['start']*1000,
  275. self.dmesg[phase]['end']*1000, dc))
  276. # ----------------- FUNCTIONS --------------------
  277. # Function: parseKernelLog
  278. # Description:
  279. # parse a kernel log for boot data
  280. def parseKernelLog():
  281. sysvals.vprint('Analyzing the dmesg data (%s)...' % \
  282. os.path.basename(sysvals.dmesgfile))
  283. phase = 'kernel'
  284. data = Data(0)
  285. data.dmesg['kernel']['start'] = data.start = ktime = 0.0
  286. sysvals.stamp = {
  287. 'time': datetime.now().strftime('%B %d %Y, %I:%M:%S %p'),
  288. 'host': sysvals.hostname,
  289. 'mode': 'boot', 'kernel': ''}
  290. tp = aslib.TestProps()
  291. devtemp = dict()
  292. if(sysvals.dmesgfile):
  293. lf = open(sysvals.dmesgfile, 'r')
  294. else:
  295. lf = Popen('dmesg', stdout=PIPE).stdout
  296. for line in lf:
  297. line = line.replace('\r\n', '')
  298. # grab the stamp and sysinfo
  299. if re.match(tp.stampfmt, line):
  300. tp.stamp = line
  301. continue
  302. elif re.match(tp.sysinfofmt, line):
  303. tp.sysinfo = line
  304. continue
  305. elif re.match(tp.cmdlinefmt, line):
  306. tp.cmdline = line
  307. continue
  308. elif re.match(tp.kparamsfmt, line):
  309. tp.kparams = line
  310. continue
  311. idx = line.find('[')
  312. if idx > 1:
  313. line = line[idx:]
  314. m = re.match('[ \t]*(\[ *)(?P<ktime>[0-9\.]*)(\]) (?P<msg>.*)', line)
  315. if(not m):
  316. continue
  317. ktime = float(m.group('ktime'))
  318. if(ktime > 120):
  319. break
  320. msg = m.group('msg')
  321. data.dmesgtext.append(line)
  322. if(ktime == 0.0 and re.match('^Linux version .*', msg)):
  323. if(not sysvals.stamp['kernel']):
  324. sysvals.stamp['kernel'] = sysvals.kernelVersion(msg)
  325. continue
  326. m = re.match('.* setting system clock to (?P<t>.*) UTC.*', msg)
  327. if(m):
  328. bt = datetime.strptime(m.group('t'), '%Y-%m-%d %H:%M:%S')
  329. bt = bt - timedelta(seconds=int(ktime))
  330. data.boottime = bt.strftime('%Y-%m-%d_%H:%M:%S')
  331. sysvals.stamp['time'] = bt.strftime('%B %d %Y, %I:%M:%S %p')
  332. continue
  333. m = re.match('^calling *(?P<f>.*)\+.* @ (?P<p>[0-9]*)', msg)
  334. if(m):
  335. func = m.group('f')
  336. pid = int(m.group('p'))
  337. devtemp[func] = (ktime, pid)
  338. continue
  339. m = re.match('^initcall *(?P<f>.*)\+.* returned (?P<r>.*) after (?P<t>.*) usecs', msg)
  340. if(m):
  341. data.valid = True
  342. data.end = ktime
  343. f, r, t = m.group('f', 'r', 't')
  344. if(f in devtemp):
  345. start, pid = devtemp[f]
  346. data.newAction(phase, f, pid, start, ktime, int(r), int(t))
  347. del devtemp[f]
  348. continue
  349. if(re.match('^Freeing unused kernel memory.*', msg)):
  350. data.tUserMode = ktime
  351. data.dmesg['kernel']['end'] = ktime
  352. data.dmesg['user']['start'] = ktime
  353. phase = 'user'
  354. if tp.stamp:
  355. sysvals.stamp = 0
  356. tp.parseStamp(data, sysvals)
  357. data.dmesg['user']['end'] = data.end
  358. lf.close()
  359. return data
  360. # Function: parseTraceLog
  361. # Description:
  362. # Check if trace is available and copy to a temp file
  363. def parseTraceLog(data):
  364. sysvals.vprint('Analyzing the ftrace data (%s)...' % \
  365. os.path.basename(sysvals.ftracefile))
  366. # if available, calculate cgfilter allowable ranges
  367. cgfilter = []
  368. if len(sysvals.cgfilter) > 0:
  369. for p in data.phases:
  370. list = data.dmesg[p]['list']
  371. for i in sysvals.cgfilter:
  372. if i in list:
  373. cgfilter.append([list[i]['start']-0.0001,
  374. list[i]['end']+0.0001])
  375. # parse the trace log
  376. ftemp = dict()
  377. tp = aslib.TestProps()
  378. tp.setTracerType('function_graph')
  379. tf = open(sysvals.ftracefile, 'r')
  380. for line in tf:
  381. if line[0] == '#':
  382. continue
  383. m = re.match(tp.ftrace_line_fmt, line.strip())
  384. if(not m):
  385. continue
  386. m_time, m_proc, m_pid, m_msg, m_dur = \
  387. m.group('time', 'proc', 'pid', 'msg', 'dur')
  388. t = float(m_time)
  389. if len(cgfilter) > 0:
  390. allow = False
  391. for r in cgfilter:
  392. if t >= r[0] and t < r[1]:
  393. allow = True
  394. break
  395. if not allow:
  396. continue
  397. if t > data.end:
  398. break
  399. if(m_time and m_pid and m_msg):
  400. t = aslib.FTraceLine(m_time, m_msg, m_dur)
  401. pid = int(m_pid)
  402. else:
  403. continue
  404. if t.fevent or t.fkprobe:
  405. continue
  406. key = (m_proc, pid)
  407. if(key not in ftemp):
  408. ftemp[key] = []
  409. ftemp[key].append(aslib.FTraceCallGraph(pid, sysvals))
  410. cg = ftemp[key][-1]
  411. res = cg.addLine(t)
  412. if(res != 0):
  413. ftemp[key].append(aslib.FTraceCallGraph(pid, sysvals))
  414. if(res == -1):
  415. ftemp[key][-1].addLine(t)
  416. tf.close()
  417. # add the callgraph data to the device hierarchy
  418. for key in ftemp:
  419. proc, pid = key
  420. for cg in ftemp[key]:
  421. if len(cg.list) < 1 or cg.invalid or (cg.end - cg.start == 0):
  422. continue
  423. if(not cg.postProcess()):
  424. pprint('Sanity check failed for %s-%d' % (proc, pid))
  425. continue
  426. # match cg data to devices
  427. devname = data.deviceMatch(pid, cg)
  428. if not devname:
  429. kind = 'Orphan'
  430. if cg.partial:
  431. kind = 'Partial'
  432. sysvals.vprint('%s callgraph found for %s %s-%d [%f - %f]' %\
  433. (kind, cg.name, proc, pid, cg.start, cg.end))
  434. elif len(cg.list) > 1000000:
  435. pprint('WARNING: the callgraph found for %s is massive! (%d lines)' %\
  436. (devname, len(cg.list)))
  437. # Function: retrieveLogs
  438. # Description:
  439. # Create copies of dmesg and/or ftrace for later processing
  440. def retrieveLogs():
  441. # check ftrace is configured first
  442. if sysvals.useftrace:
  443. tracer = sysvals.fgetVal('current_tracer').strip()
  444. if tracer != 'function_graph':
  445. doError('ftrace not configured for a boot callgraph')
  446. # create the folder and get dmesg
  447. sysvals.systemInfo(aslib.dmidecode(sysvals.mempath))
  448. sysvals.initTestOutput('boot')
  449. sysvals.writeDatafileHeader(sysvals.dmesgfile)
  450. call('dmesg >> '+sysvals.dmesgfile, shell=True)
  451. if not sysvals.useftrace:
  452. return
  453. # get ftrace
  454. sysvals.writeDatafileHeader(sysvals.ftracefile)
  455. call('cat '+sysvals.tpath+'trace >> '+sysvals.ftracefile, shell=True)
  456. # Function: colorForName
  457. # Description:
  458. # Generate a repeatable color from a list for a given name
  459. def colorForName(name):
  460. list = [
  461. ('c1', '#ec9999'),
  462. ('c2', '#ffc1a6'),
  463. ('c3', '#fff0a6'),
  464. ('c4', '#adf199'),
  465. ('c5', '#9fadea'),
  466. ('c6', '#a699c1'),
  467. ('c7', '#ad99b4'),
  468. ('c8', '#eaffea'),
  469. ('c9', '#dcecfb'),
  470. ('c10', '#ffffea')
  471. ]
  472. i = 0
  473. total = 0
  474. count = len(list)
  475. while i < len(name):
  476. total += ord(name[i])
  477. i += 1
  478. return list[total % count]
  479. def cgOverview(cg, minlen):
  480. stats = dict()
  481. large = []
  482. for l in cg.list:
  483. if l.fcall and l.depth == 1:
  484. if l.length >= minlen:
  485. large.append(l)
  486. if l.name not in stats:
  487. stats[l.name] = [0, 0.0]
  488. stats[l.name][0] += (l.length * 1000.0)
  489. stats[l.name][1] += 1
  490. return (large, stats)
  491. # Function: createBootGraph
  492. # Description:
  493. # Create the output html file from the resident test data
  494. # Arguments:
  495. # testruns: array of Data objects from parseKernelLog or parseTraceLog
  496. # Output:
  497. # True if the html file was created, false if it failed
  498. def createBootGraph(data):
  499. # html function templates
  500. html_srccall = '<div id={6} title="{5}" class="srccall" style="left:{1}%;top:{2}px;height:{3}px;width:{4}%;line-height:{3}px;">{0}</div>\n'
  501. html_timetotal = '<table class="time1">\n<tr>'\
  502. '<td class="blue">Init process starts @ <b>{0} ms</b></td>'\
  503. '<td class="blue">Last initcall ends @ <b>{1} ms</b></td>'\
  504. '</tr>\n</table>\n'
  505. # device timeline
  506. devtl = aslib.Timeline(100, 20)
  507. # write the test title and general info header
  508. devtl.createHeader(sysvals, sysvals.stamp)
  509. # Generate the header for this timeline
  510. t0 = data.start
  511. tMax = data.end
  512. tTotal = tMax - t0
  513. if(tTotal == 0):
  514. pprint('ERROR: No timeline data')
  515. return False
  516. user_mode = '%.0f'%(data.tUserMode*1000)
  517. last_init = '%.0f'%(tTotal*1000)
  518. devtl.html += html_timetotal.format(user_mode, last_init)
  519. # determine the maximum number of rows we need to draw
  520. devlist = []
  521. for p in data.phases:
  522. list = data.dmesg[p]['list']
  523. for devname in list:
  524. d = aslib.DevItem(0, p, list[devname])
  525. devlist.append(d)
  526. devtl.getPhaseRows(devlist, 0, 'start')
  527. devtl.calcTotalRows()
  528. # draw the timeline background
  529. devtl.createZoomBox()
  530. devtl.html += devtl.html_tblock.format('boot', '0', '100', devtl.scaleH)
  531. for p in data.phases:
  532. phase = data.dmesg[p]
  533. length = phase['end']-phase['start']
  534. left = '%.3f' % (((phase['start']-t0)*100.0)/tTotal)
  535. width = '%.3f' % ((length*100.0)/tTotal)
  536. devtl.html += devtl.html_phase.format(left, width, \
  537. '%.3f'%devtl.scaleH, '%.3f'%devtl.bodyH, \
  538. phase['color'], '')
  539. # draw the device timeline
  540. num = 0
  541. devstats = dict()
  542. for phase in data.phases:
  543. list = data.dmesg[phase]['list']
  544. for devname in sorted(list):
  545. cls, color = colorForName(devname)
  546. dev = list[devname]
  547. info = '@|%.3f|%.3f|%.3f|%d' % (dev['start']*1000.0, dev['end']*1000.0,
  548. dev['ulen']/1000.0, dev['ret'])
  549. devstats[dev['id']] = {'info':info}
  550. dev['color'] = color
  551. height = devtl.phaseRowHeight(0, phase, dev['row'])
  552. top = '%.6f' % ((dev['row']*height) + devtl.scaleH)
  553. left = '%.6f' % (((dev['start']-t0)*100)/tTotal)
  554. width = '%.6f' % (((dev['end']-dev['start'])*100)/tTotal)
  555. length = ' (%0.3f ms) ' % ((dev['end']-dev['start'])*1000)
  556. devtl.html += devtl.html_device.format(dev['id'],
  557. devname+length+phase+'_mode', left, top, '%.3f'%height,
  558. width, devname, ' '+cls, '')
  559. rowtop = devtl.phaseRowTop(0, phase, dev['row'])
  560. height = '%.6f' % (devtl.rowH / 2)
  561. top = '%.6f' % (rowtop + devtl.scaleH + (devtl.rowH / 2))
  562. if data.do_one_initcall:
  563. if('ftrace' not in dev):
  564. continue
  565. cg = dev['ftrace']
  566. large, stats = cgOverview(cg, 0.001)
  567. devstats[dev['id']]['fstat'] = stats
  568. for l in large:
  569. left = '%f' % (((l.time-t0)*100)/tTotal)
  570. width = '%f' % (l.length*100/tTotal)
  571. title = '%s (%0.3fms)' % (l.name, l.length * 1000.0)
  572. devtl.html += html_srccall.format(l.name, left,
  573. top, height, width, title, 'x%d'%num)
  574. num += 1
  575. continue
  576. if('ftraces' not in dev):
  577. continue
  578. for cg in dev['ftraces']:
  579. left = '%f' % (((cg.start-t0)*100)/tTotal)
  580. width = '%f' % ((cg.end-cg.start)*100/tTotal)
  581. cglen = (cg.end - cg.start) * 1000.0
  582. title = '%s (%0.3fms)' % (cg.name, cglen)
  583. cg.id = 'x%d' % num
  584. devtl.html += html_srccall.format(cg.name, left,
  585. top, height, width, title, dev['id']+cg.id)
  586. num += 1
  587. # draw the time scale, try to make the number of labels readable
  588. devtl.createTimeScale(t0, tMax, tTotal, 'boot')
  589. devtl.html += '</div>\n'
  590. # timeline is finished
  591. devtl.html += '</div>\n</div>\n'
  592. # draw a legend which describes the phases by color
  593. devtl.html += '<div class="legend">\n'
  594. pdelta = 20.0
  595. pmargin = 36.0
  596. for phase in data.phases:
  597. order = '%.2f' % ((data.dmesg[phase]['order'] * pdelta) + pmargin)
  598. devtl.html += devtl.html_legend.format(order, \
  599. data.dmesg[phase]['color'], phase+'_mode', phase[0])
  600. devtl.html += '</div>\n'
  601. hf = open(sysvals.htmlfile, 'w')
  602. # add the css
  603. extra = '\
  604. .c1 {background:rgba(209,0,0,0.4);}\n\
  605. .c2 {background:rgba(255,102,34,0.4);}\n\
  606. .c3 {background:rgba(255,218,33,0.4);}\n\
  607. .c4 {background:rgba(51,221,0,0.4);}\n\
  608. .c5 {background:rgba(17,51,204,0.4);}\n\
  609. .c6 {background:rgba(34,0,102,0.4);}\n\
  610. .c7 {background:rgba(51,0,68,0.4);}\n\
  611. .c8 {background:rgba(204,255,204,0.4);}\n\
  612. .c9 {background:rgba(169,208,245,0.4);}\n\
  613. .c10 {background:rgba(255,255,204,0.4);}\n\
  614. .vt {transform:rotate(-60deg);transform-origin:0 0;}\n\
  615. table.fstat {table-layout:fixed;padding:150px 15px 0 0;font-size:10px;column-width:30px;}\n\
  616. .fstat th {width:55px;}\n\
  617. .fstat td {text-align:left;width:35px;}\n\
  618. .srccall {position:absolute;font-size:10px;z-index:7;overflow:hidden;color:black;text-align:center;white-space:nowrap;border-radius:5px;border:1px solid black;background:linear-gradient(to bottom right,#CCC,#969696);}\n\
  619. .srccall:hover {color:white;font-weight:bold;border:1px solid white;}\n'
  620. aslib.addCSS(hf, sysvals, 1, False, extra)
  621. # write the device timeline
  622. hf.write(devtl.html)
  623. # add boot specific html
  624. statinfo = 'var devstats = {\n'
  625. for n in sorted(devstats):
  626. statinfo += '\t"%s": [\n\t\t"%s",\n' % (n, devstats[n]['info'])
  627. if 'fstat' in devstats[n]:
  628. funcs = devstats[n]['fstat']
  629. for f in sorted(funcs, key=funcs.get, reverse=True):
  630. if funcs[f][0] < 0.01 and len(funcs) > 10:
  631. break
  632. statinfo += '\t\t"%f|%s|%d",\n' % (funcs[f][0], f, funcs[f][1])
  633. statinfo += '\t],\n'
  634. statinfo += '};\n'
  635. html = \
  636. '<div id="devicedetailtitle"></div>\n'\
  637. '<div id="devicedetail" style="display:none;">\n'\
  638. '<div id="devicedetail0">\n'
  639. for p in data.phases:
  640. phase = data.dmesg[p]
  641. html += devtl.html_phaselet.format(p+'_mode', '0', '100', phase['color'])
  642. html += '</div>\n</div>\n'\
  643. '<script type="text/javascript">\n'+statinfo+\
  644. '</script>\n'
  645. hf.write(html)
  646. # add the callgraph html
  647. if(sysvals.usecallgraph):
  648. aslib.addCallgraphs(sysvals, hf, data)
  649. # add the test log as a hidden div
  650. if sysvals.testlog and sysvals.logmsg:
  651. hf.write('<div id="testlog" style="display:none;">\n'+sysvals.logmsg+'</div>\n')
  652. # add the dmesg log as a hidden div
  653. if sysvals.dmesglog:
  654. hf.write('<div id="dmesglog" style="display:none;">\n')
  655. for line in data.dmesgtext:
  656. line = line.replace('<', '&lt').replace('>', '&gt')
  657. hf.write(line)
  658. hf.write('</div>\n')
  659. # write the footer and close
  660. aslib.addScriptCode(hf, [data])
  661. hf.write('</body>\n</html>\n')
  662. hf.close()
  663. return True
  664. # Function: updateCron
  665. # Description:
  666. # (restore=False) Set the tool to run automatically on reboot
  667. # (restore=True) Restore the original crontab
  668. def updateCron(restore=False):
  669. if not restore:
  670. sysvals.rootUser(True)
  671. crondir = '/var/spool/cron/crontabs/'
  672. if not os.path.exists(crondir):
  673. crondir = '/var/spool/cron/'
  674. if not os.path.exists(crondir):
  675. doError('%s not found' % crondir)
  676. cronfile = crondir+'root'
  677. backfile = crondir+'root-analyze_boot-backup'
  678. cmd = sysvals.getExec('crontab')
  679. if not cmd:
  680. doError('crontab not found')
  681. # on restore: move the backup cron back into place
  682. if restore:
  683. if os.path.exists(backfile):
  684. shutil.move(backfile, cronfile)
  685. call([cmd, cronfile])
  686. return
  687. # backup current cron and install new one with reboot
  688. if os.path.exists(cronfile):
  689. shutil.move(cronfile, backfile)
  690. else:
  691. fp = open(backfile, 'w')
  692. fp.close()
  693. res = -1
  694. try:
  695. fp = open(backfile, 'r')
  696. op = open(cronfile, 'w')
  697. for line in fp:
  698. if not sysvals.myCronJob(line):
  699. op.write(line)
  700. continue
  701. fp.close()
  702. op.write('@reboot python %s\n' % sysvals.cronjobCmdString())
  703. op.close()
  704. res = call([cmd, cronfile])
  705. except Exception, e:
  706. pprint('Exception: %s' % str(e))
  707. shutil.move(backfile, cronfile)
  708. res = -1
  709. if res != 0:
  710. doError('crontab failed')
  711. # Function: updateGrub
  712. # Description:
  713. # update grub.cfg for all kernels with our parameters
  714. def updateGrub(restore=False):
  715. # call update-grub on restore
  716. if restore:
  717. try:
  718. call(sysvals.blexec, stderr=PIPE, stdout=PIPE,
  719. env={'PATH': '.:/sbin:/usr/sbin:/usr/bin:/sbin:/bin'})
  720. except Exception, e:
  721. pprint('Exception: %s\n' % str(e))
  722. return
  723. # extract the option and create a grub config without it
  724. sysvals.rootUser(True)
  725. tgtopt = 'GRUB_CMDLINE_LINUX_DEFAULT'
  726. cmdline = ''
  727. grubfile = '/etc/default/grub'
  728. tempfile = '/etc/default/grub.analyze_boot'
  729. shutil.move(grubfile, tempfile)
  730. res = -1
  731. try:
  732. fp = open(tempfile, 'r')
  733. op = open(grubfile, 'w')
  734. cont = False
  735. for line in fp:
  736. line = line.strip()
  737. if len(line) == 0 or line[0] == '#':
  738. continue
  739. opt = line.split('=')[0].strip()
  740. if opt == tgtopt:
  741. cmdline = line.split('=', 1)[1].strip('\\')
  742. if line[-1] == '\\':
  743. cont = True
  744. elif cont:
  745. cmdline += line.strip('\\')
  746. if line[-1] != '\\':
  747. cont = False
  748. else:
  749. op.write('%s\n' % line)
  750. fp.close()
  751. # if the target option value is in quotes, strip them
  752. sp = '"'
  753. val = cmdline.strip()
  754. if val and (val[0] == '\'' or val[0] == '"'):
  755. sp = val[0]
  756. val = val.strip(sp)
  757. cmdline = val
  758. # append our cmd line options
  759. if len(cmdline) > 0:
  760. cmdline += ' '
  761. cmdline += sysvals.kernelParams()
  762. # write out the updated target option
  763. op.write('\n%s=%s%s%s\n' % (tgtopt, sp, cmdline, sp))
  764. op.close()
  765. res = call(sysvals.blexec)
  766. os.remove(grubfile)
  767. except Exception, e:
  768. pprint('Exception: %s' % str(e))
  769. res = -1
  770. # cleanup
  771. shutil.move(tempfile, grubfile)
  772. if res != 0:
  773. doError('update grub failed')
  774. # Function: updateKernelParams
  775. # Description:
  776. # update boot conf for all kernels with our parameters
  777. def updateKernelParams(restore=False):
  778. # find the boot loader
  779. sysvals.getBootLoader()
  780. if sysvals.bootloader == 'grub':
  781. updateGrub(restore)
  782. # Function: doError Description:
  783. # generic error function for catastrphic failures
  784. # Arguments:
  785. # msg: the error message to print
  786. # help: True if printHelp should be called after, False otherwise
  787. def doError(msg, help=False):
  788. if help == True:
  789. printHelp()
  790. pprint('ERROR: %s\n' % msg)
  791. sysvals.outputResult({'error':msg})
  792. sys.exit()
  793. # Function: printHelp
  794. # Description:
  795. # print out the help text
  796. def printHelp():
  797. pprint('\n%s v%s\n'\
  798. 'Usage: bootgraph <options> <command>\n'\
  799. '\n'\
  800. 'Description:\n'\
  801. ' This tool reads in a dmesg log of linux kernel boot and\n'\
  802. ' creates an html representation of the boot timeline up to\n'\
  803. ' the start of the init process.\n'\
  804. '\n'\
  805. ' If no specific command is given the tool reads the current dmesg\n'\
  806. ' and/or ftrace log and creates a timeline\n'\
  807. '\n'\
  808. ' Generates output files in subdirectory: boot-yymmdd-HHMMSS\n'\
  809. ' HTML output: <hostname>_boot.html\n'\
  810. ' raw dmesg output: <hostname>_boot_dmesg.txt\n'\
  811. ' raw ftrace output: <hostname>_boot_ftrace.txt\n'\
  812. '\n'\
  813. 'Options:\n'\
  814. ' -h Print this help text\n'\
  815. ' -v Print the current tool version\n'\
  816. ' -verbose Print extra information during execution and analysis\n'\
  817. ' -addlogs Add the dmesg log to the html output\n'\
  818. ' -result fn Export a results table to a text file for parsing.\n'\
  819. ' -o name Overrides the output subdirectory name when running a new test\n'\
  820. ' default: boot-{date}-{time}\n'\
  821. ' [advanced]\n'\
  822. ' -fstat Use ftrace to add function detail and statistics (default: disabled)\n'\
  823. ' -f/-callgraph Add callgraph detail, can be very large (default: disabled)\n'\
  824. ' -maxdepth N limit the callgraph data to N call levels (default: 2)\n'\
  825. ' -mincg ms Discard all callgraphs shorter than ms milliseconds (e.g. 0.001 for us)\n'\
  826. ' -timeprec N Number of significant digits in timestamps (0:S, 3:ms, [6:us])\n'\
  827. ' -expandcg pre-expand the callgraph data in the html output (default: disabled)\n'\
  828. ' -func list Limit ftrace to comma-delimited list of functions (default: do_one_initcall)\n'\
  829. ' -cgfilter S Filter the callgraph output in the timeline\n'\
  830. ' -cgskip file Callgraph functions to skip, off to disable (default: cgskip.txt)\n'\
  831. ' -bl name Use the following boot loader for kernel params (default: grub)\n'\
  832. ' -reboot Reboot the machine automatically and generate a new timeline\n'\
  833. ' -manual Show the steps to generate a new timeline manually (used with -reboot)\n'\
  834. '\n'\
  835. 'Other commands:\n'\
  836. ' -flistall Print all functions capable of being captured in ftrace\n'\
  837. ' -sysinfo Print out system info extracted from BIOS\n'\
  838. ' [redo]\n'\
  839. ' -dmesg file Create HTML output using dmesg input (used with -ftrace)\n'\
  840. ' -ftrace file Create HTML output using ftrace input (used with -dmesg)\n'\
  841. '' % (sysvals.title, sysvals.version))
  842. return True
  843. # ----------------- MAIN --------------------
  844. # exec start (skipped if script is loaded as library)
  845. if __name__ == '__main__':
  846. # loop through the command line arguments
  847. cmd = ''
  848. testrun = True
  849. switchoff = ['disable', 'off', 'false', '0']
  850. simplecmds = ['-sysinfo', '-kpupdate', '-flistall', '-checkbl']
  851. cgskip = ''
  852. if '-f' in sys.argv:
  853. cgskip = sysvals.configFile('cgskip.txt')
  854. args = iter(sys.argv[1:])
  855. mdset = False
  856. for arg in args:
  857. if(arg == '-h'):
  858. printHelp()
  859. sys.exit()
  860. elif(arg == '-v'):
  861. pprint("Version %s" % sysvals.version)
  862. sys.exit()
  863. elif(arg == '-verbose'):
  864. sysvals.verbose = True
  865. elif(arg in simplecmds):
  866. cmd = arg[1:]
  867. elif(arg == '-fstat'):
  868. sysvals.useftrace = True
  869. elif(arg == '-callgraph' or arg == '-f'):
  870. sysvals.useftrace = True
  871. sysvals.usecallgraph = True
  872. elif(arg == '-cgdump'):
  873. sysvals.cgdump = True
  874. elif(arg == '-mincg'):
  875. sysvals.mincglen = aslib.getArgFloat('-mincg', args, 0.0, 10000.0)
  876. elif(arg == '-cgfilter'):
  877. try:
  878. val = args.next()
  879. except:
  880. doError('No callgraph functions supplied', True)
  881. sysvals.setCallgraphFilter(val)
  882. elif(arg == '-cgskip'):
  883. try:
  884. val = args.next()
  885. except:
  886. doError('No file supplied', True)
  887. if val.lower() in switchoff:
  888. cgskip = ''
  889. else:
  890. cgskip = sysvals.configFile(val)
  891. if(not cgskip):
  892. doError('%s does not exist' % cgskip)
  893. elif(arg == '-bl'):
  894. try:
  895. val = args.next()
  896. except:
  897. doError('No boot loader name supplied', True)
  898. if val.lower() not in ['grub']:
  899. doError('Unknown boot loader: %s' % val, True)
  900. sysvals.bootloader = val.lower()
  901. elif(arg == '-timeprec'):
  902. sysvals.setPrecision(aslib.getArgInt('-timeprec', args, 0, 6))
  903. elif(arg == '-maxdepth'):
  904. mdset = True
  905. sysvals.max_graph_depth = aslib.getArgInt('-maxdepth', args, 0, 1000)
  906. elif(arg == '-func'):
  907. try:
  908. val = args.next()
  909. except:
  910. doError('No filter functions supplied', True)
  911. sysvals.useftrace = True
  912. sysvals.usecallgraph = True
  913. sysvals.rootCheck(True)
  914. sysvals.setGraphFilter(val)
  915. elif(arg == '-ftrace'):
  916. try:
  917. val = args.next()
  918. except:
  919. doError('No ftrace file supplied', True)
  920. if(os.path.exists(val) == False):
  921. doError('%s does not exist' % val)
  922. testrun = False
  923. sysvals.ftracefile = val
  924. elif(arg == '-addlogs'):
  925. sysvals.dmesglog = True
  926. elif(arg == '-expandcg'):
  927. sysvals.cgexp = True
  928. elif(arg == '-dmesg'):
  929. try:
  930. val = args.next()
  931. except:
  932. doError('No dmesg file supplied', True)
  933. if(os.path.exists(val) == False):
  934. doError('%s does not exist' % val)
  935. testrun = False
  936. sysvals.dmesgfile = val
  937. elif(arg == '-o'):
  938. try:
  939. val = args.next()
  940. except:
  941. doError('No subdirectory name supplied', True)
  942. sysvals.testdir = sysvals.setOutputFolder(val)
  943. elif(arg == '-result'):
  944. try:
  945. val = args.next()
  946. except:
  947. doError('No result file supplied', True)
  948. sysvals.result = val
  949. elif(arg == '-reboot'):
  950. sysvals.reboot = True
  951. elif(arg == '-manual'):
  952. sysvals.reboot = True
  953. sysvals.manual = True
  954. # remaining options are only for cron job use
  955. elif(arg == '-cronjob'):
  956. sysvals.iscronjob = True
  957. else:
  958. doError('Invalid argument: '+arg, True)
  959. # compatibility errors and access checks
  960. if(sysvals.iscronjob and (sysvals.reboot or \
  961. sysvals.dmesgfile or sysvals.ftracefile or cmd)):
  962. doError('-cronjob is meant for batch purposes only')
  963. if(sysvals.reboot and (sysvals.dmesgfile or sysvals.ftracefile)):
  964. doError('-reboot and -dmesg/-ftrace are incompatible')
  965. if cmd or sysvals.reboot or sysvals.iscronjob or testrun:
  966. sysvals.rootCheck(True)
  967. if (testrun and sysvals.useftrace) or cmd == 'flistall':
  968. if not sysvals.verifyFtrace():
  969. doError('Ftrace is not properly enabled')
  970. # run utility commands
  971. sysvals.cpuInfo()
  972. if cmd != '':
  973. if cmd == 'kpupdate':
  974. updateKernelParams()
  975. elif cmd == 'flistall':
  976. for f in sysvals.getBootFtraceFilterFunctions():
  977. print f
  978. elif cmd == 'checkbl':
  979. sysvals.getBootLoader()
  980. pprint('Boot Loader: %s\n%s' % (sysvals.bootloader, sysvals.blexec))
  981. elif(cmd == 'sysinfo'):
  982. sysvals.printSystemInfo(True)
  983. sys.exit()
  984. # reboot: update grub, setup a cronjob, and reboot
  985. if sysvals.reboot:
  986. if (sysvals.useftrace or sysvals.usecallgraph) and \
  987. not sysvals.checkFtraceKernelVersion():
  988. doError('Ftrace functionality requires kernel v4.10 or newer')
  989. if not sysvals.manual:
  990. updateKernelParams()
  991. updateCron()
  992. call('reboot')
  993. else:
  994. sysvals.manualRebootRequired()
  995. sys.exit()
  996. if sysvals.usecallgraph and cgskip:
  997. sysvals.vprint('Using cgskip file: %s' % cgskip)
  998. sysvals.setCallgraphBlacklist(cgskip)
  999. # cronjob: remove the cronjob, grub changes, and disable ftrace
  1000. if sysvals.iscronjob:
  1001. updateCron(True)
  1002. updateKernelParams(True)
  1003. try:
  1004. sysvals.fsetVal('0', 'tracing_on')
  1005. except:
  1006. pass
  1007. # testrun: generate copies of the logs
  1008. if testrun:
  1009. retrieveLogs()
  1010. else:
  1011. sysvals.setOutputFile()
  1012. # process the log data
  1013. if sysvals.dmesgfile:
  1014. if not mdset:
  1015. sysvals.max_graph_depth = 0
  1016. data = parseKernelLog()
  1017. if(not data.valid):
  1018. doError('No initcall data found in %s' % sysvals.dmesgfile)
  1019. if sysvals.useftrace and sysvals.ftracefile:
  1020. parseTraceLog(data)
  1021. if sysvals.cgdump:
  1022. data.debugPrint()
  1023. sys.exit()
  1024. else:
  1025. doError('dmesg file required')
  1026. sysvals.vprint('Creating the html timeline (%s)...' % sysvals.htmlfile)
  1027. sysvals.vprint('Command:\n %s' % sysvals.cmdline)
  1028. sysvals.vprint('Kernel parameters:\n %s' % sysvals.kparams)
  1029. data.printDetails()
  1030. createBootGraph(data)
  1031. # if running as root, change output dir owner to sudo_user
  1032. if testrun and os.path.isdir(sysvals.testdir) and \
  1033. os.getuid() == 0 and 'SUDO_USER' in os.environ:
  1034. cmd = 'chown -R {0}:{0} {1} > /dev/null 2>&1'
  1035. call(cmd.format(os.environ['SUDO_USER'], sysvals.testdir), shell=True)
  1036. sysvals.stamp['boot'] = (data.tUserMode - data.start) * 1000
  1037. sysvals.stamp['lastinit'] = data.end * 1000
  1038. sysvals.outputResult(sysvals.stamp)