
In the last post the forms of our old government office got a makeover: with TOM, a field that doesn’t belong on a form, or a value of the wrong kind, no longer fits into the typewriter. That’s single fields. But clerks rarely handle one form at a time. They pull folders, look for one case among thousands, copy out a few lines and put the pile in order.
This post goes through those everyday jobs one by one. Each section shows the old CULL call and the TOM version side by side, plus a note on which C++ standard makes it possible. If you write code for the cluster scheduler, this is the part you’ll use every day.
Handles: knowing what you hold, and who owns it
Everything in TOM goes through handles. A handle wraps an element or a list and knows its type. (All names in this post live in the namespace ocs::tom; the examples leave the prefix out.)
Element<JB>andList<JB>are views: they point to a job or a list of jobs, and they don’t own it.ConstElement<JB>andConstList<JB>are the read-only versions.ElementPtr<JB>andListPtr<JB>own what they hold and free it when they go out of scope, likestd::unique_ptr.
That last point quietly removes a whole class of bugs. In CULL, whoever creates a list has to remember to free it on every path out of the function, including the early returns. With an owning handle, the compiler does the remembering.
Needs: move semantics and
std::unique_ptr-style ownership (C++11).
Walking a list
// CULL
const lListElem *job;
for_each_ep(job, job_list) {
...
}
// TOM
for (const auto job : jobs) {
...
}
A plain range-based for, and job is a typed handle, not an anonymous pointer. Sub-lists work the same way, and they know their type too:
for (const auto task : job.get<JB_ja_tasks>()) {
// task is an array task, not "some element"
}
If you’ve ever got tangled up in nested loops, I’ve written about the other kind of loops too.
Needs: range-based
for(C++11).
Looking something up
// CULL
const lListElem *job = lGetElemUlong(job_list, JB_job_number, 4711);
const lListElem *host = lGetElemHost(host_list, EH_name, "node-1");
// TOM
const auto job = jobs.find_first<JB_job_number>(4711u);
const auto host = hosts.find_first<EH_name>(Host{"node-1"});
The key has to match the field: a job number is a number, a host name is a Host. In CULL, lGetElemUlong(list, SOME_STRING_FIELD, 5) compiled and failed at run time. In TOM it doesn’t compile.
Finding all matches used to need an iterator variable and a pair of functions:
// CULL
const void *iterator = nullptr;
for (const lListElem *job = lGetElemStrFirst(job_list, JB_owner, "alice", &iterator);
job != nullptr;
job = lGetElemStrNext(job_list, JB_owner, "alice", &iterator)) {
...
}
// TOM
for (const auto job : jobs.find_all<JB_owner>("alice")) {
...
}
Lookups in a sub-list name the list and the key field, and both are checked:
// CULL
const lListElem *task = lGetSubUlong(job, JAT_task_number, 2, JB_ja_tasks);
// TOM
const auto task = job.find_first_in<JB_ja_tasks, JAT_task_number>(2u);
And here the compiler catches a mistake that’s easy to make and hard to spot. The environment list of a job holds variables, not array tasks. In CULL, lGetSubUlong(job, JAT_task_number, 1, JB_env_list) compiled. In TOM, asking the environment list for a task number is a compile error.
Needs:
template <auto F>(C++17) for the field as a template argument, concepts (C++20) for “this key fits this field”.
Adding and removing by key
// CULL
lAddSubStr(job, VA_variable, "DISPLAY", JB_env_list, VA_Type);
lDelSubStr(job, VA_variable, "DISPLAY", JB_env_list);
// TOM
job.emplace_back_in<JB_env_list, VA_variable>("DISPLAY"); // creates the list if needed
job.erase_in<JB_env_list, VA_variable>("DISPLAY"); // frees the list once it is empty
The names follow the standard library on purpose: emplace_back, erase, push_back, remove_if. If you know std::list, you already know most of TOM.
Needs: concepts (C++20) to check that the key field belongs to the element type of the list.
Picking out fields: selections
Departments rarely need a whole case file. CULL describes which fields to keep with a format string, parsed at run time:
// CULL
lEnumeration *what = lWhat("%T(%I%I)", US_Type, US_name, US_type);
// TOM
auto what = what<US_name, US_type>();
Nested selections, a few fields of the set plus a few fields of each entry in it, used to be a puzzle of %T, %I and ->:
// CULL
lWhat("%T(%I%I->%T(%I))", US_Type, US_name, US_entries, UE_Type, UE_name);
// TOM
what<US_name>().with<US_entries>(what<UE_name>());
Every field is checked against its type while compiling. A field of another type, or a nested selection on a field that isn’t a list, doesn’t compile.
Conditions
Conditions are where the old format strings got really creative:
// CULL
lCondition *where = lWhere("%T(%I == %s)", US_Type, US_name, "x");
// TOM
auto where = field<US_name>() == "x";
TOM conditions are ordinary C++ expressions. They combine with &&, || and !, and string fields offer like() for shell patterns and equals_nocase():
auto where = field<JB_owner>() == "alice" && field<JB_job_number>() > 1000u;
auto pattern = field<JB_job_name>().like("sim_*");
Comparing a number field with a string is a compile error, not a condition that silently never matches. With a condition in hand, the list does the rest:
auto first = jobs.find_if(where); // the first match
auto mine = jobs.select(where); // a new, owned list of the matches
auto removed = jobs.remove_if(where); // remove and free the matches
auto reduced = jobs.select(where, what); // matches, with only the selected fields
Needs: operator overloading on expression objects (C++98, honestly), made checkable by concepts (C++20).
Sorting
// CULL
lPSortList(job_list, "%I-%I+", JB_priority, JB_job_number);
// TOM
jobs.sort<desc<JB_priority>, asc<JB_job_number>>();
Descending by priority, then ascending by job number. The old version encodes the direction as a - or + in a format string. The new one names it, and a field of another type doesn’t compile. Both sort stably: elements with equal keys keep their order. For a sort order that has to be built at run time, there’s sort_order<...>() and insert_sorted() for adding into a list that’s already in order.
Needs: variadic templates (C++11) for any number of sort keys,
template <auto F>(C++17).
Errors you’ll still see, and where
Not everything can be decided at compile time. Some fields are only known at run time, for example when a request names them. For those, TOM keeps a run-time form, get<V>(nm) with the field name as an argument, and reports a misuse with an exception instead of aborting the process. The rule of thumb: if you can name the field in the code, the compiler checks it. If you can’t, TOM checks it at run time and tells you clearly what went wrong.
The second post tells why that check is worth more than it looks: it reads every line, including the ones no test and no cluster ever runs.
Cheat sheet: CULL to TOM
| Task | CULL | TOM |
|---|---|---|
| Read a field | lGetUlong(job, JB_job_number) | job.get<JB_job_number>() |
| Write a field | lSetString(job, JB_job_name, "x") | job.set<JB_job_name>("x") |
| Walk a list | for_each_ep(job, job_list) | for (const auto job : jobs) |
| Find by key | lGetElemUlong(job_list, JB_job_number, n) | jobs.find_first<JB_job_number>(n) |
| Find all by key | lGetElemStrFirst() / lGetElemStrNext() with an iterator | jobs.find_all<JB_owner>("alice") |
| Find in a sub-list | lGetSubUlong(job, JAT_task_number, n, JB_ja_tasks) | job.find_first_in<JB_ja_tasks, JAT_task_number>(n) |
| Add by key | lAddSubStr(job, VA_variable, "X", JB_env_list, VA_Type) | job.emplace_back_in<JB_env_list, VA_variable>("X") |
| Delete by key | lDelSubStr(job, VA_variable, "X", JB_env_list) | job.erase_in<JB_env_list, VA_variable>("X") |
| Select fields | lWhat("%T(%I%I)", US_Type, US_name, US_type) | what<US_name, US_type>() |
| Build a condition | lWhere("%T(%I == %s)", US_Type, US_name, "x") | field<US_name>() == "x" |
| First match | lFindFirstRW(lp, where) | list.find_if(where) |
| Matches into a new list | lSelect("name", lp, where, what) | list.select(where, what) |
| Keep only the matches | lSelectDestroy(lp, where) | list.remove_if(!std::move(where)) |
| Remove the matches | — (no direct form) | list.remove_if(where) |
| Sort | lPSortList(job_list, "%I-%I+", JB_priority, JB_job_number) | jobs.sort<desc<JB_priority>, asc<JB_job_number>>() |
| Own and free a list | lFreeList(&lp) on every path out | ListPtr<JB>, freed automatically |
Wrapping up the series
Three posts, one old office:
- Cleaning out the registry made the qmaster faster: scheduling nearly twice as fast as in 9.1 and up to 41 % less memory, without changing a single interface.
- Forms that can’t be filled in wrong moved a whole class of errors from the cluster to the compiler, and showed why that only became practical with modern C++.
- This post showed what that looks like in everyday code.
If you want to dig deeper, the source is in the clusterscheduler repository on GitHub, in source/libs/tom. And if you have to write or convert code against TOM yourself, the fourth post (coming on December 3) is the long reference: every CULL function, its TOM counterpart and a real data path followed from start to end.