得到CPU利用率的算法(转载)
#include #include #include #include static int timer ; void cb_quit () { gtk_timeout_remove ( timer ); gtk_main_quit (); } float cal_load ( void ) { FILE * fp ; static long pre_user , pre_nice , pre_system , pre_idle ; //in order to extend
#include
#include
#include
#include
static int timer;
void cb_quit()
{
gtk_timeout_remove (timer);
gtk_main_quit();
}
float
cal_load (void)
{
FILE * fp;
static long pre_user, pre_nice, pre_system, pre_idle; //in order to extend easily later, i defined four vars separately
long user, nice, system, idle;
long d_user, d_nice, d_system, d_idle;
static long total;
float scale;
char buf[1024];
if ((fp = fopen ("/proc/stat", "r")) != NULL){
while (fgets (buf, sizeof(buf), fp)){
if( *buf == 'c' && *(buf+1) == 'p') break;
}
fclose (fp);
}
else
return 0.0;
sscanf(buf, "cpu %ld %ld %ld %ld", &user, &nice, &system, &idle);
// calculate delta value
d_user = user - pre_user;
d_nice = nice - pre_nice;
d_system = system - pre_system;
d_idle = idle - pre_idle;
total = d_user + d_nice + d_system + d_idle;
if (total < 1) total = 1;
scale = 1.0 / total;
pre_user = user;
pre_nice = nice;
pre_system = system;
pre_idle = idle;
return (d_user + d_nice + d_system) * scale;
}
gint Repaint (gpointer da)
{
GtkWidget * drawing_area = (GtkWidget *)da;
GdkDrawable * drawable = drawing_area->window;
GdkGC *gc_chart, *gc_text;
GdkColor color;
static unsigned coory[201];
int da_width, da_height;
int x;
float load;
char buf[30];
// set chart green
gc_chart = gdk_gc_new (drawing_area->window);
color.red = 0;
color.green = 0xffff;
color.blue = 0;
gdk_gc_set_rgb_fg_color(gc_chart, &color );
// set text red
gc_text = gdk_gc_new (drawing_area->window);
color.red = 0xffff;
color.green = 0;
color.blue = 0;
gdk_gc_set_rgb_fg_color(gc_text, &color );
// getting size of drawing area
da_width = drawing_area->allocation.width;
da_height = drawing_area->allocation.height;
gdk_draw_rectangle (drawable, drawing_area->style->white_gc,
TRUE,
0,0,
da_width,
da_height);
// chart cpu load
for (x=0; x<200; x++)
coory[x] = coory[x+1];
load = cal_load();
coory[x] = (int)(load * da_height);
for(x=0;x<da_width;x++){
gdk_draw_line (drawable, gc_chart, x, da_height, x, da_height - coory[x+1]);
}
// following code for drawing text
sprintf (buf, "Cpu load: %.1f%%", load * 100);
PangoLayout *layout = gtk_widget_create_pango_layout( da, buf );
PangoFontDescription *fontdesc = pango_font_description_from_string( "Luxi Mono 12" );
pango_layout_set_font_description( layout, fontdesc );
gdk_draw_layout( drawable,
gc_text,
5, 5, layout );
pango_font_description_free( fontdesc );
g_object_unref( layout );
g_object_unref( G_OBJECT(gc_chart) );
g_object_unref( G_OBJECT(gc_text) );
return TRUE;
}
int
main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *drawing_area;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW(window), "Cpu Load");
drawing_area = gtk_drawing_area_new ();
gtk_container_add (GTK_CONTAINER(window), drawing_area);
gtk_signal_connect(GTK_OBJECT (window), "destroy", GTK_SIGNAL_FUNC(cb_quit), NULL);
g_signal_connect( drawing_area, "expose_event",
G_CALLBACK(Repaint), NULL );
gtk_drawing_area_size (GTK_DRAWING_AREA(drawing_area), 200, 100);
gtk_widget_show (drawing_area);
gtk_widget_show(window);
timer = gtk_timeout_add (1000, Repaint, (gpointer) drawing_area);
gtk_main();
return 0;
}
原文转自:http://www.ltesting.net
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
|