
In the previous post we cleaned out the registry of the qmaster, the old government office at the heart of the cluster scheduler. The clerks stopped photocopying blank forms and stopped reading excerpts from top to bottom, and the scheduling department now works nearly twice as fast. One thing didn’t change, though: how the forms are filled in.
The registry’s forms have numbered lines, and a clerk who wants to write something down asks for “line 7 of a job form, as a number”. If line 7 isn’t a number, or the form in front of him isn’t a job form at all, nobody stops him while he writes. The mistake shows when the line is actually filled in: the office stops everything, calls for help and closes for the day. In software terms, the process aborts.
This post is about the replacement: TOM, the Typed Object Model. It turns that “closes for the day” into “the form doesn’t fit into the typewriter in the first place”. And it’s a nice example of where the evolution of C++ actually pays off.
How the old registry works
In CULL, every object — a job, a queue, a host — is an element with numbered fields. Reading a field looks like this:
u_long32 number = lGetUlong(job, JB_job_number);
const char *name = lGetString(job, JB_job_name);
JB_job_number is just an integer. The function name says which type the caller expects, and the library checks at run time whether the field really has that type and whether the element really has that field. All of the following compile without a single warning:
const char *oops1 = lGetString(job, JB_job_number); // a number read as a string
u_long32 oops2 = lGetUlong(queue, JB_job_number); // a job field on a queue
Both abort the process the moment they run. If that line sits in a rarely used error path, “the moment it runs” may be months after the release, on a customer’s cluster.
The library has worked like this for three decades, and with tens of thousands of field accesses in the code base, it has held up remarkably well. But it relies on discipline and on tests that happen to run the right line. The compiler, the one reviewer that reads every line every time, had no way to help.

How TOM works
With TOM, the same reads look like this:
ocs::tom::Element<ocs::tom::JB> job{raw};
std::uint32_t number = job.get<JB_job_number>();
const char *name = job.get<JB_job_name>();
Three things changed:
- The element knows its type.
Element<JB>is a job, and the compiler knows it. - The field decides the value type. There is one
get, not one per type. What it returns follows from the field, so asking for “line 7 as a string” isn’t something you can even write. - A field of another type doesn’t fit. Each object type has its own set of fields, and the handle only accepts its own.
The two mistakes from above now stop the build instead of the cluster:
ocs::tom::Element<ocs::tom::QU> queue{raw};
queue.get<JB_job_name>(); // error: constraints not satisfied
and where a plain “no match” would leave you guessing, TOM spells it out:
error: static assertion failed: the field does not belong to the object type of the handle
That’s the shape sorter: the square block simply doesn’t go through the round hole, and you find out while you’re holding it, not after the toy has been packed and shipped.
TOM goes further than single fields. Lists know what they hold, lookups know the type of their key, sorting and selections know which fields they may use. The next post (coming on November 30) walks through those with old-and-new examples.
Why this wasn’t on the table in 1999
You could argue that C++ templates existed in 1998 and that all of this could have been done back then. In theory, yes. Andrei Alexandrescu showed in Modern C++ Design (2001) how far template metaprogramming can be pushed.
In practice, two things stood in the way. The first was the platforms. Grid Engine ran on practically every Unix of its day, from Solaris, AIX, HP-UX and IRIX to Cray, NEC SX and the early Linux distributions, and not all of them had a usable C++ compiler. On some, a plain C compiler was all there was, and where C++ compilers existed, they disagreed on what templates even meant. A data layer for all of them had to be written in C. The second was the code itself: even where it would have compiled, it would have meant unreadable code, error messages several screens long, and compile times nobody would accept.
Today the picture is reversed. Every platform the scheduler builds on has a current GCC or Clang, and they agree on the language down to the details. That common ground is what makes TOM possible at all.
What changed is that C++ grew features that turn compile-time decisions into ordinary code:
C++ archaeology
Feature Standard What it gives TOM constexprC++11 functions the compiler can evaluate static_assertC++11 a readable message instead of a template error wall if constexprC++17 code paths decided per field type at compile time template <auto F>C++17 the field itself as a template argument: get<JB_job_number>()concepts, requiresC++20 “this field belongs to this type” as a named, checkable rule constevalC++20 tables that must be computed by the compiler The code base builds as C++23.
A small, self-contained example shows the idea. It’s not TOM’s code, just the language features at work:
// Every object type gets its own enumeration of fields.
enum class JobField { number, name };
enum class QueueField { name, slots };
// Which C++ type each field holds -- written once, next to the definition.
template <auto F> struct ValueOf;
template <> struct ValueOf<JobField::number> { using type = std::uint32_t; };
template <> struct ValueOf<JobField::name> { using type = std::string; };
template <> struct ValueOf<QueueField::slots> { using type = std::uint32_t; };
// A field belongs to a type if its enumeration is that type's enumeration.
template <auto F, typename FieldEnum>
concept FieldOf = std::same_as<decltype(F), FieldEnum>;
struct Job {
template <auto F> requires FieldOf<F, JobField>
typename ValueOf<F>::type get() const;
};
job.get<JobField::name>(); // fine, returns std::string
job.get<QueueField::slots>(); // error: no matching function ... evaluated to 'false'
And consteval lets the compiler check whole tables before anything runs:
constexpr std::array<std::string_view, 2> job_field_names{"number", "name"};
consteval bool all_unique(auto const &names) {
for (std::size_t i = 0; i < names.size(); ++i)
for (std::size_t j = i + 1; j < names.size(); ++j)
if (names[i] == names[j]) return false;
return true;
}
static_assert(all_unique(job_field_names), "two job fields share a name");
A plain loop, written like any other function, run by the compiler. In 1999 the same check would have been a page of recursive templates, or a unit test.
TOM is far from the first project to move errors from run time to compile time this way. std::format and the {fmt} library check format strings while compiling, sqlpp11 refuses a column that belongs to another table, Protocol Buffers generates typed accessors from a schema, and mp-units turns “metres plus seconds” into a compile error. TOM does the same for the objects of a cluster scheduler.
What it costs
None of this is free, and a post about where modern C++ pays off should say where it costs. The numbers compare 9.1 with 9.2.0alpha1; until the first customer shipment of 9.2 they can still change.
- At run time: mostly less. A typed field read compiles to a handful of loads, with no function call on the normal path. A micro-benchmark runs the same list operations against both versions, about 130 of them: in the geometric mean, 9.2.0alpha1 is twice as fast as 9.1. Reading or writing a plain field is about three times as fast. Sorting is the nicest case for this post: the typed
sort<asc<F>, desc<G>>()compares with code the compiler generates for exactly those fields, and is about 2.5× faster than the same sort with the sort order built at run time. - Not everywhere. Looking something up in a list of fewer than 16 elements is up to twice as slow as in 9.1, which built a hash table for every list, however short; 9.2 searches short lists linearly and saves the memory. For host names and inside the scheduler the threshold is lower. Packing a job for the network is about 20–35 % slower. Both are on our list before the release.
- At compile time: a bit. After the first big conversion step, building the qmaster took about 5 % longer.
- In code size: some. Every place that can fail keeps a small cold path for the error, which adds up over thousands of call sites. The qmaster binary has about 27 % more machine code than before the conversion (6.7 MB → 8.6 MB).
- In compilers: a minimum. The oldest compiler on the build hosts sets the limit. TOM builds with GCC 13 to 15 and Clang 17 to 19 across the supported platforms.
We decided early that type safety outweighs size and compile time. The numbers above didn’t make us regret it.
Every line, not just the ones that run
Tests are good at the paths a cluster takes every day. Grid Engine has decades of them, and they are why the old library held up so well. But a test only checks the lines it runs. Code for rare combinations of features is, by nature, the code that runs least.
The conversion asked one question of every field access in the code base, run or not: “which type is this element, and does it have this field?” Almost everywhere the answer was yes, which says a lot about the people who wrote the code over thirty years. In a handful of places it wasn’t. The most interesting one sits in the support for array job dependencies (-hold_jid_ad): a check that only comes into play when an array job with a tightly integrated parallel environment finishes and another job waits on it. In that branch, a list of parallel tasks was read as if it held jobs. The combination is rare enough that it never turned up in practice, and no test exercised it. The type check found it anyway, because it reads every line, and the branch was simplified before anyone could ever run into it.
A few other places wrote a value that the old library quietly ignored, for example on a partial copy of an object that doesn’t carry that field. They did no harm, and they are gone now, too. In 9.2, such a write is an error instead of a silent no-op.
That’s the real gain of a strict reviewer: not that it finds many mistakes, but that it looks at the lines nobody else gets to.
The migration in numbers
TOM doesn’t sit next to CULL. It replaces it. That meant touching every place in the code base that uses the old interface:
- About 17,600 calls into the old library, from plain field reads to lookups, selections and sorting, outside the library itself.
- About 9,600 of them, the plain field reads and writes, were converted mechanically by a rewriting tool built on Clang. Lists, lookups, ownership and the rest went through a mix of further tool rules and work by hand, module by module.
- 270 commits, each step checked with full testsuite runs of about 800 test units, roughly three hours each, about twenty of them over the course of the conversion.
- At the end the old library was deleted. There is no CULL left in 9.2.
Next: working with TOM
Single fields are the easy part. Most of the code in a scheduler walks lists, looks things up, picks out a few fields and sorts the result. In the next post (coming on November 30) we’ll go through these function groups one by one, each with the old CULL call next to the TOM version, so you can see what changes when you write code against TOM.
The source code is in the clusterscheduler repository on GitHub, in source/libs/tom.