Project: Inspektor Gadget (Observability and Analysis / Observability)
File: cpu.md

title: 'Using profile cpu' weight: 20 description: > Analyze CPU performance by sampling stack traces. The profile cpu gadget takes samples of the stack traces. Here we deploy a small demo pod "random": ```bash $ kubectl run --restart=Never --image=busybox random -- sh -c 'cat /dev/urandom > /dev/null' pod/random created ``` Using the profile cpu gadget, we can see the list of stack traces. The following command filters only for pods named "random", execute the command and interrupt it after ~30 seconds. The `-K` option is passed to show only the kernel stack traces. ```bash $ kubectl gadget profile cpu --podname random -K Capturing stack traces... Hit Ctrl-C to end.^C ``` After a while press with Ctrl-C to stop trace collection ``` K8S.NODE K8S.NAMESPACE K8S.POD K8S.CONTAINER PID COMM COUNT minikube default random random 340800 cat 1 entrySYSCALL64afterhwframe dosyscall64 x64sysread ksys_read vfs_read urandom_read urandomreadnowarn.isra.0 extract_crng extractcrng ... minikube default random random 340800 cat 9 entrySYSCALL64afterhwframe dosyscall64 x64sysread vfs_read ksys_read urandom_read urandomreadnowarn.isra.0 copyusergeneric_string ``` From the traces above, you can see that the pod is spending CPU time in the Linux function `urandom_read`. Instead of waiting, you can use the `--timeout` argument: ```bash $ kubectl gadget profile cpu --timeout 5 --podname random -K Capturing stack traces... K8S.NODE K8S.NAMESPACE K8S.POD K8S.CONTAINER PID COMM COUNT minikube default random random 340800 cat 1 entrySYSCALL64afterhwframe dosyscall64 x64sysread ksys_read vfs_read urandom_read urandomreadnowarn.isra.0 extract_crng extractcrng ... minikube default random random 340800 cat 9 entrySYSCALL64afterhwframe dosyscall64 x64sysread vfs_read ksys_read urandom_read urandomreadnowarn.isra.0 copyusergeneric_string ``` This gadget also supports custom column outputting, for example: ```bash $ kubectl gadget profile cpu --timeout 1 --podname random -o columns=k8s.node,k8s.pod Capturing stack traces... K8S.NODE K8S.POD minikube random ... minikube random ``` The following command is the same as default printing: ```bash $ kubectl gadget profile cpu --timeout 1 --podname random -o columns=k8s.node,k8s.namespace,k8s.pod,k8s.container,pid,comm,count Capturing stack traces... K8S.NODE K8S.NAMESPACE K8S.POD K8S.CONTAINER PID COMM COUNT minikube default random random 340800 cat 1 entrySYSCALL64afterhwframe dosyscall64 x64sysread ksys_read urandom_read vfs_read urandomreadnowarn.isra.0 extract_crng extractcrng locktextstart [unknown] [unknown] ... minikube default random random 340800 cat 1 entrySYSCALL64afterhwframe dosyscall64 x64sysread ksys_read urandom_read vfs_read urandomreadnowarn.isra.0 extract_crng extractcrng locktextstart [unknown] [unknown] ``` Finally, we need to clean up our pod: ```bash $ kubectl delete pod random ``` Generate some kernel load: ```bash $ docker run -d --rm --name random busybox cat /dev/urandom > /dev/null ``` Start `ig`: ```bash $ sudo ./ig profile cpu -K --containername random --runtimes docker ``` Observe the results: ```bash sudo ./ig profile cpu -K --containername random --runtimes docker Capturing stack traces... Hit Ctrl-C to end.^C RUNTIME.CONTAINERNAME COMM PID COUNT random cat 641045 1 entrySYSCALL64afterhwframe dosyscall64 x64syssendfile64 do_sendfile splicefileto_pipe genericfilesplice_read getrandombytes_user chachablockgeneric chacha_permute ... random cat 641045 5 entrySYSCALL64afterhwframe dosyscall64 x64syssendfile64 do_sendfile splicefileto_pipe genericfilesplice_read getrandombytes_user chachablockgeneric chacha_permute ``` Remove the docker container: ```bash $ docker stop random ```