xref: /haiku/src/apps/activitymonitor/DataSource.cpp (revision 7eab6b486ebadb54ca3c306601f4b04dd92359fa)
1 /*
2  * Copyright 2008-2009, Axel Dörfler, axeld@pinc-software.de.
3  * Copyright 2024, Emir SARI, emir_sari@icloud.com.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 
8 #include "DataSource.h"
9 
10 #include <stdio.h>
11 #include <stdint.h>
12 
13 #include <Catalog.h>
14 #include <OS.h>
15 #include <String.h>
16 #include <StringForRate.h>
17 #include <StringForSize.h>
18 
19 #include "SystemInfo.h"
20 
21 #undef B_TRANSLATION_CONTEXT
22 #define B_TRANSLATION_CONTEXT "DataSource"
23 
24 
25 const DataSource* kSources[] = {
26 	new UsedMemoryDataSource(),
27 	new CachedMemoryDataSource(),
28 	new SwapSpaceDataSource(),
29 	new PageFaultsDataSource(),
30 	new CPUFrequencyDataSource(),
31 	new CPUUsageDataSource(),
32 	new CPUCombinedUsageDataSource(),
33 	new NetworkUsageDataSource(true),
34 	new NetworkUsageDataSource(false),
35 	new BlockCacheDataSource(),
36 	new SemaphoresDataSource(),
37 	new PortsDataSource(),
38 	new ThreadsDataSource(),
39 	new TeamsDataSource(),
40 	new RunningAppsDataSource(),
41 	new ClipboardSizeDataSource(false),
42 	new ClipboardSizeDataSource(true),
43 	new MediaNodesDataSource()
44 };
45 const size_t kSourcesCount = sizeof(kSources) / sizeof(kSources[0]);
46 
47 
DataSource(int64 initialMin,int64 initialMax)48 DataSource::DataSource(int64 initialMin, int64 initialMax)
49 	:
50 	fMinimum(initialMin),
51 	fMaximum(initialMax),
52 	fInterval(1000000LL),
53 	fColor((rgb_color){200, 0, 0})
54 {
55 }
56 
57 
DataSource()58 DataSource::DataSource()
59 	:
60 	fMinimum(0),
61 	fMaximum(100),
62 	fInterval(1000000LL),
63 	fColor((rgb_color){200, 0, 0})
64 {
65 }
66 
67 
DataSource(const DataSource & other)68 DataSource::DataSource(const DataSource& other)
69 {
70 	fMinimum = other.fMinimum;
71 	fMaximum = other.fMaximum;
72 	fInterval = other.fInterval;
73 	fColor = other.fColor;
74 }
75 
76 
~DataSource()77 DataSource::~DataSource()
78 {
79 }
80 
81 
82 DataSource*
Copy() const83 DataSource::Copy() const
84 {
85 	return NULL;
86 		// this class cannot be copied
87 }
88 
89 
90 DataSource*
CopyForCPU(int32 cpu) const91 DataSource::CopyForCPU(int32 cpu) const
92 {
93 	return Copy();
94 }
95 
96 
97 int64
Minimum() const98 DataSource::Minimum() const
99 {
100 	return fMinimum;
101 }
102 
103 
104 int64
Maximum() const105 DataSource::Maximum() const
106 {
107 	return fMaximum;
108 }
109 
110 
111 bigtime_t
RefreshInterval() const112 DataSource::RefreshInterval() const
113 {
114 	return fInterval;
115 }
116 
117 
118 void
SetLimits(int64 min,int64 max)119 DataSource::SetLimits(int64 min, int64 max)
120 {
121 	fMinimum = min;
122 	fMaximum = max;
123 }
124 
125 
126 void
SetRefreshInterval(bigtime_t interval)127 DataSource::SetRefreshInterval(bigtime_t interval)
128 {
129 	fInterval = interval;
130 }
131 
132 
133 void
SetColor(rgb_color color)134 DataSource::SetColor(rgb_color color)
135 {
136 	fColor = color;
137 }
138 
139 
140 int64
NextValue(SystemInfo & info)141 DataSource::NextValue(SystemInfo& info)
142 {
143 	return 0;
144 }
145 
146 
147 void
Print(BString & text,int64 value) const148 DataSource::Print(BString& text, int64 value) const
149 {
150 	text = "";
151 	fNumberFormat.Format(text, (int32)value);
152 }
153 
154 
155 const char*
ShortLabel() const156 DataSource::ShortLabel() const
157 {
158 	return Label();
159 }
160 
161 
162 const char*
Name() const163 DataSource::Name() const
164 {
165 	return Label();
166 }
167 
168 
169 const char*
Label() const170 DataSource::Label() const
171 {
172 	return "";
173 }
174 
175 
176 const char*
Unit() const177 DataSource::Unit() const
178 {
179 	return "";
180 }
181 
182 
183 rgb_color
Color() const184 DataSource::Color() const
185 {
186 	return fColor;
187 }
188 
189 
190 bool
AdaptiveScale() const191 DataSource::AdaptiveScale() const
192 {
193 	return false;
194 }
195 
196 
197 scale_type
ScaleType() const198 DataSource::ScaleType() const
199 {
200 	return kNoScale;
201 }
202 
203 
204 int32
CPU() const205 DataSource::CPU() const
206 {
207 	return 0;
208 }
209 
210 
211 bool
PerCPU() const212 DataSource::PerCPU() const
213 {
214 	return false;
215 }
216 
217 
218 bool
MultiCPUOnly() const219 DataSource::MultiCPUOnly() const
220 {
221 	return false;
222 }
223 
224 
225 bool
Primary() const226 DataSource::Primary() const
227 {
228 	return false;
229 }
230 
231 
232 /*static*/ int32
CountSources()233 DataSource::CountSources()
234 {
235 	return kSourcesCount;
236 }
237 
238 
239 /*static*/ const DataSource*
SourceAt(int32 index)240 DataSource::SourceAt(int32 index)
241 {
242 	if (index >= (int32)kSourcesCount || index < 0)
243 		return NULL;
244 
245 	return kSources[index];
246 }
247 
248 
249 /*static*/ const DataSource*
FindSource(const char * internalName)250 DataSource::FindSource(const char* internalName)
251 {
252 	for (uint32 i = 0; i < kSourcesCount; i++) {
253 		const DataSource* source = kSources[i];
254 		if (!strcmp(source->InternalName(), internalName))
255 			return source;
256 	}
257 
258 	return NULL;
259 }
260 
261 
262 /*static*/ int32
IndexOf(const DataSource * source)263 DataSource::IndexOf(const DataSource* source)
264 {
265 	const char* name = source->Name();
266 
267 	for (uint32 i = 0; i < kSourcesCount; i++) {
268 		if (!strcmp(kSources[i]->Name(), name))
269 			return i;
270 	}
271 
272 	return -1;
273 }
274 
275 
276 //	#pragma mark -
277 
278 
MemoryDataSource()279 MemoryDataSource::MemoryDataSource()
280 {
281 	SystemInfo info;
282 
283 	fMinimum = 0;
284 	fMaximum = info.MaxMemory();
285 }
286 
287 
~MemoryDataSource()288 MemoryDataSource::~MemoryDataSource()
289 {
290 }
291 
292 
293 void
Print(BString & text,int64 value) const294 MemoryDataSource::Print(BString& text, int64 value) const
295 {
296 	char buffer[32];
297 	string_for_size(value, buffer, sizeof(buffer));
298 	text = buffer;
299 }
300 
301 
302 //	#pragma mark -
303 
304 
UsedMemoryDataSource()305 UsedMemoryDataSource::UsedMemoryDataSource()
306 {
307 }
308 
309 
~UsedMemoryDataSource()310 UsedMemoryDataSource::~UsedMemoryDataSource()
311 {
312 }
313 
314 
315 DataSource*
Copy() const316 UsedMemoryDataSource::Copy() const
317 {
318 	return new UsedMemoryDataSource(*this);
319 }
320 
321 
322 int64
NextValue(SystemInfo & info)323 UsedMemoryDataSource::NextValue(SystemInfo& info)
324 {
325 	return info.UsedMemory();
326 }
327 
328 
329 const char*
InternalName() const330 UsedMemoryDataSource::InternalName() const
331 {
332 	return "Used memory";
333 }
334 
335 
336 const char*
Label() const337 UsedMemoryDataSource::Label() const
338 {
339 	return B_TRANSLATE("Used memory");
340 }
341 
342 
343 const char*
ShortLabel() const344 UsedMemoryDataSource::ShortLabel() const
345 {
346 	return B_TRANSLATE("Memory");
347 }
348 
349 
350 bool
Primary() const351 UsedMemoryDataSource::Primary() const
352 {
353 	return true;
354 }
355 
356 
357 //	#pragma mark -
358 
359 
CachedMemoryDataSource()360 CachedMemoryDataSource::CachedMemoryDataSource()
361 {
362 	fColor = (rgb_color){0, 200, 0};
363 }
364 
365 
~CachedMemoryDataSource()366 CachedMemoryDataSource::~CachedMemoryDataSource()
367 {
368 }
369 
370 
371 DataSource*
Copy() const372 CachedMemoryDataSource::Copy() const
373 {
374 	return new CachedMemoryDataSource(*this);
375 }
376 
377 
378 int64
NextValue(SystemInfo & info)379 CachedMemoryDataSource::NextValue(SystemInfo& info)
380 {
381 	return info.CachedMemory();
382 }
383 
384 
385 const char*
InternalName() const386 CachedMemoryDataSource::InternalName() const
387 {
388 	return "Cached memory";
389 }
390 
391 
392 const char*
Label() const393 CachedMemoryDataSource::Label() const
394 {
395 	return B_TRANSLATE("Cached memory");
396 }
397 
398 
399 const char*
ShortLabel() const400 CachedMemoryDataSource::ShortLabel() const
401 {
402 	return B_TRANSLATE("Cache");
403 }
404 
405 
406 bool
Primary() const407 CachedMemoryDataSource::Primary() const
408 {
409 	return true;
410 }
411 
412 
413 //	#pragma mark -
414 
415 
SwapSpaceDataSource()416 SwapSpaceDataSource::SwapSpaceDataSource()
417 {
418 	SystemInfo info;
419 
420 	fColor = (rgb_color){0, 120, 0};
421 	fMaximum = info.MaxSwapSpace();
422 }
423 
424 
~SwapSpaceDataSource()425 SwapSpaceDataSource::~SwapSpaceDataSource()
426 {
427 }
428 
429 
430 DataSource*
Copy() const431 SwapSpaceDataSource::Copy() const
432 {
433 	return new SwapSpaceDataSource(*this);
434 }
435 
436 
437 int64
NextValue(SystemInfo & info)438 SwapSpaceDataSource::NextValue(SystemInfo& info)
439 {
440 	return info.UsedSwapSpace();
441 }
442 
443 
444 const char*
InternalName() const445 SwapSpaceDataSource::InternalName() const
446 {
447 	return "Swap space";
448 }
449 
450 
451 const char*
Label() const452 SwapSpaceDataSource::Label() const
453 {
454 	return B_TRANSLATE("Swap space");
455 }
456 
457 
458 const char*
ShortLabel() const459 SwapSpaceDataSource::ShortLabel() const
460 {
461 	return B_TRANSLATE("Swap");
462 }
463 
464 
465 bool
Primary() const466 SwapSpaceDataSource::Primary() const
467 {
468 	return true;
469 }
470 
471 
472 //	#pragma mark -
473 
474 
BlockCacheDataSource()475 BlockCacheDataSource::BlockCacheDataSource()
476 {
477 	fColor = (rgb_color){0, 0, 120};
478 }
479 
480 
~BlockCacheDataSource()481 BlockCacheDataSource::~BlockCacheDataSource()
482 {
483 }
484 
485 
486 DataSource*
Copy() const487 BlockCacheDataSource::Copy() const
488 {
489 	return new BlockCacheDataSource(*this);
490 }
491 
492 
493 int64
NextValue(SystemInfo & info)494 BlockCacheDataSource::NextValue(SystemInfo& info)
495 {
496 	return info.BlockCacheMemory();
497 }
498 
499 
500 const char*
InternalName() const501 BlockCacheDataSource::InternalName() const
502 {
503 	return "Block cache memory";
504 }
505 
506 
507 const char*
Label() const508 BlockCacheDataSource::Label() const
509 {
510 	return B_TRANSLATE("Block cache memory");
511 }
512 
513 
514 const char*
ShortLabel() const515 BlockCacheDataSource::ShortLabel() const
516 {
517 	return B_TRANSLATE("Block cache");
518 }
519 
520 
521 //	#pragma mark -
522 
523 
SemaphoresDataSource()524 SemaphoresDataSource::SemaphoresDataSource()
525 {
526 	SystemInfo info;
527 
528 	fMinimum = 0;
529 	fMaximum = info.MaxSemaphores();
530 
531 	fColor = (rgb_color){100, 200, 100};
532 }
533 
534 
~SemaphoresDataSource()535 SemaphoresDataSource::~SemaphoresDataSource()
536 {
537 }
538 
539 
540 DataSource*
Copy() const541 SemaphoresDataSource::Copy() const
542 {
543 	return new SemaphoresDataSource(*this);
544 }
545 
546 
547 int64
NextValue(SystemInfo & info)548 SemaphoresDataSource::NextValue(SystemInfo& info)
549 {
550 	return info.UsedSemaphores();
551 }
552 
553 
554 const char*
InternalName() const555 SemaphoresDataSource::InternalName() const
556 {
557 	return "Semaphores";
558 }
559 
560 
561 const char*
Label() const562 SemaphoresDataSource::Label() const
563 {
564 	return B_TRANSLATE("Semaphores");
565 }
566 
567 
568 const char*
ShortLabel() const569 SemaphoresDataSource::ShortLabel() const
570 {
571 	return B_TRANSLATE("Sems");
572 }
573 
574 
575 bool
AdaptiveScale() const576 SemaphoresDataSource::AdaptiveScale() const
577 {
578 	return true;
579 }
580 
581 
582 //	#pragma mark -
583 
584 
PortsDataSource()585 PortsDataSource::PortsDataSource()
586 {
587 	SystemInfo info;
588 
589 	fMinimum = 0;
590 	fMaximum = info.MaxPorts();
591 
592 	fColor = (rgb_color){180, 200, 180};
593 }
594 
595 
~PortsDataSource()596 PortsDataSource::~PortsDataSource()
597 {
598 }
599 
600 
601 DataSource*
Copy() const602 PortsDataSource::Copy() const
603 {
604 	return new PortsDataSource(*this);
605 }
606 
607 
608 int64
NextValue(SystemInfo & info)609 PortsDataSource::NextValue(SystemInfo& info)
610 {
611 	return info.UsedPorts();
612 }
613 
614 
615 const char*
InternalName() const616 PortsDataSource::InternalName() const
617 {
618 	return "Ports";
619 }
620 
621 
622 const char*
Label() const623 PortsDataSource::Label() const
624 {
625 	return B_TRANSLATE("Ports");
626 }
627 
628 
629 bool
AdaptiveScale() const630 PortsDataSource::AdaptiveScale() const
631 {
632 	return true;
633 }
634 
635 
636 //	#pragma mark -
637 
638 
ThreadsDataSource()639 ThreadsDataSource::ThreadsDataSource()
640 {
641 	SystemInfo info;
642 
643 	fMinimum = 0;
644 	fMaximum = info.MaxThreads();
645 
646 	fColor = (rgb_color){0, 0, 200};
647 }
648 
649 
~ThreadsDataSource()650 ThreadsDataSource::~ThreadsDataSource()
651 {
652 }
653 
654 
655 DataSource*
Copy() const656 ThreadsDataSource::Copy() const
657 {
658 	return new ThreadsDataSource(*this);
659 }
660 
661 
662 int64
NextValue(SystemInfo & info)663 ThreadsDataSource::NextValue(SystemInfo& info)
664 {
665 	return info.UsedThreads();
666 }
667 
668 
669 const char*
InternalName() const670 ThreadsDataSource::InternalName() const
671 {
672 	return "Threads";
673 }
674 
675 
676 const char*
Label() const677 ThreadsDataSource::Label() const
678 {
679 	return B_TRANSLATE("Threads");
680 }
681 
682 
683 bool
AdaptiveScale() const684 ThreadsDataSource::AdaptiveScale() const
685 {
686 	return true;
687 }
688 
689 
690 //	#pragma mark -
691 
692 
TeamsDataSource()693 TeamsDataSource::TeamsDataSource()
694 {
695 	SystemInfo info;
696 
697 	fMinimum = 0;
698 	fMaximum = info.MaxTeams();
699 
700 	fColor = (rgb_color){0, 150, 255};
701 }
702 
703 
~TeamsDataSource()704 TeamsDataSource::~TeamsDataSource()
705 {
706 }
707 
708 
709 DataSource*
Copy() const710 TeamsDataSource::Copy() const
711 {
712 	return new TeamsDataSource(*this);
713 }
714 
715 
716 int64
NextValue(SystemInfo & info)717 TeamsDataSource::NextValue(SystemInfo& info)
718 {
719 	return info.UsedTeams();
720 }
721 
722 
723 const char*
InternalName() const724 TeamsDataSource::InternalName() const
725 {
726 	return "Teams";
727 }
728 
729 
730 const char*
Label() const731 TeamsDataSource::Label() const
732 {
733 	return B_TRANSLATE("Teams");
734 }
735 
736 
737 bool
AdaptiveScale() const738 TeamsDataSource::AdaptiveScale() const
739 {
740 	return true;
741 }
742 
743 
744 //	#pragma mark -
745 
746 
RunningAppsDataSource()747 RunningAppsDataSource::RunningAppsDataSource()
748 {
749 	SystemInfo info;
750 
751 	fMinimum = 0;
752 	fMaximum = info.MaxRunningApps();
753 
754 	fColor = (rgb_color){100, 150, 255};
755 }
756 
757 
~RunningAppsDataSource()758 RunningAppsDataSource::~RunningAppsDataSource()
759 {
760 }
761 
762 
763 DataSource*
Copy() const764 RunningAppsDataSource::Copy() const
765 {
766 	return new RunningAppsDataSource(*this);
767 }
768 
769 
770 int64
NextValue(SystemInfo & info)771 RunningAppsDataSource::NextValue(SystemInfo& info)
772 {
773 	return info.UsedRunningApps();
774 }
775 
776 
777 const char*
InternalName() const778 RunningAppsDataSource::InternalName() const
779 {
780 	return "Running applications";
781 }
782 
783 
784 const char*
Label() const785 RunningAppsDataSource::Label() const
786 {
787 	return B_TRANSLATE("Running applications");
788 }
789 
790 
791 const char*
ShortLabel() const792 RunningAppsDataSource::ShortLabel() const
793 {
794 	return B_TRANSLATE("Apps");
795 }
796 
797 
798 bool
AdaptiveScale() const799 RunningAppsDataSource::AdaptiveScale() const
800 {
801 	return true;
802 }
803 
804 
805 //	#pragma mark -
806 
807 
CPUFrequencyDataSource(int32 cpu)808 CPUFrequencyDataSource::CPUFrequencyDataSource(int32 cpu)
809 {
810 	fMinimum = 0;
811 	fMaximum = 1000000000ll;
812 		// Maximum initially set at 1GHz, will be automatically raised if the actual frequency gets
813 		// higher than that
814 
815 	_SetCPU(cpu);
816 }
817 
818 
CPUFrequencyDataSource(const CPUFrequencyDataSource & other)819 CPUFrequencyDataSource::CPUFrequencyDataSource(const CPUFrequencyDataSource& other)
820 	: DataSource(other)
821 {
822 	fCPU = other.fCPU;
823 	fLabel = other.fLabel;
824 	fShortLabel = other.fShortLabel;
825 }
826 
827 
~CPUFrequencyDataSource()828 CPUFrequencyDataSource::~CPUFrequencyDataSource()
829 {
830 }
831 
832 
833 DataSource*
Copy() const834 CPUFrequencyDataSource::Copy() const
835 {
836 	return new CPUFrequencyDataSource(*this);
837 }
838 
839 
840 DataSource*
CopyForCPU(int32 cpu) const841 CPUFrequencyDataSource::CopyForCPU(int32 cpu) const
842 {
843 	CPUFrequencyDataSource* copy = new CPUFrequencyDataSource(*this);
844 	copy->_SetCPU(cpu);
845 
846 	return copy;
847 }
848 
849 
850 void
Print(BString & text,int64 value) const851 CPUFrequencyDataSource::Print(BString& text, int64 value) const
852 {
853 	BString printedFrequency;
854 	fNumberFormat.Format(printedFrequency, (int32)(value / 1000000));
855 	text.SetToFormat("%s MHz", printedFrequency.String());
856 }
857 
858 
859 int64
NextValue(SystemInfo & info)860 CPUFrequencyDataSource::NextValue(SystemInfo& info)
861 {
862 	int64 value = info.CPUCurrentFrequency(fCPU);
863 
864 	if (value > fMaximum)
865 		SetLimits(0, value);
866 
867 	return value;
868 }
869 
870 
871 const char*
Label() const872 CPUFrequencyDataSource::Label() const
873 {
874 	return fLabel.String();
875 }
876 
877 
878 const char*
ShortLabel() const879 CPUFrequencyDataSource::ShortLabel() const
880 {
881 	return fShortLabel.String();
882 }
883 
884 
885 const char*
InternalName() const886 CPUFrequencyDataSource::InternalName() const
887 {
888 	return "CPU speed";
889 }
890 
891 
892 const char*
Name() const893 CPUFrequencyDataSource::Name() const
894 {
895 	return B_TRANSLATE("CPU speed");
896 }
897 
898 
899 int32
CPU() const900 CPUFrequencyDataSource::CPU() const
901 {
902 	return fCPU;
903 }
904 
905 
906 bool
PerCPU() const907 CPUFrequencyDataSource::PerCPU() const
908 {
909 	return true;
910 }
911 
912 
913 bool
Primary() const914 CPUFrequencyDataSource::Primary() const
915 {
916 	return true;
917 }
918 
919 
920 void
_SetCPU(int32 cpu)921 CPUFrequencyDataSource::_SetCPU(int32 cpu)
922 {
923 	fCPU = cpu;
924 
925 	if (SystemInfo().CPUCount() > 1) {
926 		fLabel.SetToFormat(B_TRANSLATE("CPU %d speed"), (int)cpu + 1);
927 		fShortLabel.SetToFormat(B_TRANSLATE("CPU %d"), (int)cpu + 1);
928 	} else {
929 		fLabel = B_TRANSLATE("CPU usage");
930 		fShortLabel = B_TRANSLATE("CPU");
931 	}
932 
933 	const rgb_color kColors[] = {
934 		// TODO: find some better defaults...
935 		{200, 0, 200},
936 		{0, 200, 200},
937 		{80, 80, 80},
938 		{230, 150, 50},
939 		{255, 0, 0},
940 		{0, 255, 0},
941 		{0, 0, 255},
942 		{0, 150, 230}
943 	};
944 	const uint32 kNumColors = B_COUNT_OF(kColors);
945 
946 	fColor = kColors[cpu % kNumColors];
947 }
948 
949 
950 //	#pragma mark -
951 
952 
CPUUsageDataSource(int32 cpu)953 CPUUsageDataSource::CPUUsageDataSource(int32 cpu)
954 	:
955 	fPreviousActive(0),
956 	fPreviousTime(0)
957 {
958 	fMinimum = 0;
959 	fMaximum = 1000;
960 
961 	_SetCPU(cpu);
962 }
963 
964 
CPUUsageDataSource(const CPUUsageDataSource & other)965 CPUUsageDataSource::CPUUsageDataSource(const CPUUsageDataSource& other)
966 	: DataSource(other)
967 {
968 	fPreviousActive = other.fPreviousActive;
969 	fPreviousTime = other.fPreviousTime;
970 	fCPU = other.fCPU;
971 	fLabel = other.fLabel;
972 	fShortLabel = other.fShortLabel;
973 }
974 
975 
~CPUUsageDataSource()976 CPUUsageDataSource::~CPUUsageDataSource()
977 {
978 }
979 
980 
981 DataSource*
Copy() const982 CPUUsageDataSource::Copy() const
983 {
984 	return new CPUUsageDataSource(*this);
985 }
986 
987 
988 DataSource*
CopyForCPU(int32 cpu) const989 CPUUsageDataSource::CopyForCPU(int32 cpu) const
990 {
991 	CPUUsageDataSource* copy = new CPUUsageDataSource(*this);
992 	copy->_SetCPU(cpu);
993 
994 	return copy;
995 }
996 
997 
998 void
Print(BString & text,int64 value) const999 CPUUsageDataSource::Print(BString& text, int64 value) const
1000 {
1001 	text = "";
1002 	fNumberFormat.SetPrecision(1);
1003 	fNumberFormat.FormatPercent(text, value / 1000.0);
1004 }
1005 
1006 
1007 int64
NextValue(SystemInfo & info)1008 CPUUsageDataSource::NextValue(SystemInfo& info)
1009 {
1010 	bigtime_t active = info.CPUActiveTime(fCPU);
1011 
1012 	int64 percent = int64(1000.0 * (active - fPreviousActive)
1013 		/ (info.Time() - fPreviousTime));
1014 	if (percent < 0)
1015 		percent = 0;
1016 	if (percent > 1000)
1017 		percent = 1000;
1018 
1019 	fPreviousActive = active;
1020 	fPreviousTime = info.Time();
1021 
1022 	return percent;
1023 }
1024 
1025 
1026 const char*
Label() const1027 CPUUsageDataSource::Label() const
1028 {
1029 	return fLabel.String();
1030 }
1031 
1032 
1033 const char*
ShortLabel() const1034 CPUUsageDataSource::ShortLabel() const
1035 {
1036 	return fShortLabel.String();
1037 }
1038 
1039 
1040 const char*
InternalName() const1041 CPUUsageDataSource::InternalName() const
1042 {
1043 	return "CPU usage";
1044 }
1045 
1046 
1047 const char*
Name() const1048 CPUUsageDataSource::Name() const
1049 {
1050 	return B_TRANSLATE("CPU usage");
1051 }
1052 
1053 
1054 int32
CPU() const1055 CPUUsageDataSource::CPU() const
1056 {
1057 	return fCPU;
1058 }
1059 
1060 
1061 bool
PerCPU() const1062 CPUUsageDataSource::PerCPU() const
1063 {
1064 	return true;
1065 }
1066 
1067 
1068 bool
Primary() const1069 CPUUsageDataSource::Primary() const
1070 {
1071 	return true;
1072 }
1073 
1074 
1075 void
_SetCPU(int32 cpu)1076 CPUUsageDataSource::_SetCPU(int32 cpu)
1077 {
1078 	fCPU = cpu;
1079 
1080 	if (SystemInfo().CPUCount() > 1) {
1081 		fLabel.SetToFormat(B_TRANSLATE("CPU %d usage"), (int)cpu + 1);
1082 		fShortLabel.SetToFormat(B_TRANSLATE("CPU %d"), (int)cpu + 1);
1083 
1084 	} else {
1085 		fLabel = B_TRANSLATE("CPU usage");
1086 		fShortLabel = B_TRANSLATE("CPU");
1087 	}
1088 
1089 	const rgb_color kColors[] = {
1090 		// TODO: find some better defaults...
1091 		{200, 0, 200},
1092 		{0, 200, 200},
1093 		{80, 80, 80},
1094 		{230, 150, 50},
1095 		{255, 0, 0},
1096 		{0, 255, 0},
1097 		{0, 0, 255},
1098 		{0, 150, 230}
1099 	};
1100 	const uint32 kNumColors = B_COUNT_OF(kColors);
1101 
1102 	fColor = kColors[cpu % kNumColors];
1103 }
1104 
1105 
1106 //	#pragma mark -
1107 
1108 
CPUCombinedUsageDataSource()1109 CPUCombinedUsageDataSource::CPUCombinedUsageDataSource()
1110 	:
1111 	fPreviousActive(0),
1112 	fPreviousTime(0)
1113 {
1114 	fMinimum = 0;
1115 	fMaximum = 1000;
1116 
1117 	fColor = (rgb_color){200, 200, 0};
1118 }
1119 
1120 
CPUCombinedUsageDataSource(const CPUCombinedUsageDataSource & other)1121 CPUCombinedUsageDataSource::CPUCombinedUsageDataSource(
1122 		const CPUCombinedUsageDataSource& other)
1123 	: DataSource(other)
1124 {
1125 	fPreviousActive = other.fPreviousActive;
1126 	fPreviousTime = other.fPreviousTime;
1127 }
1128 
1129 
~CPUCombinedUsageDataSource()1130 CPUCombinedUsageDataSource::~CPUCombinedUsageDataSource()
1131 {
1132 }
1133 
1134 
1135 DataSource*
Copy() const1136 CPUCombinedUsageDataSource::Copy() const
1137 {
1138 	return new CPUCombinedUsageDataSource(*this);
1139 }
1140 
1141 
1142 void
Print(BString & text,int64 value) const1143 CPUCombinedUsageDataSource::Print(BString& text, int64 value) const
1144 {
1145 	text = "";
1146 	fNumberFormat.SetPrecision(1);
1147 	fNumberFormat.FormatPercent(text, value / 1000.0);
1148 }
1149 
1150 
1151 int64
NextValue(SystemInfo & info)1152 CPUCombinedUsageDataSource::NextValue(SystemInfo& info)
1153 {
1154 	int32 running = 0;
1155 	bigtime_t active = 0;
1156 
1157 	for (uint32 cpu = 0; cpu < info.CPUCount(); cpu++) {
1158 		active += info.CPUActiveTime(cpu);
1159 		running++;
1160 			// TODO: take disabled CPUs into account
1161 	}
1162 
1163 	int64 percent = int64(1000.0 * (active - fPreviousActive)
1164 		/ (running * (info.Time() - fPreviousTime)));
1165 	if (percent < 0)
1166 		percent = 0;
1167 	if (percent > 1000)
1168 		percent = 1000;
1169 
1170 	fPreviousActive = active;
1171 	fPreviousTime = info.Time();
1172 
1173 	return percent;
1174 }
1175 
1176 
1177 const char*
Label() const1178 CPUCombinedUsageDataSource::Label() const
1179 {
1180 	return B_TRANSLATE("CPU usage");
1181 }
1182 
1183 
1184 const char*
ShortLabel() const1185 CPUCombinedUsageDataSource::ShortLabel() const
1186 {
1187 	return B_TRANSLATE("CPU");
1188 }
1189 
1190 
1191 const char*
InternalName() const1192 CPUCombinedUsageDataSource::InternalName() const
1193 {
1194 	return "CPU usage (combined)";
1195 }
1196 
1197 
1198 const char*
Name() const1199 CPUCombinedUsageDataSource::Name() const
1200 {
1201 	return B_TRANSLATE("CPU usage (combined)");
1202 }
1203 
1204 
1205 bool
MultiCPUOnly() const1206 CPUCombinedUsageDataSource::MultiCPUOnly() const
1207 {
1208 	return true;
1209 }
1210 
1211 
1212 bool
Primary() const1213 CPUCombinedUsageDataSource::Primary() const
1214 {
1215 	return true;
1216 }
1217 
1218 
1219 //	#pragma mark -
1220 
1221 
PageFaultsDataSource()1222 PageFaultsDataSource::PageFaultsDataSource()
1223 	:
1224 	fPreviousFaults(0),
1225 	fPreviousTime(0)
1226 {
1227 	SystemInfo info;
1228 	NextValue(info);
1229 
1230 	fMinimum = 0;
1231 	fMaximum = 1000000000LL;
1232 
1233 	fColor = (rgb_color){200, 0, 150, 0};
1234 }
1235 
1236 
PageFaultsDataSource(const PageFaultsDataSource & other)1237 PageFaultsDataSource::PageFaultsDataSource(const PageFaultsDataSource& other)
1238 	: DataSource(other)
1239 {
1240 	fPreviousFaults = other.fPreviousFaults;
1241 	fPreviousTime = other.fPreviousTime;
1242 }
1243 
1244 
~PageFaultsDataSource()1245 PageFaultsDataSource::~PageFaultsDataSource()
1246 {
1247 }
1248 
1249 
1250 DataSource*
Copy() const1251 PageFaultsDataSource::Copy() const
1252 {
1253 	return new PageFaultsDataSource(*this);
1254 }
1255 
1256 
1257 void
Print(BString & text,int64 value) const1258 PageFaultsDataSource::Print(BString& text, int64 value) const
1259 {
1260 	BString printedPageFaults;
1261 	fNumberFormat.SetPrecision(1);
1262 	fNumberFormat.Format(printedPageFaults, value / 1024.0);
1263 	text.SetToFormat(B_TRANSLATE("%s faults/s"), printedPageFaults.String());
1264 }
1265 
1266 
1267 int64
NextValue(SystemInfo & info)1268 PageFaultsDataSource::NextValue(SystemInfo& info)
1269 {
1270 	uint64 faults = info.PageFaults();
1271 
1272 	int64 faultsPerSecond = uint64(1024 * double(faults - fPreviousFaults)
1273 		/ (info.Time() - fPreviousTime) * 1000000.0);
1274 
1275 	fPreviousFaults = faults;
1276 	fPreviousTime = info.Time();
1277 
1278 	return faultsPerSecond;
1279 }
1280 
1281 
1282 const char*
Label() const1283 PageFaultsDataSource::Label() const
1284 {
1285 	return B_TRANSLATE("Page faults");
1286 }
1287 
1288 
1289 const char*
ShortLabel() const1290 PageFaultsDataSource::ShortLabel() const
1291 {
1292 	return B_TRANSLATE("P-faults");
1293 }
1294 
1295 
1296 const char*
InternalName() const1297 PageFaultsDataSource::InternalName() const
1298 {
1299 	return "Page faults";
1300 }
1301 
1302 
1303 const char*
Name() const1304 PageFaultsDataSource::Name() const
1305 {
1306 	return B_TRANSLATE("Page faults");
1307 }
1308 
1309 
1310 bool
AdaptiveScale() const1311 PageFaultsDataSource::AdaptiveScale() const
1312 {
1313 	return true;
1314 }
1315 
1316 
1317 bool
Primary() const1318 PageFaultsDataSource::Primary() const
1319 {
1320 	return false;
1321 }
1322 
1323 
1324 //	#pragma mark -
1325 
1326 
NetworkUsageDataSource(bool in)1327 NetworkUsageDataSource::NetworkUsageDataSource(bool in)
1328 	:
1329 	fIn(in),
1330 	fPreviousBytes(0),
1331 	fPreviousTime(0)
1332 {
1333 	SystemInfo info;
1334 	NextValue(info);
1335 
1336 	fMinimum = 0;
1337 	fMaximum = 1000000000LL;
1338 
1339 	fColor = fIn ? (rgb_color){200, 150, 0} : (rgb_color){200, 220, 0};
1340 }
1341 
1342 
NetworkUsageDataSource(const NetworkUsageDataSource & other)1343 NetworkUsageDataSource::NetworkUsageDataSource(
1344 		const NetworkUsageDataSource& other)
1345 	: DataSource(other)
1346 {
1347 	fIn = other.fIn;
1348 	fPreviousBytes = other.fPreviousBytes;
1349 	fPreviousTime = other.fPreviousTime;
1350 }
1351 
1352 
~NetworkUsageDataSource()1353 NetworkUsageDataSource::~NetworkUsageDataSource()
1354 {
1355 }
1356 
1357 
1358 DataSource*
Copy() const1359 NetworkUsageDataSource::Copy() const
1360 {
1361 	return new NetworkUsageDataSource(*this);
1362 }
1363 
1364 
1365 void
Print(BString & text,int64 value) const1366 NetworkUsageDataSource::Print(BString& text, int64 value) const
1367 {
1368 	char buffer[32];
1369 	string_for_rate(value, buffer, sizeof(buffer));
1370 
1371 	text = buffer;
1372 }
1373 
1374 
1375 int64
NextValue(SystemInfo & info)1376 NetworkUsageDataSource::NextValue(SystemInfo& info)
1377 {
1378 	uint64 transferred = fIn ? info.NetworkReceived() : info.NetworkSent();
1379 
1380 	int64 bytesPerSecond = uint64(double(transferred - fPreviousBytes)
1381 		/ (info.Time() - fPreviousTime) * 1000000.0);
1382 
1383 	fPreviousBytes = transferred;
1384 	fPreviousTime = info.Time();
1385 
1386 	return bytesPerSecond;
1387 }
1388 
1389 
1390 const char*
Label() const1391 NetworkUsageDataSource::Label() const
1392 {
1393 	return fIn ? B_TRANSLATE("Receiving") : B_TRANSLATE("Sending");
1394 }
1395 
1396 
1397 const char*
ShortLabel() const1398 NetworkUsageDataSource::ShortLabel() const
1399 {
1400 	return fIn ? B_TRANSLATE_COMMENT("RX", "Shorter version for Receiving.") :
1401 		B_TRANSLATE_COMMENT("TX", "Shorter version for Sending");
1402 }
1403 
1404 
1405 const char*
InternalName() const1406 NetworkUsageDataSource::InternalName() const
1407 {
1408 	return fIn ? "Network receive" : "Network send";
1409 }
1410 
1411 
1412 const char*
Name() const1413 NetworkUsageDataSource::Name() const
1414 {
1415 	return fIn ? B_TRANSLATE("Network receive") : B_TRANSLATE("Network send");
1416 }
1417 
1418 
1419 bool
AdaptiveScale() const1420 NetworkUsageDataSource::AdaptiveScale() const
1421 {
1422 	return true;
1423 }
1424 
1425 
1426 scale_type
ScaleType() const1427 NetworkUsageDataSource::ScaleType() const
1428 {
1429 	return kBytePerSecondScale;
1430 }
1431 
1432 
1433 bool
Primary() const1434 NetworkUsageDataSource::Primary() const
1435 {
1436 	return true;
1437 }
1438 
1439 
1440 //	#pragma mark -
1441 
1442 
ClipboardSizeDataSource(bool text)1443 ClipboardSizeDataSource::ClipboardSizeDataSource(bool text)
1444 {
1445 	fMinimum = 0;
1446 	fMaximum = UINT32_MAX;
1447 	fText = text;
1448 
1449 	fColor = (rgb_color){0, 150, 255};
1450 }
1451 
1452 
ClipboardSizeDataSource(const ClipboardSizeDataSource & other)1453 ClipboardSizeDataSource::ClipboardSizeDataSource(
1454 		const ClipboardSizeDataSource& other)
1455 	: DataSource(other)
1456 {
1457 	fText = other.fText;
1458 }
1459 
1460 
~ClipboardSizeDataSource()1461 ClipboardSizeDataSource::~ClipboardSizeDataSource()
1462 {
1463 }
1464 
1465 
1466 DataSource*
Copy() const1467 ClipboardSizeDataSource::Copy() const
1468 {
1469 	return new ClipboardSizeDataSource(*this);
1470 }
1471 
1472 
1473 int64
NextValue(SystemInfo & info)1474 ClipboardSizeDataSource::NextValue(SystemInfo& info)
1475 {
1476 	if (fText)
1477 		return info.ClipboardTextSize()/* / 1024*/;
1478 	return info.ClipboardSize()/* / 1024*/;
1479 }
1480 
1481 
1482 const char*
InternalName() const1483 ClipboardSizeDataSource::InternalName() const
1484 {
1485 	return fText ? "Text clipboard size" : "Raw clipboard size";
1486 }
1487 
1488 
1489 const char*
Label() const1490 ClipboardSizeDataSource::Label() const
1491 {
1492 	return fText ? B_TRANSLATE("Text clipboard")
1493 		: B_TRANSLATE("Raw clipboard");
1494 }
1495 
1496 
1497 const char*
Unit() const1498 ClipboardSizeDataSource::Unit() const
1499 {
1500 	return "bytes"/*"KB"*/;
1501 }
1502 
1503 
1504 bool
AdaptiveScale() const1505 ClipboardSizeDataSource::AdaptiveScale() const
1506 {
1507 	return true;
1508 }
1509 
1510 
1511 //	#pragma mark -
1512 
1513 
MediaNodesDataSource()1514 MediaNodesDataSource::MediaNodesDataSource()
1515 {
1516 	SystemInfo info;
1517 
1518 	fMinimum = 0;
1519 	fMaximum = INT32_MAX;
1520 
1521 	fColor = (rgb_color){255, 150, 225};
1522 }
1523 
1524 
~MediaNodesDataSource()1525 MediaNodesDataSource::~MediaNodesDataSource()
1526 {
1527 }
1528 
1529 
1530 DataSource*
Copy() const1531 MediaNodesDataSource::Copy() const
1532 {
1533 	return new MediaNodesDataSource(*this);
1534 }
1535 
1536 
1537 int64
NextValue(SystemInfo & info)1538 MediaNodesDataSource::NextValue(SystemInfo& info)
1539 {
1540 	return info.MediaNodes();
1541 }
1542 
1543 
1544 const char*
InternalName() const1545 MediaNodesDataSource::InternalName() const
1546 {
1547 	return "Media nodes";
1548 }
1549 
1550 
1551 const char*
Label() const1552 MediaNodesDataSource::Label() const
1553 {
1554 	return B_TRANSLATE("Media nodes");
1555 }
1556 
1557 
1558 bool
AdaptiveScale() const1559 MediaNodesDataSource::AdaptiveScale() const
1560 {
1561 	return true;
1562 }
1563 
1564 
1565