/*
 * Copyright 2018 Netflix, Inc.
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 */

package com.netflix.zuul.netty.server;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.netflix.config.DynamicIntProperty;
import com.netflix.spectator.api.Registry;
import com.netflix.spectator.api.patterns.PolledMeter;
import io.netty.util.internal.PlatformDependent;
import java.lang.reflect.Field;
import java.time.Duration;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * User: michaels@netflix.com
 * Date: 4/29/16
 * Time: 10:23 AM
 */
@Singleton
public final class DirectMemoryMonitor {
    private static final Logger LOG = LoggerFactory.getLogger(DirectMemoryMonitor.class);
    private static final String PROP_PREFIX = "zuul.directmemory";
    private static final DynamicIntProperty TASK_DELAY_PROP = new DynamicIntProperty(PROP_PREFIX + ".task.delay", 10);

    // TODO(carl-mastrangelo): this should be passed in as a dependency, so it can be shutdown and waited on for
    //    termination.
    private final ScheduledExecutorService service =
            Executors.newSingleThreadScheduledExecutor(
                    new ThreadFactoryBuilder().setDaemon(true).setNameFormat("dmm-%d").build());

    @Inject
    public DirectMemoryMonitor(Registry registry) {
        PolledMeter.using(registry)
                   .withName(PROP_PREFIX + ".reserved")
                   .withDelay(Duration.ofSeconds(TASK_DELAY_PROP.get()))
                   .scheduleOn(service)
                   .monitorValue(DirectMemoryMonitor.class, DirectMemoryMonitor::getReservedMemory);

        PolledMeter.using(registry)
                   .withName(PROP_PREFIX + ".max")
                   .withDelay(Duration.ofSeconds(TASK_DELAY_PROP.get()))
                   .scheduleOn(service)
                   .monitorValue(DirectMemoryMonitor.class, DirectMemoryMonitor::getMaxMemory);

    }

    private static double getReservedMemory(Object discard) {
        try {
            return PlatformDependent.maxDirectMemory();
        } catch (Throwable e) {
            LOG.warn("Error in DirectMemoryMonitor task.", e);
        }
        return -1;
    }

    private static double getMaxMemory(Object discard) {
        try {
            return PlatformDependent.usedDirectMemory();
        } catch (Throwable e) {
            LOG.warn("Error in DirectMemoryMonitor task.", e);
        }
        return -1;
    }
}

@@ -2817,19 +2817,20 @@
 pendent.
-max
+used
 DirectMe
@@ -3068,36 +3068,35 @@
 atformDependent.
-used
+max
 DirectMemory();%0A

Fix methods