[SOLVED] Search for free and used pages

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
Luca91
Member
Member
Posts: 35
Joined: Wed Oct 09, 2013 12:16 pm

[SOLVED] Search for free and used pages

Post by Luca91 »

Hello mates,
I know that this is not a so technical question, but I'll ask it anyway in the hope that somebody will give it a look ;)
I'm trying to write a function that tell me how many free and used pages there are in a given range of virtual address (page table aligned!!),
here is the function I wrote:

Code: Select all

uint32_t check_pages(uint32_t startAddr,uint32_t length){
    pdirectory* dir = vmm_get_directory ();
    pd_entry* pagedir = dir->m_entries;
    int cfreepage = 0;
    int cusedpage = 0;
    uint32_t x = 0, y = 0;
    for(x = startAddr; x < (startAddr+length) ; x+=4096*1024){ // check 1 pagetable at a time
        if(pagedir[x>>22] != 0){   // check if pagetable exist
            ptable* table =(ptable*) pagedir[x>>22]; 
            for(y=x;;y+=4096){ // scan every single pages in the pagetable
                pt_entry* page = (pt_entry*)table->m_entries [ PAGE_TABLE_INDEX (y) ]; 
                if(((uint32_t)(page)>>22) != 0){ // check if a page is present FIXME this might be the problem
                    cusedpage++;
                    kernelPrintf("Found used page number: %d\n",PAGE_TABLE_INDEX (y));
                }
                else{
                    cfreepage++;
                    kernelPrintf("Found free page number: %d\n",PAGE_TABLE_INDEX (y));
                }
                if(PAGE_TABLE_INDEX (y)==1023) break;
            }
        }
        else{ // if a pagetable doesn't exist add 1024 free pages to the counter
            kernelPrintf("found free pagetable! (1024 free pages)\n");
            cfreepage+=1024;

        }
    }
    kernelPrintf("Used Pages Found: %d\n",cusedpage);   
    kernelPrintf("Free Pages Found: %d\n",cfreepage);   
    return 0;
}
This code works, but have one issue: some pages that are used, will result free..
I think that the if statement that I used to check if a page is present might be the problem:

Code: Select all

if(((uint32_t)(page)>>22) != 0)
There might be a better way to check if a page is present or not..

Thanks a lot for any help (or any hint in the right direction)
Last edited by Luca91 on Sun Feb 09, 2014 12:20 pm, edited 1 time in total.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Search for free and used pages

Post by bluemoon »

Why you need to count the usage within a range of virtual address?

Statistics for usage of user space allocation, whole process usage, etc would be more interesting.

By the way, it's far easier to keep the counter upon allocation/free, in an other word, you don't write function to scan.
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: Search for free and used pages

Post by VolTeK »

bluemoon wrote:(or any hint in the right direction)
You're asking us, to help you (The Experienced C Programmer), fix his/her broken code.

#-o

bluemoon wrote:By the way, it's far easier to keep the counter upon allocation/free, in an other word, you don't write function to scan.
Assuming the allocation data keeping procedures operate without error, it's the most optimal route of doing this imo.
Luca91
Member
Member
Posts: 35
Joined: Wed Oct 09, 2013 12:16 pm

Re: Search for free and used pages

Post by Luca91 »

You're asking us, to help you (The Experienced C Programmer), fix his/her broken code.
Well, I'm just asking for help just to know if the method (that if(((uint32_t)(page)>>22) != 0){ ) is good to test if a page is present or not.
I don't want that somebody fix it for me, I just want some thought on that.
As I said on the thread, I know that's not a very technical/elitist question.
And.. I need that function also to test if my page mapper/unmapper works correctly.

Thanks you for any help
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Search for free and used pages

Post by iansjack »

I don't understand what your >>22 is for. Am I being very silly or don't you just check bit 0 to see if the page is present or not?
Luca91
Member
Member
Posts: 35
Joined: Wed Oct 09, 2013 12:16 pm

Re: Search for free and used pages

Post by Luca91 »

iansjack wrote:I don't understand what your >>22 is for. Am I being very silly or don't you just check bit 0 to see if the page is present or not?
Yes, that's what was my 1st try:

Code: Select all

if(((uint32_t)(page) & 0x1) != 0){
But it doesn't works either.. the result is that all pages looks like free...
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Search for free and used pages

Post by iansjack »

Did you zero-fill the memory allocated for your page table before using it? If so, I can't understand how the Present bit can get set unless your code sets it.
Luca91
Member
Member
Posts: 35
Joined: Wed Oct 09, 2013 12:16 pm

Re: Search for free and used pages

Post by Luca91 »

iansjack wrote:Did you zero-fill the memory allocated for your page table before using it? If so, I can't understand how the Present bit can get set unless your code sets it.
Yes, I've cleaned the memory (zero-filled) of a table, then I set all pages on that table to be present. Then I run that function pointing to the start of that table..
That's strange :/
By the way thank you so much for your patience
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Search for free and used pages

Post by iansjack »

I'm totally confused now as to what you are checking for. If you have allocated memory for every entry in the page table, and have marked that memory as being in use, then what are you checking for? It seems that you already know that every entry represents memory that is in use.

I think I must have totally misunderstood your question. And I still don't understand what the >>22 is about. It seems to be a totally arbitrary number.
Luca91
Member
Member
Posts: 35
Joined: Wed Oct 09, 2013 12:16 pm

Re: Search for free and used pages

Post by Luca91 »

iansjack wrote:I'm totally confused now as to what you are checking for. If you have allocated memory for every entry in the page table, and have marked that memory as being in use, then what are you checking for? It seems that you already know that every entry represents memory that is in use.

I think I must have totally misunderstood your question. And I still don't understand what the >>22 is about. It seems to be a totally arbitrary number.
Yes, I've allocated memory for every entry of the table, and I've marked that pages as being present, the problem is: why that function see all pages to be free ?? :?
It should say that all pages are present and not free..
Also sorry for my insistence, I'm doing this for my thesis, so I'm really frustated right now #-o
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Search for free and used pages

Post by Brendan »

Hi,
Luca91 wrote:Also sorry for my insistence, I'm doing this for my thesis, so I'm really frustated right now #-o
My advice is, never attempt to pretend that page directories or page tables contain pointers. They don't. They contain an unsigned integer that is a combination of a physical address and flags.

For example, for this line of code:

Code: Select all

    ptable* table =(ptable*) pagedir[x>>22];
The variable "table" contains the physical address of the page table plus the flags (and not just the physical address of the page table).

The wrong value of "table" is used for everything after that.

A better idea might be:

Code: Select all

    uint32_t pageDirEntry = pagedir[x>>22];
    ptable* table = (ptable*)(pageDirEntry & 0xFFFFF000);
Of course after you've enabled paging, a physical address isn't a virtual address and therefore isn't a usable pointer. I'm assuming this code runs before paging is enabled though (where "eventual virtual addresses" aren't pointers yet, but physical addresses still are).


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.
Luca91
Member
Member
Posts: 35
Joined: Wed Oct 09, 2013 12:16 pm

Re: Search for free and used pages

Post by Luca91 »

Thank you Brendan, that was the issue ouch #-o !
With the pagetable value fixed now it works.. thanks :wink:
Post Reply