Subversion Repositories Kolibri OS

Rev

Rev 7362 | Rev 7370 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7361 leency 1
/*
2
 * System Monitor
7369 leency 3
 * version 0.85
7361 leency 4
 * Author: Leency
5
*/
6
 
7
#define MEMSIZE 4096*10
8
 
9
#include "../lib/io.h"
10
#include "../lib/gui.h"
7369 leency 11
#include "../lib/fs.h"
7361 leency 12
 
13
#include "../lib/obj/libio.h"
14
#include "../lib/obj/libimg.h"
15
#include "../lib/obj/libini.h"
16
 
17
//===================================================//
18
//                                                   //
19
//                      SENSOR                       //
20
//                                                   //
21
//===================================================//
22
 
23
#define MIN_PB_BLOCK_W 19
24
#define LOAD_BG 0xFFFfff
7369 leency 25
#define LOAD_ACTIVE 0x3887EE
7361 leency 26
 
27
struct sensor {
28
	int x,y,w,h;
29
	void set_size();
30
	void draw_wrapper();
31
	void draw_progress();
32
};
33
 
34
void sensor::set_size(dword _x, _y, _w, _h)
35
{
36
	x=_x+2;
37
	y=_y;
38
	w=_w;
39
	h=_h;
7369 leency 40
	draw_wrapper();
7361 leency 41
}
42
 
43
void sensor::draw_wrapper()
44
{
45
	DrawRectangle(x-1, y-1, w+1, h+1, system.color.work_graph);
46
	DrawRectangle3D(x-2, y-2, w+3, h+3, system.color.work_dark, system.color.work_light);
47
}
48
 
49
void sensor::draw_progress(dword progress_w, active_value, bg_value, mesure)
50
{
51
	if (progress_w < MIN_PB_BLOCK_W) progress_w = MIN_PB_BLOCK_W;
52
	if (progress_w > w-MIN_PB_BLOCK_W) progress_w = w-MIN_PB_BLOCK_W;
53
 
54
	DrawBar(x, y, w-progress_w, h, LOAD_ACTIVE);
55
	sprintf(#param, "%i%s", active_value, mesure);
56
	WriteText(w-progress_w- calc(strlen(#param)*8) /2 + x, h/2-7+y, 0x90, LOAD_BG, #param);
57
 
58
	DrawBar(x+w-progress_w, y, progress_w, h, LOAD_BG);
59
	sprintf(#param, "%i%s", bg_value, mesure);
60
	WriteText(-progress_w - calc(strlen(#param)*8)/2 + w+x, h/2-7+y, 0x90, LOAD_ACTIVE, #param);
61
}
62
 
63
//===================================================//
64
//                                                   //
65
//                       DATA                        //
66
//                                                   //
67
//===================================================//
68
 
69
#define CPU_STACK 440
70
dword cpu_stack[CPU_STACK];
71
 
72
sensor cpu;
73
sensor ram;
74
sensor rd;
75
sensor tmp[10];
76
 
7369 leency 77
dword tmp_size[10];
78
 
7361 leency 79
//===================================================//
80
//                                                   //
81
//                       CODE                        //
82
//                                                   //
83
//===================================================//
84
 
85
void main()
86
{
87
	proc_info Form;
88
	dword cpu_frequency = GetCpuFrequency()/1000;
89
	int id;
90
 
91
	incn y;
92
 
93
	load_dll(libio, #libio_init,1);
94
	load_dll(libimg, #libimg_init,1);
95
	load_dll(libini, #lib_init,1);
7369 leency 96
 
97
	GetTmpDiskSizesFromIni();
7361 leency 98
 
99
	loop()
100
	{
101
		WaitEventTimeout(25);
102
		switch(EAX & 0xFF)
103
		{
104
			case evButton:
105
				if (GetButtonID()) ExitProcess();
106
				break;
107
 
108
			case evKey:
109
				GetKeys();
110
				if (key_scancode == SCAN_CODE_ESC) ExitProcess();
111
				break;
112
 
113
			case evReDraw:
114
				#define LEFT 25
115
				#define ICONGAP 45
116
				system.color.get();
117
				DefineAndDrawWindow(150, 100, CPU_STACK+LEFT+LEFT+4+9, 480 + skin_height + 4, 0x34, system.color.work, "System Monitor",0);
118
				GetProcessInfo(#Form, SelfInfo);
119
 
120
				y.n = 0;
121
				if (cpu_frequency < 1000) sprintf(#param, "CPU frequency: %i Hz", cpu_frequency);
122
				else sprintf(#param, "CPU frequency: %i MHz", cpu_frequency/1000);
7369 leency 123
				DrawBlockHeader(LEFT, y.inc(20), 37, "CPU load", #param);
124
				cpu.set_size(LEFT, y.inc(45), CPU_STACK, 100);
7361 leency 125
 
126
				sprintf(#param, "Total RAM: %i MiB", GetTotalRAM()/1024);
7369 leency 127
				DrawBlockHeader(LEFT, y.inc(cpu.h + 25), 36, "RAM usage", #param);
128
				ram.set_size(LEFT, y.inc(45), CPU_STACK, 23);
7361 leency 129
 
7369 leency 130
				DrawBlockHeader(LEFT, y.inc(ram.h + 25), 3, "System RAM Disk usage", "Fixed size: 1.44 MiB");
131
				rd.set_size(LEFT, y.inc(45), CPU_STACK, 23);
7361 leency 132
 
7369 leency 133
				sprintf(#param, "TMP Disk 0 size: %i MiB", tmp_size[0]);
134
				DrawBlockHeader(LEFT, y.inc(rd.h + 25), 50, "Virtual drive usage", #param);
135
				tmp[0].set_size(LEFT, y.inc(45), CPU_STACK, 23);
7361 leency 136
 
137
			default:
138
				MonitorCpu();
139
 
140
				ram.draw_progress(
141
					GetFreeRAM()*ram.w/GetTotalRAM(),
142
					GetTotalRAM()-GetFreeRAM()/1024,
143
					GetFreeRAM()/1024,
144
					"M"
145
					);
146
 
7369 leency 147
				dir_size.get("/rd/1");
148
 
149
				dir_size.bytes += dir_size.files/2 + 32 * 512; //file attr size + FAT table size
150
				dir_size.bytes /= 1024; //convert to KiB
151
				dir_size.bytes = 1440 - dir_size.bytes;
7361 leency 152
				rd.draw_progress(
7369 leency 153
					dir_size.bytes*rd.w/1440,
154
					1440 - dir_size.bytes,
155
					dir_size.bytes,
7361 leency 156
					"K"
157
					);
158
 
7369 leency 159
				if (tmp_size[0]) {
160
					dir_size.get("/tmp0/1");
161
					dir_size.bytes += dir_size.files/2 + 32 * 512; //file attr size + FAT table size
162
					dir_size.bytes /= 1024*1024; //convert to MiB
163
					dir_size.bytes= tmp_size[0] - dir_size.bytes;
164
					tmp[0].draw_progress(
165
						dir_size.bytes*tmp[0].w/tmp_size[0],
166
						tmp_size[0] - dir_size.bytes,
167
						dir_size.bytes,
168
						"M"
169
						);
170
				}
7361 leency 171
		}
172
	}
173
}
174
 
7369 leency 175
void DrawBlockHeader(dword _x, _y, _icon, _title, _subtitle)
176
{
177
	WriteTextB(_x+ICONGAP, _y, 0x90, system.color.work_text, _title);
178
	DrawIcon32(_x, _y, system.color.work, _icon);
179
	WriteText(_x+ICONGAP, _y+20, 0x90, system.color.work_text, _subtitle);
180
}
181
 
7361 leency 182
dword GetCpuLoad(dword max_h)
183
{
184
	dword idle;
185
	dword CPU_SEC = GetCpuFrequency() >> 20 + 1;
186
	dword IDLE_SEC = GetCpuIdleCount() >> 20 * max_h;
187
 
188
	EAX = IDLE_SEC;
189
	EBX = CPU_SEC;
190
	$cdq
191
	$div ebx
192
	idle = EAX;
193
 
194
	return max_h - idle;
195
}
196
 
7369 leency 197
_ini ini = { "/sys/settings/system.ini", "DiskSizes" };
198
void GetTmpDiskSizesFromIni()
199
{
200
	char i, key[2];
201
	key[1]=0;
202
	for (i=0; i<=9; i++)
203
	{
204
		key[0]=i+'0';
205
		tmp_size[i] = ini.GetInt(#key, 0) / 1024 / 1024;
206
	}
207
}
208
 
7361 leency 209
//===================================================//
210
//                                                   //
211
//                     MONITORS                      //
212
//                                                   //
213
//===================================================//
214
 
215
int pos=0;
216
void MonitorCpu()
217
{
218
	int i;
219
	if (!cpu.w) return;
220
 
221
	cpu_stack[pos] = GetCpuLoad(cpu.h);
222
	if (cpu_stack[pos]<=2) || (cpu_stack[pos]>cpu.h) cpu_stack[pos]=2;
223
 
224
	DrawBar(cpu.x+cpu.w-30, cpu.y-25, 30, 20, system.color.work);
225
	sprintf(#param, "%i%%", cpu_stack[pos]);
226
	WriteText(cpu.x+cpu.w-calc(strlen(#param)*8), cpu.y-25, 0x90, system.color.work_text, #param);
227
 
228
	for (i=0; i
229
		DrawBar(i+cpu.x, cpu.y, 1, cpu.h-cpu_stack[i], LOAD_BG);
230
		DrawBar(i+cpu.x, cpu.h-cpu_stack[i]+cpu.y, 1, cpu_stack[i], LOAD_ACTIVE);
231
 
232
		DrawBar(i+1+cpu.x, cpu.y, 1, cpu.h, LOAD_BG);
233
	}
234
 
235
	pos++;
236
	if (pos>=CPU_STACK) {
237
		pos = CPU_STACK-1;
238
		for (i=0; i
239
			cpu_stack[i] = cpu_stack[i+1];
240
		}
241
	}
242
}