root/mh-home-sysmon/trunk/mh-mem-source.c

Revision 93, 1.4 kB (checked in by inz, 6 months ago)

Initial code for mh-home-sysmon.

Line 
1 #include <stdio.h>
2 #include <glib.h>
3 #include "datasource.h"
4 #include "mh-mem-source.h"
5
6 typedef struct _MhMemSourceData MhMemSourceData;
7 struct _MhMemSourceData {
8         gchar *max;
9         gchar *half;
10         gchar *min;
11 };
12
13 static void *mh_mem_source_init(DataSource *data_source);
14 static double mh_mem_source_get_value(void *ds_data);
15 static void mh_mem_source_deinit(void *ds_data);
16
17 void mh_mem_source_load(DataSource *data_source)
18 {
19         data_source->init = mh_mem_source_init;
20         data_source->get_value = mh_mem_source_get_value;
21         data_source->deinit = mh_mem_source_deinit;
22
23         data_source->name = "MEM use";
24         data_source->min_text = "<b>0 MiB</b>";
25 }
26
27 static void *mh_mem_source_init(DataSource *data_source)
28 {
29         MhMemSourceData *data = g_new0(MhMemSourceData, 1);
30
31         FILE *meminfo = fopen("/proc/meminfo", "r");
32         guint total;
33
34         fscanf(meminfo,
35                         "MemTotal: %d kB", &total);
36         fclose(meminfo);
37
38         data->max = data_source->max_text = g_strdup_printf("<b>%d MiB</b>", total >> 10);
39         data->half = data_source->half_text = g_strdup_printf("<b>%d MiB</b>", total >> 11);
40
41         return data;
42 }
43
44 static double mh_mem_source_get_value(void *ds_data)
45 {
46         FILE *meminfo = fopen("/proc/meminfo", "r");
47         guint total, free, cached;
48
49         (void)ds_data;
50
51         fscanf(meminfo,
52                         "MemTotal: %d kB MemFree: %d kB "
53                         "Buffers: %*d kB Cached: %d kB",
54                         &total, &free, &cached);
55         fclose(meminfo);
56
57         return (double)(total - free - cached) / total;
58 }
59
60 static void mh_mem_source_deinit(void *ds_data)
61 {
62         g_free(ds_data);
63 }
Note: See TracBrowser for help on using the browser.