[HN Gopher] How to programmatically find out if computer is on
___________________________________________________________________
How to programmatically find out if computer is on
Author : marcodiego
Score : 183 points
Date : 2021-11-20 18:53 UTC (4 hours ago)
(HTM) web link (www.haiku-os.org)
(TXT) w3m dump (www.haiku-os.org)
| syngrog66 wrote:
| write a function whose sig is like "boolean isOn()". call it. if
| it returns true, the computer is on. if it returns false, the
| computer is also on. done. ship it!
|
| add a // TODO in v2: handle case if it never returns, or the
| process crashes, or caller times out waiting
| AnimalMuppet wrote:
| I was working on an embedded system. I told the hardware guy that
| the CPU clock input wasn't running. He said, "Any reasonably
| intelligent software should have detected that condition and
| handled it." I replied, "Seems to me the software acted
| appropriately for the conditions."
| h2odragon wrote:
| I tried inverting the clock and running a one way hash and my
| CPU vanished...
| a-dub wrote:
| woah! i had no idea beos lived on in an oss project!
| sneeeeeed wrote:
| It works great on my Thinkpad. I run it on the metal.
| capableweb wrote:
| Interesting, but I think parent was asking how to run it on
| silicon.
| a-dub wrote:
| now i'm confused.
| capableweb wrote:
| "On metal" would refer to running the OS without any
| additional layers like virtualization, in this context.
|
| But it could also refer to the material "metal" (or the
| collection name for bunch of materials really), whereas
| computers are commonly known for being made with a lot of
| silicon instead of metal.
|
| Hopefully I didn't dissect too much of the frog to kill
| it.
| IndexCardBox wrote:
| I like how it also has support for the ENIAC platform. I kicked
| my family out of the house so I had room to install the ENIAC.
| Happy to hear there's an OS I can run on it to get real work
| done, like posting unfunny jokes for strangers on Hacker News.
| wk_end wrote:
| It upsets me that this function's interface allows it to
| describe silly things like "an ENIAC running on a 68000" or "an
| Apple II running on SPARC". Wouldn't be an issue if C++
| supported GADTs.
| lifthrasiir wrote:
| While it is clearly a joke, it has a legitimate use as a syscall
| that does nothing so can be used to test the syscall process
| itself [1]. (Its cousin `is_computer_on_fire` is not a syscall.)
|
| [1]
| https://github.com/haiku/haiku/commit/ccaac177f64038c160de6f...
| jrumbut wrote:
| I was thinking the same thing, although in reverse:
|
| Imagine putting a few of these in as a joke and then you take
| it out and your parallelized code doesn't work anymore.
|
| Not sure how all that's handled on Haiku specifically.
| marcodiego wrote:
| https://xkcd.com/1172/
| tialaramex wrote:
| Sure.
|
| Of course, one important trick here is that no matter how fast
| your system call is, _not_ making a system call is just faster.
| io_uring is an example, as are futexes. So it 's worth spending
| more effort on figuring out tricks to avoid doing the system
| call than exploiting tricks to shave a cycle or two off the
| time taken to execute one.
| spicybright wrote:
| Completely depends on your use case. If you're checking if
| sys calls work, you'd only need to run one first and see if
| it succeeds. Then you can be reasonably sure you have access
| to the rest.
|
| And different programs have different speed requirements.
| DonHopkins wrote:
| Asynchronous JavaScript browser DOM callback API:
| <html onon="alert('Computer turned on!')"
| onoff="alert('Computer turned off!')" >
| kjhughes wrote:
| Asynchronous is definitely the way to go. I once tried
| synchronous code. Had to move it to a service worker so as to
| not bog down the UI, and you don't even want to know what
| service workers do when the computer is off. Let's just say
| it's undocumented.
| FartyMcFarter wrote:
| Presumably there's a non-joke version of this at some level in
| the hardware / firmware stack?
| rezonant wrote:
| https://docs.microsoft.com/en-us/windows/win32/power/system-...
| kreeben wrote:
| I read "Soft Off" as "sort of" and though, well, that's
| probably the best answer one can get in certain situations.
| kazinator wrote:
| // power cycle computer bool was_on =
| set_computer_on(true); // returns prior value if
| (was_on) { set_computer_on(false);
| set_computer_on(true); } else { // was previously
| off, is now on: nothing to do! }
| rock_artist wrote:
| is_computer_on_fire() seems more usable...
| makepanic wrote:
| weird how it's returning a constant
| https://github.com/haiku/haiku/blob/8f16317a5b6db5c672f33181...
|
| Interesting it seems to come from BeOS:
|
| https://github.com/haiku/haiku/commit/bcf475ec65f1060c0a8abf...
|
| where the same function had a hn thread about it already a
| couple years ago :D
|
| https://news.ycombinator.com/item?id=8780606
| salawat wrote:
| Not 451. I'm disappointed at the missed opportunity.
| lucb1e wrote:
| I can find various results where that number is negative
| (Faltings height of LMFDB elliptic curve 1688.c1; north east
| longitude of el Baix Segura) but not where the number is
| positive. It's part of a DOI string though, but that's not
| really a number.
|
| Probably I'm being nerd sniped. https://xkcd.com/356/
| speed_spread wrote:
| Doc doesn't specify the unit of measure used. Also would be
| nice to know if fire was caused by computer itself or from an
| external cause.
| sharmin123 wrote:
| Avoid Hacking Threats: Hacking Risk You Face With Gadgets:
| https://www.hackerslist.co/avoid-hacking-threats-hacking-ris...
| BurningFrog wrote:
| Descartes for the digital age:
|
| "I compute, therefore I am on"
| DeathArrow wrote:
| In Soviet Russia the computer checks you to see if you are on.
| thoughtsimple wrote:
| Where is the function is_halting() ?
| yjftsjthsd-h wrote:
| Right? Especially since the implementation is so simple, and
| even runs in O(1) time:) https://xkcd.com/1266/
| aequitas wrote:
| Reminds me of 'onstat' in Brendan Gregg's special tools:
| https://www.brendangregg.com/specials.html
| marcodiego wrote:
| Although many people are laughing, I believe the function works
| ok. The only complicated part is to find a way to call it when
| the computer is not on.
| YXNjaGVyZWdlbgo wrote:
| I know it's not as common anymore but if the computer is in a
| "hold and catch fire" incident it won't report temperature sensor
| readings. What happens? Is it considered off?
| mare5x wrote:
| Similar situation: a process queries the operating system (CPU
| scheduler) to find out if the process is currently being executed
| on the CPU.
| 13of40 wrote:
| Odd they have a TI 99/4a platform but no TMS990 CPU.
| [deleted]
| atkailash wrote:
| I love it when this sort of just fun random thing is thrown into
| code.
| dottrap wrote:
| Since the not-on result is 'undefined', a devilish implementer
| could return 1 for the off case, just to screw with people.
|
| (How could this work in theory? Maybe the computer has a separate
| watchdog controller board which allows the computer to be turned
| on and off remotely, and that controller acts as a proxy for the
| API request.)
| rezonant wrote:
| Only if the watchdog also has a CPU that can run programs when
| the "real" CPU is off. But then we'd need a is_watchdog_on() to
| know if that one is powered on, in the case where the wall plug
| has been pulled.
| h2odragon wrote:
| > If the computer isn't on, the value returned by this function
| is undefined.
|
| Shouldn't it just block until the computer turns back on?
| Probably interferes with pthreads.
| eminence32 wrote:
| Hmm, it seems like you need to make sure the computer is on
| before calling this function, or else you run the risk of
| getting back undefined data. Seems like we also need a function
| that can be used to tell if the computer is on...
| salawat wrote:
| Nah. Just bump that responsibility to the caller, and power
| supply manufacturer. Have the caller call power_on_computer()
| first always. This will ensure you always get defined state!
|
| ...What do you mean not every power supply manufacturer
| implements the power_on_computer() function as a NOOP if the
| computer is already on?!
| h2odragon wrote:
| Some implement it that way, some it returns and error
| message or throws an NMI; then ya got the cheap clones that
| use it to frob a toggle without regard to the state it was
| in (which there's no way to read)... Then ya got the weird
| ones (HP) where it only works right if you call it twice
| with a 40ms delay between while streaming a sample from
| "Swan Lake" over their NDA'd 1wire thingy.
| ancode wrote:
| There's a syscall to check whether the computer is on so just
| call that before this and you'll be fine
| Someone wrote:
| That introduces a time-of-check to time-of-use bug
| (https://en.wikipedia.org/wiki/Time-of-check_to_time-of-
| use).
| jcwayne wrote:
| Seems like a reasonable compiler would inline the most likely
| implementation as 1. So, while technically undefined, it's safe
| to assume that it will 'return' 1 regardless of the actual
| on/off state of the computer.
| yholio wrote:
| {return 1;} is clearly a faulty implementation, it's entirely
| possible that the computer is already turned off by the time
| the function is executed using power stored in the decoupling
| capacitors of the CPU.
| azundo wrote:
| Since the behavior is undefined when the computer is off
| this implementation seems correct in your hypothetical as
| well.
| jeremyjh wrote:
| I agree on the implementation, but I'm pretty sure if the
| computer is off you aren't getting a 1.
| josefx wrote:
| I would be unreasonable. The power state appears to me as
| highly asynchronous to program execution. I see no explicit
| guarantees on how the state is retrieved or synchronized,
| without synchronization the state wont be properly updated.
| We know that the computer was of at some point, so it starts
| of as undefined. Given undefined as starting state and the
| lack of update guarantee we can assume that it will always
| return an undefined value. This opens up a lot more
| optimization possibilities than assuming a 1.
| rcakebread wrote:
| I was expecting this to be about using IPMI on a BMC to see if
| the motherboard is powered on.
| slmjkdbtl wrote:
| I'm curious if there's any significance in number 0.63739 in this
| implementation double
| is_computer_on_fire(void) { return 0.63739;
| }
|
| https://github.com/haiku/haiku/blob/8f16317a5b6db5c672f33181...
| largbae wrote:
| This reminds me of
| http://www.hasthelargehadroncolliderdestroyedtheworldyet.com...
| Waterluvian wrote:
| I'd like to see the unit tests for the non-true return state.
| chizhik-pyzhik wrote:
| I think that would have to be an integration test
| 1_player wrote:
| Result is undefined if computer isn't on, so any value is OK,
| no tests necessary.
| 13of40 wrote:
| It's actually pretty straightforward:
|
| turn_computer_off();
|
| Assert(!is_computer_on(), "Computer should have been off but
| it's on.");
|
| The opposite is a bit tricky, though.
| [deleted]
| ancode wrote:
| This assert doesn't work like you think it does. If the
| computer is off is_computer_on is undefined, so if this is
| called when the computer is already off it could return a
| nonzero value, thus resolving to true even though the
| computer is already off.
| zoomablemind wrote:
| > is_computer_on()
|
| How does this relate to computer being in Halt state, which is
| quite valid state for servers (there could be even a dedicated
| Halt button)? Technically computer hardware is ON. I remember
| that in such state it shows a "carrot" prompt (>>>) from the SRE
| console.
|
| By the way, there is also HCF instruction (Halt and Catch Fire).
|
| https://en.wikipedia.org/wiki/Halt_and_Catch_Fire_(computing...
| spicybright wrote:
| Wouldn't a CPU halt state mean no code is running?
| wereHamster wrote:
| a computer has multiple CPUs. One may be halted while another
| still running.
| sgtnoodle wrote:
| If the function call doesn't allow you to specify a CPU
| core, then it seems like it would be testing either the
| logical or'ing of all core's powered-on state, or the state
| of the current core.
|
| In order for the code to execute anywhere, at least one
| core must be powered on and not halted. Also, I would
| consider a halted CPU to still be powered on.
|
| Otherwise, the function's name has no relation to it's
| behavior and it's a buggy function on top of being a
| useless but amusing thought experiment.
| dfox wrote:
| which implies that the power state of the whole core
| cluster is on, so the syscall is defined to return 1
| onlydnaq wrote:
| A CPU can halt as a power saving measure, and still respond
| to interrupts.
| dmitshur wrote:
| I've wondered what Go APIs involving channels should do when
| the computer is put to sleep and later on wakes up.
| Filligree wrote:
| > By the way, there is also HCF instruction (Halt and Catch
| Fire).
|
| I was somewhat recently scarred for life by this instruction,
| having just discovered that if you execute HCF on the MIPS-
| based integrated circuits in Stationeers, the simulated IC...
| explodes in your face.
| dkga wrote:
| "How to find out computer is on fire"
| kjhughes wrote:
| Functional programming with monads is the right way to implement
| this.
| avl999 wrote:
| A sideeffect free api to turn on the computer in haskell would
| be brilliant.
| WJW wrote:
| Please, can we make it `MonadIO m => m Bool` so that this can
| work with any IO-like monad and/or monad transformer stacks
| based off IO? It will also make unit testing much easier.
| perihelions wrote:
| There's an elegant FP solution that involves searching an
| infinite data structure using only a finite amount of
| computation -- actually, with precisely zero computation,
| because the machine isn't on.
|
| It's like lazy evaluation, only more lazy.
| h2odragon wrote:
| Wait. When the machine isn't on, P=NP!
|
| So our security systems are (useless | perfect)!
|
| I think there might even be other implications...
| MikeTheGreat wrote:
| Nice job - having shown that P is the same as NP factorial
| will allow us to solve a lot of really hard problems!
| rezonant wrote:
| Golf clap.
| marcosdumay wrote:
| It's obviously `IO Bool`, so it's equivalent to directly
| evaluating in an imperative language and you gain nothing from
| the monadic interface.
|
| I'm sure some impurity "pragmatist" will try to argue you can
| make a perfectly good version of it as `Bool`. That kind of
| pragmatism is better ignored because it interferes with
| compiler optimization and standardization. As an example, you
| can look at the recent discussion around `==` and `/=`.
|
| (And I just noticed some people may seriously argue that, so
| here comes the /s.)
| praptak wrote:
| See also isUserAMonkey and isUserAGoat.
|
| I think the following always holds: isUserAGoat==!isComputerOn,
| in other words the user is a goat if and only if the computer is
| off.
| omgwtf1000 wrote:
| Keeps timing out.
| Animats wrote:
| What's so amusing today is that "off" tends to be an illusion
| today. It's more like "sleeping". There's still some stuff
| running. Listening for the On button, Wake on LAN, timers, the
| system management engine...
| rossdavidh wrote:
| Javascript equivalent function is kind of wonky so they had to
| make is_the_computer_really_on() to do what you want here.
| finnh wrote:
| there's a convenient npm package to handle that for you
| h2odragon wrote:
| Yeah but you never know whose computer the result applies to
| psyc wrote:
| And a separate package by the same prolific author for not_on
| that depends on the first package.
| corpMaverick wrote:
| Paraphrasing Descartes. "I think, therefore I am on".
| cesarb wrote:
| While this is a joke, there are some contexts in which is does
| make sense to ask whether the computer is on. For instance, most
| laptops have an embedded controller which runs all the time, even
| when the laptop is powered off, and manages things like battery
| charging and the power state. Within that embedded controller, an
| is_computer_on() function could legitimately return "false".
| capableweb wrote:
| Yeah, would make sense in that case. Probably would name the
| function differently though, to indicate the difference.
| "is_computer_on" makes me think it's checking the state of the
| computer this code is running on, not the state of another
| computer than "myself".
| salawat wrote:
| I posit a much more interesting question.
|
| Given a lump of silicon, how does one programmatically tell if it
| is a computer?
| h2odragon wrote:
| `rock.exists()`? see also the last line of most "weather rock"
| forecasting stations.
| firefoxd wrote:
| Where does the code run when the computer is off?
|
| Edit: I see the downvotes, yet i still don't know if it's a joke.
| I was genuinely asking the question.
| agildehaus wrote:
| thatsthejoke.jpg
| icehawk wrote:
| In the cloud
| fragmede wrote:
| that's the joke
| tibanne wrote:
| must be serverless
| rzzzt wrote:
| Embedded controller, management engine, low-power core.
| charles_f wrote:
| It looks like you haven't heard of quantum computing
| rezonant wrote:
| The computer is only on or off when you measure it.
| h2odragon wrote:
| Genuine answer: the code cant run when the computer is off,
| thus the joke.
|
| The question is worth asking (all are) but then soliciting
| "quantum computing" as an answer makes it a doubly worthy set
| up line, so thanks twice.
| slacka wrote:
| When I first read the headline, I assumed this was some kind
| of query to the Intel Management Engine. But in this context,
| it's some good programming humor.
| zatarc wrote:
| I think the is_computer_on_fire function is far more useful
___________________________________________________________________
(page generated 2021-11-20 23:00 UTC)