Gtk简易CPU Monitor

简介

一个基于 Gtk+-2.0 的简易 CPU usage monitor,入门学习 Gtk 时用来练手。

本 monitor 主要采用 Gtk2 的 label 控件和 g_timeout 定时回调,详见 gtk-doc。其中 g_timeout 就如同 JS 的 setTimeout,为非阻塞异步定时回调。本程序只需稍加改进即可在 Gtk3 中运行。

其中这里使用到了额外的 libgtop 库和一个来自 Github 的用于计算当前 CPU 使用率的小库(通过 /proc/stat )。

程序

/**
 * cpu_usage.c
 *
 * @author YanWen <i@yanwen.email>
 * @modified 2018-03-12
 */

#include <stdio.h>
#include <stdlib.h>

#include <gtk/gtk.h>
#include <glibtop/cpu.h>

#include "cpu-usage/cpu_usage.h"

void _init_cpu_monitor();
gboolean _callback_cpu_monitor(gpointer label);

// Global vars
struct global_vars_t {
  // Data
  struct cpu_counters current_cpu_cnt_start;
  struct cpu_counters current_cpu_cnt_end;

  glibtop_cpu averange_cpu_cnt;

  // Methods
  void (* init_cpu_monitor)(void);
  gboolean (* callback_cpu_monitor)(gpointer);
} global_vars;

void _init_cpu_monitor() {
  read_cpu_counters(&global_vars.current_cpu_cnt_start);
}

/**
* CPU usage callback for g_timeout
*/
gboolean _callback_cpu_monitor(gpointer label) {
  char cpu_usage_info[20];

  // Current usage(in 1 second)
  read_cpu_counters(&global_vars.current_cpu_cnt_end);
  float usage_current = cpu_usage(&global_vars.current_cpu_cnt_start,
                                  &global_vars.current_cpu_cnt_end);

  // Average usage since boot
  glibtop_get_cpu(&global_vars.averange_cpu_cnt);
  float usage_averange = 100 - (float)global_vars.averange_cpu_cnt.idle /
                               (float)global_vars.averange_cpu_cnt.total * 100;

  sprintf(cpu_usage_info, "Current: %3.2f%%; Averange: %3.2f%%.", usage_current, usage_averange);
  gtk_label_set_text(GTK_LABEL(label), cpu_usage_info);

  // Relay
  global_vars.current_cpu_cnt_start = global_vars.current_cpu_cnt_end;

  return TRUE;
}


// GTK

/**
 * Signal, Return false and destroy
 */
gint delete_event(GtkWidget *widget, GdkEvent *event, gpointer data) {
  gtk_main_quit();
}

/**
 * Signal, destroy windows
 */
void destroy(GtkWidget *widget, gpointer data) {
  gtk_main_quit();
}

void init_window(int argc, char *argv[]) {
  GtkWidget *window;
  GtkWidget *box;
  GtkWidget *label;

  gtk_init(&argc, &argv);

  // Create a new window
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "Cpu Monitor(YanWen.Blog)");

  // Bind the shutdown signal
  gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(delete_event), NULL);
  gtk_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(destroy), NULL);

  // Main UI
  box = gtk_hbox_new(FALSE, 12);
  gtk_container_set_border_width(GTK_CONTAINER(box), 12);

  label = gtk_label_new("NULL");

  // Refresh CPU usage every 1 second
  global_vars.init_cpu_monitor();
  g_timeout_add_seconds(1, global_vars.callback_cpu_monitor, label);

  gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 24);
  gtk_container_add(GTK_CONTAINER(window), box);

  // Show
  gtk_widget_show_all(window);

  gtk_main();
}

// Main
int main(int argc, char *argv[]) {
  global_vars.init_cpu_monitor = _init_cpu_monitor;
  global_vars.callback_cpu_monitor = _callback_cpu_monitor;

  init_window(argc, argv);

  return 0;
}

编译:

gcc -o cpu_usage cpu_usage.c 'cpu-usage/cpu_usage.c' 
  `pkg-config --cflags --libs gtk+-2.0` 
  `pkg-config --cflags --libs libgtop-2.0`

运行:

参考:

  1. www.gtk.org/gtk-doc

作者: YanWen

Web 开发者

发表评论

Fill in your details below or click an icon to log in:

WordPress.com 徽标

You are commenting using your WordPress.com account. Log Out /  更改 )

Google photo

You are commenting using your Google account. Log Out /  更改 )

Twitter picture

You are commenting using your Twitter account. Log Out /  更改 )

Facebook photo

You are commenting using your Facebook account. Log Out /  更改 )

Connecting to %s