2 // SystemInformation.cs
4 // Copyright (C) 2004-2006 Novell, Inc.
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.
28 using System
.Collections
;
29 using System
.Diagnostics
;
30 using System
.Globalization
;
32 using System
.Reflection
;
33 using System
.Runtime
.InteropServices
;
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
)
56 double [] loadavg
= new double [3];
57 int retval
= getloadavg (loadavg
, 3);
60 throw new IOException ("Could not get system load average: " + Mono
.Unix
.Native
.Stdlib
.strerror (Mono
.Unix
.Native
.Stdlib
.GetLastError ()));
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
{
74 return cached_loadavg_1min
;
78 static public double LoadAverageFiveMinute
{
81 return cached_loadavg_5min
;
85 static public double LoadAverageFifteenMinute
{
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
{
106 private enum ScreenSaverKind
{
112 [DllImport ("libbeagleglue.so")]
113 extern static unsafe int screensaver_info (ScreenSaverState
*state
,
114 ScreenSaverKind
*kind
,
118 static private void CheckScreenSaver ()
120 if ((DateTime
.Now
- screensaver_time
).TotalSeconds
< screensaver_poll_delay
)
123 ScreenSaverState state
;
124 ScreenSaverKind kind
;
125 ulong til_or_since
= 0, idle
= 0;
129 retval
= screensaver_info (&state
, &kind
, &til_or_since
, &idle
);
133 cached_screensaver_running
= (state
== ScreenSaverState
.On
);
134 cached_screensaver_idle_time
= idle
/ 1000.0;
136 cached_screensaver_running
= false;
137 cached_screensaver_idle_time
= 0;
140 screensaver_time
= DateTime
.Now
;
143 static public bool ScreenSaverRunning
{
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
{
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
)
173 if ((DateTime
.Now
- using_battery_time
).TotalSeconds
< acpi_poll_delay
)
176 if (! File
.Exists (proc_ac_state_filename
)) {
177 proc_ac_state_exists
= false;
178 using_battery
= false;
182 Stream stream
= new FileStream (proc_ac_state_filename
,
183 System
.IO
.FileMode
.Open
,
186 TextReader reader
= new StreamReader (stream
);
188 string line
= reader
.ReadLine ();
189 using_battery
= (line
!= null) && (line
.IndexOf (ac_present_string
) == -1);
194 using_battery_time
= DateTime
.Now
;
197 static public bool UsingBattery
{
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 ()
240 // We only refresh the stats once per second
241 if ((DateTime
.Now
- disk_stats_time
).TotalSeconds
< disk_stats_delay
)
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)
269 major
= (uint) stat
.st_dev
>> 8;
270 minor
= (uint) stat
.st_dev
& 0xff;
273 static public int DiskStatsReadReqs
{
278 return disk_stats_read_reqs
;
282 static public int DiskStatsReadBytes
{
287 return disk_stats_read_bytes
;
291 static public int DiskStatsWriteReqs
{
296 return disk_stats_write_reqs
;
300 static public int DiskStatsWriteBytes
{
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)
315 return (stat
.st_dev
>> 8 != 0);
318 ///////////////////////////////////////////////////////////////
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
)
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 ());
336 ///////////////////////////////////////////////////////////////
338 public static string MonoRuntimeVersion
{
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
,
354 ///////////////////////////////////////////////////////////////
359 Gtk
.Application
.Init ();
361 Console
.WriteLine ("{0} {1} {2} {3} {4} {5} {6} {7}",
362 LoadAverageOneMinute
,
363 LoadAverageFiveMinute
,
364 LoadAverageFifteenMinute
,
370 System
.Threading
.Thread
.Sleep (1000);