private void SetProfilerAttributesOnDictionary(string profilerKey, string profilerNamespace, StringDictionary dictionary) { dictionary[@"OpenCover_Profiler_Key"] = profilerKey; dictionary[@"OpenCover_Profiler_Namespace"] = profilerNamespace; dictionary[@"OpenCover_Profiler_Threshold"] = _commandLine.Threshold.ToString(CultureInfo.InvariantCulture); if (_commandLine.TraceByTest) { dictionary[@"OpenCover_Profiler_TraceByTest"] = "1"; } dictionary["Cor_Profiler"] = ProfilerGuid; dictionary["Cor_Enable_Profiling"] = "1"; dictionary["CoreClr_Profiler"] = ProfilerGuid; dictionary["CoreClr_Enable_Profiling"] = "1"; switch (_commandLine.Registration) { case Registration.Path32: string profilerPath32 = ProfilerRegistration.GetProfilerPath(false); dictionary["Cor_Profiler_Path"] = profilerPath32; dictionary["CorClr_Profiler_Path"] = profilerPath32; break; case Registration.Path64: string profilerPath64 = ProfilerRegistration.GetProfilerPath(true); dictionary["Cor_Profiler_Path"] = profilerPath64; dictionary["CorClr_Profiler_Path"] = profilerPath64; break; } }
private static int RunWithContainer(CommandLineParser parser, Bootstrapper container, IPersistance persistance) { var returnCode = 0; var registered = false; try { if (parser.Register) { ProfilerRegistration.Register(parser.Registration); registered = true; } var harness = container.Resolve <IProfilerManager>(); var servicePrincipal = (parser.Service ? new[] { ServiceEnvironmentManagement.MachineQualifiedServiceAccountName(parser.Target) } : new string[0]).Where(x => !string.IsNullOrWhiteSpace(x)).ToArray(); harness.RunProcess(environment => { if (parser.Service) { RunService(parser, environment, Logger); returnCode = 0; } else { returnCode = RunProcess(parser, environment); } }, servicePrincipal); DisplayResults(persistance, parser, Logger); } catch (Exception ex) { Trace.WriteLine(string.Format("Exception: {0}\n{1}", ex.Message, ex.InnerException)); throw; } finally { if (parser.Register && registered) { ProfilerRegistration.Unregister(parser.Registration); } } return(returnCode); }
private void SetProfilerAttributesOnDictionary(string profilerKey, string profilerNamespace, StringDictionary dictionary) { dictionary[@"OpenCover_Profiler_Key"] = profilerKey; dictionary[@"OpenCover_Profiler_Namespace"] = profilerNamespace; dictionary[@"OpenCover_Profiler_Threshold"] = _commandLine.Threshold.ToString(CultureInfo.InvariantCulture); if (_commandLine.TraceByTest) { dictionary[@"OpenCover_Profiler_TraceByTest"] = "1"; } if (_commandLine.SafeMode) { dictionary[@"OpenCover_Profiler_SafeMode"] = "1"; } dictionary["Cor_Profiler"] = ProfilerGuid; dictionary["Cor_Enable_Profiling"] = "1"; dictionary["CoreClr_Profiler"] = ProfilerGuid; dictionary["CoreClr_Enable_Profiling"] = "1"; dictionary["Cor_Profiler_Path"] = string.Empty; dictionary["CorClr_Profiler_Path"] = string.Empty; if (_commandLine.CommunicationTimeout > 0) { dictionary["OpenCover_Profiler_ShortWait"] = _commandLine.CommunicationTimeout.ToString(); } var profilerPath = ProfilerRegistration.GetProfilerPath(_commandLine.Registration); if (profilerPath != null) { dictionary["Cor_Profiler_Path"] = profilerPath; dictionary["CorClr_Profiler_Path"] = profilerPath; } // new versions of clr and coreclr don't need us to "guess" the profiler bitness : we might as well include these dictionary["Cor_Profiler_Path_32"] = ProfilerRegistration.GetProfilerPath(Registration.Path32); dictionary["Cor_Profiler_Path_64"] = ProfilerRegistration.GetProfilerPath(Registration.Path64); dictionary["CoreClr_Profiler_Path_32"] = ProfilerRegistration.GetProfilerPath(Registration.Path32); dictionary["CoreClr_Profiler_Path_64"] = ProfilerRegistration.GetProfilerPath(Registration.Path64); dictionary["OpenCover_SendVisitPointsTimerInterval"] = _commandLine.SendVisitPointsTimerInterval.ToString(); }
private static int RunWithContainer(CommandLineParser parser, Bootstrapper container, IPersistance persistance) { var returnCode = 0; var registered = false; try { if (parser.Register) { Logger.Debug("Registering profiler..."); ProfilerRegistration.Register(parser.Registration); registered = true; } var harness = container.Resolve <IProfilerManager>(); var servicePrincipalList = new List <string>(); if (parser.Iis) { Logger.Debug($"Profiler configuration will use App Pool identity '{parser.IisAppPoolIdentity}'."); servicePrincipalList.Add(parser.IisAppPoolIdentity); } harness.RunProcess(environment => { returnCode = parser.Iis ? RunIisWebApplication(parser, environment) : RunProcess(parser, environment); }, servicePrincipalList.ToArray()); CalculateAndDisplayResults(persistance.CoverageSession, parser); } finally { if (registered) { Logger.Debug("Unregistering profiler..."); ProfilerRegistration.Unregister(parser.Registration); } } return(returnCode); }
private void SetProfilerAttributesOnDictionary(string profilerKey, string profilerNamespace, StringDictionary dictionary) { dictionary[@"OpenCover_Profiler_Key"] = profilerKey; dictionary[@"OpenCover_Profiler_Namespace"] = profilerNamespace; dictionary[@"OpenCover_Profiler_Threshold"] = _commandLine.Threshold.ToString(CultureInfo.InvariantCulture); if (_commandLine.TraceByTest) { dictionary[@"OpenCover_Profiler_TraceByTest"] = "1"; } if (_commandLine.SafeMode) { dictionary[@"OpenCover_Profiler_SafeMode"] = "1"; } dictionary["Cor_Profiler"] = ProfilerGuid; dictionary["Cor_Enable_Profiling"] = "1"; dictionary["CoreClr_Profiler"] = ProfilerGuid; dictionary["CoreClr_Enable_Profiling"] = "1"; dictionary["Cor_Profiler_Path"] = string.Empty; dictionary["CorClr_Profiler_Path"] = string.Empty; if (_commandLine.CommunicationTimeout > 0) { dictionary["OpenCover_Profiler_ShortWait"] = _commandLine.CommunicationTimeout.ToString(); } var profilerPath = ProfilerRegistration.GetProfilerPath(_commandLine.Registration); if (profilerPath != null) { dictionary["Cor_Profiler_Path"] = profilerPath; dictionary["CorClr_Profiler_Path"] = profilerPath; } dictionary["OpenCover_SendVisitPointsTimerInterval"] = _commandLine.SendVisitPointsTimerInterval.ToString(); }
public void RunProcess(Action <Action <StringDictionary> > process, bool isService) { var key = Guid.NewGuid().GetHashCode().ToString("X"); var processMgmt = new AutoResetEvent(false); var queueMgmt = new AutoResetEvent(false); var environmentKeyRead = new AutoResetEvent(false); var handles = new List <WaitHandle> { processMgmt }; string @namespace = isService ? "Global" : "Local"; _memoryManager.Initialise(@namespace, key); _messageQueue = new ConcurrentQueue <byte[]>(); using (_mcb = new MemoryManager.ManagedCommunicationBlock(@namespace, key, maxMsgSize, -1)) { handles.Add(_mcb.ProfilerRequestsInformation); ThreadPool.QueueUserWorkItem((state) => { try { process(dictionary => { if (dictionary == null) { return; } dictionary[@"OpenCover_Profiler_Key"] = key; dictionary[@"OpenCover_Profiler_Namespace"] = @namespace; dictionary[@"OpenCover_Profiler_Threshold"] = _commandLine.Threshold.ToString(CultureInfo.InvariantCulture); if (_commandLine.TraceByTest) { dictionary[@"OpenCover_Profiler_TraceByTest"] = "1"; } dictionary["Cor_Profiler"] = "{1542C21D-80C3-45E6-A56C-A9C1E4BEB7B8}"; dictionary["Cor_Enable_Profiling"] = "1"; dictionary["CoreClr_Profiler"] = "{1542C21D-80C3-45E6-A56C-A9C1E4BEB7B8}"; dictionary["CoreClr_Enable_Profiling"] = "1"; if (_commandLine.Registration == Registration.Path32) { dictionary["Cor_Profiler_Path"] = ProfilerRegistration.GetProfilerPath(false); } if (_commandLine.Registration == Registration.Path64) { dictionary["Cor_Profiler_Path"] = ProfilerRegistration.GetProfilerPath(true); } environmentKeyRead.Set(); }); } finally { processMgmt.Set(); } }); ThreadPool.QueueUserWorkItem((state) => { while (true) { //// use this block to introduce a delay in to the queue processing //if (_messageQueue.Count < 100) // Thread.Sleep(10); byte[] data; while (!_messageQueue.TryDequeue(out data)) { Thread.Yield(); } _perfCounters.CurrentMemoryQueueSize = _messageQueue.Count; _perfCounters.IncrementBlocksReceived(); if (data.Length == 0) { _communicationManager.Complete(); queueMgmt.Set(); return; } _persistance.SaveVisitData(data); } }); // wait for the environment key to be read if (WaitHandle.WaitAny(new WaitHandle[] { environmentKeyRead }, new TimeSpan(0, 0, 0, 10)) != -1) { ProcessMessages(handles); queueMgmt.WaitOne(); } } }
/// <summary> /// This is the initial console harness - it may become the full thing /// </summary> /// <param name="args"></param> /// <returns></returns> static int Main(string[] args) { var returnCode = 0; var returnCodeOffset = 0; var logger = LogManager.GetLogger(typeof(Bootstrapper)); try { CommandLineParser parser; if (!ParseCommandLine(args, out parser)) { return(parser.ReturnCodeOffset + 1); } LogManager.GetRepository().Threshold = parser.LogLevel; returnCodeOffset = parser.ReturnCodeOffset; var filter = BuildFilter(parser); string outputFile; if (!GetFullOutputFile(parser, out outputFile)) { return(returnCodeOffset + 1); } IPerfCounters perfCounter = new NullPerfCounter(); if (parser.EnablePerformanceCounters) { if (new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator)) { perfCounter = new PerfCounters(); } else { throw new InvalidCredentialException("You must be running as an Administrator to enable performance counters."); } } using (var container = new Bootstrapper(logger)) { var persistance = new FilePersistance(parser, logger); container.Initialise(filter, parser, persistance, perfCounter); persistance.Initialise(outputFile); var registered = false; try { if (parser.Register) { ProfilerRegistration.Register(parser.UserRegistration); registered = true; } var harness = container.Resolve <IProfilerManager>(); harness.RunProcess((environment) => { returnCode = 0; if (parser.Service) { RunService(parser, environment, logger); } else { returnCode = RunProcess(parser, environment); } }, parser.Service); DisplayResults(persistance, parser, logger); } catch (Exception ex) { Trace.WriteLine(string.Format("Exception: {0}\n{1}", ex.Message, ex.InnerException)); throw; } finally { if (parser.Register && registered) { ProfilerRegistration.Unregister(parser.UserRegistration); } } } perfCounter.ResetCounters(); } catch (Exception ex) { if (logger.IsFatalEnabled) { logger.FatalFormat("An exception occured: {0}", ex.Message); logger.FatalFormat("stack: {0}", ex.StackTrace); } returnCode = returnCodeOffset + 1; } return(returnCode); }