From 2f18caa6b887557fa4a445f354517bbace986c99 Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Wed, 9 Jul 2014 07:24:38 +0200 Subject: [PATCH] Quick implementation of free UNIX command --- Allgemeines/FreeCSharp/FreeCSharp.sln | 20 ++++++ .../FreeCSharp/FreeCSharp/FreeCSharp.csproj | 42 ++++++++++++ Allgemeines/FreeCSharp/FreeCSharp/Program.cs | 75 ++++++++++++++++++++++ .../FreeCSharp/Properties/AssemblyInfo.cs | 22 +++++++ 4 files changed, 159 insertions(+) create mode 100644 Allgemeines/FreeCSharp/FreeCSharp.sln create mode 100644 Allgemeines/FreeCSharp/FreeCSharp/FreeCSharp.csproj create mode 100644 Allgemeines/FreeCSharp/FreeCSharp/Program.cs create mode 100644 Allgemeines/FreeCSharp/FreeCSharp/Properties/AssemblyInfo.cs diff --git a/Allgemeines/FreeCSharp/FreeCSharp.sln b/Allgemeines/FreeCSharp/FreeCSharp.sln new file mode 100644 index 0000000..eb99328 --- /dev/null +++ b/Allgemeines/FreeCSharp/FreeCSharp.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FreeCSharp", "FreeCSharp\FreeCSharp.csproj", "{C6503E05-09CF-4186-8153-0F30B7642DA5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C6503E05-09CF-4186-8153-0F30B7642DA5}.Debug|x86.ActiveCfg = Debug|x86 + {C6503E05-09CF-4186-8153-0F30B7642DA5}.Debug|x86.Build.0 = Debug|x86 + {C6503E05-09CF-4186-8153-0F30B7642DA5}.Release|x86.ActiveCfg = Release|x86 + {C6503E05-09CF-4186-8153-0F30B7642DA5}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(MonoDevelopProperties) = preSolution + StartupItem = FreeCSharp\FreeCSharp.csproj + EndGlobalSection +EndGlobal diff --git a/Allgemeines/FreeCSharp/FreeCSharp/FreeCSharp.csproj b/Allgemeines/FreeCSharp/FreeCSharp/FreeCSharp.csproj new file mode 100644 index 0000000..f577e1e --- /dev/null +++ b/Allgemeines/FreeCSharp/FreeCSharp/FreeCSharp.csproj @@ -0,0 +1,42 @@ + + + + Debug + x86 + 12.0.0 + 2.0 + {C6503E05-09CF-4186-8153-0F30B7642DA5} + Exe + FreeCSharp + FreeCSharp + v4.5 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + true + x86 + + + full + true + bin\Release + prompt + 4 + true + x86 + + + + + + + + + + \ No newline at end of file diff --git a/Allgemeines/FreeCSharp/FreeCSharp/Program.cs b/Allgemeines/FreeCSharp/FreeCSharp/Program.cs new file mode 100644 index 0000000..7f2817c --- /dev/null +++ b/Allgemeines/FreeCSharp/FreeCSharp/Program.cs @@ -0,0 +1,75 @@ +using System; +using System.Text.RegularExpressions; +using System.IO; + +namespace FreeCSharp +{ + class MainClass + { + public static void Main(string[] args) + { + FreeCSharp free = new FreeCSharp(); + free.GetValues(); + + long buffersPlusCached = free.Buffers + free.Cached; + long mainUsed = free.MemTotal - free.MemFree; + + // What you would get from free command: + Console.WriteLine("-/+ buffers/cache: {0} {1}", (mainUsed - buffersPlusCached), (free.MemFree + buffersPlusCached)); + + // What means: + Console.WriteLine("Used physical memory: {0} kB", mainUsed - buffersPlusCached); + Console.WriteLine("Available physical memory: {0} kB", free.MemFree + buffersPlusCached); + } + } + + /// + /// FreeCSharp: quick implementation of free command (kind of) using C# + /// + public class FreeCSharp + { + public long MemTotal { get; private set; } + public long MemFree { get; private set; } + public long Buffers { get; private set; } + public long Cached { get; private set; } + + public void GetValues() + { + string[] memInfoLines = File.ReadAllLines(@"/proc/meminfo"); + + MemInfoMatch[] memInfoMatches = + { + new MemInfoMatch(@"^Buffers:\s+(\d+)", value => Buffers = Convert.ToInt64(value)), + new MemInfoMatch(@"^Cached:\s+(\d+)", value => Cached = Convert.ToInt64(value)), + new MemInfoMatch(@"^MemFree:\s+(\d+)", value => MemFree = Convert.ToInt64(value)), + new MemInfoMatch(@"^MemTotal:\s+(\d+)", value => MemTotal = Convert.ToInt64(value)) + }; + + foreach (string memInfoLine in memInfoLines) + { + foreach (MemInfoMatch memInfoMatch in memInfoMatches) + { + Match match = memInfoMatch.regex.Match(memInfoLine); + if (match.Groups[1].Success) + { + string value = match.Groups[1].Value; + memInfoMatch.updateValue(value); + } + } + } + } + + public class MemInfoMatch + { + public Regex regex; + public Action updateValue; + + public MemInfoMatch(string pattern, Action update) + { + this.regex = new Regex(pattern, RegexOptions.Compiled); + this.updateValue = update; + } + + } + } +} diff --git a/Allgemeines/FreeCSharp/FreeCSharp/Properties/AssemblyInfo.cs b/Allgemeines/FreeCSharp/FreeCSharp/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..322c5f2 --- /dev/null +++ b/Allgemeines/FreeCSharp/FreeCSharp/Properties/AssemblyInfo.cs @@ -0,0 +1,22 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. +[assembly: AssemblyTitle("FreeCSharp")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("gumartinm.name")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("gumartinm.name")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. +[assembly: AssemblyVersion("1.0.*")] +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + -- 2.1.4