xref: /haiku/src/apps/activitymonitor/DataSource.cpp (revision cfc3fa87da824bdf593eb8b817a83b6376e77935)
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 "DataSource.h"
8 
9 #include <stdio.h>
10 
11 #include <OS.h>
12 #include <String.h>
13 
14 #include "SystemInfo.h"
15 
16 
17 const DataSource* kSources[] = {
18 	new UsedMemoryDataSource(),
19 	new CachedMemoryDataSource(),
20 	new ThreadsDataSource(),
21 	new CpuUsageDataSource(),
22 };
23 const size_t kSourcesCount = sizeof(kSources) / sizeof(kSources[0]);
24 
25 
26 DataSource::DataSource(int64 initialMin, int64 initialMax)
27 	:
28 	fMinimum(initialMin),
29 	fMaximum(initialMax),
30 	fInterval(1000000LL),
31 	fColor((rgb_color){200, 0, 0})
32 {
33 }
34 
35 
36 DataSource::DataSource()
37 	:
38 	fMinimum(0),
39 	fMaximum(100),
40 	fInterval(1000000LL),
41 	fColor((rgb_color){200, 0, 0})
42 {
43 }
44 
45 
46 DataSource::DataSource(const DataSource& other)
47 {
48 	fMinimum = other.fMinimum;
49 	fMaximum = other.fMaximum;
50 	fInterval = other.fInterval;
51 	fColor = other.fColor;
52 }
53 
54 
55 DataSource::~DataSource()
56 {
57 }
58 
59 
60 DataSource*
61 DataSource::Copy() const
62 {
63 	return NULL;
64 		// this class cannot be copied
65 }
66 
67 
68 int64
69 DataSource::Minimum() const
70 {
71 	return fMinimum;
72 }
73 
74 
75 int64
76 DataSource::Maximum() const
77 {
78 	return fMaximum;
79 }
80 
81 
82 bigtime_t
83 DataSource::RefreshInterval() const
84 {
85 	return fInterval;
86 }
87 
88 
89 void
90 DataSource::SetLimits(int64 min, int64 max)
91 {
92 	fMinimum = min;
93 	fMaximum = max;
94 }
95 
96 
97 void
98 DataSource::SetRefreshInterval(bigtime_t interval)
99 {
100 	fInterval = interval;
101 }
102 
103 
104 void
105 DataSource::SetColor(rgb_color color)
106 {
107 	fColor = color;
108 }
109 
110 
111 int64
112 DataSource::NextValue(SystemInfo& info)
113 {
114 	return 0;
115 }
116 
117 
118 void
119 DataSource::Print(BString& text, int64 value) const
120 {
121 	text = "";
122 	text << value;
123 }
124 
125 
126 const char*
127 DataSource::Label() const
128 {
129 	return "";
130 }
131 
132 
133 const char*
134 DataSource::Unit() const
135 {
136 	return "";
137 }
138 
139 
140 rgb_color
141 DataSource::Color() const
142 {
143 	return fColor;
144 }
145 
146 
147 bool
148 DataSource::AdaptiveScale() const
149 {
150 	return false;
151 }
152 
153 
154 /*static*/ int32
155 DataSource::CountSources()
156 {
157 	return kSourcesCount;
158 }
159 
160 
161 /*static*/ const DataSource*
162 DataSource::SourceAt(int32 index)
163 {
164 	if (index >= (int32)kSourcesCount || index < 0)
165 		return NULL;
166 
167 	return kSources[index];
168 }
169 
170 
171 //	#pragma mark -
172 
173 
174 MemoryDataSource::MemoryDataSource()
175 {
176 	SystemInfo info;
177 
178 	fMinimum = 0;
179 	fMaximum = info.MaxMemory();
180 }
181 
182 
183 MemoryDataSource::~MemoryDataSource()
184 {
185 }
186 
187 
188 void
189 MemoryDataSource::Print(BString& text, int64 value) const
190 {
191 	char buffer[32];
192 	snprintf(buffer, sizeof(buffer), "%.1g MB", value / 1048576.0);
193 
194 	text = buffer;
195 }
196 
197 
198 const char*
199 MemoryDataSource::Unit() const
200 {
201 	return "MB";
202 }
203 
204 
205 //	#pragma mark -
206 
207 
208 UsedMemoryDataSource::UsedMemoryDataSource()
209 {
210 }
211 
212 
213 UsedMemoryDataSource::~UsedMemoryDataSource()
214 {
215 }
216 
217 
218 DataSource*
219 UsedMemoryDataSource::Copy() const
220 {
221 	return new UsedMemoryDataSource(*this);
222 }
223 
224 
225 int64
226 UsedMemoryDataSource::NextValue(SystemInfo& info)
227 {
228 	return info.UsedMemory();
229 }
230 
231 
232 const char*
233 UsedMemoryDataSource::Label() const
234 {
235 	return "Available Memory";
236 }
237 
238 
239 //	#pragma mark -
240 
241 
242 CachedMemoryDataSource::CachedMemoryDataSource()
243 {
244 	fColor = (rgb_color){0, 200, 0};
245 }
246 
247 
248 CachedMemoryDataSource::~CachedMemoryDataSource()
249 {
250 }
251 
252 
253 DataSource*
254 CachedMemoryDataSource::Copy() const
255 {
256 	return new CachedMemoryDataSource(*this);
257 }
258 
259 
260 int64
261 CachedMemoryDataSource::NextValue(SystemInfo& info)
262 {
263 	return info.CachedMemory();
264 }
265 
266 
267 const char*
268 CachedMemoryDataSource::Label() const
269 {
270 	return "Cached Memory";
271 }
272 
273 
274 //	#pragma mark -
275 
276 
277 ThreadsDataSource::ThreadsDataSource()
278 {
279 	SystemInfo info;
280 
281 	fMinimum = 0;
282 	fMaximum = info.MaxThreads();
283 
284 	fColor = (rgb_color){0, 0, 200};
285 }
286 
287 
288 ThreadsDataSource::~ThreadsDataSource()
289 {
290 }
291 
292 
293 DataSource*
294 ThreadsDataSource::Copy() const
295 {
296 	return new ThreadsDataSource(*this);
297 }
298 
299 
300 int64
301 ThreadsDataSource::NextValue(SystemInfo& info)
302 {
303 	return info.UsedThreads();
304 }
305 
306 
307 const char*
308 ThreadsDataSource::Label() const
309 {
310 	return "Threads";
311 }
312 
313 
314 bool
315 ThreadsDataSource::AdaptiveScale() const
316 {
317 	return true;
318 }
319 
320 
321 //	#pragma mark -
322 
323 
324 CpuUsageDataSource::CpuUsageDataSource()
325 	:
326 	fPreviousActive(0),
327 	fPreviousTime(0)
328 {
329 	fMinimum = 0;
330 	fMaximum = 1000;
331 
332 	fColor = (rgb_color){200, 200, 0};
333 }
334 
335 
336 CpuUsageDataSource::CpuUsageDataSource(const CpuUsageDataSource& other)
337 {
338 	fPreviousActive = other.fPreviousActive;
339 	fPreviousTime = other.fPreviousTime;
340 }
341 
342 
343 CpuUsageDataSource::~CpuUsageDataSource()
344 {
345 }
346 
347 
348 DataSource*
349 CpuUsageDataSource::Copy() const
350 {
351 	return new CpuUsageDataSource(*this);
352 }
353 
354 
355 void
356 CpuUsageDataSource::Print(BString& text, int64 value) const
357 {
358 	char buffer[32];
359 	snprintf(buffer, sizeof(buffer), "%.1g%%", value / 10.0);
360 
361 	text = buffer;
362 }
363 
364 
365 int64
366 CpuUsageDataSource::NextValue(SystemInfo& info)
367 {
368 	int32 running = 0;
369 	bigtime_t active = 0;
370 
371 	for (int32 cpu = 0; cpu < info.Info().cpu_count; cpu++) {
372 		active += info.Info().cpu_infos[cpu].active_time;
373 		running++;
374 			// TODO: take disabled CPUs into account
375 	}
376 
377 	int64 percent = int64(1000.0 * (active - fPreviousActive)
378 		/ (running * (info.Time() - fPreviousTime)));
379 	if (percent < 0)
380 		percent = 0;
381 	if (percent > 1000)
382 		percent = 1000;
383 
384 	fPreviousActive = active;
385 	fPreviousTime = info.Time();
386 
387 	return percent;
388 }
389 
390 
391 const char*
392 CpuUsageDataSource::Label() const
393 {
394 	return "CPU Usage";
395 }
396