Logo Search packages:      
Sourcecode: kcpuload version File versions  Download package

void KCPUProc::readLoad (  ) 

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).

Definition at line 115 of file kcpuproc.cpp.

References cIT, cIT0, cIT1, IT, IT0, IT1, smp, tot, tot0, tot1, UT, and UT0.

Referenced by KCPUProc(), and KCPULoad::takeReadingInternal().

                        {
    // OS-specific local variables.

    // ========== BSD-specific (begin) ==========
#ifdef Q_OS_BSD4
    static int name2oid[2] = { 0, 3 };
    static int oidCpuTime[CTL_MAXNAME + 2];
    static size_t oidCpuTimeLen = sizeof(oidCpuTime);
    long cpuTime[CPUSTATES];
    unsigned int cpuTimeLen = sizeof(cpuTime);
    static char *name = "kern.cp_time";
    static int initialized = 0;
#endif
    // ========== BSD-specific (end) ==========

    // Prepare to take the readings.

    // ========== Linux-specific (begin) ==========
#ifdef Q_OS_LINUX
    if ((fd = fopen("/proc/stat", "r")) == 0)
        return;
#endif
    // ========== Linux-specific (end) ==========

    if(smp) {
        // The current readings must now become the previous readings.
        UT = cUT;
        ST = cST;
        NT = cNT;
        IT = cIT;

        UT0 = cUT0;
        ST0 = cST0;
        NT0 = cNT0;
        IT0 = cIT0;

        UT1 = cUT1;
        ST1 = cST1;
        NT1 = cNT1;
        IT1 = cIT1;

        // Take a fresh set of current readings (SMP mode).

        // ========== Linux-specific (begin) ==========
#ifdef Q_OS_LINUX
        fscanf(fd, "%32s %d %d %d %d", tag, &cUT, &cNT, &cST, &cIT);
        fscanf(fd, "%32s %d %d %d %d", tag, &cUT0, &cNT0, &cST0, &cIT0);
        fscanf(fd, "%32s %d %d %d %d", tag, &cUT1, &cNT1, &cST1, &cIT1);
#endif
        // ========== Linux-specific (end) ==========

        // ========== BSD-specific (begin) ==========
#ifdef Q_OS_BSD4
        // TODO: Add SMP support for BSD.
#endif
        // ========== BSD-specific (end) ==========

        // Calculate final totals.
        tot = (cUT - UT) + (cST - ST) + (cNT - NT) + (cIT - IT);
        tot0 = (cUT0 - UT0) + (cST0 - ST0) + (cNT0 - NT0) + (cIT0 - IT0);
        tot1 = (cUT1 - UT1) + (cST1 - ST1) + (cNT1 - NT1) + (cIT1 - IT1);
    } else {
        // The current readings must now become the previous readings.
        UT = cUT;
        ST = cST;
        NT = cNT;
        IT = cIT;

        // Take a fresh set of current readings (uni-processor mode).

        // ========== Linux-specific (begin) ==========
#ifdef Q_OS_LINUX
        fscanf(fd, "%32s %d %d %d %d", tag, &cUT, &cNT, &cST, &cIT);
#endif
        // ========== Linux-specific (end) ==========

        // ========== BSD-specific (begin) ==========
#ifdef Q_OS_BSD4
        if (! initialized) {
            if (sysctl(name2oid, 2, oidCpuTime, &oidCpuTimeLen, name,
                    strlen(name)) < 0)
                return;

            oidCpuTimeLen /= sizeof(int);
            initialized = 1;
        }

        if (sysctl(oidCpuTime, oidCpuTimeLen, cpuTime, &cpuTimeLen, 0, 0) < 0)
            return;

        cUT = cpuTime[CP_USER];
        cNT = cpuTime[CP_NICE];
        cST = cpuTime[CP_SYS];
        cIT = cpuTime[CP_IDLE];
#endif
        // ========== BSD-specific (end) ==========

        // Calculate final totals.
        tot = (cUT - UT) + (cST - ST) + (cNT - NT) + (cIT - IT);
    }

    // Clean up after taking readings.

    // ========== Linux-specific (begin) ==========
#ifdef Q_OS_LINUX
    fclose(fd);
#endif
    // ========== Linux-specific (end) ==========
}


Generated by  Doxygen 1.6.0   Back to index