github.com/inspektor-gadget/inspektor-gadget@v0.28.1/docs/builtin-gadgets/profile/cpu.md (about) 1 --- 2 title: 'Using profile cpu' 3 weight: 20 4 description: > 5 Analyze CPU performance by sampling stack traces. 6 --- 7 8 The profile cpu gadget takes samples of the stack traces. 9 10 ### On Kubernetes 11 12 Here we deploy a small demo pod "random": 13 14 ```bash 15 $ kubectl run --restart=Never --image=busybox random -- sh -c 'cat /dev/urandom > /dev/null' 16 pod/random created 17 ``` 18 19 Using the profile cpu gadget, we can see the list of stack traces. 20 The following command filters only for pods named "random", execute the command 21 and interrupt it after ~30 seconds. The `-K` option is passed to show only the 22 kernel stack traces. 23 24 ```bash 25 $ kubectl gadget profile cpu --podname random -K 26 Capturing stack traces... Hit Ctrl-C to end.^C 27 ``` 28 29 After a while press with Ctrl-C to stop trace collection 30 31 ``` 32 K8S.NODE K8S.NAMESPACE K8S.POD K8S.CONTAINER PID COMM COUNT 33 minikube default random random 340800 cat 1 34 entry_SYSCALL_64_after_hwframe 35 do_syscall_64 36 __x64_sys_read 37 ksys_read 38 vfs_read 39 urandom_read 40 urandom_read_nowarn.isra.0 41 extract_crng 42 _extract_crng 43 ... 44 minikube default random random 340800 cat 9 45 entry_SYSCALL_64_after_hwframe 46 do_syscall_64 47 __x64_sys_read 48 vfs_read 49 ksys_read 50 urandom_read 51 urandom_read_nowarn.isra.0 52 copy_user_generic_string 53 ``` 54 55 From the traces above, you can see that the pod is spending CPU time in the 56 Linux function `urandom_read`. 57 58 Instead of waiting, you can use the `--timeout` argument: 59 60 ```bash 61 $ kubectl gadget profile cpu --timeout 5 --podname random -K 62 Capturing stack traces... 63 K8S.NODE K8S.NAMESPACE K8S.POD K8S.CONTAINER PID COMM COUNT 64 minikube default random random 340800 cat 1 65 entry_SYSCALL_64_after_hwframe 66 do_syscall_64 67 __x64_sys_read 68 ksys_read 69 vfs_read 70 urandom_read 71 urandom_read_nowarn.isra.0 72 extract_crng 73 _extract_crng 74 ... 75 minikube default random random 340800 cat 9 76 entry_SYSCALL_64_after_hwframe 77 do_syscall_64 78 __x64_sys_read 79 vfs_read 80 ksys_read 81 urandom_read 82 urandom_read_nowarn.isra.0 83 copy_user_generic_string 84 ``` 85 86 This gadget also supports custom column outputting, for example: 87 88 ```bash 89 $ kubectl gadget profile cpu --timeout 1 --podname random -o columns=k8s.node,k8s.pod 90 Capturing stack traces... 91 K8S.NODE K8S.POD 92 minikube random 93 ... 94 minikube random 95 ``` 96 97 The following command is the same as default printing: 98 99 ```bash 100 $ kubectl gadget profile cpu --timeout 1 --podname random -o columns=k8s.node,k8s.namespace,k8s.pod,k8s.container,pid,comm,count 101 Capturing stack traces... 102 K8S.NODE K8S.NAMESPACE K8S.POD K8S.CONTAINER PID COMM COUNT 103 minikube default random random 340800 cat 1 104 entry_SYSCALL_64_after_hwframe 105 do_syscall_64 106 __x64_sys_read 107 ksys_read 108 urandom_read 109 vfs_read 110 urandom_read_nowarn.isra.0 111 extract_crng 112 _extract_crng 113 __lock_text_start 114 [unknown] 115 [unknown] 116 ... 117 minikube default random random 340800 cat 1 118 entry_SYSCALL_64_after_hwframe 119 do_syscall_64 120 __x64_sys_read 121 ksys_read 122 urandom_read 123 vfs_read 124 urandom_read_nowarn.isra.0 125 extract_crng 126 _extract_crng 127 __lock_text_start 128 [unknown] 129 [unknown] 130 ``` 131 132 Finally, we need to clean up our pod: 133 134 ```bash 135 $ kubectl delete pod random 136 ``` 137 138 ### With `ig` 139 140 * Generate some kernel load: 141 142 ```bash 143 $ docker run -d --rm --name random busybox cat /dev/urandom > /dev/null 144 ``` 145 146 * Start `ig`: 147 148 ```bash 149 $ sudo ./ig profile cpu -K --containername random --runtimes docker 150 ``` 151 152 * Observe the results: 153 154 ```bash 155 sudo ./ig profile cpu -K --containername random --runtimes docker 156 Capturing stack traces... Hit Ctrl-C to end.^C 157 RUNTIME.CONTAINERNAME COMM PID COUNT 158 random cat 641045 1 159 entry_SYSCALL_64_after_hwframe 160 do_syscall_64 161 __x64_sys_sendfile64 162 do_sendfile 163 splice_file_to_pipe 164 generic_file_splice_read 165 get_random_bytes_user 166 chacha_block_generic 167 chacha_permute 168 ... 169 random cat 641045 5 170 entry_SYSCALL_64_after_hwframe 171 do_syscall_64 172 __x64_sys_sendfile64 173 do_sendfile 174 splice_file_to_pipe 175 generic_file_splice_read 176 get_random_bytes_user 177 chacha_block_generic 178 chacha_permute 179 ``` 180 181 * Remove the docker container: 182 183 ```bash 184 $ docker stop random 185 ```