Date/Time & CPU/RAM Usage Problem!

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
thinklogic

Date/Time & CPU/RAM Usage Problem!

Post by thinklogic »

hi room
i face these problems during my adventure to build my own operating system.
i wish someone here to help me. thank you

the problems are like below

1. i want to display time and date on my operating system
=========================================================
so i guess i will need to use BIOS INT 1AH with function 02 and 04 but how i know the date and time already change? how to continuously update the time and date? i expect myself to run a loop

Code: Select all

while(system_still_on) {
   mov_ah = 02 then 04;
   int 1a;
}
so, the problem is if we wrote a script like above, will it has the behaviour like script below?

Code: Select all

$i = 10;
while($i < 20) {
   display $i;
}
our script will bound to hang the system coz it is infinite loop. we can see the processor usage around 99% on windows task manager before script timeout.

i wish somebody to guide me on this matter. thanks.
i wish to know also what are the popular OS ways to get and display time/date.


2. i wish to know how the obtain the RAM usage and CPU usage information
========================================================================
now how can my os know the CPU usage and RAM usage?
is there any interrupt which provide such information?
if no, what are the method that those OS(s) like FreeBSD, Linux, Windows use to have such information?
thinklogic

Re:Date/Time & CPU/RAM Usage Problem!

Post by thinklogic »

okay, i wrote this clock script already
it will display second by second

Code: Select all

org 100h

jmp   start

msg1   db   'One Second (am i correct) passed ...',13,10,0
tSec   db   0

start:
   push   cs
   pop   ds
   
   mov   ah,2
   int   1ah
   mov   [tSec],dh
   
   timer_recheck:
      cmp   [tSec],dh
      je   timer_check_again
      jne   timer_notice
   
   timer_check_again:
      mov   ah,2
      int   1ah
      jmp   timer_recheck
   
   timer_notice:
      lea   si,[msg1]
      call   print
      mov   [tSec],dh
      jmp   timer_check_again
      
print:
   mov   ah,0eh
   print_start:
      lodsb
      cmp   al,0
      je   print_exit
      int   10h
      jmp   print_start
   print_exit:
      ret
assemble it to a .com file

the problem is, it hooks up 100% CPU Usage on my computer?
anyone have idea how to lower this code CPU Usage?

the goal is to make my OS display a clock.
pkd

Re:Date/Time & CPU/RAM Usage Problem!

Post by pkd »

Hi,

Have a look at Port 0x70 for the Real Time Clock in Ports.a of the interupt list (Just Search if you havent got it) its in ports.a, Using the ports is a better method if you intend to run in protected mode,

note that the values are stored in a strange format ie 0x09 = 9 but 0x10 = 10 & not 16 but you will work that out

As for your script, I would just link a variable to your timer Handler, when it expires you just run your Clock Read function,
as it is only updated when you require (eg every Second) the rest of the time your processor should be free to do other things.

Hope this Helps

pkd
thinklogic

Re:Date/Time & CPU/RAM Usage Problem!

Post by thinklogic »

i wish to display the clock once the computer starts, (i suppose i am in the real-mode)

{* i am still far away from dealing with protected mode, newbie here.}

as for the number, i suppose the BIOS just use the 16 base HEX to display the 10 base DECIMAL.

you talk about "timer Handler", so how can i write a timer handler? how the BIOS or CPU tells me that 1 second passed already if i didn't loop it to compare it with my old second (tSec) like what i did in the code i posted to this thread?

how i know my processor is in the IDLE mode?

please guide me
newbie (i don't know much, plez share ur knowledge with me)
pkd

Re:Date/Time & CPU/RAM Usage Problem!

Post by pkd »

Ok no need to worry about protected mode at the moment,
as you are still in real mode.

You are probably better of to stick with the real mode interrupt calls at the moment.

If You want to write a timer interupt handler First you would need to set the PIT Timer using the ports, and then read up on IRQ handling (search on this site for IRQ & PIT)

You will need to point the IDT (interrupt descriptor table) entry for the timer at your interrupt handler, Which would then (inc/dec) your counter until it reaches the required value, I set my timer to 10ms which is a nice round number, so for this I would call my update code after 100 interrupts ( which sounds a lot but isnt on a fast machine)

Hope This doesnt sound to complicated, good luck with your OS.

pkd
pkd

Re:Date/Time & CPU/RAM Usage Problem!

Post by pkd »

I Forgot something,

To put the cpu into idle you use the HLT instruction (Assuming ASM) but only in Ring 0 code (which at the moment you would be)


This command stops the processor until the next interrupt/Exception ocuurs.

Basicaly my message loop looks somethink like this

MsgLoop:
hlt

cmp MsgCount,0
jz MsgLoop

{then Handlers for different messages}

jmp MsgLoop

Ok bye for now.
pkd
thinklogic

Re:Date/Time & CPU/RAM Usage Problem!

Post by thinklogic »

thanks for ur great input... :) pkd rulez
i am moving into the PIT & IRQ soon
but before that, i wish to verify the thing i know with ya first, if i am wrong, please correct me, thanks in advance.

1. Ring 0 = real mode, am i correct?, can you briefly introduce to me why its name "ring" and is there any other rings.

2. you said interrupt/exception. can you briefly tell me what is this interrupt, what are the different between the INT 13 we called on BIOS INT (files) with the interrupt / exception you mentioned.

3. what is the relationship of IRQ and INTerrupt? what is IRQ. sometime i see on my windows machine IRQ for COM1/2 port, IRQ for printer, IRQ for VGA and ... what are the relationship of IRQ and PORT? <- i havent understand the concept, hopefully u willing to spend some time to guide me.

4. i havent got any information about how to check the CPU usage of my developed OS.


thanks first.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:Date/Time & CPU/RAM Usage Problem!

Post by Brendan »

Hi,
thinklogic wrote: 1. Ring 0 = real mode, am i correct?, can you briefly introduce to me why its name "ring" and is there any other rings.
The CPU has several different modes. Real mode is 16 bit, segmented with no protection. Protected mode is a completely different CPU mode.

Protected mode has 4 seperate protection levels (or protection rings). Ring 0 has the least protection, which (in general) can do anything and is normally used for kernel code. Ring 3 has the most protection and is normally used by user programs (to prevent them from accessing memory, IO ports, etc that they shouldn't). Rings 1 and 2 may be used by device drivers or something (or may not be used at all).

I'd consider it a good idea to read Intel's "Software Development Manual, Volume 3: System Programming Guide" from start to end TWICE before starting your kernel. The first time you read it most of it will probably not make much sense. The second time you read it you'll start understanding it. After you think you understand it you'll end up using it as reference material (I'm always looking up something in it).
thinklogic wrote: 2. you said interrupt/exception. can you briefly tell me what is this interrupt, what are the different between the INT 13 we called on BIOS INT (files) with the interrupt / exception you mentioned.

3. what is the relationship of IRQ and INTerrupt? what is IRQ. sometime i see on my windows machine IRQ for COM1/2 port, IRQ for printer, IRQ for VGA and ... what are the relationship of IRQ and PORT? <- i havent understand the concept, hopefully u willing to spend some time to guide me.
Different pieces of electronics (timers, floppy controller, serial ports, video cards, etc) sometimes need to tell the CPU that they need something done. To do this they generate an IRQ (Interrupt ReQuest) by sending a signal on an IRQ line. All of the IRQ lines are connected to a pair of special chips called PIC chips (Programmable Interrupt Controllers). The PIC chips convert the signals on the IRQ lines into an actual interrupt on the CPU's bus. When the CPU notices the special interrupt data on it's bus it generates an interrupt (which isn't much different to a software interrupt (for e.g. "int 0x1A" or "int 0x10") except for what caused it.

In addition the CPU can generate interrupts on it's own, called exceptions. These exceptions are caused by situations that the CPU doesn't know how to handle. For example, if a program tries to access some memory that it's not allowed to access the CPU will generate an exception (for e.g. page fault), which should be handled by the kernel.

All IRQ's, software interrupts and exceptions have a number assigned to it (for e.g. a page fault generates interrupt number 0x0E, and the instruction "int 0x66" generates interrupt number 0x66). This interrupt number is used as an index into a special table. In real mode this table is called the IVT (Interrupt Vector Table), and in protected mode it is called the IDT (Interrupt Descriptor Table). Regardless of what caused the interrupt the CPU looks it up in this table and starts executing the code that the table tells it to.

Getting back to the electronic devices (timers, floppy controller, serial ports, video cards, etc). All of these devices need to be accessable by the CPU. Most of them use IO ports, and some use special memory ranges. Any electronic device could be manufactured to use any IRQ/s, any IO ports and any memory regions, but this would create lots of confusion. To avoid this confusion certain devices always use certain IRQ/s and IO ports. For example, the PIT chip (Programmable Interval Timer chip) always uses IRQ 0 and IO ports 0x40 to 0x5F, while the keyboard controller chip always uses IRQ 1, IRQ 12 and IO ports 0x60 to 0x64. There is no special relationship - mostly it is determined by tradition (backwards compatibility).
thinklogic wrote: 4. i havent got any information about how to check the CPU usage of my developed OS.
You won't have (or need) any information about how to check the CPU usage until your OS is capable of doing more than 1 thing at a time (multi-tasking).


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
thinklogic

Re:Date/Time & CPU/RAM Usage Problem!

Post by thinklogic »

You won't have (or need) any information about how to check the CPU usage until your OS is capable of doing more than 1 thing at a time (multi-tasking).
let say my OS is single tasking...
and if i run

Code: Select all

loopback:
      jmp loopback
accidentally in my OS code

the CPU USAGE will be 100%

i think even i am single tasking, i still need the information on CPU usage... care to tell me, so that i can bench the performance of my OS
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Date/Time & CPU/RAM Usage Problem!

Post by Candy »

thinklogic wrote:
You won't have (or need) any information about how to check the CPU usage until your OS is capable of doing more than 1 thing at a time (multi-tasking).
let say my OS is single tasking...
and if i run

Code: Select all

loopback:
      jmp loopback
accidentally in my OS code

the CPU USAGE will be 100%

i think even i am single tasking, i still need the information on CPU usage... care to tell me, so that i can bench the performance of my OS
unless you use the HLT operand or ACPI/APM power states, your CPU usage will always be 100%. You can hardcode the 100% in your OS.

If you are not single tasking you can actually start measuring how often, when you found a timer pulse, you were idling. If that's 99% of the time, you're idle for 99%. If you never find the idle loop, you're not idle.
Post Reply