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
SystemInfo(SystemInfoHandler * handler)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
~SystemInfo()41 SystemInfo::~SystemInfo()
42 {
43 delete[] fCPUInfos;
44 }
45
46
47 uint64
CachedMemory() const48 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
BlockCacheMemory() const59 SystemInfo::BlockCacheMemory() const
60 {
61 return fSystemInfo.block_cache_pages * B_PAGE_SIZE;
62 }
63
64
65 uint64
UsedMemory() const66 SystemInfo::UsedMemory() const
67 {
68 return fSystemInfo.used_pages * B_PAGE_SIZE;
69 }
70
71
72 uint64
MaxMemory() const73 SystemInfo::MaxMemory() const
74 {
75 return fSystemInfo.max_pages * B_PAGE_SIZE;
76 }
77
78
79 uint32
PageFaults() const80 SystemInfo::PageFaults() const
81 {
82 return fSystemInfo.page_faults;
83 }
84
85
86 uint64
UsedSwapSpace() const87 SystemInfo::UsedSwapSpace() const
88 {
89 return (fSystemInfo.max_swap_pages - fSystemInfo.free_swap_pages)
90 * B_PAGE_SIZE;
91 }
92
93
94 uint64
MaxSwapSpace() const95 SystemInfo::MaxSwapSpace() const
96 {
97 return fSystemInfo.max_swap_pages * B_PAGE_SIZE;
98 }
99
100
101 uint32
UsedSemaphores() const102 SystemInfo::UsedSemaphores() const
103 {
104 return fSystemInfo.used_sems;
105 }
106
107
108 uint32
MaxSemaphores() const109 SystemInfo::MaxSemaphores() const
110 {
111 return fSystemInfo.max_sems;
112 }
113
114
115 uint32
UsedPorts() const116 SystemInfo::UsedPorts() const
117 {
118 return fSystemInfo.used_ports;
119 }
120
121
122 uint32
MaxPorts() const123 SystemInfo::MaxPorts() const
124 {
125 return fSystemInfo.max_ports;
126 }
127
128
129 uint32
UsedThreads() const130 SystemInfo::UsedThreads() const
131 {
132 return fSystemInfo.used_threads;
133 }
134
135
136 uint32
MaxThreads() const137 SystemInfo::MaxThreads() const
138 {
139 return fSystemInfo.max_threads;
140 }
141
142
143 uint32
UsedTeams() const144 SystemInfo::UsedTeams() const
145 {
146 return fSystemInfo.used_teams;
147 }
148
149
150 uint32
MaxTeams() const151 SystemInfo::MaxTeams() const
152 {
153 return fSystemInfo.max_teams;
154 }
155
156
157 void
_RetrieveNetwork()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
NetworkReceived()182 SystemInfo::NetworkReceived()
183 {
184 _RetrieveNetwork();
185 return fBytesReceived;
186 }
187
188
189 uint64
NetworkSent()190 SystemInfo::NetworkSent()
191 {
192 _RetrieveNetwork();
193 return fBytesSent;
194 }
195
196
197 uint32
UsedRunningApps() const198 SystemInfo::UsedRunningApps() const
199 {
200 return fRunningApps;
201 }
202
203
204 uint32
MaxRunningApps() const205 SystemInfo::MaxRunningApps() const
206 {
207 return fSystemInfo.max_teams;
208 }
209
210
211 uint32
ClipboardSize() const212 SystemInfo::ClipboardSize() const
213 {
214 return fClipboardSize;
215 }
216
217
218 uint32
ClipboardTextSize() const219 SystemInfo::ClipboardTextSize() const
220 {
221 return fClipboardTextSize;
222 }
223
224
225 uint32
MediaNodes() const226 SystemInfo::MediaNodes() const
227 {
228 return fMediaNodes;
229 }
230
231
232 uint32
MediaConnections() const233 SystemInfo::MediaConnections() const
234 {
235 return fMediaConnections;
236 }
237
238
239 uint32
MediaBuffers() const240 SystemInfo::MediaBuffers() const
241 {
242 return fMediaBuffers;
243 }
244
245
246