2 * Copyright 2017-present Open Networking Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package io.atomix.storage.statistics;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 import java.lang.management.ManagementFactory;
24 import javax.management.MBeanServer;
25 import javax.management.ObjectName;
28 * Atomix storage statistics.
30 public class StorageStatistics {
31 private static final Logger LOGGER = LoggerFactory.getLogger(StorageStatistics.class);
33 private final File file;
34 private final MBeanServer mBeanServer;
36 public StorageStatistics(File file) {
38 this.mBeanServer = ManagementFactory.getPlatformMBeanServer();
42 * Returns the amount of usable space remaining.
44 * @return the amount of usable space remaining
46 public long getUsableSpace() {
47 return file.getUsableSpace();
51 * Returns the amount of free space remaining.
53 * @return the amount of free space remaining
55 public long getFreeSpace() {
56 return file.getFreeSpace();
60 * Returns the total amount of space.
62 * @return the total amount of space
64 public long getTotalSpace() {
65 return file.getTotalSpace();
69 * Returns the amount of free memory remaining.
71 * @return the amount of free memory remaining if successful, -1 return indicates failure.
73 public long getFreeMemory() {
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);
85 * Returns the total amount of memory.
87 * @return the total amount of memory if successful, -1 return indicates failure.
89 public long getTotalMemory() {
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);