[Haiku-commits] r26409 - haiku/trunk/src/apps/activitymonitor

axeld at BerliOS axeld at mail.berlios.de
Mon Jul 14 19:29:04 CEST 2008


Author: axeld
Date: 2008-07-14 19:29:02 +0200 (Mon, 14 Jul 2008)
New Revision: 26409
ViewCVS: http://svn.berlios.de/viewcvs/haiku?rev=26409&view=rev

Modified:
   haiku/trunk/src/apps/activitymonitor/ActivityView.cpp
   haiku/trunk/src/apps/activitymonitor/ActivityView.h
   haiku/trunk/src/apps/activitymonitor/ActivityWindow.cpp
   haiku/trunk/src/apps/activitymonitor/ActivityWindow.h
   haiku/trunk/src/apps/activitymonitor/DataSource.cpp
   haiku/trunk/src/apps/activitymonitor/DataSource.h
Log:
* Now use two views when starting (one memory, one CPU usage). The third view
  that is added to the window is a networking monitor, all later views default
  to CPU usage again.
* DataSources can now share a single adaptive scale, so that they become
  comparable in a single view.
* The networking in/out sources are now using this feature.
* DataHistory::ValueAt() now uses binary search to find the right data_item;
  this also fixes bug #2140, and makes it possible to change the refresh rate
  (which is not yet implemented, though).
* Zooming the timeline now also works by clicking the first mouse button and
  dragging the mouse around (instead of only via the scroll wheel, in case you
  hadn't noticed yet :-)).


Modified: haiku/trunk/src/apps/activitymonitor/ActivityView.cpp
===================================================================
--- haiku/trunk/src/apps/activitymonitor/ActivityView.cpp	2008-07-14 16:41:21 UTC (rev 26408)
+++ haiku/trunk/src/apps/activitymonitor/ActivityView.cpp	2008-07-14 17:29:02 UTC (rev 26409)
@@ -11,7 +11,7 @@
 #include <stdlib.h>
 
 #ifdef __HAIKU__
-#include <AbstractLayoutItem.h>
+#	include <AbstractLayoutItem.h>
 #endif
 #include <Application.h>
 #include <Bitmap.h>
@@ -23,11 +23,26 @@
 
 #include "ActivityMonitor.h"
 #include "ActivityWindow.h"
-#include "DataSource.h"
 #include "SystemInfo.h"
 #include "SystemInfoHandler.h"
 
 
+class Scale {
+public:
+							Scale(scale_type type);
+
+			int64			MinimumValue() const { return fMinimumValue; }
+			int64			MaximumValue() const { return fMaximumValue; }
+
+			void			Update(int64 value);
+
+private:
+	scale_type				fType;
+	int64					fMinimumValue;
+	int64					fMaximumValue;
+	bool					fInitialized;
+};
+
 struct data_item {
 	bigtime_t	time;
 	int64		value;
@@ -85,13 +100,39 @@
 extern const char* kSignature;
 
 
+Scale::Scale(scale_type type)
+	:
+	fType(type),
+	fMinimumValue(0),
+	fMaximumValue(0),
+	fInitialized(false)
+{
+}
+
+
+void
+Scale::Update(int64 value)
+{
+	if (!fInitialized || fMinimumValue > value)
+		fMinimumValue = value;
+	if (!fInitialized || fMaximumValue > value)
+		fMaximumValue = value;
+
+	fInitialized = true;
+}
+
+
+//	#pragma mark -
+
+
 DataHistory::DataHistory(bigtime_t memorize, bigtime_t interval)
 	:
 	fBuffer(10000),
 	fMinimumValue(0),
 	fMaximumValue(0),
 	fRefreshInterval(interval),
-	fLastIndex(-1)
+	fLastIndex(-1),
+	fScale(NULL)
 {
 }
 
@@ -108,6 +149,8 @@
 		fMaximumValue = value;
 	if (fBuffer.IsEmpty() || fMinimumValue > value)
 		fMinimumValue = value;
+	if (fScale != NULL)
+		fScale->Update(value);
 
 	data_item item = {time, value};
 	fBuffer.AddItem(item);
@@ -117,12 +160,29 @@
 int64
 DataHistory::ValueAt(bigtime_t time)
 {
-	// TODO: if the refresh rate changes, this computation won't work anymore!
-	int32 index = (time - Start()) / fRefreshInterval;
-	data_item* item = fBuffer.ItemAt(index);
-	if (item != NULL)
-		return item->value;
+	int32 left = 0;
+	int32 right = fBuffer.CountItems() - 1;
+	data_item* item = NULL;
 
+	while (left <= right) {
+		int32 index = (left + right) / 2;
+		item = fBuffer.ItemAt(index);
+
+		if (item->time > time) {
+			// search in left part
+			right = index - 1;
+		} else {
+			if (index + 1 >= fBuffer.CountItems()
+				|| fBuffer.ItemAt(index + 1)->time > time) {
+				// found item
+				return item->value;
+			}
+
+			// search in right part
+			left = index + 1;
+		}
+	}
+
 	return 0;
 }
 
@@ -130,6 +190,9 @@
 int64
 DataHistory::MaximumValue() const
 {
+	if (fScale != NULL)
+		return fScale->MaximumValue();
+
 	return fMaximumValue;
 }
 
@@ -137,6 +200,9 @@
 int64
 DataHistory::MinimumValue() const
 {
+	if (fScale != NULL)
+		return fScale->MinimumValue();
+
 	return fMinimumValue;
 }
 
@@ -168,6 +234,13 @@
 }
 
 
+void
+DataHistory::SetScale(Scale* scale)
+{
+	fScale = scale;
+}
+
+
 //	#pragma mark -
 
 
@@ -378,6 +451,7 @@
 	fDrawInterval = kInitialRefreshInterval * 2;
 	fLastRefresh = 0;
 	fDrawResolution = 1;
+	fZooming = false;
 
 	fSystemInfoHandler = new SystemInfoHandler;
 
@@ -385,11 +459,8 @@
 		|| settings->FindBool("show legend", &fShowLegend) != B_OK)
 		fShowLegend = true;
 
-	if (settings == NULL) {
-		AddDataSource(new UsedMemoryDataSource());
-		AddDataSource(new CachedMemoryDataSource());
+	if (settings == NULL)
 		return;
-	}
 
 	ssize_t colorLength;
 	rgb_color *color;
@@ -466,6 +537,24 @@
 }
 
 
+Scale*
+ActivityView::_ScaleFor(scale_type type)
+{
+	if (type == kNoScale)
+		return NULL;
+
+	std::map<scale_type, ::Scale*>::iterator iterator = fScales.find(type);
+	if (iterator != fScales.end())
+		return iterator->second;
+
+	// add new scale
+	::Scale* scale = new ::Scale(type);
+	fScales[type] = scale;
+
+	return scale;
+}
+
+
 #ifdef __HAIKU__
 BLayoutItem*
 ActivityView::CreateHistoryLayoutItem()
@@ -535,6 +624,8 @@
 			return B_NO_MEMORY;
 		}
 
+		values->SetScale(_ScaleFor(source->ScaleType()));
+
 		DataSource* copy;
 		if (source->PerCPU())
 			copy = source->CopyForCPU(i);
@@ -688,14 +779,16 @@
 void
 ActivityView::MouseDown(BPoint where)
 {
-#if 0
-	int32 buttons = B_PRIMARY_MOUSE_BUTTON;
-	int32 clicks = 1;
-	if (Looper() != NULL && Looper()->CurrentMessage() != NULL) {
+	int32 buttons = B_SECONDARY_MOUSE_BUTTON;
+	if (Looper() != NULL && Looper()->CurrentMessage() != NULL)
 		Looper()->CurrentMessage()->FindInt32("buttons", &buttons);
-		Looper()->CurrentMessage()->FindInt32("clicks", &clicks);
+
+	if (buttons == B_PRIMARY_MOUSE_BUTTON) {
+		fZoomPoint = where;
+		fOriginalResolution = fDrawResolution;
+		fZooming = true;
+		return;
 	}
-#endif
 
 	BPopUpMenu *menu = new BPopUpMenu(B_EMPTY_STRING, false, false);
 	menu->SetFont(be_plain_font);
@@ -749,9 +842,34 @@
 
 
 void
+ActivityView::MouseUp(BPoint where)
+{
+	fZooming = false;
+}
+
+
+void
 ActivityView::MouseMoved(BPoint where, uint32 transit,
 	const BMessage* dragMessage)
 {
+	if (!fZooming)
+		return;
+
+	int32 previousResolution = fDrawResolution;
+
+	int32 shift = int32(where.x - fZoomPoint.x) / 25;
+	if (shift > 0)
+		fDrawResolution = fOriginalResolution << shift;
+	else
+		fDrawResolution = fOriginalResolution >> -shift;
+
+	if (fDrawResolution < 1)
+		fDrawResolution = 1;
+	if (fDrawResolution > 128)
+		fDrawResolution = 128;
+
+	if (previousResolution != fDrawResolution)
+		Invalidate();
 }
 
 

Modified: haiku/trunk/src/apps/activitymonitor/ActivityView.h
===================================================================
--- haiku/trunk/src/apps/activitymonitor/ActivityView.h	2008-07-14 16:41:21 UTC (rev 26408)
+++ haiku/trunk/src/apps/activitymonitor/ActivityView.h	2008-07-14 17:29:02 UTC (rev 26409)
@@ -6,14 +6,18 @@
 #define ACTIVITY_VIEW_H
 
 
+#include <map>
+
 #include <View.h>
 #include <ObjectList.h>
 
 #include "CircularBuffer.h"
+#include "DataSource.h"
 
 class BBitmap;
 class BMessageRunner;
 class DataSource;
+class Scale;
 class SystemInfoHandler;
 struct data_item;
 
@@ -32,6 +36,7 @@
 			bigtime_t	End() const;
 
 			void		SetRefreshInterval(bigtime_t interval);
+			void		SetScale(Scale* scale);
 
 private:
 	CircularBuffer<data_item> fBuffer;
@@ -39,6 +44,7 @@
 	int64				fMaximumValue;
 	bigtime_t			fRefreshInterval;
 	int32				fLastIndex;
+	Scale*				fScale;
 };
 
 
@@ -77,6 +83,7 @@
 
 	virtual void		FrameResized(float width, float height);
 	virtual void		MouseDown(BPoint where);
+	virtual void		MouseUp(BPoint where);
 	virtual void		MouseMoved(BPoint where, uint32 transit,
 							const BMessage* dragMessage);
 
@@ -86,6 +93,7 @@
 
 private:
 			void		_Init(const BMessage* settings);
+			::Scale*	_ScaleFor(scale_type type);
 			void		_Refresh();
 			void		_UpdateOffscreenBitmap();
 			BView*		_OffscreenView();
@@ -99,6 +107,7 @@
 							DataHistory* values, int64 value);
 			void		_DrawHistory();
 
+private:
 	class HistoryLayoutItem;
 	class LegendLayoutItem;
 
@@ -120,7 +129,11 @@
 	bigtime_t			fDrawInterval;
 	int32				fDrawResolution;
 	bool				fShowLegend;
+	bool				fZooming;
+	BPoint				fZoomPoint;
+	int32				fOriginalResolution;
 	SystemInfoHandler*	fSystemInfoHandler;
+	std::map<scale_type, ::Scale*> fScales;
 };
 
 #endif	// ACTIVITY_VIEW_H

Modified: haiku/trunk/src/apps/activitymonitor/ActivityWindow.cpp
===================================================================
--- haiku/trunk/src/apps/activitymonitor/ActivityWindow.cpp	2008-07-14 16:41:21 UTC (rev 26408)
+++ haiku/trunk/src/apps/activitymonitor/ActivityWindow.cpp	2008-07-14 17:29:02 UTC (rev 26409)
@@ -22,13 +22,14 @@
 
 #include "ActivityMonitor.h"
 #include "ActivityView.h"
+#include "DataSource.h"
 
 
 static const uint32 kMsgAddView = 'advw';
 
 
 ActivityWindow::ActivityWindow()
-	: BWindow(BRect(100, 100, 500, 250), "ActivityMonitor", B_TITLED_WINDOW,
+	: BWindow(BRect(100, 100, 500, 350), "ActivityMonitor", B_TITLED_WINDOW,
 		B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE)
 {
 	BMessage settings;
@@ -68,12 +69,11 @@
 		count++;
 	}
 	if (count == 0) {
-		ActivityView* view = new ActivityView("ActivityMonitor", NULL);
-		fLayout->AddItem(view->CreateHistoryLayoutItem());
-		fLayout->AddItem(view->CreateLegendLayoutItem());
+		// Add default views (memory & CPU usage)
+		_AddDefaultView();
+		_AddDefaultView();
 	}
-
-#else
+#else	// !__HAIKU__
 	BView *layout = new BView(Bounds(), "topmost", B_FOLLOW_NONE, 0);
 	AddChild(layout);
 
@@ -222,6 +222,34 @@
 
 
 void
+ActivityWindow::_AddDefaultView()
+{
+	ActivityView* view = new ActivityView("ActivityMonitor", NULL);
+
+	switch (ActivityViewCount()) {
+		case 0:
+			// The first view defaults to memory usage
+			view->AddDataSource(new UsedMemoryDataSource());
+			view->AddDataSource(new CachedMemoryDataSource());
+			break;
+		case 2:
+			// The third view defaults to network in/out
+			view->AddDataSource(new NetworkUsageDataSource(true));
+			view->AddDataSource(new NetworkUsageDataSource(false));
+			break;
+		case 1:
+		default:
+			// Everything beyond that defaults to a CPU usage view
+			view->AddDataSource(new CPUUsageDataSource());
+			break;
+	}
+
+	fLayout->AddItem(view->CreateHistoryLayoutItem());
+	fLayout->AddItem(view->CreateLegendLayoutItem());
+}
+
+
+void
 ActivityWindow::MessageReceived(BMessage* message)
 {
 	if (message->WasDropped()) {
@@ -240,9 +268,7 @@
 #ifdef __HAIKU__
 			BView* firstView = fLayout->View()->ChildAt(0);
 
-			ActivityView* view = new ActivityView("ActivityMonitor", NULL);
-			fLayout->AddItem(view->CreateHistoryLayoutItem());
-			fLayout->AddItem(view->CreateLegendLayoutItem());
+			_AddDefaultView();
 
 			if (firstView != NULL)
 				ResizeBy(0, firstView->Bounds().Height() + fLayout->Spacing());

Modified: haiku/trunk/src/apps/activitymonitor/ActivityWindow.h
===================================================================
--- haiku/trunk/src/apps/activitymonitor/ActivityWindow.h	2008-07-14 16:41:21 UTC (rev 26408)
+++ haiku/trunk/src/apps/activitymonitor/ActivityWindow.h	2008-07-14 17:29:02 UTC (rev 26409)
@@ -29,6 +29,7 @@
 			status_t	_LoadSettings(BMessage& settings);
 			status_t	_SaveSettings();
 
+			void		_AddDefaultView();
 			void		_UpdateRemoveItem();
 			void		_MessageDropped(BMessage *message);
 

Modified: haiku/trunk/src/apps/activitymonitor/DataSource.cpp
===================================================================
--- haiku/trunk/src/apps/activitymonitor/DataSource.cpp	2008-07-14 16:41:21 UTC (rev 26408)
+++ haiku/trunk/src/apps/activitymonitor/DataSource.cpp	2008-07-14 17:29:02 UTC (rev 26409)
@@ -176,6 +176,13 @@
 }
 
 
+scale_type
+DataSource::ScaleType() const
+{
+	return kNoScale;
+}
+
+
 int32
 DataSource::CPU() const
 {

Modified: haiku/trunk/src/apps/activitymonitor/DataSource.h
===================================================================
--- haiku/trunk/src/apps/activitymonitor/DataSource.h	2008-07-14 16:41:21 UTC (rev 26408)
+++ haiku/trunk/src/apps/activitymonitor/DataSource.h	2008-07-14 17:29:02 UTC (rev 26409)
@@ -12,6 +12,11 @@
 class SystemInfo;
 
 
+enum scale_type {
+	kNoScale,
+	kBytePerSecondScale,
+};
+
 class DataSource {
 public:
 						DataSource(int64 initialMin, int64 initialMax);
@@ -38,6 +43,7 @@
 	virtual const char*	Unit() const;
 	virtual rgb_color	Color() const;
 	virtual bool		AdaptiveScale() const;
+	virtual scale_type	ScaleType() const;
 	virtual int32		CPU() const;
 	virtual bool		PerCPU() const;
 	virtual bool		MultiCPUOnly() const;




More information about the Haiku-commits mailing list