Mercurial > hg > release > thermostat-0.7
changeset 27:a167e30d0669
read the process environment for each java process
author | Omair Majid <omajid@redhat.com> |
---|---|
date | Mon, 09 Jan 2012 13:06:14 -0500 |
parents | 5adb9bc270e2 |
children | 3c64459fb729 |
files | src/com/redhat/thermostat/backend/system/JvmStatHostListener.java src/com/redhat/thermostat/backend/system/ProcessEnvironmentBuilder.java |
diffstat | 2 files changed, 95 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/src/com/redhat/thermostat/backend/system/JvmStatHostListener.java Mon Jan 09 12:01:12 2012 -0500 +++ b/src/com/redhat/thermostat/backend/system/JvmStatHostListener.java Mon Jan 09 13:06:14 2012 -0500 @@ -87,7 +87,7 @@ long stopTime = Long.MIN_VALUE; JvmStatDataExtractor extractor = new JvmStatDataExtractor(vm); Map<String, String> properties = new HashMap<String, String>(); - Map<String, String> environment = new HashMap<String, String>(); + Map<String, String> environment = ProcessEnvironmentBuilder.build(vmId); List<String> loadedNativeLibraries = new ArrayList<String>(); info = new VmInfo(vmId, startTime, stopTime, extractor.getJavaVersion(), extractor.getJavaHome(),
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/com/redhat/thermostat/backend/system/ProcessEnvironmentBuilder.java Mon Jan 09 13:06:14 2012 -0500 @@ -0,0 +1,94 @@ +package com.redhat.thermostat.backend.system; + +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.Reader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +import com.redhat.thermostat.common.utils.LoggingUtils; + +public class ProcessEnvironmentBuilder { + + private static final Logger logger = LoggingUtils.getLogger(ProcessEnvironmentBuilder.class); + + private ProcessEnvironmentBuilder() { + /* should not be instantiated */ + } + + public static Map<String, String> build(int pid) { + Map<String, String> env = new HashMap<String, String>(); + + String filename = "/proc/" + pid + "/environ"; + try { + Reader reader = new FileReader(filename); + try { + char[] fileBuffer = new char[1024]; + int fileBufferIndex = 0; + char[] buffer = new char[1024]; + int read = 0; + while (true) { + read = reader.read(buffer); + if (read == -1) { + break; + } + + if (read + fileBufferIndex > fileBuffer.length) { + char[] newFileBuffer = new char[fileBuffer.length * 2]; + System.arraycopy(fileBuffer, 0, newFileBuffer, 0, fileBufferIndex); + fileBuffer = newFileBuffer; + } + System.arraycopy(buffer, 0, fileBuffer, fileBufferIndex, read); + fileBufferIndex = fileBufferIndex + read; + + } + List<String> parts = getParts(fileBuffer, fileBufferIndex); + for (String part : parts) { + int splitterPos = part.indexOf("="); + String key = part.substring(0, splitterPos); + String value = part.substring(splitterPos + 1); + env.put(key, value); + } + } catch (IOException e) { + logger.log(Level.WARNING, "error reading " + filename, e); + } finally { + try { + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + logger.log(Level.WARNING, "error closing " + filename); + } + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + logger.log(Level.WARNING, "file " + filename + " not found"); + } + + return env; + } + + /** + * Split a char array, where items are separated by a null into into a list + * of strings + */ + private static List<String> getParts(char[] nullSeparatedBuffer, int bufferLength) { + int maxLength = Math.min(nullSeparatedBuffer.length, bufferLength); + List<String> parts = new ArrayList<String>(); + + int lastStart = 0; + for (int i = 0; i < maxLength; i++) { + if (nullSeparatedBuffer[i] == '\0') { + String string = new String(nullSeparatedBuffer, lastStart, (i - lastStart)); + parts.add(string); + lastStart = i + 1; + } + } + return parts; + } + +}