Subversion Repositories Kolibri OS

Rev

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