char* mozilla_sampler_get_profile() { TableTicker *t = tlsTicker.get(); if (!t) { return NULL; } std::stringstream profile; t->SetPaused(true); profile << *(t->GetPrimaryThreadProfile()); t->SetPaused(false); std::string profileString = profile.str(); char *rtn = (char*)malloc( (profileString.length() + 1) * sizeof(char) ); strcpy(rtn, profileString.c_str()); return rtn; }
NS_IMETHOD Run() { TableTicker *t = tlsTicker.get(); char buff[MAXPATHLEN]; #ifdef ANDROID #define FOLDER "/sdcard/" #elif defined(XP_WIN) #define FOLDER "%TEMP%\\" #else #define FOLDER "/tmp/" #endif snprintf(buff, MAXPATHLEN, "%sprofile_%i_%i.txt", FOLDER, XRE_GetProcessType(), getpid()); #ifdef XP_WIN // Expand %TEMP% on Windows { char tmp[MAXPATHLEN]; ExpandEnvironmentStringsA(buff, tmp, mozilla::ArrayLength(tmp)); strcpy(buff, tmp); } #endif // Pause the profiler during saving. // This will prevent us from recording sampling // regarding profile saving. This will also // prevent bugs caused by the circular buffer not // being thread safe. Bug 750989. std::ofstream stream; stream.open(buff); t->SetPaused(true); if (stream.is_open()) { stream << *(t->GetPrimaryThreadProfile()); stream << "h-" << GetSharedLibraryInfoString() << std::endl; stream.close(); LOG("Saved to " FOLDER "profile_TYPE_PID.txt"); } else { LOG("Fail to open profile log file."); } t->SetPaused(false); return NS_OK; }
NS_IMETHOD Run() { TableTicker *t = tlsTicker.get(); // Pause the profiler during saving. // This will prevent us from recording sampling // regarding profile saving. This will also // prevent bugs caused by the circular buffer not // being thread safe. Bug 750989. t->SetPaused(true); // Get file path #ifdef ANDROID nsCString tmpPath; tmpPath.AppendPrintf("/sdcard/profile_%i_%i.txt", XRE_GetProcessType(), getpid()); #else nsCOMPtr<nsIFile> tmpFile; nsAutoCString tmpPath; if (NS_FAILED(NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(tmpFile)))) { LOG("Failed to find temporary directory."); return NS_ERROR_FAILURE; } tmpPath.AppendPrintf("profile_%i_%i.txt", XRE_GetProcessType(), getpid()); nsresult rv = tmpFile->AppendNative(tmpPath); if (NS_FAILED(rv)) return rv; rv = tmpFile->GetNativePath(tmpPath); if (NS_FAILED(rv)) return rv; #endif // Create a JSContext to run a JSObjectBuilder :( // Based on XPCShellEnvironment JSRuntime *rt; JSContext *cx; nsCOMPtr<nsIJSRuntimeService> rtsvc = do_GetService("@mozilla.org/js/xpc/RuntimeService;1"); if (!rtsvc || NS_FAILED(rtsvc->GetRuntime(&rt)) || !rt) { LOG("failed to get RuntimeService"); return NS_ERROR_FAILURE;; } cx = JS_NewContext(rt, 8192); if (!cx) { LOG("Failed to get context"); return NS_ERROR_FAILURE; } { JSAutoRequest ar(cx); static JSClass c = { "global", JSCLASS_GLOBAL_FLAGS, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub }; JSObject *obj = JS_NewGlobalObject(cx, &c, NULL); std::ofstream stream; stream.open(tmpPath.get()); // Pause the profiler during saving. // This will prevent us from recording sampling // regarding profile saving. This will also // prevent bugs caused by the circular buffer not // being thread safe. Bug 750989. t->SetPaused(true); if (stream.is_open()) { JSAutoCompartment autoComp(cx, obj); JSObject* profileObj = mozilla_sampler_get_profile_data(cx); jsval val = OBJECT_TO_JSVAL(profileObj); JS_Stringify(cx, &val, nullptr, JSVAL_NULL, WriteCallback, &stream); stream.close(); LOGF("Saved to %s", tmpPath.get()); } else { LOG("Fail to open profile log file."); } } JS_EndRequest(cx); JS_DestroyContext(cx); t->SetPaused(false); return NS_OK; }