Re: [Lenny] Keine CPU-Auslastung der einzelnen Prozessen bei top
Am Sonntag, den 29.03.2009, 13:15 +0200 schrieb Michael Grosseck:
> Hallo nochmal,
>
> hat niemand eine Idee, woran das liegen kann?
> Woher nimmt top und ps die Informationen zu CPU-Auslastung? Ich schätze
> aus den Untiefen des /proc - Verzeichnisses. Aber woher genau?
> Hier noch mal die CPU Info und die Kernelbezeichnung.
>
> processor : 0
> vendor_id : GenuineIntel
> cpu family : 6
> model : 7
> model name : Pentium III (Katmai)
> stepping : 3
> cpu MHz : 501.151
> cache size : 512 KB
>
> Linux itchy 2.6.26-1-686 #1 SMP Fri Mar 13 18:08:45 UTC 2009 i686 GNU/Linux
>
> Den Kernel habe ich nicht neu übersetzt, der ist so wie er von Debian kam.
Die Informationen kommen aus /proc/stat
Hab vor Jahren mal ein wenig mit rumgespielt, um mir ein kleines
Programm zu schreiben (nicht nach Beweggründen fragen ;)). Hier mein
Quelltext dazu (ich weiß, miserabel kommentiert):
/*
* cpu-usage.c
*
* Copyright 2006 Florian Sievers <dopehouse@users.sourceforge.net>
* Based on perl code from Ryan Veety
* http://www.ryanspc.com/index.pl?page=source
*
* 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; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <unistd.h>
#define BUFFER_LENGTH 100
double cpu_usage(void)
{
char buffer[BUFFER_LENGTH];
FILE *statfile;
double cpu_usage, cpu_load, cpu_total;
double cpu_load_new;
static double cpu_load_old;
double cpu_total_new;
static double cpu_total_old;
double cpu_user, cpu_nice, cpu_sys, cpu_idle;
/* Open /proc/stat to read cpu usage */
statfile = fopen("/proc/stat", "r");
if(statfile == NULL)
{
printf("Error open file /proc/stat\n");
return 1;
}
/* read untill buffer is full (default 100) */
fread(buffer, sizeof(char), BUFFER_LENGTH - 1, statfile);
/* cut string into tokens */
strtok(buffer, " ");
cpu_user = atof(strtok(NULL, " "));
cpu_nice = atof(strtok(NULL, " "));
cpu_sys = atof(strtok(NULL, " "));
cpu_idle = atof(strtok(NULL, " "));
fclose(statfile);
cpu_total_new = cpu_user + cpu_nice + cpu_sys + cpu_idle;
cpu_load_new = cpu_user + cpu_nice + cpu_sys;
cpu_load = cpu_load_old - cpu_load_new;
cpu_total = cpu_total_old - cpu_total_new;
if(cpu_load == 0)
{
cpu_usage = 100.0 * cpu_load;
}
else
{
cpu_usage = 100.0 * cpu_load / cpu_total;
}
cpu_load_old = cpu_load_new;
cpu_total_old = cpu_total_new;
return cpu_usage;
}
// ENDE
Gruß
Flo
--
Florian Sievers <florian@dynamic-core.ath.cx>
Reply to: