2 * Copyright 2008-2009, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
7 #include "DataSource.h"
15 #include <StringForRate.h>
17 #include "SystemInfo.h"
19 #undef B_TRANSLATION_CONTEXT
20 #define B_TRANSLATION_CONTEXT "DataSource"
23 const DataSource
* kSources
[] = {
24 new UsedMemoryDataSource(),
25 new CachedMemoryDataSource(),
26 new SwapSpaceDataSource(),
27 new PageFaultsDataSource(),
28 new CPUUsageDataSource(),
29 new CPUCombinedUsageDataSource(),
30 new NetworkUsageDataSource(true),
31 new NetworkUsageDataSource(false),
32 new BlockCacheDataSource(),
33 new SemaphoresDataSource(),
34 new PortsDataSource(),
35 new ThreadsDataSource(),
36 new TeamsDataSource(),
37 new RunningAppsDataSource(),
38 new ClipboardSizeDataSource(false),
39 new ClipboardSizeDataSource(true),
40 new MediaNodesDataSource()
42 const size_t kSourcesCount
= sizeof(kSources
) / sizeof(kSources
[0]);
45 DataSource::DataSource(int64 initialMin
, int64 initialMax
)
50 fColor((rgb_color
){200, 0, 0})
55 DataSource::DataSource()
60 fColor((rgb_color
){200, 0, 0})
65 DataSource::DataSource(const DataSource
& other
)
67 fMinimum
= other
.fMinimum
;
68 fMaximum
= other
.fMaximum
;
69 fInterval
= other
.fInterval
;
70 fColor
= other
.fColor
;
74 DataSource::~DataSource()
80 DataSource::Copy() const
83 // this class cannot be copied
88 DataSource::CopyForCPU(int32 cpu
) const
95 DataSource::Minimum() const
102 DataSource::Maximum() const
109 DataSource::RefreshInterval() const
116 DataSource::SetLimits(int64 min
, int64 max
)
124 DataSource::SetRefreshInterval(bigtime_t interval
)
126 fInterval
= interval
;
131 DataSource::SetColor(rgb_color color
)
138 DataSource::NextValue(SystemInfo
& info
)
145 DataSource::Print(BString
& text
, int64 value
) const
153 DataSource::ShortLabel() const
160 DataSource::Name() const
167 DataSource::Label() const
174 DataSource::Unit() const
181 DataSource::Color() const
188 DataSource::AdaptiveScale() const
195 DataSource::ScaleType() const
202 DataSource::CPU() const
209 DataSource::PerCPU() const
216 DataSource::MultiCPUOnly() const
223 DataSource::Primary() const
230 DataSource::CountSources()
232 return kSourcesCount
;
236 /*static*/ const DataSource
*
237 DataSource::SourceAt(int32 index
)
239 if (index
>= (int32
)kSourcesCount
|| index
< 0)
242 return kSources
[index
];
246 /*static*/ const DataSource
*
247 DataSource::FindSource(const char* internalName
)
249 for (uint32 i
= 0; i
< kSourcesCount
; i
++) {
250 const DataSource
* source
= kSources
[i
];
251 if (!strcmp(source
->InternalName(), internalName
))
260 DataSource::IndexOf(const DataSource
* source
)
262 const char* name
= source
->Name();
264 for (uint32 i
= 0; i
< kSourcesCount
; i
++) {
265 if (!strcmp(kSources
[i
]->Name(), name
))
276 MemoryDataSource::MemoryDataSource()
281 fMaximum
= info
.MaxMemory();
285 MemoryDataSource::~MemoryDataSource()
291 MemoryDataSource::Print(BString
& text
, int64 value
) const
294 snprintf(buffer
, sizeof(buffer
), B_TRANSLATE("%.1f MiB"), value
/ 1048576.0);
301 MemoryDataSource::Unit() const
303 return B_TRANSLATE("MiB");
310 UsedMemoryDataSource::UsedMemoryDataSource()
315 UsedMemoryDataSource::~UsedMemoryDataSource()
321 UsedMemoryDataSource::Copy() const
323 return new UsedMemoryDataSource(*this);
328 UsedMemoryDataSource::NextValue(SystemInfo
& info
)
330 return info
.UsedMemory();
335 UsedMemoryDataSource::InternalName() const
337 return "Used memory";
342 UsedMemoryDataSource::Label() const
344 return B_TRANSLATE("Used memory");
349 UsedMemoryDataSource::ShortLabel() const
351 return B_TRANSLATE("Memory");
356 UsedMemoryDataSource::Primary() const
365 CachedMemoryDataSource::CachedMemoryDataSource()
367 fColor
= (rgb_color
){0, 200, 0};
371 CachedMemoryDataSource::~CachedMemoryDataSource()
377 CachedMemoryDataSource::Copy() const
379 return new CachedMemoryDataSource(*this);
384 CachedMemoryDataSource::NextValue(SystemInfo
& info
)
386 return info
.CachedMemory();
391 CachedMemoryDataSource::InternalName() const
393 return "Cached memory";
398 CachedMemoryDataSource::Label() const
400 return B_TRANSLATE("Cached memory");
405 CachedMemoryDataSource::ShortLabel() const
407 return B_TRANSLATE("Cache");
412 CachedMemoryDataSource::Primary() const
421 SwapSpaceDataSource::SwapSpaceDataSource()
425 fColor
= (rgb_color
){0, 120, 0};
426 fMaximum
= info
.MaxSwapSpace();
430 SwapSpaceDataSource::~SwapSpaceDataSource()
436 SwapSpaceDataSource::Copy() const
438 return new SwapSpaceDataSource(*this);
443 SwapSpaceDataSource::NextValue(SystemInfo
& info
)
445 return info
.UsedSwapSpace();
450 SwapSpaceDataSource::InternalName() const
457 SwapSpaceDataSource::Label() const
459 return B_TRANSLATE("Swap space");
464 SwapSpaceDataSource::ShortLabel() const
466 return B_TRANSLATE("Swap");
471 SwapSpaceDataSource::Primary() const
480 BlockCacheDataSource::BlockCacheDataSource()
482 fColor
= (rgb_color
){0, 0, 120};
486 BlockCacheDataSource::~BlockCacheDataSource()
492 BlockCacheDataSource::Copy() const
494 return new BlockCacheDataSource(*this);
499 BlockCacheDataSource::NextValue(SystemInfo
& info
)
501 return info
.BlockCacheMemory();
506 BlockCacheDataSource::InternalName() const
508 return "Block cache memory";
513 BlockCacheDataSource::Label() const
515 return B_TRANSLATE("Block cache memory");
520 BlockCacheDataSource::ShortLabel() const
522 return B_TRANSLATE("Block cache");
529 SemaphoresDataSource::SemaphoresDataSource()
534 fMaximum
= info
.MaxSemaphores();
536 fColor
= (rgb_color
){100, 200, 100};
540 SemaphoresDataSource::~SemaphoresDataSource()
546 SemaphoresDataSource::Copy() const
548 return new SemaphoresDataSource(*this);
553 SemaphoresDataSource::NextValue(SystemInfo
& info
)
555 return info
.UsedSemaphores();
560 SemaphoresDataSource::InternalName() const
567 SemaphoresDataSource::Label() const
569 return B_TRANSLATE("Semaphores");
574 SemaphoresDataSource::ShortLabel() const
576 return B_TRANSLATE("Sems");
581 SemaphoresDataSource::AdaptiveScale() const
590 PortsDataSource::PortsDataSource()
595 fMaximum
= info
.MaxPorts();
597 fColor
= (rgb_color
){180, 200, 180};
601 PortsDataSource::~PortsDataSource()
607 PortsDataSource::Copy() const
609 return new PortsDataSource(*this);
614 PortsDataSource::NextValue(SystemInfo
& info
)
616 return info
.UsedPorts();
621 PortsDataSource::InternalName() const
628 PortsDataSource::Label() const
630 return B_TRANSLATE("Ports");
635 PortsDataSource::AdaptiveScale() const
644 ThreadsDataSource::ThreadsDataSource()
649 fMaximum
= info
.MaxThreads();
651 fColor
= (rgb_color
){0, 0, 200};
655 ThreadsDataSource::~ThreadsDataSource()
661 ThreadsDataSource::Copy() const
663 return new ThreadsDataSource(*this);
668 ThreadsDataSource::NextValue(SystemInfo
& info
)
670 return info
.UsedThreads();
675 ThreadsDataSource::InternalName() const
682 ThreadsDataSource::Label() const
684 return B_TRANSLATE("Threads");
689 ThreadsDataSource::AdaptiveScale() const
698 TeamsDataSource::TeamsDataSource()
703 fMaximum
= info
.MaxTeams();
705 fColor
= (rgb_color
){0, 150, 255};
709 TeamsDataSource::~TeamsDataSource()
715 TeamsDataSource::Copy() const
717 return new TeamsDataSource(*this);
722 TeamsDataSource::NextValue(SystemInfo
& info
)
724 return info
.UsedTeams();
729 TeamsDataSource::InternalName() const
736 TeamsDataSource::Label() const
738 return B_TRANSLATE("Teams");
743 TeamsDataSource::AdaptiveScale() const
752 RunningAppsDataSource::RunningAppsDataSource()
757 fMaximum
= info
.MaxRunningApps();
759 fColor
= (rgb_color
){100, 150, 255};
763 RunningAppsDataSource::~RunningAppsDataSource()
769 RunningAppsDataSource::Copy() const
771 return new RunningAppsDataSource(*this);
776 RunningAppsDataSource::NextValue(SystemInfo
& info
)
778 return info
.UsedRunningApps();
783 RunningAppsDataSource::InternalName() const
785 return "Running applications";
790 RunningAppsDataSource::Label() const
792 return B_TRANSLATE("Running applications");
797 RunningAppsDataSource::ShortLabel() const
799 return B_TRANSLATE("Apps");
804 RunningAppsDataSource::AdaptiveScale() const
813 CPUUsageDataSource::CPUUsageDataSource(int32 cpu
)
825 CPUUsageDataSource::CPUUsageDataSource(const CPUUsageDataSource
& other
)
828 fPreviousActive
= other
.fPreviousActive
;
829 fPreviousTime
= other
.fPreviousTime
;
831 fLabel
= other
.fLabel
;
832 fShortLabel
= other
.fShortLabel
;
836 CPUUsageDataSource::~CPUUsageDataSource()
842 CPUUsageDataSource::Copy() const
844 return new CPUUsageDataSource(*this);
849 CPUUsageDataSource::CopyForCPU(int32 cpu
) const
851 CPUUsageDataSource
* copy
= new CPUUsageDataSource(*this);
859 CPUUsageDataSource::Print(BString
& text
, int64 value
) const
862 snprintf(buffer
, sizeof(buffer
), "%.1f%%", value
/ 10.0);
869 CPUUsageDataSource::NextValue(SystemInfo
& info
)
871 bigtime_t active
= info
.CPUActiveTime(fCPU
);
873 int64 percent
= int64(1000.0 * (active
- fPreviousActive
)
874 / (info
.Time() - fPreviousTime
));
880 fPreviousActive
= active
;
881 fPreviousTime
= info
.Time();
888 CPUUsageDataSource::Label() const
890 return fLabel
.String();
895 CPUUsageDataSource::ShortLabel() const
897 return fShortLabel
.String();
902 CPUUsageDataSource::InternalName() const
909 CPUUsageDataSource::Name() const
911 return B_TRANSLATE("CPU usage");
916 CPUUsageDataSource::CPU() const
923 CPUUsageDataSource::PerCPU() const
930 CPUUsageDataSource::Primary() const
937 CPUUsageDataSource::_SetCPU(int32 cpu
)
940 fLabel
= B_TRANSLATE("CPU");
941 if (SystemInfo().CPUCount() > 1)
942 fLabel
<< " " << cpu
+ 1;
943 fShortLabel
= fLabel
;
945 fLabel
<< " " << B_TRANSLATE("usage");
947 const rgb_color kColors
[] = {
948 // TODO: find some better defaults...
958 const uint32 kNumColors
= sizeof(kColors
) / sizeof(kColors
[0]);
960 fColor
= kColors
[cpu
% kNumColors
];
967 CPUCombinedUsageDataSource::CPUCombinedUsageDataSource()
975 fColor
= (rgb_color
){200, 200, 0};
979 CPUCombinedUsageDataSource::CPUCombinedUsageDataSource(
980 const CPUCombinedUsageDataSource
& other
)
983 fPreviousActive
= other
.fPreviousActive
;
984 fPreviousTime
= other
.fPreviousTime
;
988 CPUCombinedUsageDataSource::~CPUCombinedUsageDataSource()
994 CPUCombinedUsageDataSource::Copy() const
996 return new CPUCombinedUsageDataSource(*this);
1001 CPUCombinedUsageDataSource::Print(BString
& text
, int64 value
) const
1004 snprintf(buffer
, sizeof(buffer
), "%.1f%%", value
/ 10.0);
1011 CPUCombinedUsageDataSource::NextValue(SystemInfo
& info
)
1014 bigtime_t active
= 0;
1016 for (uint32 cpu
= 0; cpu
< info
.CPUCount(); cpu
++) {
1017 active
+= info
.CPUActiveTime(cpu
);
1019 // TODO: take disabled CPUs into account
1022 int64 percent
= int64(1000.0 * (active
- fPreviousActive
)
1023 / (running
* (info
.Time() - fPreviousTime
)));
1029 fPreviousActive
= active
;
1030 fPreviousTime
= info
.Time();
1037 CPUCombinedUsageDataSource::Label() const
1039 return B_TRANSLATE("CPU usage");
1044 CPUCombinedUsageDataSource::ShortLabel() const
1046 return B_TRANSLATE("CPU");
1051 CPUCombinedUsageDataSource::InternalName() const
1053 return "CPU usage (combined)";
1058 CPUCombinedUsageDataSource::Name() const
1060 return B_TRANSLATE("CPU usage (combined)");
1065 CPUCombinedUsageDataSource::MultiCPUOnly() const
1072 CPUCombinedUsageDataSource::Primary() const
1081 PageFaultsDataSource::PageFaultsDataSource()
1090 fMaximum
= 1000000000LL;
1092 fColor
= (rgb_color
){200, 0, 150, 0};
1096 PageFaultsDataSource::PageFaultsDataSource(const PageFaultsDataSource
& other
)
1099 fPreviousFaults
= other
.fPreviousFaults
;
1100 fPreviousTime
= other
.fPreviousTime
;
1104 PageFaultsDataSource::~PageFaultsDataSource()
1110 PageFaultsDataSource::Copy() const
1112 return new PageFaultsDataSource(*this);
1117 PageFaultsDataSource::Print(BString
& text
, int64 value
) const
1120 snprintf(buffer
, sizeof(buffer
), B_TRANSLATE("%.1f faults/s"),
1128 PageFaultsDataSource::NextValue(SystemInfo
& info
)
1130 uint64 faults
= info
.PageFaults();
1132 int64 faultsPerSecond
= uint64(1024 * double(faults
- fPreviousFaults
)
1133 / (info
.Time() - fPreviousTime
) * 1000000.0);
1135 fPreviousFaults
= faults
;
1136 fPreviousTime
= info
.Time();
1138 return faultsPerSecond
;
1143 PageFaultsDataSource::Label() const
1145 return B_TRANSLATE("Page faults");
1150 PageFaultsDataSource::ShortLabel() const
1152 return B_TRANSLATE("P-faults");
1157 PageFaultsDataSource::InternalName() const
1159 return "Page faults";
1164 PageFaultsDataSource::Name() const
1166 return B_TRANSLATE("Page faults");
1171 PageFaultsDataSource::AdaptiveScale() const
1178 PageFaultsDataSource::Primary() const
1187 NetworkUsageDataSource::NetworkUsageDataSource(bool in
)
1197 fMaximum
= 1000000000LL;
1199 fColor
= fIn
? (rgb_color
){200, 150, 0} : (rgb_color
){200, 220, 0};
1203 NetworkUsageDataSource::NetworkUsageDataSource(
1204 const NetworkUsageDataSource
& other
)
1208 fPreviousBytes
= other
.fPreviousBytes
;
1209 fPreviousTime
= other
.fPreviousTime
;
1213 NetworkUsageDataSource::~NetworkUsageDataSource()
1219 NetworkUsageDataSource::Copy() const
1221 return new NetworkUsageDataSource(*this);
1226 NetworkUsageDataSource::Print(BString
& text
, int64 value
) const
1229 string_for_rate(value
, buffer
, sizeof(buffer
));
1236 NetworkUsageDataSource::NextValue(SystemInfo
& info
)
1238 uint64 transferred
= fIn
? info
.NetworkReceived() : info
.NetworkSent();
1240 int64 bytesPerSecond
= uint64(double(transferred
- fPreviousBytes
)
1241 / (info
.Time() - fPreviousTime
) * 1000000.0);
1243 fPreviousBytes
= transferred
;
1244 fPreviousTime
= info
.Time();
1246 return bytesPerSecond
;
1251 NetworkUsageDataSource::Label() const
1253 return fIn
? B_TRANSLATE("Receiving") : B_TRANSLATE("Sending");
1258 NetworkUsageDataSource::ShortLabel() const
1260 return fIn
? B_TRANSLATE_COMMENT("RX", "Shorter version for Receiving.") :
1261 B_TRANSLATE_COMMENT("TX", "Shorter version for Sending");
1266 NetworkUsageDataSource::InternalName() const
1268 return fIn
? "Network receive" : "Network send";
1273 NetworkUsageDataSource::Name() const
1275 return fIn
? B_TRANSLATE("Network receive") : B_TRANSLATE("Network send");
1280 NetworkUsageDataSource::AdaptiveScale() const
1287 NetworkUsageDataSource::ScaleType() const
1289 return kBytePerSecondScale
;
1294 NetworkUsageDataSource::Primary() const
1303 ClipboardSizeDataSource::ClipboardSizeDataSource(bool text
)
1306 fMaximum
= UINT32_MAX
;
1309 fColor
= (rgb_color
){0, 150, 255};
1313 ClipboardSizeDataSource::ClipboardSizeDataSource(
1314 const ClipboardSizeDataSource
& other
)
1317 fText
= other
.fText
;
1321 ClipboardSizeDataSource::~ClipboardSizeDataSource()
1327 ClipboardSizeDataSource::Copy() const
1329 return new ClipboardSizeDataSource(*this);
1334 ClipboardSizeDataSource::NextValue(SystemInfo
& info
)
1337 return info
.ClipboardTextSize()/* / 1024*/;
1338 return info
.ClipboardSize()/* / 1024*/;
1343 ClipboardSizeDataSource::InternalName() const
1345 return fText
? "Text clipboard size" : "Raw clipboard size";
1350 ClipboardSizeDataSource::Label() const
1352 return fText
? B_TRANSLATE("Text clipboard")
1353 : B_TRANSLATE("Raw clipboard");
1358 ClipboardSizeDataSource::Unit() const
1360 return "bytes"/*"KB"*/;
1365 ClipboardSizeDataSource::AdaptiveScale() const
1374 MediaNodesDataSource::MediaNodesDataSource()
1379 fMaximum
= INT32_MAX
;
1381 fColor
= (rgb_color
){255, 150, 225};
1385 MediaNodesDataSource::~MediaNodesDataSource()
1391 MediaNodesDataSource::Copy() const
1393 return new MediaNodesDataSource(*this);
1398 MediaNodesDataSource::NextValue(SystemInfo
& info
)
1400 return info
.MediaNodes();
1405 MediaNodesDataSource::InternalName() const
1407 return "Media nodes";
1412 MediaNodesDataSource::Label() const
1414 return B_TRANSLATE("Media nodes");
1419 MediaNodesDataSource::AdaptiveScale() const