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;
    	}
    }
    
  • 相关阅读:
    [HNOI2006]鬼谷子的钱袋
    一日游与两道题
    [HNOI2009]梦幻布丁
    [Ahoi2009]self 同类分布
    50 days before NOI2017
    Topcoder SRM 606 div1题解
    Topcoder SRM 605 div1 题解
    Topcoder SRM 604 div1题解
    Topcoder SRM 603 div1题解
    Topcoder SRM 602 div1题解
  • 原文地址:https://www.cnblogs.com/wxweven/p/5517191.html
Copyright © 2011-2022 走看看