Skip to content

Commit

Permalink
cmd/ctr/commands/tasks: build metrics with target platform
Browse files Browse the repository at this point in the history
The windows container is using containerd/[email protected] for metrics
while the linux container is using containerd/[email protected]. They are
two different packages but the metric fields in proto share the same
name. For instance, the `Metrics` type:

* in [email protected], it is named by io.containerd.cgroups.v1.Metrics[1].
* in [email protected], it's also named by io.containerd.cgroups.v1.Metrics[2].

It's hard to tell the real type, even if both types are compatible.
According to typeurl/[email protected]'s behavior[3], it will check the type
registed by the `RegisterType` first. Let's see a case.

If the data is marshaled by containerd/[email protected] and the typeurl is
`io.containerd.cgroups.v1.Metrics`, ideally the receiver should use the
same type to unmarshal it. However, if the receiver has a dependency
using the [email protected], the [email protected] package will register the
type with `io.containerd.cgroups.v1.Metrics` by `gogo.RegisterType`. And
the receiver will [email protected]'s type to unmarshal the data. It's
compatible but it is unexpected because the receiver will use the
`[email protected]` to do the type assertion, like what nerdctl pr[4] does.
They will fail to do assertion.

Currently, the windows container metrics is using
containerd/[email protected], which impacts nerdctl to do release. So I
would like to file this pr to build metrics with target platform.

REF:
[1]: https://github.com/containerd/cgroups/blob/v1.1.0/stats/v1/metrics.pb.go#L705
[2]: https://github.com/containerd/cgroups/blob/v3.0.1/cgroup1/stats/metrics.pb.go#L1705
[3]: https://github.com/containerd/typeurl/blob/v2.1.0/types.go#L238-L264
[4]: containerd/nerdctl#1952

Signed-off-by: Wei Fu <[email protected]>
  • Loading branch information
fuweid committed Mar 8, 2023
1 parent 29e10a1 commit 52ab6f8
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 144 deletions.
145 changes: 1 addition & 144 deletions cmd/ctr/commands/tasks/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,11 @@
package tasks

import (
"encoding/json"
"errors"
"fmt"
"os"
"text/tabwriter"

wstats "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/stats"
v1 "github.com/containerd/cgroups/v3/cgroup1/stats"
v2 "github.com/containerd/cgroups/v3/cgroup2/stats"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/protobuf/proto"
"github.com/containerd/typeurl/v2"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -68,52 +61,7 @@ var metricsCommand = cli.Command{
if err != nil {
return err
}
anydata, err := typeurl.UnmarshalAny(metric.Data)
if err != nil {
return err
}
var (
data *v1.Metrics
data2 *v2.Metrics
windowsStats *wstats.Statistics
)
switch v := anydata.(type) {
case *v1.Metrics:
data = v
case *v2.Metrics:
data2 = v
case *wstats.Statistics:
windowsStats = v
default:
return errors.New("cannot convert metric data to cgroups.Metrics or windows.Statistics")
}

switch context.String(formatFlag) {
case formatTable:
w := tabwriter.NewWriter(os.Stdout, 1, 8, 4, ' ', 0)
fmt.Fprintf(w, "ID\tTIMESTAMP\t\n")
fmt.Fprintf(w, "%s\t%s\t\n\n", metric.ID, metric.Timestamp)
if data != nil {
printCgroupMetricsTable(w, data)
} else if data2 != nil {
printCgroup2MetricsTable(w, data2)
} else {
err := printWindowsStats(w, windowsStats)
if err != nil {
return fmt.Errorf("cannot convert metrics data from windows.Statistics: %w", err)
}
}
return w.Flush()
case formatJSON:
marshaledJSON, err := json.MarshalIndent(anydata, "", " ")
if err != nil {
return err
}
fmt.Println(string(marshaledJSON))
return nil
default:
return errors.New("format must be table or json")
}
return printTaskMetrics(context, metric)
},
}

Expand All @@ -133,94 +81,3 @@ func printCgroupMetricsTable(w *tabwriter.Writer, data *v1.Metrics) {
fmt.Fprintf(w, "pids.limit\t%v\t\n", data.Pids.Limit)
}
}

func printCgroup2MetricsTable(w *tabwriter.Writer, data *v2.Metrics) {
fmt.Fprintf(w, "METRIC\tVALUE\t\n")
if data.Pids != nil {
fmt.Fprintf(w, "pids.current\t%v\t\n", data.Pids.Current)
fmt.Fprintf(w, "pids.limit\t%v\t\n", data.Pids.Limit)
}
if data.CPU != nil {
fmt.Fprintf(w, "cpu.usage_usec\t%v\t\n", data.CPU.UsageUsec)
fmt.Fprintf(w, "cpu.user_usec\t%v\t\n", data.CPU.UserUsec)
fmt.Fprintf(w, "cpu.system_usec\t%v\t\n", data.CPU.SystemUsec)
fmt.Fprintf(w, "cpu.nr_periods\t%v\t\n", data.CPU.NrPeriods)
fmt.Fprintf(w, "cpu.nr_throttled\t%v\t\n", data.CPU.NrThrottled)
fmt.Fprintf(w, "cpu.throttled_usec\t%v\t\n", data.CPU.ThrottledUsec)
}
if data.Memory != nil {
fmt.Fprintf(w, "memory.usage\t%v\t\n", data.Memory.Usage)
fmt.Fprintf(w, "memory.usage_limit\t%v\t\n", data.Memory.UsageLimit)
fmt.Fprintf(w, "memory.swap_usage\t%v\t\n", data.Memory.SwapUsage)
fmt.Fprintf(w, "memory.swap_limit\t%v\t\n", data.Memory.SwapLimit)
}
}

func printWindowsStats(w *tabwriter.Writer, windowsStats *wstats.Statistics) error {
if windowsStats.GetLinux() != nil {
var stats v1.Metrics

// It cannot be casted to v1.Metrics since windowsStats is still generated by gogo/protobuf.
linux := windowsStats.GetLinux()

// But Marshal/Unmarshal works because the underlying protobuf message is compatible.
data, err := linux.Marshal()
if err != nil {
return err
}
err = proto.Unmarshal(data, &stats)
if err != nil {
return err
}

printCgroupMetricsTable(w, &stats)
} else if windowsStats.GetWindows() != nil {
printWindowsContainerStatistics(w, windowsStats.GetWindows())
}
// Print VM stats if its isolated
if windowsStats.VM != nil {
printWindowsVMStatistics(w, windowsStats.VM)
}
return nil
}

func printWindowsContainerStatistics(w *tabwriter.Writer, stats *wstats.WindowsContainerStatistics) {
fmt.Fprintf(w, "METRIC\tVALUE\t\n")
fmt.Fprintf(w, "timestamp\t%s\t\n", stats.Timestamp)
fmt.Fprintf(w, "start_time\t%s\t\n", stats.ContainerStartTime)
fmt.Fprintf(w, "uptime_ns\t%d\t\n", stats.UptimeNS)
if stats.Processor != nil {
fmt.Fprintf(w, "cpu.total_runtime_ns\t%d\t\n", stats.Processor.TotalRuntimeNS)
fmt.Fprintf(w, "cpu.runtime_user_ns\t%d\t\n", stats.Processor.RuntimeUserNS)
fmt.Fprintf(w, "cpu.runtime_kernel_ns\t%d\t\n", stats.Processor.RuntimeKernelNS)
}
if stats.Memory != nil {
fmt.Fprintf(w, "memory.commit_bytes\t%d\t\n", stats.Memory.MemoryUsageCommitBytes)
fmt.Fprintf(w, "memory.commit_peak_bytes\t%d\t\n", stats.Memory.MemoryUsageCommitPeakBytes)
fmt.Fprintf(w, "memory.private_working_set_bytes\t%d\t\n", stats.Memory.MemoryUsagePrivateWorkingSetBytes)
}
if stats.Storage != nil {
fmt.Fprintf(w, "storage.read_count_normalized\t%d\t\n", stats.Storage.ReadCountNormalized)
fmt.Fprintf(w, "storage.read_size_bytes\t%d\t\n", stats.Storage.ReadSizeBytes)
fmt.Fprintf(w, "storage.write_count_normalized\t%d\t\n", stats.Storage.WriteCountNormalized)
fmt.Fprintf(w, "storage.write_size_bytes\t%d\t\n", stats.Storage.WriteSizeBytes)
}
}

func printWindowsVMStatistics(w *tabwriter.Writer, stats *wstats.VirtualMachineStatistics) {
fmt.Fprintf(w, "METRIC\tVALUE\t\n")
if stats.Processor != nil {
fmt.Fprintf(w, "vm.cpu.total_runtime_ns\t%d\t\n", stats.Processor.TotalRuntimeNS)
}
if stats.Memory != nil {
fmt.Fprintf(w, "vm.memory.working_set_bytes\t%d\t\n", stats.Memory.WorkingSetBytes)
fmt.Fprintf(w, "vm.memory.virtual_node_count\t%d\t\n", stats.Memory.VirtualNodeCount)
fmt.Fprintf(w, "vm.memory.available\t%d\t\n", stats.Memory.VmMemory.AvailableMemory)
fmt.Fprintf(w, "vm.memory.available_buffer\t%d\t\n", stats.Memory.VmMemory.AvailableMemoryBuffer)
fmt.Fprintf(w, "vm.memory.reserved\t%d\t\n", stats.Memory.VmMemory.ReservedMemory)
fmt.Fprintf(w, "vm.memory.assigned\t%d\t\n", stats.Memory.VmMemory.AssignedMemory)
fmt.Fprintf(w, "vm.memory.slp_active\t%t\t\n", stats.Memory.VmMemory.SlpActive)
fmt.Fprintf(w, "vm.memory.balancing_enabled\t%t\t\n", stats.Memory.VmMemory.BalancingEnabled)
fmt.Fprintf(w, "vm.memory.dm_operation_in_progress\t%t\t\n", stats.Memory.VmMemory.DmOperationInProgress)
}
}
97 changes: 97 additions & 0 deletions cmd/ctr/commands/tasks/metrics_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
Copyright The containerd Authors.
Licensed 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.
*/

package tasks

import (
"encoding/json"
"errors"
"fmt"
"os"
"text/tabwriter"

v1 "github.com/containerd/cgroups/v3/cgroup1/stats"
v2 "github.com/containerd/cgroups/v3/cgroup2/stats"
"github.com/containerd/containerd/api/types"
"github.com/containerd/typeurl/v2"

"github.com/urfave/cli"
)

func printTaskMetrics(gocontext *cli.Context, metric *types.Metric) error {
anydata, err := typeurl.UnmarshalAny(metric.Data)
if err != nil {
return err
}

var (
data *v1.Metrics
data2 *v2.Metrics
)

switch v := anydata.(type) {
case *v1.Metrics:
data = v
case *v2.Metrics:
data2 = v
default:
return errors.New("cannot convert metric data to cgroups.Metrics")
}

switch gocontext.String(formatFlag) {
case formatTable:
w := tabwriter.NewWriter(os.Stdout, 1, 8, 4, ' ', 0)
fmt.Fprintf(w, "ID\tTIMESTAMP\t\n")
fmt.Fprintf(w, "%s\t%s\t\n\n", metric.ID, metric.Timestamp)
if data != nil {
printCgroupMetricsTable(w, data)
} else {
printCgroup2MetricsTable(w, data2)
}
return w.Flush()
case formatJSON:
marshaledJSON, err := json.MarshalIndent(anydata, "", " ")
if err != nil {
return err
}
fmt.Println(string(marshaledJSON))
return nil
default:
return errors.New("format must be table or json")
}
}

func printCgroup2MetricsTable(w *tabwriter.Writer, data *v2.Metrics) {
fmt.Fprintf(w, "METRIC\tVALUE\t\n")
if data.Pids != nil {
fmt.Fprintf(w, "pids.current\t%v\t\n", data.Pids.Current)
fmt.Fprintf(w, "pids.limit\t%v\t\n", data.Pids.Limit)
}
if data.CPU != nil {
fmt.Fprintf(w, "cpu.usage_usec\t%v\t\n", data.CPU.UsageUsec)
fmt.Fprintf(w, "cpu.user_usec\t%v\t\n", data.CPU.UserUsec)
fmt.Fprintf(w, "cpu.system_usec\t%v\t\n", data.CPU.SystemUsec)
fmt.Fprintf(w, "cpu.nr_periods\t%v\t\n", data.CPU.NrPeriods)
fmt.Fprintf(w, "cpu.nr_throttled\t%v\t\n", data.CPU.NrThrottled)
fmt.Fprintf(w, "cpu.throttled_usec\t%v\t\n", data.CPU.ThrottledUsec)
}
if data.Memory != nil {
fmt.Fprintf(w, "memory.usage\t%v\t\n", data.Memory.Usage)
fmt.Fprintf(w, "memory.usage_limit\t%v\t\n", data.Memory.UsageLimit)
fmt.Fprintf(w, "memory.swap_usage\t%v\t\n", data.Memory.SwapUsage)
fmt.Fprintf(w, "memory.swap_limit\t%v\t\n", data.Memory.SwapLimit)
}
}
29 changes: 29 additions & 0 deletions cmd/ctr/commands/tasks/metrics_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright The containerd Authors.
Licensed 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.
*/

package tasks

import (
"fmt"

"github.com/containerd/containerd/api/types"

"github.com/urfave/cli"
)

func printTaskMetrics(*cli.Context, *types.Metric) error {
return fmt.Errorf("unsupported platforms")
}
Loading

0 comments on commit 52ab6f8

Please sign in to comment.