Skip to content

eBPF-based security platform

tools/xtop: report the by-image tree of the member furthest behind

John Fastabendeb60374a504e
diff --git a/tools/xtop/src/components/NodesView.tsx b/tools/xtop/src/components/NodesView.tsx
index a0fab65e..d91d1cf5 100644
--- a/tools/xtop/src/components/NodesView.tsx
+++ b/tools/xtop/src/components/NodesView.tsx
@@ -72,15 +72,10 @@ function ExposedCell({ exposed, unknown }: { exposed: number; unknown: number })
);
}
-function TreeCell({ last, head, atLeast = false }: { last: number | null; head: number | null; atLeast?: boolean }) {
+function TreeCell({ last, head }: { last: number | null; head: number | null }) {
if (last === null) return <Text dimColor>no tree</Text>;
if (head === null) return <Text dimColor>#{last}</Text>;
- if (last < head)
- return (
- <Text color="yellow">
- {atLeast ? "≥" : ""}#{last}/#{head}
- </Text>
- );
+ if (last < head) return <Text color="yellow">#{last}/#{head}</Text>;
return <Text color="green">✓ #{head}</Text>;
}
@@ -220,8 +215,8 @@ const IMAGE_COLUMNS: readonly Column<ImageGroup>[] = [
title: "TREE",
width: 11,
defaultDir: "asc",
- sortKey: (g) => (g.tree.minLastSeq === null ? -1 : g.tree.minLastSeq - (g.tree.headSeq ?? g.tree.minLastSeq)),
- render: (g) => <TreeCell last={g.tree.minLastSeq} head={g.tree.headSeq} atLeast />,
+ sortKey: (g) => (g.tree.lastSeq === null ? -1 : g.tree.lastSeq - (g.tree.headSeq ?? g.tree.lastSeq)),
+ render: (g) => <TreeCell last={g.tree.lastSeq} head={g.tree.headSeq} />,
},
{ id: "shields", title: "SHIELDS", width: 12, defaultDir: "desc", sortKey: (g) => shieldsKey(g.shields), render: (g) => <ShieldsCell s={g.shields} /> },
{
diff --git a/tools/xtop/src/lib/fleet.ts b/tools/xtop/src/lib/fleet.ts
index 75aef033..c75d89cd 100644
--- a/tools/xtop/src/lib/fleet.ts
+++ b/tools/xtop/src/lib/fleet.ts
@@ -93,7 +93,8 @@ export type ImageGroup = {
health: number;
/** Shared by every member, null when members differ. */
kernel: string | null;
- tree: { minLastSeq: number | null; headSeq: number | null };
+ /** The tree position of the member furthest behind its own tree. */
+ tree: { lastSeq: number | null; headSeq: number | null };
shields: Shields;
/** Exposed findings of the worst member. */
exposed: number;
@@ -127,14 +128,20 @@ export function healthPercent(nodes: readonly Pick<FleetNode, "status">[]): numb
return Math.round((100 * nodes.filter((n) => n.status === "healthy").length) / nodes.length);
}
-const minOrNull = (xs: (number | null)[]) => {
- const v = xs.filter((x): x is number => x !== null);
- return v.length ? Math.min(...v) : null;
-};
-const maxOrNull = (xs: (number | null)[]) => {
- const v = xs.filter((x): x is number => x !== null);
- return v.length ? Math.max(...v) : null;
-};
+/** head − last, 0 for a member that reports no head: how far behind its own tree a member is. */
+const lagOf = (m: FleetNode) => (m.tree.headSeq === null ? 0 : m.tree.headSeq - m.tree.lastSeq!);
+
+/**
+ * The tree position of the member furthest behind its own tree, all-null
+ * when none follows one. Members of an image can follow independent
+ * trees, so their positions never mix into one pair.
+ */
+function behindTree(members: readonly FleetNode[]): ImageGroup["tree"] {
+ const worst = members
+ .filter((m) => m.tree.lastSeq !== null)
+ .sort((a, b) => lagOf(b) - lagOf(a) || a.tree.lastSeq! - b.tree.lastSeq!)[0];
+ return { lastSeq: worst?.tree.lastSeq ?? null, headSeq: worst?.tree.headSeq ?? null };
+}
export function groupByImage(nodes: readonly FleetNode[]): ImageGroup[] {
const byImage = new Map<string, FleetNode[]>();
@@ -159,10 +166,7 @@ export function groupByImage(nodes: readonly FleetNode[]): ImageGroup[] {
),
health: healthPercent(members) ?? 0,
kernel: members.every((m) => m.kernel === first.kernel) ? first.kernel : null,
- tree: {
- minLastSeq: minOrNull(members.map((m) => m.tree.lastSeq)),
- headSeq: maxOrNull(members.map((m) => m.tree.headSeq)),
- },
+ tree: behindTree(members),
shields: { applied: sum("applied"), pending: sum("pending"), idle: sum("idle") },
exposed: Math.max(...members.map((m) => m.exposed)),
unknown: members.reduce((t, m) => t + m.unknown, 0),
diff --git a/tools/xtop/test/fleet.test.ts b/tools/xtop/test/fleet.test.ts
index 619846da..e6a748a9 100644
--- a/tools/xtop/test/fleet.test.ts
+++ b/tools/xtop/test/fleet.test.ts
@@ -211,10 +211,10 @@ describe("groupByImage", () => {
test("rolls up kernel, tree, shields and exposure", () => {
expect(fleetOs.kernel).toBe("7.4.2");
- expect(fleetOs.tree).toEqual({ minLastSeq: 2, headSeq: 3 });
+ expect(fleetOs.tree).toEqual({ lastSeq: 2, headSeq: 3 });
expect(fleetOs.shields).toEqual({ applied: 2, pending: 0, idle: 2 });
expect(fleetOs.exposed).toBe(2);
- expect(none.tree).toEqual({ minLastSeq: null, headSeq: null });
+ expect(none.tree).toEqual({ lastSeq: null, headSeq: null });
expect(none.unknown).toBe(1);
});
@@ -227,6 +227,28 @@ describe("groupByImage", () => {
const mixed = groupByImage([toFleetNode(ROWS[0]!), toFleetNode(row({ ak_name: "x", kernel_release: "7.5.0" }))]);
expect(mixed[0]!.kernel).toBeNull();
});
+
+ test("reports the tree of the member furthest behind, never a mixed pair", () => {
+ const member = (over: Partial<NodeRow>) => toFleetNode(row({ ...over }));
+ // Two members on independent trees, each at its own head: caught up.
+ const heads = groupByImage([
+ member({ ak_name: "000b001", last_seq: 3, head_seq: 3 }),
+ member({ ak_name: "000b002", last_seq: 8, head_seq: 8 }),
+ ]);
+ expect(heads[0]!.tree).toEqual({ lastSeq: 3, headSeq: 3 });
+ // A member a step behind its tree reports its real position.
+ const behind = groupByImage([
+ member({ ak_name: "000b001", last_seq: 2, head_seq: 3 }),
+ member({ ak_name: "000b002", last_seq: 8, head_seq: 8 }),
+ ]);
+ expect(behind[0]!.tree).toEqual({ lastSeq: 2, headSeq: 3 });
+ // A member without a tree does not hide the position of one that has it.
+ const mixed = groupByImage([
+ member({ ak_name: "000b001", generation: null, last_seq: null, head_seq: null }),
+ member({ ak_name: "000b002", last_seq: 5, head_seq: 5 }),
+ ]);
+ expect(mixed[0]!.tree).toEqual({ lastSeq: 5, headSeq: 5 });
+ });
});
describe("toFleetReport", () => {