DevinLeamy commented on code in PR #545: URL: https://github.com/apache/mesos/pull/545#discussion_r1548553875
########## src/slave/containerizer/mesos/isolators/cgroups2/controllers/cpu.cpp: ########## @@ -0,0 +1,164 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include <string> + +#include "linux/cgroups2.hpp" + +#include "slave/containerizer/mesos/isolators/cgroups2/constants.hpp" +#include "slave/containerizer/mesos/isolators/cgroups2/controllers/cpu.hpp" + +#include <process/id.hpp> +#include <process/owned.hpp> + +#include <stout/duration.hpp> +#include <stout/foreach.hpp> +#include <stout/nothing.hpp> +#include <stout/option.hpp> +#include <stout/try.hpp> + +#include "logging/logging.hpp" + +using std::string; + +using process::Failure; +using process::Future; +using process::Owned; + +using cgroups2::cpu::BandwidthLimit; + +namespace mesos { +namespace internal { +namespace slave { + +Try<Owned<ControllerProcess>> CpuControllerProcess::create(const Flags& flags) +{ + return Owned<ControllerProcess>(new CpuControllerProcess(flags)); +} + + +CpuControllerProcess::CpuControllerProcess(const Flags& _flags) + : ProcessBase(process::ID::generate("cgroups-v2-cpu-controller")), + ControllerProcess(_flags) {} + + +string CpuControllerProcess::name() const +{ + return CGROUPS_V2_CONTROLLER_CPU_NAME; +} + + +Future<Nothing> CpuControllerProcess::update( + const ContainerID& containerId, + const string& cgroup, + const Resources& resourceRequests, + const google::protobuf::Map<string, Value::Scalar>& resourceLimits) +{ + if (resourceRequests.cpus().isNone()) { + return Failure( + "Failed to update the 'cpu' controller: No cpu resources requested"); + } + + // Compute and update the CPU weight for this cgroup. Weight is the product of + // the requested number of CPUs and the pre-set weight per CPU. If the + // `revocable_cpu_low_priority` flag is set, less weight is given per cpu. + double cpus = *resourceRequests.cpus(); + bool revocable = resourceRequests.revocable().cpus().isSome(); + uint64_t weightPerCpu = revocable && flags.revocable_cpu_low_priority ? + CPU_SHARES_PER_CPU_REVOCABLE : CPU_SHARES_PER_CPU; + uint64_t weight = std::max( + static_cast<uint64_t>(weightPerCpu * cpus), MIN_CPU_SHARES); + + Try<Nothing> update = cgroups2::cpu::weight(cgroup, weight); + if (update.isError()) { + return Failure("Failed to update the weight: " + update.error()); + } + + Option<double> cpuLimit; + if (resourceLimits.count("cpus")) { + cpuLimit = resourceLimits.at("cpus").value(); + } + + // Set a maximum bandwidth per `CPU_CFS_PERIOD`, if a limit is requested. + // + // If `cpuLimit` is provided, the bandwidth is the product of the limit + // and the `CPU_CFS_PERIOD`. Otherwise, if the flag `cgroups_enable_cfs` + // is provided, it is the product of `cpus` and the `CPU_CFS_PERIOD`. + Option<BandwidthLimit> limit = [=, &cpuLimit] () -> Option<BandwidthLimit> { + if (cpuLimit.isSome()) { + if (std::isinf(*cpuLimit)) { + return BandwidthLimit(); + } + + uint64_t quota = static_cast<uint64_t>(*cpuLimit * CPU_CFS_PERIOD.us()); + return BandwidthLimit(CPU_CFS_PERIOD, Microseconds(quota)); + } + + if (flags.cgroups_enable_cfs) { + uint64_t quota = static_cast<uint64_t>(cpus * CPU_CFS_PERIOD.us()); + return BandwidthLimit(CPU_CFS_PERIOD, Microseconds(quota)); + } + + return None(); + }(); Review Comment: Updated. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
