-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This config backend uses ConfigJsonConfigBackend to update os.power and os.fan subfields under the "os" key, in order to set power and fan configs. The expected format for os.power and os.fan settings is: ``` { os: { power: { mode: string }, fan: { profile: string } } } ``` There may be other keys in os which are not managed by the Supervisor, so PowerFanConfig backend doesn't read or write to them. Extra keys in os.power and os.fan are ignored when getting boot config and removed when setting boot config. After this backend writes to config.json, host services os-power-mode and os-fan-profile pick up the changes, on reboot in the former's case and at runtime in the latter's case. The changes are applied by the host services, which the Supervisor does not manage aside from streaming their service logs to the dashboard. Change-type: minor Signed-off-by: Christina Ying Wang <christina@balena.io>
- Loading branch information
Showing
6 changed files
with
780 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import { isRight } from 'fp-ts/lib/Either'; | ||
import Reporter from 'io-ts-reporters'; | ||
import * as t from 'io-ts'; | ||
|
||
import { ConfigBackend } from './backend'; | ||
import type { ConfigOptions } from './backend'; | ||
import { schemaTypes } from '../schema-type'; | ||
import log from '../../lib/supervisor-console'; | ||
import * as constants from '../../lib/constants'; | ||
|
||
type ConfigJsonBackend = { | ||
get: (key: 'os') => Promise<unknown>; | ||
set: (opts: { os: Record<string, any> }) => Promise<void>; | ||
}; | ||
|
||
/** | ||
* A backend to handle Jetson power and fan control | ||
* | ||
* Supports: | ||
* - {BALENA|RESIN}_HOST_CONFIG_power_mode = "low" | "mid" | "high" | "default" |"$MODE_ID" | ||
* - {BALENA|RESIN}_HOST_CONFIG_fan_profile = "quiet" | "cool" | "default" |"$MODE_ID" | ||
*/ | ||
export class PowerFanConfig extends ConfigBackend { | ||
private static readonly CONFIGS = new Set(['power_mode', 'fan_profile']); | ||
private static readonly PREFIX = `${constants.hostConfigVarPrefix}CONFIG_`; | ||
private static readonly SCHEMA = t.exact( | ||
t.partial({ | ||
power: t.exact( | ||
t.partial({ | ||
mode: t.string, | ||
}), | ||
), | ||
fan: t.exact( | ||
t.partial({ | ||
profile: t.string, | ||
}), | ||
), | ||
}), | ||
); | ||
|
||
private readonly configJson: ConfigJsonBackend; | ||
public constructor(configJson: ConfigJsonBackend) { | ||
super(); | ||
this.configJson = configJson; | ||
} | ||
|
||
public static stripPrefix(name: string): string { | ||
if (!name.startsWith(PowerFanConfig.PREFIX)) { | ||
return name; | ||
} | ||
return name.substring(PowerFanConfig.PREFIX.length); | ||
} | ||
|
||
public async matches(deviceType: string): Promise<boolean> { | ||
// We only support Jetpack 6 devices for now, which includes all Orin devices | ||
// except for jetson-orin-nx-xv3 which is still on Jetpack 5 as of OS v5.1.36 | ||
return new Set([ | ||
'jetson-agx-orin-devkit', | ||
'jetson-agx-orin-devkit-64gb', | ||
'jetson-orin-nano-devkit-nvme', | ||
'jetson-orin-nano-seeed-j3010', | ||
'jetson-orin-nx-seeed-j4012', | ||
'jetson-orin-nx-xavier-nx-devkit', | ||
]).has(deviceType); | ||
} | ||
|
||
public async getBootConfig(): Promise<ConfigOptions> { | ||
// Get raw config.json contents | ||
let rawConf: unknown; | ||
try { | ||
rawConf = await this.configJson.get('os'); | ||
} catch (e: unknown) { | ||
log.error( | ||
`Failed to read config.json while getting power / fan configs: ${(e as Error).message ?? e}`, | ||
); | ||
return {}; | ||
} | ||
|
||
// Decode to power fan schema from object type, filtering out unrelated values | ||
const powerFanConfig = PowerFanConfig.SCHEMA.decode(rawConf); | ||
|
||
if (isRight(powerFanConfig)) { | ||
const conf = powerFanConfig.right; | ||
return { | ||
...(conf.power?.mode != null && { | ||
power_mode: conf.power.mode, | ||
}), | ||
...(conf.fan?.profile != null && { | ||
fan_profile: conf.fan.profile, | ||
}), | ||
}; | ||
} else { | ||
return {}; | ||
} | ||
} | ||
|
||
public async setBootConfig(opts: ConfigOptions): Promise<void> { | ||
// Read raw configs for "os" key from config.json | ||
let rawConf; | ||
try { | ||
rawConf = await this.configJson.get('os'); | ||
} catch (err: unknown) { | ||
log.error(`${(err as Error).message ?? err}`); | ||
return; | ||
} | ||
|
||
// Decode to "os" object type while leaving in unrelated values | ||
const maybeCurrentConf = schemaTypes.os.type.decode(rawConf); | ||
if (!isRight(maybeCurrentConf)) { | ||
log.error( | ||
'Failed to decode current os config:', | ||
Reporter.report(maybeCurrentConf), | ||
); | ||
return; | ||
} | ||
// Current config could be undefined if there's no os key in config.json, so default to empty object | ||
const conf = maybeCurrentConf.right ?? {}; | ||
|
||
// Update or delete power mode | ||
if ('power_mode' in opts) { | ||
conf.power = { | ||
mode: opts.power_mode, | ||
}; | ||
} else { | ||
delete conf?.power; | ||
} | ||
|
||
// Update or delete fan profile | ||
if ('fan_profile' in opts) { | ||
conf.fan = { | ||
profile: opts.fan_profile, | ||
}; | ||
} else { | ||
delete conf?.fan; | ||
} | ||
|
||
await this.configJson.set({ os: conf }); | ||
} | ||
|
||
public isSupportedConfig = (name: string): boolean => { | ||
return PowerFanConfig.CONFIGS.has(PowerFanConfig.stripPrefix(name)); | ||
}; | ||
|
||
public isBootConfigVar(envVar: string): boolean { | ||
return PowerFanConfig.CONFIGS.has(PowerFanConfig.stripPrefix(envVar)); | ||
} | ||
|
||
public processConfigVarName(envVar: string): string { | ||
return PowerFanConfig.stripPrefix(envVar).toLowerCase(); | ||
} | ||
|
||
public processConfigVarValue(_key: string, value: string): string { | ||
return value; | ||
} | ||
|
||
public createConfigVarName(name: string): string | null { | ||
return `${PowerFanConfig.PREFIX}${name}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.