Use forked atomix-{storage,utils}
[controller.git] / third-party / atomix / storage / src / main / java / io / atomix / storage / statistics / StorageStatistics.java
1 /*
2  * Copyright 2017-present Open Networking Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package io.atomix.storage.statistics;
17
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import java.io.File;
22 import java.lang.management.ManagementFactory;
23
24 import javax.management.MBeanServer;
25 import javax.management.ObjectName;
26
27 /**
28  * Atomix storage statistics.
29  */
30 public class StorageStatistics {
31   private static final Logger LOGGER = LoggerFactory.getLogger(StorageStatistics.class);
32
33   private final File file;
34   private final MBeanServer mBeanServer;
35
36   public StorageStatistics(File file) {
37     this.file = file;
38     this.mBeanServer = ManagementFactory.getPlatformMBeanServer();
39   }
40
41   /**
42    * Returns the amount of usable space remaining.
43    *
44    * @return the amount of usable space remaining
45    */
46   public long getUsableSpace() {
47     return file.getUsableSpace();
48   }
49
50   /**
51    * Returns the amount of free space remaining.
52    *
53    * @return the amount of free space remaining
54    */
55   public long getFreeSpace() {
56     return file.getFreeSpace();
57   }
58
59   /**
60    * Returns the total amount of space.
61    *
62    * @return the total amount of space
63    */
64   public long getTotalSpace() {
65     return file.getTotalSpace();
66   }
67
68   /**
69    * Returns the amount of free memory remaining.
70    *
71    * @return the amount of free memory remaining if successful, -1 return indicates failure.
72    */
73   public long getFreeMemory() {
74     try {
75       return (long) mBeanServer.getAttribute(new ObjectName("java.lang", "type", "OperatingSystem"), "FreePhysicalMemorySize");
76     } catch (Exception e) {
77       if (LOGGER.isDebugEnabled()) {
78         LOGGER.debug("An exception occurred during memory check", e);
79       }
80     }
81     return -1;
82   }
83
84   /**
85    * Returns the total amount of memory.
86    *
87    * @return the total amount of memory if successful, -1 return indicates failure.
88    */
89   public long getTotalMemory() {
90     try {
91       return (long) mBeanServer.getAttribute(new ObjectName("java.lang", "type", "OperatingSystem"), "TotalPhysicalMemorySize");
92     } catch (Exception e) {
93       if (LOGGER.isDebugEnabled()) {
94         LOGGER.debug("An exception occurred during memory check", e);
95       }
96     }
97     return -1;
98   }
99 }