Skip to content

Commit

Permalink
Finish implementing shutdown profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
Benoit Girard committed Nov 28, 2012
1 parent 69d42a0 commit e55e4db
Showing 1 changed file with 65 additions and 15 deletions.
80 changes: 65 additions & 15 deletions lib/main.js
Expand Up @@ -67,15 +67,24 @@ let data = require("self").data;
let clipboard = require("clipboard");
let timers = require("timers");
let file = require("file");
let tabs;
let tabs = null;
let panelModule;
let gcli_binding;
let windowUtils;
let pbModule;

let gProfileStartEpoch = null;

// For Thunderbird the tabs module is not supported, and will throw if we try
// to import it
if (!require("api-utils/xul-app").is("Thunderbird")) {
try {
tabs = require("tabs");
panelModule = require("panel");
} catch (e) {}
gcli_binding = require("gcli_binding");
windowUtils = require("window-utils");
pbModule = require("private-browsing");
}

let Request = require("request").Request;
Expand Down Expand Up @@ -604,7 +613,7 @@ function get_profile(progress_callback, finish_callback, args) {
});
}

if (args.customProfile) {
if (args && args.customProfile) {
send_profile(args.customProfile);
return;
}
Expand Down Expand Up @@ -898,6 +907,9 @@ function open_settings() {
if (!tabs) {
tabs = require("tabs");
}
if (!tabs) {
tabs = require("tabs");
}
tabs.open({
url: data.url("settings.html"),
onReady: function onReady(tab) {
Expand Down Expand Up @@ -962,7 +974,7 @@ let ThunderbirdAppWrapper = {
});
});
} catch(e) {
console.error("Profiler module not found: " + e.message);
console.error("Profiler module not found: " + e.message + ", " + e.stack);
}
},
init: function tb_init() {
Expand Down Expand Up @@ -1430,6 +1442,8 @@ let FirefoxAppWrapper = {
let env = Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment);
var profilingStartup = env.get("MOZ_PROFILER_STARTUP") === "true";

// Save the startup profile before handling the previous shutdown
// since we're still profiling here.
if (profilingStartup) {
env.set("MOZ_PROFILER_STARTUP", "");
analyze(function () {
Expand All @@ -1439,12 +1453,19 @@ let FirefoxAppWrapper = {
sps_startup();
}

var profilingShutdown = env.get("MOZ_PROFILER_SHUTDOWN") != null || env.get("MOZ_PROFILER_SHUTDOWN") !== "";
var profilingShutdown = env.get("MOZ_PROFILER_SHUTDOWN") != null && env.get("MOZ_PROFILER_SHUTDOWN") !== "";
if (profilingShutdown) {
var shutdownLog = env.get("MOZ_PROFILER_SHUTDOWN");
var profile = ReadAsString(shutdownLog);
appWrapper.open_cleopatra("desktop", {customProfile:profile});
// TODO Delete the shutdown log?
let shutdownFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
shutdownFile.initWithPath(shutdownLog);
if (shutdownFile.exists()) {
var profile = ReadAsString(shutdownLog);
appWrapper.open_cleopatra("desktop", {customProfile:profile});
shutdownFile.remove(false);
env.set("MOZ_PROFILER_SHUTDOWN", "");
} else {
dump("Failed to process MOZ_PROFILER_SHUTDOWN, shutdown file not found: '" + profilingShutdown + "'\n");
}
}

});
Expand All @@ -1457,7 +1478,13 @@ let FirefoxAppWrapper = {
init: function ff_init() {

let self = this;
panel = require("panel").Panel({
if (!tabs) {
tabs = require("tabs");
}
if (!panelModule) {
panelModule = require("panel");
}
panel = panelModule.Panel({
width:245,
height:230,
contentURL: data.url("sps_panel.html"),
Expand All @@ -1473,7 +1500,7 @@ let FirefoxAppWrapper = {
return prefs.get_pref_bool("profiler.", "enabled");
}
});
privatePanel = require("panel").Panel({
privatePanel = panelModule.Panel({
width:245,
height:230,
contentURL: data.url("sps_private.html"),
Expand Down Expand Up @@ -1505,7 +1532,7 @@ let FirefoxAppWrapper = {

panel.port.on("importpackage", function(text) {
// Example from: http://gitorious.org/addon-sdk/bugzilla-triage-scripts/blobs/master/lib/prompts.js
var window = require("window-utils").activeWindow;
var window = windowUtils.activeWindow;
var fp = Cc["@mozilla.org/filepicker;1"]
.createInstance(Ci.nsIFilePicker);
fp.init(window, "Import Gecko Profiler ZIP Package", Ci.nsIFilePicker.modeOpen);
Expand All @@ -1519,7 +1546,7 @@ let FirefoxAppWrapper = {
}
});
panel.port.on("browselibfolder", function(defaultFolder) {
var window = require("window-utils").activeWindow;
var window = windowUtils.activeWindow;
var fp = Cc["@mozilla.org/filepicker;1"]
.createInstance(Ci.nsIFilePicker);
fp.init(window, "Folder to cache mobile system libraries", Ci.nsIFilePicker.modeGetFolder);
Expand Down Expand Up @@ -1594,9 +1621,6 @@ let FirefoxAppWrapper = {
function error_cb(error) {
panel.port.emit("show_log", error.description);
if (error.url)
if (!tabs) {
tabs = require("tabs");
}
tabs.open({url: error.url});
});
} else if (target === "local") {
Expand Down Expand Up @@ -1625,10 +1649,10 @@ let FirefoxAppWrapper = {

_initLocalProfiler: function ff__initLocalProfiler(cb) {
this.initProfiler(Profiler, {}, function() {
this.pb = require("private-browsing");
// If we stopped the profiler when entering private
// browsing we will restart it when exiting
this.privateBrowsingRestartProfiler = false;
this.pb = pbModule;

this._runningProfileSessions = {};

Expand Down Expand Up @@ -1698,6 +1722,32 @@ let FirefoxAppWrapper = {
profileRestart: function ff_profileRestart() {
let env = Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment);
env.set("MOZ_PROFILER_STARTUP", "true");
var shutdownFile = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties)
.get("TmpD", Ci.nsIFile);
shutdownFile.append("sps_shutdown_" + (new Date().getTime()) + ".dat");
if (!shutdownFile.exists()) {
// TODO check if the string is ascii
var shutdownFilePath = shutdownFile.path;
// Only attempt shutdown profiling is the path is restricted
// to ASCII 32..127 because of environment variable restrictions.
function check_range(str) {
for (var i = 0; i < shutdownFilePath.length; i++) {
if (shutdownFilePath.charCodeAt(i) > 127) {
return false;
}
}
return true;
}

if (check_range(shutdownFilePath)) {
env.set("MOZ_PROFILER_SHUTDOWN", shutdownFilePath);
dump("SHUTDOWNPATH: " + env.get("MOZ_PROFILER_SHUTDOWN") + "\n");
} else {
dump("Can't save shutdown log because shutdown path contains characters outside the restricted 32..127 range.\n");
}
} else {
dump("Can't save shutdown log because shutdown file exists.\n");
}
let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"].getService(Ci.nsIAppStartup);
appStartup.quit(appStartup.eForceQuit | appStartup.eRestart);
},
Expand Down

0 comments on commit e55e4db

Please sign in to comment.