Пример #1
0
        public static unsafe void ReadProfilerTrace(ref ProfilerTrace trace, Stream stream, Allocator allocator)
        {
            var basePos = stream.Position;
            var reader  = new RawReader(stream);
            ProfilerDataSerializationHeader serializationHeader = default;

            reader.Read(&serializationHeader);
            Debug.Assert(basePos + serializationHeader.TotalLength <= stream.Length);

            ProfilerTraceHeader traceHeader = default;

            reader.Read(&traceHeader);
            trace.Header = traceHeader;

            Read(out trace.Samples, serializationHeader.NumSamples, basePos + serializationHeader.SamplesOffset);
            Read(out trace.StackFrames, serializationHeader.NumStackFrames, basePos + serializationHeader.StackFramesOffset);
            Read(out trace.Functions, serializationHeader.NumFunctions, basePos + serializationHeader.FunctionsOffset);
            Read(out trace.Modules, serializationHeader.NumModules, basePos + serializationHeader.ModulesOffset);
            Read(out trace.Threads, serializationHeader.NumThreads, basePos + serializationHeader.ThreadsOffset);
            Read(out trace.BlobData, serializationHeader.BlobLength, basePos + serializationHeader.BlobOffset);

            void Read <T>(out NativeArray <T> arr, int num, long offset) where T : unmanaged
            {
                stream.Position = basePos + offset;
                arr             = new NativeArray <T>(num, allocator, NativeArrayOptions.UninitializedMemory);
                reader.ReadBytes(arr.GetUnsafePtr(), sizeof(T) * num);
            }
        }
Пример #2
0
        public static unsafe void WriteProfilerTrace(ref ProfilerTrace trace, Stream stream)
        {
            var writer = new RawWriter(stream);
            var header = new ProfilerDataSerializationHeader
            {
                Version        = ProfilerTraceVersion,
                NumFunctions   = trace.Functions.Length,
                NumModules     = trace.Modules.Length,
                NumSamples     = trace.Samples.Length,
                NumStackFrames = trace.StackFrames.Length,
                NumThreads     = trace.Threads.Length,
                BlobLength     = trace.BlobData.Length
            };
            long headerPos = stream.Position;

            // we'll write the header again later
            writer.Write(&header);

            var profTrace = trace;

            writer.Write(&profTrace.Header);

            header.SamplesOffset = stream.Position - headerPos;
            writer.WriteArray(profTrace.Samples);

            header.StackFramesOffset = stream.Position - headerPos;
            writer.WriteArray(profTrace.StackFrames);

            header.FunctionsOffset = stream.Position - headerPos;
            writer.WriteArray(profTrace.Functions);

            header.ModulesOffset = stream.Position - headerPos;
            writer.WriteArray(profTrace.Modules);

            header.ThreadsOffset = stream.Position - headerPos;
            writer.WriteArray(profTrace.Threads);

            header.BlobOffset = stream.Position - headerPos;
            writer.WriteArray(profTrace.BlobData);

            stream.Flush();
            header.TotalLength = (int)(stream.Position - headerPos);
            stream.Position    = headerPos;
            writer.Write(&header);

            stream.Flush();
        }