xref: /haiku/src/apps/activitymonitor/SystemInfo.cpp (revision 746cac055adc6ac3308c7bc2d29040fb95689cc9)
1 /*
2  * Copyright 2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "SystemInfo.h"
8 #include "SystemInfoHandler.h"
9 
10 #include <net/if.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/socket.h>
14 #include <sys/sockio.h>
15 #include <unistd.h>
16 
17 
18 SystemInfo::SystemInfo(SystemInfoHandler* handler)
19 	:
20 	fTime(system_time()),
21 	fRetrievedNetwork(false),
22 	fRunningApps(0),
23 	fClipboardSize(0),
24 	fClipboardTextSize(0),
25 	fMediaNodes(0),
26 	fMediaConnections(0),
27 	fMediaBuffers(0)
28 {
29 	get_system_info(&fSystemInfo);
30 
31 	if (handler != NULL) {
32 		fRunningApps = handler->RunningApps();
33 		fClipboardSize = handler->ClipboardSize();
34 		fClipboardTextSize = handler->ClipboardTextSize();
35 		fMediaNodes = handler->MediaNodes();
36 		fMediaConnections = handler->MediaConnections();
37 		fMediaBuffers = handler->MediaBuffers();
38 	}
39 }
40 
41 
42 SystemInfo::~SystemInfo()
43 {
44 }
45 
46 
47 uint64
48 SystemInfo::CachedMemory() const
49 {
50 #ifdef __HAIKU__
51 	return (uint64)fSystemInfo.cached_pages * B_PAGE_SIZE;
52 #else
53 	return 0LL;
54 #endif
55 }
56 
57 
58 uint64
59 SystemInfo::UsedMemory() const
60 {
61 	return (uint64)fSystemInfo.used_pages * B_PAGE_SIZE;
62 }
63 
64 
65 uint64
66 SystemInfo::MaxMemory() const
67 {
68 	return (uint64)fSystemInfo.max_pages * B_PAGE_SIZE;
69 }
70 
71 
72 uint32
73 SystemInfo::UsedSemaphores() const
74 {
75 	return fSystemInfo.used_sems;
76 }
77 
78 
79 uint32
80 SystemInfo::MaxSemaphores() const
81 {
82 	return fSystemInfo.max_sems;
83 }
84 
85 
86 uint32
87 SystemInfo::UsedPorts() const
88 {
89 	return fSystemInfo.used_ports;
90 }
91 
92 
93 uint32
94 SystemInfo::MaxPorts() const
95 {
96 	return fSystemInfo.max_ports;
97 }
98 
99 
100 uint32
101 SystemInfo::UsedThreads() const
102 {
103 	return fSystemInfo.used_threads;
104 }
105 
106 
107 uint32
108 SystemInfo::MaxThreads() const
109 {
110 	return fSystemInfo.max_threads;
111 }
112 
113 
114 uint32
115 SystemInfo::UsedTeams() const
116 {
117 	return fSystemInfo.used_teams;
118 }
119 
120 
121 uint32
122 SystemInfo::MaxTeams() const
123 {
124 	return fSystemInfo.max_teams;
125 }
126 
127 
128 void
129 SystemInfo::_RetrieveNetwork()
130 {
131 	if (fRetrievedNetwork)
132 		return;
133 
134 	fBytesReceived = 0;
135 	fBytesSent = 0;
136 	fRetrievedNetwork = true;
137 
138 	// we need a socket to talk to the networking stack
139 	int socket = ::socket(AF_INET, SOCK_DGRAM, 0);
140 	if (socket < 0)
141 		return;
142 
143 	// get a list of all interfaces
144 
145 	ifconf config;
146 	config.ifc_len = sizeof(config.ifc_value);
147 	if (ioctl(socket, SIOCGIFCOUNT, &config, sizeof(struct ifconf)) < 0
148 		|| config.ifc_value == 0) {
149 		close(socket);
150 		return;
151 	}
152 
153 	uint32 count = (uint32)config.ifc_value;
154 
155 	void *buffer = malloc(count * sizeof(struct ifreq));
156 	if (buffer == NULL) {
157 		close(socket);
158 		return;
159 	}
160 
161 	config.ifc_len = count * sizeof(struct ifreq);
162 	config.ifc_buf = buffer;
163 	if (ioctl(socket, SIOCGIFCONF, &config, sizeof(struct ifconf)) < 0) {
164 		close(socket);
165 		return;
166 	}
167 
168 	ifreq *interface = (ifreq *)buffer;
169 
170 	for (uint32 i = 0; i < count; i++) {
171 		ifreq request;
172 		strlcpy(request.ifr_name, interface->ifr_name, IF_NAMESIZE);
173 
174 #ifdef __HAIKU__
175 		if (ioctl(socket, SIOCGIFSTATS, &request, sizeof(struct ifreq)) == 0) {
176 			fBytesReceived += request.ifr_stats.receive.bytes;
177 			fBytesSent += request.ifr_stats.send.bytes;
178 		}
179 #endif
180 
181 		interface = (ifreq *)((addr_t)interface + IF_NAMESIZE
182 			+ interface->ifr_addr.sa_len);
183 	}
184 
185 	free(buffer);
186 	close(socket);
187 }
188 
189 
190 uint64
191 SystemInfo::NetworkReceived()
192 {
193 	_RetrieveNetwork();
194 	return fBytesReceived;
195 }
196 
197 
198 uint64
199 SystemInfo::NetworkSent()
200 {
201 	_RetrieveNetwork();
202 	return fBytesSent;
203 }
204 
205 
206 uint32
207 SystemInfo::UsedRunningApps() const
208 {
209 	return fRunningApps;
210 }
211 
212 
213 uint32
214 SystemInfo::MaxRunningApps() const
215 {
216 	return fSystemInfo.max_teams;
217 }
218 
219 
220 uint32
221 SystemInfo::ClipboardSize() const
222 {
223 	return fClipboardSize;
224 }
225 
226 
227 uint32
228 SystemInfo::ClipboardTextSize() const
229 {
230 	return fClipboardTextSize;
231 }
232 
233 
234 uint32
235 SystemInfo::MediaNodes() const
236 {
237 	return fMediaNodes;
238 }
239 
240 
241 uint32
242 SystemInfo::MediaConnections() const
243 {
244 	return fMediaConnections;
245 }
246 
247 
248 uint32
249 SystemInfo::MediaBuffers() const
250 {
251 	return fMediaBuffers;
252 }
253 
254 
255