Developer Documentation
MemInfo.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 
43 
44 #include "MemInfo.hh"
45 #include <OpenFlipper/Utils/Memory/RAMInfo.hh>
46 
47 #include <QTimer>
48 
49 // GPU Memory information
50 #define GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX 0x9048
51 #define GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX 0x9049
52 
54 {
55 
56 }
57 
58 
59 void MemInfoPlugin::pluginsInitialized() {
60 
61  // Check extension for NVIDIA memory information
62  // QString glExtensions = QString((const char*)glGetString(GL_EXTENSIONS));
63 
64  updateTimer_ = new QTimer();
65  updateTimer_->setSingleShot(false);
66 
67  // 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  gpuMemBar_= new QProgressBar();
73  gpuMemBar_->setFixedWidth(260);
74  gpuMemBar_->setFormat( "GPU Mem %p% %v/%m MB" );
75 
76  emit addWidgetToStatusbar(gpuMemBar_);
77 
78  connect(updateTimer_,SIGNAL(timeout()),this,SLOT(nvidiaMemoryInfoUpdate()));
79 
80  updateTimer_->start(4000);
81 
83  }
84 
85  // 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();
93  mainMemBar_->setFixedWidth(260);
94  mainMemBar_->setFormat( "Mem %p% %v/%m MB" );
95 
96  emit addWidgetToStatusbar(mainMemBar_);
97 
98  connect(updateTimer_,SIGNAL(timeout()),this,SLOT(cpuMemoryInfoUpdate()));
99 
100  updateTimer_->start(4000);
101 
103 
104  //#endif
105 
106 }
107 
108 MemInfoPlugin::MemInfoPlugin():
109  gpuMemBar_(NULL),
110  mainMemBar_(NULL),
111  updateTimer_(NULL)
112 {
113 
114 }
115 
116 MemInfoPlugin::~MemInfoPlugin() {
117  delete updateTimer_;
118 }
119 
121 
122  if (gpuMemBar_) {
123  // get total memory on gpu
124  GLint total_mem_kb = 0;
125  glGetIntegerv(GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, &total_mem_kb);
126 
127  // get currently available memory on gpu
128  GLint cur_avail_mem_kb = 0;
129  glGetIntegerv(GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &cur_avail_mem_kb);
130 
131  gpuMemBar_->setRange( 0 , total_mem_kb/1024 );
132  gpuMemBar_->setValue( (total_mem_kb-cur_avail_mem_kb)/1024);
134  }
135 }
136 
138 
139  if (mainMemBar_) {
140  unsigned long totalRamMB = Utils::Memory::queryTotalRAM();
141  unsigned long freeRamMB = Utils::Memory::queryFreeRAM();
142 
143  mainMemBar_->setRange( 0 , totalRamMB );
144  mainMemBar_->setValue( totalRamMB - freeRamMB );
146  }
147 }
148 
149 
151 {
152  const int val = _bar->value();
153  const int maxVal = _bar->maximum();
154 
155  // red starts with 50% mem alloc with 0 and is 1 at 100% mem alloc
156  float redPerc = (val > 0.5f*maxVal) ? 2.f*(val-0.5f*maxVal) : 0.f;
157  const quint32 red = 255.f*redPerc/maxVal;
158  const quint32 green = 255u-red;
159  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 
165 }
166 
167 
void nvidiaMemoryInfoUpdate()
Update statusbar with NVIDIA memory infos.
Definition: MemInfo.cc:120
void cpuMemoryInfoUpdate()
Update statusbar with main memory infos.
Definition: MemInfo.cc:137
void setProgressBarStyleSheet(QProgressBar *_bar)
Sets the Qt Stylesheet for the progress bars.
Definition: MemInfo.cc:150
QProgressBar * gpuMemBar_
Status bar for GPU memory.
Definition: MemInfo.hh:94
void initializePlugin()
BaseInterface.
Definition: MemInfo.cc:53
bool checkExtensionSupported(const std::string &_extension)
Definition: gl.cc:107
QProgressBar * mainMemBar_
Status bar for Main memory.
Definition: MemInfo.hh:97
QTimer * updateTimer_
Timer that triggers an update of the bars.
Definition: MemInfo.hh:100