Exemplo n.º 1
0
        public void AverageCentreOneValue(int percentageToKeep, int expected, string values)
        {
            var data   = values.Split(",").Select(int.Parse).ToList();
            var result = TraceProcessor.AverageCentre(data, percentageToKeep);

            Assert.AreEqual(expected, result);
        }
Exemplo n.º 2
0
        private static int Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.Error.WriteLine("Usage CheckForAppCrash <trace.etl>");
                return(-1);
            }

            string tracePath = args[0];

            using (ITraceProcessor trace = TraceProcessor.Create(tracePath))
            {
                IPendingResult <IProcessDataSource> pendingProcesses = trace.UseProcesses();

                trace.Process();

                IProcessDataSource processData = pendingProcesses.Result;

                foreach (IProcess process in processData.Processes)
                {
                    if (string.Equals("werfault.exe", process.ImageName, StringComparison.OrdinalIgnoreCase))
                    {
                        return(1);
                    }
                }
            }

            return(0);
        }
Exemplo n.º 3
0
        private static void Process(string path)
        {
            try
            {
                if (string.IsNullOrEmpty(path))
                {
                    Console.Error.WriteLine("ETL file location not provided...");
                    return;
                }

                using (ITraceProcessor trace = TraceProcessor.Create(path))
                {
                    IPendingResult <IProcessDataSource> pendingProcessData = trace.UseProcesses();

                    trace.Process();

                    IProcessDataSource processData = pendingProcessData.Result;

                    foreach (IProcess process in processData.Processes)
                    {
                        log.Info(process.CommandLine);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occured while processing. {ex}");
                throw ex;
            }
        }
 public void CalculateUsingNull()
 {
     // Act
     Assert.Throws <ArgumentNullException>(() => TraceProcessor.CalculateDelta(null, new List <int>()));
     Assert.Throws <ArgumentNullException>(() => TraceProcessor.CalculateDelta(new List <int>(), null));
     Assert.Throws <ArgumentNullException>(() => TraceProcessor.CalculateDelta(null, null));
 }
Exemplo n.º 5
0
        public void AdjustBadSteadyStateToGoodStateState(string bad, string expected)
        {
            var data   = bad.Split(",").Select(int.Parse).ToList();
            var result = TraceProcessor.AdjustBadSteadyStateToGoodStateState(1000, data);

            Assert.AreEqual(expected, string.Join(',', result));
        }
Exemplo n.º 6
0
        public void SetRiseLocationToZero(int rise, string values, string expected)
        {
            var data   = values.Split(",").Select(int.Parse).ToList();
            var result = TraceProcessor.SetRiseLocationToColumnZero(data, rise);

            Assert.AreEqual(expected, string.Join(',', result));
        }
Exemplo n.º 7
0
        public void FindRiseColumn(int expected, int rise, string values)
        {
            var data   = values.Split(",").Select(int.Parse).ToList();
            var result = TraceProcessor.FindRiseColumn(data, rise);

            Assert.AreEqual(expected, data[result]);
            Console.WriteLine("Column {0}", result);
        }
Exemplo n.º 8
0
    static void Main(string[] args)
    {
        bool   showCPUUsage = false;
        string traceName    = "";

        foreach (string arg in args)
        {
            if (arg == "-c" || arg == "--cpuusage")
            {
                showCPUUsage = true;
            }
            else if (traceName.Length == 0)
            {
                traceName = arg;
            }
            else
            {
                Console.Error.WriteLine("error: unrecognized arguments: {0}", arg);
                return;
            }
        }

        if (traceName.Length == 0)
        {
            Console.Error.WriteLine("usage: IdentifyChromeProcesses.exe [-c] trace");
            Console.Error.WriteLine("error: too few arguments");
            return;
        }

        if (!File.Exists(traceName))
        {
            Console.Error.WriteLine("File '{0}' does not exist.", traceName);
            return;
        }

        try
        {
            using (ITraceProcessor trace = TraceProcessor.Create(traceName))
                ProcessTrace(trace, showCPUUsage, false);
        }
        catch (System.InvalidOperationException e)
        {
            // Note that wpaexporter doesn't seem to have a way to handle this,
            // which is one advantage of TraceProcessing. Note that traces with
            // lost events are "corrupt" in some sense so the results will be
            // unpredictable.
            Console.WriteLine(e.Message);
            Console.WriteLine("Trying again with AllowLostEvents and AllowTimeInversion specified. Results may be less reliable.");
            Console.WriteLine();

            var settings = new TraceProcessorSettings();
            settings.AllowLostEvents    = true;
            settings.AllowTimeInversion = true;
            using (ITraceProcessor trace = TraceProcessor.Create(traceName, settings))
                ProcessTrace(trace, showCPUUsage, true);
        }
    }
Exemplo n.º 9
0
        private void DeleteFile(string filename)
        {
            var folder = TraceProcessor.GetCurrentFolder();
            var file   = Path.Combine(folder, filename);

            if (File.Exists(file))
            {
                File.Delete(file);
            }
        }
Exemplo n.º 10
0
        public List <double> ReadDeltaFile()
        {
            var folder   = TraceProcessor.GetCurrentFolder();
            var filename = Path.Combine(folder, Constants.GoodTraceFilename);

            return(File.ReadAllText(filename)
                   .Split(',')
                   .Select(double.Parse)
                   .ToList());
        }
    static void Main(string[] args)
    {
        if (args.Length != 3)
        {
            Console.Error.WriteLine("Usage: GetCpuSampleDuration.exe <trace.etl> <imageName> <functionName>");
            return;
        }

        string tracePath    = args[0];
        string imageName    = args[1];
        string functionName = args[2];

        Dictionary <string, Duration> matchDurationByCommandLine = new Dictionary <string, Duration>();

        using (ITraceProcessor trace = TraceProcessor.Create(tracePath))
        {
            IPendingResult <ISymbolDataSource>    pendingSymbolData      = trace.UseSymbols();
            IPendingResult <ICpuSampleDataSource> pendingCpuSamplingData = trace.UseCpuSamplingData();

            trace.Process();

            ISymbolDataSource    symbolData      = pendingSymbolData.Result;
            ICpuSampleDataSource cpuSamplingData = pendingCpuSamplingData.Result;

            symbolData.LoadSymbolsForConsoleAsync(SymCachePath.Automatic, SymbolPath.Automatic).GetAwaiter().GetResult();
            Console.WriteLine();

            IThreadStackPattern pattern = AnalyzerThreadStackPattern.Parse($"{imageName}!{functionName}");

            foreach (ICpuSample sample in cpuSamplingData.Samples)
            {
                if (sample.IsExecutingDeferredProcedureCall == true || sample.IsExecutingInterruptServicingRoutine == true)
                {
                    continue;
                }

                if (sample.Stack != null && sample.Stack.Matches(pattern))
                {
                    string commandLine = sample.Process.CommandLine;

                    if (!matchDurationByCommandLine.ContainsKey(commandLine))
                    {
                        matchDurationByCommandLine.Add(commandLine, Duration.Zero);
                    }

                    matchDurationByCommandLine[commandLine] += sample.Weight;
                }
            }
        }

        foreach (string commandLine in matchDurationByCommandLine.Keys)
        {
            Console.WriteLine($"{commandLine}: {matchDurationByCommandLine[commandLine]}");
        }
    }
Exemplo n.º 12
0
        private void WriteFile <T>(string filename, List <T> data)
        {
            var folder = TraceProcessor.GetCurrentFolder();
            var output = Path.Combine(folder, filename);

            Console.WriteLine($"output: {output}");

            var text = string.Join(',', data);

            File.WriteAllText(output, text);
        }
    static void Run(string tracePath)
    {
        using (ITraceProcessor trace = TraceProcessor.Create(tracePath))
        {
            IPendingResult <IProcessDataSource> pendingProcessData = trace.UseProcesses();

            trace.Process();

            IProcessDataSource processData = pendingProcessData.Result;

            Console.WriteLine(processData.Processes.Count);
        }
    }
Exemplo n.º 14
0
        private static IReadOnlyList <string> GetServices(string tracePath)
        {
            using (ITraceProcessor trace = TraceProcessor.Create(tracePath))
            {
                IPendingResult <IServiceDataSource> pendingServices = trace.UseServices();

                trace.Process();

                IServiceDataSource serviceData = pendingServices.Result;

                return(serviceData.Services.Select(s => Cleanup(s.Name)).ToArray());
            }
        }
Exemplo n.º 15
0
        public List <int> ReadGoodFile()
        {
            var folder   = TraceProcessor.GetCurrentFolder();
            var filename = Path.Combine(folder, Constants.GoodTraceFilename);
            var result   = _reader.ReadInput(filename);

            if (!result.success)
            {
                Console.WriteLine($"Unable to read in good alpha file {filename}");
                return(null);
            }

            return(result.data[0]);
        }
Exemplo n.º 16
0
        public void MinColumnCount()
        {
            var good = new List <int> {
                1, 2, 3, 4
            };
            var bad = new List <int> {
                5, 6
            };
            var result = TraceProcessor.CalculateDelta(good, bad);

            Assert.IsNotNull(result);
            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Count);
        }
Exemplo n.º 17
0
        public void CalculateDeltaUnevenData()
        {
            var good = new List <int> {
                3000, 3000, 3000
            };
            var bad = new List <int> {
                2900, 3000, 3111
            };
            var result = TraceProcessor.CalculateDelta(good, bad);

            Assert.NotNull(result);
            Assert.AreEqual(good.Count, result.Count);
            Assert.AreEqual(3000.0 / 2900.0, result[0]);
            Assert.AreEqual(1.0, result[1]);
            Assert.AreEqual(3000.0 / 3111.0, result[2]);
        }
Exemplo n.º 18
0
        private void GatherTraceData()
        {
            using (ITraceProcessor trace = TraceProcessor.Create(EtlPath))
            {
                IPendingResult <IProcessDataSource>       pendingProcessData       = trace.UseProcesses();
                IPendingResult <IThreadDataSource>        pendingThreadData        = trace.UseThreads();
                IPendingResult <ICpuSampleDataSource>     pendingCpuSampleData     = trace.UseCpuSamplingData();
                IPendingResult <ICpuSchedulingDataSource> pendingCpuSchedulingData = trace.UseCpuSchedulingData();

                trace.Process();

                GatherProcessData(pendingProcessData.Result);
                GatherThreadData(pendingThreadData.Result);
                GatherCpuSampleData(pendingCpuSampleData.Result);
                GatherCpuSchedulingData(pendingCpuSchedulingData.Result);
            }
        }
Exemplo n.º 19
0
        public void TransposeTest()
        {
            // Arrange
            var data = new List <List <int> >
            {
                new List <int> {
                    3, 1, 2
                },
                new List <int> {
                    600, 200, 300
                }
            };

            // Act
            var result = TraceProcessor.Transpose(data);

            foreach (var y in result)
            {
                foreach (var x in y)
                {
                    Console.Write($"{x}, ");
                }

                Console.WriteLine();
            }

            // Assert
            Assert.AreEqual(3, result.Count);

            var row = result[0];

            Assert.AreEqual(2, row.Count);
            Assert.AreEqual(3, row[0]);
            Assert.AreEqual(600, row[1]);

            row = result[1];
            Assert.AreEqual(2, row.Count);
            Assert.AreEqual(1, row[0]);
            Assert.AreEqual(200, row[1]);

            row = result[2];
            Assert.AreEqual(2, row.Count);
            Assert.AreEqual(2, row[0]);
            Assert.AreEqual(300, row[1]);
        }
Exemplo n.º 20
0
        static void Main(string[] args)
        {
            if (args.Length != 1 || string.IsNullOrWhiteSpace(args[0]))
            {
                Console.Error.WriteLine("Usage: <trace.etl>");
                return;
            }

            using (var trace = TraceProcessor.Create(args[0]))
            {
                var pendingProcessData = trace.UseProcesses();
                trace.Process();
                var processData = pendingProcessData.Result;

                foreach (var process in processData.Processes)
                {
                    Console.WriteLine(process.CommandLine);
                }
            }
        }
Exemplo n.º 21
0
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.Error.WriteLine("Usage: <trace.etl>");
                return;
            }

            using (ITraceProcessor trace = TraceProcessor.Create(args[0]))
            {
                IPendingResult <IProcessDataSource> pendingProcessData = trace.UseProcesses();
                trace.Process();
                IProcessDataSource processData = pendingProcessData.Result;

                foreach (IProcess process in processData.Processes)
                {
                    Console.WriteLine(process.CommandLine);
                }
            }
        }
    public static int Main(string[] args)
    {
        if (args.Length != 1)
        {
            Console.Error.WriteLine("Usage: FindZombieProcess.exe <trace.etl>");
            return(1);
        }

        string tracePath = args[0];

        TraceProcessorSettings settings = new TraceProcessorSettings {
            AllowLostEvents = true
        };

        using (ITraceProcessor trace = TraceProcessor.Create(tracePath, settings))
        {
            IPendingResult <IHandleDataSource> pendingHandleData = trace.UseHandles();
            IPendingResult <ISymbolDataSource> pendingSymbolData = trace.UseSymbols();

            trace.Process();

            IHandleDataSource handleData = pendingHandleData.Result;
            ISymbolDataSource symbolData = pendingSymbolData.Result;

            symbolData.LoadSymbolsForConsoleAsync(SymCachePath.Automatic, SymbolPath.Automatic).GetAwaiter().GetResult();

            foreach (IProcessHandle processHandle in handleData.ProcessHandles)
            {
                // Zombie processes are processes which have exited but which still have a running process holding a handle to them
                if (processHandle.Process != null && !processHandle.CloseTime.HasValue &&
                    processHandle.Process.ExitTime.HasValue)
                {
                    string owningProcessName = processHandle.Owner?.ImageName ?? "Unknown";
                    string targetProcessName = processHandle.Process?.ImageName ?? "Unknown";
                    Console.WriteLine($"Owning process: {owningProcessName} has handle to: {targetProcessName}");
                }
            }

            return(0);
        }
    }
    static void Main(string[] args)
    {
        if (args.Length != 1)
        {
            Console.Error.WriteLine("Usage: ListImages.exe <trace.etl>");
            return;
        }

        string tracePath = args[0];

        using (ITraceProcessor trace = TraceProcessor.Create(tracePath))
        {
            IPendingResult <IProcessDataSource> pendingProcessData = trace.UseProcesses();

            trace.Process();

            IProcessDataSource processData = pendingProcessData.Result;

            foreach (IProcess process in processData.Processes)
            {
                foreach (IImage image in process.Images)
                {
                    DataSize    ImageSize       = image.Size;
                    long        TimeDataStamp   = image.Timestamp;
                    string      OrigFileName    = image.OriginalFileName;
                    string      FileDescription = image.FileDescription;
                    string      FileVersion     = image.FileVersion;
                    Version     BinFileVersion  = image.FileVersionNumber;
                    CultureInfo VerLanguage     = image.Locale;
                    string      ProductName     = image.ProductName;
                    string      CompanyName     = image.CompanyName;
                    string      ProductVersion  = image.ProductVersion;
                    string      FileId          = image.CompatibilityFileId;
                    string      ProgramId       = image.CompatibilityProgramId;
                }
            }
        }
    }
    static Timestamp GetBootTime(string tracePath)
    {
        Timestamp result = Timestamp.Zero;

        using (ITraceProcessor trace = TraceProcessor.Create(tracePath))
        {
            // Microsoft-Windows-Shell-Core
            trace.Use(new Guid[] { new Guid("30336ed4-e327-447c-9de0-51b652c86108") }, e =>
            {
                // PerfTrack_Explorer_ExplorerStartToDesktopReady
                if (e.Id != 27231)
                {
                    return;
                }

                result = e.Timestamp;
            });

            trace.Process();
        }

        return(result);
    }
        private static IReadOnlyDictionary <PageKey, uint> GetResidentSetPageCounts(string tracePath,
                                                                                    Timestamp startTime, Timestamp stopTime)
        {
            using (ITraceProcessor trace = TraceProcessor.Create(tracePath))
            {
                IPendingResult <IResidentSetDataSource> pendingResidentSet = trace.UseResidentSetData();

                trace.Process();

                IResidentSetDataSource residentSetData = pendingResidentSet.Result;

                Dictionary <PageKey, uint> pageCounts = new Dictionary <PageKey, uint>();

                foreach (IResidentSetSnapshot snapshot in residentSetData.Snapshots)
                {
                    if (snapshot.Timestamp < startTime || snapshot.Timestamp > stopTime)
                    {
                        continue;
                    }

                    foreach (IResidentSetPage page in snapshot.Pages)
                    {
                        PageKey key = new PageKey(snapshot.Timestamp, page.MemoryManagerListType, page.Priority);

                        if (!pageCounts.ContainsKey(key))
                        {
                            pageCounts.Add(key, 0);
                        }

                        ++pageCounts[key];
                    }
                }

                return(pageCounts);
            }
        }
Exemplo n.º 26
0
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Specify the name of one trace to be summarized.");
                return;
            }

            var traceName = args[0];

            if (!File.Exists(traceName))
            {
                Console.Error.WriteLine("File '{0}' does not exist.", traceName);
                return;
            }

            var settings = new TraceProcessorSettings
            {
                // Don't print a setup message on first run.
                SuppressFirstTimeSetupMessage = true
            };

            using (ITraceProcessor trace = TraceProcessor.Create(traceName, settings))
            {
                // Get process details, including command lines.
                var pendingProcessData = trace.UseProcesses();
                // Get CPU performance counters, on every context switch.
                var pendingCounterData = trace.UseProcessorCounters();

                trace.Process();

                var processData = pendingProcessData.Result;
                var counterData = pendingCounterData.Result;

                var countersByProcess = FindInterestingProcesses(processData);

                // Accumulate data for all of the interesting processes.
                foreach (var entry in counterData.ContextSwitchCounterDeltas)
                {
                    // This sometimes happens - handle it.
                    if (entry.Process == null)
                    {
                        continue;
                    }

                    Counters last;
                    if (!countersByProcess.TryGetValue(entry.Process, out last))
                    {
                        continue;
                    }

                    // Accumulate counter values and execution time.
                    foreach (var key in entry.RawCounterDeltas.Keys)
                    {
                        last.counters.TryGetValue(key, out ulong lastCount);
                        lastCount         += entry.RawCounterDeltas[key];
                        last.counters[key] = lastCount;
                    }
                    last.runTime_ns      += (entry.StopTime - entry.StartTime).Nanoseconds;
                    last.contextSwitches += 1;

                    countersByProcess[entry.Process] = last;
                }

                // Sort the data by CPU time and print it.
                var sortedCounterData = new List <KeyValuePair <IProcess, Counters> >(countersByProcess);
                sortedCounterData.Sort((x, y) => y.Value.runTime_ns.CompareTo(x.Value.runTime_ns));

                bool printHeader = true;
                foreach (var entry in sortedCounterData)
                {
                    if (printHeader)
                    {
                        Console.Write("{0,-29} - CPU time (s) - context switches", "Image name");
                        foreach (var counterName in entry.Value.counters.Keys)
                        {
                            int fieldWidth = Math.Max(13, counterName.Length);
                            Console.Write(", {0}", counterName.PadLeft(fieldWidth));
                        }
                        Console.WriteLine();
                        printHeader = false;
                    }

                    // Arbitrary cutoff for what is "interesting"
                    if (entry.Value.runTime_ns < 100 * 1000 * 1000)
                    {
                        continue;
                    }

                    Console.Write("{0,-29} -     {1,8:0.00} - {2,16}", entry.Value.description,
                                  entry.Value.runTime_ns / 1e9, entry.Value.contextSwitches);
                    foreach (var counterName in entry.Value.counters.Keys)
                    {
                        int fieldWidth = Math.Max(13, counterName.Length);
                        Console.Write(", {0}",
                                      entry.Value.counters[counterName].ToString().PadLeft(fieldWidth));
                    }
                    Console.WriteLine();
                }
            }
        }
Exemplo n.º 27
0
        static void RunWithOptions(Options opts)
        {
            using (ITraceProcessor trace = TraceProcessor.Create(opts.etlFileName))
            {
                IPendingResult <ICpuSampleDataSource> pendingCpuSampleData = trace.UseCpuSamplingData();
                IPendingResult <ISymbolDataSource>    pendingSymbolData    = trace.UseSymbols();

                trace.Process();

                ISymbolDataSource    symbolData    = pendingSymbolData.Result;
                ICpuSampleDataSource cpuSampleData = pendingCpuSampleData.Result;

                var symbolProgress = new Progress <SymbolLoadingProgress>(progress =>
                {
                    Console.Write("\r{0:P} {1} of {2} symbols processed ({3} loaded)",
                                  (double)progress.ImagesProcessed / progress.ImagesTotal,
                                  progress.ImagesProcessed,
                                  progress.ImagesTotal,
                                  progress.ImagesLoaded);
                });
                symbolData.LoadSymbolsAsync(
                    SymCachePath.Automatic, SymbolPath.Automatic, symbolProgress)
                .GetAwaiter().GetResult();
                Console.WriteLine();

                var profileWriter = new ProfileWriter(opts.etlFileName,
                                                      opts.includeInlinedFunctions,
                                                      opts.includeProcessAndThreadIds,
                                                      opts.stripSourceFileNamePrefix);

                var timeStart = opts.timeStart ?? 0;
                var timeEnd   = opts.timeEnd ?? decimal.MaxValue;

                var exportAllProcesses = opts.processFilter == "*";
                var processFilterSet   = new HashSet <string>(
                    opts.processFilter.Trim().Split(",", StringSplitOptions.RemoveEmptyEntries));

                for (int i = 0; i < cpuSampleData.Samples.Count; i++)
                {
                    if (i % 100 == 0)
                    {
                        Console.Write("\r{0:P} {1} of {2} samples processed",
                                      (double)i / cpuSampleData.Samples.Count, i, cpuSampleData.Samples.Count);
                    }

                    var cpuSample = cpuSampleData.Samples[i];

                    if ((cpuSample.IsExecutingDeferredProcedureCall ?? false) ||
                        (cpuSample.IsExecutingInterruptServicingRoutine ?? false))
                    {
                        continue;
                    }

                    if (!exportAllProcesses)
                    {
                        var processImage = cpuSample.Process.Images
                                           .FirstOrDefault(image => image.FileName == cpuSample.Process.ImageName);

                        string imagePath = processImage?.Path ?? cpuSample.Process.ImageName;

                        if (!processFilterSet.Any(filter => imagePath.Contains(filter.Replace("/", "\\"))))
                        {
                            continue;
                        }
                    }

                    var timestamp = cpuSample.Timestamp.RelativeTimestamp.TotalSeconds;
                    if (timestamp < timeStart || timestamp > timeEnd)
                    {
                        continue;
                    }

                    profileWriter.AddSample(cpuSample);
                }
                Console.WriteLine();

                long outputSize = profileWriter.Write(opts.outputFileName);
                Console.WriteLine("Wrote {0:N0} bytes to {1}", outputSize, opts.outputFileName);
            }
        }
Exemplo n.º 28
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Use this to summarize a heap snapshot or compare multiple heap snapshots");
                Console.WriteLine("from one run of a program.");
                return;
            }

            SnapshotSummary lastAllocs    = null;
            string          lastTracename = "";

            foreach (var arg in args)
            {
                if (!File.Exists(arg))
                {
                    Console.Error.WriteLine("File '{0}' does not exist.", arg);
                    continue;
                }
                using (ITraceProcessor trace = TraceProcessor.Create(arg))
                {
                    Console.WriteLine("Summarizing '{0}'", Path.GetFileName(arg));
                    var allocs = GetAllocSummary(trace);
                    if (allocs.allocsByStackId_ == null)
                    {
                        Console.WriteLine("Ignoring trace {0}.", arg);
                        continue;
                    }
                    Console.WriteLine("{0,7:F2} MB from {1,9:#,#} allocations on {2,7:#,#} stacks",
                                      allocs.totalBytes_.TotalMegabytes, allocs.allocCount_, allocs.allocsByStackId_.Count);

                    const int maxPrinted = 40;

                    Console.WriteLine("Hottest stack frames:");
                    // Display a summary of the first (possibly only) heap snapshot trace.
                    var sortedHotStackEntries = new List <KeyValuePair <string, long> >(allocs.hotStackFrames_);
                    sortedHotStackEntries.Sort((x, y) => y.Value.CompareTo(x.Value));
                    for (int i = 0; i < sortedHotStackEntries.Count && i < maxPrinted; ++i)
                    {
                        var data = sortedHotStackEntries[i];
                        Console.WriteLine("{0,5} allocs cross {1}", data.Value, data.Key);
                    }

                    if (lastAllocs != null)
                    {
                        Console.WriteLine("Comparing old ({0}) to new ({1}) snapshots.", Path.GetFileName(lastTracename), Path.GetFileName(arg));
                        if (allocs.pid_ != lastAllocs.pid_)
                        {
                            Console.WriteLine("WARNING: process IDs are different ({0} and {1}) so stack IDs may not be comparable.", lastAllocs.pid_, allocs.pid_);
                        }

                        var hotStackFramesDelta = new Dictionary <string, long>(allocs.hotStackFrames_);
                        // Subtract the lastAllocs stack frame counts fomr the current stack frame counts.
                        foreach (var entry in lastAllocs.hotStackFrames_)
                        {
                            hotStackFramesDelta.TryGetValue(entry.Key, out long count);
                            count -= entry.Value;
                            hotStackFramesDelta[entry.Key] = count;
                        }

                        Console.WriteLine("Hottest stack frame deltas:");
                        // Print the biggest deltas, positive then negative.
                        var sortedHotStackFramesDelta = new List <KeyValuePair <string, long> >(hotStackFramesDelta);
                        sortedHotStackFramesDelta.Sort((x, y) => y.Value.CompareTo(x.Value));
                        // Print the first half...
                        for (int i = 0; i < sortedHotStackFramesDelta.Count && i < maxPrinted / 2; ++i)
                        {
                            var data = sortedHotStackFramesDelta[i];
                            Console.WriteLine("{0,5} allocs cross {1}", data.Value, data.Key);
                        }
                        Console.WriteLine("...");
                        int start = sortedHotStackFramesDelta.Count - maxPrinted / 2;
                        if (start < 0)
                        {
                            start = 0;
                        }
                        for (int i = start; i < sortedHotStackFramesDelta.Count - 1; ++i)
                        {
                            var data = sortedHotStackFramesDelta[i];
                            Console.WriteLine("{0,5} allocs cross {1}", data.Value, data.Key);
                        }

                        ulong newOnlyStacks = 0;
                        ulong oldOnlyStacks = 0;
                        foreach (var tag in allocs.allocsByStackId_.Keys)
                        {
                            if (!lastAllocs.allocsByStackId_.ContainsKey(tag))
                            {
                                newOnlyStacks++;
                            }
                        }
                        foreach (var tag in lastAllocs.allocsByStackId_.Keys)
                        {
                            if (!allocs.allocsByStackId_.ContainsKey(tag))
                            {
                                oldOnlyStacks++;
                            }
                        }
                        Console.WriteLine("  Old snapshot had {0} unique-to-it stacks, new trace had {1} unique-to-it stacks.",
                                          oldOnlyStacks, newOnlyStacks);
                    }

                    lastAllocs    = allocs;
                    lastTracename = arg;
                }
            }
        }
Exemplo n.º 29
0
        private static int Main(string[] cmdLineArgs)
        {
            ParserResult <CommandLineArguments> o = Parser.Default.ParseArguments <CommandLineArguments>(cmdLineArgs);

            return(o.MapResult(
                       options =>
            {
                string sidecarJson = File.ReadAllText(options.SideCarFile);
                CLogSidecar sidecar = CLogSidecar.FromJson(sidecarJson);


                TextReader file = Console.In;

                if (!File.Exists(options.ETLFile))
                {
                    TraceLine(TraceType.Err, $"ETL File {options.ETLFile} doesnt exist");
                    return -1;
                }

                StreamWriter outputfile = null;
                if (!String.IsNullOrEmpty(options.OutputFile))
                {
                    outputfile = new StreamWriter(new FileStream(options.OutputFile, FileMode.Create));
                }

                try
                {
                    TraceProcessorSettings traceSettings = new TraceProcessorSettings {
                        AllowLostEvents = true, AllowTimeInversion = true
                    };

                    using (ITraceProcessor etwfile = TraceProcessor.Create(options.ETLFile, traceSettings))
                    {
                        HashSet <Guid> ids = new HashSet <Guid>();

                        foreach (var m in sidecar.EventBundlesV2)
                        {
                            foreach (var prop in m.Value.ModuleProperites)
                            {
                                if (prop.Key.Equals("MANIFESTED_ETW"))
                                {
                                    ids.Add(new Guid(prop.Value["ETW_Provider"]));
                                }
                                else if (prop.Key.Equals("TRACELOGGING"))
                                {
                                    ids.Add(new Guid(prop.Value["ETW_Provider"]));
                                }
                            }
                        }

                        var events = etwfile.UseGenericEvents(ids.ToArray());
                        etwfile.Process();

                        foreach (var e in events.Result.Events)
                        {
                            string line = "";

                            try
                            {
                                Dictionary <string, IClogEventArg> fixedUpArgs = new Dictionary <string, IClogEventArg>();
                                string errorString = "ERROR";

                                if (null == e.Fields)
                                {
                                    continue;
                                }

                                Dictionary <string, IClogEventArg> args = new Dictionary <string, IClogEventArg>();

                                foreach (var f in e.Fields)
                                {
                                    args[f.Name] = new ManifestedETWEvent(f);
                                }

                                CLogDecodedTraceLine bundle = null;
                                int eidAsInt = -1;

                                foreach (var b in sidecar.EventBundlesV2)
                                {
                                    Dictionary <string, string> keys;

                                    if (!e.IsTraceLogging)
                                    {
                                        if (!b.Value.ModuleProperites.TryGetValue("MANIFESTED_ETW", out keys))
                                        {
                                            continue;
                                        }

                                        string eid;

                                        if (!keys.TryGetValue("EventID", out eid))
                                        {
                                            continue;
                                        }

                                        eidAsInt = Convert.ToInt32(eid);

                                        if (eidAsInt == e.Id)
                                        {
                                            bundle = b.Value;
                                            errorString = "ERROR:" + eidAsInt;
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        if (e.ActivityName.Equals(b.Key))
                                        {
                                            bundle = b.Value;
                                            errorString = "ERROR:" + b.Key;
                                            break;
                                        }
                                    }
                                }

                                if (null == bundle)
                                {
                                    continue;
                                }

                                Dictionary <string, string> argMap;

                                if (e.IsTraceLogging)
                                {
                                    argMap = new Dictionary <string, string>();
                                    foreach (var arg in args)
                                    {
                                        argMap[arg.Key] = arg.Key;
                                    }
                                }
                                else
                                {
                                    argMap = sidecar.GetTracelineMetadata(bundle, "MANIFESTED_ETW");
                                }

                                var types = CLogFileProcessor.BuildTypes(sidecar.ConfigFile, null, bundle.TraceString, null, out string clean);

                                if (0 == types.Length)
                                {
                                    errorString = bundle.TraceString;
                                    goto toPrint;
                                }

                                int argIndex = 0;

                                foreach (var type in types)
                                {
                                    var arg = bundle.splitArgs[argIndex];
                                    CLogEncodingCLogTypeSearch node = sidecar.ConfigFile.FindType(arg);

                                    switch (node.EncodingType)
                                    {
                                    case CLogEncodingType.Synthesized:
                                        continue;

                                    case CLogEncodingType.Skip:
                                        continue;
                                    }

                                    string lookupArgName = argMap[arg.VariableInfo.SuggestedTelemetryName];

                                    if (!args.ContainsKey(lookupArgName))
                                    {
                                        Console.WriteLine($"Argmap missing {lookupArgName}");
                                        throw new Exception("InvalidType : " + node.DefinationEncoding);
                                    }

                                    if (0 != node.DefinationEncoding.CompareTo(type.TypeNode.DefinationEncoding))
                                    {
                                        Console.WriteLine("Invalid Types in Traceline");
                                        throw new Exception("InvalidType : " + node.DefinationEncoding);
                                    }

                                    fixedUpArgs[arg.VariableInfo.SuggestedTelemetryName] = args[lookupArgName];
                                    ++argIndex;
                                }

                                toPrint:

                                EventInformation ei = new EventInformation();
                                ei.Timestamp = e.Timestamp.DateTimeOffset;
                                ei.ProcessId = e.ProcessId.ToString("x");
                                ei.ThreadId = e.ThreadId.ToString("x");
                                DecodeAndTraceToConsole(outputfile, bundle, errorString, sidecar.ConfigFile, fixedUpArgs, ei, options.ShowTimestamps, options.ShowCPUInfo);
                            }
                            catch (Exception)
                            {
                                Console.WriteLine($"Invalid TraceLine : {line}");
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    CLogConsoleTrace.TraceLine(TraceType.Err, "ERROR : " + e);
                    if (null != outputfile)
                    {
                        outputfile.WriteLine("ERROR : " + e);
                    }
                }
                finally
                {
                    if (null != outputfile)
                    {
                        outputfile.Flush();
                        outputfile.Close();
                    }
                }
                return 0;
            }, err =>
            {
                Console.WriteLine("Bad Args : " + err);
                return -1;
            }));
        }
Exemplo n.º 30
0
    static void Main(string[] args)
    {
        string traceName = null;

        string[] moduleList = null;
        for (int i = 0; i < args.Length; /**/)
        {
            if (args[i] == "-modules")
            {
                ++i;
                if (i >= args.Length)
                {
                    Console.Error.WriteLine("Missing module list after -modules.");
                    return;
                }
                moduleList = args[i++].Split(';');
            }
            else
            {
                if (traceName != null)
                {
                    Console.Error.WriteLine("Unexpected argument '{0}'", args[i]);
                    return;
                }
                traceName = args[i++];
            }
        }

        if (traceName == null)
        {
            Console.Error.WriteLine("usage: gWindowsETLSummary.exe trace.etl [-modules module1.dll;module2.dll");
            Console.Error.WriteLine("error: too few arguments");
            Console.Error.WriteLine("The (case sensitive) -modules arguments are used to get inclusive CPU sampling data.");
            return;
        }

        if (!File.Exists(traceName))
        {
            // Print a more friendly error message for this case.
            Console.Error.WriteLine("File '{0}' does not exist.", traceName);
            return;
        }

        Console.WriteLine("Processing {0}...", traceName);
        var settings = new TraceProcessorSettings
        {
            // Don't print a setup message on first run.
            SuppressFirstTimeSetupMessage = true
        };

        try
        {
            using (ITraceProcessor trace = TraceProcessor.Create(traceName, settings))
                ProcessTrace(trace, moduleList);
        }
        catch (TraceLostEventsException e)
        {
            // Note that wpaexporter doesn't seem to have a way to handle this,
            // which is one advantage of TraceProcessing. Note that traces with
            // lost events are "corrupt" in some sense so the results will be
            // unpredictable.
            Console.WriteLine(e.Message);
            Console.WriteLine("Trying again with AllowLostEvents specified. Results may be less reliable.");
            Console.WriteLine();

            settings.AllowLostEvents = true;
            using (ITraceProcessor trace = TraceProcessor.Create(traceName, settings))
                ProcessTrace(trace, moduleList);
        }
    }