Subversion Repositories Kolibri OS

Rev

Rev 7422 | Rev 7532 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * System Monitor
  3.  * version 0.87
  4.  * Author: Leency
  5. */
  6.  
  7. #define MEMSIZE 4096*10
  8.  
  9. #include "../lib/io.h"
  10. #include "../lib/gui.h"
  11. #include "../lib/fs.h"
  12.  
  13. #include "../lib/obj/libio.h"
  14. #include "../lib/obj/libimg.h"
  15. #include "../lib/obj/libini.h"
  16.  
  17. //===================================================//
  18. //                                                   //
  19. //                       DATA                        //
  20. //                                                   //
  21. //===================================================//
  22.  
  23. #define CPU_STACK 440
  24. dword cpu_stack[CPU_STACK];
  25.  
  26. sensor cpu;
  27. sensor ram;
  28. sensor rd;
  29. sensor tmp[10];
  30.  
  31. dword tmp_size[10];
  32.  
  33. //===================================================//
  34. //                                                   //
  35. //                       CODE                        //
  36. //                                                   //
  37. //===================================================//
  38.  
  39. void main()
  40. {
  41.         proc_info Form;
  42.         dword cpu_frequency = GetCpuFrequency()/1000;
  43.         int id;
  44.  
  45.         incn y;
  46.  
  47.         load_dll(libio, #libio_init,1);
  48.         load_dll(libimg, #libimg_init,1);
  49.         load_dll(libini, #lib_init,1);
  50.  
  51.         GetTmpDiskSizesFromIni();
  52.        
  53.         loop()
  54.         {
  55.                 WaitEventTimeout(25);
  56.                 switch(EAX & 0xFF)
  57.                 {
  58.                         case evButton:
  59.                                 if (GetButtonID()) ExitProcess();
  60.                                 break;
  61.                  
  62.                         case evKey:
  63.                                 GetKeys();
  64.                                 if (key_scancode == SCAN_CODE_ESC) ExitProcess();
  65.                                 break;
  66.                          
  67.                         case evReDraw:
  68.                                 #define LEFT 25
  69.                                 #define ICONGAP 45
  70.                                 system.color.get();
  71.                                 DefineAndDrawWindow(150, 100, CPU_STACK+LEFT+LEFT+4+9, 480 + skin_height + 4, 0x34, system.color.work, "System Monitor",0);
  72.                                 GetProcessInfo(#Form, SelfInfo);
  73.  
  74.                                 y.n = 0;
  75.                                 if (cpu_frequency < 1000) sprintf(#param, "CPU frequency: %i Hz", cpu_frequency);
  76.                                 else sprintf(#param, "CPU frequency: %i MHz", cpu_frequency/1000);
  77.                                 DrawBlockHeader(LEFT, y.inc(20), 37, "CPU load", #param);
  78.                                 cpu.set_size(LEFT, y.inc(45), CPU_STACK, 100);
  79.  
  80.                                 sprintf(#param, "Total RAM: %i MiB", GetTotalRAM()/1024);
  81.                                 DrawBlockHeader(LEFT, y.inc(cpu.h + 25), 36, "RAM usage", #param);
  82.                                 ram.set_size(LEFT, y.inc(45), CPU_STACK, 23);
  83.  
  84.                                 DrawBlockHeader(LEFT, y.inc(ram.h + 25), 3, "System RAM Disk usage", "Fixed size: 1.44 MiB");
  85.                                 rd.set_size(LEFT, y.inc(45), CPU_STACK, 23);
  86.  
  87.                                 sprintf(#param, "TMP Disk 0 size: %i MiB", tmp_size[0]);
  88.                                 DrawBlockHeader(LEFT, y.inc(rd.h + 25), 50, "Virtual drive usage", #param);
  89.                                 tmp[0].set_size(LEFT, y.inc(45), CPU_STACK, 23);
  90.  
  91.                         default:
  92.                                 MonitorCpu();
  93.  
  94.                                 //MonitorRam();
  95.                                 ram.draw_progress(
  96.                                         GetFreeRAM()*ram.w/GetTotalRAM(),
  97.                                         GetTotalRAM()-GetFreeRAM()/1024,
  98.                                         GetFreeRAM()/1024,
  99.                                         "M"
  100.                                         );
  101.                                 DrawBar(ram.x+ram.w-96, ram.y-25, 96, 20, system.color.work);
  102.                                 sprintf(#param, "%i KiB", GetTotalRAM()-GetFreeRAM());
  103.                                 WriteText(ram.x+ram.w-calc(strlen(#param)*8), ram.y-25, 0x90, system.color.work_text, #param);
  104.  
  105.                                 //MonitorRd();
  106.                                 dir_size.get("/rd/1");                 
  107.                                 dir_size.bytes += dir_size.files/2 + 32 * 512; //file attr size + FAT table size
  108.                                 dir_size.bytes /= 1024; //convert to KiB
  109.                                 dir_size.bytes = 1440 - dir_size.bytes;
  110.                                 rd.draw_progress(
  111.                                         dir_size.bytes*rd.w/1440,
  112.                                         1440 - dir_size.bytes,
  113.                                         dir_size.bytes,
  114.                                         "K"
  115.                                         );
  116.  
  117.                                 //MonitorTmp();
  118.                                 if (tmp_size[0]) {
  119.                                         dir_size.get("/tmp0/1");
  120.                                         dir_size.bytes += dir_size.files/2 + 32 * 512; //file attr size + FAT table size
  121.                                         dir_size.bytes /= 1024*1024; //convert to MiB
  122.                                         dir_size.bytes= tmp_size[0] - dir_size.bytes;
  123.                                         tmp[0].draw_progress(
  124.                                                 dir_size.bytes*tmp[0].w/tmp_size[0],
  125.                                                 tmp_size[0] - dir_size.bytes,
  126.                                                 dir_size.bytes,
  127.                                                 "M"
  128.                                                 );                                     
  129.                                 }
  130.                 }
  131.         }
  132. }
  133.  
  134. void DrawBlockHeader(dword _x, _y, _icon, _title, _subtitle)
  135. {
  136.         WriteTextB(_x+ICONGAP, _y, 0x90, system.color.work_text, _title);
  137.         DrawIcon32(_x, _y, system.color.work, _icon);
  138.         WriteText(_x+ICONGAP, _y+20, 0x90, system.color.work_text, _subtitle); 
  139. }
  140.  
  141. dword GetCpuLoad(dword max_h)
  142. {
  143.         dword idle;
  144.         dword CPU_SEC = GetCpuFrequency() >> 20 + 1;
  145.         dword IDLE_SEC = GetCpuIdleCount() >> 20 * max_h;
  146.  
  147.         EAX = IDLE_SEC;
  148.         EBX = CPU_SEC;
  149.         $cdq
  150.         $div ebx
  151.         idle = EAX;
  152.  
  153.         return max_h - idle;
  154. }
  155.  
  156. _ini ini = { "/sys/settings/system.ini", "DiskSizes" };
  157. void GetTmpDiskSizesFromIni()
  158. {
  159.         char i, key[2];
  160.         key[1]=0;
  161.         for (i=0; i<=9; i++)
  162.         {
  163.                 key[0]=i+'0';
  164.                 tmp_size[i] = ini.GetInt(#key, 0) / 1024 / 1024;
  165.         }
  166. }
  167.  
  168. //===================================================//
  169. //                                                   //
  170. //                     MONITORS                      //
  171. //                                                   //
  172. //===================================================//
  173.  
  174. int pos=0;
  175. void MonitorCpu()
  176. {
  177.         int i;
  178.         if (!cpu.w) return;
  179.  
  180.         cpu_stack[pos] = GetCpuLoad(cpu.h);
  181.         if (cpu_stack[pos]<=2) || (cpu_stack[pos]>cpu.h) cpu_stack[pos]=2;
  182.        
  183.         DrawBar(cpu.x+cpu.w-30, cpu.y-25, 30, 20, system.color.work);
  184.         sprintf(#param, "%i%%", cpu_stack[pos]);
  185.         WriteText(cpu.x+cpu.w-calc(strlen(#param)*8), cpu.y-25, 0x90, system.color.work_text, #param);
  186.  
  187.         for (i=0; i<CPU_STACK; i+=2) {
  188.                 DrawBar(i+cpu.x, cpu.y, 1, cpu.h-cpu_stack[i], PROGRESS_BG);
  189.                 DrawBar(i+cpu.x, cpu.h-cpu_stack[i]+cpu.y, 1, cpu_stack[i], LOAD_CPU);
  190.  
  191.                 DrawBar(i+1+cpu.x, cpu.y, 1, cpu.h, PROGRESS_BG);
  192.         }
  193.  
  194.         pos++;
  195.         if (pos>=CPU_STACK) {
  196.                 pos = CPU_STACK-1;
  197.                 for (i=0; i<pos; i++) {
  198.                         cpu_stack[i] = cpu_stack[i+1];
  199.                 }
  200.         }
  201. }
  202.