test_js_memory_telemetry.js (2140B)
1 "use strict"; 2 3 add_task(function test_compartment_realm_counts() { 4 const compsSystem = "MEMORY_JS_COMPARTMENTS_SYSTEM"; 5 const compsUser = "MEMORY_JS_COMPARTMENTS_USER"; 6 const realmsSystem = "MEMORY_JS_REALMS_SYSTEM"; 7 const realmsUser = "MEMORY_JS_REALMS_USER"; 8 9 Cu.forceShrinkingGC(); 10 11 Services.telemetry.gatherMemory(); 12 let snapshot1 = Services.telemetry.getSnapshotForHistograms("main", true).parent; 13 14 // We can't hard code exact counts, but we can check some basic invariants: 15 // 16 // * Compartments must contain at least one realm, so there must be more 17 // realms than compartments. 18 // * There must be at least one system realm. 19 20 Assert.ok(snapshot1[realmsSystem].sum <= snapshot1[compsSystem].sum, 21 "Number of system compartments can't exceed number of system realms"); 22 Assert.ok(snapshot1[realmsUser].sum <= snapshot1[compsUser].sum, 23 "Number of user compartments can't exceed number of user realms"); 24 Assert.ok(snapshot1[realmsSystem].sum > 0, 25 "There must be at least one system realm"); 26 27 // Now we create a bunch of sandboxes (more than one to be more resilient 28 // against GCs happening in the meantime), so we can check: 29 // 30 // * There are now more realms and user compartments than before. Not system 31 // compartments, because system realms share a compartment. 32 // * The system compartment contains multiple realms. 33 34 let systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal(); 35 let arr = []; 36 for (let i = 0; i < 5; i++) { 37 arr.push(Cu.Sandbox(null)); 38 arr.push(Cu.Sandbox(systemPrincipal)); 39 } 40 41 Services.telemetry.gatherMemory(); 42 let snapshot2 = Services.telemetry.getSnapshotForHistograms("main", true).parent; 43 44 for (let k of [realmsSystem, realmsUser, compsUser]) { 45 Assert.ok(snapshot2[k].sum > snapshot1[k].sum, 46 "There must be more compartments/realms now: " + k); 47 } 48 49 Assert.ok(snapshot2[realmsSystem].sum > snapshot2[compsSystem].sum, 50 "There must be more system realms than system compartments now"); 51 52 arr[0].x = 10; // Ensure the JS engine keeps |arr| alive until this point. 53 });