Subversion Repositories Kolibri OS

Rev

Rev 8946 | Rev 9301 | 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
8381 leency 3
 * version 1.36
7361 leency 4
 * Author: Leency
5
*/
6
 
7614 leency 7
#define MEMSIZE 4096*30
7361 leency 8
 
7614 leency 9
//===================================================//
10
//                                                   //
11
//                       LIB                         //
12
//                                                   //
13
//===================================================//
14
 
7361 leency 15
#include "../lib/gui.h"
7369 leency 16
#include "../lib/fs.h"
7614 leency 17
#include "../lib/list_box.h"
7361 leency 18
 
19
#include "../lib/obj/libimg.h"
7614 leency 20
#include "../lib/obj/box_lib.h"
7361 leency 21
 
7614 leency 22
#include "../lib/patterns/select_list.h"
23
#include "../lib/patterns/restart_process.h"
24
 
7361 leency 25
//===================================================//
26
//                                                   //
8277 leency 27
//                      CONST                        //
7361 leency 28
//                                                   //
29
//===================================================//
30
 
8277 leency 31
#define GAP 16   //Window padding
32
#define WIN_CONTENT_X GAP
33
#define WIN_CONTENT_Y GAP+15
34
#define PROCESS_LIST_W 260
35
#define RIGHT_X PROCESS_LIST_W + GAP + GAP + 22
7906 leency 36
#define ICONGAP 26
8277 leency 37
#define BOTPANEL_H 36
7361 leency 38
 
7906 leency 39
#ifdef LANG_RUS
8277 leency 40
	#define T_APP_TITLE      "Системный монитор"
41
	#define T_SHOW_SYSTEM    "Системные"
42
	#define T_DETAILS        "Подробнее"
43
	#define T_PROC_KILL      "Снять задачу"
44
	#define T_PROC_INFO      "Инфо"
45
	#define T_PROC_HEADER    "Процесс        ОЗУ Кб      ЦП %"
46
	#define T_CPU_LOAD       "Загрузка процессора %i%%   "
47
	#define T_RAM_USAGE      "Память ОЗУ: %i Мб свободно из %i Мб"
48
	#define T_RD_USAGE       "Системный диск: %i Кб свободно из 1.4 Мб"
49
	#define T_TMP_USAGE      "TMP%i диск: %i Мб свободно из %i Мб"
7906 leency 50
#else
8277 leency 51
	#define T_APP_TITLE      "System Monitor"
52
	#define T_SHOW_SYSTEM    "System"
53
	#define T_DETAILS        "Details"
54
	#define T_PROC_KILL      "Terminate"
55
	#define T_PROC_INFO      "Info"
56
	#define T_PROC_HEADER    "Process        RAM Kb     CPU %"
57
	#define T_CPU_LOAD       "CPU load %i%%   "
58
	#define T_RAM_USAGE      "RAM usage: %i Mb free of %i Mb"
59
	#define T_RD_USAGE       "System disk usage: %i Kb free of 1.4 Mb"
60
	#define T_TMP_USAGE      "TMP%i usage: %i Mb free of %i Mb"
7906 leency 61
#endif
7369 leency 62
 
8277 leency 63
enum {
64
	BTN_ID_SHOW_SYSTEM_PROCESSES=200,
65
	BTN_ID_PROC_KILL,
66
	BTN_ID_PROC_INFO,
67
	BTN_ID_MENU
68
};
7361 leency 69
 
7906 leency 70
//===================================================//
71
//                                                   //
8277 leency 72
//                       VARS                        //
7906 leency 73
//                                                   //
74
//===================================================//
75
 
8277 leency 76
int current_process_id = 0;
77
int proc_list[256];
78
 
79
checkbox show_system = { T_SHOW_SYSTEM, false };
80
 
81
sensor cpu;
82
sensor ram;
83
sensor rd;
84
sensor tmp;
85
 
86
proc_info Form;
87
 
88
int right_w;
89
 
90
//===================================================//
91
//                                                   //
92
//                       CODE                        //
93
//                                                   //
94
//===================================================//
95
 
96
void load_lib()
7369 leency 97
{
8277 leency 98
	load_dll(libimg, #libimg_init,1);
99
	load_dll(boxlib, #box_lib_init,0);
7369 leency 100
}
101
 
8277 leency 102
void main()
7361 leency 103
{
8277 leency 104
	int btn;
105
	load_lib();
8407 leency 106
	@SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER);
8381 leency 107
	loop() switch(@WaitEventTimeout(50))
8277 leency 108
	{
109
	   	case evMouse:
110
			SelectList_ProcessMouse();
111
			break;
112
		case evKey:
113
			GetKeys();
114
			if (key_scancode == SCAN_CODE_ESC) ExitProcess();
115
			if (key_scancode == SCAN_CODE_DEL) EventKillCurrentProcess();
116
			if (select_list.ProcessKey(key_scancode)) SelectList_LineChanged();
117
			break;
118
		case evButton:
8407 leency 119
			btn = @GetButtonID();
8277 leency 120
			if (1==btn) ExitProcess();
121
 
122
			if (show_system.click(btn)) {
123
				SelectList_LineChanged();
124
			}
125
			if (BTN_ID_PROC_KILL == btn) {
126
				EventKillCurrentProcess();
127
			}
128
			if (BTN_ID_PROC_INFO == btn) {
8383 leency 129
				RunProgram("/sys/tinfo", itoa(GetProcessSlot(current_process_id)));
8277 leency 130
			}
131
			break;
132
		case evReDraw:
133
			sc.get();
134
			DefineAndDrawWindow(screen.width/2 - 350, 100, 700, 490, 0x33, sc.work, T_APP_TITLE,0);
135
			GetProcessInfo(#Form, SelfInfo);
8946 leency 136
			if (Form.status_window&ROLLED_UP) break;
8277 leency 137
			if (Form.width  < RIGHT_X+370) { MoveSize(OLD,OLD,RIGHT_X+370,OLD); break; }
138
			if (Form.height < 420) { MoveSize(OLD,OLD,OLD,420); break; }
139
			right_w = Form.cwidth - RIGHT_X - GAP;
140
			right_w &= ~1; // make sure the number is even
141
			WriteText(GAP+5, WIN_CONTENT_Y-20, 0x90, sc.work_text, T_PROC_HEADER);
142
 
143
			//bool burger_active = false;
144
			//if (menu_id == OPEN_FILE) burger_active = true;
145
			//DrawTopPanelButton(BTN_ID_MENU, Form.cwidth-GAP-3, GAP, -1, burger_active);
146
 
147
			SelectList_Init(GAP, WIN_CONTENT_Y, PROCESS_LIST_W,
8933 leency 148
				Form.cheight-BOTPANEL_H-WIN_CONTENT_Y);
8277 leency 149
			SelectList_DrawBorder();
150
 
151
			DrawBar(select_list.x-2, select_list.y+select_list.h+2,
152
				select_list.w+scroll1.size_x+4, BOTPANEL_H, sc.work);
153
			DrawCaptButton(PROCESS_LIST_W+GAP-110+18, select_list.y+select_list.h+5,
154
				110,23,BTN_ID_PROC_KILL,0xF38181, 0xFFFfff, T_PROC_KILL);
155
			DrawCaptButton(PROCESS_LIST_W+GAP-165+18, select_list.y+select_list.h+5,
156
				46,23,BTN_ID_PROC_INFO,sc.button, sc.button_text, T_PROC_INFO);
157
			show_system.draw(GAP-1, select_list.y+select_list.h+10);
158
 
159
			//WriteText(RIGHT_X, WIN_CONTENT_Y+25, 0x90, sc.work, "Update period: 5 seconds");
160
			cpu.set_size(RIGHT_X, WIN_CONTENT_Y+25, right_w, 100);
161
			ram.set_size(RIGHT_X, WIN_CONTENT_Y+170, right_w, 23);
162
			rd.set_size(RIGHT_X, WIN_CONTENT_Y+240, right_w, 23);
163
		default:
164
			MonitorCpu();
165
			MonitorRam();
8381 leency 166
			SelectList_LineChanged();
8277 leency 167
			MonitorRd();
168
			MonitorTmp();
169
	}
7361 leency 170
}
7614 leency 171
 
8277 leency 172
void EventKillCurrentProcess()
7906 leency 173
{
8277 leency 174
	KillProcess(current_process_id);
175
	pause(10);
176
	SelectList_LineChanged();
7906 leency 177
}
7614 leency 178
 
8277 leency 179
void Processes__GetProcessList()
180
{
181
	int i, j;
182
	proc_info Process;
7906 leency 183
 
8277 leency 184
	select_list.count=0;
185
	for (i=0; i
186
	{
187
		GetProcessInfo(#Process, i);
188
		if (Process.name)
189
		{
190
			for (j=0; j<11; j++) if (Process.name[j]!=' ') {
191
				if (show_system.checked==false) {
192
					//do not show system process
193
					if (Process.name[0]=='@') break;
194
					if (!strcmp(#Process.name, "IDLE")) break;
195
					if (!strcmp(#Process.name, "OS")) break;
196
				}
197
				proc_list[select_list.count] = i;
198
				select_list.count++;
199
				break;
200
			}
201
		}
202
	}
203
}
204
 
205
void SelectList_DrawLine(dword i)
7906 leency 206
{
8353 leency 207
	int posy, j, len;
208
	char cpu_use[16], mem_use[16], mem_use_pretty[16];
8277 leency 209
	dword bg_color;
210
	proc_info Process;
211
	static unsigned maxcpu;
212
	if (!maxcpu) maxcpu = GetCpuFrequency();
7909 leency 213
 
8277 leency 214
	GetProcessInfo(#Process, proc_list[i+select_list.first]);
215
 
216
	posy = i *select_list.item_h + select_list.y;
217
	if (i % 2) bg_color = 0xFFFfff; else bg_color = 0xF0F0F0;
218
	if (i+select_list.first == select_list.cur_y) {
219
		current_process_id = Process.ID;
220
		bg_color = 0x67CCEB;
221
	}
222
	DrawBar(select_list.x, posy, select_list.w, select_list.item_h, bg_color);
7909 leency 223
 
8277 leency 224
	WriteText(GAP+5, posy+select_list.text_y, 0x90, 0, #Process.name);
225
 
8353 leency 226
	if (Process.use_memory < 3670016000)
227
	{
8277 leency 228
		sprintf(#mem_use, "%i", Process.use_memory/1024);
8353 leency 229
		len = strlen(#mem_use);
230
		strcpy(#mem_use_pretty, "               ");
231
 
232
		for (j=1; j<=len; j++) {
233
			EDI = sizeof(mem_use_pretty)-1-j - calc(j/4);
234
			mem_use_pretty[EDI] = mem_use[len-j];
235
		}
236
 
237
		WriteText(GAP+109, posy+select_list.text_y, 0x90, 0x444444, #mem_use_pretty+16-9);
8277 leency 238
	}
239
 
240
	sprintf(#cpu_use, "%i", Process.use_cpu*100/maxcpu);
241
	if (maxcpu) WriteText(GAP+203 - calc(strlen(#cpu_use)-4*8),
242
		posy+select_list.text_y, 0x90, 0x444444, #cpu_use);
243
}
244
 
245
void SelectList_LineChanged()
246
{
247
	Processes__GetProcessList();
248
	SelectList_Draw();
249
}
250
 
251
void MonitorRd()
252
{
253
	dword rdempty = malloc(1440*1024);
254
	CreateFile(0, 1440*1024, rdempty, "/rd/1/rdempty");
255
	free(rdempty);
8392 leency 256
	rdempty = get_file_size("/rd/1/rdempty") / 1024;
8277 leency 257
	DeleteFile("/rd/1/rdempty");
258
 
259
	sprintf(#param, T_RD_USAGE, rdempty);
260
	DrawIconWithText(RIGHT_X, rd.y - 25, 5, #param);
261
 
262
	rd.draw_progress(rdempty * rd.w / 1440);
263
}
264
 
265
dword GetTmpDiskFreeSpace(int _id)
266
{
267
	DIR_SIZE dir_size;
268
	sprintf(#param, "/tmp%i/1", _id);
269
	dir_size.get(#param);
270
	dir_size.bytes += dir_size.files/2 + 32 * 512; //file attr size + FAT table size
271
	dir_size.bytes /= 1024*1024; //convert to MiB
272
	return dir_size.bytes;
273
}
274
 
275
void MonitorTmp()
276
{
277
	char text_status[64];
278
	int i, yy=WIN_CONTENT_Y+300;
279
	dword tmp_size[10];
280
	dword free_space;
281
	for (i=0; i<=9; i++)
282
	{
8392 leency 283
		get_file_size( sprintf(#param, "/tmp%i/1", i) );
284
		if (EAX) {
285
			tmp_size[i] =  EAX / 1024 / 1024;
8277 leency 286
			free_space = tmp_size[i] - GetTmpDiskFreeSpace(i);
287
			sprintf(#text_status, T_TMP_USAGE, i, free_space, tmp_size[i]);
288
			tmp.set_size(RIGHT_X, yy, right_w, 23);
289
			tmp.draw_progress(free_space * right_w / tmp_size[i]);
290
			DrawIconWithText(RIGHT_X, tmp.y - 25, 50, #text_status);
291
			yy += 65;
292
		}
293
	}
294
}
295
 
296
void DrawIconWithText(dword _x, _y, _icon, _title)
297
{
8821 leency 298
	int size = DrawIcon16(_x, _y, sc.work, _icon);
299
	WriteTextWithBg(_x+ICONGAP, _y + size - 16, 0xD0, sc.work_text, _title, sc.work);
8277 leency 300
}
301
 
302
dword GetCpuLoad(dword max_h)
303
{
304
	dword idle;
305
	dword CPU_SEC = GetCpuFrequency() >> 20 + 1;
306
	dword IDLE_SEC = GetCpuIdleCount() >> 20 * max_h;
307
 
308
	EAX = IDLE_SEC;
309
	EBX = CPU_SEC;
310
	$cdq
311
	$div ebx
312
	idle = EAX;
313
 
314
	return max_h - idle;
315
}
316
 
317
int pos=0;
318
void MonitorCpu()
319
{
320
	static dword cpu_stack[1980*3];
321
	int i;
322
	if (!cpu.w) return;
323
 
324
	cpu_stack[pos] = GetCpuLoad(cpu.h);
325
	if (cpu_stack[pos]<=2) || (cpu_stack[pos]>cpu.h) cpu_stack[pos]=2;
326
 
327
	sprintf(#param, T_CPU_LOAD, cpu_stack[pos]);
328
	DrawIconWithText(RIGHT_X, cpu.y - 25, 48, #param);
329
 
8381 leency 330
	#define LINEW 8
331
	for (i=0; i
332
		DrawBar(i+cpu.x, cpu.y, LINEW, cpu.h-cpu_stack[i], PROGRESS_BG);
333
		DrawBar(i+cpu.x, cpu.h-cpu_stack[i]+cpu.y, LINEW, cpu_stack[i], 0xDFA13B);
334
		//DrawBar(i+LINEW+cpu.x, cpu.y, 1, cpu.h, PROGRESS_BG);
8277 leency 335
	}
336
 
337
	pos++;
338
	if (pos>=right_w) {
339
		pos = right_w-1;
340
		for (i=0; i
341
			cpu_stack[i] = cpu_stack[i+1];
342
		}
343
	}
344
}
345
 
346
void MonitorRam()
347
{
348
	ram.draw_progress(GetFreeRAM()*ram.w/GetTotalRAM());
349
	sprintf(#param, T_RAM_USAGE, GetFreeRAM()/1024, GetTotalRAM()/1024);
350
	DrawIconWithText(RIGHT_X, ram.y - 25, 51, #param);
351
}
352