4 ms·
Suppose you WANT malloc to be eager (i.e. make sure all the pages are committed right away, so you don't pay a performance penalty for it later), what do you do
by OskarS 7y ago
Suppose you WANT malloc to be eager (i.e. make sure all the pages are committed right away, so you don't pay a performance penalty for it later), what do you do?
The obvious answer is just looping through it and setting it to zero would be replaced with a call to `calloc` by an optimizing compiler, and doesn't calloc also do some kind of similar "lazy" trick? So what do you do? Mark the pointer as volatile and loop through it and set all the memory to 0 (or some other value)? Call memset, I guess?
EDIT: apparently malloc + memset compiles to calloc as well [0], so if you want to allocate, zero out memory, and make sure it's committed, using volatile seems like the best bet to me...
[0]: https://godbolt.org/z/qhaaYc https://godbolt.org/z/qhaaYc
- deleted 7y ago[deleted]
- cma 7y agommap has some kind of option to immediately make resident.
- OskarS 7y agoYeah, but that's a much lower-level call compared to malloc. Also platform-dependent.
- chrisseaton 7y agoYou have to use low-level and platform-dependent calls to do low-level and platform-dependent things, yes. Seems reasonable to me? What do you want to do? Bake in the virtual memory semantics of the Intel architecture into C somehow? No, you need to use tools out of the scope of C for this and that’s how it should be.
- saagarjha 7y agoMaybe memset_s?
- htfy96 7y agoFirst of all, you only need to touch a single byte of each page to populate it. Besides, this approach doesn't ensure that the backing pages is allocated, for it's (technically) legal to only allocate one physical zero page after the calloc. Therefore, you must use OS-dependent primitives to force eager allocation. On linux this can be achieved by two flags for mmap: MAP_POPULATE initializes the page table entries eagerly (but doesn't guarantee a backing store allocation), and MAP_LOCKED for touching all pages (just like the calloc, but at kernel level and more deterministic). Note that MAP_LOCKED doesn't raise error for touching errors due to low memory, so you still need to touch it again at user-level.
- OskarS 7y agoWhy wouldn’t the “volatile” approach be valid? I mean, the only way to guarantee that you can write to memory without error (or to make sure it’s backed by actual memory pages) is to actually write to it, and the way to guarantee you’re doing that in a platform independent way in C is to use volatile, right? Are you really saying that this is not possible with malloc, you have to drop down to platform-dependent system calls like mmap?
- codys 7y agoBecause it's legal for a platform to perform page deduplication as long as from the perspective of the program the correct operating model is followed, many operating systems (including Linux) have a "zero page" that is used for _all_ zeroed memory blocks in the system. Writing a non zero value then would replace it with a different page. This means that writing zero is unlikely to cause a real allocation to occur. Writing a non zero value may be more likely to cause a page allocation. That said: it can be entirely legal for a platform to do more page deduplication than just special casing the zero page. Linux has some kernel tooling called KSM (kernel same page merging).