xref: /haiku/src/apps/activitymonitor/SystemInfo.cpp (revision e81a954787e50e56a7f06f72705b7859b6ab06d1)
1 /*
2  * Copyright 2008-2010, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "SystemInfo.h"
8 
9 #include <NetworkInterface.h>
10 #include <NetworkRoster.h>
11 
12 #include "SystemInfoHandler.h"
13 
14 
15 SystemInfo::SystemInfo(SystemInfoHandler* handler)
16 	:
17 	fTime(system_time()),
18 	fRetrievedNetwork(false),
19 	fRunningApps(0),
20 	fClipboardSize(0),
21 	fClipboardTextSize(0),
22 	fMediaNodes(0),
23 	fMediaConnections(0),
24 	fMediaBuffers(0)
25 {
26 	get_system_info(&fSystemInfo);
27 	fCPUInfos = new cpu_info[fSystemInfo.cpu_count];
28 	get_cpu_info(0, fSystemInfo.cpu_count, fCPUInfos);
29 
30 	if (handler != NULL) {
31 		fRunningApps = handler->RunningApps();
32 		fClipboardSize = handler->ClipboardSize();
33 		fClipboardTextSize = handler->ClipboardTextSize();
34 		fMediaNodes = handler->MediaNodes();
35 		fMediaConnections = handler->MediaConnections();
36 		fMediaBuffers = handler->MediaBuffers();
37 	}
38 }
39 
40 
41 SystemInfo::~SystemInfo()
42 {
43 	delete[] fCPUInfos;
44 }
45 
46 
47 uint64
48 SystemInfo::CachedMemory() const
49 {
50 #ifdef __HAIKU__
51 	return fSystemInfo.cached_pages * B_PAGE_SIZE;
52 #else
53 	return 0LL;
54 #endif
55 }
56 
57 
58 uint64
59 SystemInfo::BlockCacheMemory() const
60 {
61 	return fSystemInfo.block_cache_pages * B_PAGE_SIZE;
62 }
63 
64 
65 uint64
66 SystemInfo::UsedMemory() const
67 {
68 	return fSystemInfo.used_pages * B_PAGE_SIZE;
69 }
70 
71 
72 uint64
73 SystemInfo::MaxMemory() const
74 {
75 	return fSystemInfo.max_pages * B_PAGE_SIZE;
76 }
77 
78 
79 uint32
80 SystemInfo::PageFaults() const
81 {
82 	return fSystemInfo.page_faults;
83 }
84 
85 
86 uint64
87 SystemInfo::UsedSwapSpace() const
88 {
89 	return (fSystemInfo.max_swap_pages - fSystemInfo.free_swap_pages)
90 		* B_PAGE_SIZE;
91 }
92 
93 
94 uint64
95 SystemInfo::MaxSwapSpace() const
96 {
97 	return fSystemInfo.max_swap_pages * B_PAGE_SIZE;
98 }
99 
100 
101 uint32
102 SystemInfo::UsedSemaphores() const
103 {
104 	return fSystemInfo.used_sems;
105 }
106 
107 
108 uint32
109 SystemInfo::MaxSemaphores() const
110 {
111 	return fSystemInfo.max_sems;
112 }
113 
114 
115 uint32
116 SystemInfo::UsedPorts() const
117 {
118 	return fSystemInfo.used_ports;
119 }
120 
121 
122 uint32
123 SystemInfo::MaxPorts() const
124 {
125 	return fSystemInfo.max_ports;
126 }
127 
128 
129 uint32
130 SystemInfo::UsedThreads() const
131 {
132 	return fSystemInfo.used_threads;
133 }
134 
135 
136 uint32
137 SystemInfo::MaxThreads() const
138 {
139 	return fSystemInfo.max_threads;
140 }
141 
142 
143 uint32
144 SystemInfo::UsedTeams() const
145 {
146 	return fSystemInfo.used_teams;
147 }
148 
149 
150 uint32
151 SystemInfo::MaxTeams() const
152 {
153 	return fSystemInfo.max_teams;
154 }
155 
156 
157 void
158 SystemInfo::_RetrieveNetwork()
159 {
160 	if (fRetrievedNetwork)
161 		return;
162 
163 	fBytesReceived = 0;
164 	fBytesSent = 0;
165 	fRetrievedNetwork = true;
166 
167 	BNetworkRoster& roster = BNetworkRoster::Default();
168 
169 	BNetworkInterface interface;
170 	uint32 cookie = 0;
171 	while (roster.GetNextInterface(&cookie, interface) == B_OK) {
172 		ifreq_stats stats;
173 		if (interface.GetStats(stats) == B_OK) {
174 			fBytesReceived += stats.receive.bytes;
175 			fBytesSent += stats.send.bytes;
176 		}
177 	}
178 }
179 
180 
181 uint64
182 SystemInfo::NetworkReceived()
183 {
184 	_RetrieveNetwork();
185 	return fBytesReceived;
186 }
187 
188 
189 uint64
190 SystemInfo::NetworkSent()
191 {
192 	_RetrieveNetwork();
193 	return fBytesSent;
194 }
195 
196 
197 uint32
198 SystemInfo::UsedRunningApps() const
199 {
200 	return fRunningApps;
201 }
202 
203 
204 uint32
205 SystemInfo::MaxRunningApps() const
206 {
207 	return fSystemInfo.max_teams;
208 }
209 
210 
211 uint32
212 SystemInfo::ClipboardSize() const
213 {
214 	return fClipboardSize;
215 }
216 
217 
218 uint32
219 SystemInfo::ClipboardTextSize() const
220 {
221 	return fClipboardTextSize;
222 }
223 
224 
225 uint32
226 SystemInfo::MediaNodes() const
227 {
228 	return fMediaNodes;
229 }
230 
231 
232 uint32
233 SystemInfo::MediaConnections() const
234 {
235 	return fMediaConnections;
236 }
237 
238 
239 uint32
240 SystemInfo::MediaBuffers() const
241 {
242 	return fMediaBuffers;
243 }
244 
245 
246