File: CpuInfo.cs

package info (click to toggle)
sysinfo 0.7-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 1,832 kB
  • ctags: 1,140
  • sloc: cs: 2,933; sh: 701; makefile: 115; xml: 98
file content (123 lines) | stat: -rw-r--r-- 2,868 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Filename: CpuInfo.cs
// Contains functions to extract information displayed in CPU category

using System;
using System.IO;

namespace Sysinfo {
	
	public class CpuInfo {
		
		//global variables
		public String cpu_vendor = "unknown";
		public Int32 cpu_cpus = 0;
		
		public String cpu_name = "unknown";
		public String cpu_frequency = "unknown";
		public String cpu_cache = "unknown";
		
		public String cpu_bogomips = "unknown";
		public String cpu_numbering = "unknown";
		public String cpu_flags = "unknown";
		
		//read cpu function
		public void CpuStaticInfo() {
			
			String temp;
			Boolean modelB = true;
			Boolean staticB = true;
			cpu_cpus = 0;
			
			try {
				
				//get cpu information from /proc
				using (TextReader textread = File.OpenText("/proc/cpuinfo")) {

					while ( textread.Peek() != (-1) ) {
						
						temp = textread.ReadLine();

						//vendor
						if ( temp.StartsWith("vendor_id") && staticB )
							cpu_vendor = temp.Remove(0, 12);
						
						//model name
						if ( temp.StartsWith("model name") && staticB )
							cpu_name =  temp.Remove(0, 13);
										
						//cache
						if ( temp.StartsWith("cache size") && staticB ) 
							cpu_cache =  temp.Remove(0, 13);
						
						//numbering1 family
						if ( temp.StartsWith("cpu family") && staticB )
							cpu_numbering =  "family(" + temp.Remove(0, 13);

						//numbering2 model
						if ( temp.StartsWith("model") && modelB  && staticB ) {
							
							cpu_numbering =  cpu_numbering + ") model(" + temp.Remove(0, 9);
							modelB = false;
						}	

						//numbering3 stepping
						if ( temp.StartsWith("stepping") && staticB )
							cpu_numbering =  cpu_numbering + ") stepping(" + temp.Remove(0, 11) + ")";
						
						//flags
						if ( temp.StartsWith("flags") && staticB ) {
						
							cpu_flags =  temp.Remove(0, 9);
							staticB = false;
						}
						
						//number of cpus
						if ( temp.StartsWith("processor"))
							cpu_cpus++;
						
					}
				}
			}catch (FileNotFoundException ex) { Console.WriteLine(ex); }
		}
		
		//this function needs to be called more than once, to update info
		public void CpuDynamicInfo() {
			
			String temp;
			Boolean dynamicB = true;
			Boolean dynamicF = true;
			
			try {
				
				//get cpu information from /proc
				using (TextReader textread = File.OpenText("/proc/cpuinfo")) {
					
					while ( dynamicB ) {
						
						temp = textread.ReadLine();

						if (temp == null)
							break;
						
						//frequency
						if ( temp.StartsWith("cpu MHz")) {
							cpu_frequency =  temp.Remove(0, 11) + " MHz";
							dynamicF = false;
						}
						
						//bogomips
						if ( temp.StartsWith("bogomips")) {
						
							cpu_bogomips =  temp.Remove(0, 11);
							dynamicB = false;
						}

					}
				}
			}catch (FileNotFoundException ex) { Console.WriteLine( ex ); }
		}
		
	}
}

//ghaefb