Home | History | Annotate | Download | only in scripts
      1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // You should run this with v8, like v8_shell alloc.js datafile.json
      6 
      7 function toHex(num) {
      8   var str = "";
      9   var table = "0123456789abcdef";
     10   while (num != 0) {
     11     str = table.charAt(num & 0xf) + str;
     12     num >>= 4;
     13   }
     14   return str;
     15 }
     16 
     17 function parseEvents(z) {
     18   var crits =  { }
     19   var calls = { }
     20 
     21   for (var i = 0, il = z.length; i < il; ++i) {
     22     var e = z[i];
     23 
     24     if (e['eventtype'] == 'EVENT_TYPE_ENTER_CS' ||
     25         e['eventtype'] == 'EVENT_TYPE_TRYENTER_CS' ||
     26         e['eventtype'] == 'EVENT_TYPE_LEAVE_CS') {
     27       cs = e['critical_section'];
     28       if (!(cs in crits)) {
     29         crits[cs] = [ ];
     30       }
     31       crits[cs].push(e);
     32     }
     33   }
     34 
     35   // Verify that the locks get unlocked, and operations stay on the same thread.
     36   for (var key in crits) {
     37     var cs = key;
     38     var es = crits[key];
     39 
     40     var tid_stack = [ ];
     41     for (var j = 0, jl = es.length; j < jl; ++j) {
     42       var e = es[j];
     43       if (e['eventtype'] == 'EVENT_TYPE_ENTER_CS') {
     44         tid_stack.push(e);
     45       } else if (e['eventtype'] == 'EVENT_TYPE_TRYENTER_CS') {
     46         if (e['retval'] != 0)
     47           tid_stack.push(e);
     48       } else if (e['eventtype'] == 'EVENT_TYPE_LEAVE_CS') {
     49         if (tid_stack.length == 0) {
     50           print('fail ' + e);
     51         }
     52         var tid = tid_stack.pop()
     53         if (tid['thread'] != e['thread']) {
     54           print('fail ' + tid);
     55         }
     56       }
     57     }
     58   }
     59 
     60   // Look for long-held / contended locks.  We can't really know it is
     61   // contended without looking if anyone is waiting on the embedded event...
     62   // Just look for locks are are held a long time?  Not so good...
     63   for (var key in crits) {
     64     var cs = key;
     65     var es = crits[key];
     66 
     67     var tid_stack = [ ];
     68     for (var j = 0, jl = es.length; j < jl; ++j) {
     69       var e = es[j];
     70       if (e['eventtype'] == 'EVENT_TYPE_ENTER_CS') {
     71         tid_stack.push(e);
     72       } else if (e['eventtype'] == 'EVENT_TYPE_TRYENTER_CS') {
     73         if (e['retval'] != 0)
     74           tid_stack.push(e);
     75       } else if (e['eventtype'] == 'EVENT_TYPE_LEAVE_CS') {
     76         if (tid_stack.length == 0) {
     77           print('fail ' + e);
     78         }
     79         var tid = tid_stack.pop();
     80         var dur = e['ms'] - tid['ms'];
     81         if (dur > 0.1) {
     82           print('Lock: 0x' + toHex(cs) + ' for ' + dur + ' at: ' + e['ms']);
     83         }
     84       }
     85     }
     86   }
     87 }
     88