summaryrefslogtreecommitdiffstats
path: root/src/kcpuproc.h
blob: 8b844f5ab8c3197a5ebf8463656b52254a22b98b (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

/***************************************************************************
 *                                                                         *
 *   KCPULoad is copyright (c) 1999-2000, Markus Gustavsson                *
 *                         (c) 2002, Ben Burton                            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef __KCPUPROC_H
#define __KCPUPROC_H

#include <cstdio>
#include <tqglobal.h>
#include <vector>

/**
 * A class used to read raw CPU load details from the system.
 *
 * See kcpuproc.cpp for details of supported operating systems.
 */
class KCPUProc {
public:
    /**
     * Constructor.
     *
     * In the constructor, a set of initial CPU tick readings are taken
     * and SMP support is determined.
     */
    KCPUProc();

    /**
     * Does this system appear to have SMP?
     */
    bool hasSMP() const;

    /**
     * Takes a fresh set of CPU tick readings.  The numerical statistics
     * returned refer to the time period between this reading and the
     * previous (or between this reading and the object's construction
     * if there was no previous reading).
     */
    void readLoad();

    /**
     * Contains tick differences for a CPU.
     */
    struct CPU {
        /**
         * Contains user/system/nice/idle tick readings.
         */
        struct Ticks {
            int U, S, N, I;
            Ticks();
            /** Returns user ticks */
            int u() const { return U; }
            /** Returns system ticks */
            int s() const { return S + N; }
            /** Returns total (non-idle) ticks */
            int t() const { return U + S + N; }
            /** Returns elapsed ticks */
            int e() const { return U + S + N + I; }
        };

        Ticks p;        /**< The previous tick readings */
        Ticks c;        /**< The last (most recent) tick readings */

        /**
         * The percentage of ticks between the last reading and the previous
         * reading used by the user loop (compared to the nice, system and
         * idle loops).
         *
         * This routine involves a short arithmetical calculation.
         * If you're paranoid about running time, you might want to cache
         * the result.
         */
        int userPercent() const;

        /**
         * The percentage of ticks between the last reading and the previous
         * reading used by the system and nice loops (compared to the user and
         * idle loops).
         *
         * This routine involves a short arithmetical calculation.
         * If you're paranoid about running time, you might want to cache
         * the result.
         */
        int systemPercent() const;

        /**
         * The percentage of ticks between the last reading and the previous
         * reading used by the user, system and nice loops (compared to the
         * idle loop).
         *
         * This routine involves a short arithmetical calculation.
         * If you're paranoid about running time, you might want to cache
         * the result.
         */
        int totalPercent() const;

        /**
         * OS-specific helper routines.
         */

        // ========== Linux-specific (begin) ==========
#ifdef Q_OS_LINUX
        /**
         * Parses the tick readings of the current line in #fd.
         * The return value is \c false if the line does not begin with
         * the tag "cpu" (optionally followed by a number).
         * If the line can be parsed successfully, the last reading becomes
         * the previous reading and the newly parsed reading becomes
         * the last reading.
         */
        bool parse(FILE* fd);
#endif
        // ========== Linux-specific (end) ==========
    };

    /*
     * Variables used in all modes.
     */
    CPU all;    /**< Tick readings for all CPUs. */

    /*
     * Variables used only with SMP.
     */
    std::vector<CPU> cpu;   /**< Tick readings for CPUs. */

private:
    /*
     * SMP support.
     */
    bool smp;
        /**< Does this system appear to have SMP? */

    /**
     * OS-specific data members.
     */

    // ========== Linux-specific (begin) ==========
#ifdef Q_OS_LINUX
    FILE *fd;
        /**< The file /proc/stat. */
#endif
    // ========== Linux-specific (end) ==========
};

inline bool KCPUProc::hasSMP() const {
    return smp;
}

inline int KCPUProc::CPU::userPercent() const {
    int tot = c.e() - p.e();
    return (tot > 0 ? (100 * (c.u() - p.u())) / tot : 0);
}
inline int KCPUProc::CPU::systemPercent() const {
    int tot = c.e() - p.e();
    return (tot > 0 ? (100 * (c.s() - p.s())) / tot : 0);
}
inline int KCPUProc::CPU::totalPercent() const {
    int tot = c.e() - p.e();
    return (tot > 0 ? (100 * (c.t() - p.t())) / tot : 0);
}

#endif