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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 
51 
52 #include "MemInfo.hh"
53 #include <OpenFlipper/Utils/Memory/RAMInfo.hh>
54 
55 // GPU Memory information
56 #define GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX 0x9048
57 #define GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX 0x9049
58 
60 {
61 
62 }
63 
64 
65 void MemInfoPlugin::pluginsInitialized() {
66 
67  // Check extension for NVIDIA memory information
68  QString glExtensions = QString((const char*)glGetString(GL_EXTENSIONS));
69 
70  updateTimer_ = new QTimer();
71  updateTimer_->setSingleShot(false);
72 
73  // NVIDIA cards
74  if ( glExtensions.contains("GL_NVX_gpu_memory_info") ) {
75 
76 // emit log(LOGINFO,"NVIDIA card Memory info supported, installing gpu memory monitor into status bar");
77 
78  gpuMemBar_= new QProgressBar();
79  gpuMemBar_->setFixedWidth(260);
80  gpuMemBar_->setFormat( "GPU Mem %p% %v/%m MB" );
81 
82  emit addWidgetToStatusbar(gpuMemBar_);
83 
84  connect(updateTimer_,SIGNAL(timeout()),this,SLOT(nvidiaMemoryInfoUpdate()));
85 
86  updateTimer_->start(4000);
87 
89  }
90 
91  // Main Memory information
92 // #ifdef ARCH_DARWIN //Apple
93 
94 // #else // Linux and Windows
95 
96  // emit log(LOGINFO,"Main Memory monitoring supported, installing main memory monitor into status bar");
97 
98  mainMemBar_= new QProgressBar();
99  mainMemBar_->setFixedWidth(260);
100  mainMemBar_->setFormat( "Mem %p% %v/%m MB" );
101 
102  emit addWidgetToStatusbar(mainMemBar_);
103 
104  connect(updateTimer_,SIGNAL(timeout()),this,SLOT(cpuMemoryInfoUpdate()));
105 
106  updateTimer_->start(4000);
107 
109 
110  //#endif
111 
112 }
113 
114 MemInfoPlugin::MemInfoPlugin():
115  gpuMemBar_(NULL),
116  mainMemBar_(NULL),
117  updateTimer_(NULL)
118 {
119 
120 }
121 
122 MemInfoPlugin::~MemInfoPlugin() {
123  delete updateTimer_;
124 }
125 
127 
128  if (gpuMemBar_) {
129  // get total memory on gpu
130  GLint total_mem_kb = 0;
131  glGetIntegerv(GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, &total_mem_kb);
132 
133  // get currently available memory on gpu
134  GLint cur_avail_mem_kb = 0;
135  glGetIntegerv(GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &cur_avail_mem_kb);
136 
137  gpuMemBar_->setRange( 0 , total_mem_kb/1024 );
138  gpuMemBar_->setValue( (total_mem_kb-cur_avail_mem_kb)/1024);
140  }
141 }
142 
144 
145  if (mainMemBar_) {
146  unsigned long totalRamMB = Utils::Memory::queryTotalRAM();
147  unsigned long freeRamMB = Utils::Memory::queryFreeRAM();
148 
149  mainMemBar_->setRange( 0 , totalRamMB );
150  mainMemBar_->setValue( totalRamMB - freeRamMB );
152  }
153 }
154 
155 
157 {
158  const int val = _bar->value();
159  const int maxVal = _bar->maximum();
160 
161  // red starts with 50% mem alloc with 0 and is 1 at 100% mem alloc
162  float redPerc = (val > 0.5f*maxVal) ? 2.f*(val-0.5f*maxVal) : 0.f;
163  const quint32 red = 255.f*redPerc/maxVal;
164  const quint32 green = 255u-red;
165  const quint32 blue = 0u;
166 
167  //save color in a 32bit integer
168  const quint32 color = (red<<16)+(green<<8)+blue;
169  _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')));
170 
171 }
172 
173 #if QT_VERSION < 0x050000
174  Q_EXPORT_PLUGIN2( meminfoplugin , MemInfoPlugin );
175 #endif
176 
void setProgressBarStyleSheet(QProgressBar *_bar)
Sets the Qt Stylesheet for the progress bars.
Definition: MemInfo.cc:156
QTimer * updateTimer_
Timer that triggers an update of the bars.
Definition: MemInfo.hh:108
void nvidiaMemoryInfoUpdate()
Update statusbar with NVIDIA memory infos.
Definition: MemInfo.cc:126
void initializePlugin()
BaseInterface.
Definition: MemInfo.cc:59
void cpuMemoryInfoUpdate()
Update statusbar with main memory infos.
Definition: MemInfo.cc:143
QProgressBar * gpuMemBar_
Status bar for GPU memory.
Definition: MemInfo.hh:102
QProgressBar * mainMemBar_
Status bar for Main memory.
Definition: MemInfo.hh:105