| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CpuUsage |
|
| 4.0;4 |
| 1 | /** | |
| 2 | * Copyright 2009 Martin Vysny. | |
| 3 | * | |
| 4 | * This file is part of WebVM. | |
| 5 | * | |
| 6 | * WebVM is free software: you can redistribute it and/or modify | |
| 7 | * it under the terms of the GNU General Public License as published by | |
| 8 | * the Free Software Foundation, either version 3 of the License, or | |
| 9 | * (at your option) any later version. | |
| 10 | * | |
| 11 | * WebVM is distributed in the hope that it will be useful, | |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | * GNU General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU General Public License | |
| 17 | * along with WebVM. If not, see <http://www.gnu.org/licenses/>. | |
| 18 | */ | |
| 19 | package sk.baka.webvm.analyzer; | |
| 20 | ||
| 21 | import java.util.logging.Level; | |
| 22 | import java.util.logging.Logger; | |
| 23 | import sk.baka.webvm.analyzer.hostos.Cpu; | |
| 24 | ||
| 25 | /** | |
| 26 | * Provides a CPU measurement support. | |
| 27 | * @author Martin Vysny | |
| 28 | */ | |
| 29 | public final class CpuUsage { | |
| 30 | ||
| 31 | 1 | private static final Logger LOG = Logger.getLogger(Cpu.class.getName()); |
| 32 | /** | |
| 33 | * The CPU usage measurer. | |
| 34 | */ | |
| 35 | private final ICpuUsageMeasure cpuUsage; | |
| 36 | ||
| 37 | /** | |
| 38 | * Creates a new CPU usage measurer. | |
| 39 | * @param usage retrieve usage data from this object. | |
| 40 | */ | |
| 41 | 11 | public CpuUsage(final ICpuUsageMeasure usage) { |
| 42 | 11 | if (usage.supported()) { |
| 43 | 11 | cpuUsage = usage; |
| 44 | } else { | |
| 45 | 0 | cpuUsage = null; |
| 46 | } | |
| 47 | 11 | } |
| 48 | ||
| 49 | /** | |
| 50 | * Checks if this measurement is supported. | |
| 51 | * @return true if supported, false if {@link #getCpuUsage()} will always return -1. | |
| 52 | */ | |
| 53 | public boolean supported() { | |
| 54 | 0 | return cpuUsage != null; |
| 55 | } | |
| 56 | ||
| 57 | /** | |
| 58 | * Returns an average CPU usage in a time slice starting at the previous call of this method. | |
| 59 | * @return average overall CPU usage or -1 if CPU sampling is unsupported or error occurred. | |
| 60 | */ | |
| 61 | public int getCpuUsage() { | |
| 62 | 8 | if (cpuUsage == null) { |
| 63 | 0 | return -1; |
| 64 | } | |
| 65 | 8 | if (cpuMeasurement == null) { |
| 66 | try { | |
| 67 | 4 | cpuMeasurement = cpuUsage.measure(); |
| 68 | 0 | } catch (Exception ex) { |
| 69 | 0 | LOG.log(Level.SEVERE, "Failed to measure a CPU usage", ex); |
| 70 | 0 | return -1; |
| 71 | 4 | } |
| 72 | 4 | return 0; |
| 73 | } | |
| 74 | final Object newMeasurement; | |
| 75 | try { | |
| 76 | 4 | newMeasurement = cpuUsage.measure(); |
| 77 | 0 | } catch (Exception ex) { |
| 78 | 0 | LOG.log(Level.SEVERE, "Failed to measure a CPU usage", ex); |
| 79 | 0 | return -1; |
| 80 | 4 | } |
| 81 | 4 | final int result = cpuUsage.getAvgCpuUsage(cpuMeasurement, newMeasurement); |
| 82 | 4 | cpuMeasurement = newMeasurement; |
| 83 | 4 | return result; |
| 84 | } | |
| 85 | 11 | private Object cpuMeasurement = null; |
| 86 | } |