Replaced the StreamReader based lm-sensors access with an implementation using the FileStream class in order to avoid buffering problems when seeking.

This commit is contained in:
Michael Möller
2011-04-10 23:36:07 +00:00
parent 1d8aa26d14
commit 0f1ba8e90f
3 changed files with 61 additions and 34 deletions

View File

@@ -39,10 +39,10 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.IO;
namespace OpenHardwareMonitor.Hardware.CPU {
@@ -65,7 +65,7 @@ namespace OpenHardwareMonitor.Hardware.CPU {
private readonly uint miscellaneousControlAddress;
private readonly ushort miscellaneousControlDeviceId;
private readonly StreamReader temperatureReader;
private readonly FileStream temperatureStream;
private double timeStampCounterMultiplier;
@@ -118,7 +118,7 @@ namespace OpenHardwareMonitor.Hardware.CPU {
ThreadAffinity.Set(mask);
// the file reader for lm-sensors support on Linux
temperatureReader = null;
temperatureStream = null;
int p = (int)Environment.OSVersion.Platform;
if ((p == 4) || (p == 128)) {
string[] devicePaths = Directory.GetDirectories("/sys/class/hwmon/");
@@ -130,7 +130,8 @@ namespace OpenHardwareMonitor.Hardware.CPU {
} catch (IOException) { }
switch (name) {
case "k10temp":
temperatureReader = new StreamReader(path + "/device/temp1_input");
temperatureStream = new FileStream(path + "/device/temp1_input",
FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
break;
}
}
@@ -215,10 +216,23 @@ namespace OpenHardwareMonitor.Hardware.CPU {
return 0.5 * (frequencyID + 0x10) / (1 << (int)divisorID);
}
private string ReadFirstLine(Stream stream) {
StringBuilder sb = new StringBuilder();
try {
stream.Seek(0, SeekOrigin.Begin);
int b = stream.ReadByte();
while (b != -1 && b != 10) {
sb.Append((char)b);
b = stream.ReadByte();
}
} catch { }
return sb.ToString();
}
public override void Update() {
base.Update();
if (temperatureReader == null) {
if (temperatureStream == null) {
if (miscellaneousControlAddress != Ring0.InvalidPciAddress) {
uint value;
if (Ring0.ReadPciConfig(miscellaneousControlAddress,
@@ -231,8 +245,7 @@ namespace OpenHardwareMonitor.Hardware.CPU {
}
}
} else {
temperatureReader.BaseStream.Seek(0, SeekOrigin.Begin);
string s = temperatureReader.ReadLine();
string s = ReadFirstLine(temperatureStream);
try {
coreTemperature.Value = 0.001f *
long.Parse(s, CultureInfo.InvariantCulture);
@@ -276,8 +289,8 @@ namespace OpenHardwareMonitor.Hardware.CPU {
}
public override void Close() {
if (temperatureReader != null) {
temperatureReader.Close();
if (temperatureStream != null) {
temperatureStream.Close();
}
}
}

View File

@@ -38,6 +38,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
namespace OpenHardwareMonitor.Hardware.LPC {
@@ -116,9 +117,9 @@ namespace OpenHardwareMonitor.Hardware.LPC {
private readonly float?[] temperatures;
private readonly float?[] fans;
private readonly StreamReader[] voltageReaders;
private readonly StreamReader[] temperatureReaders;
private readonly StreamReader[] fanReaders;
private readonly FileStream[] voltageStreams;
private readonly FileStream[] temperatureStreams;
private readonly FileStream[] fanStreams;
public Chip Chip { get { return chip; } }
public float?[] Voltages { get { return voltages; } }
@@ -132,21 +133,24 @@ namespace OpenHardwareMonitor.Hardware.LPC {
string[] voltagePaths = Directory.GetFiles(path, "in*_input");
this.voltages = new float?[voltagePaths.Length];
this.voltageReaders = new StreamReader[voltagePaths.Length];
this.voltageStreams = new FileStream[voltagePaths.Length];
for (int i = 0; i < voltagePaths.Length; i++)
voltageReaders[i] = new StreamReader(voltagePaths[i]);
voltageStreams[i] = new FileStream(voltagePaths[i],
FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
string[] temperaturePaths = Directory.GetFiles(path, "temp*_input");
this.temperatures = new float?[temperaturePaths.Length];
this.temperatureReaders = new StreamReader[temperaturePaths.Length];
this.temperatureStreams = new FileStream[temperaturePaths.Length];
for (int i = 0; i < temperaturePaths.Length; i++)
temperatureReaders[i] = new StreamReader(temperaturePaths[i]);
temperatureStreams[i] = new FileStream(temperaturePaths[i],
FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
string[] fanPaths = Directory.GetFiles(path, "fan*_input");
this.fans = new float?[fanPaths.Length];
this.fanReaders = new StreamReader[fanPaths.Length];
this.fanStreams = new FileStream[fanPaths.Length];
for (int i = 0; i < fanPaths.Length; i++)
fanReaders[i] = new StreamReader(fanPaths[i]);
fanStreams[i] = new FileStream(fanPaths[i],
FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
public byte? ReadGPIO(int index) {
@@ -159,10 +163,22 @@ namespace OpenHardwareMonitor.Hardware.LPC {
return null;
}
private string ReadFirstLine(Stream stream) {
StringBuilder sb = new StringBuilder();
try {
stream.Seek(0, SeekOrigin.Begin);
int b = stream.ReadByte();
while (b != -1 && b != 10) {
sb.Append((char)b);
b = stream.ReadByte();
}
} catch { }
return sb.ToString();
}
public void Update() {
for (int i = 0; i < voltages.Length; i++) {
voltageReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
string s = voltageReaders[i].ReadLine();
string s = ReadFirstLine(voltageStreams[i]);
try {
voltages[i] = 0.001f *
long.Parse(s, CultureInfo.InvariantCulture);
@@ -172,8 +188,7 @@ namespace OpenHardwareMonitor.Hardware.LPC {
}
for (int i = 0; i < temperatures.Length; i++) {
temperatureReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
string s = temperatureReaders[i].ReadLine();
string s = ReadFirstLine(temperatureStreams[i]);
try {
temperatures[i] = 0.001f *
long.Parse(s, CultureInfo.InvariantCulture);
@@ -183,8 +198,7 @@ namespace OpenHardwareMonitor.Hardware.LPC {
}
for (int i = 0; i < fans.Length; i++) {
fanReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
string s = fanReaders[i].ReadLine();
string s = ReadFirstLine(fanStreams[i]);
try {
fans[i] = long.Parse(s, CultureInfo.InvariantCulture);
} catch {
@@ -194,12 +208,12 @@ namespace OpenHardwareMonitor.Hardware.LPC {
}
public void Close() {
foreach (StreamReader reader in voltageReaders)
reader.Close();
foreach (StreamReader reader in temperatureReaders)
reader.Close();
foreach (StreamReader reader in fanReaders)
reader.Close();
foreach (FileStream stream in voltageStreams)
stream.Close();
foreach (FileStream stream in temperatureStreams)
stream.Close();
foreach (FileStream stream in fanStreams)
stream.Close();
}
}
}

View File

@@ -37,5 +37,5 @@
using System.Reflection;
[assembly: AssemblyVersion("0.2.1.16")]
[assembly: AssemblyInformationalVersion("0.2.1.16 Alpha")]
[assembly: AssemblyVersion("0.2.1.17")]
[assembly: AssemblyInformationalVersion("0.2.1.17 Alpha")]