@Override public Long invoke(File f, VirtualChannel channel) throws IOException { return f.getTotalSpace(); } }
@Override public long getTotalSpace() { return _root.getTotalSpace(); } }
/** * Returns the total amount of space. * * @return the total amount of space */ public long getTotalSpace() { return file.getTotalSpace(); }
/** * Returns the capacity for a given path * @param path path * @return total space */ public static long getContainerCapacity(final Path path) { return path.toFile().getTotalSpace(); }
StorageLocation(File path, long maxSize, @Nullable Double freeSpacePercent) { this.path = path; this.maxSize = maxSize; if (freeSpacePercent != null) { long totalSpaceInPartition = path.getTotalSpace(); this.freeSpaceToKeep = (long) ((freeSpacePercent * totalSpaceInPartition) / 100); log.info( "SegmentLocation[%s] will try and maintain [%d:%d] free space while loading segments.", path, freeSpaceToKeep, totalSpaceInPartition ); } else { this.freeSpaceToKeep = 0; } this.segments = new HashSet<>(); }
public static double getDiskPartitionSpaceUsedPercent(final String path) { if (null == path || path.isEmpty()) return -1; try { File file = new File(path); if (!file.exists()) return -1; long totalSpace = file.getTotalSpace(); if (totalSpace > 0) { long freeSpace = file.getFreeSpace(); long usedSpace = totalSpace - freeSpace; return usedSpace / (double) totalSpace; } } catch (Exception e) { return -1; } return -1; }
@Override public long percentAvailable(File file) { return Math.round(((double) file.getFreeSpace() / file.getTotalSpace()) * 100); } }
public String check(String folder) { if (folder == null) throw new IllegalArgumentException("folder"); File f = new File(folder); folderSize = getFileSize(f); usage = 1.0*(f.getTotalSpace() - f.getFreeSpace())/ f.getTotalSpace(); return String.format("used %d files %d disk in use %f", folderSize, fileCount, usage); }
private static String getDiskSpace( DatabaseLayout databaseLayout ) { File directory = databaseLayout.databaseDirectory(); long free = directory.getFreeSpace(); long total = directory.getTotalSpace(); long percentage = total != 0 ? (free * 100 / total) : 0; return String.format( "Disk space on partition (Total / Free / Free %%): %s / %s / %s", total, free, percentage ); }
@Override public long getSpace(String path, SpaceType type) throws IOException { path = stripPath(path); File file = new File(path); switch (type) { case SPACE_TOTAL: return file.getTotalSpace(); case SPACE_FREE: return file.getFreeSpace(); case SPACE_USED: return file.getTotalSpace() - file.getFreeSpace(); default: throw new IOException("Unknown space type: " + type); } }
System.out.println("Total space (bytes): " + root.getTotalSpace()); System.out.println("Free space (bytes): " + root.getFreeSpace()); System.out.println("Usable space (bytes): " + root.getUsableSpace());
@Override public FsStatus getStatus(Path p) throws IOException { File partition = pathToFile(p == null ? new Path("/") : p); //File provides getUsableSpace() and getFreeSpace() //File provides no API to obtain used space, assume used = total - free return new FsStatus(partition.getTotalSpace(), partition.getTotalSpace() - partition.getFreeSpace(), partition.getFreeSpace()); }
@Override public FsStats fsStats() { final Map<String, FsStats.Filesystem> filesystems = new HashMap<>(locations.size()); for (File location : locations) { final String path = location.getAbsolutePath(); final long total = location.getTotalSpace(); final long free = location.getFreeSpace(); final long available = location.getUsableSpace(); final long used = total - free; final short usedPercent = (short) ((double) used / total * 100); final FsStats.Filesystem filesystem = FsStats.Filesystem.create( path, total, free, available, used, usedPercent); filesystems.put(path, filesystem); } return FsStats.create(filesystems); } }
protected void doRun() { long free = Jenkins.getInstance().getRootDir().getUsableSpace(); long total = Jenkins.getInstance().getRootDir().getTotalSpace(); if(free<=0 || total<=0) { // information unavailable. pointless to try. LOGGER.info("JENKINS_HOME disk usage information isn't available. aborting to monitor"); cancel(); return; } LOGGER.fine("Monitoring disk usage of JENKINS_HOME. total="+total+" free="+free); // if it's more than 90% full and less than the minimum, activate // it's AND and not OR so that small Hudson home won't get a warning, // and similarly, if you have a 1TB disk, you don't get a warning when you still have 100GB to go. HudsonHomeDiskUsageMonitor.get().activated = (total/free>10 && free< FREE_SPACE_THRESHOLD); }
protected void percentLimitFromFile(File directory) { if (percentLimit > 0) { if (total > 0) { this.setLimit(total * percentLimit / 100); } else if (directory != null) { File dir = StoreUtil.findParentDirectory(directory); if (dir != null) { this.setLimit(dir.getTotalSpace() * percentLimit / 100); } } } }
private StorageLocation fakeLocation(long total, long free, long max, Double percent) { File file = EasyMock.mock(File.class); EasyMock.expect(file.getTotalSpace()).andReturn(total).anyTimes(); EasyMock.expect(file.getFreeSpace()).andReturn(free).anyTimes(); EasyMock.replay(file); return new StorageLocation(file, max, percent); }
@Test public void updateSendsTotalToRecordsStats() { File dir = mock(File.class); when(dir.exists()).thenReturn(true); final long expectedTotal = 123; when(dir.getTotalSpace()).thenReturn(expectedTotal); TestableDiskUsage diskUsage = new TestableDiskUsage(dir); diskUsage.update(0.0f, 1.0f); assertThat(diskUsage.getTotal()).isEqualTo(expectedTotal); }
@Test public void criticalMessageStatesUsageExceedsCritical() { File dir = mock(File.class); when(dir.exists()).thenReturn(true); when(dir.getTotalSpace()).thenReturn(1024L * 1024L * 3L); when(dir.getUsableSpace()).thenReturn(1024L * 1024L * 2L); TestableDiskUsage diskUsage = new TestableDiskUsage(dir, 1/* one megabyte */); assertThat(diskUsage.update(0.0f, 33.2f)).isEqualTo(DiskState.CRITICAL); assertThat(diskUsage.getNext()).isEqualTo(DiskState.CRITICAL); assertThat(diskUsage.getPct()).isEqualTo("33.3%"); assertThat(diskUsage.getCriticalMessage()) .isEqualTo("the file system is 33.3% full, which reached the critical threshold of 33.2%."); }
@Test public void criticalMessageStatesUsageExceedsCriticalWithManyDigits() { File dir = mock(File.class); when(dir.exists()).thenReturn(true); when(dir.getTotalSpace()).thenReturn(1024L * 1024L * 3L); when(dir.getUsableSpace()).thenReturn(1024L * 1024L * 2L); TestableDiskUsage diskUsage = new TestableDiskUsage(dir, 1/* one megabyte */); assertThat(diskUsage.update(0.0f, 33.25783495783648593746336f)).isEqualTo(DiskState.CRITICAL); assertThat(diskUsage.getNext()).isEqualTo(DiskState.CRITICAL); assertThat(diskUsage.getPct()).isEqualTo("33.3%"); assertThat(diskUsage.getCriticalMessage()) .isEqualTo("the file system is 33.3% full, which reached the critical threshold of 33.3%."); }
@Test public void shouldPrintDiskUsage() { // Not sure how to get around this w/o spying. The method that we're unit testing will construct // other File instances with this guy as parent and internally the File constructor uses the field 'path' // which, if purely mocked, won't be assigned. At the same time we want to control the total/free space methods // and what they return... a tough one. File storeDir = Mockito.spy( new File( "storeDir" ) ); DatabaseLayout layout = mock( DatabaseLayout.class ); when( layout.databaseDirectory() ).thenReturn( storeDir ); when( storeDir.getTotalSpace() ).thenReturn( 100L ); when( storeDir.getFreeSpace() ).thenReturn( 40L ); AssertableLogProvider logProvider = new AssertableLogProvider(); KernelDiagnostics.StoreFiles storeFiles = new KernelDiagnostics.StoreFiles( layout ); storeFiles.dump( logProvider.getLog( getClass() ).debugLogger() ); logProvider.assertContainsMessageContaining( "100 / 40 / 40" ); }