WispCounterMXBeanImpl.java 5.4 KB
Newer Older
Y
yunyao.zxl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
/*
 * Copyright (c) 2020 Alibaba Group Holding Limited. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation. Alibaba designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package com.alibaba.wisp.engine;

import com.alibaba.management.WispCounterMXBean;

import sun.management.Util;

import javax.management.ObjectName;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

/**
 * Implementation class for WispCounterMXBean.
 */
public class WispCounterMXBeanImpl implements WispCounterMXBean {

    private final static String WISP_COUNTER_MXBEAN_NAME = "com.alibaba.management:type=WispCounter";

    private final static Map<Long, WispCounter> managedEngineCounters;

    static {
        if (!WispEngine.transparentWispSwitch()) {
            managedEngineCounters = new HashMap<>();
        } else {
            managedEngineCounters = new ConcurrentHashMap<>(100);
        }
    }

    @Override
    public List<Boolean> getRunningStates() {
        return aggregate(WispCounter::getRunningState);
    }

    @Override
    public List<Long> getSwitchCount() {
        return aggregate(WispCounter::getSwitchCount);
    }

    @Override
    public List<Long> getWaitTimeTotal() {
        return aggregate(WispCounter::getWaitTimeTotal);
    }

    @Override
    public List<Long> getRunningTimeTotal() {
        return aggregate(WispCounter::getRunningTimeTotal);
    }

    @Override
    public List<Long> getCompleteTaskCount() {
        return aggregate(WispCounter::getCompletedTaskCount);
    }

    @Override
    public List<Long> getCreateTaskCount() {
        return aggregate(WispCounter::getCreateTaskCount);
    }

    @Override
    public List<Long> getParkCount() {
        return aggregate(WispCounter::getParkCount);
    }

    @Override
    public List<Long> getUnparkCount() {
        return aggregate(WispCounter::getUnparkCount);
    }

    @Override
    public List<Long> getLazyUnparkCount() {
        return aggregate(w -> 0L);
    }

    @Override
    public List<Long> getUnparkInterruptSelectorCount() {
        return aggregate(WispCounter::getUnparkInterruptSelectorCount);
    }

    @Override
    public List<Long> getSelectableIOCount() {
        return aggregate(WispCounter::getSelectableIOCount);
    }

    @Override
    public List<Long> getTimeOutCount() {
        return aggregate(WispCounter::getTimeOutCount);
    }

    @Override
    public List<Long> getEventLoopCount() {
        return aggregate(WispCounter::getEventLoopCount);
    }

    @Override
    public List<Long> getQueueLength() {
        return aggregate(WispCounter::getCurrentTaskQueueLength);
    }

    @Override
    public List<Long> getNumberOfRunningTasks() {
        return aggregate(WispCounter::getCurrentRunningTaskCount);
    }

    @Override
    public List<Long> getTotalEnqueueTime() {
        return aggregate(WispCounter::getTotalEnqueueTime);
    }

    @Override
    public List<Long> getEnqueueCount() {
        return aggregate(WispCounter::getEnqueueCount);
    }

    @Override
    public List<Long> getTotalExecutionTime() {
        return aggregate(WispCounter::getTotalExecutionTime);
    }

    @Override
    public List<Long> getExecutionCount() {
        return aggregate(WispCounter::getExecutionCount);
    }

    @Override
    public List<Long> getTotalWaitSocketIOTime() {
        return aggregate(WispCounter::getTotalWaitSocketIOTime);
    }

    @Override
    public List<Long> getWaitSocketIOCount() {
        return aggregate(WispCounter::getWaitSocketIOCount);
    }

    @Override
    public List<Long> getTotalBlockingTime() {
        return aggregate(WispCounter::getTotalBlockingTime);
    }

    /**
     * @param id WispCarrier id
     * @return WispCounter
     */
    @Override
    public WispCounter getWispCounter(long id) {
        return WispEngine.getWispCounter(id);
    }

    private <T> List<T> aggregate(Function<WispCounter, T> getter) {
        List<T> result = new ArrayList<>(managedEngineCounters.size());
        for (Entry<Long, WispCounter> entry : managedEngineCounters.entrySet()) {
            result.add(getter.apply(entry.getValue()));
        }
        return result;
    }

    @Override
    public ObjectName getObjectName() {
        return Util.newObjectName(WISP_COUNTER_MXBEAN_NAME);
    }

    static void register(WispCounter counter) {
        managedEngineCounters.put(counter.carrier.getId(), counter);
    }

    static void deRegister(WispCounter counter) {
        managedEngineCounters.remove(counter.carrier.getId());
        counter.cleanup();
    }
}