From e55e4db9c32f01b384927814ca9878190494bc8b Mon Sep 17 00:00:00 2001 From: Benoit Girard Date: Tue, 27 Nov 2012 23:10:09 -0500 Subject: [PATCH] Finish implementing shutdown profiling --- lib/main.js | 80 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 15 deletions(-) diff --git a/lib/main.js b/lib/main.js index 83824f1..61f9b0a 100644 --- a/lib/main.js +++ b/lib/main.js @@ -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; @@ -604,7 +613,7 @@ function get_profile(progress_callback, finish_callback, args) { }); } - if (args.customProfile) { + if (args && args.customProfile) { send_profile(args.customProfile); return; } @@ -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) { @@ -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() { @@ -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 () { @@ -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"); + } } }); @@ -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"), @@ -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"), @@ -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); @@ -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); @@ -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") { @@ -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 = {}; @@ -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); },