public void Init(string targetDir, string[] files) { CacheDirectory = ResolveCacheDirectory(targetDir); if (!Directory.Exists(CacheDirectory)) // Todo: do all below once, if we have to create the directory { Directory.CreateDirectory(CacheDirectory); } Manifests = BinaryEtwObservable.BinaryManifestFromSequentialFiles(files) .ToEnumerable() .GroupBy(manifest => manifest.ManifestId, StringComparer.OrdinalIgnoreCase) .Select(grp => grp.First()) .ToArray(); if (Manifests.Length == 0) { throw new Exception("No Bond manifests found"); } var sources = new List <string>(Manifests.Length); foreach (var m in Manifests) { string bondFileName = Path.Combine(CacheDirectory, "BondTypes" + sources.Count + ".bond"); File.WriteAllText(bondFileName, m.Manifest); var source = GenerateCSharpCode(bondFileName); sources.Add(source); } foreach (var m in Manifests) { var source = GenerateManifestOverrideCSharpClass( m.Manifest, m.ManifestId); sources.Add(source); } string asm = Path.Combine(CacheDirectory, @"BondTypes.dll"); OutputAssembly(sources.ToArray(), asm); }
/// <summary> /// Returns argument values to pass to into the data context's constructor, based on a given connection info. /// </summary> /// <param name="cxInfo"> Connection information, as entered by the user. </param> /// <returns> Argument values to pass to into the data context's constructor. </returns> public override object[] GetContextConstructorArguments(IConnectionInfo cxInfo) { var playback = new Playback(); var bondInEtwProperties = new BondInEtwProperties(cxInfo); try { var bondTypemap = new GeneralPartitionableTypeMap(); var typeMaps = TypeFinder .LoadTypeMaps(TypeCache.ResolveCacheDirectory(bondInEtwProperties.ContextName)) .Where(i => i != typeof(GeneralPartitionableTypeMap)) .Select(Activator.CreateInstance) .OfType <ITypeMap <BinaryEnvelope> >() .Concat(new[] { bondTypemap }) .ToArray(); foreach (var file in bondInEtwProperties.Files) { var file1 = file; ((IPlaybackConfiguration)playback).AddInput( () => BinaryEtwObservable.FromFiles(file1), typeMaps); } Thread.SetData(LocalDataStoreSlot, playback); return(new object[] { playback }); } catch (Exception error) { MessageBox.Show(error.Message, "Error compiling generated code."); } return(new object[0]); }
public Dictionary <Type, EventStatistics> GetTypeStatistics(TypeCache typeCache, string inputFile) { var statsPerType = new Dictionary <Type, EventStatistics>(); if (string.IsNullOrWhiteSpace(inputFile)) { throw new ArgumentException("inputFile"); } Console.WriteLine("Getting Statistics..."); var rawCount = from events in BinaryEtwObservable.FromSequentialFiles(inputFile) group events by events.PayloadId into eventTypes from all in eventTypes.Aggregate( new { EventCount = (long)0, Bytes = (long)0, minTime = long.MaxValue, maxTime = 0L }, (ac, events) => new { EventCount = ac.EventCount + 1, Bytes = ac.Bytes + events.EventPayloadLength, minTime = Math.Min(ac.minTime, events.ReceiveFileTimeUtc), maxTime = Math.Max(ac.maxTime, events.ReceiveFileTimeUtc) }) select new { ManifestId = eventTypes.Key, all.EventCount, all.Bytes, all.minTime, all.maxTime }; var counts = rawCount.ToEnumerable().ToArray(); foreach (var c in counts) { var manifest = typeCache.Manifests .FirstOrDefault(m => string.Equals(m.ManifestId, c.ManifestId, StringComparison.OrdinalIgnoreCase)); if (manifest != null) { var line = manifest.Manifest .Split('\n').LastOrDefault(l => l.Trim().StartsWith(@"struct ", StringComparison.OrdinalIgnoreCase)); var className = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[1].Trim(); var type = typeCache.Types.FirstOrDefault(t => t.Name == className); var minDateTime = DateTime.FromFileTimeUtc(c.minTime); var maxDateTime = DateTime.FromFileTimeUtc(c.maxTime); var duration = (maxDateTime - minDateTime).TotalSeconds; if (Math.Abs(this.duration) < 0.01) { this.duration = 1; } var stats = new EventStatistics { AverageByteSize = c.Bytes / c.EventCount, ByteSize = c.Bytes, EventCount = c.EventCount, EventsPerSecond = c.EventCount / duration }; statsPerType.Add(type, stats); } } return(statsPerType); }
public void Initialize(string targetDir, params string[] files) { // Don't validate targetDir for empty. it will be created. if (targetDir == null) { throw new ArgumentNullException("targetDir"); } if (files == null || files.Length <= 0) { throw new ArgumentNullException("files"); } this.CacheDirectory = ResolveCacheDirectory(targetDir); if (!Directory.Exists(CacheDirectory)) { Directory.CreateDirectory(CacheDirectory); } else { Utilities.EmptyDirectory(this.CacheDirectory); } // Get all manifests. var manifestsFromFiles = BinaryEtwObservable.BinaryManifestFromSequentialFiles(files) .ToEnumerable() .Where(a => !string.IsNullOrWhiteSpace(a.Manifest)) .GroupBy(manifest => manifest.ManifestId, StringComparer.OrdinalIgnoreCase) .Select(grp => grp.First()) .ToArray(); if (manifestsFromFiles.Length == 0) { throw new Exception("No Bond manifests found. Ensure that the Bond manifests are written to the ETL file."); } int counter = 0; foreach (var manifestItem in manifestsFromFiles) { var codeSources = new List <string>(); var namespaceAndClasses = this.ParseClassNames(manifestItem.Manifest); var assembliesOfThisManifest = new List <string>(); if (!this.AreAnyTypesAlreadyInCache(namespaceAndClasses.Item2)) { string bondFileName = Path.Combine(CacheDirectory, "BondTypes" + counter + ".bond"); counter++; File.WriteAllText(bondFileName, manifestItem.Manifest); var codeGenerated = this.GenerateCSharpCode(bondFileName); if (!string.IsNullOrWhiteSpace(codeGenerated)) { codeSources.Add(codeGenerated); foreach (var @class in namespaceAndClasses.Item2) { var completeName = namespaceAndClasses.Item1 + "." + @class; var id = BondIdentifierHelpers.GenerateGuidFromName(@class.ToUpperInvariant()); // case when single manifest has multiple structs. if (namespaceAndClasses.Item2.Length > 1 && !string.Equals(manifestItem.ManifestId, id.ToString(), StringComparison.OrdinalIgnoreCase)) { codeSources.Add(GenerateAdditionalSourceCodeItems(namespaceAndClasses.Item1, @class, id)); this.Cache.Add(new TypeCacheItem { Manifest = new EventManifest { ActivityId = manifestItem.ActivityId, Manifest = manifestItem.Manifest, ManifestId = id.ToString(), OccurenceFileTimeUtc = manifestItem.OccurenceFileTimeUtc, Protocol = manifestItem.Protocol, ReceiveFileTimeUtc = manifestItem.ReceiveFileTimeUtc, Source = manifestItem.Source, }, }); } else { codeSources.Add(GenerateAdditionalSourceCodeItems(namespaceAndClasses.Item1, @class, new Guid(manifestItem.ManifestId))); this.Cache.Add(new TypeCacheItem { Manifest = new EventManifest { ActivityId = manifestItem.ActivityId, Manifest = manifestItem.Manifest, ManifestId = manifestItem.ManifestId, OccurenceFileTimeUtc = manifestItem.OccurenceFileTimeUtc, Protocol = manifestItem.Protocol, ReceiveFileTimeUtc = manifestItem.ReceiveFileTimeUtc, Source = manifestItem.Source, }, }); } } // After building the assembly, the types will be available. string asm = Path.Combine(CacheDirectory, bondFileName + ".dll"); this.OutputAssembly(codeSources.ToArray(), asm); assembliesOfThisManifest.Add(asm); try { var types = assembliesOfThisManifest.Select(Assembly.LoadFile) .SelectMany(a => a.GetTypes().Where(type => type.IsPublic)) .ToArray(); foreach (var item in types) { var targetCacheItem = this.FindMatchOrDefault(item.GUID.ToString()); if (targetCacheItem != null) { targetCacheItem.Type = item; } } } catch { //Ignore this type throw; } } } } }
private Dictionary <Type, CsvRelatedStats> CalculateTypeStatistics(TypeCache typeCache, string inputFile) { Console.WriteLine("Getting statistics from file {0}.", inputFile); var statsPerType = new Dictionary <Type, CsvRelatedStats>(); Stopwatch sw = Stopwatch.StartNew(); var rawCount = from events in BinaryEtwObservable.FromSequentialFiles(inputFile) group events by events.TypeId into eventTypes from all in eventTypes.Aggregate( new { EventCount = (long)0, Bytes = (long)0, minTime = long.MaxValue, maxTime = 0L }, (ac, events) => new { EventCount = ac.EventCount + 1, Bytes = ac.Bytes + events.Payload.Length, minTime = Math.Min(ac.minTime, events.ReceivedTime.ToFileTime()), maxTime = Math.Max(ac.maxTime, events.ReceivedTime.ToFileTime()), }) select new { ManifestId = eventTypes.Key, all.EventCount, all.Bytes, all.minTime, all.maxTime, }; var counts = rawCount.ToEnumerable().ToArray(); sw.Stop(); Console.WriteLine("Query took {0} milliseconds.", sw.ElapsedMilliseconds); foreach (var c in counts) { var typeCacheItem = typeCache.FindMatchOrDefault(c.ManifestId); if (typeCacheItem != null && typeCacheItem.Type != null) { var type = typeCacheItem.Type; if (type != null) { var minDateTime = DateTime.FromFileTimeUtc(c.minTime); var maxDateTime = DateTime.FromFileTimeUtc(c.maxTime); var duration = (maxDateTime - minDateTime).TotalSeconds; if (Math.Abs(duration) < 0.01) { duration = 1; } var stats = new EventStatistics { AverageByteSize = c.Bytes / c.EventCount, ByteSize = c.Bytes, EventCount = c.EventCount, EventsPerSecond = Math.Round(c.EventCount / duration, 3, MidpointRounding.AwayFromZero), }; statsPerType[type] = new CsvRelatedStats { ManifestId = c.ManifestId, Statistics = stats, }; } } } return(statsPerType); }
private Dictionary <Type, CsvRelatedStats> CalculateTypeStatistics(TypeCache typeCache, string inputFile) { Console.WriteLine("Getting statistics from file {0}.", inputFile); var statsPerType = new Dictionary <Type, CsvRelatedStats>(); Stopwatch sw = Stopwatch.StartNew(); var rawCount = from events in BinaryEtwObservable.FromSequentialFiles(inputFile) group events by events.PayloadId into eventTypes from all in eventTypes.Aggregate( new { EventCount = (long)0, Bytes = (long)0, minTime = long.MaxValue, maxTime = 0L }, (ac, events) => new { EventCount = ac.EventCount + 1, Bytes = ac.Bytes + events.EventPayloadLength, minTime = Math.Min(ac.minTime, events.ReceiveFileTimeUtc), maxTime = Math.Max(ac.maxTime, events.ReceiveFileTimeUtc) }) select new { ManifestId = eventTypes.Key, all.EventCount, all.Bytes, all.minTime, all.maxTime }; var counts = rawCount.ToEnumerable().ToArray(); sw.Stop(); Console.WriteLine("Query took {0} milliseconds.", sw.ElapsedMilliseconds); foreach (var c in counts) { var manifest = typeCache.Manifests .FirstOrDefault(m => string.Equals(m.ManifestId, c.ManifestId, StringComparison.OrdinalIgnoreCase)); if (manifest != null) { var line = manifest.Manifest .Split('\n').LastOrDefault(l => l.Trim().StartsWith(@"struct ", StringComparison.OrdinalIgnoreCase)); if (line != null) { var className = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[1].Trim(); var type = typeCache.Types.FirstOrDefault(t => t.Name == className); if (type != null) { var minDateTime = DateTime.FromFileTimeUtc(c.minTime); var maxDateTime = DateTime.FromFileTimeUtc(c.maxTime); var duration = (maxDateTime - minDateTime).TotalSeconds; if (Math.Abs(duration) < 0.01) { duration = 1; } var stats = new EventStatistics { AverageByteSize = c.Bytes / c.EventCount, ByteSize = c.Bytes, EventCount = c.EventCount, EventsPerSecond = c.EventCount / duration }; statsPerType[type] = new CsvRelatedStats { ManifestId = c.ManifestId, Statistics = stats, }; } } } } return(statsPerType); }
public void Init(string targetDir, string[] files) { // Don't validate targetDir for empty. it will be created. if (targetDir == null) { throw new ArgumentNullException("targetDir"); } if (files == null || files.Length <= 0) { throw new ArgumentNullException("files"); } Stopwatch sw = Stopwatch.StartNew(); CacheDirectory = ResolveCacheDirectory(targetDir); if (!Directory.Exists(CacheDirectory)) { // Todo: do all below once, if we have to create the directory Directory.CreateDirectory(CacheDirectory); } else { // Clean Directory.Delete(CacheDirectory, true); // Recreate. Directory.CreateDirectory(CacheDirectory); } var manifestsFromFiles = BinaryEtwObservable.BinaryManifestFromSequentialFiles(files) .ToEnumerable() .GroupBy(manifest => manifest.ManifestId, StringComparer.OrdinalIgnoreCase) .Select(grp => grp.First()) .ToArray(); if (manifestsFromFiles.Length == 0) { throw new Exception("No Bond manifests found"); } var sources = new List <string>(); List <EventManifest> manifests = new List <EventManifest>(); foreach (var manifestTypes in manifestsFromFiles .GroupBy(i => i.Manifest, StringComparer.Ordinal)) { string bondFileName = Path.Combine(CacheDirectory, "BondTypes" + sources.Count + ".bond"); File.WriteAllText(bondFileName, manifestTypes.Key); var source = this.GenerateCSharpCode(bondFileName); if (!string.IsNullOrWhiteSpace(source)) { manifests.Add(manifestTypes.First()); sources.Add(source); } var manifestInfo = this.ParseClassNames(manifestTypes.Key); sources.AddRange(GenerateAdditionalSourceCodeItems(manifestInfo, manifestTypes.Select(i => i.ManifestId).ToArray())); } this.Manifests = manifests.ToArray(); string asm = Path.Combine(CacheDirectory, @"BondTypes.dll"); OutputAssembly(sources.ToArray(), asm); sw.Stop(); Console.WriteLine("TypeCache Init took {0} milliseconds.", sw.ElapsedMilliseconds); }