views:

305

answers:

4

is it possible to find out the page size of a processor using C program and malloc? and not by using sysconf() call?

+4  A: 

Standard C has no concept of a "page size" so, based on your tags, no. There may be an implementation-specific method - do you have a particular aversion to using sysconf? I'm assuming you mention it because it's a way to actually get the information you need, in which case, why not use it?

paxdiablo
interest is purely academic and this question was asked to me by someone! see replies below for the stuff I have tried so far.
cforfun
+1  A: 

Malloc isn't likely to tell you what you want to know. While it often has minimum sizes and alignment considerations of what it will allocate, they aren't necessarily the same as (or related to in any defined way) the page size. Are you willing to look elsewhere? What environment are you running?

Steven Schlansker
I am running in linux environment! Do you have a solution in mind for something other than malloc, see below what I have tried so far.
cforfun
+1  A: 

If you can #include some linux kernel header, you can find the macro PAGE_SIZE in

<asm/page.h>

On cygwin (and perhaps windows' ddk) i's in

<w32api/ddk/winddk.h>

There are "dirty tricks" (very, very dirty!) to calculat the page size at runtime. All unportable and totally system-dependent.

Let's have some fun!

A possible trick on may systems is to create a shared memory object only 1 byte long. The system call usually rounds the size up to the system page size. Bleach!

Another one, if you are running on unix-like systems and you dare to intercept the SIGSEGV signal, you could try to explore the memory by looking for valid areas and see which power of 2 they are rounded. Uhm...

So, why sysctl is not good for you?

Regards

Giuseppe Guerrini
yap, this is a good try! but see my answer below why it won't work since SEGV is not happening in a predictable way.
cforfun
A: 

(its not really the answer, but I am unable to add it in comments section, so replying here: its just some results from my investigation)

to find out page size of system/microprocessor without using sysconf is just for fun! This is what I tried so far:

-get the actual page size using sysconf(_SC_PAGE_SIZE): I got in this computer 0x1000 which is 4K (its intel 386 processor with 32-bit linux loaded in it.

-then I allocated 1 byte memory

-in a loop, using offset counter, I wrote beyond 1 byte allocated memory. it worked fine till 0x19a8 which is beyond 1 page size and just below 2 page size!

-SEGV happens somewhere between 1page to 2page size, towards the end. This is repeatble.

-so this method couldn't find the page size reliably.

That's why I asked this question. Is there a trick (other than using direct information available in kernel) to find out the page size? I am even ready to compromise on malloc (means using some other trick).

cforfun
"get the actual page size using sysconf(_SC_PAGE_SIZE): I got in this computer 0x1000 which is 4K (its intel 386 processor with 32-bit linux loaded in it." - most normal people would have just stopped there and considered themselves successful :-)
paxdiablo
"then I allocated 1 byte memory" (malloc?). Was that byte at a page boundary? if not, it's not surprising that your counter stopped at some unexpected address. I guess that your byte was at an address terminating by 0x658 (0x2000-0x19a8). You should loop toward lower addresses as well. Anyway the SIGSEGV trick only finds user address space fences. You should apply the trich to a shared memory map, which usually is allocated at a page boundary, and is preceded and followed by invalid pages (at least on the majority of OSes). Bye!
Giuseppe Guerrini
+1@Giuseppe. @cforfun if you get unexpected results it is most likely you who is at fault. `sysconf` will give you the OS page size, period. `malloc` is giving you an address from a pool, obviously larger than the default page size. OS page sizes need not match hardware pages exactly, they can be a subset or superset. Which is the reason you use sysconf instead of a value in a header. Even Giuseppe's method could fail as you can't know what will be in pages next to the one you are allocating(the runtime might already have allocated memory when you enter main).
jbcreix