Пример #1
0
        public void MimallocAllocFreeCallLatency()
        {
            Mem.OptionSetEnabled(Mem.Option.EagerCommit, true);
            Mem.OptionSetEnabled(Mem.Option.LargeOsPages, true);
            Mem.OptionSetEnabled(Mem.Option.ResetDecommits, true);
            Mem.OptionSetEnabled(Mem.Option.PageReset, true);
            Mem.OptionSetEnabled(Mem.Option.SegmentReset, true);
            Mem.OptionSetEnabled(Mem.Option.AbandonedPageReset, true);
            // Mem.OptionSet(Mem.Option.ResetDelay, 0);
            Mem.OptionSetEnabled(Mem.Option.EagerRegionCommit, true);

            Mem.RegisterOutput((str, arg) => { Console.Write(str); }, null);

            var rng       = new Random();
            var allocated = 0L;

            var h = Mem.HeapNew();

            var count = 100_000L;
            var size  = 32 * 4096;

            IntPtr[] ptrs = new IntPtr[count];

            for (int r = 0; r < 4; r++)
            {
                var histogram = new HdrHistogram.LongHistogram(1, 1000000, 1);

                using (Benchmark.Run("AllocFree" + r, count))
                {
                    for (int i = 0; i < count; i++)
                    {
                        var x     = rng.NextDouble();
                        var start = Stopwatch.GetTimestamp();
                        if (allocated < 1000)
                        {
                            Allocate();
                        }
                        else if (allocated > 2000)
                        {
                            Free();
                        }
                        else if (((2000 - allocated) / 1000.0 * x) > 0.5)
                        {
                            Allocate();
                        }
                        else
                        {
                            Free();
                        }

                        var time = Stopwatch.GetTimestamp() - start;

                        histogram.RecordValue(time);

                        void Allocate()
                        {
                            ptrs[allocated] = (IntPtr)Mem.HeapMalloc(h, (UIntPtr)size);
                            for (int j = 0; j < size; j += 4096)
                            {
                                ((byte *)ptrs[allocated])[j] = 0;
                            }
                            ((byte *)ptrs[allocated])[size - 1] = 0;
                            allocated++;
                        }

                        void Free()
                        {
                            Mem.Free((byte *)ptrs[allocated - 1]);
                            allocated--;
                        }
                    }
                }

                histogram.OutputPercentileDistribution(Console.Out,
                                                       outputValueUnitScalingRatio: OutputScalingFactor.TimeStampToMicroseconds);
            }

            Mem.StatsPrint();
        }