News for hackers. Stuff that matters.
Re:Why I Write Games in C (yes, C) by notacoward on Saturday September 21, 2019 @04:50PM (#21037852)
> it was generally non-deterministic

This is basically it. People who have never worked on actual real-time systems just never seem to get that in those environments determinism often matters more than raw performance. I don't know about "soft" real-time (e.g. games, or audio/video) but in "hard" real-time (e.g. avionics, industrial control) it's pretty routine to do things like disable caches and take a huge performance hit for the sake of determinism. If you can run 10% faster but miss deadlines 0.1% more often, that's a fail. It's too easy for tyros to say pauses don't matter. In many environments they do.

Re:Why I Write Games in C (yes, C) by Skunkleton on Saturday September 21, 2019 @05:22PM (#21037989)
Its not just pauses either. Non-deterministic memory usage is a big deal too.
Re:Why I Write Games in C (yes, C) by GuB-42 on Saturday September 21, 2019 @06:28PM (#21038277)
I have actually worked on a system where malloc() was forbidden. In fact it always returned null. Buffers were all statically allocated and stack usage was kept to a minimum (it was only a few kB anyways).

The software was shipped with a memory map file so you know exactly what each memory address is used for. A lot of test procedures involved reading and writing at specific memory locations.

It was for avionics BTW. As you may have guessed, it was certified code with hard real time constraints. Exceeding the cycle time is equivalent to a crash, causing the watchdog to trigger a reset.

Re:Why I Write Games in C (yes, C) by Impossible on Saturday September 21, 2019 @09:41PM (#21039095)
Malloc was forbidden in a AAA title I worked on. Interestingly enough we also had two (!!!) embedded garbage collected scripting languages that could only allocate in small (32MB, 16 MB) arenas and would assert if they over allocated.
Re:Why I Write Games in C (yes, C) by zippie on Sunday September 22, 2019 @02:53AM (#21040036)
In modern C libraries malloc is mmap’ed anon pages underneath for an increasingly larger set of conditions these days.
Re:Why I Write Games in C (yes, C) by hackits on Saturday September 21, 2019 @07:27PM (#21038571)
Sounds very similar to NASA C programming guidelines. Each module during the initialization period would allocate its static memory size. Every loop had a upper bound's max iteration to prevent infinite loops.

I think they posted the guidelines and it was a wonderful read about how they developed real time systems.

Re:Why I Write Games in C (yes, C) by mike00632 on Sunday September 22, 2019 @03:05AM (#21040064)
Do you happen to have a link to the guidelines?
Re:Why I Write Games in C (yes, C) by kimown on Sunday September 22, 2019 @04:46AM (#21040339)
Re:Why I Write Games in C (yes, C) by hackits on Sunday September 22, 2019 @04:34AM (#21040306)
Re:Why I Write Games in C (yes, C) by neop1x on Monday September 23, 2019 @03:06AM (#21046579)
I was wondering why I couldn't open this pdf on my android pdf viewer and this link gives me cloudflare's captcha. It uses google's ReCaptcha - storefronts, bicycles, semaphores. What a terrible practice. People stop overusing cloudflare!
Re:Why I Write Games in C (yes, C) by loeg on Saturday September 21, 2019 @08:05PM (#21038735)
Why even define a malloc() in this environment if it always returns NULL?
Re:Why I Write Games in C (yes, C) by seanmcdirmid on Saturday September 21, 2019 @09:50PM (#21039130)
Because even if you don't use it directly, you might use some code that uses it. And even then, it might not ever be called given the way you are reusing the code, so...a dynamic check is the best way to ensure it never actually gets used.
Re:Why I Write Games in C (yes, C) by loeg on Saturday September 21, 2019 @11:21PM (#21039440)
> Because even if you don't use it directly, you might use some code that uses it.

It seems extremely unlikely that any general purpose code you might adopt, which happens to invoke malloc() at all, would be fit for purpose in such a restricted environment without substantial modification; in which case you would just remove the malloc() calls as well.

> And even then, it might not ever be called given the way you are reusing the code, so...a dynamic check is the best way to ensure it never actually gets used.

In such a restricted environment, it is unlikely you just have unknown deadcode in your project. "Oh, those parts that call malloc()? They're probably not live code and we'll find out via a crash at runtime." That's like the opposite of what you want in a hard realtime system.

So, no — a static, compile/link time check is a strictly superior way to ensure it never gets used.

Re:Why I Write Games in C (yes, C) by lonelappde on Sunday September 22, 2019 @03:40AM (#21040151)
Spoken like someone who never had a 3rd party binary blob as a critical dependency.
Re:Why I Write Games in C (yes, C) by jorams on Sunday September 22, 2019 @04:15AM (#21040260)
If you need your system to never dynamically allocate memory, an opaque 3rd party binary blob which might still do that doesn't seem like a valid inclusion.
Re:Why I Write Games in C (yes, C) by loeg on Sunday September 22, 2019 @07:44PM (#21044739)
Again, we're talking about a very restricted hard realtime environment. How do you trust, let alone qualify, a 3rd party binary blob that you know calls malloc(), which is a direct violation of your own design requirements?
Re:Why I Write Games in C (yes, C) by alxlaz on Sunday September 22, 2019 @05:25AM (#21040438)
Not OP but the most common reason I've seen is that this was an additional restriction that they imposed for their project. The standard library includes a perfectly working malloc. You override it so that you can't end up calling it accidentally, either explicitly (i.e. your brain farts and you do end up malloc()-ing something) or implicitly (i.e by calling a function that ends up calling malloc down the line). The latter is what happens, and surprisingly easy and often. Not all libraries have the kind of documentation that glibc has, nor can you always see the code.
Re:Why I Write Games in C (yes, C) by the_mitsuhiko on Saturday September 21, 2019 @10:11PM (#21039209)
Some things are okay to allocate at some point in time. So you can disable malloc later when you no longer want it to provide memory.
Re:Why I Write Games in C (yes, C) by loeg on Saturday September 21, 2019 @11:21PM (#21039443)
That's plausible, but not how OP defined it:

> a system where malloc() was forbidden. In fact it always returned null.

Re:Why I Write Games in C (yes, C) by drblast on Saturday September 21, 2019 @08:14PM (#21038775)
So you crash immediately if someone tries to use it or a library gets added that does?

Never experienced the malloc() thing but do throw exceptions and fail fast under conditions like these so they're caught in testing.

Re:Why I Write Games in C (yes, C) by a_t48 on Saturday September 21, 2019 @09:34PM (#21039076)
I think what he's asking to do is to make it a linker failure by not adding it to the standard lib at all. Compile fails are nicer than runtime fails.
Re:Why I Write Games in C (yes, C) by loeg on Saturday September 21, 2019 @11:23PM (#21039450)
Like sibling commenter says, a compile/link error is even better and faster than finding out at runtime with a crash.
Re:Why I Write Games in C (yes, C) by aetherspawn on Saturday September 21, 2019 @07:26PM (#21038563)
I’d be interested in knowing more if you care to write more about this. I’m currently costing/scoping out what’s required in writing software at that level but don’t really have some real industry insight into what other people do

Static memory is something we already do but we’re pretty interested whether industry actually adopts redundant code paths, monitors, to what extent watchdogs (how many cycles can be missed?), etc.

Re:Why I Write Games in C (yes, C) by rramadass on Saturday September 21, 2019 @11:43PM (#21039532)
I suggest you take a look at the books by Michael Pont and the offerings through his company "SafeTTy Systems". His/Company's work deals with hard real-time systems with all the associated standards.
Re:Why I Write Games in C (yes, C) by drudru11 on Saturday September 21, 2019 @05:46PM (#21038103)
Wow - disabling the caches is an extreme measure. I get it though. After doing that, you can (probably) do cycle counting on routines again. Just like the 80s or earlier.
Re:Why I Write Games in C (yes, C) by kabdib on Sunday September 22, 2019 @04:45AM (#21040337)
There's probably enough jitter in the memory system and in instruction parallelism that accurate cycle counting will still be challenging.

Also, you probably want some padding so that newer versions of the CPU can be used without too much worry. It's possible for cycle counts of some routines to increase, depending on how new chips implement things under the hood.

[says a guy who was counting cycles, in the 1980s :-)]

Re:Why I Write Games in C (yes, C) by viraptor on Saturday September 21, 2019 @08:37PM (#21038854)
I don't. Maybe we're thinking about different kind of caches, but if these are transparent, no-performance-impact caches, then why wouldn't you prove the system works well with caches off (guarantee deadlines are met), then enable caches for opportunistic power gains?
Re:Why I Write Games in C (yes, C) by notacoward on Saturday September 21, 2019 @10:25PM (#21039249)
> if these are transparent, no-performance-impact caches

If there were no performance impact, there would be no point. I'm not just being snarky; there's an important point here. Caches exist to have a performance impact. In many domains it's OK to think about caching as a normal case, and to consider cache hit ratio during designs. When you say "no performance impact" you mean no negative performance impact, and that might be technically true (or it might not), but...

But that's not how a hard real-time system is designed. In that world, uncached has to be treated as the normal case. Zero cache hit ratio, all the time. That's what you have to design against, even counting cycles and so on if you need to. If you're designing and testing a system to do X work in Y time every time under worst-case assumptions, then any positive impact of caches doesn't buy you anything. Does completing a task before deadline because of caching allow you to do more work? No, because it's generally considered preferable to keep those idle cycles as a buffer against unforeseen conditions (or future system changes) than try to use them. Anything optional should have been passed off to the non-realtime parts of the larger system anyway. There should be nothing to fill that space. If that means the system was overbuilt, so be it.

The only thing caches can do in such a system is mask the rare cases where a code path really is over budget, so it slips through testing and then misses a deadline in a live system where the data-dependent cache behavior is less favorable. Oops. That's a good way for your product and your company to lose trust in that market. Once you're designing for predictability in the worst case, it's actually safer for the worst case to be the every-time case.

It's really different than how most people in other domains think about performance, I know, but within context it actually makes a lot of sense. I for one am glad that computing's not all the same, that there are little pockets of exotic or arcane practice like this, and kudos to all those brave enough to work in them.

Re:Why I Write Games in C (yes, C) by nightcracker on Sunday September 22, 2019 @04:57AM (#21040368)
While you might test your hard real-time requirements with caches disabled, there's still reason to run the code with caches afterwards.

E.g. errors that didn't match a branch or input scenario during testing which would go over budget without cache, but with cache might prevent a crash.

Another could be power consumption, latency optimization, or improvement of accuracy. E.g. some signal analysis doesn't work at all if the real-time code is above some required Nyquist threshold, but faster performance improves the maximum frequency that can be handled, improving accuracy.

Re:Why I Write Games in C (yes, C) by notacoward on Sunday September 22, 2019 @08:07AM (#21040967)
You could be right on some of those. That didn't seem to be the prevailing attitude when I worked in that area, but as I said that was a long time ago - and it was in only a few specific sub-domains as well.
Re:Why I Write Games in C (yes, C) by notacoward on Sunday September 22, 2019 @11:25AM (#21041775)
Forgot to mention: cache misses can be more expensive than uncached accesses, so testing with caches off and then turning them on in production can be a disaster if you hit a cache-busting access pattern. Always run what you tested.
Re:Why I Write Games in C (yes, C) by Baeocystin on Saturday September 21, 2019 @09:16PM (#21039010)
Because you lose deterministic behavior, and there are cases where that is non-negotiable, regardless of performance cost.
Re:Why I Write Games in C (yes, C) by piaste on Saturday September 21, 2019 @08:56PM (#21038933)
Who will provide a guarantee that the caches are truly transparent and will not trigger any new bugs?

Essentially, you would need to prove the statement "if a system works well with caches off, then it works well with caches on" to the satisfaction of whatever authority is giving you such stringent requirements.

Re:Why I Write Games in C (yes, C) by squeaky-clean on Saturday September 21, 2019 @09:10PM (#21038985)
You'd have to very solidly prove that in the wors-case a cache only ever make execution time equal to or faster than a processor not using cache and never causes anything to be slower.
Re:Why I Write Games in C (yes, C) by darnir on Tuesday September 24, 2019 @01:22AM (#21056821)
Even that is not enough. The cache may make everything faster, but it could lead to higher contention on a different physical resource slowing things down there. The cache cannot be guaranteed to prevent that.
Re:Why I Write Games in C (yes, C) by PorterDuff on Saturday September 21, 2019 @07:58PM (#21038701)
""soft" real-time (e.g. games, or audio/video) "

Hah. Who sez that audio and video products have 'soft' real time? Go on now.

Re:Why I Write Games in C (yes, C) by notacoward on Saturday September 21, 2019 @09:58PM (#21039156)
I didn't make up the terminology. It was already in common use at least thirty years ago when I was actively working in that domain. To simplify, it's basically about whether a missed deadline is considered fatal or recoverable. That difference leads to very different design choices. Perhaps some kinds of video software is hard real-time by that definition, but for sure a lot of it isn't. I'd apologize, but it was never meant to be pejorative in the first place and being on one side of the line is cause for neither pride nor shame. They're just different kinds of systems.
Re:Why I Write Games in C (yes, C) by detaro on Saturday September 21, 2019 @08:05PM (#21038741)
What percentage of the market is A/V build to actual hard real-time standards, and not expected to run on devices that can't provide it (so no PCs with normal OSes, no smartphones)? For the vast majority, soft real-time is fine, since an occasional deadline-miss results in minor inconvenience, not property damage, injury or death.

I assume some dedicated devices are more or less hard real time, due to running way simpler software stacks on dedicated hardware.

Re:Why I Write Games in C (yes, C) by PorterDuff on Saturday September 21, 2019 @08:27PM (#21038819)
I take it that scarcely anyone here has written software for video switchers, routers, DVEs, linear editors, audio mixers, glue products, master control systems, character generators, etc. etc. Missing a RT schedule rarely results in death, but you'd think so given the attitude from the customer. That's a silly definition for it.

There's a whole world out there of hard real time, the world is not simply made up of streaming video and cell phones.

The cool thing on HN is you can get down voted for simply making that observation. It's a sign of the times I'm afraid.

Re:Why I Write Games in C (yes, C) by AnimalMuppet on Sunday September 22, 2019 @12:12AM (#21039621)
I actually have written software for video routers and character generators. We didn't consider them hard real time, though I wouldn't claim that such was standard industry usage.

For example, if you're doing a take, you have to complete it during the blanking interval, but usually the hardware guarantees that. In the software, you want you take to happen in one particular vertical blanking interval (and yes, it really is a frame-accurate industry). But if you miss, you're only going to miss by one. We didn't (so far as I know) specify guarantees to the customer ("If you get your command to the router X ms before the vertical interval, your take will happen in that vertical"), so we could always claim that the customer didn't get the command to the router in time. Again, so far as I know - there may have been guarantees given to the customer, but I didn't know about them.

But that was 20 years ago, back in the NTSC 525 days.

Nice name, by the way. Do you know of any video cards that will do a true Porter & Duff composite these days? I recall looking (again, 20 years ago) at off-the-shelf video cards, and while they could do an alpha composite, it wasn't right (and therefore wasn't useful to us).

Re:Why I Write Games in C (yes, C) by ajanuary on Sunday September 22, 2019 @02:12AM (#21039941)
I currently work on software controlling the hardware like video routers, and this is definitely my experience. It’s all very much soft real-time.

In terms of customers and how much they care, the North American market seems to care less than Europe.

Re:Why I Write Games in C (yes, C) by jcelerier on Sunday September 22, 2019 @02:32AM (#21039986)
I work on an open-source music sequencer (https://ossia.io) and no later than two days ago I had a fair amount of mails with someone who wanted to know the best settings for his machine to not have any clicks during the show (which are the audio symptoms of "missed deadline"). I've met some users who did not care, but the overwhelming majority does, even for a single click in a 1-hour long concert.
Re:Why I Write Games in C (yes, C) by littlestymaar on Sunday September 22, 2019 @01:58PM (#21042800)
If it's running in a consumer OS (not a RT one) and it counts on having enough CPU available to avoid missing the deadline, that's exactly what soft-realtime is.

Compare your “not a single click in an hour [for quality reason]” to a “not a single missed deadline in 30 years of the life expectancy of a plane, on a fleet of a few thousands planes [for safety reasons]”. That's the difference of requirements between hard and soft RT.

I did some soft real-time (video decoding) and I have a friend working on hard real-time (avionics) and we clearly didn't worked in the same world.

Re:Why I Write Games in C (yes, C) by AnimalMuppet on Sunday September 22, 2019 @02:59PM (#21043178)
Yeah. To me, hard real time is when you count cycle (or have a tool that does it for you), to guarantee that you make your timing requirements. We never did that.
Re:Why I Write Games in C (yes, C) by fastball on Saturday September 21, 2019 @08:47PM (#21038889)
You just agreed with the OC.

RT video/audio failing never results in death. Where as failures in "avionics, industrial control" absolutely can / do. That seems to be where OC was drawing the line.

Re:Why I Write Games in C (yes, C) by detaro on Saturday September 21, 2019 @08:59PM (#21038940)
Seems to be a common distinction, although GP is right with the addition that the production side of things is more demanding (and at least would suffer financial damage if problems occur to often) than the playback side formed by random consumer gear, and has some, especially low-level/synchronization-related, gear to hard standards. But often soft is enough, as long as it's reliable enough on average.
Re:Why I Write Games in C (yes, C) by coldtea on Saturday September 21, 2019 @08:44PM (#21038876)
>For the vast majority, soft real-time is fine, since an occasional deadline-miss results in minor inconvenience, not property damage, injury or death.

A "minor inconvenience" like a recording session going wrong, a live show with stuttering audio, skipped frames in a live TV show, and so on?

Re:Why I Write Games in C (yes, C) by squeaky-clean on Saturday September 21, 2019 @09:33PM (#21039068)
Most professional recording studios are using consumer computer hardware that can't do hard realtime with software that doesn't support hard realtime.

People like deadmau5, Daft Punk, Lady Gaga all perform with Ableton Live and a laptop or desktop behind their rig. If it were anything more than a minor inconvenience, these people wouldn't use this.

It's very unlikely to have audio drop outs, a proper setup will basically never have them. But still if you have one audio dropout in your life, you're not dead, your audience isn't dead, a fire doesn't start, a medical device doesn't fail to pump, and so on.

And yes you can badly configure and system, but the point is you can't configure these to be 100% guaranteed, 99.99% is perfectly fine.

Edit: Sometimes people call these "firm" realtime systems. Implying the deadline cannot be missed for it to operate, but also that failure to meet deadlines doesn't result in something serious like death (e.g in a video game you can display frames slower than realtime and it kind of works but feels laggy, however you cannot also slow down the audio processing because you'll a lowered pitch, so you have to drop the audio.)

Re:Why I Write Games in C (yes, C) by detaro on Saturday September 21, 2019 @09:20PM (#21039022)
As long as the individual event happens seldom enough few of these actually are a big problem. Soft real-time being allowed to blow deadlines doesn't mean it can't be expected to have a very high rate of success (at least that's the definitions I've learned), and clearly a sufficiently low rate of failure is tolerated. There's a vast difference between "there's an audio stutter every day/week/month/..." and "noticeably stuttering audio". The production side is obviously a lot more sensitive about this than playback, but will still run parts e.g. on relatively normal desktop systems because the failure rate is low enough.
Re:Why I Write Games in C (yes, C) by tripzilch on Monday September 23, 2019 @04:29AM (#21046964)
The production side usually renders the final audio mix off-line, so no real-time requirements there for getting optimum sound quality. I'd say the occasional rare pop or stutter is worse to have during a live performance than when mixing and producing music.