public void ParseTraces(Action <T> traceDispatcher, string fileName, DateTime startTime, DateTime endTime) { _traceDispatcher = traceDispatcher; using (var reader = new TraceFileEventReader(fileName)) { TraceSessionMetaData = reader.ReadTraceSessionMetadata(); if (TraceSessionMetaData.StartTime > endTime | TraceSessionMetaData.EndTime < startTime) { Log.Warning($"{fileName} outside time range start:{TraceSessionMetaData.StartTime} end:{TraceSessionMetaData.EndTime}"); return; } reader.EventRead += this.OnEventRead; reader.ReadEvents(startTime, endTime); EventsLost = (int)reader.EventsLost; } }
internal ulong ProcessTraces(string lttTracesDirectory, bool seekTimestamp, ulong timestampUnixEpochNanoSec) { this.traceSource.WriteInfo( this.logSourceId, "Starting to process Ltt traces at: {0} - starting at timestamp: {1}", lttTracesDirectory, LttngTraceFolderEventReader.ConvertFromUnixEpoch(timestampUnixEpochNanoSec).ToString("o")); StringBuilder lttTracesDir = new StringBuilder(); lttTracesDir.Append(lttTracesDirectory); // reseting events read counter. This is incremented in DeliverEventToSinks. this.numEventsRead = 0; // reseting the counter for events failed to be written to .dtr file. this.dataCorruptionStatistics = 0; string baseFileName = DateTime.UtcNow.ToString("yyyy-MM-dd_HH:mm:ss"); string csvOutputFilePath = Path.Combine(this.dtrTracesDirectory, baseFileName + ".trace"); string tableOutputFilePath = Path.Combine(this.dtrTracesDirectory, baseFileName + ".table1"); string csvOutputFileFinishedPath = Path.Combine(this.dtrTracesDirectory, baseFileName + ".dtr"); string tableOutputFileFinishedPath = Path.Combine(this.dtrTracesDirectory, baseFileName + ".table"); csvOutputFilePath = Path.Combine(this.dtrTracesDirectory, csvOutputFilePath); TraceFileEventReaderFactory traceFileEventReaderFactory = new TraceFileEventReaderFactory(); try { using (ITraceFileEventReader eventReader = traceFileEventReaderFactory.CreateTraceFileEventReader(lttTracesDirectory)) { try { TraceSessionMetadata metadata = eventReader.ReadTraceSessionMetadata(); this.traceSource.WriteInfo( this.logSourceId, $"Total Number of events lost : {metadata.EventsLostCount}"); } catch (InvalidDataException e) { this.traceSource.WriteError( this.logSourceId, e.Message); return(LttngTraceFolderEventReader.ConvertToUnixEpoch( DateTime.FromFileTimeUtc(this.lastEventReadTimestampFileTime))); } // reading event by event using libbabeltrace API and writing it to CSV file // The exception handling is separate from the above to prevent the creation of files // when an exception happens in the initialization using (this.fileStreamDtr = new StreamWriter(csvOutputFilePath)) using (this.fileStreamTable = (this.createTableFiles ? new StreamWriter(tableOutputFilePath) : null)) { eventReader.EventRead += this.DeliverEventToSinks; eventReader.EventRead += this.WriteEventToCsvAndTableFiles; try { eventReader.ReadEvents( LttngTraceFolderEventReader.ConvertFromUnixEpoch(timestampUnixEpochNanoSec), LttngTraceFolderEventReader.ConvertFromUnixEpoch((ulong)LTTngTraceTimestampFlag.LAST_TRACE_TIMESTAMP)); } catch (Exception e) { this.traceSource.WriteError( this.logSourceId, e.Message); } } if (this.dataCorruptionStatistics > 0) { this.traceSource.WriteError( this.logSourceId, $"Failed to decode ${this.dataCorruptionStatistics} events. " + "This failure may indicate corrupted trace data or an mismatch in the manifest file used to decode events."); } } } catch (DirectoryNotFoundException e) { // TODO - make this a warning this.traceSource.WriteExceptionAsWarning(this.logSourceId, e, "Error processing traces."); return(timestampUnixEpochNanoSec); } // renaming files to their final names after finishing processing. var filePairs = new [] { new { Temp = csvOutputFilePath, Finished = csvOutputFileFinishedPath }, new { Temp = tableOutputFilePath, Finished = tableOutputFileFinishedPath } }; foreach (var fp in filePairs) { try { if (File.Exists(fp.Temp)) { File.Move(fp.Temp, fp.Finished); } } catch (Exception e) { this.traceSource.WriteError( this.logSourceId, "Exception encountered while moving trace and table files ({0} and {1}). {2}", fp.Temp, fp.Finished, e); } } this.traceSource.WriteInfo( this.logSourceId, "Finished to process Ltt traces at: {0} - finished at timestamp: {1} - {2} events read", lttTracesDirectory, LttngTraceFolderEventReader.ConvertFromUnixEpoch(timestampUnixEpochNanoSec).ToString("o"), this.numEventsRead); return(LttngTraceFolderEventReader.ConvertToUnixEpoch( DateTime.FromFileTimeUtc(this.lastEventReadTimestampFileTime))); }