Storm4
2016-05-22 17:16
232 查看
package storm.scheduler; import java.lang.management.ManagementFactory; import java.lang.management.ThreadMXBean; import java.util.HashMap; import java.util.Map; import java.util.Set; import cpuinfo.CPUInfo; /** * 负载监视器 * @author wxweven * @version 1.0 * @email wxweven@qq.com * @blog http://wxweven.com * @Copyright: Copyright (c) wxweven 2009 - 2016 */ public class LoadMonitor { private static final int SECS_TO_NANOSECS = 1000000000; private static LoadMonitor instance = null; private final long cpuSpeed; // Hz Map<Long, Long> loadHistory; public static LoadMonitor getInstance() { if (instance == null) { instance = new LoadMonitor(); } return instance; } private LoadMonitor() { cpuSpeed = CPUInfo.getInstance().getCoreInfo(0).getSpeed(); } public Map<Long, Long> getLoadInfo(Set<Long> threadIds) { // get current load Map<Long, Long> currentLoadInfo = new HashMap<Long, Long>(); ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); for (long id : threadIds) { currentLoadInfo.put(id, threadBean.getThreadCpuTime(id)); } // compute difference wrt history Map<Long, Long> loadInfo = new HashMap<Long, Long>(); for (long id : threadIds) { // Long oldObj = (loadHistory != null)?loadHistory.get(id):0; // long old = (oldObj != null)?oldObj.longValue():0; long old = 0; if (loadHistory != null && loadHistory.get(id) != null) { old = loadHistory.get(id); } double deltaTime = (double)(currentLoadInfo.get(id) - old) / SECS_TO_NANOSECS; // sec loadInfo.put(id, (long)(deltaTime * cpuSpeed)); } // replace history with current loadHistory = currentLoadInfo; return loadInfo; } }
相关文章推荐
- 九度 OJ 1198:a+b
- 如何获取操作系统的语言版本
- poj 2752
- FreeMarker
- Activity向fragment传值
- Map的4种遍历方式
- Storm3
- Storm2
- Android 不让应用出现在最近使用APP列表
- Homebrew 1.0.0 发布,MacOS 上的包管理器,比如安装qt5keychain
- Storm测试
- 学习进度条——第12周
- Mybatis 快速入门之Hello World
- 视差滚动原理及实现
- 单元测试-单元测试概述
- 第103讲: 动手实战联合使用Spark Streaming、Broadcast、Accumulator实现在线黑名单过滤和计数
- C++ STL 基础及应用(2) 模板与操作符重载
- Red Hat Enterprise Linux 7.0 安装方法
- 从模板方法模式深入理解Java抽象类
- 在cocos2dx 3.x 中实现带颜色滤镜的Sprite