示例#1
0
 /// <inheritdoc/>
 public Task <bool> OverclockAsync(CpuMaxFrequencyLevel cpuMaxFrequencyLevel)
 {
     this.logger.Debug("Application layer -> ControlPanelService -> OverclockAsync");
     return(this.onDemandService.OverclockAsync(cpuMaxFrequencyLevel));
 }
        /// <inheritdoc/>
        public async Task <bool> OverclockAsync(CpuMaxFrequencyLevel cpuMaxFrequencyLevel)
        {
            this.logger.Debug("Infra layer -> ControlPanelService -> OverclockAsync");

            var result = await BashCommands.CatBootConfig.BashAsync();

            this.logger.Trace($"Result of '{BashCommands.CatBootConfig}' command: '{result}'");
            var lines = result.Split(
                new[] { Environment.NewLine },
                StringSplitOptions.RemoveEmptyEntries);
            var frequencyLine      = lines.FirstOrDefault(line => line.Contains("arm_freq="));
            var frequencyLineRegex = new Regex(@"^(?<commented>#?)\s*arm_freq=(?<frequency>\d+)$");

            this.logger.Trace($"Frequency line in config file: '{frequencyLine}'");
            var frequencyLineGroups = frequencyLineRegex.Match(frequencyLine).Groups;
            var currentFrequency    = !string.IsNullOrEmpty(frequencyLineGroups["commented"].Value) ?
                                      1500 : int.Parse(frequencyLineGroups["frequency"].Value);

            if (currentFrequency == (int)cpuMaxFrequencyLevel)
            {
                this.logger.Info($"Frequency already set to {currentFrequency}, no need to restart");
                return(false);
            }

            var overVoltageLine = lines.FirstOrDefault(line => line.Contains("over_voltage="));

            if (string.IsNullOrEmpty(overVoltageLine))
            {
                this.logger.Info($"over_voltage configuration didn't exist, creating...");
                var createOverVoltageConfigCommand = string.Format(
                    BashCommands.SudoSedBootConfig,
                    frequencyLine,
                    $"#over_voltage=0\\n{frequencyLine}");
                result = await createOverVoltageConfigCommand.BashAsync();

                if (!string.IsNullOrEmpty(result))
                {
                    this.logger.Error($"Result of '{createOverVoltageConfigCommand}' command: '{result}', couldn't create over_voltage configuration");
                    throw new BusinessException("Couldn't create over_voltage configuration");
                }

                this.logger.Debug($"Result of '{createOverVoltageConfigCommand}' command is empty, success");
                overVoltageLine = "#over_voltage=0";
            }

            var overVoltage = 0;
            var frequency   = 1500;

            switch (cpuMaxFrequencyLevel)
            {
            case CpuMaxFrequencyLevel.Default:
                break;

            case CpuMaxFrequencyLevel.High:
                overVoltage = 2;
                frequency   = 1750;
                break;

            case CpuMaxFrequencyLevel.Maximum:
                overVoltage = 6;
                frequency   = 2000;
                break;

            default:
                throw new BusinessException($"Invalid value for cpu frequency level: {cpuMaxFrequencyLevel}");
            }

            var setOverVoltageConfigCommand = string.Format(
                BashCommands.SudoSedBootConfig,
                $"{overVoltageLine}",
                $"over_voltage={overVoltage}");

            result = await setOverVoltageConfigCommand.BashAsync();

            if (!string.IsNullOrEmpty(result))
            {
                this.logger.Error($"Result of '{setOverVoltageConfigCommand}' command: '{result}', couldn't set over_voltage configuration");
                throw new BusinessException("Couldn't set over_voltage configuration");
            }

            this.logger.Debug($"Result of '{setOverVoltageConfigCommand}' command is empty, success");

            var setFrequencyConfigCommand = string.Format(
                BashCommands.SudoSedBootConfig,
                $"{frequencyLine}",
                $"arm_freq={frequency}");

            result = await setFrequencyConfigCommand.BashAsync();

            if (!string.IsNullOrEmpty(result))
            {
                this.logger.Error($"Result of '{setFrequencyConfigCommand}' command: '{result}', couldn't set over_voltage configuration");
                throw new BusinessException("Couldn't set arm_freq configuration");
            }

            this.logger.Debug($"Result of '{setFrequencyConfigCommand}' command is empty, success");

            return(await this.RebootAsync());
        }