Interesting Questions

Below is a breif collection of selected interesting questions I've received---and responded to---while TAing Operating Systems (COMS 4118).

Caching

Why use prefetch?

I found that there is a function called
prefetch()
inside the definition of list_for_each_entry:
#define list_for_each_entry(pos, head, member) \
    for (pos = list_entry((head)->next, typeof(*pos), member); \
    prefetch(pos->member.next), &pos->member != (head); \
    pos = list_entry(pos->member.next, typeof(*pos), member))

Answer

Loading that into L1 will speed up the execution of your program. (There're a couple x86 instructions PREFETCHXX, with XX being a couple options.) We see the speedup because we can load the data into cache while other instructions are executing. This avoids a stall for fetch from lower levels in the memory hierarchy when it's first used.