zoukankan      html  css  js  c++  java
  • Storm3

    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
     */
    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;
    	}
    }
    
  • 相关阅读:
    js画矩形
    js加载pdf截屏生成图片调用ocr识别成文字
    C#List或者Set集合相同的key合并Value的值
    Oracle学习笔记读懂执行计划(十八)
    Java 阻塞队列
    SpringMVC(三):参数绑定、输入输出转换
    springMVC(二): @RequestBody @ResponseBody 注解实现分析
    Spring Security 4.2.3 Filters 解析
    MySQL 加锁处理分析
    Innodb semi-consistent 简介
  • 原文地址:https://www.cnblogs.com/wxweven/p/5517191.html
Copyright © 2011-2022 走看看