# Copyright 2019 Google LLC
#
# 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
#
#     https://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.
#
#
#
# Change TARGET_PGREP below to names of processes to trace.
# Also change KERNEL_VERSION below to version being tested.

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: enable-perf-trace
  labels:
    app: enable-perf-trace
spec:
  selector:
    matchLabels:
      name: enable-perf-trace
  template:
    metadata:
      labels:
        name: enable-perf-trace
    spec:
      nodeSelector:
        "enable-perf": "true"
      hostPID: true
      volumes:
        - name: host
          hostPath:
            path: /
      containers:
        - name: enable-perf-trace
          image: debian
          imagePullPolicy: Always
          volumeMounts:
            - name: host
              mountPath: /host
          securityContext:
            privileged: true
          env:
            # TARGET_PGREP options
            # Values:
            #   empty or unset to do full trace
            #   pgrep query to filter
            - name: TARGET_PGREP
              value: "kubelet"
          command:
            - /bin/bash
            - -c
            - |

              set -o errexit
              set -o pipefail
              set -o nounset

              MAX_LOG_SIZE="16777215" # 16 MB
              MAX_LOGS="2000"
              KERNEL_VERSION="5.0.0"

              apt-get update && apt-get install -y curl procps daemontools build-essential bison flex libelf-dev binutils-dev
              curl -O https://mirrors.edge.kernel.org/pub/linux/kernel/tools/perf/v"${KERNEL_VERSION}"/perf-"${KERNEL_VERSION}".tar.gz
              tar xzf perf-"${KERNEL_VERSION}".tar.gz
              make -C perf-"${KERNEL_VERSION}"/tools/perf install
              PERF="/root/bin/perf"

              d=$(date '+%Y-%m-%dT%H:%M:%SZ')

              out_dir="/host/var/log/perf_trace/${d}"
              mkdir -p "${out_dir}"

              echo "starting perf! will dump to ${out_dir}"

              if [[ -z "${TARGET_PGREP+x}" ]]; then
                echo "full system perf trace"
                "${PERF}" trace |& multilog t s${MAX_LOG_SIZE} n${MAX_LOGS} "${out_dir}"
              else
                echo "PID perf trace"
                PIDS=$(pgrep "${TARGET_PGREP}" -d ",")
                echo "staring perf on pids == ${PIDS}"
                "${PERF}" trace --pid="${PIDS}" |& multilog t s${MAX_LOG_SIZE} n${MAX_LOGS} "${out_dir}"
              fi

Add DaemonSet to run perf
Add DaemonSet to run perf
