View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.beans.stats.collectors;
12  
13  import javax.inject.Inject;
14  
15  import org.springframework.beans.factory.annotation.Value;
16  
17  import psiprobe.beans.RuntimeInfoAccessorBean;
18  import psiprobe.model.jmx.RuntimeInformation;
19  import psiprobe.tools.TimeExpression;
20  
21  /**
22   * The Class RuntimeStatsCollectorBean.
23   */
24  public class RuntimeStatsCollectorBean extends AbstractStatsCollectorBean {
25  
26    /** The runtime info accessor bean. */
27    @Inject
28    private RuntimeInfoAccessorBean runtimeInfoAccessorBean;
29  
30    /**
31     * Gets the runtime info accessor bean.
32     *
33     * @return the runtime info accessor bean
34     */
35    public RuntimeInfoAccessorBean getRuntimeInfoAccessorBean() {
36      return runtimeInfoAccessorBean;
37    }
38  
39    /**
40     * Sets the runtime info accessor bean.
41     *
42     * @param runtimeInfoAccessorBean the new runtime info accessor bean
43     */
44    public void setRuntimeInfoAccessorBean(RuntimeInfoAccessorBean runtimeInfoAccessorBean) {
45      this.runtimeInfoAccessorBean = runtimeInfoAccessorBean;
46    }
47  
48    @Override
49    public void collect() throws Exception {
50      RuntimeInformation ri = runtimeInfoAccessorBean.getRuntimeInformation();
51      if (ri != null) {
52        long time = System.currentTimeMillis();
53        buildAbsoluteStats("os.memory.committed", ri.getCommittedVirtualMemorySize() / 1024, time);
54        buildAbsoluteStats("os.memory.physical",
55            (ri.getTotalPhysicalMemorySize() - ri.getFreePhysicalMemorySize()) / 1024, time);
56        buildAbsoluteStats("os.memory.swap",
57            (ri.getTotalSwapSpaceSize() - ri.getFreeSwapSpaceSize()) / 1024, time);
58  
59        buildAbsoluteStats("os.fd.open", ri.getOpenFileDescriptorCount(), time);
60        buildAbsoluteStats("os.fd.max", ri.getMaxFileDescriptorCount(), time);
61        // convert from nanoseconds so times use the same units
62        long processCpuTimeMs = ri.getProcessCpuTime() / 1000000;
63        // divide by the number of processors to reflect shared load (<= 100%)
64        buildTimePercentageStats("os.cpu", processCpuTimeMs / ri.getAvailableProcessors(), time);
65      }
66    }
67  
68    /**
69     * Sets the max series expression.
70     *
71     * @param period the period
72     * @param span the span
73     */
74    public void setMaxSeries(@Value("${psiprobe.beans.stats.collectors.runtime.period}") long period,
75        @Value("${psiprobe.beans.stats.collectors.runtime.span}") long span) {
76      super.setMaxSeries((int) TimeExpression.dataPoints(period, span));
77    }
78  
79  }