⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timings.c

📁 linux 操作系统下对系统资源监控程序的编写
💻 C
字号:
/* $Id: timings.c,v 1.10 2003/10/20 15:19:36 hadess Exp $ *//* Copyright (C) 1998-99 Martin Baulig   This file is part of LibGTop 1.0.   Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.   LibGTop 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.   LibGTop 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 LibGTop; see the file COPYING. If not, write to the   Free Software Foundation, Inc., 59 Temple Place - Suite 330,   Boston, MA 02111-1307, USA.*/#include <locale.h>#include <glibtop.h>#include <glibtop/open.h>#include <glibtop/close.h>#include <glibtop/parameter.h>#include <glibtop/union.h>#include <glibtop/sysdeps.h>#include <sys/times.h>#include <sys/resource.h>#ifndef PROFILE_COUNT#define PROFILE_COUNT			100000L#endif#ifndef PROFILE_COUNT_EXPENSIVE#define PROFILE_COUNT_EXPENSIVE		10000L#endif#define ELAPSED_UTIME ((unsigned long) elapsed_utime.tv_sec * 1000000 +	(unsigned long) elapsed_utime.tv_usec)#define ELAPSED_STIME ((unsigned long) elapsed_stime.tv_sec * 1000000 + (unsigned long) elapsed_stime.tv_usec)#define libgtop_timeradd(tvp, uvp, vvp)						\	do {								\		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\		if ((vvp)->tv_usec >= 1000000) {			\			(vvp)->tv_sec++;				\			(vvp)->tv_usec -= 1000000;			\		}							\	} while (0)#define libgtop_timersub(tvp, uvp, vvp)						\	do {								\		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\		if ((vvp)->tv_usec < 0) {				\			(vvp)->tv_sec--;				\			(vvp)->tv_usec += 1000000;			\		}							\	} while (0)intmain (int argc, char *argv []){	glibtop_union data;	unsigned c, count, *ptr;	struct rusage total_start, total_end;	struct rusage rusage_start, rusage_end;	struct timeval elapsed_utime, elapsed_stime;	pid_t pid;	count = PROFILE_COUNT;	setlocale (LC_ALL, "");	bindtextdomain (GETTEXT_PACKAGE, GTOPLOCALEDIR);	textdomain (GETTEXT_PACKAGE);	printf ("%-12s (%-10s): %7s - %9s - %9s\n",		"Feature", "Flags", "Count", "utime", "stime");	printf ("-------------------------------------------"		"---------------\n");		glibtop_init_r (&glibtop_global_server, 0, 0);	getrusage (RUSAGE_SELF, &total_start);	getrusage (RUSAGE_SELF, &rusage_start);	for (c = 0; c < PROFILE_COUNT; c++)		glibtop_get_cpu (&data.cpu);	getrusage (RUSAGE_SELF, &rusage_end);	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,			  &elapsed_utime);	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,			  &elapsed_stime);	printf ("CPU          (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",		(unsigned long) data.cpu.flags, PROFILE_COUNT,		(long double) ELAPSED_UTIME / PROFILE_COUNT,		(long double) ELAPSED_STIME / PROFILE_COUNT);	getrusage (RUSAGE_SELF, &rusage_start);	for (c = 0; c < PROFILE_COUNT_EXPENSIVE; c++)		glibtop_get_mem (&data.mem);	getrusage (RUSAGE_SELF, &rusage_end);	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,			  &elapsed_utime);	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,			  &elapsed_stime);	printf ("Memory       (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",		(unsigned long) data.mem.flags, PROFILE_COUNT_EXPENSIVE,		(long double) ELAPSED_UTIME / PROFILE_COUNT_EXPENSIVE,		(long double) ELAPSED_STIME / PROFILE_COUNT_EXPENSIVE);	getrusage (RUSAGE_SELF, &rusage_start);	for (c = 0; c < PROFILE_COUNT_EXPENSIVE; c++)		glibtop_get_swap (&data.swap);	getrusage (RUSAGE_SELF, &rusage_end);	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,			  &elapsed_utime);	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,			  &elapsed_stime);	printf ("Swap         (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",		(unsigned long) data.swap.flags, PROFILE_COUNT_EXPENSIVE,		(long double) ELAPSED_UTIME / PROFILE_COUNT_EXPENSIVE,		(long double) ELAPSED_STIME / PROFILE_COUNT_EXPENSIVE);	getrusage (RUSAGE_SELF, &rusage_start);	for (c = 0; c < PROFILE_COUNT; c++)		glibtop_get_uptime (&data.uptime);	getrusage (RUSAGE_SELF, &rusage_end);	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,			  &elapsed_utime);	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,			  &elapsed_stime);	printf ("Uptime       (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",		(unsigned long) data.uptime.flags, PROFILE_COUNT,		(long double) ELAPSED_UTIME / PROFILE_COUNT,		(long double) ELAPSED_STIME / PROFILE_COUNT);	getrusage (RUSAGE_SELF, &rusage_start);	for (c = 0; c < PROFILE_COUNT; c++)		glibtop_get_loadavg (&data.loadavg);	getrusage (RUSAGE_SELF, &rusage_end);	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,			  &elapsed_utime);	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,			  &elapsed_stime);	printf ("Loadavg      (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",		(unsigned long) data.loadavg.flags, PROFILE_COUNT,		(long double) ELAPSED_UTIME / PROFILE_COUNT,		(long double) ELAPSED_STIME / PROFILE_COUNT);	printf ("\n");	getrusage (RUSAGE_SELF, &rusage_start);	for (c = 0; c < PROFILE_COUNT_EXPENSIVE; c++) {		ptr = glibtop_get_proclist (&data.proclist, 0, 0);		g_free (ptr);	}	getrusage (RUSAGE_SELF, &rusage_end);	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,			  &elapsed_utime);	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,			  &elapsed_stime);	printf ("Proclist     (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",		(unsigned long) data.proclist.flags,		PROFILE_COUNT_EXPENSIVE,		(long double) ELAPSED_UTIME / PROFILE_COUNT_EXPENSIVE,		(long double) ELAPSED_STIME / PROFILE_COUNT_EXPENSIVE);	pid = getpid ();	printf ("\n");	getrusage (RUSAGE_SELF, &rusage_start);	for (c = 0; c < PROFILE_COUNT; c++)		glibtop_get_proc_state (&data.proc_state, pid);	getrusage (RUSAGE_SELF, &rusage_end);	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,			  &elapsed_utime);	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,			  &elapsed_stime);	printf ("Proc_State   (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",		(unsigned long) data.proc_state.flags, PROFILE_COUNT,		(long double) ELAPSED_UTIME / PROFILE_COUNT,		(long double) ELAPSED_STIME / PROFILE_COUNT);	getrusage (RUSAGE_SELF, &rusage_start);	for (c = 0; c < PROFILE_COUNT; c++)		glibtop_get_proc_uid (&data.proc_uid, pid);	getrusage (RUSAGE_SELF, &rusage_end);	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,			  &elapsed_utime);	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,			  &elapsed_stime);	printf ("Proc_Uid     (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",		(unsigned long) data.proc_uid.flags, PROFILE_COUNT,		(long double) ELAPSED_UTIME / PROFILE_COUNT,		(long double) ELAPSED_STIME / PROFILE_COUNT);	getrusage (RUSAGE_SELF, &rusage_start);	for (c = 0; c < PROFILE_COUNT; c++)		glibtop_get_proc_mem (&data.proc_mem, pid);	getrusage (RUSAGE_SELF, &rusage_end);	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,			  &elapsed_utime);	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,			  &elapsed_stime);	printf ("Proc_Mem     (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",		(unsigned long) data.proc_mem.flags, PROFILE_COUNT,		(long double) ELAPSED_UTIME / PROFILE_COUNT,		(long double) ELAPSED_STIME / PROFILE_COUNT);	getrusage (RUSAGE_SELF, &rusage_start);	for (c = 0; c < PROFILE_COUNT; c++)		glibtop_get_proc_segment (&data.proc_segment, pid);	getrusage (RUSAGE_SELF, &rusage_end);	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,			  &elapsed_utime);	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,			  &elapsed_stime);	printf ("Proc_Segment (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",		(unsigned long) data.proc_segment.flags, PROFILE_COUNT,		(long double) ELAPSED_UTIME / PROFILE_COUNT,		(long double) ELAPSED_STIME / PROFILE_COUNT);	getrusage (RUSAGE_SELF, &rusage_start);	for (c = 0; c < PROFILE_COUNT; c++)		glibtop_get_proc_time (&data.proc_time, pid);	getrusage (RUSAGE_SELF, &rusage_end);	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,			  &elapsed_utime);	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,			  &elapsed_stime);	printf ("Proc_Time    (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",		(unsigned long) data.proc_time.flags, PROFILE_COUNT,		(long double) ELAPSED_UTIME / PROFILE_COUNT,		(long double) ELAPSED_STIME / PROFILE_COUNT);	getrusage (RUSAGE_SELF, &rusage_start);	for (c = 0; c < PROFILE_COUNT; c++)		glibtop_get_proc_signal (&data.proc_signal, pid);		getrusage (RUSAGE_SELF, &rusage_end);	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,			  &elapsed_utime);	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,			  &elapsed_stime);	printf ("Proc_Signal  (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",		(unsigned long) data.proc_signal.flags, PROFILE_COUNT,		(long double) ELAPSED_UTIME / PROFILE_COUNT,		(long double) ELAPSED_STIME / PROFILE_COUNT);	getrusage (RUSAGE_SELF, &rusage_start);	for (c = 0; c < PROFILE_COUNT; c++)		glibtop_get_proc_kernel (&data.proc_kernel, pid);	getrusage (RUSAGE_SELF, &rusage_end);	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,			  &elapsed_utime);	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,			  &elapsed_stime);	printf ("Proc_Kernel  (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",		(unsigned long) data.proc_kernel.flags, PROFILE_COUNT,		(long double) ELAPSED_UTIME / PROFILE_COUNT,		(long double) ELAPSED_STIME / PROFILE_COUNT);		getrusage (RUSAGE_SELF, &total_end);	libgtop_timersub (&total_end.ru_utime, &total_start.ru_utime,			  &elapsed_utime);	libgtop_timersub (&total_end.ru_stime, &total_start.ru_stime,			  &elapsed_stime);	printf ("-------------------------------------------"		"---------------\n");	printf ("%-36s %9lu - %9lu\n\n", "TOTAL",		ELAPSED_UTIME, ELAPSED_STIME);	printf ("All timings are in clock ticks "		"(1000000 ticks per second).\n\n");	glibtop_close ();	exit (0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -