void ActivityView::_Refresh() { bigtime_t lastTimeout = system_time() - RefreshInterval(); BMessenger target(this); while (true) { status_t status = acquire_sem_etc(fRefreshSem, 1, B_ABSOLUTE_TIMEOUT, lastTimeout + RefreshInterval()); if (status == B_OK || status == B_BAD_SEM_ID) break; if (status == B_INTERRUPTED) continue; SystemInfo info(fSystemInfoHandler); lastTimeout += RefreshInterval(); fSourcesLock.Lock(); for (uint32 i = fSources.CountItems(); i-- > 0;) { DataSource* source = fSources.ItemAt(i); DataHistory* values = fValues.ItemAt(i); int64 value = source->NextValue(info); values->AddValue(info.Time(), value); } fSourcesLock.Unlock(); target.SendMessage(B_INVALIDATE); } }
void ActivityWindow::_AddDefaultView() { BMessage settings; settings.AddInt64("refresh interval", RefreshInterval()); ActivityView* view = new ActivityView("ActivityMonitor", &settings); 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()); }
status_t ActivityView::AddDataSource(const DataSource* source, const BMessage* state) { if (source == NULL) return B_BAD_VALUE; BAutolock _(fSourcesLock); // Search for the correct insert spot to maintain the order of the sources int32 insert = DataSource::IndexOf(source); for (int32 i = 0; i < fSources.CountItems() && i < insert; i++) { DataSource* before = fSources.ItemAt(i); if (DataSource::IndexOf(before) > insert) { insert = i; break; } } if (insert > fSources.CountItems()) insert = fSources.CountItems(); // Generate DataHistory and ViewHistory objects for the source // (one might need one history per CPU) uint32 count = 1; if (source->PerCPU()) { SystemInfo info; count = info.CPUCount(); } for (uint32 i = 0; i < count; i++) { DataHistory* values = new(std::nothrow) DataHistory(10 * 60000000LL, RefreshInterval()); ListAddDeleter<DataHistory> valuesDeleter(fValues, values, insert); ViewHistory* viewValues = new(std::nothrow) ViewHistory; ListAddDeleter<ViewHistory> viewValuesDeleter(fViewValues, viewValues, insert); if (valuesDeleter.Failed() || viewValuesDeleter.Failed()) return B_NO_MEMORY; values->SetScale(_ScaleFor(source->ScaleType())); DataSource* copy; if (source->PerCPU()) copy = source->CopyForCPU(i); else copy = source->Copy(); ListAddDeleter<DataSource> sourceDeleter(fSources, copy, insert); if (sourceDeleter.Failed()) return B_NO_MEMORY; BString colorName = source->Name(); colorName << " color"; if (state != NULL) { const rgb_color* color = NULL; ssize_t colorLength; if (state->FindData(colorName.String(), B_RGB_COLOR_TYPE, i, (const void**)&color, &colorLength) == B_OK && colorLength == sizeof(rgb_color)) copy->SetColor(*color); } valuesDeleter.Detach(); viewValuesDeleter.Detach(); sourceDeleter.Detach(); insert++; } #ifdef __HAIKU__ InvalidateLayout(); #endif return B_OK; }
void ActivityView::_DrawHistory(bool drawBackground) { _UpdateOffscreenBitmap(); BView* view = this; if (fOffscreen != NULL) { fOffscreen->Lock(); view = _OffscreenView(); } BRect frame = _HistoryFrame(); BRect outerFrame = frame.InsetByCopy(-2, -2); // draw the outer frame uint32 flags = 0; if (!drawBackground) flags |= BControlLook::B_BLEND_FRAME; be_control_look->DrawTextControlBorder(this, outerFrame, outerFrame, fLegendBackgroundColor, flags); // convert to offscreen view if necessary if (view != this) frame.OffsetTo(B_ORIGIN); view->SetLowColor(fHistoryBackgroundColor); view->FillRect(frame, B_SOLID_LOW); uint32 step = 2; uint32 resolution = fDrawResolution; if (fDrawResolution > 1) { step = 1; resolution--; } // We would get a negative number of steps which isn't a good idea. if (frame.IntegerWidth() <= 10) return; uint32 width = frame.IntegerWidth() - 10; uint32 steps = width / step; bigtime_t timeStep = RefreshInterval() * resolution; bigtime_t now = system_time(); // Draw scale // TODO: add second markers? view->SetPenSize(1); rgb_color scaleColor = view->LowColor(); uint32 average = (scaleColor.red + scaleColor.green + scaleColor.blue) / 3; if (average < 96) scaleColor = tint_color(scaleColor, B_LIGHTEN_2_TINT); else scaleColor = tint_color(scaleColor, B_DARKEN_2_TINT); view->SetHighColor(scaleColor); view->StrokeLine(BPoint(frame.left, frame.top + frame.Height() / 2), BPoint(frame.right, frame.top + frame.Height() / 2)); // Draw values view->SetPenSize(1.5); BAutolock _(fSourcesLock); for (uint32 i = fSources.CountItems(); i-- > 0;) { ViewHistory* viewValues = fViewValues.ItemAt(i); DataSource* source = fSources.ItemAt(i); DataHistory* values = fValues.ItemAt(i); viewValues->Update(values, steps, fDrawResolution, now, timeStep, RefreshInterval()); if (viewValues->Start() >= (int32)steps - 1) continue; uint32 x = viewValues->Start() * step; bool first = true; view->SetHighColor(source->Color()); view->SetLineMode(B_BUTT_CAP, B_ROUND_JOIN); view->MovePenTo(B_ORIGIN); try { view->BeginLineArray(steps - viewValues->Start() - 1); BPoint prev; for (uint32 j = viewValues->Start(); j < steps; x += step, j++) { float y = _PositionForValue(source, values, viewValues->ValueAt(j)); if (first) { first = false; } else view->AddLine(prev, BPoint(x, y), source->Color()); prev.Set(x, y); } } catch (std::bad_alloc) { // Not enough memory to allocate the line array. // TODO we could try to draw using the slower but less memory // consuming solution using StrokeLine. } view->EndLineArray(); } // TODO: add marks when an app started or quit view->Sync(); if (fOffscreen != NULL) { fOffscreen->Unlock(); DrawBitmap(fOffscreen, outerFrame.LeftTop()); } }
void ActivityView::_DrawHistory(bool drawBackground) { _UpdateOffscreenBitmap(); BView* view = this; if (fOffscreen != NULL) { fOffscreen->Lock(); view = _OffscreenView(); } BRect frame = _HistoryFrame(); BRect outerFrame = frame.InsetByCopy(-2, -2); // draw the outer frame uint32 flags = 0; if (!drawBackground) flags |= BControlLook::B_BLEND_FRAME; be_control_look->DrawTextControlBorder(this, outerFrame, outerFrame, fLegendBackgroundColor, flags); // convert to offscreen view if necessary if (view != this) frame.OffsetTo(B_ORIGIN); view->SetLowColor(fHistoryBackgroundColor); view->FillRect(frame, B_SOLID_LOW); uint32 step = 2; uint32 resolution = fDrawResolution; if (fDrawResolution > 1) { step = 1; resolution--; } uint32 width = frame.IntegerWidth() - 10; uint32 steps = width / step; bigtime_t timeStep = RefreshInterval() * resolution; bigtime_t now = system_time(); // Draw scale // TODO: add second markers? view->SetPenSize(1); rgb_color scaleColor = view->LowColor(); uint32 average = (scaleColor.red + scaleColor.green + scaleColor.blue) / 3; if (average < 96) scaleColor = tint_color(scaleColor, B_LIGHTEN_2_TINT); else scaleColor = tint_color(scaleColor, B_DARKEN_2_TINT); view->SetHighColor(scaleColor); view->StrokeLine(BPoint(frame.left, frame.top + frame.Height() / 2), BPoint(frame.right, frame.top + frame.Height() / 2)); // Draw values view->SetPenSize(1.5); BAutolock _(fSourcesLock); for (uint32 i = fSources.CountItems(); i-- > 0;) { ViewHistory* viewValues = fViewValues.ItemAt(i); DataSource* source = fSources.ItemAt(i); DataHistory* values = fValues.ItemAt(i); viewValues->Update(values, steps, fDrawResolution, now, timeStep, RefreshInterval()); uint32 x = viewValues->Start() * step; BShape shape; bool first = true; for (uint32 i = viewValues->Start(); i < steps; x += step, i++) { float y = _PositionForValue(source, values, viewValues->ValueAt(i)); if (first) { shape.MoveTo(BPoint(x, y)); first = false; } else shape.LineTo(BPoint(x, y)); } view->SetHighColor(source->Color()); view->SetLineMode(B_BUTT_CAP, B_ROUND_JOIN); view->MovePenTo(B_ORIGIN); view->StrokeShape(&shape); } // TODO: add marks when an app started or quit view->Sync(); if (fOffscreen != NULL) { fOffscreen->Unlock(); DrawBitmap(fOffscreen, outerFrame.LeftTop()); } }