diff --git a/src/gui/dboxinfo.cpp b/src/gui/dboxinfo.cpp index 82e3246fe..abdc7d88d 100644 --- a/src/gui/dboxinfo.cpp +++ b/src/gui/dboxinfo.cpp @@ -54,6 +54,7 @@ #include #include +#include #include #include #include @@ -75,7 +76,6 @@ CDBoxInfoWidget::CDBoxInfoWidget() height = 0; x = 0; y = 0; - stat_total = 0; } @@ -211,14 +211,18 @@ void CDBoxInfoWidget::paint() int nameWidth = fontWidth * 17;//WWWwwwwwww height = hheight; height += mheight/2; // space + int cpuload_y0 = height; height += mheight; // time height += mheight; // uptime height += mheight; // load + int cpuload_y1 = height; height += mheight/2; // space int frontend_count = CFEManager::getInstance()->getFrontendCount(); - if (frontend_count) - height += mheight * frontend_count + mheight/2; + if (frontend_count) { + height += mheight * frontend_count; + height += mheight/2; + } int icon_w = 0, icon_h = 0; frameBuffer->getIconSize(NEUTRINO_ICON_REC, &icon_w, &icon_h); @@ -352,28 +356,10 @@ void CDBoxInfoWidget::paint() ypos += hheight + mheight/2; - long current_load = -1; - in.open("/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; - 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(); - } + cSysLoad *sysload = cSysLoad::getInstance(); + int data_last = sysload->data_last; char ubuf[80]; - time_t now = time(NULL); 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); @@ -386,12 +372,27 @@ void CDBoxInfoWidget::paint() g_Font[SNeutrinoSettings::FONT_TYPE_MENU]->RenderString(x+ 10, ypos+ mheight, width - 10, ubuf, COL_MENUCONTENT_TEXT); ypos += mheight; - if (current_load > -1) { - snprintf(ubuf, sizeof(ubuf), "Load: %ld%s%ld%%", current_load/10, g_Locale->getText(LOCALE_UNIT_DECIMAL), current_load%10); + if (data_last > -1) { + 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); } 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; if (frontend_count) { @@ -457,7 +458,6 @@ void CDBoxInfoWidget::paint() 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); } - int pbw = width - offsetw - 10; if (pbw > 8) /* smaller progressbar is not useful ;) */ { 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) 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 ;) */ { CProgressBar pb(x+offsetw, ypos+3, pbw, mheight-10); diff --git a/src/gui/dboxinfo.h b/src/gui/dboxinfo.h index 4a8cc52ee..5587ba7d0 100644 --- a/src/gui/dboxinfo.h +++ b/src/gui/dboxinfo.h @@ -47,7 +47,6 @@ class CDBoxInfoWidget : public CMenuTarget int width; int height; int hheight,mheight; // head/menu font height - unsigned long stat_total, stat_idle; void paint(); diff --git a/src/neutrino.cpp b/src/neutrino.cpp index 56ab88e34..7548983b2 100644 --- a/src/neutrino.cpp +++ b/src/neutrino.cpp @@ -106,6 +106,7 @@ #include #include #include +#include #include @@ -1978,6 +1979,8 @@ TIMER_START(); hintBox->hide(); delete hintBox; + cSysLoad::getInstance(); + TIMER_STOP("################################## after all ##################################"); RealRun(personalize.getWidget(0)/**main**/); diff --git a/src/system/Makefile.am b/src/system/Makefile.am index 11bad7cd8..bee2e4350 100644 --- a/src/system/Makefile.am +++ b/src/system/Makefile.am @@ -42,5 +42,6 @@ libneutrino_system_a_SOURCES = \ helpers.cpp \ ping.c \ settings.cpp \ + sysload.cpp \ ytparser.cpp \ setting_helpers.cpp diff --git a/src/system/sysload.cpp b/src/system/sysload.cpp new file mode 100644 index 000000000..5338adcc9 --- /dev/null +++ b/src/system/sysload.cpp @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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; +} diff --git a/src/system/sysload.h b/src/system/sysload.h new file mode 100644 index 000000000..7cfa9535b --- /dev/null +++ b/src/system/sysload.h @@ -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 +#endif + +#include +#include +#include +#include + +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