Talk:Linux CPU utilization
someone to review this task
I think this task is pretty much ready to go... but as its the first task I've added to Rosetta Code, I'd like to get someone to review it first :) --Paul (talk) 09:49, 27 May 2015 (UTC)
OS specific task
I don't think OS specific tasks are a good idea. Does this really need to be OS specific?
For starters, /proc is not unique to Linux,
although other OSes may provide other/better ways to get similar information
(e.g. getloadavg(3)
).
Some programming languages may provide OS agnostic routines/libraries to get similar information (which would be superior to grepping around in /proc/stat IMO). —dchapes (talk | contribs) 13:35, 27 May 2015 (UTC)
- Actually, it's probably better to have something like this be OS-specific. I've seen plenty of other tasks which are written in a way which implies they are OS-agnostic but where the difficulty of implementation is trivial for implementations which assume a specific OS context and near to implement portably without such assumptions. It'll never be perfect. And this one seems simple enough.
- That said, this one can be a pain if you don't have access to a linux implementation of your language. You can approximate, though, by using a surrogate file (representing /proc/stat) and code against that (changing it between reads). --Rdm (talk) 16:04, 27 May 2015 (UTC)
- It's easy for you to say (using a surrogate file), but I don't have access to a Linux system. But if somebody would include such a (sample) file here, it would be a snap to code this Rosetta Code task in REXX. -- Gerard Schildberger (talk) 00:10, 21 July 2016 (UTC)
- I was thinking something like that, after my undo of the (silly) Applesoft BASIC entry. Languages which cannot run on Linux should perhaps instead parse the string "cpu 259246 7001 60190 34250993 137517 772 0" as if they'd read it from the first line of /proc/stat, and display average CPU usage since boot, once, rather than using the delta to continuously show a more up-to-date figure. I've done just that with the Phix entry, leaving the Linux-only lines commented out, which as a bonus lets me claim that entry is now js-compatible. --Pete Lomax (talk) 17:39, 21 July 2022 (UTC)
Exclude guest and guest_nice
As per https://github.com/pcolby/scripts/issues/2 I'll update the description to specify that only the first (up to) 8 fields should be summed to calculate the total (as the 9th and 10th fields are already included in earlier ones). However, this does mean that all current solutions will be slightly incorrect, and need to updated. The Bash solution has already been updated - it was a one-line fix. Most other solutions should be nearly as simple to correct.
Output does not match top
I was working on a similar function for my i3bar & had this issue, so I came looking for answers. Even after adapting my implementation to mimick your calculation the result still appears to differ from top's by a factor of about 10.
Making implementations light
As this is an ongoing function with continual updates, it might be interesting to work on making each implementation efficient (this would also improve the actual reading).
For example, as I'm working with bash, I've tried to implement this with builtins to avoid spawning processes.