Example #1
0
        /// <summary>
        /// Creates a new profiler using a process start info of an executable and a data writer.
        /// </summary>
        public Profiler(ProcessStartInfo info, IProfilingDataWriter dataWriter, ProfilerOptions options)
        {
            if (dataWriter == null)
            {
                throw new ArgumentNullException("dataWriter");
            }

            if (info == null)
            {
                throw new ArgumentNullException("info");
            }

            if (!DetectBinaryType.IsDotNetExecutable(info.FileName))
            {
                throw new ProfilerException("File is not a valid .NET executable file!");
            }

            this.profilerOptions = options;

            this.is64Bit = DetectBinaryType.RunsAs64Bit(info.FileName);

            this.profilerOutput      = new StringBuilder();
            this.performanceCounters = options.Counters;

            foreach (var counter in performanceCounters)
            {
                counter.Reset();
            }

            this.dataWriter = dataWriter;

            this.threadListMutex   = new Mutex(false, MutexId);
            this.accessEventHandle = new EventWaitHandle(true, EventResetMode.ManualReset, this.AccessEventId);

            this.psi = info;
            this.psi.UseShellExecute = false;                                                                   // needed to get env vars working!
            this.psi.EnvironmentVariables["SharedMemoryName"]     = SharedMemoryId;
            this.psi.EnvironmentVariables["MutexName"]            = MutexId;                                    // mutex for process pause/continue sychronization
            this.psi.EnvironmentVariables["AccessEventName"]      = AccessEventId;                              // name for access event of controller
            this.psi.EnvironmentVariables["COR_ENABLE_PROFILING"] = "1";                                        // enable profiling; 0 = disable
            this.psi.EnvironmentVariables["COR_PROFILER"]         = ProfilerGuid;                               // GUID for the profiler
            this.psi.EnvironmentVariables["COMPLUS_ProfAPI_ProfilerCompatibilitySetting"] = "EnableV2Profiler"; // enable CLR 2.0 for CLR 4.0

            file = MemoryMappedFile.CreateSharedMemory(SharedMemoryId, profilerOptions.SharedMemorySize);

            fullView = file.MapView(0, profilerOptions.SharedMemorySize);

            this.dataWriter.ProcessorFrequency = GetProcessorFrequency();

            this.logger              = new Thread(new ParameterizedThreadStart(Logging));
            this.logger.Name         = "Logger";
            this.logger.IsBackground = true;             // don't let the logger thread prevent our process from exiting

            this.dataCollector              = new Thread(new ThreadStart(DataCollection));
            this.dataCollector.Name         = "DataCollector";
            this.dataCollector.IsBackground = true;
        }
        public void WriteAndReadFromBufferUsingMemoryMappedFile()
        {
            using (MemoryMappedFile mmf1 = MemoryMappedFile.CreateSharedMemory("Local\\TestMemory", 1024)) {
                using (UnmanagedMemory view1 = mmf1.MapView(0, 1024)) {
                    using (UnmanagedCircularBuffer ncb = UnmanagedCircularBuffer.Create(view1.Start, (int)view1.Length)) {
                        using (Stream ws = ncb.CreateWritingStream()) {
                            ws.WriteByte(0x42);
                        }

                        using (MemoryMappedFile mmf2 = MemoryMappedFile.Open("Local\\TestMemory")) {
                            using (UnmanagedMemory view2 = mmf1.MapView(0, 1024)) {
                                Assert.AreNotEqual(view1.Start, view2.Start);
                                using (UnmanagedCircularBuffer ncb2 = UnmanagedCircularBuffer.Open(view2.Start)) {
                                    using (Stream rs = ncb2.CreateReadingStream()) {
                                        Assert.AreEqual(0x42, rs.ReadByte());
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }