Pagefaults

Started by
13 comments, last by jonr 20 years, 1 month ago
What sort of performance hit does a page fault incur? 50ms? It might be small, but if you get a page fault every time you go through your main loop, that''ll slow things down a hell of a lot...
Advertisement
A page fault can''t possible take 50ms.. when working with virtual memory (usually by the OS), page faults occur all the time. IIRC from some online tutorial, the memory has to be swapped to disk, and when it needs to be accessed, it will generate a page fault to signal that it needs to be swapped to memory.

edit: Oh. SiCrane already said that.
quote:Original post by psykr
A page fault can''t possible take 50ms..


Yes. Consider the initial report - 500-700 page faults / second. That suggests something in the range of 1.5 - 2.0 ms. At 50 ms, 500 pfs would take 25 seconds.

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
A page fault can be either hard or soft.

A soft pf means the requested page was in memory, but not mapped into the process'' working set (the page could either be in memory but mapped to another process'' address space, or on its way to be written to the pagefile).

A hard page fault means the page wasn''t in memory and has to be fetched from disk, in which case the thread will wait for i/o completion and another thread will be scheduled instead.

A hard pf will take at least 10-15ms due to disk i/o, normally probably 30-50ms. On a saturated system it could be much longer (seconds).

I wouldn''t necessarily worry only because I saw 500-700 pf/s, although it''s probably possible to lower that figure. Are you having performance problems also?

You already received the advise to minimize code size. In addition, try to minimize allocations from the heap.

instead of allocating on heap:

int* foo = new int[256];
func(foo);
delete[] foo;

try to keep memory on stack:

int foo[256];
func(foo);

You could play with the small block heap (see _set_sbh_threshold, don''t forget to check it''s return value) to minimize allocations from the OS.

If you have some data structure that you often allocate and free, try to re-use it instead.

Instead of:

void OnClientConnected()
{
m_clientdata = new ClientData();
}
void OnClientDisconnected()
{
delete m_clientdata;
}

you could have a pool of ClientData instances that you re-use.

void OnClientConnected()
{
m_clientdata = g_clientdataPool.Get();
}
void OnClientDisconnected()
{
m_clientdata.Reset();
g_clientdataPool.Put(m_clientdata);
}

If you have large data structures, make sure to keep minimal data size. For instance, don''t have an array of int:s if you only need an array of byte:s.
thats all sound advice, its time for some good ol'' fashion code optimizing. Performance isnt bad yet but it can always be better

This topic is closed to new replies.

Advertisement