MemInfo.cc 8.11 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper 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 Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
Jan Möbius's avatar
Prpset  
Jan Möbius committed
37 38 39
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
Jan Möbius's avatar
Jan Möbius committed
40 41 42 43 44 45 46
*                                                                            *
\*===========================================================================*/



#include "MemInfo.hh"
#include <ACG/GL/gl.hh>
Matthias Möller's avatar
Matthias Möller committed
47
#include <QtGlobal>
Jan Möbius's avatar
Jan Möbius committed
48 49 50 51 52 53

// Main Memory information
#ifdef WIN32
#elif defined ARCH_DARWIN
#else
#include <sys/sysinfo.h>
Jan Möbius's avatar
Jan Möbius committed
54
//Info class returned by sysinfo
Jan Möbius's avatar
Jan Möbius committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
//struct sysinfo {
//  long uptime;              /* Sekunden seit dem letzten Systemstart */
//  unsigned long loads[3];   /* 1, 5 und 15 minütige Systemlast */
//  unsigned long totalram;   /* nutzbare Hauptspeichergröße */
//  unsigned long freeram;    /* verfügbare Speichergröße */
//  unsigned long sharedram;  /* Größe des gemeinsamen Speichers */
//  unsigned long bufferram;  /* von Puffern benutzter Speicher */
//  unsigned long totalswap;  /* Größe des Auslagerungsspeichers */
//  unsigned long freeswap;   /* verfügbarer Auslagerungsspeicher */
//  unsigned short procs;     /* Aktuelle Prozesszahl */
//  unsigned long totalhigh;  /* Gesamtgröße des oberen Speicherbereichs */
//  unsigned long freehigh;   /* verfügbarer oberer Speicherbereich */
//  unsigned int mem_unit;    /* Größe der Speichereinheit in Byte */
//  char _f[20-2*sizeof(long)-sizeof(int)]; /* Auffüllung auf 64 bytes */
//};
#endif

#define GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX   0x9048
#define GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX 0x9049

void MemInfoPlugin::initializePlugin()
{

}


void MemInfoPlugin::pluginsInitialized() {

  // Check extension for NVIDIA memory information
  QString glExtensions = QString((const char*)glGetString(GL_EXTENSIONS));

  updateTimer_ = new QTimer();
  updateTimer_->setSingleShot(false);

  // NVIDIA cards
  if ( glExtensions.contains("GL_NVX_gpu_memory_info") ) {

92
//    emit log(LOGINFO,"NVIDIA card Memory info supported, installing gpu memory monitor into status bar");
Jan Möbius's avatar
Jan Möbius committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

    gpuMemBar_= new QProgressBar();
    gpuMemBar_->setFixedWidth(260);
    gpuMemBar_->setFormat( "GPU Mem %p% %v/%m MB" );

    emit addWidgetToStatusbar(gpuMemBar_);

    connect(updateTimer_,SIGNAL(timeout()),this,SLOT(nvidiaMemoryInfoUpdate()));

    updateTimer_->start(4000);

    nvidiaMemoryInfoUpdate();
  }

  // Main Memory information
Jan Möbius's avatar
Jan Möbius committed
108
  #ifdef ARCH_DARWIN //Apple
Jan Möbius's avatar
Jan Möbius committed
109

Jan Möbius's avatar
Jan Möbius committed
110
  #else // Linux and Windows
Jan Möbius's avatar
Jan Möbius committed
111

112
 //   emit log(LOGINFO,"Main Memory monitoring supported, installing main memory monitor into status bar");
Jan Möbius's avatar
Jan Möbius committed
113

Jan Möbius's avatar
Jan Möbius committed
114
    mainMemBar_= new QProgressBar();
115 116 117 118 119 120
    mainMemBar_->setFixedWidth(260);
    mainMemBar_->setFormat( "Mem %p% %v/%m MB" );

    emit addWidgetToStatusbar(mainMemBar_);

    connect(updateTimer_,SIGNAL(timeout()),this,SLOT(cpuMemoryInfoUpdate()));
Jan Möbius's avatar
Jan Möbius committed
121

Jan Möbius's avatar
Jan Möbius committed
122
    updateTimer_->start(4000);
Jan Möbius's avatar
Jan Möbius committed
123

Jan Möbius's avatar
Jan Möbius committed
124
    cpuMemoryInfoUpdate();
Jan Möbius's avatar
Jan Möbius committed
125 126 127 128 129

  #endif

}

130 131 132 133 134 135 136 137
MemInfoPlugin::MemInfoPlugin():
    gpuMemBar_(NULL),
    mainMemBar_(NULL),
    updateTimer_(NULL)
{

}

Jan Möbius's avatar
Jan Möbius committed
138
MemInfoPlugin::~MemInfoPlugin() {
139
  delete updateTimer_;
Jan Möbius's avatar
Jan Möbius committed
140 141 142 143 144 145 146 147 148 149 150 151
}

void MemInfoPlugin::nvidiaMemoryInfoUpdate() {

  if (gpuMemBar_) {
    // get total memory on gpu
    GLint total_mem_kb = 0;
    glGetIntegerv(GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, &total_mem_kb);

    // get currently available memory on gpu
    GLint cur_avail_mem_kb = 0;
    glGetIntegerv(GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &cur_avail_mem_kb);
152 153 154 155 156 157 158

    gpuMemBar_->setRange(  0 , total_mem_kb/1024 );
    gpuMemBar_->setValue( (total_mem_kb-cur_avail_mem_kb)/1024);
    setProgressBarStyleSheet(gpuMemBar_);
  }
}

Jan Möbius's avatar
Jan Möbius committed
159 160 161 162
void MemInfoPlugin::cpuMemoryInfoUpdate() {

  if (mainMemBar_) {

163 164 165
    unsigned long totalRamMB   = 0;
    unsigned long freeRamMB    = 0;
    unsigned long bufferRamMB  = 0;
Jan Möbius's avatar
Jan Möbius committed
166

Jan Möbius's avatar
Jan Möbius committed
167 168
    // Main Memory information
    #ifdef WIN32 //Windows
Jan Möbius's avatar
Jan Möbius committed
169 170 171 172 173 174 175
      // Define memory structure
      MEMORYSTATUSEX ms;
      // Set the size ( required according to spec ... why???? )
      ms.dwLength = sizeof (ms);
      // Get the info
      GlobalMemoryStatusEx(&ms);

176
	  totalRamMB = ms.ullTotalPhys/1024/1024;
Jan Möbius's avatar
Jan Möbius committed
177
      freeRamMB  = ms.ullAvailPhys/1024/1024;
Jan Möbius's avatar
Jan Möbius committed
178

Jan Möbius's avatar
Jan Möbius committed
179
    #elif defined ARCH_DARWIN // Apple
Jan Möbius's avatar
Jan Möbius committed
180

Jan Möbius's avatar
Jan Möbius committed
181
    #else // Linux
Jan Möbius's avatar
Jan Möbius committed
182

Jan Möbius's avatar
Jan Möbius committed
183 184 185
      struct sysinfo sys_info;
      sysinfo(&sys_info);

186 187


Jan Möbius's avatar
Jan Möbius committed
188
      // Unit in bytes ; /1024 -> KB ; /1024 MB
189 190 191
      totalRamMB  = sys_info.totalram  / 1024 / 1024 * sys_info.mem_unit;
      freeRamMB   = sys_info.freeram   / 1024 / 1024 * sys_info.mem_unit;
      bufferRamMB = sys_info.bufferram / 1024 / 1024 * sys_info.mem_unit; // Buffers get freed, if we don't have enough free ram
Jan Möbius's avatar
Jan Möbius committed
192
    #endif
193 194 195 196 197 198

    mainMemBar_->setRange(  0 , totalRamMB  );
    mainMemBar_->setValue( totalRamMB-freeRamMB-bufferRamMB);
    setProgressBarStyleSheet(mainMemBar_);
  }
}
Jan Möbius's avatar
Jan Möbius committed
199

Jan Möbius's avatar
Jan Möbius committed
200

201 202 203 204 205 206
void MemInfoPlugin::setProgressBarStyleSheet(QProgressBar* _bar)
{
  const int val = _bar->value();
  const int maxVal = _bar->maximum();
  
  // red starts with 50% mem alloc with 0 and is 1 at 100% mem alloc
Matthias Möller's avatar
Matthias Möller committed
207 208 209 210
  float redPerc = (val > 0.5f*maxVal) ? 2.f*(val-0.5f*maxVal) : 0.f;
  const quint32 red = 255.f*redPerc/maxVal;
  const quint32 green = 255u-red;
  const quint32 blue = 0u;
211 212

  //save color in a 32bit integer
Matthias Möller's avatar
Matthias Möller committed
213
  const quint32 color = (red<<16)+(green<<8)+blue;
Jan Möbius's avatar
Jan Möbius committed
214
  _bar->setStyleSheet(QString(" QProgressBar { border: 2px solid grey; border-radius: 2px; text-align: center; } QProgressBar::chunk {background-color: #%1; width: 1px;}").arg(color,6,16,QChar('0')));
215 216
  
}
Jan Möbius's avatar
Jan Möbius committed
217 218 219 220 221

#if QT_VERSION < 0x050000
  Q_EXPORT_PLUGIN2( meminfoplugin , MemInfoPlugin );
#endif