Unix, the Alarm System Call, and Power Management

Published at 20:37 on 25 April 2023

And by “Unix” I include Unix-like operating systems like Linux and MacOS. In fact, my experience is limited to Linux and MacOS in this regard, but I would be surprised if the various BSD and System V Unix systems out there with automatic power management differ much.

I have a simple alarm clock/reminder script I wrote in Python. The heart of it was the following logic:

def sleep_until(then):
    delta = then - time.time()
    if delta > 0.0:
        time.sleep(delta)
        return True
    return False

Now, the time.sleep call in Python is implemented as a call to sleep in the C standard library, which in turn is implemented as via the alarm system call. All of these accept an offset in seconds, which in the former case specifies the amount of time to sleep, and in the latter the amount of time before an alarm signal is delivered to the process.

The logic above is simplicity itself, yet from time to time my reminders would come in late! Eventually, I linked it to those times when the system suspended itself due to lack of activity for a while; and my alerts were late by an amount that corresponded with the time the system was suspended. Apparently, when Unix and Unix-like systems suspend themselves, time as specified to alarm ceases to pass; that system call only counts seconds that transpire when the system is awake.

The cure is to break up the sleeping into chunks, and to repeatedly check the system clock:

MAX_SLEEP = 60.0
def sleep_until(then):
    delta = then - time.time()
    if delta <= 0.0:
        return False
    while delta > 0.0:
        time.sleep(min(MAX_SLEEP, delta))
        delta = then - time.time()
    return True

At least, this seems to work. I implemented the change yesterday and alerts that spanned times when my computer was asleep got raised at the correct time. It’s a little ugly to replace a blocking with busy-waiting like this, but although the above logic busy-waits, it still spends most of its time blocked.

Note that this seems to affect other programs as well. In fact, one of my motives for writing this script was the frequent failure of the Gnome clock app to issue alarms at the proper time.

Note also that this assumes the computer will be in an awake state at the time the alert is scheduled. If the computer goes to sleep and stays asleep, it will issue no alerts during the time it is asleep. Remedying this state of affairs requires privileged system calls that one must be careful making. I decided that the safety of having a nonprivileged program was worth the slight chance of a missed alert; in my case, the problem almost always happens as a result of a system suspending itself on lunch break, with the alert time being while I am at my desk in the afternoon.

Halide Review

Published at 07:04 on 24 April 2023

It’s six weeks since I spent the money and decide to give Halide a whirl. It actually exceeds expectations. The manual focus feature it offers is still not as good as a traditional viewfinder and focus ring, but together with the focus peaking feature it’s pretty darn good. All of the close-ups in this gallery were taken with Halide.

iPhone plus Halide has basically become my camera of choice when I do not want to take (or simply do not have) my micro four-thirds outfit with me. It’s definitely a more capable close-up photography tool than a standard compact digicam.

Where the Rust Language Makes Sense

Published at 19:48 on 19 April 2023

Per this, I think Rust makes the most sense for things you would have otherwise written in C or C++. It is a more modern, relatively low-level, language than either of these two (and is much cleaner than C++, which was an attempt to add all sorts of extra features onto C, and which suffered as a result of having to be a proper superset of that earlier language).

If you were not going to write it in C/C++, in other words if computing resource limitations are not a constraining factor, then writing it in Rust just doesn’t make sense. Use some other programming language with automatic garbage collection, so you don’t have to worry so much about memory management.

Which means, that for other than embedded systems, it is generally stupid to use Rust from the ground up. Use a higher-level language like Python. If the higher-level language proves too slow or too memory-inefficient, do some profiling, find the weak links in the chain, and rewrite those in Rust instead of rewriting them in C/C++. There’s already libraries out there to facilitate doing the latter.

And that is why I can’t feel much love for Rust: because I am right now not running up against any resource constraints that make Python, Java, or Kotlin impractical.

Trump’s Ace in the Hole

Published at 21:46 on 3 April 2023

Trump has far better chances of winning in 2024 than some informed observers seem to think.

It all rests on what the group No Labels does. They have been rightly castigated as being mostly a benefit to Trump, and boy could they ever.

You see, despite how the media loves to fawn over them as the rational and reasonable ones, centrists have a really seamy and dare I say even militant underside. They know what they want, they want it really bad, and they will act on those wants, even to the ultimate detriment of their preferences.

It is exactly the same criticism often made of the Left, but it applies to centrists even more. Those of us on the left are at least constantly being lectured to about the dangers of refusing to compromise. But the same chattering classes that never tire of sermonizing the Left tend to see the Center as intrinsically “responsible.”

Witness what happened to Humphrey in 1968 (lost votes on the left) and McGovern in 1972 (lost votes in the center). There’s no shortage of voices blaming Humphrey’s lost on voters to the left sitting it out. By contrast, when it comes to McGovern those same voices… blame left/liberal voters for having selected McGovern in the first place! In other words: if you don’t support our preferred candidate, it’s all your fault, and if we don’t support your preferred candidate, it’s also all your fault.

Many centrists are so beset with feelings of entitlement, and so coddled and pandered to by the media, that they are largely shielded from being confronted with the downsides of their own political behavior. And many of them to this day have trouble seeing their hypocrisy.

It is in this light that the seriousness of a threat by a centrist No Labels candidate can fully be seen.

To the above you can add that Republicans tend to be more extreme and inflexible in general, meaning it will be much easier for a centrist to peel votes away from the Democrats. No Labels itself is aware of this; here is one of their initial projection maps showing how they hoped to win. Note how most of the states they think they can flip went for Biden in the last election:

They probably won’t do that well, of course. All it would take is them failing to flip Texas and Florida and boom! Second Trump term.