View Javadoc

1   package org.apache.helix.healthcheck;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Date;
23  
24  import org.apache.helix.ZNRecord;
25  import org.apache.log4j.Logger;
26  
27  
28  @Deprecated
29  public class DefaultPerfCounters extends ZNRecord
30  {
31    private static final Logger _logger = Logger
32        .getLogger(DefaultPerfCounters.class);
33  
34    public final static String _availableCPUs = "availableCPUs";
35    public final static String _freePhysicalMemory = "freePhysicalMemory";
36    public final static String _totalJvmMemory = "totalJvmMemory";
37    public final static String _freeJvmMemory = "freeJvmMemory";
38    public final static String _averageSystemLoad = "averageSystemLoad";
39  
40    public DefaultPerfCounters(String instanceName, long availableCPUs,
41        long freePhysicalMemory, long freeJvmMemory, long totalJvmMemory,
42        double averageSystemLoad)
43    {
44      super("DefaultPerfCounters");
45      setSimpleField("instanceName", instanceName);
46      setSimpleField("createTime", new Date().toString());
47  
48      setSimpleField(_availableCPUs, "" + availableCPUs);
49      setSimpleField(_freePhysicalMemory, "" + freePhysicalMemory);
50      setSimpleField(_freeJvmMemory, "" + freeJvmMemory);
51      setSimpleField(_totalJvmMemory, "" + totalJvmMemory);
52      setSimpleField(_averageSystemLoad, "" + averageSystemLoad);
53    }
54  
55    public long getAvailableCpus()
56    {
57      return getSimpleLongVal(_availableCPUs);
58    }
59  
60    public double getAverageSystemLoad()
61    {
62      return getSimpleDoubleVal(_averageSystemLoad);
63    }
64  
65    public long getTotalJvmMemory()
66    {
67      return getSimpleLongVal(_totalJvmMemory);
68    }
69  
70    public long getFreeJvmMemory()
71    {
72      return getSimpleLongVal(_freeJvmMemory);
73    }
74  
75    public long getFreePhysicalMemory()
76    {
77      return getSimpleLongVal(_freePhysicalMemory);
78    }
79  
80    long getSimpleLongVal(String key)
81    {
82      String strVal = getSimpleField(key);
83      if (strVal == null)
84      {
85        return 0;
86      }
87      try
88      {
89        return Long.parseLong(strVal);
90      }
91      catch (Exception e)
92      {
93        _logger.warn(e);
94        return 0;
95      }
96    }
97  
98    double getSimpleDoubleVal(String key)
99    {
100     String strVal = getSimpleField(key);
101     if (strVal == null)
102     {
103       return 0;
104     }
105     try
106     {
107       return Double.parseDouble(strVal);
108     }
109     catch (Exception e)
110     {
111       _logger.warn(e);
112       return 0;
113     }
114   }
115 }