tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_HeapSnapshot_takeCensus_09.js (2972B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 "use strict";
      4 
      5 // HeapSnapshot.prototype.takeCensus: by: allocationStack breakdown
      6 //
      7 // Ported from js/src/jit-test/tests/debug/Memory-takeCensus-09.js
      8 
      9 function run_test() {
     10  const g = newGlobal();
     11  const dbg = new Debugger(g);
     12 
     13  g.eval(`                                              // 1
     14         var log = [];                                  // 2
     15         function f() { log.push(allocationMarker()); } // 3
     16         function g() { f(); }                          // 4
     17         function h() { f(); }                          // 5
     18         `);
     19 
     20  // Create one allocationMarker with tracking turned off,
     21  // so it will have no associated stack.
     22  g.f();
     23 
     24  dbg.memory.allocationSamplingProbability = 1;
     25 
     26  for (const [func, n] of [
     27    [g.f, 20],
     28    [g.g, 10],
     29    [g.h, 5],
     30  ]) {
     31    for (let i = 0; i < n; i++) {
     32      dbg.memory.trackingAllocationSites = true;
     33      // All allocations of allocationMarker occur with this line as the oldest
     34      // stack frame.
     35      func();
     36      dbg.memory.trackingAllocationSites = false;
     37    }
     38  }
     39 
     40  const census = saveHeapSnapshotAndTakeCensus(dbg, {
     41    breakdown: {
     42      by: "objectClass",
     43      then: {
     44        by: "allocationStack",
     45        then: { by: "count", label: "haz stack" },
     46        noStack: {
     47          by: "count",
     48          label: "no haz stack",
     49        },
     50      },
     51    },
     52  });
     53 
     54  const map = census.AllocationMarker;
     55  ok(map instanceof Map, "Should be a Map instance");
     56  equal(
     57    map.size,
     58    4,
     59    "Should have 4 allocation stacks (including the lack of a stack)"
     60  );
     61 
     62  // Gather the stacks we are expecting to appear as keys, and
     63  // check that there are no unexpected keys.
     64  const stacks = {};
     65 
     66  map.forEach((v, k) => {
     67    if (k === "noStack") {
     68      // No need to save this key.
     69    } else if (
     70      k.functionDisplayName === "f" &&
     71      k.parent.functionDisplayName === "run_test"
     72    ) {
     73      stacks.f = k;
     74    } else if (
     75      k.functionDisplayName === "f" &&
     76      k.parent.functionDisplayName === "g" &&
     77      k.parent.parent.functionDisplayName === "run_test"
     78    ) {
     79      stacks.fg = k;
     80    } else if (
     81      k.functionDisplayName === "f" &&
     82      k.parent.functionDisplayName === "h" &&
     83      k.parent.parent.functionDisplayName === "run_test"
     84    ) {
     85      stacks.fh = k;
     86    } else {
     87      dumpn("Unexpected allocation stack:");
     88      k.toString()
     89        .split(/\n/g)
     90        .forEach(s => dumpn(s));
     91      ok(false);
     92    }
     93  });
     94 
     95  equal(map.get("noStack").label, "no haz stack");
     96  equal(map.get("noStack").count, 1);
     97 
     98  ok(stacks.f);
     99  equal(map.get(stacks.f).label, "haz stack");
    100  equal(map.get(stacks.f).count, 20);
    101 
    102  ok(stacks.fg);
    103  equal(map.get(stacks.fg).label, "haz stack");
    104  equal(map.get(stacks.fg).count, 10);
    105 
    106  ok(stacks.fh);
    107  equal(map.get(stacks.fh).label, "haz stack");
    108  equal(map.get(stacks.fh).count, 5);
    109 
    110  do_test_finished();
    111 }