Skip to content

Commit

Permalink
HADOOP-12211. Collect disk usage on the node. Contributed by Robert G…
Browse files Browse the repository at this point in the history
…randl
  • Loading branch information
cdouglas committed Jul 13, 2015
1 parent 9ef03a4 commit a431ed9
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 4 deletions.
2 changes: 2 additions & 0 deletions hadoop-common-project/hadoop-common/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,8 @@ Release 2.8.0 - UNRELEASED

HADOOP-12210. Collect network usage on the node (Robert Grandl via cdouglas)

HADOOP-12211. Collect disk usage on the node (Robert Grandl via cdouglas)

OPTIMIZATIONS

HADOOP-11785. Reduce the number of listStatus operation in distcp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,18 @@ public static SysInfo newInstance() {
*/
public abstract long getNetworkBytesWritten();

/**
* Obtain the aggregated number of bytes read from disks.
*
* @return total number of bytes read.
*/
public abstract long getStorageBytesRead();

/**
* Obtain the aggregated number of bytes written to disks.
*
* @return total number of bytes written.
*/
public abstract long getStorageBytesWritten();

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -94,11 +95,27 @@ public class SysInfoLinux extends SysInfo {
"[ \t]*([0-9]+)[ \t]*([0-9]+)[ \t]*([0-9]+)[ \t]*([0-9]+)" +
"[ \t]*([0-9]+)[ \t]*([0-9]+)[ \t]*([0-9]+)[ \t]*([0-9]+).*");

/**
* Pattern for parsing /proc/diskstats.
*/
private static final String PROCFS_DISKSFILE = "/proc/diskstats";
private static final Pattern PROCFS_DISKSFILE_FORMAT =
Pattern.compile("^[ \t]*([0-9]+)[ \t]*([0-9 ]+)" +
"(?!([a-zA-Z]+[0-9]+))([a-zA-Z]+)" +
"[ \t]*([0-9]+)[ \t]*([0-9]+)[ \t]*([0-9]+)[ \t]*([0-9]+)" +
"[ \t]*([0-9]+)[ \t]*([0-9]+)[ \t]*([0-9]+)[ \t]*([0-9]+)" +
"[ \t]*([0-9]+)[ \t]*([0-9]+)[ \t]*([0-9]+)");
/**
* Pattern for parsing /sys/block/partition_name/queue/hw_sector_size.
*/
private static final Pattern PROCFS_DISKSECTORFILE_FORMAT =
Pattern.compile("^([0-9]+)");

private String procfsMemFile;
private String procfsCpuFile;
private String procfsStatFile;
private String procfsNetFile;
private String procfsDisksFile;
private long jiffyLengthInMillis;

private long ramSize = 0;
Expand All @@ -113,10 +130,15 @@ public class SysInfoLinux extends SysInfo {
private long cpuFrequency = 0L; // CPU frequency on the system (kHz)
private long numNetBytesRead = 0L; // aggregated bytes read from network
private long numNetBytesWritten = 0L; // aggregated bytes written to network
private long numDisksBytesRead = 0L; // aggregated bytes read from disks
private long numDisksBytesWritten = 0L; // aggregated bytes written to disks

private boolean readMemInfoFile = false;
private boolean readCpuInfoFile = false;

/* map for every disk its sector size */
private HashMap<String, Integer> perDiskSectorSize = null;

public static final long PAGE_SIZE = getConf("PAGESIZE");
public static final long JIFFY_LENGTH_IN_MILLIS =
Math.max(Math.round(1000D / getConf("CLK_TCK")), -1);
Expand Down Expand Up @@ -145,7 +167,7 @@ long getCurrentTime() {

public SysInfoLinux() {
this(PROCFS_MEMFILE, PROCFS_CPUINFO, PROCFS_STAT,
PROCFS_NETFILE, JIFFY_LENGTH_IN_MILLIS);
PROCFS_NETFILE, PROCFS_DISKSFILE, JIFFY_LENGTH_IN_MILLIS);
}

/**
Expand All @@ -155,20 +177,24 @@ public SysInfoLinux() {
* @param procfsCpuFile fake file for /proc/cpuinfo
* @param procfsStatFile fake file for /proc/stat
* @param procfsNetFile fake file for /proc/net/dev
* @param procfsDisksFile fake file for /proc/diskstats
* @param jiffyLengthInMillis fake jiffy length value
*/
@VisibleForTesting
public SysInfoLinux(String procfsMemFile,
String procfsCpuFile,
String procfsStatFile,
String procfsNetFile,
String procfsDisksFile,
long jiffyLengthInMillis) {
this.procfsMemFile = procfsMemFile;
this.procfsCpuFile = procfsCpuFile;
this.procfsStatFile = procfsStatFile;
this.procfsNetFile = procfsNetFile;
this.procfsDisksFile = procfsDisksFile;
this.jiffyLengthInMillis = jiffyLengthInMillis;
this.cpuTimeTracker = new CpuTimeTracker(jiffyLengthInMillis);
this.perDiskSectorSize = new HashMap<String, Integer>();
}

/**
Expand Down Expand Up @@ -411,6 +437,119 @@ private void readProcNetInfoFile() {
}
}

/**
* Read /proc/diskstats file, parse and calculate amount
* of bytes read and written from/to disks.
*/
private void readProcDisksInfoFile() {

numDisksBytesRead = 0L;
numDisksBytesWritten = 0L;

// Read "/proc/diskstats" file
BufferedReader in;
try {
in = new BufferedReader(new InputStreamReader(
new FileInputStream(procfsDisksFile), Charset.forName("UTF-8")));
} catch (FileNotFoundException f) {
return;
}

Matcher mat;
try {
String str = in.readLine();
while (str != null) {
mat = PROCFS_DISKSFILE_FORMAT.matcher(str);
if (mat.find()) {
String diskName = mat.group(4);
assert diskName != null;
// ignore loop or ram partitions
if (diskName.contains("loop") || diskName.contains("ram")) {
str = in.readLine();
continue;
}

Integer sectorSize;
synchronized (perDiskSectorSize) {
sectorSize = perDiskSectorSize.get(diskName);
if (null == sectorSize) {
// retrieve sectorSize
// if unavailable or error, assume 512
sectorSize = readDiskBlockInformation(diskName, 512);
perDiskSectorSize.put(diskName, sectorSize);
}
}

String sectorsRead = mat.group(7);
String sectorsWritten = mat.group(11);
if (null == sectorsRead || null == sectorsWritten) {
return;
}
numDisksBytesRead += Long.parseLong(sectorsRead) * sectorSize;
numDisksBytesWritten += Long.parseLong(sectorsWritten) * sectorSize;
}
str = in.readLine();
}
} catch (IOException e) {
LOG.warn("Error reading the stream " + procfsDisksFile, e);
} finally {
// Close the streams
try {
in.close();
} catch (IOException e) {
LOG.warn("Error closing the stream " + procfsDisksFile, e);
}
}
}

/**
* Read /sys/block/diskName/queue/hw_sector_size file, parse and calculate
* sector size for a specific disk.
* @return sector size of specified disk, or defSector
*/
int readDiskBlockInformation(String diskName, int defSector) {

assert perDiskSectorSize != null && diskName != null;

String procfsDiskSectorFile =
"/sys/block/" + diskName + "/queue/hw_sector_size";

BufferedReader in;
try {
in = new BufferedReader(new InputStreamReader(
new FileInputStream(procfsDiskSectorFile),
Charset.forName("UTF-8")));
} catch (FileNotFoundException f) {
return defSector;
}

Matcher mat;
try {
String str = in.readLine();
while (str != null) {
mat = PROCFS_DISKSECTORFILE_FORMAT.matcher(str);
if (mat.find()) {
String secSize = mat.group(1);
if (secSize != null) {
return Integer.parseInt(secSize);
}
}
str = in.readLine();
}
return defSector;
} catch (IOException|NumberFormatException e) {
LOG.warn("Error reading the stream " + procfsDiskSectorFile, e);
return defSector;
} finally {
// Close the streams
try {
in.close();
} catch (IOException e) {
LOG.warn("Error closing the stream " + procfsDiskSectorFile, e);
}
}
}

/** {@inheritDoc} */
@Override
public long getPhysicalMemorySize() {
Expand Down Expand Up @@ -492,6 +631,18 @@ public long getNetworkBytesWritten() {
return numNetBytesWritten;
}

@Override
public long getStorageBytesRead() {
readProcDisksInfoFile();
return numDisksBytesRead;
}

@Override
public long getStorageBytesWritten() {
readProcDisksInfoFile();
return numDisksBytesWritten;
}

/**
* Test the {@link SysInfoLinux}.
*
Expand All @@ -515,6 +666,10 @@ public static void main(String[] args) {
+ plugin.getNetworkBytesRead());
System.out.println("Total network written (bytes) : "
+ plugin.getNetworkBytesWritten());
System.out.println("Total storage read (bytes) : "
+ plugin.getStorageBytesRead());
System.out.println("Total storage written (bytes) : "
+ plugin.getStorageBytesWritten());
try {
// Sleep so we can compute the CPU usage
Thread.sleep(500L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,16 @@ public long getNetworkBytesWritten() {
return 0L;
}

@Override
public long getStorageBytesRead() {
// TODO unimplemented
return 0L;
}

@Override
public long getStorageBytesWritten() {
// TODO unimplemented
return 0L;
}

}
Loading

0 comments on commit a431ed9

Please sign in to comment.