зеркало из https://github.com/microsoft/BPerf.git
Apply dn formatter & signed unsigned type changes
This commit is contained in:
Родитель
8e372f2343
Коммит
ba8efe9bd0
|
@ -8,17 +8,17 @@ namespace BPerfCPUSamplesCollector
|
|||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal readonly struct BPFCreateMapAttr
|
||||
{
|
||||
private readonly int mapType;
|
||||
private readonly uint mapType;
|
||||
|
||||
private readonly int keySize;
|
||||
private readonly uint keySize;
|
||||
|
||||
private readonly int valueSize;
|
||||
private readonly uint valueSize;
|
||||
|
||||
private readonly int maxEntries;
|
||||
private readonly uint maxEntries;
|
||||
|
||||
private readonly int mapFlags;
|
||||
private readonly uint mapFlags;
|
||||
|
||||
public BPFCreateMapAttr(int mapType, int keySize, int valueSize, int maxEntries, int mapFlags)
|
||||
public BPFCreateMapAttr(uint mapType, uint keySize, uint valueSize, uint maxEntries, uint mapFlags)
|
||||
{
|
||||
this.mapType = mapType;
|
||||
this.keySize = keySize;
|
||||
|
|
|
@ -8,7 +8,7 @@ namespace BPerfCPUSamplesCollector
|
|||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal readonly struct BPFObjAttr
|
||||
{
|
||||
private readonly unsafe void* pathName;
|
||||
private readonly ulong pathName;
|
||||
|
||||
private readonly int fileDescriptor;
|
||||
|
||||
|
@ -16,7 +16,7 @@ namespace BPerfCPUSamplesCollector
|
|||
|
||||
public unsafe BPFObjAttr(void* pathName, int fd, int flags)
|
||||
{
|
||||
this.pathName = pathName;
|
||||
this.pathName = (ulong)pathName;
|
||||
this.fileDescriptor = fd;
|
||||
this.fileFlags = flags;
|
||||
}
|
||||
|
|
|
@ -5,35 +5,55 @@ namespace BPerfCPUSamplesCollector
|
|||
{
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
internal readonly struct BPFProgLoadAttr
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
private readonly BPFProgType progType;
|
||||
|
||||
[FieldOffset(4)]
|
||||
private readonly int instructionCount;
|
||||
|
||||
private readonly unsafe void* instructions;
|
||||
[FieldOffset(8)]
|
||||
private readonly ulong instructions;
|
||||
|
||||
private readonly unsafe void* license;
|
||||
[FieldOffset(16)]
|
||||
private readonly ulong license;
|
||||
|
||||
[FieldOffset(24)]
|
||||
private readonly int logLevel;
|
||||
|
||||
[FieldOffset(28)]
|
||||
private readonly int logSize;
|
||||
|
||||
private readonly unsafe void* logBuffer;
|
||||
[FieldOffset(32)]
|
||||
private readonly ulong logBuffer;
|
||||
|
||||
[FieldOffset(40)]
|
||||
private readonly int kernVersion; /* Checked when ProgType=KProbe */
|
||||
|
||||
[FieldOffset(44)]
|
||||
private readonly int progFlags;
|
||||
|
||||
[FieldOffset(48)]
|
||||
private readonly ulong progName1;
|
||||
|
||||
[FieldOffset(56)]
|
||||
private readonly ulong progName2;
|
||||
|
||||
public unsafe BPFProgLoadAttr(BPFProgType progType, int instructionCount, void* instructions, void* license, int logLevel, int logSize, void* logBuffer, int kernVersion)
|
||||
{
|
||||
this.progType = progType;
|
||||
this.instructionCount = instructionCount;
|
||||
this.instructions = instructions;
|
||||
this.license = license;
|
||||
this.instructions = (ulong)instructions;
|
||||
this.license = (ulong)license;
|
||||
this.logLevel = logLevel;
|
||||
this.logSize = logSize;
|
||||
this.logBuffer = logBuffer;
|
||||
this.logBuffer = (ulong)logBuffer;
|
||||
this.kernVersion = kernVersion;
|
||||
this.progFlags = 0;
|
||||
this.progName1 = 0;
|
||||
this.progName2 = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,22 +5,26 @@ namespace BPerfCPUSamplesCollector
|
|||
{
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
internal readonly struct BPFUpdateMapAttr
|
||||
{
|
||||
private readonly long mapFD;
|
||||
[FieldOffset(0)]
|
||||
private readonly int mapfd;
|
||||
|
||||
private readonly unsafe void* key;
|
||||
[FieldOffset(8)]
|
||||
private readonly ulong key;
|
||||
|
||||
private readonly unsafe void* value;
|
||||
[FieldOffset(16)]
|
||||
private readonly ulong value;
|
||||
|
||||
private readonly long flags;
|
||||
[FieldOffset(24)]
|
||||
private readonly ulong flags;
|
||||
|
||||
public unsafe BPFUpdateMapAttr(long mapFD, void* key, void* value, long flags)
|
||||
public unsafe BPFUpdateMapAttr(int mapfd, void* key, void* value, ulong flags)
|
||||
{
|
||||
this.mapFD = mapFD;
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
this.mapfd = mapfd;
|
||||
this.key = (ulong)key;
|
||||
this.value = (ulong)value;
|
||||
this.flags = flags;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<LangVersion>preview</LangVersion>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<CodeAnalysisRuleSet>..\default.ruleset</CodeAnalysisRuleSet>
|
||||
|
@ -67,6 +68,10 @@
|
|||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="3.8.0-2.final" />
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="InvokeBuildTools" BeforeTargets="CoreCompile">
|
||||
<Exec WorkingDirectory="$(MSBuildThisFileDirectory)../tools" Command="dotnet run $(GenStringMapExePath) $(MSBuildThisFileDirectory)Strings.resx $(MSBuildThisFileDirectory)\StringMap.cs BPerfCPUSamplesCollector Strings" />
|
||||
</Target>
|
||||
|
|
|
@ -15,8 +15,6 @@ namespace BPerfCPUSamplesCollector
|
|||
|
||||
internal struct ELFHeader
|
||||
{
|
||||
public static ReadOnlySpan<byte> ElfMagic => new byte[] { 0x7F, (byte)'E', (byte)'L', (byte)'F', (byte)'\0' };
|
||||
|
||||
public unsafe fixed char Identity[16]; /* ELF identification */
|
||||
public ELFHeaderType Type; // e_type; /* Object file type */
|
||||
public ushort Machine; // e_machine; /* Machine type */
|
||||
|
@ -31,5 +29,7 @@ namespace BPerfCPUSamplesCollector
|
|||
public ushort SectionHeaderEntrySize; // e_shentsize; /* Size of section header entry */
|
||||
public ushort SectionHeaderCount; // e_shnum; /* Number of section header entries */
|
||||
public ushort SectionHeaderStringIndex; // e_shstrndx; /* Section name string table index */
|
||||
|
||||
public static ReadOnlySpan<byte> ElfMagic => new byte[] { 0x7F, (byte)'E', (byte)'L', (byte)'F', (byte)'\0' };
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ namespace BPerfCPUSamplesCollector
|
|||
|
||||
private const int PERF_MAX_STACK_DEPTH = 127;
|
||||
|
||||
private const long PERF_EVENT_IOC_SET_BPF = 0x40042408;
|
||||
private const nint PERF_EVENT_IOC_SET_BPF = 0x40042408;
|
||||
|
||||
public static unsafe int BPFProgLoad(ReadOnlySpan<byte> instructions, ReadOnlySpan<byte> logBuf, in byte license)
|
||||
{
|
||||
|
@ -21,7 +21,7 @@ namespace BPerfCPUSamplesCollector
|
|||
return BPF(BPFCmd.BPF_PROG_LOAD, in attr);
|
||||
}
|
||||
|
||||
public static int PerfEventOpen(in PerfEventAttr attr, long cpu, long pid = -1)
|
||||
public static int PerfEventOpen(in PerfEventAttr attr, nint cpu, nint pid = -1)
|
||||
{
|
||||
var syscallNumber = RuntimeInformation.OSArchitecture switch
|
||||
{
|
||||
|
@ -31,7 +31,7 @@ namespace BPerfCPUSamplesCollector
|
|||
_ => throw new NotSupportedException()
|
||||
};
|
||||
|
||||
const long PERF_FLAG_FD_CLOEXEC = 8;
|
||||
const int PERF_FLAG_FD_CLOEXEC = 8;
|
||||
var fd = (int)SysCallPerfEventOpen(syscallNumber, in attr, pid, cpu, -1, PERF_FLAG_FD_CLOEXEC);
|
||||
if (fd == -1)
|
||||
{
|
||||
|
@ -53,7 +53,13 @@ namespace BPerfCPUSamplesCollector
|
|||
|
||||
public static bool SetMaxResourceLimit()
|
||||
{
|
||||
var r = new RLimit(ulong.MaxValue, ulong.MaxValue);
|
||||
nuint maxValue;
|
||||
unchecked
|
||||
{
|
||||
maxValue = IntPtr.Size == 8 ? (nuint)ulong.MaxValue : (nuint)uint.MaxValue;
|
||||
}
|
||||
|
||||
var r = new RLimit(maxValue, maxValue);
|
||||
|
||||
const int RLIMIT_MEMLOCK = 8;
|
||||
if (SetResourceLimit(RLIMIT_MEMLOCK, in r) != 0)
|
||||
|
@ -70,21 +76,21 @@ namespace BPerfCPUSamplesCollector
|
|||
return BPF(BPFCmd.BPF_OBJ_PIN, in attr);
|
||||
}
|
||||
|
||||
public static int BPFCreatePerCPUArray(int valueSize, int maxEntries)
|
||||
public static int BPFCreatePerCPUArray(uint valueSize, uint maxEntries)
|
||||
{
|
||||
var attr = new BPFCreateMapAttr((int)BPFMapType.BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(int), valueSize, maxEntries, 0);
|
||||
return BPF(BPFCmd.BPF_MAP_CREATE, in attr);
|
||||
}
|
||||
|
||||
public static int BPFCreateStackTraceMap(int maxEntries)
|
||||
public static int BPFCreateStackTraceMap(uint maxEntries)
|
||||
{
|
||||
var attr = new BPFCreateMapAttr((int)BPFMapType.BPF_MAP_TYPE_STACK_TRACE, sizeof(int), PERF_MAX_STACK_DEPTH * IntPtr.Size /* this is the max value size possible */, maxEntries, 0);
|
||||
var attr = new BPFCreateMapAttr((int)BPFMapType.BPF_MAP_TYPE_STACK_TRACE, sizeof(int), (uint)(PERF_MAX_STACK_DEPTH * IntPtr.Size) /* this is the max value size possible */, maxEntries, 0);
|
||||
return BPF(BPFCmd.BPF_MAP_CREATE, in attr);
|
||||
}
|
||||
|
||||
public static int BPFCreatePerfEventArrayMap()
|
||||
{
|
||||
var attr = new BPFCreateMapAttr((int)BPFMapType.BPF_MAP_TYPE_PERF_EVENT_ARRAY, sizeof(int), sizeof(int), GetNumberOfConfiguredPrcoessors(), 0);
|
||||
var attr = new BPFCreateMapAttr((int)BPFMapType.BPF_MAP_TYPE_PERF_EVENT_ARRAY, sizeof(int), sizeof(int), (uint)GetNumberOfConfiguredPrcoessors(), 0);
|
||||
return BPF(BPFCmd.BPF_MAP_CREATE, in attr);
|
||||
}
|
||||
|
||||
|
@ -116,7 +122,7 @@ namespace BPerfCPUSamplesCollector
|
|||
}
|
||||
}
|
||||
|
||||
public static int GetFileSize(in byte filename)
|
||||
public static ulong GetFileSize(in byte filename)
|
||||
{
|
||||
StatX statx = default;
|
||||
var retVal = StatXSize(in statx, in filename);
|
||||
|
@ -126,7 +132,7 @@ namespace BPerfCPUSamplesCollector
|
|||
return 0;
|
||||
}
|
||||
|
||||
return (int)statx.Size;
|
||||
return statx.Size;
|
||||
}
|
||||
|
||||
public static int CloseFileDescriptor(int fd)
|
||||
|
@ -138,7 +144,7 @@ namespace BPerfCPUSamplesCollector
|
|||
public static extern int GetNumberOfConfiguredPrcoessors();
|
||||
|
||||
[DllImport("libc", EntryPoint = "getpagesize", SetLastError = true)]
|
||||
public static extern int GetMemoryPageSize();
|
||||
public static extern uint GetMemoryPageSize();
|
||||
|
||||
[DllImport("libc", EntryPoint = "open", SetLastError = true)]
|
||||
public static extern int OpenFileDescriptor(in byte filepath, int flags);
|
||||
|
@ -153,16 +159,17 @@ namespace BPerfCPUSamplesCollector
|
|||
public static extern int EPollWait(int epfd, ref EPollEvent events, int maxevents, int timeout);
|
||||
|
||||
[DllImport("libc", EntryPoint = "mmap", SetLastError = true)]
|
||||
public static extern IntPtr MemoryMap(IntPtr addr, long length, int prot, int flags, int fd, long offset);
|
||||
public static extern IntPtr MemoryMap(IntPtr addr, nuint length, int prot, int flags, int fd, nuint offset);
|
||||
|
||||
[DllImport("libc", EntryPoint = "munmap", SetLastError = true)]
|
||||
public static extern IntPtr MemoryUnmap(IntPtr addr, long length);
|
||||
public static extern IntPtr MemoryUnmap(IntPtr addr, nuint length);
|
||||
|
||||
private static int BPF<T>(BPFCmd cmd, in T attr)
|
||||
where T : unmanaged
|
||||
{
|
||||
var number = RuntimeInformation.OSArchitecture switch
|
||||
{
|
||||
Architecture.Arm => 386,
|
||||
Architecture.Arm64 => 280,
|
||||
Architecture.X64 => 321,
|
||||
_ => throw new NotSupportedException()
|
||||
|
@ -181,6 +188,7 @@ namespace BPerfCPUSamplesCollector
|
|||
{
|
||||
var number = RuntimeInformation.OSArchitecture switch
|
||||
{
|
||||
Architecture.Arm => 364,
|
||||
Architecture.Arm64 => 291,
|
||||
Architecture.X64 => 332,
|
||||
_ => throw new NotSupportedException()
|
||||
|
@ -194,7 +202,7 @@ namespace BPerfCPUSamplesCollector
|
|||
}
|
||||
|
||||
[DllImport("libc", EntryPoint = "syscall", SetLastError = true)]
|
||||
private static extern unsafe long SysCallBPF(long number, BPFCmd cmd, void* attr, long size);
|
||||
private static extern unsafe long SysCallBPF(nint number, BPFCmd cmd, void* attr, nint size);
|
||||
|
||||
/// <remarks>
|
||||
///
|
||||
|
@ -206,13 +214,13 @@ namespace BPerfCPUSamplesCollector
|
|||
/// /proc/sys/kernel/perf_event_mlock_kb 516
|
||||
/// </remarks>
|
||||
[DllImport("libc", EntryPoint = "syscall", SetLastError = true)]
|
||||
private static extern long SysCallPerfEventOpen(long number, in PerfEventAttr attr, long pid, long cpu, long groupfd, long flags);
|
||||
private static extern long SysCallPerfEventOpen(nint number, in PerfEventAttr attr, nint pid, nint cpu, nint groupfd, nint flags);
|
||||
|
||||
[DllImport("libc", EntryPoint = "syscall", SetLastError = true)]
|
||||
private static extern long SysCallStatX(long number, long dfd, in byte filename, ulong flags, uint mask, in StatX statx);
|
||||
private static extern long SysCallStatX(nint number, nint dfd, in byte filename, nint flags, nuint mask, in StatX statx);
|
||||
|
||||
[DllImport("libc", EntryPoint = "ioctl", SetLastError = true)]
|
||||
private static extern int IOControl(int fd, long value, long flags);
|
||||
private static extern int IOControl(int fd, nint value, nint flags);
|
||||
|
||||
[DllImport("libc", EntryPoint = "setrlimit", SetLastError = true)]
|
||||
private static extern int SetResourceLimit(int resource, in RLimit r);
|
||||
|
|
|
@ -11,7 +11,7 @@ namespace BPerfCPUSamplesCollector
|
|||
|
||||
internal static class Program
|
||||
{
|
||||
internal const int RingBufferSize = 64 * 1024;
|
||||
internal const uint RingBufferSize = 64 * 1024;
|
||||
|
||||
private const int EPERM = 1;
|
||||
|
||||
|
@ -128,7 +128,7 @@ namespace BPerfCPUSamplesCollector
|
|||
|
||||
for (var i = 0; i < numberOfReadyFileDescriptors; ++i)
|
||||
{
|
||||
var ptr = ringBuffers[events[i].Index].Address;
|
||||
IntPtr ptr = ringBuffers[events[i].Index].Address;
|
||||
ProcessRingBuffer(ptr, 4096);
|
||||
}
|
||||
}
|
||||
|
@ -138,9 +138,9 @@ namespace BPerfCPUSamplesCollector
|
|||
|
||||
private static unsafe void ProcessRingBuffer(IntPtr data, int length)
|
||||
{
|
||||
var copyMem = stackalloc byte[64 * 1024];
|
||||
byte* copyMem = stackalloc byte[64 * 1024];
|
||||
|
||||
ref var mmapPage = ref MemoryMarshal.AsRef<PerfEventMMapPage>(new Span<byte>((void*)data, length));
|
||||
ref PerfEventMMapPage mmapPage = ref MemoryMarshal.AsRef<PerfEventMMapPage>(new Span<byte>((void*)data, length));
|
||||
|
||||
var dataHead = Volatile.Read(ref mmapPage.DataHead);
|
||||
|
||||
|
@ -186,7 +186,7 @@ namespace BPerfCPUSamplesCollector
|
|||
|
||||
ptr += sizeof(int);
|
||||
|
||||
ref readonly var bperfevent = ref MemoryMarshal.AsRef<BPerfEvent>(new ReadOnlySpan<byte>(ptr, rawSize));
|
||||
ref readonly BPerfEvent bperfevent = ref MemoryMarshal.AsRef<BPerfEvent>(new ReadOnlySpan<byte>(ptr, rawSize));
|
||||
|
||||
Console.WriteLine($"Type: {data->Type}, Size: {data->Size}, Misc: {data->Misc}, Raw Size: {rawSize}");
|
||||
Console.WriteLine(bperfevent);
|
||||
|
|
|
@ -8,11 +8,11 @@ namespace BPerfCPUSamplesCollector
|
|||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal readonly struct RLimit
|
||||
{
|
||||
private readonly ulong current;
|
||||
private readonly nuint current;
|
||||
|
||||
private readonly ulong maximum;
|
||||
private readonly nuint maximum;
|
||||
|
||||
public RLimit(ulong cur, ulong max)
|
||||
public RLimit(nuint cur, nuint max)
|
||||
{
|
||||
this.current = cur;
|
||||
this.maximum = max;
|
||||
|
|
|
@ -12,7 +12,7 @@ namespace BPerfCPUSamplesCollector
|
|||
{
|
||||
private readonly IntPtr addr;
|
||||
|
||||
private readonly int size;
|
||||
private readonly uint size;
|
||||
|
||||
private readonly int fd;
|
||||
|
||||
|
@ -21,7 +21,7 @@ namespace BPerfCPUSamplesCollector
|
|||
const int O_RDONLY = 0;
|
||||
this.fd = OpenFileDescriptor(in filename, O_RDONLY);
|
||||
|
||||
this.size = GetFileSize(in filename);
|
||||
this.size = (uint)GetFileSize(in filename); // uint conversion so it plays nice with nuint
|
||||
|
||||
const int MAP_PRIVATE = 2;
|
||||
const int PROT_READ = 1;
|
||||
|
@ -30,7 +30,7 @@ namespace BPerfCPUSamplesCollector
|
|||
this.addr = MemoryMap(IntPtr.Zero, this.size, PROT_READ | PROT_WRITE, MAP_PRIVATE, this.fd, 0);
|
||||
}
|
||||
|
||||
public unsafe Span<byte> Contents => new Span<byte>((void*)this.addr, this.size);
|
||||
public unsafe Span<byte> Contents => new Span<byte>((void*)this.addr, (int)this.size); // int conversion because spans only support int.MaxValue
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
|
|
|
@ -9,11 +9,11 @@ namespace BPerfCPUSamplesCollector
|
|||
{
|
||||
public readonly IntPtr Address;
|
||||
|
||||
public readonly int Size;
|
||||
public readonly uint Size;
|
||||
|
||||
public readonly int FileDescriptor;
|
||||
|
||||
public RingBuffer(IntPtr address, int size, int fd)
|
||||
public RingBuffer(IntPtr address, uint size, int fd)
|
||||
{
|
||||
this.Address = address;
|
||||
this.Size = size;
|
||||
|
|
|
@ -834,15 +834,11 @@ HRESULT STDMETHODCALLTYPE BPerfProfilerCallback::DynamicMethodUnloaded(FunctionI
|
|||
|
||||
HRESULT STDMETHODCALLTYPE BPerfProfilerCallback::EventPipeEventDelivered(EVENTPIPE_PROVIDER provider, DWORD eventId, DWORD eventVersion, ULONG cbMetadataBlob, LPCBYTE metadataBlob, ULONG cbEventData, LPCBYTE eventData, LPCGUID pActivityId, LPCGUID pRelatedActivityId, ThreadID eventThread, ULONG numStackFrames, UINT_PTR stackFrames[])
|
||||
{
|
||||
this->corProfilerInfo->Even
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE BPerfProfilerCallback::EventPipeProviderCreated(EVENTPIPE_PROVIDER provider)
|
||||
{
|
||||
ULONG cchName;
|
||||
this->corProfilerInfo->
|
||||
IfFailRet(this->corProfilerInfo->EventPipeGetProviderInfo(provider, 0, &cchName, nullptr));
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
|
|
|
@ -296,8 +296,6 @@ private:
|
|||
std::atomic<size_t> numberOfFrozenSegments;
|
||||
|
||||
portable_wide_string reportOutOfBandMessage;
|
||||
|
||||
thread_local std::unordered_map<EVENTPIPE_PROVIDER, portable_wide_string>
|
||||
};
|
||||
|
||||
template <class TInterface>
|
||||
|
|
Загрузка…
Ссылка в новой задаче