Mailinglist Archive: opensuse (2831 mails)

< Previous Next >
[SLE] Re: CPU load programm fuer console
  • From: "Ozgur Karatas" <ozgur@xxxxxxxxxxxxxxxx>
  • Date: Tue, 4 Jul 2006 21:57:36 +0300 (EEST)
  • Message-id: <16098.88.246.52.241.1152039456.squirrel@xxxxxxxxxxxxxxxxxxxx>
Hi,
i am coding cpustat.c in takes linux kernel of license GPL.
and adding to file, you can use gathering..

compile: gcc -O cpustat.c -o cpustat
./cpustat

$vi cpustat.c

/*
* /linux/fs/cpustat.c
*
* Copyright (C) 2006 Ozgur Karatas
*/

/*
* This code is written to take CPU statistics. cpustat; shows the usage
statistic of your CPU.
* The Code tested for Intel Processors. After then code will be ported
for AMD.
*/
#include <stdio.h>

int main (void)
{
FILE *fp;
char *str;
char buf[1000];
unsigned long long int c_user = 0;
unsigned long long int c_user_ = 0;
unsigned long long int c_nice = 0;
unsigned long long int c_nice_ = 0;
unsigned long long int c_sys = 0;
unsigned long long int c_sys_ = 0;
unsigned long long int c_idle = 0;
unsigned long long int c_idle_ = 0;
double u;
double n;
double s;
double i;
double t;

again:
fp = fopen("/proc/stat", "r");
while (!feof(fp)) {
fgets(buf, sizeof(buf), fp);
if (strncmp(buf, "cpu ", 4) == 0) {
break;
}
}
fclose(fp);

c_user_ = c_user;
c_nice_ = c_nice;
c_sys_ = c_sys;
c_idle_ = c_idle;

str = buf + 4;
c_user = strtoull(str, &str, 0);
c_nice = strtoull(str, &str, 0);
c_sys = strtoull(str, &str, 0);
c_idle = strtoull(str, &str, 0);

if (c_user == 0 &&
c_nice == 0 &&
c_sys == 0 &&
c_idle == 0) {
goto again;
}

u = (float) (c_user - c_user_);
n = (float) (c_nice - c_nice_);
s = (float) (c_sys - c_sys_);
i = (float) (c_idle - c_idle_);
t = u + n + s + i;
u = (u * 100) / t;
n = (n * 100) / t;
s = (s * 100) / t;
i = (i * 100) / t;

printf("total:%.1f user:%.1f nice:%.1f sys:%.1f idle:%.1f\n", t,
u, n, s,
i);

sleep(1);
goto again;

return 0;
}

Best Regards,

> Hallo
>
> ich suche ein Programm, dass mir ähnlich wie top die CPU-Last
> in einem bestimmten Intervall (crontab) in eine lesbare Logdatei und
evtl. daraus noch ein HTML-Formular mit graphischer Darstellung
> erzeugt.
> Hintergrund ist der, dass einer meiner Server immer mal eine
> hohe CPU-Last hat. Dies tritt jedoch nur sporradisch auf, so
> dass ich es meist nicht mitbekomme, die Kunden vor Ort jedoch
> massig Probleme beim mailen, squid oder smb-connect haben.
> Bisher habe ich da nur nette X-Anwendungen gefunden.
>
> Sicher gibts da sowas nettes wie top oder sowas ... ;-)
>
> --
> Regards,
>
> Jens Strohschnitter
>
> -------------------------------------
> *!!!LINUX LINUX LINUX LINUX LINUX!!!*
>
> * http://www.jens-strohschnitter.de *
> -------------------------------------
> Set the controls for
> the heart of the sun
> -------------------------------------

,''`. Ozgur Karatas
: :' : ozgur@xxxxxxxxxxxxxxxx
`. `' http://www.ozgurkaratas.com
`- Powered By Debian GNU\Linux


--
Check the headers for your unsubscription address
For additional commands send e-mail to suse-linux-e-help@xxxxxxxx
Also check the archives at http://lists.suse.com
Please read the FAQs: suse-linux-e-faq@xxxxxxxx


< Previous Next >
This Thread
  • No further messages