News for hackers. Stuff that matters.
Re:The C standard library function atexit is broken by jcranmer on Wednesday June 19, 2019 @08:50PM (#20228803)
> The first reason was disk space: object files could simply be smaller. Nowadays, for all intents and purposes, disk space is free and unlimited.

It's not for disk space, it's runtime RAM occupation usage. The code for a .so is going to be slightly larger than the equivalent .a. At the very least, you'll need to burn extra space for the PLT and GOT. And PIC relocations are going to tend to burn more space than static linking relocations.

The advantage of a .so you're talking about is that you can load a library shared by multiple processes at a fixed virtual address location that's the same for all processes, and thus you only burn the library once in RAM. For static libraries, the embedded addresses are going to be slightly different depending on their corresponding libraries, so even magic deduplication isn't going to be able to keep you from having the library in multiple places in physical memory.

Re:The C standard library function atexit is broken by ThemalSpan on Wednesday June 19, 2019 @09:04PM (#20228890)
To your first point: iOS takes this idea to its logical conclusion and it seems to be an important aspect of iOS’s performance. https://iphonedevwiki.net/index.php/Dyld_shared_cache
Re:The C standard library function atexit is broken by loeg on Thursday June 20, 2019 @12:49PM (#20234724)
icache, TLB entries, and even L1 are much more limited than main memory, especially on embedded devices. By linking everything into a single object, iOS can save on wasted padding and use superpages to map the shared library into each process in a TLB-efficient way.
Re:The C standard library function atexit is broken by comex on Wednesday June 19, 2019 @10:54PM (#20229417)
macOS as well.
Re:The C standard library function atexit is broken by kazinator on Thursday June 20, 2019 @03:23AM (#20230656)
Modern shared libs load at different addresses in different processes. For that to work, they have to be compiled with relocatable code generation, and mapped read-only.

Ancient Linux distributions once had something called "a.out shared libs". These were mapped to fixed addresses. The distribution had to work out non-conflicting addresses for all available libraries. The build procedure for a.out shared libs involved intercepting the assembly output of GCC and doing some text processing on it.

Re:The C standard library function atexit is broken by dexen on Thursday June 20, 2019 @04:59AM (#20231074)
A modest proposal: let's use only static libraries (plugins aside). Let's help KSM efficiently merge the R/X, and the R/O memory pages of the static libraries of running processes. To maintain the current performance, let's give KSM a hint: the hint would be a string of standardized, canonical library pathname, including name and version number.

During the (static) linking of libraries, an extra metadata item would be stored in the ELF file: the canonical pathnames of all the static libraries used, along with base addresses of R/X segments (code) and R/O segments (r/o data). Upon forking a process, the information would be provided to kernel, and passed over to the KSM, to use as basis of same-page lookup and possible merging of the pages.

This effectively inverts the current mechanism. Currently when libraries are dynamically loaded, the dynloader uses actual file pathname of the shlib as the key for operation opposite to "merging" - i.e., re-uses the already loaded R/X and R/O pages, by adding proper memory mapping.

The proposed change is three-fold:

  1) extend the linker to add the metadata to ELF,

  2) extend in-kernel ELF interpreter to extract the info upon exec() and friends,

  3a) extend the KSM with a limited mode, where it would look up & merge only the hinted memory regions, in linear fashion, right after an exec() & friends.
An alternative to 3a), to avoid fussing about with KSM:

  3b) modify the VM subsystem to extend current swap/SHM so it provides an unique address range for each static lib canonical pathname. Requesting pages from this address range would map from the shared pages, if any process already loaded one such.
To handle adversarial processes on single machine, a further extension where crypto signatures are checked is possible.
Re:The C standard library function atexit is broken by CamperBob2 on Wednesday June 19, 2019 @09:56PM (#20229147)
RAM is free, too, at least as far as library code size is concerned.
Re:The C standard library function atexit is broken by mikeash on Wednesday June 19, 2019 @10:17PM (#20229258)
A typical system has dozens or hundreds of processes running even when nothing is going on. If they all had their own copy of every system library in RAM, it wouldn’t look so free. Especially on mobile devices.
Re:The C standard library function atexit is broken by zingermc on Wednesday June 19, 2019 @10:26PM (#20229296)
What if the OS could dedupe pages?
Re:The C standard library function atexit is broken by adrianmonk on Thursday June 20, 2019 @01:21AM (#20230129)
You'd need to arrange for the linking process to result in identical (and identically-aligned!) code pages. If you're going to all the trouble to ensure that, why not just write it all to a file and call it a shared library?
Re:The C standard library function atexit is broken by bayareanative on Thursday June 20, 2019 @06:08AM (#20231358)
Exactly. Emperors' new clothes from naive convention abandonment of solved problems.
Re:The C standard library function atexit is broken by loeg on Thursday June 20, 2019 @01:00AM (#20230029)
On the fly memory dedupe is incredibly expensive. Especially compared to just not duplicating in the first place.

Also, it's quite likely that static linked libraries wouldn't be located at the exact same page offsets every time they're linked in to a binary, making page-level dedupe not useful.

Re:The C standard library function atexit is broken by sebcat on Thursday June 20, 2019 @01:38AM (#20230207)
What if the OS could map a shared object that resides in one section of memory into the virtual address space of multiple processes?
Re:The C standard library function atexit is broken by kelnos on Wednesday June 19, 2019 @11:28PM (#20229593)
Do any OSes actually do this, though? If not, this is academic until one actually does.

Linux does have something called "KSM" that de-dupes memory pages, but it appears that it's only so a hypervisor can de-dupe guest OS pages? Odd that it's not more general than that. Though I guess there is a cost: the KSM code needs to scan through memory to find duplicate pages, which I can't imagine is fast.

Re:The C standard library function atexit is broken by choeger on Thursday June 20, 2019 @04:06AM (#20230860)
Would not work with any kind of LTO.
Re:The C standard library function atexit is broken by CamperBob2 on Wednesday June 19, 2019 @10:30PM (#20229309)
Meh, not so much. The processes will only page in the code they actually use.

Shared libraries are a goofy hack dating back to a time when people mistakenly thought they needed them, and they need to go away.

Re:The C standard library function atexit is broken by Annatar on Thursday June 20, 2019 @01:51AM (#20230267)
Shared object libraries are incredibly useful because they save memory and CPU resources. Cloning the process space during a fork(2) call, initializing structures, then context switching and whatnot is incredibly expensive.
Re:The C standard library function atexit is broken by _rtld_global_ro on Thursday June 20, 2019 @10:32AM (#20233278)
shared library can be expensive as well, the dynamic symbol lookup could be a lot more expensive than people would assume.
Re:The C standard library function atexit is broken by notacoward on Thursday June 20, 2019 @08:07PM (#20238721)
Correct. Shared libraries trade away CPU for memory. Even with some benefits in terms of cache warmth, the extra indirection and loader/VM complexity make shared libraries a net loss in terms of CPU.
Re:The C standard library function atexit is broken by coldtea on Thursday June 20, 2019 @04:05AM (#20230856)
>Cloning the process space during a fork(2) call, initializing structures, then context switching and whatnot is incredibly expensive.

Err, we're running Electron apps today, with a full blown embedded browser engine, a vm, and a hellish complex DOM for rendering. Others run apps inside containers, with their own basic OS and standard libs -- compared to that the above is negligible.

Re:The C standard library function atexit is broken by saagarjha on Thursday June 20, 2019 @04:47AM (#20231037)
> we're running Electron apps today

Some of us try not to on the belief that they don't perform acceptably.

Re:The C standard library function atexit is broken by imtringued on Thursday June 20, 2019 @07:42AM (#20231821)
You can usually avoid this problem by using the web version of Slack, Discord, Rocketchat, etc. A single browser running everything is obviously more efficient than running ten.
Re:The C standard library function atexit is broken by bayareanative on Thursday June 20, 2019 @06:12AM (#20231374)
Nope. A strawman wrong doesn't justify anything related to the topic. fork() duplicates everything, which maybe overkill, which is why pthreads and vfork+exec also exist.
Re:The C standard library function atexit is broken by notacoward on Thursday June 20, 2019 @08:04PM (#20238708)
That's not why pthreads exist, and vfork barely does. Pthreads exist to facilitate parallel operation (hence the name) on shared data, not as an efficiency measure. As for vfork, it has been deprecated since every UNIX implementation started marking pages as copy-on-write instead of actually copying. I was doing some of that work myself in 1990-91, so it's hardly new.

The real problems with static linking don't have to do with fork, except in the sense of increasing reliance on VM overcommit (which IMO is a bad idea already). It's more to do with masking reuse of the same library across unrelated processes, as an efficiency issue but even more importantly in terms of tracking dependencies and updating software. No matter how good your configuration management (or similar mechanism) is, rebuilding large-N statically linked executables is less efficient and more error-prone than using the same information to update small-N shared libraries.

Re:The C standard library function atexit is broken by choeger on Thursday June 20, 2019 @04:04AM (#20230848)
You are working on one of these electron-style apps, right?
Re:The C standard library function atexit is broken by CamperBob2 on Thursday June 20, 2019 @06:10AM (#20231365)
(Shrug) RAM is there to be used.

I don't think we need to get into my optimization street cred, but suffice it to say I've never used Electron and couldn't really tell you exactly what it is.

Re:The C standard library function atexit is broken by bayareanative on Thursday June 20, 2019 @06:15AM (#20231386)
Resources aren't infinite. It's arrogant and wasteful to be lazy and resource greedy.

Electron is an embedded web-browser meets cross-platform application platform.. it's bloated, slow and awful... it makes Java look snappy, lightweight and brilliant.

Re:The C standard library function atexit is broken by imtringued on Thursday June 20, 2019 @07:44AM (#20231840)
I don't get it. You're using more RAM for literally no benefit. The end result is exactly the same. There isn't even a productivity benefit in the vast majority of cases because a huge chunk of software is packaged by the OS to use shared libraries by default.
Re:The C standard library function atexit is broken by CamperBob2 on Thursday June 20, 2019 @02:54PM (#20236029)
Avoiding DLL hell is certainly a benefit. It seems likely that few participants in this thread besides myself have ever actually had to support end users.
Re:The C standard library function atexit is broken by dekhn on Thursday June 20, 2019 @09:54AM (#20232927)
you would be surprised to learn that for very large binaries (think about monolithic front end codes at large internet companies), code size is actually enough to put pressure on instruction caches and lead to paging!
Re:The C standard library function atexit is broken by imtringued on Thursday June 20, 2019 @07:34AM (#20231762)
It used to be free, until people thought it was free to waste.
Re:The C standard library function atexit is broken by tinus_hn on Thursday June 20, 2019 @02:00PM (#20235456)
RAM is free as long as you don’t have to pay the price. But it does have a price.