this post was submitted on 16 Feb 2024
917 points (99.1% liked)

linuxmemes

25144 readers
492 users here now

Hint: :q!


Sister communities:


Community rules (click to expand)

1. Follow the site-wide rules

2. Be civil
  • Understand the difference between a joke and an insult.
  • Do not harrass or attack users for any reason. This includes using blanket terms, like "every user of thing".
  • Don't get baited into back-and-forth insults. We are not animals.
  • Leave remarks of "peasantry" to the PCMR community. If you dislike an OS/service/application, attack the thing you dislike, not the individuals who use it. Some people may not have a choice.
  • Bigotry will not be tolerated.
  • 3. Post Linux-related content
  • Including Unix and BSD.
  • Non-Linux content is acceptable as long as it makes a reference to Linux. For example, the poorly made mockery of sudo in Windows.
  • No porn, no politics, no trolling or ragebaiting.
  • 4. No recent reposts
  • Everybody uses Arch btw, can't quit Vim, <loves/tolerates/hates> systemd, and wants to interject for a moment. You can stop now.
  • 5. 🇬🇧 Language/язык/Sprache
  • This is primarily an English-speaking community. 🇬🇧🇦🇺🇺🇸
  • Comments written in other languages are allowed.
  • The substance of a post should be comprehensible for people who only speak English.
  • Titles and post bodies written in other languages will be allowed, but only as long as the above rule is observed.
  • 6. (NEW!) Regarding public figuresWe all have our opinions, and certain public figures can be divisive. Keep in mind that this is a community for memes and light-hearted fun, not for airing grievances or leveling accusations.
  • Keep discussions polite and free of disparagement.
  • We are never in possession of all of the facts. Defamatory comments will not be tolerated.
  • Discussions that get too heated will be locked and offending comments removed.
  •  

    Please report posts and comments that break these rules!


    Important: never execute code or follow advice that you don't understand or can't verify, especially here. The word of the day is credibility. This is a meme community -- even the most helpful comments might just be shitposts that can damage your system. Be aware, be smart, don't remove France.

    founded 2 years ago
    MODERATORS
     
    all 30 comments
    sorted by: hot top controversial new old
    [–] bleistift2@feddit.de 107 points 1 year ago (6 children)

    I find this a highly interesting problem. How do you measure how much CPU time a program needs? The OS has ceded its control of the CPU to the program. Does it just look at the clock after it’s in charge again to derive a program’s load?

    While we’re at it: How does the OS even yank the CPU away from the currently running process?

    [–] Jajcus@kbin.social 76 points 1 year ago

    Well behaving programs give control back to the kernel as soon as they are done with what they are doing. If they don't the control is forcefully taken away after some assigned time.

    It looks something like this:

    Something happens – e.g. a key is pressed – a process waiting for this event is woken up and gets e.g. 100ms to do it stuff. If it can handle the key press in 50ms, kernel notes it used 50 ms of CPU time and can give control to another process waiting for an event or busy with other work. If the key press triggered long computation the process won't be done in 100ms, the kernel notes it used 100ms of CPU time and gives control to other processes with pending events or busy with other work.
    After one second the kernel may have noted:

    Process A: used 50ms, then nothing, then 100ms, another 100ms and another 100ms
    Process B: was constantly busy doing something, so it got allocated 6 * 100ms in that one second
    Process C: just got one event and handled it in 50ms
    Process D: was not waken at all

    So total of 1000ms was used – the CPU was 100% busy
    Of that 60% was process B, 35% process A and 5% process C.

    And then that information is read from the kernel by top and displayed.

    How does the OS even yank the CPU away from the currently running process?

    Interrupts. CPU has means triggering and interrupt at a specific time. Interrupt means that CPU stops what it is doing and runs selected piece of kernel code. This piece of kernel code can save the current state of user process execution and do something else or restore saved execution of another process.

    [–] agent_flounder@lemmy.world 66 points 1 year ago (3 children)

    Timer based interrupts are the foundation of pre-emptive multitasking operating systems.

    You set up a timer to run every N milliseconds and generate an interrupt. The interrupt handler, the scheduler, decides what process will run during the next time slice (the time between these interrupts), and handles the task of saving the current process' state and restoring the next process' state.

    To do that it saves all the CPU registers (incl stack pointer, instruction pointer, etc), updates the state of the process (runnable, running, blocked), and restores the registers for the next process, changes it's state to running, then exits and the CPU resumes where the next process left off last time it was in a running state.

    While it does that switcheroo, it can add how long the previous process was running.

    The other thing that can cause a process to change state is when it asks for a resource that will take a while to access. Like waiting for keyboard input. Or reading from the disk. Or waiting for a tcp connection. Long and short of it is the kernel puts the process in a blocked state and waits for the appropriate I/O interrupt to put the process in a runnable state.

    Or something along those lines. It's been ages since I took an OS class and maybe I don't have the details perfect but hopefully that gives you the gist of it.

    [–] raspberriesareyummy@lemmy.world 11 points 1 year ago

    as a non computer scientist who is programming multitasking applications, you did a good job at explaining context switching :)

    [–] AE5NE@lemmy.radio 8 points 1 year ago* (last edited 1 year ago) (2 children)

    I think a lot of modern kernels are “tickless” - they don’t use a timeslice timer, and only context switch on IO interrupt, process yield, or when timeouts are specifically requested (including capping cpu-bound processes). https://en.m.wikipedia.org/wiki/Tickless_kernel

    [–] kbotc@lemmy.world 16 points 1 year ago (1 children)

    Tickless means it’s not based on the computer frequency and idle CPUs can stay idle rather than being annoyingly brought into high power mode ever 100 Hz, but it’s still firing interrupts based on scaling timed variables.

    They’re now called “Dynticks”

    SUSE wrote the vaguely more understandable write up that Linux foundation links to: https://www.suse.com/c/cpu-isolation-full-dynticks-part2/

    BTW, the Linux RCU code is evil but interesting: https://www.p99conf.io/session/how-to-avoid-learning-the-linux-kernel-memory-model/

    [–] agent_flounder@lemmy.world 3 points 1 year ago

    Fascinating stuff. Obviously a lot has changed since I took an undergrad OS class lol. Hell, Linux didn't even exist back then.

    [–] itsnotits@lemmy.world 4 points 1 year ago (1 children)

    changes its* state to running

    [–] lars@lemmy.sdf.org 6 points 1 year ago

    changes its* state to running

    Oh, because its* a pointer to state-change. Good catch!

    [–] lambchop@lemmy.world 18 points 1 year ago (1 children)
    [–] bleistift2@feddit.de 3 points 1 year ago

    An awesome find. Thank you very much!

    [–] AE5NE@lemmy.radio 16 points 1 year ago* (last edited 1 year ago)

    yes, it looks at a fine-grained clock, usually a cycle counter provided by the CPU for this purpose, to aggregate total on-cpu time for each process.

    [–] chellomere@lemmy.world 6 points 1 year ago

    Look up preemptive task scheduling. Basic processes will get interrupted after using a too long time slice.

    [–] henfredemars 5 points 1 year ago* (last edited 1 year ago) (1 children)

    How do you measure how much memory a process is using?

    Shared libraries. Shared memory. Virtual. Physical. Memory mapped files. Some applications depend heavily on the availability of the file system cache for good performance, so should you consider that memory technically in use by that process? What if a process is providing a service to other applications and allocates memory on behalf of those apps to provide those services? If I ask the kernel to allocate lots of things for me to do my job should that really be my memory?

    There are popular measures to get an idea of the memory consumed by a process, but none can tell the whole truth and nothing but.

    I.e. could be the dress.

    [–] agent_flounder@lemmy.world 3 points 1 year ago* (last edited 1 year ago)

    Hmm. It's been a hot minute (ok 30 years) since I learned about this stuff and I don't know how it works in Linux in excruciating detail. Just the general idea.

    So I would be curious to know if I am off base.. but I would guess that, since the kernel is in charge of memory allocation, memory mapping, shared libraries, shared memory, and virtualization, that it simply keeps track of all the associated info.

    Info includes the virtual memory pages it allocates to a process, which of those pages is mapped to physical memory vs swap, the working set size, mapping of shared memory into process virtual memory, and the memory the kernel has reserved for shared libraries.

    I don't think it is necessary to find out how much is "technically in use" by a process. That seems philosophical.

    The job of the OS is all just resource management and whether the systems available physical memory is used up or not. Because if the system spends too much time swapping memory pages in and out, it slows down everything. Shared memory is accounted for correctly in managing all that.

    Maybe I am not understanding your point, but the file system is a different resource than memory so a file system cache (like a browser used) has zero impact on or relevance to memory usage as far as resource management goes.

    [–] Crack0n7uesday@lemmy.world 48 points 1 year ago (2 children)
    [–] autokludge@programming.dev 1 points 1 year ago* (last edited 1 year ago)

    "sudo kill -9 4346" for additional brutality

    [–] PraiseTheSoup@lemm.ee 25 points 1 year ago (2 children)
    [–] senkora@lemmy.zip 70 points 1 year ago

    100% equals one full core. Higher numbers are possible for multithreaded processes.

    [–] summerof69@lemm.ee 27 points 1 year ago (1 children)
    [–] oce@jlai.lu 10 points 1 year ago (1 children)

    Is that from one of those crazy lads who dared open more than one tab on Chrome?

    [–] summerof69@lemm.ee 7 points 1 year ago

    That'd be the RAM column.

    [–] XTL@sopuli.xyz 20 points 1 year ago

    Heh, freshly started Java. Hasn't allocated all your ram and swap yet.

    [–] Thcdenton@lemmy.world 17 points 1 year ago

    Missing the zombie process

    [–] knacht1@lemmy.world 13 points 1 year ago

    Try btop. It looks nice..

    [–] Vampiric_Luma@lemmy.ca 9 points 1 year ago

    I was wondering how to do this last night

    [–] badbytes@lemmy.world 6 points 1 year ago
    [–] Unforeseen@sh.itjust.works 2 points 1 year ago

    Lmao at RAM chips and CPU cola