Add --enable-deletion option to buildindex. If used, buildindex will remove deleted...
[beagle.git] / Util / SystemInformation.cs
blobb5aa02f6d04ad4c902bbc4cdff899a5e889d5b9d
1 //
2 // SystemInformation.cs
3 //
4 // Copyright (C) 2004-2006 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 using System;
28 using System.Collections;
29 using System.Diagnostics;
30 using System.Globalization;
31 using System.IO;
32 using System.Reflection;
33 using System.Runtime.InteropServices;
34 using System.Text;
35 using System.Text.RegularExpressions;
37 namespace Beagle.Util {
39 public class SystemInformation {
41 [DllImport ("libc", SetLastError=true)]
42 static extern int getloadavg (double[] loadavg, int nelem);
44 const double loadavg_poll_delay = 3;
45 static DateTime proc_loadavg_time = DateTime.MinValue;
46 static double cached_loadavg_1min = -1;
47 static double cached_loadavg_5min = -1;
48 static double cached_loadavg_15min = -1;
50 static private void CheckLoadAverage ()
52 // Only call getloadavg() at most once every 10 seconds
53 if ((DateTime.Now - proc_loadavg_time).TotalSeconds < loadavg_poll_delay)
54 return;
56 double [] loadavg = new double [3];
57 int retval = getloadavg (loadavg, 3);
59 if (retval == -1)
60 throw new IOException ("Could not get system load average: " + Mono.Unix.Native.Stdlib.strerror (Mono.Unix.Native.Stdlib.GetLastError ()));
61 else if (retval != 3)
62 throw new IOException ("Could not get system load average: getloadavg() returned an unexpected number of samples");
64 cached_loadavg_1min = loadavg [0];
65 cached_loadavg_5min = loadavg [1];
66 cached_loadavg_15min = loadavg [2];
68 proc_loadavg_time = DateTime.Now;
71 static public double LoadAverageOneMinute {
72 get {
73 CheckLoadAverage ();
74 return cached_loadavg_1min;
78 static public double LoadAverageFiveMinute {
79 get {
80 CheckLoadAverage ();
81 return cached_loadavg_5min;
85 static public double LoadAverageFifteenMinute {
86 get {
87 CheckLoadAverage ();
88 return cached_loadavg_15min;
92 ///////////////////////////////////////////////////////////////
94 const double screensaver_poll_delay = 1;
95 static DateTime screensaver_time = DateTime.MinValue;
96 static bool cached_screensaver_running = false;
97 static double cached_screensaver_idle_time = 0;
99 private enum ScreenSaverState {
100 Off = 0,
101 On = 1,
102 Cycle = 2,
103 Disabled = 3
106 private enum ScreenSaverKind {
107 Blanked = 0,
108 Internal = 1,
109 External = 2
112 [DllImport ("libbeagleglue.so")]
113 extern static unsafe int screensaver_info (ScreenSaverState *state,
114 ScreenSaverKind *kind,
115 ulong *til_or_since,
116 ulong *idle);
118 static private void CheckScreenSaver ()
120 if ((DateTime.Now - screensaver_time).TotalSeconds < screensaver_poll_delay)
121 return;
123 ScreenSaverState state;
124 ScreenSaverKind kind;
125 ulong til_or_since = 0, idle = 0;
126 int retval;
128 unsafe {
129 retval = screensaver_info (&state, &kind, &til_or_since, &idle);
132 if (retval != 0) {
133 cached_screensaver_running = (state == ScreenSaverState.On);
134 cached_screensaver_idle_time = idle / 1000.0;
135 } else {
136 cached_screensaver_running = false;
137 cached_screensaver_idle_time = 0;
140 screensaver_time = DateTime.Now;
143 static public bool ScreenSaverRunning {
144 get {
145 CheckScreenSaver ();
146 return cached_screensaver_running;
150 // returns number of seconds since input was received
151 // from the user on any input device
152 static public double InputIdleTime {
153 get {
154 CheckScreenSaver ();
155 return cached_screensaver_idle_time;
159 ///////////////////////////////////////////////////////////////
161 const double acpi_poll_delay = 30;
162 const string proc_ac_state_filename = "/proc/acpi/ac_adapter/AC/state";
163 const string ac_present_string = "on-line";
164 static bool proc_ac_state_exists = true;
165 static DateTime using_battery_time = DateTime.MinValue;
166 static bool using_battery;
168 static public void CheckAcpi ()
170 if (! proc_ac_state_exists)
171 return;
173 if ((DateTime.Now - using_battery_time).TotalSeconds < acpi_poll_delay)
174 return;
176 if (! File.Exists (proc_ac_state_filename)) {
177 proc_ac_state_exists = false;
178 using_battery = false;
179 return;
182 Stream stream = new FileStream (proc_ac_state_filename,
183 System.IO.FileMode.Open,
184 FileAccess.Read,
185 FileShare.Read);
186 TextReader reader = new StreamReader (stream);
188 string line = reader.ReadLine ();
189 using_battery = (line != null) && (line.IndexOf (ac_present_string) == -1);
191 reader.Close ();
192 stream.Close ();
194 using_battery_time = DateTime.Now;
197 static public bool UsingBattery {
198 get {
199 CheckAcpi ();
200 return using_battery;
204 ///////////////////////////////////////////////////////////////
206 [DllImport ("libbeagleglue")]
207 extern static int get_vmsize ();
209 [DllImport ("libbeagleglue")]
210 extern static int get_vmrss ();
212 static public int VmSize {
213 get { return get_vmsize (); }
216 static public int VmRss {
217 get { return get_vmrss (); }
220 ///////////////////////////////////////////////////////////////
222 static private int disk_stats_read_reqs;
223 static private int disk_stats_write_reqs;
224 static private int disk_stats_read_bytes;
225 static private int disk_stats_write_bytes;
227 static private DateTime disk_stats_time = DateTime.MinValue;
228 static private double disk_stats_delay = 1.0;
230 static private uint major, minor;
232 // Update the disk statistics with data for block device on the (major,minor) pair.
233 static private void UpdateDiskStats ()
235 string buffer;
237 if (major == 0)
238 return;
240 // We only refresh the stats once per second
241 if ((DateTime.Now - disk_stats_time).TotalSeconds < disk_stats_delay)
242 return;
244 // Read in all of the disk stats
245 using (StreamReader stream = new StreamReader ("/proc/diskstats"))
246 buffer = stream.ReadToEnd ();
248 // Find our partition and parse it
249 const string REGEX = "[\\s]+{0}[\\s]+{1}[\\s]+[a-zA-Z0-9]+[\\s]+([0-9]+)[\\s]+([0-9]+)[\\s]+([0-9]+)[\\s]+([0-9]+)";
250 string regex = String.Format (REGEX, major, minor);
251 Regex r = new Regex (regex, RegexOptions.IgnoreCase | RegexOptions.Compiled);
252 for (System.Text.RegularExpressions.Match m = r.Match (buffer); m.Success; m = m.NextMatch ()) {
253 disk_stats_read_reqs = Convert.ToInt32 (m.Groups[1].ToString ());
254 disk_stats_read_bytes = Convert.ToInt32 (m.Groups[2].ToString ());
255 disk_stats_write_reqs = Convert.ToInt32 (m.Groups[3].ToString ());
256 disk_stats_write_bytes = Convert.ToInt32 (m.Groups[4].ToString ());
259 disk_stats_time = DateTime.Now;
262 // Get the (major,minor) pair for the block device from which the index is mounted.
263 static private void GetIndexDev ()
265 Mono.Unix.Native.Stat stat;
266 if (Mono.Unix.Native.Syscall.stat (PathFinder.StorageDir, out stat) != 0)
267 return;
269 major = (uint) stat.st_dev >> 8;
270 minor = (uint) stat.st_dev & 0xff;
273 static public int DiskStatsReadReqs {
274 get {
275 if (major == 0)
276 GetIndexDev ();
277 UpdateDiskStats ();
278 return disk_stats_read_reqs;
282 static public int DiskStatsReadBytes {
283 get {
284 if (major == 0)
285 GetIndexDev ();
286 UpdateDiskStats ();
287 return disk_stats_read_bytes;
291 static public int DiskStatsWriteReqs {
292 get {
293 if (major == 0)
294 GetIndexDev ();
295 UpdateDiskStats ();
296 return disk_stats_write_reqs;
300 static public int DiskStatsWriteBytes {
301 get {
302 if (major == 0)
303 GetIndexDev ();
304 UpdateDiskStats ();
305 return disk_stats_write_bytes;
309 static public bool IsPathOnBlockDevice (string path)
311 Mono.Unix.Native.Stat stat;
312 if (Mono.Unix.Native.Syscall.stat (path, out stat) != 0)
313 return false;
315 return (stat.st_dev >> 8 != 0);
318 ///////////////////////////////////////////////////////////////
320 [DllImport("libc")]
321 private static extern int prctl (int option, byte [] arg2, ulong arg3, ulong arg4, ulong arg5);
323 // From /usr/include/linux/prctl.h
324 private const int PR_SET_NAME = 15;
326 public static void SetProcessName(string name)
328 #if OS_LINUX
329 if (prctl (PR_SET_NAME, Encoding.ASCII.GetBytes (name + '\0'), 0, 0, 0) < 0) {
330 Logger.Log.Warn ("Couldn't set process name to '{0}': {1}", name,
331 Mono.Unix.Native.Stdlib.GetLastError ());
333 #endif
336 ///////////////////////////////////////////////////////////////
338 public static string MonoRuntimeVersion {
339 get {
340 Type t = typeof (object).Assembly.GetType ("Mono.Runtime");
342 string ver = (string) t.InvokeMember ("GetDisplayName",
343 BindingFlags.InvokeMethod
344 | BindingFlags.NonPublic
345 | BindingFlags.Static
346 | BindingFlags.DeclaredOnly
347 | BindingFlags.ExactBinding,
348 null, null, null);
350 return ver;
354 ///////////////////////////////////////////////////////////////
356 #if false
357 static void Main ()
359 Gtk.Application.Init ();
360 while (true) {
361 Console.WriteLine ("{0} {1} {2} {3} {4} {5} {6} {7}",
362 LoadAverageOneMinute,
363 LoadAverageFiveMinute,
364 LoadAverageFifteenMinute,
365 ScreenSaverRunning,
366 InputIdleTime,
367 UsingBattery,
368 DiskStatsReadReqs,
369 VmSize);
370 System.Threading.Thread.Sleep (1000);
373 #endif