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");
30 let line = stat.read("line");
32 return trim(split(line, "\t", 2)[1]);
35 function set_task_cpu(pid, cpu) {
37 cpu = join(",", map(cpus, (cpu) => cpu.id));
38 if (debug || do_nothing)
39 warn(`taskset -p -c ${cpu} ${task_name(pid)}\n`);
41 system(`taskset -p -c ${cpu} ${pid}`);
44 function set_netdev_cpu(dev, cpu) {
45 let queues = glob(`/sys/class/net/${dev}/queues/rx-*/rps_cpus`);
46 let val = sprintf("%x", (1 << int(cpu)));
49 for (let queue in queues) {
50 if (debug || do_nothing)
51 warn(`echo ${val} > ${queue}\n`);
53 writefile(queue, `${val}`);
57 function task_device_match(name, device)
59 let napi_match = match(name, /napi\/([^-+])-\d+/);
61 napi_match = match(name, /mt76-tx (phy\d+)/);
63 (index(device.phy, napi_match[1]) >= 0 ||
64 index(device.netdev, napi_match[1]) >= 0))
67 if (device.driver == "mtk_soc_eth" && match(name, /napi\/mtk_eth-/))
73 cpus = map(glob("/sys/bus/cpu/devices/*"), (path) => {
75 id: int(match(path, /.*cpu(\d+)/)[1]),
76 core: int(trim(readfile(`${path}/topology/core_id`))),
81 cpus = slice(cpus, 0, 64);
85 function cpu_add_weight(cpu_id, weight)
87 let cpu = cpus[cpu_id];
89 for (let sibling in cpus) {
90 if (sibling == cpu || sibling.core != cpu.core)
92 sibling.load += weight * cpu_thread_weight;
96 function get_next_cpu(weight, prev_cpu)
101 let sort_cpus = sort(slice(cpus), (a, b) => a.load - b.load);
104 if (prev_cpu != null && sort_cpus[idx].id == prev_cpu)
107 let cpu = sort_cpus[idx].id;
108 cpu_add_weight(cpu, weight);
113 let netdev_phys = {};
114 let netdevs = map(glob("/sys/class/net/*"), (dev) => basename(dev));
116 for (let dev in netdevs) {
117 let pdev_path = realpath(`/sys/class/net/${dev}/device`);
121 if (length(glob(`/sys/class/net/${dev}/lower_*`)) > 0)
124 let pdev = phys_devs[pdev_path];
126 pdev = phys_devs[pdev_path] = {
128 driver: basename(readlink(`${pdev_path}/driver`)),
135 let phyidx = trim(readfile(`/sys/class/net/${dev}/phy80211/index`));
136 if (phyidx != null) {
137 let phy = `phy${phyidx}`;
138 if (index(pdev.phy, phy) < 0)
142 push(pdev.netdev, dev);
143 netdev_phys[dev] = pdev;
146 for (let path in glob("/proc/*/exe")) {
148 if (error() != "No such file or directory")
151 let pid = basename(dirname(path));
152 let name = task_name(pid);
153 for (let devname in phys_devs) {
154 let dev = phys_devs[devname];
155 if (!task_device_match(name, dev))
158 push(dev.tasks, pid);
163 function assign_dev_cpu(dev) {
164 if (length(dev.tasks) > 0) {
165 let cpu = dev.napi_cpu = get_next_cpu(napi_weight);
166 for (let task in dev.tasks)
167 set_task_cpu(task, cpu);
170 if (length(dev.netdev) > 0) {
171 let cpu = dev.rx_cpu = get_next_cpu(rx_weight, dev.napi_cpu);
172 for (let netdev in dev.netdev)
173 set_netdev_cpu(netdev, cpu);
177 // Assign ethernet devices first
178 for (let devname in phys_devs) {
179 let dev = phys_devs[devname];
180 if (!length(dev.phy))
184 // Add bias to avoid assigning other tasks to CPUs with ethernet NAPI
185 for (let devname in phys_devs) {
186 let dev = phys_devs[devname];
187 if (!length(dev.tasks) || dev.napi_cpu == null)
189 cpu_add_weight(dev.napi_cpu, eth_bias);
192 // Assign WLAN devices
193 for (let devname in phys_devs) {
194 let dev = phys_devs[devname];
195 if (length(dev.phy) > 0)
200 warn(sprintf("devices: %.J\ncpus: %.J\n", phys_devs, cpus));