Replaced the StreamReader based lm-sensors access with an implementation using the FileStream class in order to avoid buffering problems when seeking.
authormoel.mich
Sun, 10 Apr 2011 23:36:07 +0000
changeset 268844ba72c11de
parent 267 c26ff31e7b54
child 269 38cd90779aad
Replaced the StreamReader based lm-sensors access with an implementation using the FileStream class in order to avoid buffering problems when seeking.
Hardware/CPU/AMD10CPU.cs
Hardware/LPC/LMSensors.cs
Properties/AssemblyVersion.cs
     1.1 --- a/Hardware/CPU/AMD10CPU.cs	Sun Apr 10 20:22:20 2011 +0000
     1.2 +++ b/Hardware/CPU/AMD10CPU.cs	Sun Apr 10 23:36:07 2011 +0000
     1.3 @@ -39,10 +39,10 @@
     1.4  using System.Collections.Generic;
     1.5  using System.Diagnostics;
     1.6  using System.Globalization;
     1.7 +using System.IO;
     1.8  using System.Runtime.InteropServices;
     1.9  using System.Text;
    1.10  using System.Threading;
    1.11 -using System.IO;
    1.12  
    1.13  namespace OpenHardwareMonitor.Hardware.CPU {
    1.14  
    1.15 @@ -65,7 +65,7 @@
    1.16      private readonly uint miscellaneousControlAddress;
    1.17      private readonly ushort miscellaneousControlDeviceId;
    1.18  
    1.19 -    private readonly StreamReader temperatureReader;
    1.20 +    private readonly FileStream temperatureStream;
    1.21  
    1.22      private double timeStampCounterMultiplier;
    1.23  
    1.24 @@ -118,7 +118,7 @@
    1.25        ThreadAffinity.Set(mask);
    1.26  
    1.27        // the file reader for lm-sensors support on Linux
    1.28 -      temperatureReader = null;
    1.29 +      temperatureStream = null;
    1.30        int p = (int)Environment.OSVersion.Platform;
    1.31        if ((p == 4) || (p == 128)) {
    1.32          string[] devicePaths = Directory.GetDirectories("/sys/class/hwmon/");
    1.33 @@ -130,7 +130,8 @@
    1.34            } catch (IOException) { }
    1.35            switch (name) {
    1.36              case "k10temp":
    1.37 -              temperatureReader = new StreamReader(path + "/device/temp1_input");
    1.38 +              temperatureStream = new FileStream(path + "/device/temp1_input", 
    1.39 +                FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    1.40                break;
    1.41            }
    1.42          }
    1.43 @@ -215,10 +216,23 @@
    1.44        return 0.5 * (frequencyID + 0x10) / (1 << (int)divisorID);
    1.45      }
    1.46  
    1.47 +    private string ReadFirstLine(Stream stream) {
    1.48 +      StringBuilder sb = new StringBuilder();
    1.49 +      try {
    1.50 +        stream.Seek(0, SeekOrigin.Begin);
    1.51 +        int b = stream.ReadByte();
    1.52 +        while (b != -1 && b != 10) {
    1.53 +          sb.Append((char)b);
    1.54 +          b = stream.ReadByte();
    1.55 +        }
    1.56 +      } catch { }
    1.57 +      return sb.ToString();
    1.58 +    }
    1.59 +
    1.60      public override void Update() {
    1.61        base.Update();
    1.62  
    1.63 -      if (temperatureReader == null) {
    1.64 +      if (temperatureStream == null) {
    1.65          if (miscellaneousControlAddress != Ring0.InvalidPciAddress) {
    1.66            uint value;
    1.67            if (Ring0.ReadPciConfig(miscellaneousControlAddress,
    1.68 @@ -231,8 +245,7 @@
    1.69            }
    1.70          }
    1.71        } else {
    1.72 -        temperatureReader.BaseStream.Seek(0, SeekOrigin.Begin);
    1.73 -        string s = temperatureReader.ReadLine();
    1.74 +        string s = ReadFirstLine(temperatureStream);
    1.75          try {
    1.76            coreTemperature.Value = 0.001f *
    1.77              long.Parse(s, CultureInfo.InvariantCulture);
    1.78 @@ -276,8 +289,8 @@
    1.79      }
    1.80  
    1.81      public override void Close() {
    1.82 -      if (temperatureReader != null) {
    1.83 -        temperatureReader.Close();
    1.84 +      if (temperatureStream != null) {
    1.85 +        temperatureStream.Close();
    1.86        }
    1.87      }
    1.88    }
     2.1 --- a/Hardware/LPC/LMSensors.cs	Sun Apr 10 20:22:20 2011 +0000
     2.2 +++ b/Hardware/LPC/LMSensors.cs	Sun Apr 10 23:36:07 2011 +0000
     2.3 @@ -38,6 +38,7 @@
     2.4  using System.Collections.Generic;
     2.5  using System.Globalization;
     2.6  using System.IO;
     2.7 +using System.Text;
     2.8  
     2.9  namespace OpenHardwareMonitor.Hardware.LPC {
    2.10  
    2.11 @@ -116,9 +117,9 @@
    2.12        private readonly float?[] temperatures;
    2.13        private readonly float?[] fans;
    2.14  
    2.15 -      private readonly StreamReader[] voltageReaders;
    2.16 -      private readonly StreamReader[] temperatureReaders;
    2.17 -      private readonly StreamReader[] fanReaders;
    2.18 +      private readonly FileStream[] voltageStreams;
    2.19 +      private readonly FileStream[] temperatureStreams;
    2.20 +      private readonly FileStream[] fanStreams;
    2.21  
    2.22        public Chip Chip { get { return chip; } }
    2.23        public float?[] Voltages { get { return voltages; } }
    2.24 @@ -132,21 +133,24 @@
    2.25  
    2.26          string[] voltagePaths = Directory.GetFiles(path, "in*_input");
    2.27          this.voltages = new float?[voltagePaths.Length];
    2.28 -        this.voltageReaders = new StreamReader[voltagePaths.Length];
    2.29 +        this.voltageStreams = new FileStream[voltagePaths.Length];
    2.30          for (int i = 0; i < voltagePaths.Length; i++)
    2.31 -          voltageReaders[i] = new StreamReader(voltagePaths[i]);
    2.32 +          voltageStreams[i] = new FileStream(voltagePaths[i],
    2.33 +            FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    2.34  
    2.35          string[] temperaturePaths = Directory.GetFiles(path, "temp*_input");
    2.36          this.temperatures = new float?[temperaturePaths.Length];
    2.37 -        this.temperatureReaders = new StreamReader[temperaturePaths.Length];
    2.38 +        this.temperatureStreams = new FileStream[temperaturePaths.Length];
    2.39          for (int i = 0; i < temperaturePaths.Length; i++)
    2.40 -          temperatureReaders[i] = new StreamReader(temperaturePaths[i]);
    2.41 +          temperatureStreams[i] = new FileStream(temperaturePaths[i],
    2.42 +            FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    2.43  
    2.44          string[] fanPaths = Directory.GetFiles(path, "fan*_input");
    2.45          this.fans = new float?[fanPaths.Length];
    2.46 -        this.fanReaders = new StreamReader[fanPaths.Length];
    2.47 +        this.fanStreams = new FileStream[fanPaths.Length];
    2.48          for (int i = 0; i < fanPaths.Length; i++)
    2.49 -          fanReaders[i] = new StreamReader(fanPaths[i]);
    2.50 +          fanStreams[i] = new FileStream(fanPaths[i],
    2.51 +            FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    2.52        }
    2.53  
    2.54        public byte? ReadGPIO(int index) {
    2.55 @@ -159,12 +163,24 @@
    2.56          return null;
    2.57        }
    2.58  
    2.59 +      private string ReadFirstLine(Stream stream) {
    2.60 +        StringBuilder sb = new StringBuilder();
    2.61 +        try {
    2.62 +          stream.Seek(0, SeekOrigin.Begin);
    2.63 +          int b = stream.ReadByte();
    2.64 +          while (b != -1 && b != 10) {
    2.65 +            sb.Append((char)b);
    2.66 +            b = stream.ReadByte();
    2.67 +          }
    2.68 +        } catch { }
    2.69 +        return sb.ToString();
    2.70 +      }
    2.71 +
    2.72        public void Update() {
    2.73          for (int i = 0; i < voltages.Length; i++) {
    2.74 -          voltageReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
    2.75 -          string s = voltageReaders[i].ReadLine();
    2.76 +          string s = ReadFirstLine(voltageStreams[i]);
    2.77            try {
    2.78 -            voltages[i] = 0.001f * 
    2.79 +            voltages[i] = 0.001f *
    2.80                long.Parse(s, CultureInfo.InvariantCulture);
    2.81            } catch {
    2.82              voltages[i] = null;
    2.83 @@ -172,10 +188,9 @@
    2.84          }
    2.85  
    2.86          for (int i = 0; i < temperatures.Length; i++) {
    2.87 -          temperatureReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
    2.88 -          string s = temperatureReaders[i].ReadLine();
    2.89 +          string s = ReadFirstLine(temperatureStreams[i]);
    2.90            try {
    2.91 -            temperatures[i] = 0.001f * 
    2.92 +            temperatures[i] = 0.001f *
    2.93                long.Parse(s, CultureInfo.InvariantCulture);
    2.94            } catch {
    2.95              temperatures[i] = null;
    2.96 @@ -183,8 +198,7 @@
    2.97          }
    2.98  
    2.99          for (int i = 0; i < fans.Length; i++) {
   2.100 -          fanReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
   2.101 -          string s = fanReaders[i].ReadLine();
   2.102 +          string s = ReadFirstLine(fanStreams[i]);
   2.103            try {
   2.104              fans[i] = long.Parse(s, CultureInfo.InvariantCulture);
   2.105            } catch {
   2.106 @@ -194,12 +208,12 @@
   2.107        }
   2.108  
   2.109        public void Close() {
   2.110 -        foreach (StreamReader reader in voltageReaders)
   2.111 -          reader.Close();
   2.112 -        foreach (StreamReader reader in temperatureReaders)
   2.113 -          reader.Close();
   2.114 -        foreach (StreamReader reader in fanReaders)
   2.115 -          reader.Close();
   2.116 +        foreach (FileStream stream in voltageStreams)
   2.117 +          stream.Close();
   2.118 +        foreach (FileStream stream in temperatureStreams)
   2.119 +          stream.Close();
   2.120 +        foreach (FileStream stream in fanStreams)
   2.121 +          stream.Close();
   2.122        }
   2.123      }
   2.124    }
     3.1 --- a/Properties/AssemblyVersion.cs	Sun Apr 10 20:22:20 2011 +0000
     3.2 +++ b/Properties/AssemblyVersion.cs	Sun Apr 10 23:36:07 2011 +0000
     3.3 @@ -37,5 +37,5 @@
     3.4  
     3.5  using System.Reflection;
     3.6  
     3.7 -[assembly: AssemblyVersion("0.2.1.16")]
     3.8 -[assembly: AssemblyInformationalVersion("0.2.1.16 Alpha")]
     3.9 \ No newline at end of file
    3.10 +[assembly: AssemblyVersion("0.2.1.17")]
    3.11 +[assembly: AssemblyInformationalVersion("0.2.1.17 Alpha")]
    3.12 \ No newline at end of file