summaryrefslogtreecommitdiff
path: root/sysinfo.c
blob: 8de6e340ab56c5b23ca1565dbe273a24c37060a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * sysinfo.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * Date: 03.06.07
 *
 */

#include <stdio.h>
#include <unistd.h>

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

#ifndef WITHOUT_VDR
# include <vdr/tools.h>
#endif

#include <sysinfo.h>

int Sysinfo::initialized = no;
double Sysinfo::lastIdle = 0;
double Sysinfo::lastTotal = 0;

//***************************************************************************
// Init
//***************************************************************************

int Sysinfo::init()
{
   if (initialized)
      return done;

#if LIBGTOP_MAJOR_VERSION > 2 || (LIBGTOP_MAJOR_VERSION == 2 && LIBGTOP_MINOR_VERSION >= 14)
   tell(0, "init libgtop (%d.%d)", 
     LIBGTOP_MAJOR_VERSION, LIBGTOP_MINOR_VERSION);
   glibtop_init();
#endif

   initialized = yes;

   return done;
}

//***************************************************************************
// Init
//***************************************************************************

int Sysinfo::exit()
{
   if (!initialized)
      return done;

#if LIBGTOP_MAJOR_VERSION > 2 || (LIBGTOP_MAJOR_VERSION == 2 && LIBGTOP_MINOR_VERSION >= 14)
      glibtop_close();
#endif

   initialized = no;

   return done;

}

//***************************************************************************
// Get CPU Load
//***************************************************************************

int Sysinfo::cpuLoad()
{	
   static uint64_t lastCall = 0;
   static int lastLoad = na;

   if (!initialized)
      init();

   int load;
	glibtop_cpu cpu;

#ifndef WITHOUT_VDR
   if (lastCall && lastLoad != na && cTimeMs::Now() - lastCall < 200)
      return lastLoad;
#endif

	glibtop_get_cpu(&cpu);

   double total = ((unsigned long)cpu.total) ? ((double)cpu.total) : 1.0;
   double idle = ((unsigned long)cpu.idle)  ? ((double)cpu.idle)  : 1.0;

	total /= (double)cpu.frequency;
	idle  /= (double)cpu.frequency;
	
   // calc tic deltas (from last)

   double loadTics = total- idle - lastTotal;
   double idleTics = idle - lastIdle;

   // calc load

	load = (int) (loadTics * 100 / (loadTics + idleTics));

   // remember

	lastTotal = total - idle;
	lastIdle = idle;
   lastLoad = load;

#ifndef WITHOUT_VDR
   lastCall = cTimeMs::Now();
#endif

	return load;
}

//***************************************************************************
// Get Memory Info
//***************************************************************************

int Sysinfo::memInfoMb(unsigned long& total, 
                       unsigned long& used, 
                       unsigned long& free,
                       unsigned long& cached)
{
   glibtop_mem memory;

   if (!initialized)
      init();

   glibtop_get_mem(&memory);

   total = (unsigned long)memory.total/(1024*1024);
   used = (unsigned long)memory.used/(1024*1024);
   free = (unsigned long)memory.free/(1024*1024);
   cached = (unsigned long)memory.cached/(1024*1024);

   /*
   tell(4, "\nMEMORY USING\n\n"
     "Memory Total : %ld MB\n"
     "Memory Used : %ld MB\n"
     "Memory Free : %ld MB\n"
     "Memory Buffered : %ld MB\n"
     "Memory Cached : %ld MB\n"
     "Memory user : %ld MB\n"
     "Memory Locked : %ld MB\n",
     total, used, free,
     (unsigned long)memory.shared/(1024*1024),
     (unsigned long)memory.buffer/(1024*1024),
     (unsigned long)memory.cached/(1024*1024),
     (unsigned long)memory.user/(1024*1024),
     (unsigned long)memory.locked/(1024*1024));
   */
	
   return done;
} 

//***************************************************************************