[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

code cpustat.c



Hello,
I have written a program which gives CPU statistics, then what should i do
for putting this program in to the debian? This code is written to
take CPU loading statistics. cpustat; shows the usage load statistic of
your CPU. I Code tested for Intel Processors. After then code will be
ported for AMD. Can we add this debian?


$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;
}

--

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



Reply to: