File: CMonitorProcesses.cpp

package info (click to toggle)
unixodbc 2.2.14p2-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 14,628 kB
  • ctags: 12,533
  • sloc: ansic: 104,243; cpp: 38,571; sh: 15,958; makefile: 2,727; sql: 1
file content (135 lines) | stat: -rw-r--r-- 4,161 bytes parent folder | download | duplicates (2)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*!
 * \file
 *
 * \author  Peter Harvey <pharvey@peterharvey.org>
 * \author  \sa AUTHORS file
 * \version 2
 * \date    2007
 * \license Copyright unixODBC Project 2007-2008, LGPL
 */
#include "CMonitorProcesses.h"

#define MAXPROCESSES 100
#define MAXHANDLES 4

CMonitorProcesses::CMonitorProcesses( QWidget* pwidgetParent )
	: QTableWidget( pwidgetParent )
{
    setToolTip( tr( "list of processes currently owning one or more ODBC handles" ) );
    setWhatsThis( tr( "This shows a list of processes currently owning one or more ODBC handles. The number of handles are shown. This is revised frequently while in view. This is useful for debugging and monitoring performance." ) );

    // lets assume, for the moment, that we can get monitoring info from DM...
    bEnabled = true;

    // setup table...
    {
        // we remember rows from last load so we can optimize next load...
        nRowsWithValues = 0;

        // we use a fixed number of row/cols so init here...
        setRowCount( MAXPROCESSES );
        setColumnCount( 5 );

        // init headers and decorations...
        {
            QStringList stringlist;
            setHorizontalHeaderLabels( stringlist << "PID" << "Environments" << "Connections" << "Statements" << "Descriptors" );
            setSelectionBehavior( QAbstractItemView::SelectRows );
            setSelectionMode( QAbstractItemView::SingleSelection );
            verticalHeader()->setVisible( false );
        }
        
        // preload table with item objects...
        for ( int nRow = 0; nRow < rowCount(); nRow++ )
        {
            for ( int nCol = 0; nCol < columnCount(); nCol++ )
            {
                setItem( nRow, nCol, new QTableWidgetItem( "" ) );
            }
        }
    }

    // init timer...
    {
        hStats = 0;
        pTimer = new QTimer( this );
        connect( pTimer, SIGNAL(timeout()), SLOT(slotLoad()) );
        pTimer->start( 2000 ); // 1000 = 1second
    }
}

CMonitorProcesses::~CMonitorProcesses()
{
    if ( hStats )
        uodbc_close_stats( hStats );
}

void CMonitorProcesses::slotLoad()
{
    // only bother with this if we are visible...
    if ( !isVisible() )
        return;

    if ( !bEnabled )
        return;

    // init coms with DM but don't even bother the DM until (if) we become visible... 
    if ( !hStats )
    {
        if ( uodbc_open_stats( &hStats, UODBC_STATS_READ ) != 0 )
        {
//            char szError[512];
//            QMessageBox::critical( this, tr( "Monitor Processes" ),  QString( "uodbc_open_stats failed\n%1" ).arg( uodbc_stats_error( szError, 512 ) ) );
//            bEnabled = false;
            return;
        }
    }

    // get current processes info...
    {
        uodbc_stats_retentry aPIDs[MAXPROCESSES];
        int nPIDs = uodbc_get_stats( hStats, 0, aPIDs, MAXPROCESSES );

        for ( int nPID = 0; nPID < MAXPROCESSES; nPID++ )
        {
            if ( nPID < nPIDs )
            {
                // get handle count for current process...
                uodbc_stats_retentry aHandles[MAXHANDLES];
                int nHandles = uodbc_get_stats( hStats, aPIDs[nPID].value.l_value, aHandles, MAXHANDLES );

                if ( nHandles > 0 )
                {
                    item( nPID, 0 )->setText( QString( "%1" ).arg( aPIDs[nPID].value.l_value ) );
                    for ( int nHandle = 0; nHandle < MAXHANDLES; nHandle++ )
                    {
                        item( nPID, nHandle + 1 )->setText( QString( "%1" ).arg( aHandles[nHandle].value.l_value ) );
                    }
                }
                else
                    clearRow( nPID );
            }
            else
            {
                // no need to clear remaining rows if no data in them...
                if ( nPID >= nRowsWithValues )
                {
                    nRowsWithValues = nPID;
                    return;
                }
                clearRow( nPID );
            }
        }
    }
}

void CMonitorProcesses::clearRow( int nRow )
{
    int nCol;

    for ( nCol = 0; nCol < columnCount(); nCol++ )
    {
        item( nRow, nCol )->setText( "" );
    }
}