Subversion Repositories Kolibri OS

Rev

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