Subversion Repositories Kolibri OS

Rev

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