Subversion Repositories Kolibri OS

Rev

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