MemInfo.cc 6.3 KB
Newer Older
1 2 3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
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 37 38
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
39 40 41
*                                                                            *
\*===========================================================================*/

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

43 44

#include "MemInfo.hh"
Martin Schultz's avatar
Martin Schultz committed
45
#include <OpenFlipper/Utils/Memory/RAMInfo.hh>
46

47 48
#include <QTimer>

Martin Schultz's avatar
Martin Schultz committed
49
// GPU Memory information
50 51 52 53 54 55 56 57 58 59 60 61
#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
62
 // QString glExtensions = QString((const char*)glGetString(GL_EXTENSIONS));
63 64 65 66 67

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

  // NVIDIA cards
68
  if ( ACG::checkExtensionSupported("GL_NVX_gpu_memory_info") ) {
69

70
//    emit log(LOGINFO,"NVIDIA card Memory info supported, installing gpu memory monitor into status bar");
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

    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
86
//  #ifdef ARCH_DARWIN //Apple
87

88
//  #else // Linux and Windows
89

90
 //   emit log(LOGINFO,"Main Memory monitoring supported, installing main memory monitor into status bar");
91

92
    mainMemBar_= new QProgressBar();
Martin Schultz's avatar
Martin Schultz committed
93 94 95 96 97 98
    mainMemBar_->setFixedWidth(260);
    mainMemBar_->setFormat( "Mem %p% %v/%m MB" );

    emit addWidgetToStatusbar(mainMemBar_);

    connect(updateTimer_,SIGNAL(timeout()),this,SLOT(cpuMemoryInfoUpdate()));
99

100
    updateTimer_->start(4000);
101

102
    cpuMemoryInfoUpdate();
103

104
  //#endif
105 106 107

}

108 109 110 111 112 113 114 115
MemInfoPlugin::MemInfoPlugin():
    gpuMemBar_(NULL),
    mainMemBar_(NULL),
    updateTimer_(NULL)
{

}

116
MemInfoPlugin::~MemInfoPlugin() {
117
  delete updateTimer_;
118 119 120 121 122 123 124 125 126 127 128 129
}

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);
130

Martin Schultz's avatar
Martin Schultz committed
131 132 133 134 135
    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
136

Martin Schultz's avatar
Martin Schultz committed
137
void MemInfoPlugin::cpuMemoryInfoUpdate() {
Jan Möbius's avatar
Jan Möbius committed
138

Martin Schultz's avatar
Martin Schultz committed
139 140 141
  if (mainMemBar_) {
    unsigned long totalRamMB = Utils::Memory::queryTotalRAM();
    unsigned long freeRamMB = Utils::Memory::queryFreeRAM();
Jan Möbius's avatar
Jan Möbius committed
142

Martin Schultz's avatar
Martin Schultz committed
143
    mainMemBar_->setRange(  0 , totalRamMB  );
144
    mainMemBar_->setValue( totalRamMB - freeRamMB );
Martin Schultz's avatar
Martin Schultz committed
145 146 147
    setProgressBarStyleSheet(mainMemBar_);
  }
}
148

149

150 151 152 153 154 155
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
156 157 158 159
  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;
160 161

  //save color in a 32bit integer
162
  const quint32 color = (red<<16)+(green<<8)+blue;
163
  _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')));
164
  
Martin Schultz's avatar
Martin Schultz committed
165
}
166 167