# HG changeset patch # User moel.mich # Date 1302478567 0 # Node ID 844ba72c11dee59c19ceba999e757cc2b31fcbeb # Parent c26ff31e7b54c5709096396223766d70ca291ac9 Replaced the StreamReader based lm-sensors access with an implementation using the FileStream class in order to avoid buffering problems when seeking. diff -r c26ff31e7b54 -r 844ba72c11de Hardware/CPU/AMD10CPU.cs --- a/Hardware/CPU/AMD10CPU.cs Sun Apr 10 20:22:20 2011 +0000 +++ b/Hardware/CPU/AMD10CPU.cs Sun Apr 10 23:36:07 2011 +0000 @@ -39,10 +39,10 @@ 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 @@ private readonly uint miscellaneousControlAddress; private readonly ushort miscellaneousControlDeviceId; - private readonly StreamReader temperatureReader; + private readonly FileStream temperatureStream; private double timeStampCounterMultiplier; @@ -118,7 +118,7 @@ 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 @@ } 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 @@ 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 @@ } } } 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 @@ } public override void Close() { - if (temperatureReader != null) { - temperatureReader.Close(); + if (temperatureStream != null) { + temperatureStream.Close(); } } } diff -r c26ff31e7b54 -r 844ba72c11de Hardware/LPC/LMSensors.cs --- a/Hardware/LPC/LMSensors.cs Sun Apr 10 20:22:20 2011 +0000 +++ b/Hardware/LPC/LMSensors.cs Sun Apr 10 23:36:07 2011 +0000 @@ -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 @@ 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 @@ 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,12 +163,24 @@ 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 * + voltages[i] = 0.001f * long.Parse(s, CultureInfo.InvariantCulture); } catch { voltages[i] = null; @@ -172,10 +188,9 @@ } 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 * + temperatures[i] = 0.001f * long.Parse(s, CultureInfo.InvariantCulture); } catch { temperatures[i] = null; @@ -183,8 +198,7 @@ } 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 @@ } 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(); } } } diff -r c26ff31e7b54 -r 844ba72c11de Properties/AssemblyVersion.cs --- a/Properties/AssemblyVersion.cs Sun Apr 10 20:22:20 2011 +0000 +++ b/Properties/AssemblyVersion.cs Sun Apr 10 23:36:07 2011 +0000 @@ -37,5 +37,5 @@ using System.Reflection; -[assembly: AssemblyVersion("0.2.1.16")] -[assembly: AssemblyInformationalVersion("0.2.1.16 Alpha")] \ No newline at end of file +[assembly: AssemblyVersion("0.2.1.17")] +[assembly: AssemblyInformationalVersion("0.2.1.17 Alpha")] \ No newline at end of file