internal static async Task <ProfileResultInfo> StartProfileAsync(int processId, ITracer tracer = null, bool iisProfiling = false) { tracer = tracer ?? NullTracer.Instance; using (tracer.Step("ProfileManager.StartProfileAsync")) { // Check if the profiling is already running for the given process. If it does, then just return with 200. if (_profilingList.ContainsKey(processId)) { return(new ProfileResultInfo(HttpStatusCode.OK, string.Empty)); } int profilingSessionId = GetNextProfilingSessionId(); ProfileResultInfo profileProcessResponse = null; if (iisProfiling) { profileProcessResponse = await StartIisSessionAsync(processId, profilingSessionId, tracer); } else { string arguments; // This is temp fix while we are in process of retiring Windows 2012 server. if (Environment.OSVersion.Version.Major < 10) { arguments = System.Environment.ExpandEnvironmentVariables(string.Format("start {0} /attach:{1} /loadAgent:{2};DiagnosticsHub.CpuAgent.dll /scratchLocation:%LOCAL_EXPANDED%\\Temp", profilingSessionId, processId, DiagnosticsHubAgentGuid)); } else { arguments = System.Environment.ExpandEnvironmentVariables(string.Format("start {0} /attach:{1} /loadAgent:{2};DiagnosticsHub.CpuAgent.dll;{{\\\"collectNgenPdbs\\\":false}} /scratchLocation:%LOCAL_EXPANDED%\\Temp", profilingSessionId, processId, DiagnosticsHubAgentGuid)); } profileProcessResponse = await ExecuteProfilingCommandAsync(arguments, tracer); } if (profileProcessResponse.StatusCode != HttpStatusCode.OK) { return(profileProcessResponse); } // This may fail if we got 2 requests at the same time to start a profiling session // in that case, only 1 will be added and the other one will be stopped. if (!_profilingList.TryAdd(processId, new ProfileInfo(profilingSessionId, iisProfiling))) { tracer.TraceWarning("A profiling session was already running for process {0}, stopping profiling session {1}", processId, profilingSessionId); await StopProfileInternalAsync(processId, profilingSessionId, true, tracer, iisProfiling); return(new ProfileResultInfo(HttpStatusCode.OK, string.Empty)); } tracer.Step("started session id: {0} for pid: {1}", profilingSessionId, processId); EnsureIdleTimer(iisProfiling); return(new ProfileResultInfo(HttpStatusCode.OK, string.Empty)); } }
internal static ProfileResultInfo StartProfile(int processId, bool iisProfiling = true, int[] additionaProcessIds = null) { Logger.LogInfo("ProfileManager.StartProfile"); // Check if the profiling is already running for the given process. If it does, then just return with 200. if (_profilingList.ContainsKey(processId)) { return(new ProfileResultInfo(HttpStatusCode.OK, string.Empty)); } int profilingSessionId = GetNextProfilingSessionId(); ProfileResultInfo profileProcessResponse = null; if (iisProfiling) { profileProcessResponse = StartIisSession(processId, profilingSessionId, additionaProcessIds); } else { string arguments = Environment.ExpandEnvironmentVariables(string.Format("start {0} /attach:{1} /loadAgent:{2};DiagnosticsHub.CpuAgent.dll;{{\\\"collectNgenPdbs\\\":false}} /scratchLocation:%LOCAL_EXPANDED%\\Temp", profilingSessionId, processId, DiagnosticsHubAgentGuid)); profileProcessResponse = ExecuteProfilingCommand(arguments); } if (profileProcessResponse.StatusCode != HttpStatusCode.OK) { return(profileProcessResponse); } // This may fail if we got 2 requests at the same time to start a profiling session // in that case, only 1 will be added and the other one will be stopped. if (!_profilingList.TryAdd(processId, new ProfileInfo(profilingSessionId, true))) { Logger.LogDiagnoserVerboseEvent(string.Format("WARNING:A profiling session was already running for process {0}, stopping profiling session {1}", processId, profilingSessionId)); StopProfileInternal(processId, profilingSessionId); return(new ProfileResultInfo(HttpStatusCode.OK, string.Empty)); } Logger.LogInfo(string.Format("started session id: {0} for pid: {1}", profilingSessionId, processId)); return(new ProfileResultInfo(HttpStatusCode.OK, string.Empty)); }