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

📄 cpu.c

📁 系统任务管理器
💻 C
📖 第 1 页 / 共 3 页
字号:
/* GKrellM|  Copyright (C) 1999-2006 Bill Wilson||  Author:  Bill Wilson    billw@gkrellm.net|  Latest versions might be found at:  http://gkrellm.net||  This program is free software which I release under the GNU General Public|  License. You may redistribute and/or modify this program under the terms|  of that license as published by the Free Software Foundation; either|  version 2 of the License, or (at your option) any later version.||  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.  Version 2 is in the|  COPYRIGHT file in the top level directory of this distribution.| |  To get a copy of the GNU General Puplic License, write to the Free Software|  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*/#include "gkrellm.h"#include "gkrellm-private.h"#include "gkrellm-sysdeps.h"static void	cb_alert_config(GkrellmAlert *ap, gpointer data);#if !defined(WIN32)static void	cb_alert_config_create(GkrellmAlert *ap, GtkWidget *vbox,					gpointer data);#endif#ifdef CLK_TCK#define	CPU_TICKS_PER_SECOND	CLK_TCK#else#define	CPU_TICKS_PER_SECOND	100		/* XXX */#endif  /* Values for smp_mode - must be same order as buttons in config */#define	SMP_REAL_MODE				0#define	SMP_COMPOSITE_MODE			1#define	SMP_COMPOSITE_AND_REAL_MODE	2typedef struct	{	gchar			*name;	gchar			*panel_label;	GtkWidget		*vbox;	GkrellmChart	*chart;	GkrellmChartconfig	*chart_config;	GkrellmChartdata	*sys_cd,					*user_cd,					*nice_cd;	gint			enabled;	gint			extra_info;	gint			is_composite;	gulong			previous_total;	gint			instance;	gpointer		sensor_temp,					sensor_fan;	GkrellmDecal	*sensor_decal,		/* temperature and possibly fan */					*fan_decal;			/* fan if separate draw mode	*/	gboolean		show_temperature,					show_fan,					new_text_format;	gint			save_label_position;  /* When toggling sensors on/off */	GkrellmAlert	*alert;	gulong			previous_alert_value,					previous_alert_total;	GkrellmLauncher	launch;	GtkWidget		*launch_entry,					*tooltip_entry;	gulong			user,					nice,					sys,					idle;	}	CpuMon;static GkrellmMonitor				*mon_cpu;GList			*cpu_mon_list,				*instance_list;void			(*read_cpu_data)();static CpuMon	*composite_cpu;static GkrellmAlert	*cpu_alert;		/* One alert dupped for each CPU */static gboolean	alert_includes_nice;static gint		smp_mode;static gboolean	cpu_enabled  = TRUE;static gboolean	omit_nice_mode,				config_tracking,				sys_handles_composite_data,				nice_time_unsupported;static gint		style_id;static gint		sensor_separate_mode;static gchar    *text_format,				*text_format_locale;static gint		n_cpus;static gint		n_smp_cpus;static gbooleansetup_cpu_interface(void)	{	if (!read_cpu_data && !_GK.client_mode && gkrellm_sys_cpu_init())		{		read_cpu_data = gkrellm_sys_cpu_read_data;		}	return read_cpu_data ? TRUE : FALSE;	}voidgkrellm_cpu_client_divert(void (*read_func)())	{	read_cpu_data = read_func;	}  /* Must be called from gkrellm_sys_cpu_init()  */voidgkrellm_cpu_set_number_of_cpus(gint n)	{	CpuMon	*cpu;	GList	*list;	gint	i;	n_cpus = n;	if (instance_list && g_list_length(instance_list) != n)		{		instance_list = NULL;		fprintf(stderr, "Bad sysdep cpu instance list length.\n");		}	if (!instance_list)		for (i = 0; i < n; ++i)			instance_list = g_list_append(instance_list, GINT_TO_POINTER(i));	if (n > 1)		{		++n_cpus;	/* Include the composite cpu */		n_smp_cpus = n;		}	for (i = 0; i < n_cpus; ++i)		{		cpu = g_new0(CpuMon, 1);		if (i == 0)			{			cpu->name = g_strdup("cpu");			if (n_smp_cpus > 0)				{				cpu->is_composite = TRUE;				cpu->instance = -1;				composite_cpu = cpu;				}			else				cpu->instance = GPOINTER_TO_INT(instance_list->data);			}		else			{			list = g_list_nth(instance_list, i - 1);			cpu->instance = GPOINTER_TO_INT(list->data);			cpu->name = g_strdup_printf("cpu%d", cpu->instance);			}		cpu->panel_label = g_strdup_printf(_("CPU%s"), &(cpu->name)[3]);		cpu_mon_list = g_list_append(cpu_mon_list, cpu);		}	}  /* If cpu numbering is not sequential starting from zero, then sysdep code  |  should call this function for each cpu to make a list of cpu numbers.  */voidgkrellm_cpu_add_instance(gint instance)	{	instance_list = g_list_append(instance_list, GINT_TO_POINTER(instance));	}voidgkrellm_cpu_nice_time_unsupported(void)	{	nice_time_unsupported = TRUE;	}voidgkrellm_cpu_assign_composite_data(gulong user, gulong nice,			gulong sys, gulong idle)	{	if (!composite_cpu)		return;	sys_handles_composite_data = TRUE;	composite_cpu->user = user;	composite_cpu->nice = nice;	composite_cpu->sys = sys;	composite_cpu->idle = idle;	}voidgkrellm_cpu_assign_data(gint n, gulong user, gulong nice,			gulong sys, gulong idle)	{	CpuMon	*cpu = NULL;	GList	*list;	for (list = cpu_mon_list; list; list = list->next)		{		cpu = (CpuMon *) list->data;		if (cpu->instance == n)			break;		}	if (list)		{		cpu->user = user;		cpu->nice = nice;		cpu->sys = sys;		cpu->idle = idle;		if (composite_cpu && !sys_handles_composite_data)			{			composite_cpu->user += user;			composite_cpu->nice += nice;			composite_cpu->sys += sys;			composite_cpu->idle += idle;			}		}	}/* ======================================================================== *//* Exporting CPU data for plugins */gintgkrellm_smp_cpus(void)	{	return n_smp_cpus;	}gbooleangkrellm_cpu_stats(gint n, gulong *user, gulong *nice,			gulong *sys, gulong *idle)	{	GList	*list;	CpuMon	*cpu;	list = g_list_nth(cpu_mon_list, n);	if (!list)		return FALSE;	cpu = (CpuMon *) list->data;	if (user)		*user = cpu->user;	if (nice)		*nice = cpu->nice;	if (sys)		*sys = cpu->sys;	if (idle)		*idle = cpu->idle;	return TRUE;	}/* ======================================================================== */static voidformat_cpu_data(CpuMon *cpu, gchar *src_string, gchar *buf, gint size)	{	GkrellmChart	*cp;	gchar			c, *s;	gint			len, sys, user, nice = 0, total, t;	if (!buf || size < 1)		return;	--size;	*buf = '\0';	if (!src_string)		return;	cp = cpu->chart;	sys = gkrellm_get_current_chartdata(cpu->sys_cd);	user = gkrellm_get_current_chartdata(cpu->user_cd);	total = sys + user;	if (!nice_time_unsupported)		{		nice = gkrellm_get_current_chartdata(cpu->nice_cd);		if (!omit_nice_mode && !gkrellm_get_chartdata_hide(cpu->nice_cd))			total += nice;		}	for (s = src_string; *s != '\0' && size > 0; ++s)		{		len = 1;		if (*s == '$' && *(s + 1) != '\0')			{			t = -1;			if ((c = *(s + 1)) == 'T')				t = total;			else if (c == 's')				t = sys;			else if (c == 'u')				t = user;			else if (c == 'n')				t = nice;			else if (c == 'L')				len = snprintf(buf, size, "%s", cp->panel->label->string);			else if (c == 'H')				len = snprintf(buf, size, "%s", gkrellm_sys_get_host_name());			else				{				*buf = *s;				if (size > 1)					{					*(buf + 1) = *(s + 1);					++len;					}				}			if (t >= 0)				{				/* ChartData values have been scaled to the chart max scale				|  of CPU_TICKS_PER_SECOND.				*/				t = ((200 * t / CPU_TICKS_PER_SECOND) + 1) / 2;				if (t > 100)					t = 100;				len = snprintf(buf, size, "%d%%", t);				}			++s;			}		else			*buf = *s;		size -= len;		buf += len;		}	*buf = '\0';		}static voiddraw_cpu_extra(CpuMon *cpu)	{	GkrellmChart	*cp = cpu->chart;	gchar			buf[128];	if (!cp)		return;	format_cpu_data(cpu, text_format_locale, buf, sizeof(buf));	if (!cpu->new_text_format)		gkrellm_chart_reuse_text_format(cp);	cpu->new_text_format = FALSE;	gkrellm_draw_chart_text(cp, style_id, buf);	}static voidcb_command_process(GkrellmAlert *alert, gchar *src, gchar *dst, gint len,				CpuMon *cpu)    {    format_cpu_data(cpu, src, dst, len);	}static voidrefresh_cpu_chart(CpuMon *cpu)	{	GkrellmChart	*cp	= cpu->chart;	gkrellm_draw_chartdata(cp);	if (cpu->extra_info)		draw_cpu_extra(cpu);	gkrellm_draw_chart_to_screen(cp);	}static voiddraw_sensor_decals(CpuMon *cpu)	{	GkrellmPanel	*p = cpu->chart->panel;	gchar			units;	gfloat			t, f;	gint			toggle;	if (sensor_separate_mode && cpu->show_temperature && cpu->show_fan)		{		gkrellm_sensor_read_temperature(cpu->sensor_temp, &t, &units);		gkrellm_sensor_draw_temperature_decal(p, cpu->sensor_decal, t, units);		gkrellm_sensor_read_fan(cpu->sensor_fan, &f);		gkrellm_sensor_draw_fan_decal(p, cpu->fan_decal, f);		}	else		{		toggle = _GK.time_now & 2;		if (cpu->show_fan && (toggle || !cpu->show_temperature) )			{			gkrellm_sensor_read_fan(cpu->sensor_fan, &f);			gkrellm_sensor_draw_fan_decal(p, cpu->sensor_decal, f);			}		else if (cpu->show_temperature && (!toggle || !cpu->show_fan) )			{			gkrellm_sensor_read_temperature(cpu->sensor_temp, &t, &units);			gkrellm_sensor_draw_temperature_decal(p, cpu->sensor_decal, t,							units);			}		}	}voidgkrellm_cpu_draw_sensors(gpointer sr)	{	GList	*list;	CpuMon	*cpu;	for (list = cpu_mon_list; list; list = list->next)		{		cpu = (CpuMon *) list->data;		if (sr && sr != cpu->sensor_temp && sr != cpu->sensor_fan)			continue;		if (cpu->enabled)			draw_sensor_decals(cpu);		}	}static voidupdate_cpu(void)	{	GList			*list;	CpuMon			*cpu;	GkrellmChart	*cp;	GkrellmPanel	*p;	GkrellmKrell	*krell;	gulong			total, krell_value, full_scale;	gulong			alert_value, alert_total_diff;	_GK.cpu_sys_activity = 0;	if (composite_cpu)		{		composite_cpu->user = 0;		composite_cpu->nice = 0;		composite_cpu->sys = 0;		composite_cpu->idle = 0;		}	(*read_cpu_data)();	for (list = cpu_mon_list; list; list = list->next)		{		cpu = (CpuMon *) list->data;		if (!cpu->enabled)			continue;		cp = cpu->chart;		p = cp->panel;		if (smp_mode == SMP_REAL_MODE)			_GK.cpu_sys_activity += (int)(cpu->sys - cpu->sys_cd->previous);		else if (list == cpu_mon_list)	/* Use composite cpu values */			_GK.cpu_sys_activity = (int)(cpu->sys - cpu->sys_cd->previous);		total = cpu->user + cpu->nice + cpu->sys + cpu->idle;		if (GK.second_tick)			{			gkrellm_store_chartdata(cp, total, cpu->sys, cpu->user, cpu->nice);			refresh_cpu_chart(cpu);			alert_value = cpu->sys + cpu->user;			if (alert_includes_nice)				alert_value += cpu->nice;			alert_total_diff = total - cpu->previous_alert_total;			if (   alert_total_diff > 0				&& (   ( cpu->is_composite && smp_mode == SMP_COMPOSITE_MODE)					|| (!cpu->is_composite && smp_mode != SMP_COMPOSITE_MODE)				   )			   )				gkrellm_check_alert(cpu->alert,					100.0 * (gfloat) (alert_value - cpu->previous_alert_value)					/ (gfloat) alert_total_diff);			cpu->previous_alert_value = alert_value;			cpu->previous_alert_total = total;			}		if (   (GK.two_second_tick && !sensor_separate_mode)			|| (GK.five_second_tick && sensor_separate_mode)		   )			draw_sensor_decals(cpu);		krell = KRELL(cp->panel);		full_scale = total - cpu->previous_total;		if (full_scale > 0)		/* Can be 0 for data from gkrellmd */			gkrellm_set_krell_full_scale(krell, (gint) full_scale, 10);		krell_value = cpu->sys + cpu->user;		if (cpu->previous_total > 0)			{			if (!omit_nice_mode && !gkrellm_get_chartdata_hide(cpu->nice_cd))				krell_value += cpu->nice;

⌨️ 快捷键说明

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