From nobody Mon Apr 6 04:58:05 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CE69AC6FA83 for ; Sun, 11 Sep 2022 08:35:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229966AbiIKIfp (ORCPT ); Sun, 11 Sep 2022 04:35:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229908AbiIKIfk (ORCPT ); Sun, 11 Sep 2022 04:35:40 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D68F30F4C for ; Sun, 11 Sep 2022 01:35:36 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id j3-20020a170902da8300b001782a6fbc87so557179plx.5 for ; Sun, 11 Sep 2022 01:35:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=+SqLElBwbW3rvekY3ijlqjV+hQEFZtxBqbrdA9ngSjc=; b=cdgtThGnOagpzntnndBJxW7/R89LxjmSM5iRoI5HtHKhX4XuzS1OHEoUPCkIIHB3qr BhjlKiSC9/HYGlhUzYaOJthaAhYpKWGiczWOEnLI+7fmt2RV16BjDN+9uR0boVf2k7/Y zmZTC94zr8SJXyv1UDGHYsq1oBYpR5svSUiFPTwXHrNbjb4mg1QltASaPGdlmfOGFAGd DZi6Tgqs67mDSIQH7R5+7T4MLP2S9xds+UcmQWwQjClEiXyQJIFt4GJEqoSfPzeE55P/ YorAcJD1+UzBhw3lNkSNUfOBVxmIp8uhV6b/qoxEaIiGGrbo37lNYMzgPu1mnc6VtLgj zWfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=+SqLElBwbW3rvekY3ijlqjV+hQEFZtxBqbrdA9ngSjc=; b=j8pFpvmDhD3XASqd5qD5PJ74tjjwTPnoE4zYJaUfa4kcRHCgaT0z2Nj1g/YUo/IQFP tI+y9yZuZfHjxtkUvtvYJ5IclNpjTmInZ4jY6BbYEHueXmZdYzrIXzSrMHyFoJVl4Bxh WKlInRge5jI4gL+CsaW4uZDTmLx/p2wlBFuBAeeUBjNvPYOYux3OALvmSmm7/XDOl4DY gz1poRuiIFjWnFiZSN3YX/wV6QLeveGHtBZDdxjCY7+fmulsgFFgSw/nnr7FztNJ8rgg aUqkN+AMRpjDuLQiEql5DJuV8PBg+MmJpCrTLhuGY5exj5Tc1wCOsV0mliG1wQZvcLmc t29w== X-Gm-Message-State: ACgBeo3QzNu936T/x+/udMoWfDhpBo7dCKshj0Yp8IuIQHh7YMW/HwGZ l4ckldGdJGeBSjHTwYkKF5hoqRMN5WV6 X-Google-Smtp-Source: AA6agR4ShsWN+jgimkiPO+j8MYcaEgkkuvMf05zPiNiX/H3atMP+rzUDrkMRVZ8epv0HbboYAH8LNzeQz47l X-Received: from yuanchu.svl.corp.google.com ([2620:15c:2d4:203:5076:f273:1383:891d]) (user=yuanchu job=sendgmr) by 2002:a05:6a00:2395:b0:536:8296:51d5 with SMTP id f21-20020a056a00239500b00536829651d5mr22475520pfc.84.1662885336105; Sun, 11 Sep 2022 01:35:36 -0700 (PDT) Date: Sun, 11 Sep 2022 01:34:18 -0700 In-Reply-To: <20220911083418.2818369-1-yuanchu@google.com> Mime-Version: 1.0 References: <20220911083418.2818369-1-yuanchu@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220911083418.2818369-3-yuanchu@google.com> Subject: [RFC PATCH 2/2] mm: add a BPF-based per-process heatmap tool From: Yuanchu Xie To: linux-mm@kvack.org, Yu Zhao Cc: Michael Larabel , Jon Corbet , Andrew Morton , Yuanchu Xie , linux-kernel@vger.kernel.org, bpf@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The heatmap tool uses a bpf program integrated with a TUI as a proof of concept for consuming page access information from MGLRU. It displays heats, NUMA node, and anon/other with configurable aging intervals and aggregation intervals. Signed-off-by: Yuanchu Xie --- tools/vm/heatmap/Makefile | 30 ++ tools/vm/heatmap/heatmap.bpf.c | 123 +++++++ tools/vm/heatmap/heatmap.user.c | 188 ++++++++++ tools/vm/heatmap/heatmap_tui.py | 600 ++++++++++++++++++++++++++++++++ 4 files changed, 941 insertions(+) create mode 100644 tools/vm/heatmap/Makefile create mode 100644 tools/vm/heatmap/heatmap.bpf.c create mode 100644 tools/vm/heatmap/heatmap.user.c create mode 100755 tools/vm/heatmap/heatmap_tui.py diff --git a/tools/vm/heatmap/Makefile b/tools/vm/heatmap/Makefile new file mode 100644 index 000000000000..43ae4af67781 --- /dev/null +++ b/tools/vm/heatmap/Makefile @@ -0,0 +1,30 @@ +# SPDX-License-Identifier: GPL-2.0 +include ../../build/Build.include + +MAKEFLAGS +=3D --no-builtin-rules +MAKEFLAGS +=3D --no-builtin-variables + +CC :=3D clang +KERNEL_DIR :=3D $(abspath ../../..) + +.PHONY: clean all + +all: heatmap.user heatmap.skel.h vmlinux.h + +%.user: %.user.o + $(CC) -g -O2 $^ -l:libbpf.a -lelf -lz -o $@ -static + +vmlinux.h: $(KERNEL_DIR)/vmlinux + bpftool btf dump file $(KERNEL_DIR)/vmlinux format c > vmlinux.h + +%.bpf.o: vmlinux.h %.bpf.c + $(CC) -g -D__TARGET_ARCH_x86 -O2 -target bpf -c $*.bpf.c -o $*.bpf.o + +%.skel.h: %.bpf.o + bpftool gen skeleton $*.bpf.o > $*.skel.h + +%.user.o: %.skel.h %.user.c + $(CC) -g -O2 -c $*.user.c -o $*.user.o + +clean: + rm -f *.o heatmap.user *.skel.h vmlinux.h diff --git a/tools/vm/heatmap/heatmap.bpf.c b/tools/vm/heatmap/heatmap.bpf.c new file mode 100644 index 000000000000..924d896a0c4f --- /dev/null +++ b/tools/vm/heatmap/heatmap.bpf.c @@ -0,0 +1,123 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "vmlinux.h" + +#include +#include + +static const u8 one =3D 1; +pid_t target_pid; +#define MAP_SHIFT (12 + 9) +#define MIXED_NODES -1 +#define MIXED_MEM -1 + +struct region_stat { + u16 accesses; + s8 mem_type; /* NON_ANON, ANON */ + s8 node_id; +}; + +struct heatmap_outer { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(map_flags, BPF_F_NO_PREALLOC); + __uint(max_entries, 1000000); + __type(key, u64); + __type(value, struct region_stat); +} heatmap SEC(".maps"); + +int probe(unsigned int nid, unsigned long addr, unsigned long len, bool an= on) +{ + u64 map_key =3D addr >> MAP_SHIFT; + struct region_stat *region =3D bpf_map_lookup_elem(&heatmap, &map_key); + int err; + struct region_stat to_insert; + + if (!region) { + to_insert.accesses =3D len; + to_insert.mem_type =3D anon; + to_insert.node_id =3D nid; + err =3D bpf_map_update_elem(&heatmap, &map_key, &to_insert, + BPF_NOEXIST); + if (err) + return err; + } else { + region->accesses +=3D len; + if (region->node_id !=3D (int)nid) + region->node_id =3D MIXED_NODES; + if (region->mem_type !=3D anon) + region->mem_type =3D MIXED_MEM; + err =3D bpf_map_update_elem(&heatmap, &map_key, region, + BPF_EXIST); + if (err) + return err; + } + return 0; +} + +SEC("fentry/mglru_pte_probe") +int BPF_PROG(fentry_mglru_pte_probe, pid_t pid, unsigned int nid, + unsigned long addr, unsigned long len, bool anon) +{ + int err; + + if (pid !=3D target_pid) + return 0; + err =3D probe(nid, addr, len, anon); + if (err) + bpf_printk("PTE called addr:0x%lx len:%lu error:%ld", addr, len, + err); + return 0; +} + +SEC("fentry/mglru_pmd_probe") +int BPF_PROG(fentry_mglru_pmd_probe, pid_t pid, unsigned int nid, + unsigned long addr, unsigned long len, bool anon) +{ + int err; + + if (pid !=3D target_pid) + return 0; + err =3D probe(nid, addr, len, anon); + if (err) + bpf_printk("PMD called addr:0x%lx len:%lu error:%ld", addr, len, + err); + return 0; +} + +extern void +bpf_set_skip_mm(struct bpf_mglru_should_skip_mm_control *should_skip) __ks= ym; + +SEC("fentry/bpf_mglru_should_skip_mm") +int BPF_PROG(bpf_mglru_should_skip_mm, + struct bpf_mglru_should_skip_mm_control *ctl) +{ + if (ctl->pid !=3D target_pid) { + bpf_printk("aging wrong pid"); + bpf_set_skip_mm(ctl); + } + return 0; +} + +extern int bpf_run_aging(int memcg_id, bool can_swap, bool force_scan) __k= sym; + +struct args { + int memcg_id; +}; + +SEC("syscall") +int memcg_run_aging(struct args *ctx) +{ + int err; + + err =3D bpf_run_aging(ctx->memcg_id, true, true); + + if (err !=3D 0) { + bpf_printk("aging failed for memcg %ld with error %ld", + ctx->memcg_id, err); + return 0; + } + + bpf_printk("aging succeeded for memcg %ld", ctx->memcg_id); + return 0; +} + +char LICENSE[] SEC("license") =3D "GPL"; diff --git a/tools/vm/heatmap/heatmap.user.c b/tools/vm/heatmap/heatmap.use= r.c new file mode 100644 index 000000000000..094ba1e49233 --- /dev/null +++ b/tools/vm/heatmap/heatmap.user.c @@ -0,0 +1,188 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "heatmap.skel.h" + +#define MAP_SHIFT (12 + 9) + +static bool terminate; + +struct args { + int memcg_id; +}; + +void handle_sigint(int sig) +{ + terminate =3D true; +} + +static int libbpf_print_fn(enum libbpf_print_level level, const char *form= at, + va_list args) +{ + return vfprintf(stderr, format, args); +} + +int run_aging(int aging_fd, int memcg_id) +{ + struct args ctx =3D { + .memcg_id =3D memcg_id, + }; + LIBBPF_OPTS(bpf_test_run_opts, tattr, .ctx_in =3D &ctx, + .ctx_size_in =3D sizeof(ctx)); + return bpf_prog_test_run_opts(aging_fd, &tattr); +} + +int attach_progs(pid_t pid, struct heatmap_bpf **heatmap_obj, int *aging_f= d, + int *heatmap_fd) +{ + int err; + int fd; + struct heatmap_bpf *obj; + + obj =3D heatmap_bpf__open(); + if (obj =3D=3D NULL) { + perror("Error when opening heatmap bpf object"); + return -1; + } + obj->bss->target_pid =3D pid; + + err =3D heatmap_bpf__load(obj); + if (err) { + perror("Error loading heatmap bpf object"); + goto cleanup; + } + + fd =3D bpf_program__fd(obj->progs.memcg_run_aging); + + err =3D heatmap_bpf__attach(obj); + if (err) { + perror("Error attaching heatmap bpf object"); + goto cleanup; + } + + *aging_fd =3D fd; + *heatmap_fd =3D bpf_map__fd(obj->maps.heatmap); + *heatmap_obj =3D obj; + return 0; + +cleanup: + heatmap_bpf__destroy(obj); + return err; +} + +int bpf_map_delete_and_get_next_key(int fd, const void *key, void *next_ke= y) +{ + int err =3D bpf_map_get_next_key(fd, key, next_key); + + bpf_map_delete_elem(fd, key); + return err; +} + +struct region_stat { + __u16 accesses; + __s8 mem_type; /* NON_ANON, ANON */ + __s8 node_id; +}; + +void dump_map(int fd) +{ + __u64 prev_key, key; + struct region_stat value; + int err; + + while (bpf_map_delete_and_get_next_key(fd, &prev_key, &key) =3D=3D 0) { + err =3D bpf_map_lookup_elem(fd, &key, &value); + if (err < 0) { + /* impossible if we don't have racing deletions */ + exit(-1); + } + printf("%llu %u %d %d\n", key << MAP_SHIFT, value.accesses, + value.mem_type, value.node_id); + prev_key =3D key; + } +} + +void detach_progs(struct heatmap_bpf *heatmap_obj) +{ + heatmap_bpf__detach(heatmap_obj); + heatmap_bpf__destroy(heatmap_obj); +} + +int main(void) +{ + struct heatmap_bpf *heatmap_obj =3D NULL; + int aging_fd =3D -1; + int heatmap_fd =3D -1; + int memcg_id =3D -1; + int err; + + signal(SIGINT, handle_sigint); + setvbuf(stdout, NULL, _IONBF, BUFSIZ); + libbpf_set_print(libbpf_print_fn); + + while (!terminate) { + char *buffer =3D NULL; + + if (scanf("%ms", &buffer) =3D=3D 1) { + if (strcmp(buffer, "exit") =3D=3D 0) { + printf("No hard feelings.\n"); + exit(0); + + } else if (heatmap_obj =3D=3D NULL && + strcmp(buffer, "attach") =3D=3D 0) { + pid_t pid_; + int memcg_id_; + + if (scanf("%d %d", &pid_, &memcg_id_) =3D=3D 2) { + err =3D attach_progs(pid_, &heatmap_obj, + &aging_fd, + &heatmap_fd); + if (err) { + printf("error: aging %d\n", + err); + goto next; + } + memcg_id =3D memcg_id_; + printf("success: attach\n"); + + } else + printf("error: invalid arguments\n"); + + } else if (heatmap_obj !=3D NULL) { + if (strcmp(buffer, "map") =3D=3D 0) { + dump_map(heatmap_fd); + printf("success: map\n"); + + } else if (strcmp(buffer, "age") =3D=3D 0) { + err =3D run_aging(aging_fd, memcg_id); + if (err) + printf("error: age %d\n", err); + else + printf("success: age\n"); + + } else if (strcmp(buffer, "detach") =3D=3D 0) { + detach_progs(heatmap_obj); + heatmap_obj =3D NULL; + heatmap_fd =3D -1; + aging_fd =3D -1; + memcg_id =3D -1; + printf("success: detach\n"); + } + + } else + printf("error: invalid command\n"); + +next: + free(buffer); + } else + printf("error: invalid command\n"); + } +} diff --git a/tools/vm/heatmap/heatmap_tui.py b/tools/vm/heatmap/heatmap_tui= .py new file mode 100755 index 000000000000..9be6b611bc24 --- /dev/null +++ b/tools/vm/heatmap/heatmap_tui.py @@ -0,0 +1,600 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 +import urwid +import parse +import subprocess +import os + +from time import time +from math import floor + +UPDATE_INTERVAL =3D 0.5 +MEM_MAX =3D 0 +MEM_MIN =3D 0x10000000000000000 +INVALID_NODE =3D -2 +MIXED_NODE =3D -1 +INVALID_MEM =3D -2 +MIXED_MEM =3D -1 +ANON_MEM =3D 1 +MAX_AGGREGATIONS =3D 20 +MIN_ROWS =3D 40 +MAX_COL =3D 7 + + +class HeatmapModel: + def __init__(self, max_nr_aggregations): + self.data_range =3D (MEM_MIN, MEM_MAX) + self.data =3D [] + self.max_nr_aggregations =3D max_nr_aggregations + + def get_nr_aggregations(self): + return len(self.data) + + def get_max_nr_aggregations(self): + return self.max_nr_generations + + def set_max_nr_aggregations(self, max_nr_aggregations): + self.max_nr_aggregations =3D max_nr_aggregations + + def append_data(self, bpf_access_data): + region_min =3D MEM_MIN + region_max =3D MEM_MAX + for region in bpf_access_data: + if region["address"] > region_max: + region_max =3D region["address"] + if region["address"] < region_min: + region_min =3D region["address"] + + self.data.append({ + "min": region_min, + "max": region_max, + "data": sorted(bpf_access_data, key=3Dlambda d: d["address"]), + }) + + if len(self.data) > self.max_nr_aggregations: + del self.data[:len(self.data) - self.max_nr_aggregations] + + + def get_display_sections(self, rows): + REGION_SIZE =3D 2 * 1024 * 1024 # region size (21 bits) 2 MB + ranges =3D [] + for d in self.data: + for r in d["data"]: + addr =3D r["address"] + ranges.append(("start", addr - REGION_SIZE)) + ranges.append(("end", addr + REGION_SIZE * 2)) + + ranges =3D sorted(ranges, key=3Dlambda d: d[1]) + sections =3D [] + start_addr =3D None + nesting =3D 0 + total_size =3D 0 + for (tag, addr) in ranges: + if tag =3D=3D "start": + nesting +=3D 1 + if start_addr =3D=3D None: + start_addr =3D addr + + if tag =3D=3D "end": + nesting -=3D 1; + if nesting =3D=3D 0: + total_size +=3D addr - start_addr + sections.append((start_addr, addr)) + start_addr =3D None + + if len(sections) > rows: + # compact some sections + sections_with_idx =3D [((start, end), i, end - start) for (i, = (start, end)) in enumerate(sections)] + sections_with_idx =3D sorted(sections_with_idx, key=3Dlambda x: + x[-1] + abs(sections[x[1] + 1][0] -= x[0][1]) + abs(sections[x[1] - 1][1] - x[0][0])) + for i in range(0, len(sections) - rows): + # natural number of regions is greater than the number of = rows + ((start, end), section_i, size) =3D sections_with_idx[i] + (_, prev_end) =3D sections[section_i - 1] + (succ_start, _) =3D sections[section_i + 1] + if abs(succ_start - end) > abs(prev_end - start): + sections[section_i - 1][1] =3D end + else: + sections[section_i - 1][0] =3D start + + new_sections =3D [] + for i in range(len(sections) - rows, len(sections)): + (_, section_i, _) =3D sections_with_idx[i] + new_sections.append(sections[section_i]) + sections =3D sorted(new_sections, key=3Dlambda x: x[0]) + + else: + extra_rows =3D rows - len(sections) + split_sections =3D [] + spill_over_factor =3D 0 + for (start, end) in sections: + fraction_of_row =3D (end - start) / total_size * rows + if fraction_of_row > 1: + additional_rows_frac =3D (spill_over_factor + (fractio= n_of_row - 1)) + spill_over_factor =3D additional_rows_frac - floor(add= itional_rows_frac) + additional_rows =3D min(floor(additional_rows_frac), e= xtra_rows) + extra_rows -=3D additional_rows + new_rows =3D additional_rows + 1 + # split current interval + inc =3D (end - start + new_rows - 1) // new_rows # rou= nd up + for i in range(0, new_rows): + split_sections.append((start + inc * i, min(start = + inc * (i + 1), end))) + else: + split_sections.append((start, end)) + while extra_rows > 0: + split_sections.append((0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFF= FFF)) + extra_rows -=3D 1 + sections =3D split_sections + + return sections + + def format_data(self, rows): + cell_init =3D (0, INVALID_MEM, INVALID_NODE) + def cell_update(cell, r): + (acc, mem, node) =3D cell + acc +=3D r["accesses"] + if mem =3D=3D INVALID_MEM: + mem =3D r["mem"] + elif mem !=3D r["mem"]: + mem =3D MIXED_MEM + + if node =3D=3D INVALID_NODE: + node =3D r["node"] + elif node !=3D r["node"]: + node =3D MIXED_NODE + + return (acc, mem, node) + + return self.__format_data(rows, cell_init, cell_update) + + def __format_data(self, rows, cell_init, cell_update): + row_ranges =3D self.get_display_sections(rows) + assert(len(row_ranges) =3D=3D rows) + graph =3D [] + for d in self.data: + (curr_start, curr_end) =3D row_ranges[0] + range_idx =3D 0 + + col =3D [] + cell =3D cell_init + for r in d["data"]: + while r["address"] >=3D curr_end: + col.append(cell) + cell =3D cell_init + range_idx +=3D 1 + (curr_start, curr_end) =3D row_ranges[range_idx] + + cell =3D cell_update(cell, r) + + while range_idx < len(row_ranges): + (curr_start, curr_end) =3D row_ranges[range_idx] + col.append(cell) + cell =3D cell_init + range_idx +=3D 1 + + assert(len(col) =3D=3D rows) + graph.append(col) + + col_labels =3D [start for (start, end) in row_ranges] + return (col_labels, graph) + + +class GraphView(urwid.Widget): + def __init__(self, model, modes): + self.model =3D model + self.mode =3D modes[0] + urwid.Widget.__init__(self) + + def set_mode(self, mode): + self.mode =3D mode + self.update() + + def render_col(self, col): + l =3D [] + if self.mode =3D=3D "Anon/Other": + for (_, mem, _) in col: + if mem =3D=3D MIXED_MEM: + l.append(("mixed", "M" * MAX_COL)) + elif mem =3D=3D INVALID_MEM: + l.append(("invalid", "." * MAX_COL)) + elif mem =3D=3D ANON_MEM: + l.append(("anon mem", "A" * MAX_COL)) + else: + l.append(("other mem", "X" * MAX_COL)) + elif self.mode =3D=3D "NUMA Node": + for (_, _, node) in col: + if node =3D=3D MIXED_NODE: + l.append(("mixed", "M" * MAX_COL)) + elif node =3D=3D INVALID_NODE: + l.append(("invalid", "." * MAX_COL)) + else: + node_str =3D str(node) + left_pad =3D (MAX_COL - len(node_str)) // 2 + right_pad =3D MAX_COL - left_pad - len(node_str) + node_style =3D "node" + if node < 4 and node >=3D 0: + node_style +=3D " " + node_str + l.append((node_style, "_" * left_pad + node_str + "_" = * right_pad)) + else: + for (acc, _, _) in col: + acc_str =3D str(acc) + left_pad =3D (MAX_COL - len(acc_str)) // 2 + right_pad =3D MAX_COL - left_pad - len(acc_str) + heat_style =3D "heat" + if acc >=3D 512: + heat_style +=3D " mid" + elif acc >=3D 1024: + heat_style +=3D " high" + l.append((heat_style, u"\u00a0" * left_pad + acc_str + u"\= u00a0" * right_pad)) + + return (urwid.Text(l).render((MAX_COL,)), None, False, MAX_COL) + + def render(self, size, focus=3DFalse): + (cols, rows) =3D size + LABEL_COLS =3D 16 + 2 + 4 # 0x and 16 chars of hex address, plus p= adding + self.model.set_max_nr_aggregations((cols - LABEL_COLS) // MAX_COL) + data_cols =3D cols - LABEL_COLS + (labels, data) =3D self.model.format_data(rows)[-(data_cols // MAX= _COL):] + if len(labels) =3D=3D 0: + label_col =3D urwid.SolidCanvas(" ", LABEL_COLS, rows) + else: + label_col =3D [("pg smooth", "0x{:016X}".format(addr) + u"\u00= a0" * 4) for addr in labels] + label_col =3D urwid.Text(label_col).render((LABEL_COLS,)) + + label_col =3D (label_col, None, False, LABEL_COLS) + if len(data) > 0: + return urwid.CanvasJoin([label_col] + list(map(self.render_col= , data)) + [ + (urwid.SolidCanvas(" ", data_cols - len(data) * MAX_COL, r= ows), + None, False, data_cols - len(data) * MAX_COL)]) + else: + return urwid.SolidCanvas(" ", cols, rows) + + def update(self): + self._invalidate() + + def rows(self, size, focus=3DFalse): + return MIN_ROWS + + def keypress(self, size, key): + return key + + +class HeatmapView(urwid.WidgetWrap): + palette =3D [ + ('body', 'black', 'light gray', 'standout'), + ('header', 'white', 'dark red', 'bold'), + ('screen edge', 'light blue', 'dark cyan'), + ('main shadow', 'dark gray', 'black'), + ('line', 'black', 'light gray', 'standout'), + ('bg background','light gray', 'black'), + ('bg 1', 'black', 'dark blue', 'standout'), + ('bg 1 smooth', 'dark blue', 'black'), + ('bg 2', 'black', 'dark cyan', 'standout'), + ('bg 2 smooth', 'dark cyan', 'black'), + ('button normal','light gray', 'dark blue', 'standout'), + ('button select','white', 'dark green'), + ('line', 'black', 'light gray', 'standout'), + ('pg normal', 'white', 'black', 'standout'), + ('pg complete', 'white', 'dark magenta'), + ('pg smooth', 'dark magenta','black'), + + ("mixed", "light blue", "black"), + ("invalid", "dark gray", "black"), + ("anon mem", "white", "dark cyan"), + ("other mem", "light blue", "dark red"), + + ("node", "white", "black"), + + ("node 0", "light green", "black"), + ("node 1", "light blue", "black"), + ("node 2", "light red", "black"), + ("node 3", "yellow", "black"), + + ("heat", "yellow", "black"), + ("heat mid", "dark red", "brown"), + ("heat high", "light red", "dark red"), + ] + + def __init__(self, controller): + self.controller =3D controller + urwid.WidgetWrap.__init__(self, self.draw_view()) + + def update_graph(self): + self.graph_view.update() + pass + + def set_selected_mode(self, new_mode): + self.graph_view.set_mode(new_mode) + for b in self.mode_buttons: + if b.get_label() =3D=3D new_mode: + b.set_state(True, do_callback=3DFalse) + break + + def on_mode_button(self, button, state): + if state: + self.controller.on_mode_change(button.get_label()) + + def radio_button(self, group, label, state, on_state_change): + w =3D urwid.RadioButton(group, label, state, on_state_change=3Don_= state_change) + w =3D urwid.AttrWrap(w, 'button normal', 'button select') + return w + + def button(self, label, on_press): + w =3D urwid.Button(label, on_press) + w =3D urwid.AttrWrap(w, 'button normal', 'button select') + return w + + def set_alert_message(self, new_alert_text): + self.alert_text.set_text(new_alert_text) + pass + + def set_start_button_text(self, new_text): + self.start_button.set_label(new_text) + pass + + def edit_box(self, label, text, on_change): + w =3D urwid.Edit(label, text) + urwid.connect_signal(w, 'change', on_change) + w =3D urwid.AttrWrap(w, 'edit') + return w + + def draw_control_pane(self): + g =3D [] + self.mode_buttons =3D [self.radio_button(g, mode, mode =3D=3D self= .controller.mode, + self.on_mode_button) + for mode in self.controller.overlay_modes] + self.pid_box =3D self.edit_box("PID: ", "1", self.controller.on_pi= d_change) + self.memcg_box =3D self.edit_box("memcg id: ", "1", self.controlle= r.on_memcg_change) + self.start_button =3D self.button("Start", self.controller.on_star= t_button) + self.alert_text =3D urwid.Text("", align=3D"center") + + aging_text =3D urwid.Text("Aging interval", align=3D"center") + self.aging_box =3D self.edit_box("Seconds: ", str(0.5), self.contr= oller.on_aging_change) + aggregation_text =3D urwid.Text("Aggregation Interval", align=3D"c= enter") + self.aggregation_box =3D self.edit_box("Aging cycles: ", str(3), s= elf.controller.on_aggregation_change) + + self.quit_button =3D self.button("Quit", self.controller.on_quit_b= utton) + l =3D [ + urwid.Text("Overlay Mode", align=3D"center") + ] + self.mode_buttons + [ + urwid.Divider(), + self.pid_box, + self.memcg_box, + self.start_button, + urwid.Divider(), + aging_text, + self.aging_box, + aggregation_text, + self.aggregation_box, + urwid.Divider(), + self.alert_text, + urwid.Divider(), + self.quit_button + ] + return urwid.ListBox(urwid.SimpleListWalker(l)) + + def main_shadow(self, w): + # Wrap a shadow and background around widget w + bg =3D urwid.AttrWrap(urwid.SolidFill(u"\u2592"), 'screen edge') + shadow =3D urwid.AttrWrap(urwid.SolidFill(u" "), 'main shadow') + + bg =3D urwid.Overlay( shadow, bg, + ('fixed left', 3), ('fixed right', 1), + ('fixed top', 2), ('fixed bottom', 1)) + w =3D urwid.Overlay( w, bg, + ('fixed left', 2), ('fixed right', 3), + ('fixed top', 1), ('fixed bottom', 2)) + return w + + def draw_view(self): + control_pane =3D self.draw_control_pane() + self.graph_view =3D GraphView(self.controller.model, self.controll= er.overlay_modes) + vline =3D urwid.AttrWrap(urwid.SolidFill(u'\u2502'), 'line') + w =3D urwid.Columns([("weight", 4, self.graph_view), ("fixed", 1, = vline), control_pane], + dividechars=3D1, focus_column=3D2) + w =3D urwid.Padding(w,('fixed left',1),('fixed right',0)) + w =3D urwid.AttrWrap(w,'body') + w =3D urwid.LineBox(w) + w =3D urwid.AttrWrap(w,'line') + w =3D self.main_shadow(w) + return w + +class BpfBridge: + def __init__(self): + self.bpf_bridge =3D subprocess.Popen(os.path.join(os.path.dirname(= os.path.abspath(__file__)), + "heatmap.user"), + stdin=3Dsubprocess.PIPE, + stdout=3Dsubprocess.PIPE, + stderr=3Dsubprocess.DEVNULL, + text=3DTrue) + + def attach(self, pid, memcg_id): + self.bpf_bridge.stdin.write("attach\n") + self.bpf_bridge.stdin.write("{:d} {:d}\n".format(pid, memcg_id)) + self.bpf_bridge.stdin.flush() + result =3D self.bpf_bridge.stdout.readline() + if "success" not in result: + return result + + return "success" + + def detach(self): + self.bpf_bridge.stdin.write("detach\n") + self.bpf_bridge.stdin.flush() + result =3D self.bpf_bridge.stdout.readline() + if "success" not in result: + return result + + return "success" + + def run_aging(self): + self.bpf_bridge.stdin.write("age\n") + self.bpf_bridge.stdin.flush() + result =3D self.bpf_bridge.stdout.readline() + if "success" not in result: + return result + + return "success" + + def get_map(self): + self.bpf_bridge.stdin.write("map\n") + self.bpf_bridge.stdin.flush() + result =3D "" + access_data =3D [] + parser =3D parse.compile("{:d} {:d} {:d} {:d}\n") + result =3D self.bpf_bridge.stdout.readline() + while "success" not in result: + parsed_access =3D parser.parse(result) + if parsed_access is None: + raise Exception("woo " + result) + return result + + access_data.append({ + "address": parsed_access[0], + "accesses": parsed_access[1], + "mem": parsed_access[2], + "node": parsed_access[3], + }) + result =3D self.bpf_bridge.stdout.readline() + + return access_data + + +class HeatmapController: + def __init__(self): + self.last_aged =3D 0 + self.aging_interval =3D 0.5 + self.aging_count =3D 0 + self.aggregation_interval =3D 3 + self.overlay_modes =3D ["Heat", "NUMA Node", "Anon/Other"] + self.monitoring_pid =3D 1 + self.monitoring_memcg_id =3D 1 + self.mode =3D self.overlay_modes[0] + self.model =3D HeatmapModel(MAX_AGGREGATIONS) + self.bpf_bridge =3D BpfBridge() + self.monitoring =3D False + self.view =3D HeatmapView(self) + + def main(self): + self.loop =3D urwid.MainLoop(self.view, self.view.palette) + self.timer =3D self.loop.set_alarm_in(UPDATE_INTERVAL, self.on_tim= er) + # spawn the monitored process + self.loop.run() + + def on_mode_change(self, new_mode): + mode =3D new_mode + self.view.set_selected_mode(new_mode) + + def on_aging_change(self, w, new_value): + try: + aging_interval =3D float(new_value) + if aging_interval > 0: + self.aging_interval =3D aging_interval + self.view.set_alert_message("") + else: + self.view.set_alert_message("invalid aging interval") + except ValueError: + self.view.set_alert_message("invalid aging interval") + + def on_aggregation_change(self, w, new_value): + try: + aggregation_interval =3D int(new_value) + if aggregation_interval > 0: + self.aggregation_interval =3D aggregation_interval + self.view.set_alert_message("") + else: + self.view.set_alert_message("invalid aggregation interval") + except ValueError: + self.view.set_alert_message("invalid aggregation interval") + + def update_graph(self, new_data): + self.model.append_data(new_data) + self.view.update_graph() + + def on_timer(self, loop=3DNone, user_data=3DNone): + # perform aging + # read data + delta_time =3D -time() + if self.last_aged <=3D -delta_time - self.aging_interval: + if self.monitoring: + # aging + self.last_aged =3D -delta_time + self.aging_count +=3D 1 + err =3D self.bpf_bridge.run_aging() + if "success" not in err: + self.disable_monitoring() + self.view.set_alert_message(err) + + if self.aging_count % self.aggregation_interval =3D=3D 0: + # aggregation + data =3D self.bpf_bridge.get_map() + if isinstance(data, str): + # get map failed + self.disable_monitoring() + self.view.set_alert_message(err) + else: + self.update_graph(data) + + delta_time +=3D time() + if delta_time > UPDATE_INTERVAL: + self.view.set_alert_message("timer running behind") + self.loop.set_alarm_in(0, self.on_timer) + else: + self.loop.set_alarm_in(UPDATE_INTERVAL - delta_time, self.on_t= imer) + + def on_pid_change(self, widget, new_text): + pid =3D parse.parse("{:d}", new_text) + self.view.set_alert_message("") + if self.monitoring: + return + + if pid is not None: + self.monitoring_pid =3D pid[0] + else: + self.monitoring_pid =3D -1 + + def on_memcg_change(self, widget, new_text): + memcg_id =3D parse.parse("{:d}", new_text) + self.view.set_alert_message("") + if self.monitoring: + return + + if memcg_id is not None: + self.monitoring_memcg_id =3D memcg_id[0] + else: + self.monitoring_memcg_id =3D -1 + + def disable_monitoring(self): + self.monitoring =3D False + self.bpf_bridge.detach() + self.view.set_start_button_text("Start") + + def on_start_button(self, w): + if self.monitoring: + err =3D self.bpf_bridge.detach() + self.monitoring =3D False + self.view.set_start_button_text("Start") + if "success" not in err: + self.view.set_alert_message(err) + self.view.pid_box.set_edit_text(str(self.monitoring_pid)) + else: + if self.monitoring_pid !=3D -1 and self.monitoring_memcg_id != =3D -1: + err =3D self.bpf_bridge.attach(self.monitoring_pid, self.m= onitoring_memcg_id) + if "success" in err: + self.view.set_start_button_text("Stop") + self.monitoring =3D True + else: + self.view.set_alert_message(err) + else: + self.view.set_alert_message("invalid pid/memcg") + + + + def on_quit_button(self, w): + raise urwid.ExitMainLoop() + +def main(): + HeatmapController().main() + +if "__main__" =3D=3D __name__: + main() --=20 2.37.2.789.g6183377224-goog