Fix CPU plugin concurrency issues
Summary: Half way through sandy conversion I ended up redoing the task scheduling in this plugin because it was sketchy and unreliable. Previously, it was using setInterval to schedule a task every 500ms, which would query a lot of data from the device. The problem was that if these tasks took >500ms (which they did for me at least), you'd get overlapping ones, causing havoc. Now it uses setTimeout instead, promisifies everything, and only ever schedules the next timeout when the current task has finished. Also fixed a race condition where you could hit stop, but not actually stop the recurring tasks... and then there was no way to stop them because it was already "stopped". Reviewed By: mweststrate Differential Revision: D28061469 fbshipit-source-id: 2bb8aba7ddf37ba1fdbdc52c95ec258cae96f509
This commit is contained in:
committed by
Facebook GitHub Bot
parent
a53f1b8f8d
commit
92f51485c9
@@ -153,7 +153,11 @@ export function devicePlugin(client: PluginClient<{}, {}>) {
|
||||
displayCPUDetail: true,
|
||||
});
|
||||
|
||||
const updateCoreFrequency = (core: number, type: string) => {
|
||||
const updateCoreFrequency: (core: number, type: string) => Promise<void> = (
|
||||
core: number,
|
||||
type: string,
|
||||
) => {
|
||||
return new Promise((resolve, _reject) => {
|
||||
executeShell((output: string) => {
|
||||
cpuState.update((draft) => {
|
||||
const newFreq = isNormalInteger(output) ? parseInt(output, 10) : -1;
|
||||
@@ -167,10 +171,15 @@ export function devicePlugin(client: PluginClient<{}, {}>) {
|
||||
}
|
||||
return draft;
|
||||
});
|
||||
resolve();
|
||||
}, 'cat /sys/devices/system/cpu/cpu' + core + '/cpufreq/' + type);
|
||||
});
|
||||
};
|
||||
|
||||
const updateAvailableFrequencies = (core: number) => {
|
||||
const updateAvailableFrequencies: (core: number) => Promise<void> = (
|
||||
core: number,
|
||||
) => {
|
||||
return new Promise((resolve, _reject) => {
|
||||
executeShell((output: string) => {
|
||||
cpuState.update((draft) => {
|
||||
const freqs = output.split(' ').map((num: string) => {
|
||||
@@ -183,10 +192,15 @@ export function devicePlugin(client: PluginClient<{}, {}>) {
|
||||
}
|
||||
return draft;
|
||||
});
|
||||
resolve();
|
||||
}, 'cat /sys/devices/system/cpu/cpu' + core + '/cpufreq/scaling_available_frequencies');
|
||||
});
|
||||
};
|
||||
|
||||
const updateCoreGovernor = (core: number) => {
|
||||
const updateCoreGovernor: (core: number) => Promise<void> = (
|
||||
core: number,
|
||||
) => {
|
||||
return new Promise((resolve, _reject) => {
|
||||
executeShell((output: string) => {
|
||||
cpuState.update((draft) => {
|
||||
if (output.toLowerCase().includes('no such file')) {
|
||||
@@ -196,29 +210,35 @@ export function devicePlugin(client: PluginClient<{}, {}>) {
|
||||
}
|
||||
return draft;
|
||||
});
|
||||
resolve();
|
||||
}, 'cat /sys/devices/system/cpu/cpu' + core + '/cpufreq/scaling_governor');
|
||||
};
|
||||
|
||||
const readAvailableGovernors = (core: number) => {
|
||||
executeShell((output: string) => {
|
||||
cpuState.update((draft) => {
|
||||
draft.cpuFreq[core].scaling_available_governors = output.split(' ');
|
||||
return draft;
|
||||
});
|
||||
}, 'cat /sys/devices/system/cpu/cpu' + core + '/cpufreq/scaling_available_governors');
|
||||
};
|
||||
|
||||
const readCoreFrequency = (core: number) => {
|
||||
const readAvailableGovernors: (core: number) => Promise<string[]> = (
|
||||
core: number,
|
||||
) => {
|
||||
return new Promise((resolve, _reject) => {
|
||||
executeShell((output: string) => {
|
||||
// draft.cpuFreq[core].scaling_available_governors = output.split(' ');
|
||||
resolve(output.split(' '));
|
||||
}, 'cat /sys/devices/system/cpu/cpu' + core + '/cpufreq/scaling_available_governors');
|
||||
});
|
||||
};
|
||||
|
||||
const readCoreFrequency = async (core: number) => {
|
||||
const freq = cpuState.get().cpuFreq[core];
|
||||
const promises = [];
|
||||
if (freq.cpuinfo_max_freq < 0) {
|
||||
updateCoreFrequency(core, 'cpuinfo_max_freq');
|
||||
promises.push(updateCoreFrequency(core, 'cpuinfo_max_freq'));
|
||||
}
|
||||
if (freq.cpuinfo_min_freq < 0) {
|
||||
updateCoreFrequency(core, 'cpuinfo_min_freq');
|
||||
promises.push(updateCoreFrequency(core, 'cpuinfo_min_freq'));
|
||||
}
|
||||
updateCoreFrequency(core, 'scaling_cur_freq');
|
||||
updateCoreFrequency(core, 'scaling_min_freq');
|
||||
updateCoreFrequency(core, 'scaling_max_freq');
|
||||
promises.push(updateCoreFrequency(core, 'scaling_cur_freq'));
|
||||
promises.push(updateCoreFrequency(core, 'scaling_min_freq'));
|
||||
promises.push(updateCoreFrequency(core, 'scaling_max_freq'));
|
||||
return Promise.all(promises).then(() => {});
|
||||
};
|
||||
|
||||
const updateHardwareInfo = () => {
|
||||
@@ -313,17 +333,25 @@ export function devicePlugin(client: PluginClient<{}, {}>) {
|
||||
}
|
||||
|
||||
for (let i = 0; i < cpuState.get().cpuCount; ++i) {
|
||||
readAvailableGovernors(i);
|
||||
readAvailableGovernors(i).then((output) => {
|
||||
cpuState.update((draft) => {
|
||||
draft.cpuFreq[i].scaling_available_governors = output;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
intervalID = setInterval(() => {
|
||||
console.log('Starting task');
|
||||
const update = async () => {
|
||||
const promises = [];
|
||||
for (let i = 0; i < cpuState.get().cpuCount; ++i) {
|
||||
readCoreFrequency(i);
|
||||
updateCoreGovernor(i);
|
||||
updateAvailableFrequencies(i); // scaling max might change, so we also update this
|
||||
promises.push(readCoreFrequency(i));
|
||||
promises.push(updateCoreGovernor(i));
|
||||
promises.push(updateAvailableFrequencies(i)); // scaling max might change, so we also update this
|
||||
}
|
||||
}, 500);
|
||||
await Promise.all(promises);
|
||||
intervalID = setTimeout(update, 500);
|
||||
};
|
||||
|
||||
intervalID = setTimeout(update, 500);
|
||||
cpuState.update((draft) => {
|
||||
draft.monitoring = true;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user