Home | History | Annotate | Download | only in importer
      1 <!DOCTYPE html>
      2 <html>
      3 <!--
      4 Copyright (c) 2012 The Chromium Authors. All rights reserved.
      5 Use of this source code is governed by a BSD-style license that can be
      6 found in the LICENSE file.
      7 -->
      8 <head>
      9 <title>LinuxPerfImporter tests</title>
     10 <script src="../base.js"></script>
     11 </head>
     12 <body>
     13 <script>
     14 'use strict';
     15 
     16 base.require('unittest');
     17 base.require('test_utils');
     18 base.require('importer.linux_perf_importer');
     19 
     20 function testLineParserWithLegacyFmt() {
     21   var p = tracing.importer._LinuxPerfImporterTestExports.lineParserWithLegacyFmt;
     22   var x = p('   <idle>-0     [001]  4467.843475: sched_switch: ' +
     23       'prev_comm=swapper prev_pid=0 prev_prio=120 prev_state=R ==> ' +
     24       'next_comm=SurfaceFlinger next_pid=178 next_prio=112');
     25   assertNotNull(x);
     26   assertEquals('<idle>', x.threadName);
     27   assertEquals('0', x.pid);
     28   assertEquals('001', x.cpuNumber);
     29   assertEquals('4467.843475', x.timestamp);
     30   assertEquals('sched_switch', x.eventName);
     31   assertEquals('prev_comm=swapper prev_pid=0 prev_prio=120 prev_state=R ==> ' +
     32       'next_comm=SurfaceFlinger next_pid=178 next_prio=112', x.details);
     33 
     34   var x = p('Binder-Thread #-647   [001]   260.464294: sched_switch: ' +
     35       'prev_comm=Binder Thread # prev_pid=647 prev_prio=120 prev_state=D ==> ' +
     36       'next_comm=.android.chrome next_pid=1562 next_prio=120');
     37   assertNotNull(x);
     38   assertEquals('Binder-Thread #', x.threadName);
     39   assertEquals('647', x.pid);
     40 }
     41 
     42 function testLineParserWithIRQInfo() {
     43   var p = tracing.importer._LinuxPerfImporterTestExports.lineParserWithIRQInfo;
     44   var x = p('     systrace.sh-5441  [001] d...  1031.091570: ' +
     45         'sched_wakeup: comm=debugd pid=4978 prio=120 success=1 target_cpu=000');
     46   assertNotNull(x);
     47   assertEquals('systrace.sh', x.threadName);
     48   assertEquals('5441', x.pid);
     49   assertEquals('001', x.cpuNumber);
     50   assertEquals('1031.091570', x.timestamp);
     51   assertEquals('sched_wakeup', x.eventName);
     52   assertEquals('comm=debugd pid=4978 prio=120 success=1 target_cpu=000', x.details);
     53 }
     54 
     55 function testLineParserWithTGID() {
     56   var p = tracing.importer._LinuxPerfImporterTestExports.lineParserWithTGID;
     57   var x = p('     systrace.sh-5441  (54321) [001] d...  1031.091570: ' +
     58         'sched_wakeup: comm=debugd pid=4978 prio=120 success=1 target_cpu=000');
     59   assertNotNull(x);
     60   assertEquals('systrace.sh', x.threadName);
     61   assertEquals('5441', x.pid);
     62   assertEquals('54321', x.tgid);
     63   assertEquals('001', x.cpuNumber);
     64   assertEquals('1031.091570', x.timestamp);
     65   assertEquals('sched_wakeup', x.eventName);
     66   assertEquals('comm=debugd pid=4978 prio=120 success=1 target_cpu=000', x.details);
     67 
     68   var x = p('     systrace.sh-5441  (  321) [001] d...  1031.091570: ' +
     69         'sched_wakeup: comm=debugd pid=4978 prio=120 success=1 target_cpu=000');
     70   assertNotNull(x);
     71   assertEquals('321', x.tgid);
     72 
     73   var x = p('     systrace.sh-5441  (-----) [001] d...  1031.091570: ' +
     74         'sched_wakeup: comm=debugd pid=4978 prio=120 success=1 target_cpu=000');
     75   assertNotNull(x);
     76   assertEquals(undefined, x.tgid);
     77 }
     78 
     79 function testAutodetectLineCornerCases() {
     80   var detectParser =
     81     tracing.importer._LinuxPerfImporterTestExports.autoDetectLineParser;
     82   var lineParserWithLegacyFmt =
     83     tracing.importer._LinuxPerfImporterTestExports.lineParserWithLegacyFmt;
     84   var lineParserWithIRQInfo =
     85     tracing.importer._LinuxPerfImporterTestExports.lineParserWithIRQInfo;
     86   var lineParserWithTGID =
     87     tracing.importer._LinuxPerfImporterTestExports.lineParserWithTGID;
     88 
     89   var lineWithLegacyFmt =
     90     'systrace.sh-8170  [001] 15180.978813: sched_switch: ' +
     91     'prev_comm=systrace.sh prev_pid=8170 prev_prio=120 ' +
     92     'prev_state=x ==> next_comm=kworker/1:0 next_pid=7873 ' +
     93     'next_prio=120';
     94   var detected = detectParser(lineWithLegacyFmt);
     95   assertEquals(detected, lineParserWithLegacyFmt);
     96 
     97   var lineWithIRQInfo =
     98     'systrace.sh-8170  [001] d... 15180.978813: sched_switch: ' +
     99     'prev_comm=systrace.sh prev_pid=8170 prev_prio=120 ' +
    100     'prev_state=x ==> next_comm=kworker/1:0 next_pid=7873 ' +
    101     'next_prio=120';
    102   var detected = detectParser(lineWithIRQInfo);
    103   assertEquals(detected, lineParserWithIRQInfo);
    104 
    105   var lineWithTGID =
    106     'systrace.sh-8170  (54321) [001] d... 15180.978813: sched_switch: ' +
    107     'prev_comm=systrace.sh prev_pid=8170 prev_prio=120 ' +
    108     'prev_state=x ==> next_comm=kworker/1:0 next_pid=7873 ' +
    109     'next_prio=120';
    110   var detected = detectParser(lineWithTGID);
    111   assertEquals(detected, lineParserWithTGID);
    112 }
    113 
    114 function testTraceEventClockSyncRE() {
    115   var re = tracing.importer._LinuxPerfImporterTestExports.traceEventClockSyncRE;
    116   var x = re.exec('trace_event_clock_sync: parent_ts=19581477508');
    117   assertNotNull(x);
    118   assertEquals('19581477508', x[1]);
    119 
    120   var x = re.exec('trace_event_clock_sync: parent_ts=123.456');
    121   assertNotNull(x);
    122   assertEquals('123.456', x[1]);
    123 }
    124 
    125 function testCanImport() {
    126   var lines = [
    127     '# tracer: nop',
    128     '#',
    129     '#           TASK-PID    CPU#    TIMESTAMP  FUNCTION',
    130     '#              | |       |          |         |',
    131     '          <idle>-0     [001]  4467.843475: sched_switch: ' +
    132         'prev_comm=swapper prev_pid=0 prev_prio=120 prev_state=R ==> ' +
    133         'next_comm=SurfaceFlinger next_pid=178 next_prio=112',
    134 
    135     '  SurfaceFlinger-178   [001]  4467.843536: sched_switch: ' +
    136         'prev_comm=SurfaceFlinger prev_pid=178 prev_prio=112 prev_state=S ' +
    137         '==> next_comm=kworker/u:2 next_pid=2844 next_prio=120',
    138 
    139     '     kworker/u:2-2844  [001]  4467.843567: sched_switch: ' +
    140         'prev_comm=kworker/u:2 prev_pid=2844 prev_prio=120 prev_state=S ' +
    141         '==> next_comm=swapper next_pid=0 next_prio=120',
    142 
    143     '          <idle>-0     [001]  4467.844208: sched_switch: ' +
    144           'prev_comm=swapper prev_pid=0 prev_prio=120 prev_state=R ==> ' +
    145           'next_comm=kworker/u:2 next_pid=2844 next_prio=120'
    146   ];
    147   assertTrue(tracing.importer.LinuxPerfImporter.canImport(lines.join('\n')));
    148 
    149   var lines = [
    150     '          <idle>-0     [001]  4467.843475: sched_switch: ' +
    151             'prev_comm=swapper prev_pid=0 prev_prio=120 prev_state=R ==> ' +
    152             'next_comm=SurfaceFlinger next_pid=178 next_prio=112'
    153   ];
    154   assertTrue(tracing.importer.LinuxPerfImporter.canImport(lines.join('\n')));
    155 
    156   var lines = [
    157     '          <idle>-0     [001]  4467.843475: sched_switch: ' +
    158               'prev_comm=swapper prev_pid=0 prev_prio=120 prev_state=R ==> ' +
    159               'next_comm=SurfaceFlinger next_pid=178 next_prio=112',
    160 
    161     '  SurfaceFlinger-178   [001]  4467.843536: sched_switch: ' +
    162               'prev_comm=SurfaceFlinger prev_pid=178 prev_prio=112 ' +
    163               'prev_state=S ==> next_comm=kworker/u:2 next_pid=2844 ' +
    164               'next_prio=120'
    165   ];
    166   assertTrue(tracing.importer.LinuxPerfImporter.canImport(lines.join('\n')));
    167 
    168   var lines = [
    169     'SomeRandomText',
    170     'More random text'
    171   ];
    172   assertFalse(tracing.importer.LinuxPerfImporter.canImport(lines.join('\n')));
    173 }
    174 
    175 function testCanImport34AndLater() {
    176   var lines = [
    177     '# tracer: nop',
    178     '#',
    179     '# entries-in-buffer/entries-written: 55191/55191   #P:2',
    180     '#',
    181     '#                              _-----=> irqs-off',
    182     '#                             / _----=> need-resched',
    183     '#                            | / _---=> hardirq/softirq',
    184     '#                            || / _--=> preempt-depth',
    185     '#                            ||| /     delay',
    186     '#           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION',
    187     '#              | |       |   ||||       |         |',
    188     '     systrace.sh-5441  [001] d...  1031.091570: sched_wakeup: ' +
    189         'comm=debugd pid=4978 prio=120 success=1 target_cpu=000',
    190     '     systrace.sh-5441  [001] d...  1031.091584: sched_switch: ' +
    191         'prev_comm=systrace.sh prev_pid=5441 prev_prio=120 prev_state=x ' +
    192         '==> next_comm=chrome next_pid=5418 next_prio=120'
    193   ];
    194   assertTrue(tracing.importer.LinuxPerfImporter.canImport(lines.join('\n')));
    195 
    196   var lines = [
    197     '     systrace.sh-5441  [001] d...  1031.091570: sched_wakeup: ' +
    198         'comm=debugd pid=4978 prio=120 success=1 target_cpu=000',
    199     '     systrace.sh-5441  [001] d...  1031.091584: sched_switch: ' +
    200         'prev_comm=systrace.sh prev_pid=5441 prev_prio=120 prev_state=x ' +
    201         '==> next_comm=chrome next_pid=5418 next_prio=120'
    202   ];
    203   assertTrue(tracing.importer.LinuxPerfImporter.canImport(lines.join('\n')));
    204 }
    205 
    206 function testCanImportSystraceFile() {
    207   var html_lines = [
    208     '<!DOCTYPE HTML>',
    209     '<html>',
    210     '<head i18n-values="dir:textdirection;">',
    211     '<title>Android System Trace</title>',
    212     '<style type="text/css">tabbox{-webkit-box-orient:vertical;display:-webkit-box;}tabs{-webkit-padding-start</style>',
    213     '<script language="javascript">function onLoad(){reload()}function reload(){if(linuxPerfData){var g=new tracing.Model;g.importEvents("[]",!0,[linuxPerfData]);var e=document.querySelector(".view");cr.ui.decorate(e,tracing.View);e.model=g;e.tabIndex=1;e.timeline.focusElement=e}}document.addEventListener("DOMContentLoaded",onLoad);var global=this;',
    214     'this.cr=function(){function g(a,b,c,f){var e=new cr.Event(b+"Change");e.propertyName=b;e.newValue=c;e.oldValue=f;a.dispatchEvent(e)}function e(a){return a.replace(/([A-Z])/g,"-$1").toLowerCase()}function c(b,c){switch(c){case a.JS:var f=b+"_";return function(){return this[f]};case a.ATTR:var h=e(b);return function(){return this.getAttribute(h)};case a.BOOL_ATTR:return h=e(b),function(){return this.hasAttribute(h)}}}function f(b,c,f){switch(c){case a.JS:var h=b+"_";return function(a){var c=this[h];',
    215     '  </div>',
    216     '  <script>',
    217     '  var linuxPerfData = "\\',
    218     '# tracer: nop\\n\\',
    219     '#\\n\\',
    220     '#           TASK-PID    CPU#    TIMESTAMP  FUNCTION\\n\\',
    221     '#              | |       |          |         |\\n\\',
    222     '          atrace-14662 [000] 50260.647576: sched_switch: prev_comm=atrace prev_pid=14662 prev_prio=120 prev_state=S ==> next_comm=kworker/0:0 next_pid=13696 next_prio=120\\n\\',
    223     '     kworker/0:0-13696 [000] 50260.647590: sched_wakeup: comm=mmcqd/0 pid=95 prio=120 success=1 target_cpu=000\\n\\',
    224     '     kworker/0:0-13696 [000] 50260.647602: sched_wakeup: comm=adbd pid=14582 prio=120 success=1 target_cpu=000\\n\\',
    225     '     kworker/0:0-13696 [000] 50260.647610: sched_switch: prev_comm=kworker/0:0 prev_pid=13696 prev_prio=120 prev_state=S ==> next_comm=adbd next_pid=14582 next_prio=120\\n\\',
    226     '            adbd-14582 [000] 50260.647722: sched_wakeup: comm=adbd pid=14584 prio=120 success=1 target_cpu=000\\n\\',
    227     '            adbd-14582 [000] 50260.647756: sched_switch: prev_comm=adbd prev_pid=14582 prev_prio=120 prev_state=S ==> next_comm=adbd next_pid=14584 next_prio=120\\n\\',
    228     '            adbd-14584 [000] 50260.647833: sched_switch: prev_comm=adbd prev_pid=14584 prev_prio=120 prev_state=S ==> next_comm=mmcqd/0 next_pid=95 next_prio=120\\n\\',
    229     '         mmcqd/0-95    [000] 50260.647846: sched_switch: prev_comm=mmcqd/0 prev_pid=95 prev_prio=120 prev_state=S ==> next_comm=WebViewCoreThre next_pid=11043 next_prio=120\\n\\',
    230     ' WebViewCoreThre-11043 [000] 50260.648275: sched_switch: prev_comm=WebViewCoreThre prev_pid=11043 prev_prio=120 prev_state=S ==> next_comm=swapper next_pid=0 next_prio=120\\n";',
    231     '  <\/script>',
    232     '</body>',
    233     '</html>'
    234   ];
    235   var html_text = html_lines.join('\n');
    236   assertTrue(tracing.importer.LinuxPerfImporter.canImport(html_text));
    237 
    238   var expected_event_lines = [
    239     '# tracer: nop',
    240     '#',
    241     '#           TASK-PID    CPU#    TIMESTAMP  FUNCTION',
    242     '#              | |       |          |         |',
    243     '          atrace-14662 [000] 50260.647576: sched_switch: prev_comm=atrace prev_pid=14662 prev_prio=120 prev_state=S ==> next_comm=kworker/0:0 next_pid=13696 next_prio=120',
    244     '     kworker/0:0-13696 [000] 50260.647590: sched_wakeup: comm=mmcqd/0 pid=95 prio=120 success=1 target_cpu=000',
    245     '     kworker/0:0-13696 [000] 50260.647602: sched_wakeup: comm=adbd pid=14582 prio=120 success=1 target_cpu=000',
    246     '     kworker/0:0-13696 [000] 50260.647610: sched_switch: prev_comm=kworker/0:0 prev_pid=13696 prev_prio=120 prev_state=S ==> next_comm=adbd next_pid=14582 next_prio=120',
    247     '            adbd-14582 [000] 50260.647722: sched_wakeup: comm=adbd pid=14584 prio=120 success=1 target_cpu=000',
    248     '            adbd-14582 [000] 50260.647756: sched_switch: prev_comm=adbd prev_pid=14582 prev_prio=120 prev_state=S ==> next_comm=adbd next_pid=14584 next_prio=120',
    249     '            adbd-14584 [000] 50260.647833: sched_switch: prev_comm=adbd prev_pid=14584 prev_prio=120 prev_state=S ==> next_comm=mmcqd/0 next_pid=95 next_prio=120',
    250     '         mmcqd/0-95    [000] 50260.647846: sched_switch: prev_comm=mmcqd/0 prev_pid=95 prev_prio=120 prev_state=S ==> next_comm=WebViewCoreThre next_pid=11043 next_prio=120',
    251     ' WebViewCoreThre-11043 [000] 50260.648275: sched_switch: prev_comm=WebViewCoreThre prev_pid=11043 prev_prio=120 prev_state=S ==> next_comm=swapper next_pid=0 next_prio=120'
    252   ];
    253   var expected_event_text = expected_event_lines.join('\n');
    254   var res =
    255       tracing.importer.LinuxPerfImporter._extractEventsFromSystraceHTML(
    256           html_text, true);
    257   var actual_event_text = res.lines.join('\n');
    258   assertEquals(actual_event_text, expected_event_text);
    259 }
    260 
    261 function testImportOneSequence() {
    262   var lines = [
    263     '          <idle>-0     [001]  4467.843475: sched_switch: ' +
    264                 'prev_comm=swapper prev_pid=0 prev_prio=120 prev_state=R ==> ' +
    265                 'next_comm=SurfaceFlinger next_pid=178 next_prio=112',
    266 
    267                 '  SurfaceFlinger-178   [001]  4467.843536: sched_switch: ' +
    268                 'prev_comm=SurfaceFlinger prev_pid=178 prev_prio=112 ' +
    269                 'prev_state=S ==> next_comm=kworker/u:2 next_pid=2844 ' +
    270                 'next_prio=120',
    271 
    272                 '     kworker/u:2-2844  [001]  4467.843567: sched_switch: ' +
    273                 'prev_comm=kworker/u:2 prev_pid=2844 prev_prio=120 ' +
    274                 'prev_state=S ==> next_comm=swapper next_pid=0 next_prio=120'
    275   ];
    276   var m = new tracing.Model(lines.join('\n'), false);
    277   assertEquals(0, m.importErrors.length);
    278 
    279   var c = m.cpus[1];
    280   assertEquals(2, c.slices.length);
    281 
    282   assertEquals('SurfaceFlinger', c.slices[0].title);
    283   assertEquals(4467843.475, c.slices[0].start);
    284   assertAlmostEquals(.536 - .475, c.slices[0].duration);
    285 }
    286 
    287 function testImportOneSequenceWithSpacyThreadName() {
    288   var lines = [
    289     '          <idle>-0     [001]  4467.843475: sched_switch: ' +
    290                 'prev_comm=swapper prev_pid=0 prev_prio=120 prev_state=R ==> ' +
    291                 'next_comm=Surface Flinger  next_pid=178 next_prio=112',
    292 
    293                 'Surface Flinger -178   [001]  4467.843536: sched_switch: ' +
    294                 'prev_comm=Surface Flinger  prev_pid=178 prev_prio=112 ' +
    295                 'prev_state=S ==> next_comm=kworker/u:2 next_pid=2844 ' +
    296                 'next_prio=120',
    297 
    298                 '     kworker/u:2-2844  [001]  4467.843567: sched_switch: ' +
    299                 'prev_comm=kworker/u:2 prev_pid=2844 prev_prio=120 ' +
    300                 'prev_state=S ==> next_comm=swapper next_pid=0 next_prio=120'
    301   ];
    302   var m = new tracing.Model(lines.join('\n'), false);
    303   assertEquals(0, m.importErrors.length);
    304 
    305   var c = m.cpus[1];
    306   assertEquals(2, c.slices.length);
    307 
    308   assertEquals('Surface Flinger ', c.slices[0].title);
    309   assertEquals(4467843.475, c.slices[0].start);
    310   assertAlmostEquals(.536 - .475, c.slices[0].duration);
    311 }
    312 
    313 function testImportWithNewline() {
    314   var lines = [
    315     ''
    316   ];
    317   var m = new tracing.Model(lines.join('\n'));
    318   assertEquals(0, m.importErrors.length);
    319 }
    320 
    321 function testClockSync() {
    322   var lines = [
    323     '          <idle>-0     [001]  4467.843475: sched_switch: ' +
    324                   'prev_comm=swapper prev_pid=0 prev_prio=120 prev_state=R ' +
    325                   '==> next_comm=SurfaceFlinger next_pid=178 next_prio=112',
    326     '  SurfaceFlinger-178   [001]  4467.843536: sched_switch: ' +
    327                   'prev_comm=SurfaceFlinger prev_pid=178 prev_prio=112 ' +
    328                   'prev_state=S ==> next_comm=kworker/u:2 next_pid=2844 ' +
    329                   'next_prio=120',
    330     '     kworker/u:2-2844  [001]  4467.843567: sched_switch: ' +
    331                   'prev_comm=kworker/u:2 prev_pid=2844 prev_prio=120 ' +
    332                   'prev_state=S ==> next_comm=swapper next_pid=0 ' +
    333                   'next_prio=120',
    334     '     kworker/u:2-2844  [001]  4467.843000: 0: ' +
    335                   'trace_event_clock_sync: parent_ts=0.1'
    336   ];
    337   var m = new tracing.Model(lines.join('\n'), false);
    338   assertEquals(0, m.importErrors.length);
    339 
    340   var c = m.cpus[1];
    341   assertEquals(2, c.slices.length);
    342 
    343   assertAlmostEquals((467.843475 - (467.843 - 0.1)) * 1000, c.slices[0].start);
    344 }
    345 
    346 function testClockSyncMarkWrite() {
    347   var lines = [
    348     'systrace.sh-8170  [001] 15180.978813: sched_switch: ' +
    349               'prev_comm=systrace.sh prev_pid=8170 prev_prio=120 ' +
    350               'prev_state=x ==> next_comm=kworker/1:0 next_pid=7873 ' +
    351               'next_prio=120',
    352     ' kworker/1:0-7873  [001] 15180.978836: sched_switch: ' +
    353               'prev_comm=kworker/1:0 prev_pid=7873 prev_prio=120 ' +
    354               'prev_state=S ==> next_comm=debugd next_pid=4404 next_prio=120',
    355     '     debugd-4404  [001] 15180.979010: sched_switch: prev_comm=debugd ' +
    356               'prev_pid=4404 prev_prio=120 prev_state=S ==> ' +
    357               'next_comm=dbus-daemon next_pid=510 next_prio=120',
    358     'systrace.sh-8182  [000] 15186.203900: tracing_mark_write: ' +
    359               'trace_event_clock_sync: parent_ts=0'
    360   ];
    361   var m = new tracing.Model(lines.join('\n'), false);
    362   assertEquals(0, m.importErrors.length);
    363 
    364   var c = m.cpus[1];
    365   assertEquals(2, c.slices.length);
    366 
    367   assertAlmostEquals((15180.978813 - 0) * 1000, c.slices[0].start);
    368 }
    369 
    370 
    371 function testImportWithoutClockSyncDeletesEverything() {
    372 }
    373 
    374 </script>
    375 </body>
    376 </html>
    377