Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Exploring Linux command-line space time (fabiensanglard.net)
260 points by bertman on Sept 26, 2023 | hide | past | favorite | 38 comments


Wow, Collect2 sounds kinda hacky based on the linked documentation¹:

- There is user code that needs to run before main. With C/C++ allowing separate compilation, the compiler invocation that creates the entrypoint doesn't know about all of these static constructors.

- Therefore Collect2 pretends to be ld; when invoked, it tries to call the real ld and examines its output

- It creates a c file with a table containing any constructor symbols it finds, then compiles that

- Finally it links the program again, together with the new object file

¹: https://gcc.gnu.org/onlinedocs/gccint/Collect2.html


Thank you for the explanation I did not have the courage to gather.


I'm pretty sure the documentation actually lies though.

Yes, `collect2` exists, but with the existence of all the modern linker sections and scripts, it simply forwards everything to `ld` unchanged. There's no sneaky inspection, C code generation, or second linking pass.


Thanks for the clarification. I was questioning my sanity, because collect2 as described in the doc sounds not just hacky, but also like a waste of compile time.


The words "undefined reference to '__main'" give me war flashbacks.


IIRC collect2 is only required on a few weird platforms such as AIX.


Great write up from Fabien as usual.

FYI, you can also get a lot from the `time` command right now in your shell:

   /usr/bin/time -v ls
        Command being timed: "ls"
        User time (seconds): 0.00
        System time (seconds): 0.00
        Percent of CPU this job got: 100%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00
        Average shared text size (kbytes): 0
        Average unshared data size (kbytes): 0
        Average stack size (kbytes): 0
        Average total size (kbytes): 0
        Maximum resident set size (kbytes): 2304
        Average resident set size (kbytes): 0
        Major (requiring I/O) page faults: 0
        Minor (reclaiming a frame) page faults: 117
        Voluntary context switches: 1
        Involuntary context switches: 0
        Swaps: 0
        File system inputs: 0
        File system outputs: 0
        Socket messages sent: 0
        Socket messages received: 0
        Signals delivered: 0
        Page size (bytes): 4096
        Exit status: 0


Instead of "/usr/bin/time" you can also write "command time".


or its shorter form, \time


This is excellent across a few key dimensions.

- Clear and concise code.

- Clear communication: writing, technical explanations, and conveyance of thought process.

- Thorough and systematic.

Major kudos to the author. It was a fantastic refresher on the mechanics of fork() for me!

This kind of article is the reason I troll HN so much. Occasionally gems pop up.


Fabian is a treasure. He also made the Game Engine Black Books covering Wolfenstein 3D and Doom.

https://fabiensanglard.net/gebbdoom/


And his book on the Capcom system is interesting for me even though I’ve never played one.


this is a small personal nit complaint, but increasingly I see "Linux command-line" used to refer to things which have nothing to do with command-lines per se, which to me suggests some cli-fu with p|pes || other tricky \\&\\&& if-fi $(things).

Perhaps we need some better vocabulary to separate these ideas, but the world of unix includes "shell" ideas, and their interplay with cli, there's ascii text ttys, there's ansi tty control and TUIs, and what this article is about, traditional unix userspace process-space, system calls, C-libs, compilers vs interpreters got skipped, but C-vs-Rust, just all without the overhead of GUI frameworks which brings a bit more clarity to timings but they don't absolutely, to do timings could be better to write a program to launch all these things and make sure that program is "sticky" in unix space-time.

This is a good article, but because I like to search for things again later, just as a headline I wouldn't really call this exploring Linux command-line space or time


"Exploring a C program's memory use over time, on Linux" would be more accurate. Nothing in this article deals with our three spatial dimensions, the typical use of the word "space".


For software contexts, we refer to space as memory or overall storage depending on the architecture.


Yet when refering to both space and time, you should consider explicating that space = memory, time = time.


> you should consider explicating that space = memory, time = time.

That seems unnecessary. Even a layman knows that in the context of a computer, space != physical space. Most people think of disk space, but it can also refer to memory usage [0]. The meaning of the name was obvious to me after reading the first half of the first sentence of the article. I thought it was clever :)

[0] https://en.m.wikipedia.org/wiki/Space_complexity


TFA covered more than just C programs.


And what exactly does TFA have to do with this conversation? It's literally not mentioned in the link or these comments (currently).


TFA stands for “the fucking article”


And I always read it as "The Featured Article"


I've always preferred "The Full Article", in contrast to reading _only_ the headline.


There is also "The Fine Article".

FWIW, all these things date back to the ancient Usenet "RTFM" (where M = manual) which would undermine an expansion of "featured" in my view (since manuals are not "featured", like links in news aggregators). The other alternative Fs still work, though.


While I do like the idea, etc, and I know there are many examples of this, the actual name st is used by several other linux programs. Perhaps the author should consider calling it differently? For example (and I do have tape in most of my boxes, so st is installed on my main machine and my backup machine).

    st SCSI Tape device
    st simple terminal
Just wondering how others handle conflicts like this too...


> Following process/thread creation is done via netlink(7).

shouldn't ptrace with PTRACE_O_TRACEFORK (etc.) be enough (and much easier)?

the high overhead of strace the author notes is due to it doing PTRACE_SYSCALL, which is not needed here.


Some terminals probably scroll sideways, but this `st` program seems to require running to completion. So, ^C does not give you any report at all.

While it does RSS not Proportional Set Size (PSS) { arguably parsing /proc/PID/smaps_rollup should be added } and does not integrate program launch with tracking or use netlink to track making kids, an alternative that might interest some is a scrolling terminal kind of log report out of the Nim https://github.com/c-blake/procs .

    pd -pBHd1 -f "%N %R %< %> %J %a" $(pf firefox)
or launch the program with `prog &` and use the shell `$!` to specify the PID or add -D'R<>J' to get differential reports (or many other things). On the plus side, this can track many other things besides just memory & kids { like IO in the above example, assuming your Linux has /proc/PID/io support compiled in }.

You could always capture the output and feed it to some plotting program.


I like the blog style.

Does anyone have a single CSS sheet with a similar style, instead of individual elements/lines all being spans with inline css?


Absolutely beautiful, sharp, expressive and minimal. Can be viewed on an e-reader.

It looks like he made the change to this theme in 2018:

https://fabiensanglard.net/bloated/index.html


> Does anyone have a single CSS sheet with a similar style, instead of individual elements/lines all being spans with inline css?

Sorry what are you asking?


I've seen this design style before. I'm hoping there's an existing model to work from, e.g. how Tufte css spawned many individual layouts but instead inspired by a terminal.


He talks about his theme here:

https://fabiensanglard.net/html/index.html


The second example says

"First allocate 1GiB, free it, then spawn a process which does the same but with 2GiB."

In the code it seems it is the parent process that does the second call too.

  if (pid != 0) {
    malloc_and_fill(1L << 31);   
  } 
The result is not affected but the wording is not very clear I think.


Thank you for reporting this mistake. It is fixed now :) !


Would be curious to see how a "Hello World!" in Zig compares.


Why not run `st` on it and post the result here?


Can you explain the AOSP compilation time? I don't understand how it can be 8 minutes - was that a clean build?


The GitHub repo link is broken





Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: