3 import { glob, basename, dirname, readlink, readfile, realpath, writefile, error, open } from "fs";
6 let cpu_thread_weight = 0.75;
9 let debug = 0, do_nothing = 0;
13 for (let arg in ARGV) {
27 function task_name(pid)
29 let stat = open(`/proc/${pid}/status`, "r");
32 let line = stat.read("line");
34 return trim(split(line, "\t", 2)[1]);
37 function set_task_cpu(pid, cpu) {
39 cpu = join(",", map(cpus, (cpu) => cpu.id));
40 let name = task_name(pid);
43 if (debug || do_nothing)
44 warn(`taskset -p -c ${cpu} ${name}\n`);
46 system(`taskset -p -c ${cpu} ${pid}`);
49 function set_netdev_cpu(dev, cpu) {
50 let queues = glob(`/sys/class/net/${dev}/queues/rx-*/rps_cpus`);
51 let val = sprintf("%x", (1 << int(cpu)));
54 for (let queue in queues) {
55 if (debug || do_nothing)
56 warn(`echo ${val} > ${queue}\n`);
58 writefile(queue, `${val}`);
62 function task_device_match(name, device)
64 let napi_match = match(name, /napi\/([^-+])-\d+/);
66 napi_match = match(name, /mt76-tx (phy\d+)/);
68 (index(device.phy, napi_match[1]) >= 0 ||
69 index(device.netdev, napi_match[1]) >= 0))
72 if (device.driver == "mtk_soc_eth" && match(name, /napi\/mtk_eth-/))
78 cpus = map(glob("/sys/bus/cpu/devices/*"), (path) => {
80 id: int(match(path, /.*cpu(\d+)/)[1]),
81 core: int(trim(readfile(`${path}/topology/core_id`))),
86 cpus = slice(cpus, 0, 64);
90 function cpu_add_weight(cpu_id, weight)
92 let cpu = cpus[cpu_id];
94 for (let sibling in cpus) {
95 if (sibling == cpu || sibling.core != cpu.core)
97 sibling.load += weight * cpu_thread_weight;
101 function get_next_cpu(weight, prev_cpu)
106 let sort_cpus = sort(slice(cpus), (a, b) => a.load - b.load);
109 if (prev_cpu != null && sort_cpus[idx].id == prev_cpu)
112 let cpu = sort_cpus[idx].id;
113 cpu_add_weight(cpu, weight);
118 let netdev_phys = {};
119 let netdevs = map(glob("/sys/class/net/*"), (dev) => basename(dev));
121 for (let dev in netdevs) {
122 let pdev_path = realpath(`/sys/class/net/${dev}/device`);
126 if (length(glob(`/sys/class/net/${dev}/lower_*`)) > 0)
129 let pdev = phys_devs[pdev_path];
131 pdev = phys_devs[pdev_path] = {
133 driver: basename(readlink(`${pdev_path}/driver`)),
140 let phyidx = trim(readfile(`/sys/class/net/${dev}/phy80211/index`));
141 if (phyidx != null) {
142 let phy = `phy${phyidx}`;
143 if (index(pdev.phy, phy) < 0)
147 push(pdev.netdev, dev);
148 netdev_phys[dev] = pdev;
151 for (let path in glob("/proc/*/exe")) {
153 if (error() != "No such file or directory")
156 let pid = basename(dirname(path));
157 let name = task_name(pid);
158 for (let devname in phys_devs) {
159 let dev = phys_devs[devname];
160 if (!task_device_match(name, dev))
163 push(dev.tasks, pid);
168 function assign_dev_cpu(dev) {
169 if (length(dev.tasks) > 0) {
170 let cpu = dev.napi_cpu = get_next_cpu(napi_weight);
171 for (let task in dev.tasks)
172 set_task_cpu(task, cpu);
175 if (length(dev.netdev) > 0) {
176 let cpu = dev.rx_cpu = get_next_cpu(rx_weight, dev.napi_cpu);
177 for (let netdev in dev.netdev)
178 set_netdev_cpu(netdev, cpu);
182 // Assign ethernet devices first
183 for (let devname in phys_devs) {
184 let dev = phys_devs[devname];
185 if (!length(dev.phy))
189 // Add bias to avoid assigning other tasks to CPUs with ethernet NAPI
190 for (let devname in phys_devs) {
191 let dev = phys_devs[devname];
192 if (!length(dev.tasks) || dev.napi_cpu == null)
194 cpu_add_weight(dev.napi_cpu, eth_bias);
197 // Assign WLAN devices
198 for (let devname in phys_devs) {
199 let dev = phys_devs[devname];
200 if (length(dev.phy) > 0)
205 warn(sprintf("devices: %.J\ncpus: %.J\n", phys_devs, cpus));