diff --git a/completions/bash/crio b/completions/bash/crio index 41b4be05711..fdbf1ec1384 100755 --- a/completions/bash/crio +++ b/completions/bash/crio @@ -75,6 +75,7 @@ h --pinns-path --profile --profile-port +--rdt-config-file --read-only --registries-conf --registries-conf-dir diff --git a/completions/fish/crio.fish b/completions/fish/crio.fish index a9d3cc5703d..a621339b3ca 100644 --- a/completions/fish/crio.fish +++ b/completions/fish/crio.fish @@ -112,6 +112,7 @@ complete -c crio -n '__fish_crio_no_subcommand' -f -l pids-limit -r -d 'Maximum complete -c crio -n '__fish_crio_no_subcommand' -f -l pinns-path -r -d 'The path to find the pinns binary, which is needed to manage namespace lifecycle. Will be searched for in $PATH if empty (default: "")' complete -c crio -n '__fish_crio_no_subcommand' -f -l profile -d 'Enable pprof remote profiler on localhost:6060' complete -c crio -n '__fish_crio_no_subcommand' -f -l profile-port -r -d 'Port for the pprof profiler' +complete -c crio -n '__fish_crio_no_subcommand' -f -l rdt-config-file -r -d 'Path to the RDT configuration file for configuring the resctrl pseudo-filesystem' complete -c crio -n '__fish_crio_no_subcommand' -f -l read-only -d 'Setup all unprivileged containers to run as read-only. Automatically mounts tmpfs on `/run`, `/tmp` and `/var/tmp`. (default: false)' complete -c crio -n '__fish_crio_no_subcommand' -f -l registry -r -d 'Registry to be prepended when pulling unqualified images, can be specified multiple times' complete -c crio -n '__fish_crio_no_subcommand' -l root -s r -r -d 'The CRI-O root directory' diff --git a/completions/zsh/_crio b/completions/zsh/_crio index 3bd16ddfb1b..cc247ced5a5 100644 --- a/completions/zsh/_crio +++ b/completions/zsh/_crio @@ -7,7 +7,7 @@ it later with **--config**. Global options will modify the output.' 'version:dis _describe 'commands' cmds local -a opts - opts=('--absent-mount-sources-to-reject' '--additional-devices' '--address' '--apparmor-profile' '--big-files-temporary-dir' '--bind-mount-prefix' '--cgroup-manager' '--clean-shutdown-file' '--cni-config-dir' '--cni-default-network' '--cni-plugin-dir' '--config' '--config-dir' '--conmon' '--conmon-cgroup' '--conmon-env' '--container-attach-socket-dir' '--container-exits-dir' '--ctr-stop-timeout' '--decryption-keys-path' '--default-capabilities' '--default-env' '--default-mounts-file' '--default-runtime' '--default-sysctls' '--default-transport' '--default-ulimits' '--device-ownership-from-security-context' '--drop-infra-ctr' '--enable-metrics' '--enable-profile-unix-socket' '--gid-mappings' '--global-auth-file' '--grpc-max-recv-msg-size' '--grpc-max-send-msg-size' '--hooks-dir' '--image-volumes' '--infra-ctr-cpuset' '--insecure-registry' '--internal-wipe' '--irqbalance-config-file' '--listen' '--log' '--log-dir' '--log-filter' '--log-format' '--log-journald' '--log-level' '--log-size-max' '--metrics-cert' '--metrics-collectors' '--metrics-key' '--metrics-port' '--metrics-socket' '--namespaces-dir' '--no-pivot' '--pause-command' '--pause-image' '--pause-image-auth-file' '--pids-limit' '--pinns-path' '--profile' '--profile-port' '--read-only' '--registries-conf' '--registries-conf-dir' '--registry' '--root' '--runroot' '--runtimes' '--seccomp-profile' '--seccomp-use-default-when-empty' '--selinux' '--separate-pull-cgroup' '--signature-policy' '--storage-driver' '--storage-opt' '--stream-address' '--stream-enable-tls' '--stream-idle-timeout' '--stream-port' '--stream-tls-ca' '--stream-tls-cert' '--stream-tls-key' '--uid-mappings' '--version-file' '--version-file-persist' '--help' '--version') + opts=('--absent-mount-sources-to-reject' '--additional-devices' '--address' '--apparmor-profile' '--big-files-temporary-dir' '--bind-mount-prefix' '--cgroup-manager' '--clean-shutdown-file' '--cni-config-dir' '--cni-default-network' '--cni-plugin-dir' '--config' '--config-dir' '--conmon' '--conmon-cgroup' '--conmon-env' '--container-attach-socket-dir' '--container-exits-dir' '--ctr-stop-timeout' '--decryption-keys-path' '--default-capabilities' '--default-env' '--default-mounts-file' '--default-runtime' '--default-sysctls' '--default-transport' '--default-ulimits' '--device-ownership-from-security-context' '--drop-infra-ctr' '--enable-metrics' '--enable-profile-unix-socket' '--gid-mappings' '--global-auth-file' '--grpc-max-recv-msg-size' '--grpc-max-send-msg-size' '--hooks-dir' '--image-volumes' '--infra-ctr-cpuset' '--insecure-registry' '--internal-wipe' '--irqbalance-config-file' '--listen' '--log' '--log-dir' '--log-filter' '--log-format' '--log-journald' '--log-level' '--log-size-max' '--metrics-cert' '--metrics-collectors' '--metrics-key' '--metrics-port' '--metrics-socket' '--namespaces-dir' '--no-pivot' '--pause-command' '--pause-image' '--pause-image-auth-file' '--pids-limit' '--pinns-path' '--profile' '--profile-port' '--rdt-config-file' '--read-only' '--registries-conf' '--registries-conf-dir' '--registry' '--root' '--runroot' '--runtimes' '--seccomp-profile' '--seccomp-use-default-when-empty' '--selinux' '--separate-pull-cgroup' '--signature-policy' '--storage-driver' '--storage-opt' '--stream-address' '--stream-enable-tls' '--stream-idle-timeout' '--stream-port' '--stream-tls-ca' '--stream-tls-cert' '--stream-tls-key' '--uid-mappings' '--version-file' '--version-file-persist' '--help' '--version') _describe 'global options' opts return diff --git a/docs/crio.8.md b/docs/crio.8.md index fd3bcc0c7f7..50d47b8f668 100644 --- a/docs/crio.8.md +++ b/docs/crio.8.md @@ -74,6 +74,7 @@ crio [--pinns-path]=[value] [--profile-port]=[value] [--profile] +[--rdt-config-file]=[value] [--read-only] [--registry]=[value] [--root|-r]=[value] @@ -290,6 +291,8 @@ crio [GLOBAL OPTIONS] command [COMMAND OPTIONS] [ARGUMENTS...] **--profile-port**="": Port for the pprof profiler (default: 6060) +**--rdt-config-file**="": Path to the RDT configuration file for configuring the resctrl pseudo-filesystem + **--read-only**: Setup all unprivileged containers to run as read-only. Automatically mounts tmpfs on `/run`, `/tmp` and `/var/tmp`. (default: false) **--registry**="": Registry to be prepended when pulling unqualified images, can be specified multiple times (default: []) diff --git a/docs/crio.conf.5.md b/docs/crio.conf.5.md index 0bca086dff9..960f8999d9f 100644 --- a/docs/crio.conf.5.md +++ b/docs/crio.conf.5.md @@ -141,6 +141,9 @@ the container runtime configuration. Used to change irqbalance service config file which is used by CRI-O. For CentOS/SUSE, this file is located at /etc/sysconfig/irqbalance. For Ubuntu, this file is located at /etc/default/irqbalance. +**rdt_config_file**="" + Path to the RDT configuration file for configuring the resctrl pseudo-filesystem. + **cgroup_manager**="systemd" Cgroup management implementation used for the runtime. diff --git a/go.mod b/go.mod index 4ecca7302db..4fd9a1d7f11 100644 --- a/go.mod +++ b/go.mod @@ -37,6 +37,7 @@ require ( github.com/google/renameio v1.0.1 github.com/google/uuid v1.3.0 github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 + github.com/intel/goresctrl v0.0.0-20210623080121-be5fa857f4b4 github.com/json-iterator/go v1.1.11 github.com/onsi/ginkgo v1.16.4 github.com/onsi/gomega v1.14.0 @@ -55,7 +56,7 @@ require ( github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 github.com/urfave/cli/v2 v2.3.0 github.com/vishvananda/netlink v1.1.1-0.20201029203352-d40f9887b852 - golang.org/x/net v0.0.0-20210520170846-37e1c6afe023 + golang.org/x/net v0.0.0-20210525063256-abc453219eb5 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c golang.org/x/sys v0.0.0-20210616094352-59db8d763f22 google.golang.org/grpc v1.39.0 @@ -69,6 +70,7 @@ require ( k8s.io/utils v0.0.0-20210707171843-4b05e18ac7d9 mvdan.cc/sh/v3 v3.3.0 sigs.k8s.io/release-utils v0.3.0 + sigs.k8s.io/yaml v1.2.0 sigs.k8s.io/zeitgeist v0.3.0 ) diff --git a/go.sum b/go.sum index 0cc3c3cc611..911ec295cd6 100644 --- a/go.sum +++ b/go.sum @@ -626,8 +626,9 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-containerregistry v0.3.0/go.mod h1:BJ7VxR1hAhdiZBGGnvGETHEmFs1hzXc4VM1xjOPO9wA= github.com/google/go-github/v33 v33.0.0 h1:qAf9yP0qc54ufQxzwv+u9H0tiVOnPJxo0lI/JXqw3ZM= github.com/google/go-github/v33 v33.0.0/go.mod h1:GMdDnVZY/2TsWgp/lkYnpSAh6TrzhANBBwm6k6TTEXg= @@ -636,8 +637,9 @@ github.com/google/go-intervals v0.0.2/go.mod h1:MkaR3LNRfeKLPmqgJYs4E66z5InYjmCj github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/google/gofuzz v1.1.0 h1:Hsa8mG0dQ46ij8Sl2AYJDUv1oA9/d6Vk+3LG99Oe02g= github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= +github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/licenseclassifier/v2 v2.0.0-alpha.1/go.mod h1:YAgBGGTeNDMU+WfIgaFvjZe4rudym4f6nIn8ZH5X+VM= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= @@ -694,8 +696,9 @@ github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFb github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q= github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8= github.com/hashicorp/errwrap v0.0.0-20141028054710-7554cd9344ce/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= -github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= +github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM= github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI= @@ -740,6 +743,8 @@ github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/insomniacslk/dhcp v0.0.0-20210120172423-cc9239ac6294/go.mod h1:TKl4jN3Voofo4UJIicyNhWGp/nlQqQkFxmwIFTvBkKI= +github.com/intel/goresctrl v0.0.0-20210623080121-be5fa857f4b4 h1:p1oaKfbi31/qnFP0SLgeo8gN4/gpG3/MLqhh7/4IbGQ= +github.com/intel/goresctrl v0.0.0-20210623080121-be5fa857f4b4/go.mod h1:VyYDHANYKMmWY0vM+OL91NLPrCbcG+AKeSJSsU+kzVw= github.com/ishidawataru/sctp v0.0.0-20190723014705-7c296d48a2b5/go.mod h1:DM4VvS+hD/kDi1U1QsX2fnZowwBhqD0Dk3bRPKF/Oc8= github.com/ishidawataru/sctp v0.0.0-20210226210310-f2269e66cdee h1:PAXLXk1heNZ5yokbMBpVLZQxo43wCZxRwl00mX+dd44= github.com/ishidawataru/sctp v0.0.0-20210226210310-f2269e66cdee/go.mod h1:co9pwDoBCm1kGxawmb4sPq0cSIOOWNPT4KnHotMP1Zg= @@ -941,7 +946,6 @@ github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+ github.com/nakabonne/nestif v0.3.0/go.mod h1:dI314BppzXjJ4HsCnbo7XzrJHPszZsjnk5wEBSYHI2c= github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d/go.mod h1:o96djdrsSGy3AWPyBgZMAGfxZNfgntdJG+11KU4QvbU= github.com/ncw/swift v1.0.47/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM= -github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/nozzle/throttler v0.0.0-20180817012639-2ea982251481 h1:Up6+btDp321ZG5/zdSLo48H9Iaq0UQGthrhWC6pCxzE= github.com/nozzle/throttler v0.0.0-20180817012639-2ea982251481/go.mod h1:yKZQO8QE2bHlgozqWDiRVqTFlLQSj30K/6SAK8EeYFw= @@ -1061,8 +1065,9 @@ github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y8 github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= github.com/prometheus/common v0.6.0/go.mod h1:eBmuwkDJBwy6iBfxCBob6t6dR6ENT/y+J+Zk0j9GMYc= github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo= -github.com/prometheus/common v0.26.0 h1:iMAkS2TDoNWnKM+Kopnx/8tnEStIfpYA0ur0xQzzhMQ= github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc= +github.com/prometheus/common v0.29.0 h1:3jqPBvKT4OHAbje2Ql7KeaaSicDBCxMYwEJU1zRJceE= +github.com/prometheus/common v0.29.0/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls= github.com/prometheus/procfs v0.0.0-20180125133057-cb4147076ac7/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= @@ -1434,8 +1439,9 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20210331212208-0fccb6fa2b5c/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= -golang.org/x/net v0.0.0-20210520170846-37e1c6afe023 h1:ADo5wSpq2gqaCGQWzk7S5vd//0iyyLeAratkEoG5dLE= golang.org/x/net v0.0.0-20210520170846-37e1c6afe023/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20210525063256-abc453219eb5 h1:wjuX4b5yYQnEQHzd+CBcrcC6OVR2J1CN6mUy0oSxIPo= +golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -1445,8 +1451,9 @@ golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4Iltr golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= -golang.org/x/oauth2 v0.0.0-20210112200429-01de73cf58bd h1:0n2rzLq6xLtV9OFaT0BF2syUkjOwRrJ1zvXY5hH7Kkc= golang.org/x/oauth2 v0.0.0-20210112200429-01de73cf58bd/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= +golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c h1:pkQiBZBvdos9qq4wBAHqlzuZHEXo07pqV06ef90u1WI= +golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -1758,8 +1765,9 @@ gopkg.in/check.v1 v1.0.0-20141024133853-64131543e789/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b h1:QRR6H1YWRnHb4Y/HeNFCTJLFVxaq6wH4YuVdsUOr75U= gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0 h1:0vLT13EuvQ0hNvakwLuFZ/jYrLp5F3kcWHXdRggjCE8= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= diff --git a/internal/config/rdt/rdt.go b/internal/config/rdt/rdt.go new file mode 100644 index 00000000000..d9a439cc202 --- /dev/null +++ b/internal/config/rdt/rdt.go @@ -0,0 +1,106 @@ +package rdt + +import ( + "fmt" + "io/ioutil" + + "github.com/pkg/errors" + "github.com/sirupsen/logrus" + "sigs.k8s.io/yaml" + + "github.com/intel/goresctrl/pkg/rdt" +) + +const ( + // DefaultRdtConfigFile is the default value for RDT config file path + DefaultRdtConfigFile = "" + // ResctrlPrefix is the prefix used for class/closid directories under the resctrl filesystem + ResctrlPrefix = "" +) + +type Config struct { + supported bool + enabled bool + config *rdt.Config +} + +// New creates a new RDT config instance +func New() *Config { + c := &Config{ + supported: true, + config: &rdt.Config{}, + } + + rdt.SetLogger(logrus.StandardLogger()) + + if err := rdt.Initialize(ResctrlPrefix); err != nil { + logrus.Infof("RDT is not enabled: %v", err) + c.supported = false + } + return c +} + +// Supported returns true if RDT is enabled in the host system +func (c *Config) Supported() bool { + return c.supported +} + +// Enabled returns true if RDT is enabled in CRI-O +func (c *Config) Enabled() bool { + return c.enabled +} + +// Load loads and validates RDT config +func (c *Config) Load(path string) error { + c.enabled = false + + if !c.Supported() { + logrus.Info("RDT not available in the host system") + return nil + } + + if path == "" { + logrus.Info("No RDT config file specified, RDT not enabled") + return nil + } + + tmpCfg, err := loadConfigFile(path) + if err != nil { + return err + } + + if err := rdt.SetConfig(tmpCfg, true); err != nil { + return errors.Wrap(err, "configuring RDT failed") + } + + logrus.Infof("RDT enabled, config successfully loaded from %q", path) + c.enabled = true + c.config = tmpCfg + + return nil +} + +func loadConfigFile(path string) (*rdt.Config, error) { + data, err := ioutil.ReadFile(path) + if err != nil { + return nil, errors.Wrap(err, "reading rdt config file failed") + } + + c := &rdt.Config{} + if err = yaml.Unmarshal(data, c); err != nil { + return nil, errors.Wrap(err, "parsing RDT config failed") + } + + return c, nil +} + +func (c *Config) ContainerClassFromAnnotations(containerName string, containerAnnotations, podAnnotations map[string]string) (string, error) { + cls, err := rdt.ContainerClassFromAnnotations(containerName, containerAnnotations, podAnnotations) + if err != nil { + return "", err + } + if cls != "" && !c.Enabled() { + return "", fmt.Errorf("RDT disabled, refusing to set RDT class of container %q to %q", containerName, cls) + } + return cls, nil +} diff --git a/internal/config/rdt/rdt_test.go b/internal/config/rdt/rdt_test.go new file mode 100644 index 00000000000..b8145d48f8a --- /dev/null +++ b/internal/config/rdt/rdt_test.go @@ -0,0 +1,47 @@ +package rdt + +import ( + "io/ioutil" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func tempFileWithData(data string) string { + f := t.MustTempFile("") + Expect(ioutil.WriteFile(f, []byte(data), 0o644)).To(BeNil()) + return f +} + +// The actual test suite +var _ = t.Describe("When parsing RDT config file", func() { + t.Describe("non-existent file", func() { + It("should return an error", func() { + _, err := loadConfigFile("non-existent-file") + Expect(err).NotTo(BeNil()) + }) + }) + + t.Describe("invalid file format", func() { + It("should return an error", func() { + f := tempFileWithData(`partitions: +- foo +`) + _, err := loadConfigFile(f) + Expect(err).NotTo(BeNil()) + }) + }) + + t.Describe("correct file format", func() { + It("should not return an error", func() { + f := tempFileWithData(`partitions: + default: + l3Allocation: 100% + classes: + default: +`) + _, err := loadConfigFile(f) + Expect(err).To(BeNil()) + }) + }) +}) diff --git a/internal/config/rdt/suite_test.go b/internal/config/rdt/suite_test.go new file mode 100644 index 00000000000..cec6116f169 --- /dev/null +++ b/internal/config/rdt/suite_test.go @@ -0,0 +1,26 @@ +package rdt + +import ( + "testing" + + . "github.com/cri-o/cri-o/test/framework" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +// TestLib runs the created specs +func TestLibConfig(t *testing.T) { + RegisterFailHandler(Fail) + RunFrameworkSpecs(t, "RdtConfig") +} + +var t *TestFramework + +var _ = BeforeSuite(func() { + t = NewTestFramework(NilFunc, NilFunc) + t.Setup() +}) + +var _ = AfterSuite(func() { + t.Teardown() +}) diff --git a/internal/criocli/criocli.go b/internal/criocli/criocli.go index 004bbfb4863..7eef5acfc98 100644 --- a/internal/criocli/criocli.go +++ b/internal/criocli/criocli.go @@ -180,6 +180,9 @@ func mergeConfig(config *libconfig.Config, ctx *cli.Context) error { if ctx.IsSet("irqbalance-config-file") { config.IrqBalanceConfigFile = ctx.String("irqbalance-config-file") } + if ctx.IsSet("rdt-config-file") { + config.RdtConfigFile = ctx.String("rdt-config-file") + } if ctx.IsSet("cgroup-manager") { config.CgroupManagerName = ctx.String("cgroup-manager") } @@ -575,6 +578,11 @@ func getCrioFlags(defConf *libconfig.Config) []cli.Flag { Usage: "The irqbalance service config file which is used by CRI-O.", Value: defConf.IrqBalanceConfigFile, }, + &cli.StringFlag{ + Name: "rdt-config-file", + Usage: "Path to the RDT configuration file for configuring the resctrl pseudo-filesystem", + Value: defConf.RdtConfigFile, + }, &cli.BoolFlag{ Name: "selinux", Usage: fmt.Sprintf("Enable selinux support (default: %t)", defConf.SELinux), diff --git a/pkg/annotations/annotations.go b/pkg/annotations/annotations.go index da741211b56..99a6df8296a 100644 --- a/pkg/annotations/annotations.go +++ b/pkg/annotations/annotations.go @@ -1,5 +1,9 @@ package annotations +import ( + "github.com/intel/goresctrl/pkg/rdt" +) + const ( // UsernsMode is the user namespace mode to use UsernsModeAnnotation = "io.kubernetes.cri-o.userns-mode" @@ -38,4 +42,5 @@ var AllAllowedAnnotations = []string{ CPUQuotaAnnotation, IRQLoadBalancingAnnotation, OCISeccompBPFHookAnnotation, + rdt.RdtContainerAnnotation, } diff --git a/pkg/config/config.go b/pkg/config/config.go index 12ad119a837..b720460ba24 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -26,6 +26,7 @@ import ( "github.com/cri-o/cri-o/internal/config/device" "github.com/cri-o/cri-o/internal/config/node" "github.com/cri-o/cri-o/internal/config/nsmgr" + "github.com/cri-o/cri-o/internal/config/rdt" "github.com/cri-o/cri-o/internal/config/seccomp" "github.com/cri-o/cri-o/internal/config/ulimits" "github.com/cri-o/cri-o/pkg/annotations" @@ -267,6 +268,9 @@ type RuntimeConfig struct { // for configuring irqbalance daemon. IrqBalanceConfigFile string `toml:"irqbalance_config_file"` + // RdtConfigFile is the RDT config file used for configuring resctrl fs + RdtConfigFile string `toml:"rdt_config_file"` + // CgroupManagerName is the manager implementation name which is used to // handle cgroups for containers. CgroupManagerName string `toml:"cgroup_manager"` @@ -350,6 +354,9 @@ type RuntimeConfig struct { // apparmorConfig is the internal AppArmor configuration apparmorConfig *apparmor.Config + // rdtConfig is the internal Rdt configuration + rdtConfig *rdt.Config + // ulimitConfig is the internal ulimit configuration ulimitsConfig *ulimits.Config @@ -662,6 +669,7 @@ func DefaultConfig() (*Config, error) { SELinux: selinuxEnabled(), ApparmorProfile: apparmor.DefaultProfile, IrqBalanceConfigFile: DefaultIrqBalanceConfigFile, + RdtConfigFile: rdt.DefaultRdtConfigFile, CgroupManagerName: cgroupManager.Name(), PidsLimit: DefaultPidsLimit, ContainerExitsDir: containerExitsDir, @@ -675,6 +683,7 @@ func DefaultConfig() (*Config, error) { DropInfraCtr: true, seccompConfig: seccomp.New(), apparmorConfig: apparmor.New(), + rdtConfig: rdt.New(), ulimitsConfig: ulimits.New(), cgroupManager: cgroupManager, deviceConfig: device.New(), @@ -940,6 +949,11 @@ func (c *RuntimeConfig) Validate(systemContext *types.SystemContext, onExecution if err := c.apparmorConfig.LoadProfile(c.ApparmorProfile); err != nil { return errors.Wrap(err, "unable to load AppArmor profile") } + + if err := c.rdtConfig.Load(c.RdtConfigFile); err != nil { + return errors.Wrap(err, "rdt configuration") + } + cgroupManager, err := cgmgr.SetCgroupManager(c.CgroupManagerName) if err != nil { return errors.Wrap(err, "unable to update cgroup manager") @@ -1012,6 +1026,11 @@ func (c *RuntimeConfig) AppArmor() *apparmor.Config { return c.apparmorConfig } +// Rdt returns the RDT configuration +func (c *RuntimeConfig) Rdt() *rdt.Config { + return c.rdtConfig +} + // CgroupManager returns the CgroupManager configuration func (c *RuntimeConfig) CgroupManager() cgmgr.CgroupManager { return c.cgroupManager diff --git a/pkg/config/reload.go b/pkg/config/reload.go index 88a9eee7a8d..fb73c750c25 100644 --- a/pkg/config/reload.go +++ b/pkg/config/reload.go @@ -81,6 +81,9 @@ func (c *Config) Reload() error { if err := c.ReloadAppArmorProfile(newConfig); err != nil { return err } + if err := c.ReloadRdtConfig(newConfig); err != nil { + return err + } return nil } @@ -197,3 +200,15 @@ func (c *Config) ReloadAppArmorProfile(newConfig *Config) error { } return nil } + +// ReloadRdtConfig reloads the RDT configuration if changed +func (c *Config) ReloadRdtConfig(newConfig *Config) error { + if c.RdtConfigFile != newConfig.RdtConfigFile { + if err := c.Rdt().Load(newConfig.RdtConfigFile); err != nil { + return errors.Wrap(err, "unable to reload rdt_config_file") + } + c.RdtConfigFile = newConfig.RdtConfigFile + logConfig("rdt_config_file", c.RdtConfigFile) + } + return nil +} diff --git a/pkg/config/template.go b/pkg/config/template.go index cc5392a1553..64254fe026c 100644 --- a/pkg/config/template.go +++ b/pkg/config/template.go @@ -245,6 +245,11 @@ func initCrioTemplateConfig(c *Config) ([]*templateConfigValue, error) { group: crioRuntimeConfig, isDefaultValue: simpleEqual(dc.IrqBalanceConfigFile, c.IrqBalanceConfigFile), }, + { + templateString: templateStringCrioRuntimeRdtConfigFile, + group: crioRuntimeConfig, + isDefaultValue: simpleEqual(dc.RdtConfigFile, c.RdtConfigFile), + }, { templateString: templateStringCrioRuntimeCgroupManager, group: crioRuntimeConfig, @@ -766,6 +771,12 @@ irqbalance_config_file = "{{ .IrqBalanceConfigFile }}" ` +const templateStringCrioRuntimeRdtConfigFile = `# Path to the RDT configuration file for configuring the resctrl pseudo-filesystem. +# This option supports live configuration reload. +rdt_config_file = "{{ .RdtConfigFile }}" + +` + const templateStringCrioRuntimeCgroupManager = `# Cgroup management implementation used for the runtime. cgroup_manager = "{{ .CgroupManagerName }}" @@ -980,6 +991,7 @@ const templateStringCrioRuntimeRuntimesRuntimeHandler = `# The "crio.runtime.run # "io.kubernetes.cri-o.ShmSize" for configuring the size of /dev/shm. # "io.kubernetes.cri-o.UnifiedCgroup.$CTR_NAME" for configuring the cgroup v2 unified block for a container. # "io.containers.trace-syscall" for tracing syscalls via the OCI seccomp BPF hook. +# "io.kubernetes.cri.rdt-class" for setting the RDT class of a container {{ range $runtime_name, $runtime_handler := .Runtimes }} [crio.runtime.runtimes.{{ $runtime_name }}] diff --git a/server/container_create_linux.go b/server/container_create_linux.go index 323db6bda24..7cd03baffac 100644 --- a/server/container_create_linux.go +++ b/server/container_create_linux.go @@ -21,6 +21,7 @@ import ( "github.com/cri-o/cri-o/internal/config/cgmgr" "github.com/cri-o/cri-o/internal/config/device" "github.com/cri-o/cri-o/internal/config/node" + "github.com/cri-o/cri-o/internal/config/rdt" "github.com/cri-o/cri-o/internal/lib/sandbox" "github.com/cri-o/cri-o/internal/log" oci "github.com/cri-o/cri-o/internal/oci" @@ -575,6 +576,17 @@ func (s *Server) createSandboxContainer(ctx context.Context, ctr ctrIface.Contai return nil, err } + // Get RDT class + rdtClass, err := s.Config().Rdt().ContainerClassFromAnnotations(metadata.Name, containerConfig.Annotations, sb.Annotations()) + if err != nil { + return nil, err + } + if rdtClass != "" { + log.Debugf(ctx, "Setting RDT ClosID of container %s to %q", containerID, rdt.ResctrlPrefix+rdtClass) + // TODO: patch runtime-tools to support setting ClosID via a helper func similar to SetLinuxIntelRdtL3CacheSchema() + specgen.Config.Linux.IntelRdt = &rspec.LinuxIntelRdt{ClosID: rdt.ResctrlPrefix + rdtClass} + } + err = ctr.SpecAddAnnotations(ctx, sb, containerVolumes, mountPoint, containerImageConfig.Config.StopSignal, imgResult, s.config.CgroupManager().IsSystemd(), node.SystemdHasCollectMode()) if err != nil { return nil, err diff --git a/vendor/github.com/google/go-cmp/cmp/path.go b/vendor/github.com/google/go-cmp/cmp/path.go index 3d45c1a47f2..f01eff318c5 100644 --- a/vendor/github.com/google/go-cmp/cmp/path.go +++ b/vendor/github.com/google/go-cmp/cmp/path.go @@ -315,7 +315,7 @@ func (tf Transform) Option() Option { return tf.trans } // pops the address from the stack. Thus, when traversing into a pointer from // reflect.Ptr, reflect.Slice element, or reflect.Map, we can detect cycles // by checking whether the pointer has already been visited. The cycle detection -// uses a seperate stack for the x and y values. +// uses a separate stack for the x and y values. // // If a cycle is detected we need to determine whether the two pointers // should be considered equal. The definition of equality chosen by Equal diff --git a/vendor/github.com/google/go-cmp/cmp/report_slices.go b/vendor/github.com/google/go-cmp/cmp/report_slices.go index 168f92f3c12..2ad3bc85ba8 100644 --- a/vendor/github.com/google/go-cmp/cmp/report_slices.go +++ b/vendor/github.com/google/go-cmp/cmp/report_slices.go @@ -7,6 +7,7 @@ package cmp import ( "bytes" "fmt" + "math" "reflect" "strconv" "strings" @@ -96,15 +97,16 @@ func (opts formatOptions) FormatDiffSlice(v *valueNode) textNode { } // Auto-detect the type of the data. - var isLinedText, isText, isBinary bool var sx, sy string + var ssx, ssy []string + var isString, isMostlyText, isPureLinedText, isBinary bool switch { case t.Kind() == reflect.String: sx, sy = vx.String(), vy.String() - isText = true // Initial estimate, verify later + isString = true case t.Kind() == reflect.Slice && t.Elem() == reflect.TypeOf(byte(0)): sx, sy = string(vx.Bytes()), string(vy.Bytes()) - isBinary = true // Initial estimate, verify later + isString = true case t.Kind() == reflect.Array: // Arrays need to be addressable for slice operations to work. vx2, vy2 := reflect.New(t).Elem(), reflect.New(t).Elem() @@ -112,13 +114,12 @@ func (opts formatOptions) FormatDiffSlice(v *valueNode) textNode { vy2.Set(vy) vx, vy = vx2, vy2 } - if isText || isBinary { - var numLines, lastLineIdx, maxLineLen int - isBinary = !utf8.ValidString(sx) || !utf8.ValidString(sy) + if isString { + var numTotalRunes, numValidRunes, numLines, lastLineIdx, maxLineLen int for i, r := range sx + sy { - if !(unicode.IsPrint(r) || unicode.IsSpace(r)) || r == utf8.RuneError { - isBinary = true - break + numTotalRunes++ + if (unicode.IsPrint(r) || unicode.IsSpace(r)) && r != utf8.RuneError { + numValidRunes++ } if r == '\n' { if maxLineLen < i-lastLineIdx { @@ -128,8 +129,26 @@ func (opts formatOptions) FormatDiffSlice(v *valueNode) textNode { numLines++ } } - isText = !isBinary - isLinedText = isText && numLines >= 4 && maxLineLen <= 1024 + isPureText := numValidRunes == numTotalRunes + isMostlyText = float64(numValidRunes) > math.Floor(0.90*float64(numTotalRunes)) + isPureLinedText = isPureText && numLines >= 4 && maxLineLen <= 1024 + isBinary = !isMostlyText + + // Avoid diffing by lines if it produces a significantly more complex + // edit script than diffing by bytes. + if isPureLinedText { + ssx = strings.Split(sx, "\n") + ssy = strings.Split(sy, "\n") + esLines := diff.Difference(len(ssx), len(ssy), func(ix, iy int) diff.Result { + return diff.BoolResult(ssx[ix] == ssy[iy]) + }) + esBytes := diff.Difference(len(sx), len(sy), func(ix, iy int) diff.Result { + return diff.BoolResult(sx[ix] == sy[iy]) + }) + efficiencyLines := float64(esLines.Dist()) / float64(len(esLines)) + efficiencyBytes := float64(esBytes.Dist()) / float64(len(esBytes)) + isPureLinedText = efficiencyLines < 4*efficiencyBytes + } } // Format the string into printable records. @@ -138,9 +157,7 @@ func (opts formatOptions) FormatDiffSlice(v *valueNode) textNode { switch { // If the text appears to be multi-lined text, // then perform differencing across individual lines. - case isLinedText: - ssx := strings.Split(sx, "\n") - ssy := strings.Split(sy, "\n") + case isPureLinedText: list = opts.formatDiffSlice( reflect.ValueOf(ssx), reflect.ValueOf(ssy), 1, "line", func(v reflect.Value, d diffMode) textRecord { @@ -229,7 +246,7 @@ func (opts formatOptions) FormatDiffSlice(v *valueNode) textNode { // If the text appears to be single-lined text, // then perform differencing in approximately fixed-sized chunks. // The output is printed as quoted strings. - case isText: + case isMostlyText: list = opts.formatDiffSlice( reflect.ValueOf(sx), reflect.ValueOf(sy), 64, "byte", func(v reflect.Value, d diffMode) textRecord { @@ -237,7 +254,6 @@ func (opts formatOptions) FormatDiffSlice(v *valueNode) textNode { return textRecord{Diff: d, Value: textLine(s)} }, ) - delim = "" // If the text appears to be binary data, // then perform differencing in approximately fixed-sized chunks. @@ -299,7 +315,7 @@ func (opts formatOptions) FormatDiffSlice(v *valueNode) textNode { // Wrap the output with appropriate type information. var out textNode = &textWrap{Prefix: "{", Value: list, Suffix: "}"} - if !isText { + if !isMostlyText { // The "{...}" byte-sequence literal is not valid Go syntax for strings. // Emit the type for extra clarity (e.g. "string{...}"). if t.Kind() == reflect.String { @@ -338,8 +354,11 @@ func (opts formatOptions) formatDiffSlice( vx, vy reflect.Value, chunkSize int, name string, makeRec func(reflect.Value, diffMode) textRecord, ) (list textList) { - es := diff.Difference(vx.Len(), vy.Len(), func(ix int, iy int) diff.Result { - return diff.BoolResult(vx.Index(ix).Interface() == vy.Index(iy).Interface()) + eq := func(ix, iy int) bool { + return vx.Index(ix).Interface() == vy.Index(iy).Interface() + } + es := diff.Difference(vx.Len(), vy.Len(), func(ix, iy int) diff.Result { + return diff.BoolResult(eq(ix, iy)) }) appendChunks := func(v reflect.Value, d diffMode) int { @@ -364,6 +383,7 @@ func (opts formatOptions) formatDiffSlice( groups := coalesceAdjacentEdits(name, es) groups = coalesceInterveningIdentical(groups, chunkSize/4) + groups = cleanupSurroundingIdentical(groups, eq) maxGroup := diffStats{Name: name} for i, ds := range groups { if maxLen >= 0 && numDiffs >= maxLen { @@ -416,25 +436,36 @@ func (opts formatOptions) formatDiffSlice( // coalesceAdjacentEdits coalesces the list of edits into groups of adjacent // equal or unequal counts. +// +// Example: +// +// Input: "..XXY...Y" +// Output: [ +// {NumIdentical: 2}, +// {NumRemoved: 2, NumInserted 1}, +// {NumIdentical: 3}, +// {NumInserted: 1}, +// ] +// func coalesceAdjacentEdits(name string, es diff.EditScript) (groups []diffStats) { - var prevCase int // Arbitrary index into which case last occurred - lastStats := func(i int) *diffStats { - if prevCase != i { + var prevMode byte + lastStats := func(mode byte) *diffStats { + if prevMode != mode { groups = append(groups, diffStats{Name: name}) - prevCase = i + prevMode = mode } return &groups[len(groups)-1] } for _, e := range es { switch e { case diff.Identity: - lastStats(1).NumIdentical++ + lastStats('=').NumIdentical++ case diff.UniqueX: - lastStats(2).NumRemoved++ + lastStats('!').NumRemoved++ case diff.UniqueY: - lastStats(2).NumInserted++ + lastStats('!').NumInserted++ case diff.Modified: - lastStats(2).NumModified++ + lastStats('!').NumModified++ } } return groups @@ -444,6 +475,35 @@ func coalesceAdjacentEdits(name string, es diff.EditScript) (groups []diffStats) // equal groups into adjacent unequal groups that currently result in a // dual inserted/removed printout. This acts as a high-pass filter to smooth // out high-frequency changes within the windowSize. +// +// Example: +// +// WindowSize: 16, +// Input: [ +// {NumIdentical: 61}, // group 0 +// {NumRemoved: 3, NumInserted: 1}, // group 1 +// {NumIdentical: 6}, // ├── coalesce +// {NumInserted: 2}, // ├── coalesce +// {NumIdentical: 1}, // ├── coalesce +// {NumRemoved: 9}, // └── coalesce +// {NumIdentical: 64}, // group 2 +// {NumRemoved: 3, NumInserted: 1}, // group 3 +// {NumIdentical: 6}, // ├── coalesce +// {NumInserted: 2}, // ├── coalesce +// {NumIdentical: 1}, // ├── coalesce +// {NumRemoved: 7}, // ├── coalesce +// {NumIdentical: 1}, // ├── coalesce +// {NumRemoved: 2}, // └── coalesce +// {NumIdentical: 63}, // group 4 +// ] +// Output: [ +// {NumIdentical: 61}, +// {NumIdentical: 7, NumRemoved: 12, NumInserted: 3}, +// {NumIdentical: 64}, +// {NumIdentical: 8, NumRemoved: 12, NumInserted: 3}, +// {NumIdentical: 63}, +// ] +// func coalesceInterveningIdentical(groups []diffStats, windowSize int) []diffStats { groups, groupsOrig := groups[:0], groups for i, ds := range groupsOrig { @@ -463,3 +523,91 @@ func coalesceInterveningIdentical(groups []diffStats, windowSize int) []diffStat } return groups } + +// cleanupSurroundingIdentical scans through all unequal groups, and +// moves any leading sequence of equal elements to the preceding equal group and +// moves and trailing sequence of equal elements to the succeeding equal group. +// +// This is necessary since coalesceInterveningIdentical may coalesce edit groups +// together such that leading/trailing spans of equal elements becomes possible. +// Note that this can occur even with an optimal diffing algorithm. +// +// Example: +// +// Input: [ +// {NumIdentical: 61}, +// {NumIdentical: 1 , NumRemoved: 11, NumInserted: 2}, // assume 3 leading identical elements +// {NumIdentical: 67}, +// {NumIdentical: 7, NumRemoved: 12, NumInserted: 3}, // assume 10 trailing identical elements +// {NumIdentical: 54}, +// ] +// Output: [ +// {NumIdentical: 64}, // incremented by 3 +// {NumRemoved: 9}, +// {NumIdentical: 67}, +// {NumRemoved: 9}, +// {NumIdentical: 64}, // incremented by 10 +// ] +// +func cleanupSurroundingIdentical(groups []diffStats, eq func(i, j int) bool) []diffStats { + var ix, iy int // indexes into sequence x and y + for i, ds := range groups { + // Handle equal group. + if ds.NumDiff() == 0 { + ix += ds.NumIdentical + iy += ds.NumIdentical + continue + } + + // Handle unequal group. + nx := ds.NumIdentical + ds.NumRemoved + ds.NumModified + ny := ds.NumIdentical + ds.NumInserted + ds.NumModified + var numLeadingIdentical, numTrailingIdentical int + for i := 0; i < nx && i < ny && eq(ix+i, iy+i); i++ { + numLeadingIdentical++ + } + for i := 0; i < nx && i < ny && eq(ix+nx-1-i, iy+ny-1-i); i++ { + numTrailingIdentical++ + } + if numIdentical := numLeadingIdentical + numTrailingIdentical; numIdentical > 0 { + if numLeadingIdentical > 0 { + // Remove leading identical span from this group and + // insert it into the preceding group. + if i-1 >= 0 { + groups[i-1].NumIdentical += numLeadingIdentical + } else { + // No preceding group exists, so prepend a new group, + // but do so after we finish iterating over all groups. + defer func() { + groups = append([]diffStats{{Name: groups[0].Name, NumIdentical: numLeadingIdentical}}, groups...) + }() + } + // Increment indexes since the preceding group would have handled this. + ix += numLeadingIdentical + iy += numLeadingIdentical + } + if numTrailingIdentical > 0 { + // Remove trailing identical span from this group and + // insert it into the succeeding group. + if i+1 < len(groups) { + groups[i+1].NumIdentical += numTrailingIdentical + } else { + // No succeeding group exists, so append a new group, + // but do so after we finish iterating over all groups. + defer func() { + groups = append(groups, diffStats{Name: groups[len(groups)-1].Name, NumIdentical: numTrailingIdentical}) + }() + } + // Do not increment indexes since the succeeding group will handle this. + } + + // Update this group since some identical elements were removed. + nx -= numIdentical + ny -= numIdentical + groups[i] = diffStats{Name: ds.Name, NumRemoved: nx, NumInserted: ny} + } + ix += nx + iy += ny + } + return groups +} diff --git a/vendor/github.com/google/gofuzz/.travis.yml b/vendor/github.com/google/gofuzz/.travis.yml index f8684d99fc4..061d72ae079 100644 --- a/vendor/github.com/google/gofuzz/.travis.yml +++ b/vendor/github.com/google/gofuzz/.travis.yml @@ -1,13 +1,10 @@ language: go go: - - 1.4 - - 1.3 - - 1.2 - - tip - -install: - - if ! go get code.google.com/p/go.tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi + - 1.11.x + - 1.12.x + - 1.13.x + - master script: - go test -cover diff --git a/vendor/github.com/google/gofuzz/CONTRIBUTING.md b/vendor/github.com/google/gofuzz/CONTRIBUTING.md index 51cf5cd1ada..97c1b34fd5e 100644 --- a/vendor/github.com/google/gofuzz/CONTRIBUTING.md +++ b/vendor/github.com/google/gofuzz/CONTRIBUTING.md @@ -1,7 +1,7 @@ # How to contribute # We'd love to accept your patches and contributions to this project. There are -a just a few small guidelines you need to follow. +just a few small guidelines you need to follow. ## Contributor License Agreement ## diff --git a/vendor/github.com/google/gofuzz/README.md b/vendor/github.com/google/gofuzz/README.md index 386c2a457a8..b503aae7d71 100644 --- a/vendor/github.com/google/gofuzz/README.md +++ b/vendor/github.com/google/gofuzz/README.md @@ -68,4 +68,22 @@ f.Fuzz(&myObject) // Type will correspond to whether A or B info is set. See more examples in ```example_test.go```. +You can use this library for easier [go-fuzz](https://github.com/dvyukov/go-fuzz)ing. +go-fuzz provides the user a byte-slice, which should be converted to different inputs +for the tested function. This library can help convert the byte slice. Consider for +example a fuzz test for a the function `mypackage.MyFunc` that takes an int arguments: +```go +// +build gofuzz +package mypackage + +import fuzz "github.com/google/gofuzz" + +func Fuzz(data []byte) int { + var i int + fuzz.NewFromGoFuzz(data).Fuzz(&i) + MyFunc(i) + return 0 +} +``` + Happy testing! diff --git a/vendor/github.com/google/gofuzz/bytesource/bytesource.go b/vendor/github.com/google/gofuzz/bytesource/bytesource.go new file mode 100644 index 00000000000..5bb36594969 --- /dev/null +++ b/vendor/github.com/google/gofuzz/bytesource/bytesource.go @@ -0,0 +1,81 @@ +/* +Copyright 2014 Google Inc. All rights reserved. + +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 bytesource provides a rand.Source64 that is determined by a slice of bytes. +package bytesource + +import ( + "bytes" + "encoding/binary" + "io" + "math/rand" +) + +// ByteSource implements rand.Source64 determined by a slice of bytes. The random numbers are +// generated from each 8 bytes in the slice, until the last bytes are consumed, from which a +// fallback pseudo random source is created in case more random numbers are required. +// It also exposes a `bytes.Reader` API, which lets callers consume the bytes directly. +type ByteSource struct { + *bytes.Reader + fallback rand.Source +} + +// New returns a new ByteSource from a given slice of bytes. +func New(input []byte) *ByteSource { + s := &ByteSource{ + Reader: bytes.NewReader(input), + fallback: rand.NewSource(0), + } + if len(input) > 0 { + s.fallback = rand.NewSource(int64(s.consumeUint64())) + } + return s +} + +func (s *ByteSource) Uint64() uint64 { + // Return from input if it was not exhausted. + if s.Len() > 0 { + return s.consumeUint64() + } + + // Input was exhausted, return random number from fallback (in this case fallback should not be + // nil). Try first having a Uint64 output (Should work in current rand implementation), + // otherwise return a conversion of Int63. + if s64, ok := s.fallback.(rand.Source64); ok { + return s64.Uint64() + } + return uint64(s.fallback.Int63()) +} + +func (s *ByteSource) Int63() int64 { + return int64(s.Uint64() >> 1) +} + +func (s *ByteSource) Seed(seed int64) { + s.fallback = rand.NewSource(seed) + s.Reader = bytes.NewReader(nil) +} + +// consumeUint64 reads 8 bytes from the input and convert them to a uint64. It assumes that the the +// bytes reader is not empty. +func (s *ByteSource) consumeUint64() uint64 { + var bytes [8]byte + _, err := s.Read(bytes[:]) + if err != nil && err != io.EOF { + panic("failed reading source") // Should not happen. + } + return binary.BigEndian.Uint64(bytes[:]) +} diff --git a/vendor/github.com/google/gofuzz/fuzz.go b/vendor/github.com/google/gofuzz/fuzz.go index da0a5f93800..761520a8cee 100644 --- a/vendor/github.com/google/gofuzz/fuzz.go +++ b/vendor/github.com/google/gofuzz/fuzz.go @@ -22,6 +22,9 @@ import ( "reflect" "regexp" "time" + + "github.com/google/gofuzz/bytesource" + "strings" ) // fuzzFuncMap is a map from a type to a fuzzFunc that handles that type. @@ -61,6 +64,34 @@ func NewWithSeed(seed int64) *Fuzzer { return f } +// NewFromGoFuzz is a helper function that enables using gofuzz (this +// project) with go-fuzz (https://github.com/dvyukov/go-fuzz) for continuous +// fuzzing. Essentially, it enables translating the fuzzing bytes from +// go-fuzz to any Go object using this library. +// +// This implementation promises a constant translation from a given slice of +// bytes to the fuzzed objects. This promise will remain over future +// versions of Go and of this library. +// +// Note: the returned Fuzzer should not be shared between multiple goroutines, +// as its deterministic output will no longer be available. +// +// Example: use go-fuzz to test the function `MyFunc(int)` in the package +// `mypackage`. Add the file: "mypacakge_fuzz.go" with the content: +// +// // +build gofuzz +// package mypacakge +// import fuzz "github.com/google/gofuzz" +// func Fuzz(data []byte) int { +// var i int +// fuzz.NewFromGoFuzz(data).Fuzz(&i) +// MyFunc(i) +// return 0 +// } +func NewFromGoFuzz(data []byte) *Fuzzer { + return New().RandSource(bytesource.New(data)) +} + // Funcs adds each entry in fuzzFuncs as a custom fuzzing function. // // Each entry in fuzzFuncs must be a function taking two parameters. @@ -141,7 +172,7 @@ func (f *Fuzzer) genElementCount() int { } func (f *Fuzzer) genShouldFill() bool { - return f.r.Float64() > f.nilChance + return f.r.Float64() >= f.nilChance } // MaxDepth sets the maximum number of recursive fuzz calls that will be made @@ -240,6 +271,7 @@ func (fc *fuzzerContext) doFuzz(v reflect.Value, flags uint64) { fn(v, fc.fuzzer.r) return } + switch v.Kind() { case reflect.Map: if fc.fuzzer.genShouldFill() { @@ -450,10 +482,10 @@ var fillFuncMap = map[reflect.Kind]func(reflect.Value, *rand.Rand){ v.SetFloat(r.Float64()) }, reflect.Complex64: func(v reflect.Value, r *rand.Rand) { - panic("unimplemented") + v.SetComplex(complex128(complex(r.Float32(), r.Float32()))) }, reflect.Complex128: func(v reflect.Value, r *rand.Rand) { - panic("unimplemented") + v.SetComplex(complex(r.Float64(), r.Float64())) }, reflect.String: func(v reflect.Value, r *rand.Rand) { v.SetString(randString(r)) @@ -465,38 +497,105 @@ var fillFuncMap = map[reflect.Kind]func(reflect.Value, *rand.Rand){ // randBool returns true or false randomly. func randBool(r *rand.Rand) bool { - if r.Int()&1 == 1 { - return true - } - return false + return r.Int31()&(1<<30) == 0 +} + +type int63nPicker interface { + Int63n(int64) int64 } -type charRange struct { - first, last rune +// UnicodeRange describes a sequential range of unicode characters. +// Last must be numerically greater than First. +type UnicodeRange struct { + First, Last rune } +// UnicodeRanges describes an arbitrary number of sequential ranges of unicode characters. +// To be useful, each range must have at least one character (First <= Last) and +// there must be at least one range. +type UnicodeRanges []UnicodeRange + // choose returns a random unicode character from the given range, using the // given randomness source. -func (r *charRange) choose(rand *rand.Rand) rune { - count := int64(r.last - r.first) - return r.first + rune(rand.Int63n(count)) +func (ur UnicodeRange) choose(r int63nPicker) rune { + count := int64(ur.Last - ur.First + 1) + return ur.First + rune(r.Int63n(count)) +} + +// CustomStringFuzzFunc constructs a FuzzFunc which produces random strings. +// Each character is selected from the range ur. If there are no characters +// in the range (cr.Last < cr.First), this will panic. +func (ur UnicodeRange) CustomStringFuzzFunc() func(s *string, c Continue) { + ur.check() + return func(s *string, c Continue) { + *s = ur.randString(c.Rand) + } } -var unicodeRanges = []charRange{ +// check is a function that used to check whether the first of ur(UnicodeRange) +// is greater than the last one. +func (ur UnicodeRange) check() { + if ur.Last < ur.First { + panic("The last encoding must be greater than the first one.") + } +} + +// randString of UnicodeRange makes a random string up to 20 characters long. +// Each character is selected form ur(UnicodeRange). +func (ur UnicodeRange) randString(r *rand.Rand) string { + n := r.Intn(20) + sb := strings.Builder{} + sb.Grow(n) + for i := 0; i < n; i++ { + sb.WriteRune(ur.choose(r)) + } + return sb.String() +} + +// defaultUnicodeRanges sets a default unicode range when user do not set +// CustomStringFuzzFunc() but wants fuzz string. +var defaultUnicodeRanges = UnicodeRanges{ {' ', '~'}, // ASCII characters {'\u00a0', '\u02af'}, // Multi-byte encoded characters {'\u4e00', '\u9fff'}, // Common CJK (even longer encodings) } +// CustomStringFuzzFunc constructs a FuzzFunc which produces random strings. +// Each character is selected from one of the ranges of ur(UnicodeRanges). +// Each range has an equal probability of being chosen. If there are no ranges, +// or a selected range has no characters (.Last < .First), this will panic. +// Do not modify any of the ranges in ur after calling this function. +func (ur UnicodeRanges) CustomStringFuzzFunc() func(s *string, c Continue) { + // Check unicode ranges slice is empty. + if len(ur) == 0 { + panic("UnicodeRanges is empty.") + } + // if not empty, each range should be checked. + for i := range ur { + ur[i].check() + } + return func(s *string, c Continue) { + *s = ur.randString(c.Rand) + } +} + +// randString of UnicodeRanges makes a random string up to 20 characters long. +// Each character is selected form one of the ranges of ur(UnicodeRanges), +// and each range has an equal probability of being chosen. +func (ur UnicodeRanges) randString(r *rand.Rand) string { + n := r.Intn(20) + sb := strings.Builder{} + sb.Grow(n) + for i := 0; i < n; i++ { + sb.WriteRune(ur[r.Intn(len(ur))].choose(r)) + } + return sb.String() +} + // randString makes a random string up to 20 characters long. The returned string // may include a variety of (valid) UTF-8 encodings. func randString(r *rand.Rand) string { - n := r.Intn(20) - runes := make([]rune, n) - for i := range runes { - runes[i] = unicodeRanges[r.Intn(len(unicodeRanges))].choose(r) - } - return string(runes) + return defaultUnicodeRanges.randString(r) } // randUint64 makes random 64 bit numbers. diff --git a/vendor/github.com/hashicorp/errwrap/errwrap.go b/vendor/github.com/hashicorp/errwrap/errwrap.go index a733bef18c0..44e368e5692 100644 --- a/vendor/github.com/hashicorp/errwrap/errwrap.go +++ b/vendor/github.com/hashicorp/errwrap/errwrap.go @@ -44,6 +44,8 @@ func Wrap(outer, inner error) error { // // format is the format of the error message. The string '{{err}}' will // be replaced with the original error message. +// +// Deprecated: Use fmt.Errorf() func Wrapf(format string, err error) error { outerMsg := "" if err != nil { @@ -148,6 +150,9 @@ func Walk(err error, cb WalkFunc) { for _, err := range e.WrappedErrors() { Walk(err, cb) } + case interface{ Unwrap() error }: + cb(err) + Walk(e.Unwrap(), cb) default: cb(err) } @@ -167,3 +172,7 @@ func (w *wrappedError) Error() string { func (w *wrappedError) WrappedErrors() []error { return []error{w.Outer, w.Inner} } + +func (w *wrappedError) Unwrap() error { + return w.Inner +} diff --git a/vendor/github.com/intel/goresctrl/LICENSE b/vendor/github.com/intel/goresctrl/LICENSE new file mode 100644 index 00000000000..261eeb9e9f8 --- /dev/null +++ b/vendor/github.com/intel/goresctrl/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. diff --git a/vendor/github.com/intel/goresctrl/pkg/kubernetes/annotations.go b/vendor/github.com/intel/goresctrl/pkg/kubernetes/annotations.go new file mode 100644 index 00000000000..a98e9b87b01 --- /dev/null +++ b/vendor/github.com/intel/goresctrl/pkg/kubernetes/annotations.go @@ -0,0 +1,58 @@ +/* +Copyright 2021 Intel Corporation + +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 kubernetes + +// ClassOrigin type indicates the source of container's class +// information: whether it is found from CRI level container +// annotations, Kubernetes' pod annotations, or it has not been found +// at all. +type ClassOrigin int + +const ( + ClassOriginNotFound ClassOrigin = iota + ClassOriginContainerAnnotation + ClassOriginPodAnnotation +) + +func (c ClassOrigin) String() string { + switch c { + case ClassOriginNotFound: + return "" + case ClassOriginContainerAnnotation: + return "container annotations" + case ClassOriginPodAnnotation: + return "pod annotations" + default: + return "" + } +} + +// ContainerClassFromAnnotations determines the effective class of a +// container from the Pod annotations and CRI level container +// annotations of a container. +func ContainerClassFromAnnotations(containerAnnotation, podAnnotation, podAnnotationContainerPrefix string, containerName string, containerAnnotations, podAnnotations map[string]string) (string, ClassOrigin) { + if clsName, ok := containerAnnotations[containerAnnotation]; ok { + return clsName, ClassOriginContainerAnnotation + } + if clsName, ok := podAnnotations[podAnnotationContainerPrefix+containerName]; ok { + return clsName, ClassOriginPodAnnotation + } + if clsName, ok := podAnnotations[podAnnotation]; ok { + return clsName, ClassOriginPodAnnotation + } + return "", ClassOriginNotFound +} diff --git a/vendor/github.com/intel/goresctrl/pkg/log/log.go b/vendor/github.com/intel/goresctrl/pkg/log/log.go new file mode 100644 index 00000000000..899a820555a --- /dev/null +++ b/vendor/github.com/intel/goresctrl/pkg/log/log.go @@ -0,0 +1,85 @@ +/* +Copyright 2019-2021 Intel Corporation + +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 log + +import ( + "fmt" + stdlog "log" + "strings" +) + +// Logger is the logging interface for goresctl +type Logger interface { + Debugf(format string, v ...interface{}) + Infof(format string, v ...interface{}) + Warnf(format string, v ...interface{}) + Errorf(format string, v ...interface{}) + Panicf(format string, v ...interface{}) + Fatalf(format string, v ...interface{}) +} + +type logger struct { + *stdlog.Logger +} + +// NewLoggerWrapper wraps an implementation of the golang standard intreface +// into a goresctl specific compatible logger interface +func NewLoggerWrapper(l *stdlog.Logger) Logger { + return &logger{Logger: l} +} + +func (l *logger) Debugf(format string, v ...interface{}) { + l.Logger.Printf("DEBUG: "+format, v...) +} + +func (l *logger) Infof(format string, v ...interface{}) { + l.Logger.Printf("INFO: "+format, v...) +} + +func (l *logger) Warnf(format string, v ...interface{}) { + l.Logger.Printf("WARN: "+format, v...) +} + +func (l *logger) Errorf(format string, v ...interface{}) { + l.Logger.Printf("ERROR: "+format, v...) +} + +func (l *logger) Panicf(format string, v ...interface{}) { + l.Logger.Panicf(format, v...) +} + +func (l *logger) Fatalf(format string, v ...interface{}) { + l.Logger.Fatalf(format, v...) +} + +func InfoBlock(l Logger, heading, linePrefix, format string, v ...interface{}) { + l.Infof("%s", heading) + + lines := strings.Split(fmt.Sprintf(format, v...), "\n") + for _, line := range lines { + l.Infof("%s%s", linePrefix, line) + } +} + +func DebugBlock(l Logger, heading, linePrefix, format string, v ...interface{}) { + l.Debugf("%s", heading) + + lines := strings.Split(fmt.Sprintf(format, v...), "\n") + for _, line := range lines { + l.Debugf("%s%s", linePrefix, line) + } +} diff --git a/vendor/github.com/intel/goresctrl/pkg/rdt/bitmask.go b/vendor/github.com/intel/goresctrl/pkg/rdt/bitmask.go new file mode 100644 index 00000000000..82b50775fc9 --- /dev/null +++ b/vendor/github.com/intel/goresctrl/pkg/rdt/bitmask.go @@ -0,0 +1,118 @@ +/* +Copyright 2019 Intel Corporation + +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 rdt + +import ( + "fmt" + "math/bits" + "strconv" + "strings" +) + +// bitmask represents a generic 64 bit wide bitmask +type bitmask uint64 + +// MarshalJSON implements the Marshaler interface of "encoding/json" +func (b bitmask) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf("\"%#x\"", b)), nil +} + +// listStr prints the bitmask in human-readable format, similar to e.g. the +// cpuset format of the Linux kernel +func (b bitmask) listStr() string { + str := "" + sep := "" + + shift := int(0) + lsbOne := b.lsbOne() + + // Process "ranges of ones" + for lsbOne != -1 { + b >>= uint(lsbOne) + + // Get range lenght from the position of the first zero + numOnes := b.lsbZero() + + if numOnes == 1 { + str += sep + strconv.Itoa(lsbOne+shift) + } else { + str += sep + strconv.Itoa(lsbOne+shift) + "-" + strconv.Itoa(lsbOne+numOnes-1+shift) + } + + // Shift away the bits that have been processed + b >>= uint(numOnes) + shift += lsbOne + numOnes + + // Get next bit that is set (if any) + lsbOne = b.lsbOne() + + sep = "," + } + + return str +} + +// listStrToBitmask parses a string containing a human-readable list of bit +// numbers into a bitmask +func listStrToBitmask(str string) (bitmask, error) { + b := bitmask(0) + + // Empty bitmask + if len(str) == 0 { + return b, nil + } + + ranges := strings.Split(str, ",") + for _, ran := range ranges { + split := strings.SplitN(ran, "-", 2) + + bitNum, err := strconv.ParseUint(split[0], 10, 6) + if err != nil { + return b, fmt.Errorf("invalid bitmask %q: %v", str, err) + } + + if len(split) == 1 { + b |= 1 << bitNum + } else { + endNum, err := strconv.ParseUint(split[1], 10, 6) + if err != nil { + return b, fmt.Errorf("invalid bitmask %q: %v", str, err) + } + if endNum <= bitNum { + return b, fmt.Errorf("invalid range %q in bitmask %q", ran, str) + } + b |= (1<<(endNum-bitNum+1) - 1) << bitNum + } + } + return b, nil +} + +func (b bitmask) lsbOne() int { + if b == 0 { + return -1 + } + return bits.TrailingZeros64(uint64(b)) +} + +func (b bitmask) msbOne() int { + // Returns -1 for b == 0 + return 63 - bits.LeadingZeros64(uint64(b)) +} + +func (b bitmask) lsbZero() int { + return bits.TrailingZeros64(^uint64(b)) +} diff --git a/vendor/github.com/intel/goresctrl/pkg/rdt/config.go b/vendor/github.com/intel/goresctrl/pkg/rdt/config.go new file mode 100644 index 00000000000..bea18f73fa5 --- /dev/null +++ b/vendor/github.com/intel/goresctrl/pkg/rdt/config.go @@ -0,0 +1,1193 @@ +/* +Copyright 2019-2021 Intel Corporation + +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 rdt + +import ( + "encoding/json" + "fmt" + "math" + "math/bits" + "sort" + "strconv" + "strings" + + grclog "github.com/intel/goresctrl/pkg/log" + "github.com/intel/goresctrl/pkg/utils" +) + +// Config is the user-specified RDT configuration. +type Config struct { + Options Options `json:"options"` + Partitions map[string]struct { + L2Allocation CatConfig `json:"l2Allocation"` + L3Allocation CatConfig `json:"l3Allocation"` + MBAllocation MbaConfig `json:"mbAllocation"` + Classes map[string]struct { + L2Allocation CatConfig `json:"l2Allocation"` + L3Allocation CatConfig `json:"l3Allocation"` + MBAllocation MbaConfig `json:"mbAllocation"` + Kubernetes KubernetesOptions `json:"kubernetes"` + } `json:"classes"` + } `json:"partitions"` +} + +// CatConfig contains the L2 or L3 cache allocation configuration for one partition or class. +type CatConfig map[string]CacheIdCatConfig + +// MbaConfig contains the memory bandwidth configuration for one partition or class. +type MbaConfig map[string]CacheIdMbaConfig + +// CacheIdCatConfig is the cache allocation configuration for one cache id. +// Code and Data represent an optional configuration for separate code and data +// paths and only have effect when RDT CDP (Code and Data Prioritization) is +// enabled in the system. Code and Data go in tandem so that both or neither +// must be specified - only specifying the other is considered a configuration +// error. +type CacheIdCatConfig struct { + Unified CacheProportion + Code CacheProportion + Data CacheProportion +} + +// CacheIdMbaConfig is the memory bandwidth configuration for one cache id. +// It's an array of at most two values, specifying separate values to be used +// for percentage based and MBps based memory bandwidth allocation. For +// example, `{"80%", "1000MBps"}` would allocate 80% if percentage based +// allocation is used by the Linux kernel, or 1000 MBps in case MBps based +// allocation is in use. +type CacheIdMbaConfig []MbProportion + +// MbProportion specifies a share of available memory bandwidth. It's an +// integer value followed by a unit. Two units are supported: +// +// - percentage, e.g. `80%` +// - MBps, e.g. `1000MBps` +type MbProportion string + +// CacheProportion specifies a share of the available cache lines. +// Supported formats: +// +// - percentage, e.g. `50%` +// - percentage range, e.g. `50-60%` +// - bit numbers, e.g. `0-5`, `2,3`, must contain one contiguous block of bits set +// - hex bitmask, e.g. `0xff0`, must contain one contiguous block of bits set +type CacheProportion string + +// CacheIdAll is a special cache id used to denote a default, used as a +// fallback for all cache ids that are not explicitly specified. +const CacheIdAll = "all" + +// config represents the final (parsed and resolved) runtime configuration of +// RDT Control +type config struct { + Options Options + Partitions partitionSet + Classes classSet +} + +// partitionSet represents the pool of rdt partitions +type partitionSet map[string]*partitionConfig + +// classSet represents the pool of rdt classes +type classSet map[string]*classConfig + +// partitionConfig is the final configuration of one partition +type partitionConfig struct { + CAT map[cacheLevel]catSchema + MB mbSchema +} + +// classConfig represents configuration of one class, i.e. one CTRL group in +// the Linux resctrl interface +type classConfig struct { + Partition string + CATSchema map[cacheLevel]catSchema + MBSchema mbSchema + Kubernetes KubernetesOptions +} + +// Options contains common settings. +type Options struct { + L2 CatOptions `json:"l2"` + L3 CatOptions `json:"l3"` + MB MbOptions `json:"mb"` +} + +// CatOptions contains the common settings for cache allocation. +type CatOptions struct { + Optional bool +} + +// MbOptions contains the common settings for memory bandwidth allocation. +type MbOptions struct { + Optional bool +} + +// KubernetesOptions contains per-class settings for the Kubernetes-related functionality. +type KubernetesOptions struct { + DenyPodAnnotation bool `json:"denyPodAnnotation"` + DenyContainerAnnotation bool `json:"denyContainerAnnotation"` +} + +// catSchema represents a cache part of the schemata of a class (i.e. resctrl group) +type catSchema struct { + Lvl cacheLevel + Alloc catSchemaRaw +} + +// catSchemaRaw is the cache schemata without the information about cache level +type catSchemaRaw map[uint64]catAllocation + +// mbSchema represents the MB part of the schemata of a class (i.e. resctrl group) +type mbSchema map[uint64]uint64 + +// catAllocation describes the allocation configuration for one cache id +type catAllocation struct { + Unified cacheAllocation + Code cacheAllocation `json:",omitempty"` + Data cacheAllocation `json:",omitempty"` +} + +// cacheAllocation is the basic interface for handling cache allocations of one +// type (unified, code, data) +type cacheAllocation interface { + Overlay(bitmask, uint64) (bitmask, error) +} + +// catAbsoluteAllocation represents an explicitly specified cache allocation +// bitmask +type catAbsoluteAllocation bitmask + +// catPctAllocation represents a relative (percentage) share of the available +// bitmask +type catPctAllocation uint64 + +// catPctRangeAllocation represents a percentage range of the available bitmask +type catPctRangeAllocation struct { + lowPct uint64 + highPct uint64 +} + +// catSchemaType represents different L3 cache allocation schemes +type catSchemaType string + +const ( + // catSchemaTypeUnified is the schema type when CDP is not enabled + catSchemaTypeUnified catSchemaType = "unified" + // catSchemaTypeCode is the 'code' part of CDP schema + catSchemaTypeCode catSchemaType = "code" + // catSchemaTypeData is the 'data' part of CDP schema + catSchemaTypeData catSchemaType = "data" +) + +// cat returns CAT options for the specified cache level. +func (o Options) cat(lvl cacheLevel) CatOptions { + switch lvl { + case L2: + return o.L2 + case L3: + return o.L3 + } + return CatOptions{} +} + +func (t catSchemaType) toResctrlStr() string { + if t == catSchemaTypeUnified { + return "" + } + return strings.ToUpper(string(t)) +} + +const ( + mbSuffixPct = "%" + mbSuffixMbps = "MBps" +) + +func newCatSchema(typ cacheLevel) catSchema { + return catSchema{ + Lvl: typ, + Alloc: make(map[uint64]catAllocation), + } +} + +// toStr returns the CAT schema in a format accepted by the Linux kernel +// resctrl (schemata) interface +func (s catSchema) toStr(typ catSchemaType, baseSchema catSchema) (string, error) { + schema := string(s.Lvl) + typ.toResctrlStr() + ":" + sep := "" + + // Get a sorted slice of cache ids for deterministic output + ids := make([]uint64, 0, len(baseSchema.Alloc)) + for id := range baseSchema.Alloc { + ids = append(ids, id) + } + utils.SortUint64s(ids) + + minBits := info.cat[s.Lvl].minCbmBits() + for _, id := range ids { + baseMask, ok := baseSchema.Alloc[id].getEffective(typ).(catAbsoluteAllocation) + if !ok { + return "", fmt.Errorf("BUG: basemask not of type catAbsoluteAllocation") + } + bmask := bitmask(baseMask) + + if s.Alloc != nil { + var err error + + masks := s.Alloc[id] + overlayMask := masks.getEffective(typ) + + bmask, err = overlayMask.Overlay(bmask, minBits) + if err != nil { + return "", err + } + } + schema += fmt.Sprintf("%s%d=%x", sep, id, bmask) + sep = ";" + } + + return schema + "\n", nil +} + +func (a catAllocation) get(typ catSchemaType) cacheAllocation { + switch typ { + case catSchemaTypeCode: + return a.Code + case catSchemaTypeData: + return a.Data + } + return a.Unified +} + +func (a catAllocation) set(typ catSchemaType, v cacheAllocation) catAllocation { + switch typ { + case catSchemaTypeCode: + a.Code = v + case catSchemaTypeData: + a.Data = v + default: + a.Unified = v + } + + return a +} + +func (a catAllocation) getEffective(typ catSchemaType) cacheAllocation { + switch typ { + case catSchemaTypeCode: + if a.Code != nil { + return a.Code + } + case catSchemaTypeData: + if a.Data != nil { + return a.Data + } + } + // Use Unified as the default/fallback for Code and Data + return a.Unified +} + +// Overlay function of the cacheAllocation interface +func (a catAbsoluteAllocation) Overlay(baseMask bitmask, minBits uint64) (bitmask, error) { + if err := verifyCatBaseMask(baseMask, minBits); err != nil { + return 0, err + } + + shiftWidth := baseMask.lsbOne() + + // Treat our bitmask relative to the basemask + bmask := bitmask(a) << shiftWidth + + // Do bounds checking that we're "inside" the base mask + if bmask|baseMask != baseMask { + return 0, fmt.Errorf("bitmask %#x (%#x << %d) does not fit basemask %#x", bmask, a, shiftWidth, baseMask) + } + + return bmask, nil +} + +// MarshalJSON implements the Marshaler interface of "encoding/json" +func (a catAbsoluteAllocation) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf("\"%#x\"", a)), nil +} + +// Overlay function of the cacheAllocation interface +func (a catPctAllocation) Overlay(baseMask bitmask, minBits uint64) (bitmask, error) { + return catPctRangeAllocation{highPct: uint64(a)}.Overlay(baseMask, minBits) +} + +// Overlay function of the cacheAllocation interface +func (a catPctRangeAllocation) Overlay(baseMask bitmask, minBits uint64) (bitmask, error) { + if err := verifyCatBaseMask(baseMask, minBits); err != nil { + return 0, err + } + + baseMaskMsb := uint64(baseMask.msbOne()) + baseMaskLsb := uint64(baseMask.lsbOne()) + baseMaskNumBits := baseMaskMsb - baseMaskLsb + 1 + + low, high := a.lowPct, a.highPct + if low == 0 { + low = 1 + } + if low > high || low > 100 || high > 100 { + return 0, fmt.Errorf("invalid percentage range in %v", a) + } + + // Convert percentage limits to bit numbers + // Our effective range is 1%-100%, use substraction (-1) because of + // arithmetics, so that we don't overflow on 100% + lsb := (low - 1) * baseMaskNumBits / 100 + msb := (high - 1) * baseMaskNumBits / 100 + + // Make sure the number of bits set satisfies the minimum requirement + numBits := msb - lsb + 1 + if numBits < minBits { + gap := minBits - numBits + + // First, widen the mask from the "lsb end" + if gap <= lsb { + lsb -= gap + gap = 0 + } else { + gap -= lsb + lsb = 0 + } + // If needed, widen the mask from the "msb end" + msbAvailable := baseMaskNumBits - msb - 1 + if gap <= msbAvailable { + msb += gap + } else { + return 0, fmt.Errorf("BUG: not enough bits available for cache bitmask (%v applied on basemask %#x)", a, baseMask) + } + } + + value := ((1 << (msb - lsb + 1)) - 1) << (lsb + baseMaskLsb) + + return bitmask(value), nil +} + +func verifyCatBaseMask(baseMask bitmask, minBits uint64) error { + if baseMask == 0 { + return fmt.Errorf("empty basemask not allowed") + } + + // Check that the basemask contains one (and only one) contiguous block of + // (enough) bits set + baseMaskWidth := baseMask.msbOne() - baseMask.lsbOne() + 1 + if bits.OnesCount64(uint64(baseMask)) != baseMaskWidth { + return fmt.Errorf("invalid basemask %#x: more than one block of bits set", baseMask) + } + if uint64(bits.OnesCount64(uint64(baseMask))) < minBits { + return fmt.Errorf("invalid basemask %#x: fewer than %d bits set", baseMask, minBits) + } + + return nil +} + +// MarshalJSON implements the Marshaler interface of "encoding/json" +func (a catPctAllocation) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf("\"%d%%\"", a)), nil +} + +// MarshalJSON implements the Marshaler interface of "encoding/json" +func (a catPctRangeAllocation) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf("\"%d-%d%%\"", a.lowPct, a.highPct)), nil +} + +// toStr returns the MB schema in a format accepted by the Linux kernel +// resctrl (schemata) interface +func (s mbSchema) toStr(base map[uint64]uint64) string { + schema := "MB:" + sep := "" + + // Get a sorted slice of cache ids for deterministic output + ids := make([]uint64, 0, len(base)) + for id := range base { + ids = append(ids, id) + } + utils.SortUint64s(ids) + + for _, id := range ids { + baseAllocation := base[id] + value := uint64(0) + if info.mb.mbpsEnabled { + value = math.MaxUint32 + if s != nil { + value = s[id] + } + // Limit to given base value + if value > baseAllocation { + value = baseAllocation + } + } else { + allocation := uint64(100) + if s != nil { + allocation = s[id] + } + value = allocation * baseAllocation / 100 + // Guarantee minimum bw so that writing out the schemata does not fail + if value < info.mb.minBandwidth { + value = info.mb.minBandwidth + } + } + + schema += fmt.Sprintf("%s%d=%d", sep, id, value) + sep = ";" + } + + return schema + "\n" +} + +// listStrToArray parses a string containing a human-readable list of numbers +// into an integer array +func listStrToArray(str string) ([]int, error) { + a := []int{} + + // Empty list + if len(str) == 0 { + return a, nil + } + + ranges := strings.Split(str, ",") + for _, ran := range ranges { + split := strings.SplitN(ran, "-", 2) + + // We limit to 8 bits in order to avoid accidental super long slices + num, err := strconv.ParseInt(split[0], 10, 8) + if err != nil { + return a, fmt.Errorf("invalid integer %q: %v", str, err) + } + + if len(split) == 1 { + a = append(a, int(num)) + } else { + endNum, err := strconv.ParseInt(split[1], 10, 8) + if err != nil { + return a, fmt.Errorf("invalid integer in range %q: %v", str, err) + } + if endNum <= num { + return a, fmt.Errorf("invalid integer range %q in %q", ran, str) + } + for i := num; i <= endNum; i++ { + a = append(a, int(i)) + } + } + } + sort.Ints(a) + return a, nil +} + +// resolve tries to resolve the requested configuration into a working +// configuration +func (c *Config) resolve() (config, error) { + var err error + conf := config{Options: c.Options} + + grclog.DebugBlock(log, "resolving configuration:", " ", "%s", utils.DumpJSON(c)) + + conf.Partitions, err = c.resolvePartitions() + if err != nil { + return conf, err + } + + conf.Classes, err = c.resolveClasses() + + return conf, err +} + +// resolvePartitions tries to resolve the requested resource allocations of +// partitions +func (c *Config) resolvePartitions() (partitionSet, error) { + // Initialize empty partition configuration + conf := make(partitionSet, len(c.Partitions)) + for name := range c.Partitions { + conf[name] = &partitionConfig{ + CAT: map[cacheLevel]catSchema{ + L2: newCatSchema(L2), + L3: newCatSchema(L3), + }, + MB: make(mbSchema, len(info.mb.cacheIds))} + } + + // Resolve L2 partition allocations + err := c.resolveCatPartitions(L2, conf) + if err != nil { + return nil, err + } + + // Try to resolve L3 partition allocations + err = c.resolveCatPartitions(L3, conf) + if err != nil { + return nil, err + } + + // Try to resolve MB partition allocations + err = c.resolveMBPartitions(conf) + if err != nil { + return nil, err + } + + return conf, nil +} + +// resolveCatPartitions tries to resolve requested cache allocations between partitions +func (c *Config) resolveCatPartitions(lvl cacheLevel, conf partitionSet) error { + // Resolve partitions in sorted order for reproducibility + names := make([]string, 0, len(c.Partitions)) + for name := range c.Partitions { + names = append(names, name) + } + sort.Strings(names) + + resolver := newCacheResolver(lvl, names) + + // Parse requested allocations from user config and load the resolver + for _, name := range names { + var allocations catSchema + var err error + switch lvl { + case L2: + allocations, err = c.Partitions[name].L2Allocation.toSchema(L2) + case L3: + allocations, err = c.Partitions[name].L3Allocation.toSchema(L3) + } + + if err != nil { + return fmt.Errorf("failed to parse %s allocation request for partition %q: %v", lvl, name, err) + } + + resolver.requests[name] = allocations.Alloc + } + + // Run resolver fo partition allocations + grants, err := resolver.resolve() + if err != nil { + return err + } + if grants == nil { + log.Debugf("%s allocation disabled for all partitions", lvl) + return nil + } + + for name, grant := range grants { + conf[name].CAT[lvl] = grant + } + + heading := fmt.Sprintf("actual (and requested) %s allocations per partition and cache id:", lvl) + infoStr := "" + for name, partition := range resolver.requests { + infoStr += name + "\n" + for _, id := range resolver.ids { + infoStr += fmt.Sprintf(" %2d: ", id) + allocationReq := partition[id] + for _, typ := range []catSchemaType{catSchemaTypeUnified, catSchemaTypeCode, catSchemaTypeData} { + infoStr += string(typ) + " " + requested := allocationReq.get(typ) + switch v := requested.(type) { + case catAbsoluteAllocation: + infoStr += fmt.Sprintf(" ", v) + case catPctAllocation: + granted := grants[name].Alloc[id].get(typ).(catAbsoluteAllocation) + requestedPct := fmt.Sprintf("(%d%%)", v) + truePct := float64(bits.OnesCount64(uint64(granted))) * 100 / float64(resolver.bitsTotal) + infoStr += fmt.Sprintf("%5.1f%% %-6s ", truePct, requestedPct) + case nil: + infoStr += " " + } + } + infoStr += "\n" + } + } + grclog.DebugBlock(log, heading, " ", "%s", infoStr) + + return nil +} + +// cacheResolver is a helper for resolving exclusive (partition) cache // allocation requests +type cacheResolver struct { + lvl cacheLevel + ids []uint64 + minBits uint64 + bitsTotal uint64 + partitions []string + requests map[string]catSchemaRaw + grants map[string]catSchema +} + +func newCacheResolver(lvl cacheLevel, partitions []string) *cacheResolver { + r := &cacheResolver{ + lvl: lvl, + ids: info.cat[lvl].cacheIds, + minBits: info.cat[lvl].minCbmBits(), + bitsTotal: uint64(info.cat[lvl].cbmMask().lsbZero()), + partitions: partitions, + requests: make(map[string]catSchemaRaw, len(partitions)), + grants: make(map[string]catSchema, len(partitions))} + + for _, p := range partitions { + r.grants[p] = catSchema{Lvl: lvl, Alloc: make(catSchemaRaw, len(r.ids))} + } + + return r +} + +func (r *cacheResolver) resolve() (map[string]catSchema, error) { + for _, id := range r.ids { + err := r.resolveID(id) + if err != nil { + return nil, err + } + } + return r.grants, nil +} + +// resolveCacheID resolves the partition allocations for one cache id +func (r *cacheResolver) resolveID(id uint64) error { + for _, typ := range []catSchemaType{catSchemaTypeUnified, catSchemaTypeCode, catSchemaTypeData} { + log.Debugf("resolving partitions for %q schema for cache id %d", typ, id) + err := r.resolveType(id, typ) + if err != nil { + return err + } + } + return nil +} + +// resolveType resolve one schema type for one cache id +func (r *cacheResolver) resolveType(id uint64, typ catSchemaType) error { + // Sanity check: if any partition has l3 allocation of this schema type + // configured check that all other partitions have it, too + nils := []string{} + for _, partition := range r.partitions { + if r.requests[partition][id].get(typ) == nil { + nils = append(nils, partition) + } + } + if len(nils) > 0 && len(nils) != len(r.partitions) { + return fmt.Errorf("some partitions (%s) missing %s %q allocation request for cache id %d", + strings.Join(nils, ", "), r.lvl, typ, id) + } + + // Act depending on the type of the first request in the list + a := r.requests[r.partitions[0]][id].get(typ) + switch a.(type) { + case catAbsoluteAllocation: + return r.resolveAbsolute(id, typ) + case nil: + default: + return r.resolveRelative(id, typ) + } + return nil +} + +func (r *cacheResolver) resolveRelative(id uint64, typ catSchemaType) error { + type reqHelper struct { + name string + req uint64 + } + + // Sanity check: + // 1. allocation requests are of the same type (relative) + // 2. total allocation requested for this cache id does not exceed 100 percent + // Additionally fill a helper structure for sorting partitions + percentageTotal := uint64(0) + reqs := make([]reqHelper, 0, len(r.partitions)) + for _, partition := range r.partitions { + switch a := r.requests[partition][id].get(typ).(type) { + case catPctAllocation: + percentageTotal += uint64(a) + reqs = append(reqs, reqHelper{name: partition, req: uint64(a)}) + case catAbsoluteAllocation: + return fmt.Errorf("error resolving %s allocation for cache id %d: mixing "+ + "relative and absolute allocations between partitions not supported", r.lvl, id) + case catPctRangeAllocation: + return fmt.Errorf("percentage ranges in partition allocation not supported") + default: + return fmt.Errorf("BUG: unknown cacheAllocation type %T", a) + } + } + if percentageTotal < 100 { + log.Infof("requested total %s %q partition allocation for cache id %d <100%% (%d%%)", r.lvl, typ, id, percentageTotal) + } else if percentageTotal > 100 { + return fmt.Errorf("accumulated %s %q partition allocation requests for cache id %d exceeds 100%% (%d%%)", r.lvl, typ, id, percentageTotal) + } + + // Sort partition allocations. We want to resolve smallest allocations + // first in order to try to ensure that all allocations can be satisfied + // because small percentages might need to be rounded up + sort.Slice(reqs, func(i, j int) bool { + return reqs[i].req < reqs[j].req + }) + + // Calculate number of bits granted to each partition. + grants := make(map[string]uint64, len(r.partitions)) + bitsTotal := percentageTotal * uint64(r.bitsTotal) / 100 + bitsAvailable := bitsTotal + for i, req := range reqs { + percentageAvailable := bitsAvailable * percentageTotal / bitsTotal + + // This might happen e.g. if number of partitions would be greater + // than the total number of bits + if bitsAvailable < r.minBits { + return fmt.Errorf("unable to resolve %s allocation for cache id %d, not enough exlusive bits available", r.lvl, id) + } + + // Use integer arithmetics, effectively always rounding down + // fractional allocations i.e. trying to avoid over-allocation + numBits := req.req * bitsAvailable / percentageAvailable + + // Guarantee a non-zero allocation + if numBits < r.minBits { + numBits = r.minBits + } + // Don't overflow, allocate all remaining bits to the last partition + if numBits > bitsAvailable || i == len(reqs)-1 { + numBits = bitsAvailable + } + + grants[req.name] = numBits + bitsAvailable -= numBits + } + + // Construct the actual bitmasks for each partition + lsbID := uint64(0) + for _, partition := range r.partitions { + // Compose the actual bitmask + v := r.grants[partition].Alloc[id].set(typ, catAbsoluteAllocation(bitmask(((1< 0 { + return fmt.Errorf("overlapping %s partition allocation requests for cache id %d", r.lvl, id) + } + mask |= bitmask(a) + + r.grants[partition].Alloc[id] = r.grants[partition].Alloc[id].set(typ, a) + } + + return nil +} + +// resolveMBPartitions tries to resolve requested MB allocations between partitions +func (c *Config) resolveMBPartitions(conf partitionSet) error { + // We use percentage values directly from the user conf + for name, partition := range c.Partitions { + allocations, err := partition.MBAllocation.toSchema() + if err != nil { + return fmt.Errorf("failed to resolve MB allocation for partition %q: %v", name, err) + } + for id, allocation := range allocations { + conf[name].MB[id] = allocation + // Check that we don't go under the minimum allowed bandwidth setting + if !info.mb.mbpsEnabled && allocation < info.mb.minBandwidth { + conf[name].MB[id] = info.mb.minBandwidth + } + } + } + + return nil +} + +// resolveClasses tries to resolve class allocations of all partitions +func (c *Config) resolveClasses() (classSet, error) { + classes := make(classSet) + + for bname, partition := range c.Partitions { + for gname, class := range partition.Classes { + gname = unaliasClassName(gname) + + if !IsQualifiedClassName(gname) { + return classes, fmt.Errorf("unqualified class name %q (must not be '.' or '..' and must not contain '/' or newline)", gname) + } + if _, ok := classes[gname]; ok { + return classes, fmt.Errorf("class names must be unique, %q defined multiple times", gname) + } + + var err error + gc := &classConfig{Partition: bname, + CATSchema: make(map[cacheLevel]catSchema), + Kubernetes: class.Kubernetes} + + gc.CATSchema[L2], err = class.L2Allocation.toSchema(L2) + if err != nil { + return classes, fmt.Errorf("failed to resolve L2 allocation for class %q: %v", gname, err) + } + if gc.CATSchema[L2].Alloc != nil && partition.L2Allocation == nil { + return classes, fmt.Errorf("L2 allocation missing from partition %q but class %q specifies L2 schema", bname, gname) + } + + gc.CATSchema[L3], err = class.L3Allocation.toSchema(L3) + if err != nil { + return classes, fmt.Errorf("failed to resolve L3 allocation for class %q: %v", gname, err) + } + if gc.CATSchema[L3].Alloc != nil && partition.L3Allocation == nil { + return classes, fmt.Errorf("L3 allocation missing from partition %q but class %q specifies L3 schema", bname, gname) + } + + gc.MBSchema, err = class.MBAllocation.toSchema() + if err != nil { + return classes, fmt.Errorf("failed to resolve MB allocation for class %q: %v", gname, err) + } + if gc.MBSchema != nil && partition.MBAllocation == nil { + return classes, fmt.Errorf("MB allocation missing from partition %q but class %q specifies MB schema", bname, gname) + } + + classes[gname] = gc + } + } + + return classes, nil +} + +// toSchema converts a cache allocation config to effective allocation schema covering all cache IDs +func (c CatConfig) toSchema(lvl cacheLevel) (catSchema, error) { + if c == nil { + return catSchema{Lvl: lvl}, nil + } + + allocations := newCatSchema(lvl) + minBits := info.cat[lvl].minCbmBits() + + d, ok := c[CacheIdAll] + if !ok { + d = CacheIdCatConfig{Unified: "100%"} + } + defaultVal, err := d.parse(minBits) + if err != nil { + return allocations, err + } + + // Pre-fill with defaults + for _, i := range info.cat[lvl].cacheIds { + allocations.Alloc[i] = defaultVal + } + + for key, val := range c { + if key == CacheIdAll { + continue + } + + ids, err := listStrToArray(key) + if err != nil { + return allocations, err + } + + schemaVal, err := val.parse(minBits) + if err != nil { + return allocations, err + } + + for _, id := range ids { + if _, ok := allocations.Alloc[uint64(id)]; ok { + allocations.Alloc[uint64(id)] = schemaVal + } + } + } + + return allocations, nil +} + +// catConfig is a helper for unmarshalling CatConfig +type catConfig CatConfig + +// UnmarshalJSON implements the Unmarshaler interface of "encoding/json" +func (c *CatConfig) UnmarshalJSON(data []byte) error { + raw := new(interface{}) + + err := json.Unmarshal(data, raw) + if err != nil { + return err + } + + conf := CatConfig{} + switch v := (*raw).(type) { + case string: + conf[CacheIdAll] = CacheIdCatConfig{Unified: CacheProportion(v)} + default: + // Use the helper type to avoid infinite recursion + helper := catConfig{} + if err := json.Unmarshal(data, &helper); err != nil { + return err + } + for k, v := range helper { + conf[k] = v + } + } + *c = conf + return nil +} + +// toSchema converts an MB allocation config to effective allocation schema covering all cache IDs +func (c MbaConfig) toSchema() (mbSchema, error) { + if c == nil { + return nil, nil + } + + d, ok := c[CacheIdAll] + if !ok { + d = CacheIdMbaConfig{"100" + mbSuffixPct, "4294967295" + mbSuffixMbps} + } + defaultVal, err := d.parse() + if err != nil { + return nil, err + } + + allocations := make(mbSchema, len(info.mb.cacheIds)) + // Pre-fill with defaults + for _, i := range info.mb.cacheIds { + allocations[i] = defaultVal + } + + for key, val := range c { + if key == CacheIdAll { + continue + } + + ids, err := listStrToArray(key) + if err != nil { + return nil, err + } + + schemaVal, err := val.parse() + if err != nil { + return nil, err + } + + for _, id := range ids { + if _, ok := allocations[uint64(id)]; ok { + allocations[uint64(id)] = schemaVal + } + } + } + + return allocations, nil +} + +// mbaConfig is a helper for unmarshalling MbaConfig +type mbaConfig MbaConfig + +// UnmarshalJSON implements the Unmarshaler interface of "encoding/json" +func (c *MbaConfig) UnmarshalJSON(data []byte) error { + raw := new(interface{}) + + err := json.Unmarshal(data, raw) + if err != nil { + return err + } + + conf := MbaConfig{} + switch (*raw).(type) { + case []interface{}: + helper := CacheIdMbaConfig{} + if err := json.Unmarshal(data, &helper); err != nil { + return err + } + conf[CacheIdAll] = helper + default: + // Use the helper type to avoid infinite recursion + helper := mbaConfig{} + if err := json.Unmarshal(data, &helper); err != nil { + return err + } + for k, v := range helper { + conf[k] = v + } + } + *c = conf + return nil +} + +// parse per cache-id CAT configuration into an effective allocation to be used +// in the CAT schema +func (c *CacheIdCatConfig) parse(minBits uint64) (catAllocation, error) { + var err error + allocation := catAllocation{} + + allocation.Unified, err = c.Unified.parse(minBits) + if err != nil { + return allocation, err + } + allocation.Code, err = c.Code.parse(minBits) + if err != nil { + return allocation, err + } + allocation.Data, err = c.Data.parse(minBits) + if err != nil { + return allocation, err + } + + // Sanity check for the configuration + if allocation.Unified == nil { + return allocation, fmt.Errorf("'unified' not specified in cache schema %s", *c) + } + if allocation.Code != nil && allocation.Data == nil { + return allocation, fmt.Errorf("'code' specified but missing 'data' from cache schema %s", *c) + } + if allocation.Code == nil && allocation.Data != nil { + return allocation, fmt.Errorf("'data' specified but missing 'code' from cache schema %s", *c) + } + + return allocation, nil +} + +// cacheIdCatConfig is a helper for unmarshalling CacheIdCatConfig +type cacheIdCatConfig CacheIdCatConfig + +// UnmarshalJSON implements the Unmarshaler interface of "encoding/json" +func (c *CacheIdCatConfig) UnmarshalJSON(data []byte) error { + raw := new(interface{}) + + err := json.Unmarshal(data, raw) + if err != nil { + return err + } + + conf := CacheIdCatConfig{} + switch v := (*raw).(type) { + case string: + conf.Unified = CacheProportion(v) + default: + // Use the helper type to avoid infinite recursion + helper := cacheIdCatConfig{} + if err := json.Unmarshal(data, &helper); err != nil { + return err + } + conf.Unified = helper.Unified + conf.Code = helper.Code + conf.Data = helper.Data + } + *c = conf + return nil +} + +// parse converts a per cache-id MBA configuration into effective value +// to be used in the MBA schema +func (c *CacheIdMbaConfig) parse() (uint64, error) { + for _, v := range *c { + str := string(v) + if strings.HasSuffix(str, mbSuffixPct) { + if !info.mb.mbpsEnabled { + value, err := strconv.ParseUint(strings.TrimSuffix(str, mbSuffixPct), 10, 7) + if err != nil { + return 0, err + } + return value, nil + } + } else if strings.HasSuffix(str, mbSuffixMbps) { + if info.mb.mbpsEnabled { + value, err := strconv.ParseUint(strings.TrimSuffix(str, mbSuffixMbps), 10, 32) + if err != nil { + return 0, err + } + return value, nil + } + } else { + log.Warnf("unrecognized MBA allocation unit in %q", str) + } + } + + // No value for the active mode was specified + if info.mb.mbpsEnabled { + return 0, fmt.Errorf("missing 'MBps' value from mbSchema; required because 'mba_MBps' is enabled in the system") + } + return 0, fmt.Errorf("missing '%%' value from mbSchema; required because percentage-based MBA allocation is enabled in the system") +} + +// parse converts a string value into cacheAllocation type +func (c CacheProportion) parse(minBits uint64) (cacheAllocation, error) { + if c == "" { + return nil, nil + } + + if c[len(c)-1] == '%' { + // Percentages of the max number of bits + split := strings.SplitN(string(c)[0:len(c)-1], "-", 2) + var allocation cacheAllocation + + if len(split) == 1 { + pct, err := strconv.ParseUint(split[0], 10, 7) + if err != nil { + return allocation, err + } + if pct > 100 { + return allocation, fmt.Errorf("invalid percentage value %q", c) + } + allocation = catPctAllocation(pct) + } else { + low, err := strconv.ParseUint(split[0], 10, 7) + if err != nil { + return allocation, err + } + high, err := strconv.ParseUint(split[1], 10, 7) + if err != nil { + return allocation, err + } + if low > high || low > 100 || high > 100 { + return allocation, fmt.Errorf("invalid percentage range %q", c) + } + allocation = catPctRangeAllocation{lowPct: low, highPct: high} + } + + return allocation, nil + } + + // Absolute allocation + var value uint64 + var err error + if strings.HasPrefix(string(c), "0x") { + // Hex value + value, err = strconv.ParseUint(string(c[2:]), 16, 64) + if err != nil { + return nil, err + } + } else { + // Last, try "list" format (i.e. smthg like 0,2,5-9,...) + tmp, err := listStrToBitmask(string(c)) + value = uint64(tmp) + if err != nil { + return nil, err + } + } + + // Sanity check of absolute allocation: bitmask must (only) contain one + // contiguous block of ones wide enough + numOnes := bits.OnesCount64(value) + if numOnes != bits.Len64(value)-bits.TrailingZeros64(value) { + return nil, fmt.Errorf("invalid cache bitmask %q: more than one continuous block of ones", c) + } + if uint64(numOnes) < minBits { + return nil, fmt.Errorf("invalid cache bitmask %q: number of bits less than %d", c, minBits) + } + + return catAbsoluteAllocation(value), nil +} diff --git a/vendor/github.com/intel/goresctrl/pkg/rdt/info.go b/vendor/github.com/intel/goresctrl/pkg/rdt/info.go new file mode 100644 index 00000000000..abd0727bdaa --- /dev/null +++ b/vendor/github.com/intel/goresctrl/pkg/rdt/info.go @@ -0,0 +1,341 @@ +/* +Copyright 2019-2021 Intel Corporation + +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 rdt + +import ( + "bufio" + "fmt" + "io/ioutil" + "os" + "path/filepath" + "sort" + "strconv" + "strings" +) + +// resctrlInfo contains information about the RDT support in the system +type resctrlInfo struct { + resctrlPath string + resctrlMountOpts map[string]struct{} + numClosids uint64 + cat map[cacheLevel]catInfoAll + l3mon l3MonInfo + mb mbInfo +} + +type cacheLevel string + +const ( + L2 cacheLevel = "L2" + L3 cacheLevel = "L3" +) + +type catInfoAll struct { + cacheIds []uint64 + unified catInfo + code catInfo + data catInfo +} + +type catInfo struct { + cbmMask bitmask + minCbmBits uint64 + shareableBits bitmask +} + +type l3MonInfo struct { + numRmids uint64 + monFeatures []string +} + +type mbInfo struct { + cacheIds []uint64 + bandwidthGran uint64 + delayLinear uint64 + minBandwidth uint64 + mbpsEnabled bool // true if MBA_MBps is enabled +} + +var mountInfoPath string = "/proc/mounts" + +// getInfo is a helper method for a "unified API" for getting L3 information +func (i catInfoAll) getInfo() catInfo { + switch { + case i.code.Supported(): + return i.code + case i.data.Supported(): + return i.data + } + return i.unified +} + +func (i catInfoAll) cbmMask() bitmask { + mask := i.getInfo().cbmMask + if mask != 0 { + return mask + } + return bitmask(^uint64(0)) +} + +func (i catInfoAll) minCbmBits() uint64 { + return i.getInfo().minCbmBits +} + +func getRdtInfo() (*resctrlInfo, error) { + var err error + info := &resctrlInfo{cat: make(map[cacheLevel]catInfoAll)} + + info.resctrlPath, info.resctrlMountOpts, err = getResctrlMountInfo() + if err != nil { + return info, fmt.Errorf("failed to detect resctrl mount point: %v", err) + } + log.Infof("detected resctrl filesystem at %q", info.resctrlPath) + + // Check that RDT is available + infopath := filepath.Join(info.resctrlPath, "info") + if _, err := os.Stat(infopath); err != nil { + return info, fmt.Errorf("failed to read RDT info from %q: %v", infopath, err) + } + + // Check CAT feature available + for _, cl := range []cacheLevel{L2, L3} { + cat := catInfoAll{} + catFeatures := map[string]*catInfo{ + "": &cat.unified, + "CODE": &cat.code, + "DATA": &cat.data, + } + for suffix, i := range catFeatures { + dir := string(cl) + suffix + subpath := filepath.Join(infopath, dir) + if _, err = os.Stat(subpath); err == nil { + *i, info.numClosids, err = getCatInfo(subpath) + if err != nil { + return info, fmt.Errorf("failed to get %s info from %q: %v", dir, subpath, err) + } + } + } + if cat.getInfo().Supported() { + cat.cacheIds, err = getCacheIds(info.resctrlPath, string(cl)) + if err != nil { + return info, fmt.Errorf("failed to get %s CAT cache IDs: %v", cl, err) + } + } + info.cat[cl] = cat + } + + // Check MON features available + subpath := filepath.Join(infopath, "L3_MON") + if _, err = os.Stat(subpath); err == nil { + info.l3mon, err = getL3MonInfo(subpath) + if err != nil { + return info, fmt.Errorf("failed to get L3_MON info from %q: %v", subpath, err) + } + } + + // Check MBA feature available + subpath = filepath.Join(infopath, "MB") + if _, err = os.Stat(subpath); err == nil { + info.mb, info.numClosids, err = getMBInfo(subpath) + if err != nil { + return info, fmt.Errorf("failed to get MBA info from %q: %v", subpath, err) + } + + info.mb.cacheIds, err = getCacheIds(info.resctrlPath, "MB") + if err != nil { + return info, fmt.Errorf("failed to get MBA cache IDs: %v", err) + } + } + + return info, nil +} + +func getCatInfo(basepath string) (catInfo, uint64, error) { + var err error + var numClosids uint64 + info := catInfo{} + + info.cbmMask, err = readFileBitmask(filepath.Join(basepath, "cbm_mask")) + if err != nil { + return info, numClosids, err + } + info.minCbmBits, err = readFileUint64(filepath.Join(basepath, "min_cbm_bits")) + if err != nil { + return info, numClosids, err + } + info.shareableBits, err = readFileBitmask(filepath.Join(basepath, "shareable_bits")) + if err != nil { + return info, numClosids, err + } + numClosids, err = readFileUint64(filepath.Join(basepath, "num_closids")) + if err != nil { + return info, numClosids, err + } + + return info, numClosids, nil +} + +// Supported returns true if L3 cache allocation has is supported and enabled in the system +func (i catInfo) Supported() bool { + return i.cbmMask != 0 +} + +func getL3MonInfo(basepath string) (l3MonInfo, error) { + var err error + info := l3MonInfo{} + + info.numRmids, err = readFileUint64(filepath.Join(basepath, "num_rmids")) + if err != nil { + return info, err + } + + lines, err := readFileString(filepath.Join(basepath, "mon_features")) + if err != nil { + return info, err + } + info.monFeatures = strings.Split(lines, "\n") + sort.Strings(info.monFeatures) + + return info, nil +} + +// Supported returns true if L3 monitoring is supported and enabled in the system +func (i l3MonInfo) Supported() bool { + return i.numRmids != 0 && len(i.monFeatures) > 0 +} + +func getMBInfo(basepath string) (mbInfo, uint64, error) { + var err error + var numClosids uint64 + info := mbInfo{} + + info.bandwidthGran, err = readFileUint64(filepath.Join(basepath, "bandwidth_gran")) + if err != nil { + return info, numClosids, err + } + info.delayLinear, err = readFileUint64(filepath.Join(basepath, "delay_linear")) + if err != nil { + return info, numClosids, err + } + info.minBandwidth, err = readFileUint64(filepath.Join(basepath, "min_bandwidth")) + if err != nil { + return info, numClosids, err + } + numClosids, err = readFileUint64(filepath.Join(basepath, "num_closids")) + if err != nil { + return info, numClosids, err + } + + // Detect MBps mode directly from mount options as it's not visible in MB + // info directory + _, mountOpts, err := getResctrlMountInfo() + if err != nil { + return info, numClosids, fmt.Errorf("failed to get resctrl mount options: %v", err) + } + if _, ok := mountOpts["mba_MBps"]; ok { + info.mbpsEnabled = true + } + + return info, numClosids, nil +} + +// Supported returns true if memory bandwidth allocation has is supported and enabled in the system +func (i mbInfo) Supported() bool { + return i.minBandwidth != 0 +} + +func getCacheIds(basepath string, prefix string) ([]uint64, error) { + var ids []uint64 + + // Parse cache IDs from the root schemata + data, err := readFileString(filepath.Join(basepath, "schemata")) + if err != nil { + return ids, fmt.Errorf("failed to read root schemata: %v", err) + } + + for _, line := range strings.Split(data, "\n") { + trimmed := strings.TrimSpace(line) + lineSplit := strings.SplitN(trimmed, ":", 2) + + // Find line with given resource prefix + if len(lineSplit) == 2 && strings.HasPrefix(lineSplit[0], prefix) { + schema := strings.Split(lineSplit[1], ";") + ids = make([]uint64, len(schema)) + + // Get individual cache configurations from the schema + for idx, definition := range schema { + split := strings.Split(definition, "=") + if len(split) != 2 { + return ids, fmt.Errorf("looks like an invalid schema %q", trimmed) + } + ids[idx], err = strconv.ParseUint(split[0], 10, 64) + if err != nil { + return ids, fmt.Errorf("failed to parse cache id in %q: %v", trimmed, err) + } + } + return ids, nil + } + } + return ids, fmt.Errorf("no %s resources in root schemata", prefix) +} + +func getResctrlMountInfo() (string, map[string]struct{}, error) { + mountOptions := map[string]struct{}{} + + f, err := os.Open(mountInfoPath) + if err != nil { + return "", mountOptions, err + } + defer f.Close() + + s := bufio.NewScanner(f) + for s.Scan() { + split := strings.Split(s.Text(), " ") + if len(split) > 3 && split[2] == "resctrl" { + opts := strings.Split(split[3], ",") + for _, opt := range opts { + mountOptions[opt] = struct{}{} + } + return split[1], mountOptions, nil + } + } + return "", mountOptions, fmt.Errorf("resctrl not found in " + mountInfoPath) +} + +func readFileUint64(path string) (uint64, error) { + data, err := readFileString(path) + if err != nil { + return 0, err + } + + return strconv.ParseUint(data, 10, 64) +} + +func readFileBitmask(path string) (bitmask, error) { + data, err := readFileString(path) + if err != nil { + return 0, err + } + + value, err := strconv.ParseUint(data, 16, 64) + return bitmask(value), err +} + +func readFileString(path string) (string, error) { + data, err := ioutil.ReadFile(path) + return strings.TrimSpace(string(data)), err +} diff --git a/vendor/github.com/intel/goresctrl/pkg/rdt/kubernetes.go b/vendor/github.com/intel/goresctrl/pkg/rdt/kubernetes.go new file mode 100644 index 00000000000..2d7368c09a1 --- /dev/null +++ b/vendor/github.com/intel/goresctrl/pkg/rdt/kubernetes.go @@ -0,0 +1,74 @@ +/* +Copyright 2021 Intel Corporation + +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 rdt + +import ( + "fmt" + "github.com/intel/goresctrl/pkg/kubernetes" +) + +const ( + // RdtContainerAnnotation is the CRI level container annotation for setting + // the RDT class (CLOS) of a container + RdtContainerAnnotation = "io.kubernetes.cri.rdt-class" + + // RdtPodAnnotation is a Pod annotation for setting the RDT class (CLOS) of + // all containers of the pod + RdtPodAnnotation = "rdt.resources.beta.kubernetes.io/pod" + + // RdtPodAnnotationContainerPrefix is prefix for per-container Pod annotation + // for setting the RDT class (CLOS) of one container of the pod + RdtPodAnnotationContainerPrefix = "rdt.resources.beta.kubernetes.io/container." +) + +// ContainerClassFromAnnotations determines the effective RDT class of a +// container from the Pod annotations and CRI level container annotations of a +// container. Verifies that the class exists in goresctrl configuration and that +// it is allowed to be used. +func ContainerClassFromAnnotations(containerName string, containerAnnotations, podAnnotations map[string]string) (string, error) { + clsName, clsOrigin := kubernetes.ContainerClassFromAnnotations( + RdtContainerAnnotation, RdtPodAnnotation, RdtPodAnnotationContainerPrefix, + containerName, containerAnnotations, podAnnotations) + + if clsOrigin != kubernetes.ClassOriginNotFound { + if rdt == nil { + return "", fmt.Errorf("RDT not initialized, class %q not available", clsName) + } + + // Verify validity of class name + if !IsQualifiedClassName(clsName) { + return "", fmt.Errorf("unqualified RDT class name %q", clsName) + } + + // If RDT has been initialized we check that the class exists + if _, ok := rdt.getClass(clsName); !ok { + return "", fmt.Errorf("RDT class %q does not exist in configuration", clsName) + } + + // If classes have been configured by goresctrl + if clsConf, ok := rdt.conf.Classes[unaliasClassName(clsName)]; ok { + // Check that the class is allowed + if clsOrigin == kubernetes.ClassOriginPodAnnotation && clsConf.Kubernetes.DenyPodAnnotation { + return "", fmt.Errorf("RDT class %q not allowed from Pod annotations", clsName) + } else if clsOrigin == kubernetes.ClassOriginContainerAnnotation && clsConf.Kubernetes.DenyContainerAnnotation { + return "", fmt.Errorf("RDT class %q not allowed from Container annotation", clsName) + } + } + } + + return clsName, nil +} diff --git a/vendor/github.com/intel/goresctrl/pkg/rdt/prometheus.go b/vendor/github.com/intel/goresctrl/pkg/rdt/prometheus.go new file mode 100644 index 00000000000..70dcc8705ca --- /dev/null +++ b/vendor/github.com/intel/goresctrl/pkg/rdt/prometheus.go @@ -0,0 +1,124 @@ +/* +Copyright 2020 Intel Corporation + +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 rdt + +import ( + "fmt" + "sync" + + "github.com/prometheus/client_golang/prometheus" +) + +var customLabels []string = []string{} + +// collector implements prometheus.Collector interface +type collector struct { + descriptors map[string]*prometheus.Desc +} + +// NewCollector creates new Prometheus collector of RDT metrics +func NewCollector() (prometheus.Collector, error) { + c := &collector{descriptors: make(map[string]*prometheus.Desc)} + return c, nil +} + +// RegisterCustomPrometheusLabels registers monitor group annotations to be +// exported as Prometheus metrics labels +func RegisterCustomPrometheusLabels(names ...string) { +Names: + for _, n := range names { + for _, c := range customLabels { + if n == c { + break Names + } + } + customLabels = append(customLabels, n) + } +} + +// Describe method of the prometheus.Collector interface +func (c *collector) Describe(ch chan<- *prometheus.Desc) { + for resource, features := range GetMonFeatures() { + switch resource { + case MonResourceL3: + for _, f := range features { + ch <- c.describeL3(f) + } + } + } +} + +// Collect method of the prometheus.Collector interface +func (c collector) Collect(ch chan<- prometheus.Metric) { + var wg sync.WaitGroup + + for _, cls := range GetClasses() { + for _, monGrp := range cls.GetMonGroups() { + wg.Add(1) + g := monGrp + go func() { + defer wg.Done() + c.collectGroupMetrics(ch, g) + }() + } + } + wg.Wait() +} + +func (c *collector) describeL3(feature string) *prometheus.Desc { + d, ok := c.descriptors[feature] + if !ok { + name := "l3_" + feature + help := "L3 " + feature + + switch feature { + case "llc_occupancy": + help = "L3 (LLC) occupancy" + case "mbm_local_bytes": + help = "bytes transferred to/from local memory through LLC" + case "mbm_total_bytes": + help = "total bytes transferred to/from memory through LLC" + } + labels := append([]string{"rdt_class", "rdt_mon_group", "cache_id"}, customLabels...) + d = prometheus.NewDesc(name, help, labels, nil) + c.descriptors[feature] = d + } + return d +} + +func (c *collector) collectGroupMetrics(ch chan<- prometheus.Metric, mg MonGroup) { + allData := mg.GetMonData() + + annotations := mg.GetAnnotations() + customLabelValues := make([]string, len(customLabels)) + for i, name := range customLabels { + customLabelValues[i] = annotations[name] + } + + for cacheID, data := range allData.L3 { + for feature, value := range data { + labels := append([]string{mg.Parent().Name(), mg.Name(), fmt.Sprint(cacheID)}, customLabelValues...) + + ch <- prometheus.MustNewConstMetric( + c.describeL3(feature), + prometheus.CounterValue, + float64(value), + labels..., + ) + } + } +} diff --git a/vendor/github.com/intel/goresctrl/pkg/rdt/rdt.go b/vendor/github.com/intel/goresctrl/pkg/rdt/rdt.go new file mode 100644 index 00000000000..f442ba2382d --- /dev/null +++ b/vendor/github.com/intel/goresctrl/pkg/rdt/rdt.go @@ -0,0 +1,859 @@ +/* +Copyright 2019 Intel Corporation + +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 rdt implements an API for managing Intel® RDT technologies via the +// resctrl pseudo-filesystem of the Linux kernel. It provides flexible +// configuration with a hierarchical approach for easy management of exclusive +// cache allocations. +// +// Goresctrl supports all available RDT technologies, i.e. L2 and L3 Cache +// Allocation (CAT) with Code and Data Prioritization (CDP) and Memory +// Bandwidth Allocation (MBA) plus Cache Monitoring (CMT) and Memory Bandwidth +// Monitoring (MBM). +// +// Basic usage example: +// rdt.SetLogger(logrus.New()) +// +// if err := rdt.Initialize(""); err != nil { +// return fmt.Errorf("RDT not supported: %v", err) +// } +// +// if err := rdt.SetConfigFromFile("/path/to/rdt.conf.yaml", false); err != nil { +// return fmt.Errorf("RDT configuration failed: %v", err) +// } +// +// if cls, ok := rdt.GetClass("my-class"); ok { +// // Set PIDs 12345 and 12346 to class "my-class" +// if err := cls.AddPids("12345", "12346"); err != nil { +// return fmt.Errorf("failed to add PIDs to RDT class: %v", err) +// } +// } +package rdt + +import ( + "errors" + "fmt" + "io/ioutil" + stdlog "log" + "os" + "path/filepath" + "sort" + "strconv" + "strings" + "syscall" + + "sigs.k8s.io/yaml" + + grclog "github.com/intel/goresctrl/pkg/log" + "github.com/intel/goresctrl/pkg/utils" +) + +const ( + // RootClassName is the name we use in our config for the special class + // that configures the "root" resctrl group of the system + RootClassName = "system/default" + // RootClassAlias is an alternative name for the root class + RootClassAlias = "" +) + +type control struct { + grclog.Logger + + resctrlGroupPrefix string + conf config + rawConf Config + classes map[string]*ctrlGroup +} + +var log grclog.Logger = grclog.NewLoggerWrapper(stdlog.New(os.Stderr, "[ rdt ] ", 0)) + +var info *resctrlInfo + +var rdt *control + +// Function for removing resctrl groups from the filesystem. This is +// configurable because of unit tests. +var groupRemoveFunc func(string) error = os.Remove + +// CtrlGroup defines the interface of one goresctrl managed RDT class. It maps +// to one CTRL group directory in the goresctrl pseudo-filesystem. +type CtrlGroup interface { + ResctrlGroup + + // CreateMonGroup creates a new monitoring group under this CtrlGroup. + CreateMonGroup(name string, annotations map[string]string) (MonGroup, error) + + // DeleteMonGroup deletes a monitoring group from this CtrlGroup. + DeleteMonGroup(name string) error + + // DeleteMonGroups deletes all monitoring groups from this CtrlGroup. + DeleteMonGroups() error + + // GetMonGroup returns a specific monitoring group under this CtrlGroup. + GetMonGroup(name string) (MonGroup, bool) + + // GetMonGroups returns all monitoring groups under this CtrlGroup. + GetMonGroups() []MonGroup +} + +// ResctrlGroup is the generic interface for resctrl CTRL and MON groups. It +// maps to one CTRL or MON group directory in the goresctrl pseudo-filesystem. +type ResctrlGroup interface { + // Name returns the name of the group. + Name() string + + // GetPids returns the process ids assigned to the group. + GetPids() ([]string, error) + + // AddPids assigns the given process ids to the group. + AddPids(pids ...string) error + + // GetMonData retrieves the monitoring data of the group. + GetMonData() MonData +} + +// MonGroup represents the interface to a RDT monitoring group. It maps to one +// MON group in the goresctrl filesystem. +type MonGroup interface { + ResctrlGroup + + // Parent returns the CtrlGroup under which the monitoring group exists. + Parent() CtrlGroup + + // GetAnnotations returns the annotations stored to the monitoring group. + GetAnnotations() map[string]string +} + +// MonData contains monitoring stats of one monitoring group. +type MonData struct { + L3 MonL3Data +} + +// MonL3Data contains L3 monitoring stats of one monitoring group. +type MonL3Data map[uint64]MonLeafData + +// MonLeafData represents the raw numerical stats from one RDT monitor data leaf. +type MonLeafData map[string]uint64 + +// MonResource is the type of RDT monitoring resource. +type MonResource string + +const ( + // MonResourceL3 is the RDT L3 cache monitor resource. + MonResourceL3 MonResource = "l3" +) + +type ctrlGroup struct { + resctrlGroup + + monPrefix string + monGroups map[string]*monGroup +} + +type monGroup struct { + resctrlGroup + + annotations map[string]string +} + +type resctrlGroup struct { + prefix string + name string + parent *ctrlGroup // parent for MON groups +} + +// SetLogger sets the logger instance to be used by the package. This function +// may be called even before Initialize(). +func SetLogger(l grclog.Logger) { + log = l + if rdt != nil { + rdt.setLogger(l) + } +} + +// Initialize detects RDT from the system and initializes control interface of +// the package. +func Initialize(resctrlGroupPrefix string) error { + var err error + + info = nil + rdt = nil + + // Get info from the resctrl filesystem + info, err = getRdtInfo() + if err != nil { + return err + } + + r := &control{Logger: log, resctrlGroupPrefix: resctrlGroupPrefix} + + // NOTE: we lose monitoring group annotations (i.e. prometheus metrics + // labels) on re-init + if r.classes, err = r.classesFromResctrlFs(); err != nil { + return fmt.Errorf("failed to initialize classes from resctrl fs: %v", err) + } + + if err := r.pruneMonGroups(); err != nil { + return err + } + + rdt = r + + return nil +} + +// DiscoverClasses discovers existing classes from the resctrl filesystem. +// Makes it possible to discover gropus with another prefix than was set with +// Initialize(). The original prefix is still used for monitoring groups. +func DiscoverClasses(resctrlGroupPrefix string) error { + if rdt != nil { + return rdt.discoverFromResctrl(resctrlGroupPrefix) + } + return fmt.Errorf("rdt not initialized") +} + +// SetConfig (re-)configures the resctrl filesystem according to the specified +// configuration. +func SetConfig(c *Config, force bool) error { + if rdt != nil { + return rdt.setConfig(c, force) + } + return fmt.Errorf("rdt not initialized") +} + +// SetConfigFromData takes configuration as raw data, parses it and +// reconfigures the resctrl filesystem. +func SetConfigFromData(data []byte, force bool) error { + cfg := &Config{} + if err := yaml.Unmarshal(data, &cfg); err != nil { + return fmt.Errorf("failed to parse configuration data: %v", err) + } + + return SetConfig(cfg, force) +} + +// SetConfigFromFile reads configuration from the filesystem and reconfigures +// the resctrl filesystem. +func SetConfigFromFile(path string, force bool) error { + data, err := ioutil.ReadFile(path) + if err != nil { + return fmt.Errorf("failed to read config file: %v", err) + } + + if err := SetConfigFromData(data, force); err != nil { + return err + } + + log.Infof("configuration successfully loaded from %q", path) + return nil +} + +// GetClass returns one RDT class. +func GetClass(name string) (CtrlGroup, bool) { + if rdt != nil { + return rdt.getClass(name) + } + return nil, false +} + +// GetClasses returns all available RDT classes. +func GetClasses() []CtrlGroup { + if rdt != nil { + return rdt.getClasses() + } + return []CtrlGroup{} +} + +// MonSupported returns true if RDT monitoring features are available. +func MonSupported() bool { + if rdt != nil { + return rdt.monSupported() + } + return false +} + +// GetMonFeatures returns the available monitoring stats of each available +// monitoring technology. +func GetMonFeatures() map[MonResource][]string { + if rdt != nil { + return rdt.getMonFeatures() + } + return map[MonResource][]string{} +} + +// IsQualifiedClassName returns true if given string qualifies as a class name +func IsQualifiedClassName(name string) bool { + // Must be qualified as a file name + return name == RootClassName || (len(name) < 4096 && name != "." && name != ".." && !strings.ContainsAny(name, "/\n")) +} + +func (c *control) getClass(name string) (CtrlGroup, bool) { + cls, ok := c.classes[unaliasClassName(name)] + return cls, ok +} + +func (c *control) getClasses() []CtrlGroup { + ret := make([]CtrlGroup, 0, len(c.classes)) + + for _, v := range c.classes { + ret = append(ret, v) + } + sort.Slice(ret, func(i, j int) bool { return ret[i].Name() < ret[j].Name() }) + + return ret +} + +func (c *control) monSupported() bool { + return info.l3mon.Supported() +} + +func (c *control) getMonFeatures() map[MonResource][]string { + ret := make(map[MonResource][]string) + if info.l3mon.Supported() { + ret[MonResourceL3] = append([]string{}, info.l3mon.monFeatures...) + } + + return ret +} + +func (c *control) setLogger(l grclog.Logger) { + c.Logger = l +} + +func (c *control) setConfig(newConfig *Config, force bool) error { + c.Infof("configuration update") + + conf, err := (*newConfig).resolve() + if err != nil { + return fmt.Errorf("invalid configuration: %v", err) + } + + err = c.configureResctrl(conf, force) + if err != nil { + return fmt.Errorf("resctrl configuration failed: %v", err) + } + + c.conf = conf + // TODO: we'd better create a deep copy + c.rawConf = *newConfig + c.Infof("configuration finished") + + return nil +} + +func (c *control) configureResctrl(conf config, force bool) error { + grclog.DebugBlock(c, "applying resolved config:", " ", "%s", utils.DumpJSON(conf)) + + // Remove stale resctrl groups + classesFromFs, err := c.classesFromResctrlFs() + if err != nil { + return err + } + + for name, cls := range classesFromFs { + if _, ok := conf.Classes[cls.name]; !isRootClass(cls.name) && !ok { + if !force { + tasks, err := cls.GetPids() + if err != nil { + return fmt.Errorf("failed to get resctrl group tasks: %v", err) + } + if len(tasks) > 0 { + return fmt.Errorf("refusing to remove non-empty resctrl group %q", cls.relPath("")) + } + } + log.Debugf("removing existing resctrl group %q", cls.relPath("")) + err = groupRemoveFunc(cls.path("")) + if err != nil { + return fmt.Errorf("failed to remove resctrl group %q: %v", cls.relPath(""), err) + } + + delete(c.classes, name) + } + } + + for name, cls := range c.classes { + if _, ok := conf.Classes[cls.name]; !ok || cls.prefix != c.resctrlGroupPrefix { + if !isRootClass(cls.name) { + log.Debugf("dropping stale class %q (%q)", name, cls.path("")) + delete(c.classes, name) + } + } + } + + if _, ok := c.classes[RootClassName]; !ok { + log.Warnf("root class missing from runtime data, re-adding...") + c.classes[RootClassName] = classesFromFs[RootClassName] + } + + // Try to apply given configuration + for name, class := range conf.Classes { + if _, ok := c.classes[name]; !ok { + cg, err := newCtrlGroup(c.resctrlGroupPrefix, c.resctrlGroupPrefix, name) + if err != nil { + return err + } + c.classes[name] = cg + } + partition := conf.Partitions[class.Partition] + if err := c.classes[name].configure(name, class, partition, conf.Options); err != nil { + return err + } + } + + if err := c.pruneMonGroups(); err != nil { + return err + } + + return nil +} + +func (c *control) discoverFromResctrl(prefix string) error { + c.Debugf("running class discovery from resctrl filesystem using prefix %q", prefix) + + classesFromFs, err := c.classesFromResctrlFsPrefix(prefix) + if err != nil { + return err + } + + // Drop stale classes + for name, cls := range c.classes { + if _, ok := classesFromFs[cls.name]; !ok || cls.prefix != prefix { + if !isRootClass(cls.name) { + log.Debugf("dropping stale class %q (%q)", name, cls.path("")) + delete(c.classes, name) + } + } + } + + for name, cls := range classesFromFs { + if _, ok := c.classes[name]; !ok { + c.classes[name] = cls + log.Debugf("adding discovered class %q (%q)", name, cls.path("")) + } + } + + if err := c.pruneMonGroups(); err != nil { + return err + } + + return nil +} + +func (c *control) classesFromResctrlFs() (map[string]*ctrlGroup, error) { + return c.classesFromResctrlFsPrefix(c.resctrlGroupPrefix) +} + +func (c *control) classesFromResctrlFsPrefix(prefix string) (map[string]*ctrlGroup, error) { + names := []string{RootClassName} + if g, err := resctrlGroupsFromFs(prefix, info.resctrlPath); err != nil { + return nil, err + } else { + for _, n := range g { + if prefix != c.resctrlGroupPrefix && + strings.HasPrefix(n, c.resctrlGroupPrefix) && + strings.HasPrefix(c.resctrlGroupPrefix, prefix) { + // Skip groups in the standard namespace + continue + } + names = append(names, n[len(prefix):]) + } + } + + classes := make(map[string]*ctrlGroup, len(names)+1) + for _, name := range names { + g, err := newCtrlGroup(prefix, c.resctrlGroupPrefix, name) + if err != nil { + return nil, err + } + classes[name] = g + } + + return classes, nil +} + +func (c *control) pruneMonGroups() error { + for name, cls := range c.classes { + if err := cls.pruneMonGroups(); err != nil { + return fmt.Errorf("failed to prune stale monitoring groups of %q: %v", name, err) + } + } + return nil +} + +func (c *control) readRdtFile(rdtPath string) ([]byte, error) { + return ioutil.ReadFile(filepath.Join(info.resctrlPath, rdtPath)) +} + +func (c *control) writeRdtFile(rdtPath string, data []byte) error { + if err := ioutil.WriteFile(filepath.Join(info.resctrlPath, rdtPath), data, 0644); err != nil { + return c.cmdError(err) + } + return nil +} + +func (c *control) cmdError(origErr error) error { + errData, readErr := c.readRdtFile(filepath.Join("info", "last_cmd_status")) + if readErr != nil { + return origErr + } + cmdStatus := strings.TrimSpace(string(errData)) + if len(cmdStatus) > 0 && cmdStatus != "ok" { + return fmt.Errorf("%s", cmdStatus) + } + return origErr +} + +func newCtrlGroup(prefix, monPrefix, name string) (*ctrlGroup, error) { + cg := &ctrlGroup{ + resctrlGroup: resctrlGroup{prefix: prefix, name: name}, + monPrefix: monPrefix, + } + + if err := os.Mkdir(cg.path(""), 0755); err != nil && !os.IsExist(err) { + return nil, err + } + + var err error + cg.monGroups, err = cg.monGroupsFromResctrlFs() + if err != nil { + return nil, fmt.Errorf("error when retrieving existing monitor groups: %v", err) + } + + return cg, nil +} + +func (c *ctrlGroup) CreateMonGroup(name string, annotations map[string]string) (MonGroup, error) { + if mg, ok := c.monGroups[name]; ok { + return mg, nil + } + + log.Debugf("creating monitoring group %s/%s", c.name, name) + mg, err := newMonGroup(c.monPrefix, name, c, annotations) + if err != nil { + return nil, fmt.Errorf("failed to create new monitoring group %q: %v", name, err) + } + + c.monGroups[name] = mg + + return mg, err +} + +func (c *ctrlGroup) DeleteMonGroup(name string) error { + mg, ok := c.monGroups[name] + if !ok { + log.Warnf("trying to delete non-existent mon group %s/%s", c.name, name) + return nil + } + + log.Debugf("deleting monitoring group %s/%s", c.name, name) + if err := groupRemoveFunc(mg.path("")); err != nil { + return fmt.Errorf("failed to remove monitoring group %q: %v", mg.relPath(""), err) + } + + delete(c.monGroups, name) + + return nil +} + +func (c *ctrlGroup) DeleteMonGroups() error { + for name := range c.monGroups { + if err := c.DeleteMonGroup(name); err != nil { + return err + } + } + return nil +} + +func (c *ctrlGroup) GetMonGroup(name string) (MonGroup, bool) { + mg, ok := c.monGroups[name] + return mg, ok +} + +func (c *ctrlGroup) GetMonGroups() []MonGroup { + ret := make([]MonGroup, 0, len(c.monGroups)) + + for _, v := range c.monGroups { + ret = append(ret, v) + } + sort.Slice(ret, func(i, j int) bool { return ret[i].Name() < ret[j].Name() }) + + return ret +} + +func (c *ctrlGroup) configure(name string, class *classConfig, + partition *partitionConfig, options Options) error { + schemata := "" + + // Handle cache allocation + for _, lvl := range []cacheLevel{L2, L3} { + switch { + case info.cat[lvl].unified.Supported(): + schema, err := class.CATSchema[lvl].toStr(catSchemaTypeUnified, partition.CAT[lvl]) + if err != nil { + return err + } + schemata += schema + case info.cat[lvl].data.Supported() || info.cat[lvl].code.Supported(): + schema, err := class.CATSchema[lvl].toStr(catSchemaTypeCode, partition.CAT[lvl]) + if err != nil { + return err + } + schemata += schema + + schema, err = class.CATSchema[lvl].toStr(catSchemaTypeData, partition.CAT[lvl]) + if err != nil { + return err + } + schemata += schema + default: + if class.CATSchema[lvl].Alloc != nil && !options.cat(lvl).Optional { + return fmt.Errorf("%s cache allocation for %q specified in configuration but not supported by system", lvl, name) + } + } + } + + // Handle memory bandwidth allocation + switch { + case info.mb.Supported(): + schemata += class.MBSchema.toStr(partition.MB) + default: + if class.MBSchema != nil && !options.MB.Optional { + return fmt.Errorf("memory bandwidth allocation for %q specified in configuration but not supported by system", name) + } + } + + if len(schemata) > 0 { + log.Debugf("writing schemata %q to %q", schemata, c.relPath("")) + if err := rdt.writeRdtFile(c.relPath("schemata"), []byte(schemata)); err != nil { + return err + } + } else { + log.Debugf("empty schemata") + } + + return nil +} + +func (c *ctrlGroup) monGroupsFromResctrlFs() (map[string]*monGroup, error) { + names, err := resctrlGroupsFromFs(c.monPrefix, c.path("mon_groups")) + if err != nil && !os.IsNotExist(err) { + return nil, err + } + + grps := make(map[string]*monGroup, len(names)) + for _, name := range names { + name = name[len(c.monPrefix):] + mg, err := newMonGroup(c.monPrefix, name, c, nil) + if err != nil { + return nil, err + } + grps[name] = mg + } + return grps, nil +} + +// Remove empty monitoring groups +func (c *ctrlGroup) pruneMonGroups() error { + for name, mg := range c.monGroups { + pids, err := mg.GetPids() + if err != nil { + return fmt.Errorf("failed to get pids for monitoring group %q: %v", mg.relPath(""), err) + } + if len(pids) == 0 { + if err := c.DeleteMonGroup(name); err != nil { + return fmt.Errorf("failed to remove monitoring group %q: %v", mg.relPath(""), err) + } + } + } + return nil +} + +func (r *resctrlGroup) Name() string { + return r.name +} + +func (r *resctrlGroup) GetPids() ([]string, error) { + data, err := rdt.readRdtFile(r.relPath("tasks")) + if err != nil { + return []string{}, err + } + split := strings.Split(strings.TrimSpace(string(data)), "\n") + if len(split[0]) > 0 { + return split, nil + } + return []string{}, nil +} + +func (r *resctrlGroup) AddPids(pids ...string) error { + f, err := os.OpenFile(r.path("tasks"), os.O_WRONLY, 0644) + if err != nil { + return err + } + defer f.Close() + + for _, pid := range pids { + if _, err := f.WriteString(pid + "\n"); err != nil { + if errors.Is(err, syscall.ESRCH) { + log.Debugf("no task %s", pid) + } else { + return fmt.Errorf("failed to assign processes %v to class %q: %v", pids, r.name, rdt.cmdError(err)) + } + } + } + return nil +} + +func (r *resctrlGroup) GetMonData() MonData { + m := MonData{} + + if info.l3mon.Supported() { + l3, err := r.getMonL3Data() + if err != nil { + log.Warnf("failed to retrieve L3 monitoring data: %v", err) + } else { + m.L3 = l3 + } + } + + return m +} + +func (r *resctrlGroup) getMonL3Data() (MonL3Data, error) { + files, err := ioutil.ReadDir(r.path("mon_data")) + if err != nil { + return nil, err + } + + m := MonL3Data{} + for _, file := range files { + name := file.Name() + if strings.HasPrefix(name, "mon_L3_") { + // Parse cache id from the dirname + id, err := strconv.ParseUint(strings.TrimPrefix(name, "mon_L3_"), 10, 32) + if err != nil { + // Just print a warning, we try to retrieve as much info as possible + log.Warnf("error parsing L3 monitor data directory name %q: %v", name, err) + continue + } + + data, err := r.getMonLeafData(filepath.Join("mon_data", name)) + if err != nil { + log.Warnf("failed to read monitor data: %v", err) + continue + } + + m[id] = data + } + } + + return m, nil +} + +func (r *resctrlGroup) getMonLeafData(path string) (MonLeafData, error) { + files, err := ioutil.ReadDir(r.path(path)) + if err != nil { + return nil, err + } + + m := make(MonLeafData, len(files)) + + for _, file := range files { + name := file.Name() + + // We expect that all the files in the dir are regular files + val, err := readFileUint64(r.path(path, name)) + if err != nil { + // Just print a warning, we want to retrieve as much info as possible + log.Warnf("error reading data file: %v", err) + continue + } + + m[name] = val + } + return m, nil +} + +func (r *resctrlGroup) relPath(elem ...string) string { + if r.parent == nil { + if r.name == RootClassName { + return filepath.Join(elem...) + } + return filepath.Join(append([]string{r.prefix + r.name}, elem...)...) + } + // Parent is only intended for MON groups - non-root CTRL groups are considered + // as peers to the root CTRL group (as they are in HW) and do not have a parent + return r.parent.relPath(append([]string{"mon_groups", r.prefix + r.name}, elem...)...) +} + +func (r *resctrlGroup) path(elem ...string) string { + return filepath.Join(info.resctrlPath, r.relPath(elem...)) +} + +func newMonGroup(prefix string, name string, parent *ctrlGroup, annotations map[string]string) (*monGroup, error) { + mg := &monGroup{ + resctrlGroup: resctrlGroup{prefix: prefix, name: name, parent: parent}, + annotations: make(map[string]string, len(annotations))} + + if err := os.Mkdir(mg.path(""), 0755); err != nil && !os.IsExist(err) { + return nil, err + } + for k, v := range annotations { + mg.annotations[k] = v + } + + return mg, nil +} + +func (m *monGroup) Parent() CtrlGroup { + return m.parent +} + +func (m *monGroup) GetAnnotations() map[string]string { + a := make(map[string]string, len(m.annotations)) + for k, v := range m.annotations { + a[k] = v + } + return a +} + +func resctrlGroupsFromFs(prefix string, path string) ([]string, error) { + files, err := ioutil.ReadDir(path) + if err != nil { + return nil, err + } + + grps := make([]string, 0, len(files)) + for _, file := range files { + filename := file.Name() + if strings.HasPrefix(filename, prefix) { + if s, err := os.Stat(filepath.Join(path, filename, "tasks")); err == nil && !s.IsDir() { + grps = append(grps, filename) + } + } + } + return grps, nil +} + +func isRootClass(name string) bool { + return name == RootClassName || name == RootClassAlias +} + +func unaliasClassName(name string) string { + if isRootClass(name) { + return RootClassName + } + return name +} diff --git a/vendor/github.com/intel/goresctrl/pkg/utils/json.go b/vendor/github.com/intel/goresctrl/pkg/utils/json.go new file mode 100644 index 00000000000..0606422e876 --- /dev/null +++ b/vendor/github.com/intel/goresctrl/pkg/utils/json.go @@ -0,0 +1,32 @@ +/* +Copyright 2019 Intel Corporation + +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 utils + +import ( + "fmt" + + "sigs.k8s.io/yaml" +) + +// DumpJSON dumps a json-compatible struct in human-readable form +func DumpJSON(r interface{}) string { + out, err := yaml.Marshal(r) + if err != nil { + return fmt.Sprintf("!!!!!\nUnable to stringify %T: %v\n!!!!!", r, err) + } + return string(out) +} diff --git a/vendor/github.com/intel/goresctrl/pkg/utils/sort.go b/vendor/github.com/intel/goresctrl/pkg/utils/sort.go new file mode 100644 index 00000000000..964e679b21b --- /dev/null +++ b/vendor/github.com/intel/goresctrl/pkg/utils/sort.go @@ -0,0 +1,36 @@ +// Copyright 2020 Intel Corporation. All Rights Reserved. +// +// 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 utils + +import ( + "sort" +) + +// SortUint64s sorts a slice of uint64 in increasing order. +func SortUint64s(a []uint64) { + sort.Sort(Uint64Slice(a)) +} + +// Uint64Slice implmenents sort.Interface for a slice of uint64. +type Uint64Slice []uint64 + +// Len returns the length of an UintSlice +func (s Uint64Slice) Len() int { return len(s) } + +// Less returns true if element at 'i' is less than the element at 'j' +func (s Uint64Slice) Less(i, j int) bool { return s[i] < s[j] } + +// Swap swaps the values of two elements +func (s Uint64Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } diff --git a/vendor/golang.org/x/oauth2/internal/client_appengine.go b/vendor/golang.org/x/oauth2/internal/client_appengine.go index 7434871880a..e1755d1d9ac 100644 --- a/vendor/golang.org/x/oauth2/internal/client_appengine.go +++ b/vendor/golang.org/x/oauth2/internal/client_appengine.go @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +//go:build appengine // +build appengine package internal diff --git a/vendor/modules.txt b/vendor/modules.txt index 6025d9fc171..dc73ec56b92 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -589,7 +589,7 @@ github.com/golang/snappy github.com/gomarkdown/markdown/ast github.com/gomarkdown/markdown/html github.com/gomarkdown/markdown/parser -# github.com/google/go-cmp v0.5.5 +# github.com/google/go-cmp v0.5.6 github.com/google/go-cmp/cmp github.com/google/go-cmp/cmp/internal/diff github.com/google/go-cmp/cmp/internal/flags @@ -601,8 +601,9 @@ github.com/google/go-github/v33/github github.com/google/go-intervals/intervalset # github.com/google/go-querystring v1.0.0 github.com/google/go-querystring/query -# github.com/google/gofuzz v1.1.0 +# github.com/google/gofuzz v1.2.0 github.com/google/gofuzz +github.com/google/gofuzz/bytesource # github.com/google/renameio v1.0.1 ## explicit github.com/google/renameio @@ -620,7 +621,7 @@ github.com/gorilla/mux # github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 ## explicit github.com/grpc-ecosystem/go-grpc-middleware -# github.com/hashicorp/errwrap v1.0.0 +# github.com/hashicorp/errwrap v1.1.0 github.com/hashicorp/errwrap # github.com/hashicorp/go-cleanhttp v0.5.1 github.com/hashicorp/go-cleanhttp @@ -638,6 +639,12 @@ github.com/hpcloud/tail/winfile github.com/imdario/mergo # github.com/inconshreveable/mousetrap v1.0.0 github.com/inconshreveable/mousetrap +# github.com/intel/goresctrl v0.0.0-20210623080121-be5fa857f4b4 +## explicit +github.com/intel/goresctrl/pkg/kubernetes +github.com/intel/goresctrl/pkg/log +github.com/intel/goresctrl/pkg/rdt +github.com/intel/goresctrl/pkg/utils # github.com/ishidawataru/sctp v0.0.0-20210226210310-f2269e66cdee github.com/ishidawataru/sctp # github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 @@ -834,7 +841,7 @@ github.com/prometheus/client_golang/prometheus/internal github.com/prometheus/client_golang/prometheus/promhttp # github.com/prometheus/client_model v0.2.0 github.com/prometheus/client_model/go -# github.com/prometheus/common v0.26.0 +# github.com/prometheus/common v0.29.0 github.com/prometheus/common/expfmt github.com/prometheus/common/internal/bitbucket.org/ww/goautoneg github.com/prometheus/common/model @@ -973,7 +980,7 @@ golang.org/x/mod/internal/lazyregexp golang.org/x/mod/modfile golang.org/x/mod/module golang.org/x/mod/semver -# golang.org/x/net v0.0.0-20210520170846-37e1c6afe023 +# golang.org/x/net v0.0.0-20210525063256-abc453219eb5 ## explicit golang.org/x/net/context golang.org/x/net/context/ctxhttp @@ -989,7 +996,7 @@ golang.org/x/net/internal/timeseries golang.org/x/net/proxy golang.org/x/net/trace golang.org/x/net/websocket -# golang.org/x/oauth2 v0.0.0-20210112200429-01de73cf58bd +# golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c golang.org/x/oauth2 golang.org/x/oauth2/internal # golang.org/x/sync v0.0.0-20210220032951-036812b2e83c @@ -1581,6 +1588,7 @@ sigs.k8s.io/structured-merge-diff/v4/schema sigs.k8s.io/structured-merge-diff/v4/typed sigs.k8s.io/structured-merge-diff/v4/value # sigs.k8s.io/yaml v1.2.0 +## explicit sigs.k8s.io/yaml # sigs.k8s.io/zeitgeist v0.3.0 ## explicit