static ByteBufferUtil() { string allocType = SystemPropertyUtil.Get("io.netty.allocator.type", "pooled"); allocType = allocType.Trim(); IByteBufferAllocator alloc; if ("unpooled".Equals(allocType, StringComparison.OrdinalIgnoreCase)) { alloc = UnpooledByteBufferAllocator.Default; Logger.Debug("-Dio.netty.allocator.type: {}", allocType); } else if ("pooled".Equals(allocType, StringComparison.OrdinalIgnoreCase)) { alloc = PooledByteBufferAllocator.Default; Logger.Debug("-Dio.netty.allocator.type: {}", allocType); } else { alloc = PooledByteBufferAllocator.Default; Logger.Debug("-Dio.netty.allocator.type: pooled (unknown: {})", allocType); } DefaultAllocator = alloc; }
static ByteBufferUtil() { Logger = InternalLoggerFactory.GetInstance(typeof(ByteBufferUtil)); string allocType = SystemPropertyUtil.Get("io.netty.allocator.type", "pooled"); allocType = allocType.Trim(); IByteBufferAllocator alloc; if ("unpooled".Equals(allocType, StringComparison.OrdinalIgnoreCase)) { alloc = UnpooledByteBufferAllocator.Default; Logger.Debug("-Dio.netty.allocator.type: {}", allocType); } else if ("pooled".Equals(allocType, StringComparison.OrdinalIgnoreCase)) { alloc = PooledByteBufferAllocator.Default; Logger.Debug("-Dio.netty.allocator.type: {}", allocType); } else if ("arraypooled".Equals(allocType, StringComparison.OrdinalIgnoreCase)) { alloc = ArrayPooledByteBufferAllocator.Default; Logger.Debug("-Dio.netty.allocator.type: {}", allocType); } else { alloc = PooledByteBufferAllocator.Default; Logger.Debug("-Dio.netty.allocator.type: pooled (unknown: {})", allocType); } DefaultAllocator = alloc; MaxBytesPerCharUtf8 = Encoding.UTF8.GetMaxByteCount(1); AsciiByteProcessor = new FindNonAscii(); }
static DefaultChannelId() { int processId = -1; string customProcessId = SystemPropertyUtil.Get("io.netty.processId"); if (customProcessId is object) { if (!int.TryParse(customProcessId, out processId)) { processId = -1; } if (processId < 0 || processId > MaxProcessId) { processId = -1; Logger.Warn("-Dio.netty.processId: {} (malformed)", customProcessId); } else if (Logger.DebugEnabled) { Logger.Debug("-Dio.netty.processId: {} (user-set)", processId); } } if (processId < 0) { processId = DefaultProcessId(); if (Logger.DebugEnabled) { Logger.Debug("-Dio.netty.processId: {} (auto-detected)", processId); } } ProcessId = processId; byte[] machineId = null; string customMachineId = SystemPropertyUtil.Get("io.netty.machineId"); if (customMachineId is object) { if (MachineIdPattern.Match(customMachineId).Success) { machineId = ParseMachineId(customMachineId); if (Logger.DebugEnabled) { Logger.Debug("-Dio.netty.machineId: {} (user-set)", customMachineId); } } else { Logger.Warn("-Dio.netty.machineId: {} (malformed)", customMachineId); } } if (machineId is null) { machineId = DefaultMachineId(); if (Logger.DebugEnabled) { Logger.Debug("-Dio.netty.machineId: {} (auto-detected)", MacAddressUtil.FormatAddress(machineId)); } } MachineId = machineId; }
static AbstractByteBuffer() { CheckAccessible = SystemPropertyUtil.GetBoolean(PropMode, true); if (Logger.DebugEnabled) { Logger.Debug("-D{}: {}", PropMode, CheckAccessible); } }
static AdvancedLeakAwareByteBuffer() { AcquireAndReleaseOnly = SystemPropertyUtil.GetBoolean(PropAcquireAndReleaseOnly, false); if (Logger.DebugEnabled) { Logger.Debug("-D{}: {}", PropAcquireAndReleaseOnly, AcquireAndReleaseOnly); } }
static PooledByteBufferAllocator() { int defaultPageSize = SystemPropertyUtil.GetInt("io.netty.allocator.pageSize", 8192); Exception pageSizeFallbackCause = null; try { ValidateAndCalculatePageShifts(defaultPageSize); } catch (Exception t) { pageSizeFallbackCause = t; defaultPageSize = 8192; } DefaultPageSize = defaultPageSize; int defaultMaxOrder = SystemPropertyUtil.GetInt("io.netty.allocator.maxOrder", 11); Exception maxOrderFallbackCause = null; try { ValidateAndCalculateChunkSize(DefaultPageSize, defaultMaxOrder); } catch (Exception t) { maxOrderFallbackCause = t; defaultMaxOrder = 11; } DefaultMaxOrder = defaultMaxOrder; // todo: Determine reasonable default for heapArenaCount // Assuming each arena has 3 chunks, the pool should not consume more than 50% of max memory. // Use 2 * cores by default to reduce contention as we use 2 * cores for the number of EventLoops // in NIO and EPOLL as well. If we choose a smaller number we will run into hotspots as allocation and // deallocation needs to be synchronized on the PoolArena. // See https://github.com/netty/netty/issues/3888 int defaultMinNumArena = Environment.ProcessorCount * 2; DefaultNumHeapArena = Math.Max(0, SystemPropertyUtil.GetInt("io.netty.allocator.numHeapArenas", defaultMinNumArena)); DefaultNumDirectArena = Math.Max(0, SystemPropertyUtil.GetInt("io.netty.allocator.numDirectArenas", defaultMinNumArena)); // cache sizes DefaultTinyCacheSize = SystemPropertyUtil.GetInt("io.netty.allocator.tinyCacheSize", 512); DefaultSmallCacheSize = SystemPropertyUtil.GetInt("io.netty.allocator.smallCacheSize", 256); DefaultNormalCacheSize = SystemPropertyUtil.GetInt("io.netty.allocator.normalCacheSize", 64); // 32 kb is the default maximum capacity of the cached buffer. Similar to what is explained in // 'Scalable memory allocation using jemalloc' DefaultMaxCachedBufferCapacity = SystemPropertyUtil.GetInt("io.netty.allocator.maxCachedBufferCapacity", 32 * 1024); // the number of threshold of allocations when cached entries will be freed up if not frequently used DefaultCacheTrimInterval = SystemPropertyUtil.GetInt( "io.netty.allocator.cacheTrimInterval", 8192); Default = new PooledByteBufferAllocator(PlatformDependent.DirectBufferPreferred); }
static ThreadDeathWatcher() { string poolName = "threadDeathWatcher"; string serviceThreadPrefix = SystemPropertyUtil.Get("io.netty.serviceThreadPrefix"); if (!string.IsNullOrEmpty(serviceThreadPrefix)) { poolName = serviceThreadPrefix + poolName; } }
static AbstractByteBuffer() { if (SystemPropertyUtil.Contains(PropCheckAccessible)) { CheckAccessible = SystemPropertyUtil.GetBoolean(PropCheckAccessible, true); } else { CheckAccessible = SystemPropertyUtil.GetBoolean(LegacyPropCheckAccessible, true); } CheckBounds = SystemPropertyUtil.GetBoolean(PropCheckBounds, true); if (Logger.DebugEnabled) { Logger.Debug("-D{}: {}", PropCheckAccessible, CheckAccessible); Logger.Debug("-D{}: {}", PropCheckBounds, CheckBounds); } }
static ResourceLeakDetector() { // If new property name is present, use it string levelStr = SystemPropertyUtil.Get(PropLevel, DefaultLevel.ToString()); if (!Enum.TryParse(levelStr, true, out DetectionLevel level)) { level = DefaultLevel; } TargetRecords = SystemPropertyUtil.GetInt(PropTargetRecords, DefaultTargetRecords); Level = level; if (Logger.DebugEnabled) { Logger.Debug("-D{}: {}", PropLevel, level.ToString().ToLower()); Logger.Debug("-D{}: {}", PropTargetRecords, TargetRecords); } }
static ResourceLeakDetector() { Logger = InternalLoggerFactory.GetInstance <ResourceLeakDetector>(); bool disabled = false; if (SystemPropertyUtil.Get("io.netty.noResourceLeakDetection") is object) { disabled = SystemPropertyUtil.GetBoolean("io.netty.noResourceLeakDetection", false); if (Logger.DebugEnabled) { Logger.Debug("-Dio.netty.noResourceLeakDetection: {}", disabled); } Logger.Warn( "-Dio.netty.noResourceLeakDetection is deprecated. Use '-D{}={}' instead.", PropLevel, DefaultLevel.ToString().ToLowerInvariant()); } var defaultLevel = disabled ? DetectionLevel.Disabled : DefaultLevel; // If new property name is present, use it string levelStr = SystemPropertyUtil.Get(PropLevel, defaultLevel.ToString()); if (!Enum.TryParse(levelStr, true, out DetectionLevel level)) { level = defaultLevel; } s_targetRecords = SystemPropertyUtil.GetInt(PropTargetRecords, DefaultTargetRecords); s_samplingInterval = SystemPropertyUtil.GetInt(PropSamplingInterval, DefaultSamplingInterval); Level = level; if (Logger.DebugEnabled) { Logger.Debug("-D{}: {}", PropLevel, level.ToString().ToLower()); Logger.Debug("-D{}: {}", PropTargetRecords, s_targetRecords); } }
static FABChannelId() { int processId = -1; string customProcessId = SystemPropertyUtil.Get("io.netty.processId"); if (customProcessId != null) { if (!int.TryParse(customProcessId, out processId)) { processId = -1; } if (processId < 0 || processId > MaxProcessId) { processId = -1; } } if (processId < 0) { processId = DefaultProcessId(); } ProcessId = processId; byte[] machineId = null; string customMachineId = SystemPropertyUtil.Get("io.netty.machineId"); if (customMachineId != null) { if (MachineIdPattern.Match(customMachineId).Success) { machineId = ParseMachineId(customMachineId); } } if (machineId == null) { machineId = DefaultMachineId(); } MachineId = machineId; }
static StringUtil() { EmptyString = string.Empty; Newline = SystemPropertyUtil.Get("line.separator", Environment.NewLine); Byte2HexPad = new string[256]; Byte2HexNopad = new string[256]; // Generate the lookup table that converts a byte into a 2-digit hexadecimal integer. int i; for (i = 0; i < 10; i++) { var buf = new StringBuilder(2); _ = buf.Append('0'); _ = buf.Append(i); Byte2HexPad[i] = buf.ToString(); Byte2HexNopad[i] = (i).ToString(); } for (; i < 16; i++) { var buf = new StringBuilder(2); char c = (char)('A' + i - 10); _ = buf.Append('0'); _ = buf.Append(c); Byte2HexPad[i] = buf.ToString(); Byte2HexNopad[i] = c.ToString(); /* String.valueOf(c);*/ } for (; i < Byte2HexPad.Length; i++) { var buf = new StringBuilder(2); _ = buf.Append(i.ToString("X") /*Integer.toHexString(i)*/); string str = buf.ToString(); Byte2HexPad[i] = str; Byte2HexNopad[i] = str; } }
static ByteBufferUtil() { char[] digits = "0123456789abcdef".ToCharArray(); for (int i = 0; i < 256; i++) { HexdumpTable[i << 1] = digits[(int)((uint)i >> 4 & 0x0F)]; HexdumpTable[(i << 1) + 1] = digits[i & 0x0F]; } // Generate the lookup table for byte-to-hex-dump conversion for (int i = 0; i < Byte2Hex.Length; i++) { Byte2Hex[i] = ' ' + StringUtil.ByteToHexStringPadded(i); } // Generate the lookup table for hex dump paddings for (int i = 0; i < HexPadding.Length; i++) { int padding = HexPadding.Length - i; var buf = new StringBuilder(padding * 3); for (int j = 0; j < padding; j++) { buf.Append(" "); } HexPadding[i] = buf.ToString(); } // Generate the lookup table for byte dump paddings for (int i = 0; i < BytePadding.Length; i++) { int padding = BytePadding.Length - i; var buf = new StringBuilder(padding); for (int j = 0; j < padding; j++) { buf.Append(' '); } BytePadding[i] = buf.ToString(); } // Generate the lookup table for byte-to-char conversion for (int i = 0; i < Byte2Char.Length; i++) { if (i <= 0x1f || i >= 0x7f) { Byte2Char[i] = '.'; } else { Byte2Char[i] = (char)i; } } // Generate the lookup table for the start-offset header in each row (up to 64KiB). for (int i = 0; i < HexDumpRowPrefixes.Length; i++) { var buf = new StringBuilder(12); buf.Append(Environment.NewLine); buf.Append((i << 4 & 0xFFFFFFFFL | 0x100000000L).ToString("X2")); buf.Insert(buf.Length - 9, '|'); buf.Append('|'); HexDumpRowPrefixes[i] = buf.ToString(); } string allocType = SystemPropertyUtil.Get( "io.netty.allocator.type", "pooled"); allocType = allocType.Trim(); IByteBufferAllocator alloc; if ("unpooled".Equals(allocType, StringComparison.OrdinalIgnoreCase)) { alloc = UnpooledByteBufferAllocator.Default; Logger.Debug("-Dio.netty.allocator.type: {}", allocType); } else if ("pooled".Equals(allocType, StringComparison.OrdinalIgnoreCase)) { alloc = PooledByteBufferAllocator.Default; Logger.Debug("-Dio.netty.allocator.type: {}", allocType); } else { alloc = PooledByteBufferAllocator.Default; Logger.Debug("-Dio.netty.allocator.type: pooled (unknown: {})", allocType); } DefaultAllocator = alloc; }
static PooledByteBufferAllocator() { int defaultPageSize = SystemPropertyUtil.GetInt("io.netty.allocator.pageSize", 8192); Exception pageSizeFallbackCause = null; try { ValidateAndCalculatePageShifts(defaultPageSize); } catch (Exception t) { pageSizeFallbackCause = t; defaultPageSize = 8192; } DEFAULT_PAGE_SIZE = defaultPageSize; int defaultMaxOrder = SystemPropertyUtil.GetInt("io.netty.allocator.maxOrder", 11); Exception maxOrderFallbackCause = null; try { ValidateAndCalculateChunkSize(DEFAULT_PAGE_SIZE, defaultMaxOrder); } catch (Exception t) { maxOrderFallbackCause = t; defaultMaxOrder = 11; } DEFAULT_MAX_ORDER = defaultMaxOrder; // Determine reasonable default for nHeapArena and nDirectArena. // Assuming each arena has 3 chunks, the pool should not consume more than 50% of max memory. // Use 2 * cores by default to reduce contention as we use 2 * cores for the number of EventLoops // in NIO and EPOLL as well. If we choose a smaller number we will run into hotspots as allocation and // deallocation needs to be synchronized on the PoolArena. // See https://github.com/netty/netty/issues/3888 int defaultMinNumArena = Environment.ProcessorCount * 2; int defaultChunkSize = DEFAULT_PAGE_SIZE << DEFAULT_MAX_ORDER; DEFAULT_NUM_HEAP_ARENA = Math.Max(0, SystemPropertyUtil.GetInt("dotNetty.allocator.numHeapArenas", defaultMinNumArena)); // cache sizes DEFAULT_TINY_CACHE_SIZE = SystemPropertyUtil.GetInt("io.netty.allocator.tinyCacheSize", 512); DEFAULT_SMALL_CACHE_SIZE = SystemPropertyUtil.GetInt("io.netty.allocator.smallCacheSize", 256); DEFAULT_NORMAL_CACHE_SIZE = SystemPropertyUtil.GetInt("io.netty.allocator.normalCacheSize", 64); // 32 kb is the default maximum capacity of the cached buffer. Similar to what is explained in // 'Scalable memory allocation using jemalloc' DEFAULT_MAX_CACHED_BUFFER_CAPACITY = SystemPropertyUtil.GetInt("io.netty.allocator.maxCachedBufferCapacity", 32 * 1024); // the number of threshold of allocations when cached entries will be freed up if not frequently used DEFAULT_CACHE_TRIM_INTERVAL = SystemPropertyUtil.GetInt( "io.netty.allocator.cacheTrimInterval", 8192); if (Logger.DebugEnabled) { Logger.Debug("-Dio.netty.allocator.numHeapArenas: {}", DEFAULT_NUM_HEAP_ARENA); if (pageSizeFallbackCause == null) { Logger.Debug("-Dio.netty.allocator.pageSize: {}", DEFAULT_PAGE_SIZE); } else { Logger.Debug("-Dio.netty.allocator.pageSize: {}", DEFAULT_PAGE_SIZE, pageSizeFallbackCause); } if (maxOrderFallbackCause == null) { Logger.Debug("-Dio.netty.allocator.maxOrder: {}", DEFAULT_MAX_ORDER); } else { Logger.Debug("-Dio.netty.allocator.maxOrder: {}", DEFAULT_MAX_ORDER, maxOrderFallbackCause); } Logger.Debug("-Dio.netty.allocator.chunkSize: {}", DEFAULT_PAGE_SIZE << DEFAULT_MAX_ORDER); Logger.Debug("-Dio.netty.allocator.tinyCacheSize: {}", DEFAULT_TINY_CACHE_SIZE); Logger.Debug("-Dio.netty.allocator.smallCacheSize: {}", DEFAULT_SMALL_CACHE_SIZE); Logger.Debug("-Dio.netty.allocator.normalCacheSize: {}", DEFAULT_NORMAL_CACHE_SIZE); Logger.Debug("-Dio.netty.allocator.maxCachedBufferCapacity: {}", DEFAULT_MAX_CACHED_BUFFER_CAPACITY); Logger.Debug("-Dio.netty.allocator.cacheTrimInterval: {}", DEFAULT_CACHE_TRIM_INTERVAL); } Default = new PooledByteBufferAllocator(); }
static AdvancedLeakAwareByteBuffer() { AcquireAndReleaseOnly = SystemPropertyUtil.GetBoolean(PropAcquireAndReleaseOnly, false); }
static AbstractByteBuffer() { CheckAccessible = SystemPropertyUtil.GetBoolean(PropMode, true); }