gui/dboxinfo: move cpu load calculation to dedicated thread ...

... that keeps history; show load graph


Origin commit data
------------------
Branch: ni/coolstream
Commit: 6fff156164
Author: martii <m4rtii@gmx.de>
Date: 2013-12-30 (Mon, 30 Dec 2013)



------------------
This commit was generated by Migit
This commit is contained in:
martii
2013-12-30 01:34:07 +01:00
committed by vanhofen
parent 2598ee53f1
commit fa3f04726a
6 changed files with 196 additions and 29 deletions

View File

@@ -54,6 +54,7 @@
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#include <sys/vfs.h> #include <sys/vfs.h>
#include <system/sysload.h>
#include <system/helpers.h> #include <system/helpers.h>
#include <map> #include <map>
#include <iostream> #include <iostream>
@@ -75,7 +76,6 @@ CDBoxInfoWidget::CDBoxInfoWidget()
height = 0; height = 0;
x = 0; x = 0;
y = 0; y = 0;
stat_total = 0;
} }
@@ -211,14 +211,18 @@ void CDBoxInfoWidget::paint()
int nameWidth = fontWidth * 17;//WWWwwwwwww int nameWidth = fontWidth * 17;//WWWwwwwwww
height = hheight; height = hheight;
height += mheight/2; // space height += mheight/2; // space
int cpuload_y0 = height;
height += mheight; // time height += mheight; // time
height += mheight; // uptime height += mheight; // uptime
height += mheight; // load height += mheight; // load
int cpuload_y1 = height;
height += mheight/2; // space height += mheight/2; // space
int frontend_count = CFEManager::getInstance()->getFrontendCount(); int frontend_count = CFEManager::getInstance()->getFrontendCount();
if (frontend_count) if (frontend_count) {
height += mheight * frontend_count + mheight/2; height += mheight * frontend_count;
height += mheight/2;
}
int icon_w = 0, icon_h = 0; int icon_w = 0, icon_h = 0;
frameBuffer->getIconSize(NEUTRINO_ICON_REC, &icon_w, &icon_h); frameBuffer->getIconSize(NEUTRINO_ICON_REC, &icon_w, &icon_h);
@@ -352,28 +356,10 @@ void CDBoxInfoWidget::paint()
ypos += hheight + mheight/2; ypos += hheight + mheight/2;
long current_load = -1; cSysLoad *sysload = cSysLoad::getInstance();
in.open("/proc/stat"); int data_last = sysload->data_last;
if (in.is_open()) {
std::string line;
while (getline(in, line)) {
unsigned long _stat_user, _stat_nice, _stat_system, _stat_idle;
if (4 == sscanf(line.c_str(), "cpu %lu %lu %lu %lu", &_stat_user, &_stat_nice, &_stat_system, &_stat_idle)) {
if (stat_total) {
unsigned long div = _stat_user + _stat_nice + _stat_system + _stat_idle - stat_total;
if (div > 0)
current_load = (long)(1000 - 1000 * (_stat_idle - stat_idle) / div);
}
stat_idle = _stat_idle;
stat_total = _stat_user + _stat_nice + _stat_system + _stat_idle;
break;
}
}
in.close();
}
char ubuf[80]; char ubuf[80];
time_t now = time(NULL); time_t now = time(NULL);
strftime(ubuf, sizeof(ubuf), "Time: %F %H:%M:%S%z", localtime(&now)); strftime(ubuf, sizeof(ubuf), "Time: %F %H:%M:%S%z", localtime(&now));
g_Font[SNeutrinoSettings::FONT_TYPE_MENU]->RenderString(x+ 10, ypos+ mheight, width - 10, ubuf, COL_MENUCONTENT_TEXT); g_Font[SNeutrinoSettings::FONT_TYPE_MENU]->RenderString(x+ 10, ypos+ mheight, width - 10, ubuf, COL_MENUCONTENT_TEXT);
@@ -386,12 +372,27 @@ void CDBoxInfoWidget::paint()
g_Font[SNeutrinoSettings::FONT_TYPE_MENU]->RenderString(x+ 10, ypos+ mheight, width - 10, ubuf, COL_MENUCONTENT_TEXT); g_Font[SNeutrinoSettings::FONT_TYPE_MENU]->RenderString(x+ 10, ypos+ mheight, width - 10, ubuf, COL_MENUCONTENT_TEXT);
ypos += mheight; ypos += mheight;
if (current_load > -1) { if (data_last > -1) {
snprintf(ubuf, sizeof(ubuf), "Load: %ld%s%ld%%", current_load/10, g_Locale->getText(LOCALE_UNIT_DECIMAL), current_load%10); snprintf(ubuf, sizeof(ubuf), "Load: %d%s%d%%", data_last/10, g_Locale->getText(LOCALE_UNIT_DECIMAL), data_last%10);
g_Font[SNeutrinoSettings::FONT_TYPE_MENU]->RenderString(x+ 10, ypos+ mheight, width - 10, ubuf, COL_MENUCONTENT_TEXT); g_Font[SNeutrinoSettings::FONT_TYPE_MENU]->RenderString(x+ 10, ypos+ mheight, width - 10, ubuf, COL_MENUCONTENT_TEXT);
} }
ypos += mheight; ypos += mheight;
int pbw = width - offsetw - 10;
if (pbw > 8) /* smaller progressbar is not useful ;) */
{
unsigned int h = cpuload_y1 - cpuload_y0;
cpuload_y0 += y;
cpuload_y1 += y;
frameBuffer->paintBoxRel(x + offsetw, cpuload_y0, pbw, h, COL_MENUCONTENT_PLUS_2);
int off = std::max(0, (int)sysload->data_avail - pbw);
for (unsigned int i = 0; i < sysload->data_avail - off; i++) {
if (sysload->data[i + off] > -1)
frameBuffer->paintVLine(x+offsetw + i, cpuload_y1 - sysload->data[i + off] * h / 1000, cpuload_y1, COL_MENUCONTENT_PLUS_7);
}
}
ypos += mheight/2; ypos += mheight/2;
if (frontend_count) { if (frontend_count) {
@@ -457,7 +458,6 @@ void CDBoxInfoWidget::paint()
center = (widths[j] - g_Font[SNeutrinoSettings::FONT_TYPE_MENU]->getRenderWidth(tmp, true))/2; center = (widths[j] - g_Font[SNeutrinoSettings::FONT_TYPE_MENU]->getRenderWidth(tmp, true))/2;
g_Font[SNeutrinoSettings::FONT_TYPE_MENU]->RenderString(x + mpOffset + center, ypos+ mheight, width - 10, tmp, COL_MENUCONTENT_TEXT); g_Font[SNeutrinoSettings::FONT_TYPE_MENU]->RenderString(x + mpOffset + center, ypos+ mheight, width - 10, tmp, COL_MENUCONTENT_TEXT);
} }
int pbw = width - offsetw - 10;
if (pbw > 8) /* smaller progressbar is not useful ;) */ if (pbw > 8) /* smaller progressbar is not useful ;) */
{ {
CProgressBar pb(x+offsetw, ypos+3, pbw, mheight-10); CProgressBar pb(x+offsetw, ypos+3, pbw, mheight-10);
@@ -526,8 +526,6 @@ void CDBoxInfoWidget::paint()
if ((*it).second && icon_w>0 && icon_h>0) if ((*it).second && icon_w>0 && icon_h>0)
frameBuffer->paintIcon(NEUTRINO_ICON_REC, x + nameWidth - icon_w, ypos + (mheight/2 - icon_h/2)); frameBuffer->paintIcon(NEUTRINO_ICON_REC, x + nameWidth - icon_w, ypos + (mheight/2 - icon_h/2));
} }
int pbw = width - offsetw - 10;
//fprintf(stderr, "width: %d offsetw: %d pbw: %d\n", width, offsetw, pbw);
if (pbw > 8) /* smaller progressbar is not useful ;) */ if (pbw > 8) /* smaller progressbar is not useful ;) */
{ {
CProgressBar pb(x+offsetw, ypos+3, pbw, mheight-10); CProgressBar pb(x+offsetw, ypos+3, pbw, mheight-10);

View File

@@ -47,7 +47,6 @@ class CDBoxInfoWidget : public CMenuTarget
int width; int width;
int height; int height;
int hheight,mheight; // head/menu font height int hheight,mheight; // head/menu font height
unsigned long stat_total, stat_idle;
void paint(); void paint();

View File

@@ -106,6 +106,7 @@
#include <system/setting_helpers.h> #include <system/setting_helpers.h>
#include <system/settings.h> #include <system/settings.h>
#include <system/helpers.h> #include <system/helpers.h>
#include <system/sysload.h>
#include <timerdclient/timerdmsg.h> #include <timerdclient/timerdmsg.h>
@@ -1978,6 +1979,8 @@ TIMER_START();
hintBox->hide(); hintBox->hide();
delete hintBox; delete hintBox;
cSysLoad::getInstance();
TIMER_STOP("################################## after all ##################################"); TIMER_STOP("################################## after all ##################################");
RealRun(personalize.getWidget(0)/**main**/); RealRun(personalize.getWidget(0)/**main**/);

View File

@@ -42,5 +42,6 @@ libneutrino_system_a_SOURCES = \
helpers.cpp \ helpers.cpp \
ping.c \ ping.c \
settings.cpp \ settings.cpp \
sysload.cpp \
ytparser.cpp \ ytparser.cpp \
setting_helpers.cpp setting_helpers.cpp

115
src/system/sysload.cpp Normal file
View File

@@ -0,0 +1,115 @@
/*
Neutrino-HD
License: GPL
(C) 2013 martii
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __USE_FILE_OFFSET64
#define __USE_FILE_OFFSET64 1
#endif
#include <string>
#include <iostream>
#include <fstream>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/vfs.h>
#include <fcntl.h>
#include <time.h>
#include <stdarg.h>
#include <global.h>
#include <system/set_threadname.h>
#include <system/sysload.h>
static cSysLoad *instance = NULL;
cSysLoad *cSysLoad::getInstance(void)
{
if (!instance)
instance = new cSysLoad;
return instance;
}
void *cSysLoad::Run(void *arg)
{
set_threadname("sysload");
class cSysLoad *caller = (class cSysLoad *)arg;
unsigned long stat_idle = 0, stat_total = 0;
while (caller->running) {
std::ifstream in("/proc/stat");
if (in.is_open()) {
std::string line;
while (getline(in, line)) {
unsigned long _stat_user, _stat_nice, _stat_system, _stat_idle;
if (4 == sscanf(line.c_str(), "cpu %lu %lu %lu %lu", &_stat_user, &_stat_nice, &_stat_system, &_stat_idle)) {
if (stat_total) {
unsigned long div = _stat_user + _stat_nice + _stat_system + _stat_idle - stat_total;
caller->data_last = (int)(1000 - 1000 * (_stat_idle - stat_idle) / div);
if (caller->data_avail < caller->data_size) {
caller->data[caller->data_avail++] = caller->data_last;
} else {
memmove(caller->data, caller->data + 1, (caller->data_size - 1) * sizeof(int));
caller->data[caller->data_size - 1] = caller->data_last;
}
}
stat_idle = _stat_idle;
stat_total = _stat_user + _stat_nice + _stat_system + _stat_idle;
break;
}
}
in.close();
}
timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += caller->period;
sem_timedwait(&caller->sem, &ts);
}
pthread_exit(NULL);
}
cSysLoad::cSysLoad(void)
{
data_last = -1;
data_avail = 0;
period = 5;
data_size = 1800/period;
data = new int[data_size];
for (unsigned int i = 0; i < data_size; i++)
data[i] = -1;
running = true;
sem_init(&sem, 0, 0);
if (pthread_create(&thr, NULL, Run, this))
running = false;
}
cSysLoad::~cSysLoad(void)
{
if (running) {
running = false;
sem_post(&sem);
pthread_join(thr, NULL);
}
sem_destroy(&sem);
delete[] data;
}

51
src/system/sysload.h Normal file
View File

@@ -0,0 +1,51 @@
/*
Neutrino-HD
License: GPL
(C) 2013 martii
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __SYSTEM_SYSLOAD__H_
#define __SYSTEM_SYSLOAD__H_
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string>
#include <vector>
#include <pthread.h>
#include <semaphore.h>
class cSysLoad
{
private:
pthread_t thr;
cSysLoad();
static void* Run(void *);
public:
int *data;
size_t data_avail;
size_t data_size;
unsigned int period;
bool running;
sem_t sem;
~cSysLoad(void);
static cSysLoad *getInstance(void);
int data_last;
};
#endif