Joel Fernandes | c5c0516 | 2018-10-16 13:26:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * time_in_state eBPF program |
| 3 | * |
| 4 | * Copyright (C) 2018 Google |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License version |
| 8 | * 2 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | |
Joel Fernandes (Google) | 4134dbb | 2019-02-12 12:47:25 -0500 | [diff] [blame] | 17 | #include <bpf_helpers.h> |
Joel Fernandes | c5c0516 | 2018-10-16 13:26:56 -0700 | [diff] [blame] | 18 | |
Maciej Żenczykowski | 162826e | 2019-04-17 18:03:17 -0700 | [diff] [blame] | 19 | typedef struct { |
Joel Fernandes | c5c0516 | 2018-10-16 13:26:56 -0700 | [diff] [blame] | 20 | uint32_t uid; |
| 21 | uint32_t freq; |
Maciej Żenczykowski | 162826e | 2019-04-17 18:03:17 -0700 | [diff] [blame] | 22 | } time_key; |
Joel Fernandes | c5c0516 | 2018-10-16 13:26:56 -0700 | [diff] [blame] | 23 | |
Maciej Żenczykowski | 162826e | 2019-04-17 18:03:17 -0700 | [diff] [blame] | 24 | DEFINE_BPF_MAP(uid_times_map, PERCPU_HASH, time_key, uint64_t, 10240) |
| 25 | DEFINE_BPF_MAP(cpu_last_update_map, PERCPU_ARRAY, uint32_t, uint64_t, 1) |
Joel Fernandes | c5c0516 | 2018-10-16 13:26:56 -0700 | [diff] [blame] | 26 | |
Maciej Żenczykowski | 162826e | 2019-04-17 18:03:17 -0700 | [diff] [blame] | 27 | /* Assume max of 1024 CPUs */ |
| 28 | DEFINE_BPF_MAP(cpu_freq_map, ARRAY, uint32_t, uint32_t, 1024) |
Joel Fernandes | c5c0516 | 2018-10-16 13:26:56 -0700 | [diff] [blame] | 29 | |
| 30 | struct switch_args { |
| 31 | unsigned long long ignore; |
| 32 | char prev_comm[16]; |
| 33 | int prev_pid; |
| 34 | int prev_prio; |
| 35 | long long prev_state; |
| 36 | char next_comm[16]; |
| 37 | int next_pid; |
| 38 | int next_prio; |
| 39 | }; |
| 40 | |
| 41 | SEC("tracepoint/sched/sched_switch") |
| 42 | int tp_sched_switch(struct switch_args* args) { |
| 43 | uint32_t zero = 0; |
Maciej Żenczykowski | 162826e | 2019-04-17 18:03:17 -0700 | [diff] [blame] | 44 | uint64_t* last = bpf_cpu_last_update_map_lookup_elem(&zero); |
Joel Fernandes | c5c0516 | 2018-10-16 13:26:56 -0700 | [diff] [blame] | 45 | if (!last) return 0; |
| 46 | uint64_t old_last = *last; |
| 47 | uint64_t time = bpf_ktime_get_ns(); |
| 48 | *last = time; |
| 49 | uint32_t cpu = bpf_get_smp_processor_id(); |
Maciej Żenczykowski | 162826e | 2019-04-17 18:03:17 -0700 | [diff] [blame] | 50 | uint32_t* freq = bpf_cpu_freq_map_lookup_elem(&cpu); |
Joel Fernandes | c5c0516 | 2018-10-16 13:26:56 -0700 | [diff] [blame] | 51 | if (args->prev_pid && old_last && freq && *freq) { |
| 52 | uint32_t uid = bpf_get_current_uid_gid(); |
Maciej Żenczykowski | 162826e | 2019-04-17 18:03:17 -0700 | [diff] [blame] | 53 | time_key key = {.uid = uid, .freq = *freq}; |
| 54 | uint64_t* tot_time = bpf_uid_times_map_lookup_elem(&key); |
Joel Fernandes | c5c0516 | 2018-10-16 13:26:56 -0700 | [diff] [blame] | 55 | uint64_t delta = time - old_last; |
| 56 | if (!tot_time) |
Maciej Żenczykowski | 162826e | 2019-04-17 18:03:17 -0700 | [diff] [blame] | 57 | bpf_uid_times_map_update_elem(&key, &delta, BPF_ANY); |
Joel Fernandes | c5c0516 | 2018-10-16 13:26:56 -0700 | [diff] [blame] | 58 | else |
| 59 | *tot_time += delta; |
| 60 | } |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | struct cpufreq_args { |
| 65 | unsigned long long ignore; |
| 66 | unsigned int state; |
| 67 | unsigned int cpu_id; |
| 68 | }; |
| 69 | |
| 70 | SEC("tracepoint/power/cpu_frequency") |
| 71 | int tp_cpufreq(struct cpufreq_args* args) { |
Maciej Żenczykowski | 162826e | 2019-04-17 18:03:17 -0700 | [diff] [blame] | 72 | uint32_t cpu = args->cpu_id; |
Joel Fernandes | c5c0516 | 2018-10-16 13:26:56 -0700 | [diff] [blame] | 73 | unsigned int new = args->state; |
Maciej Żenczykowski | 162826e | 2019-04-17 18:03:17 -0700 | [diff] [blame] | 74 | bpf_cpu_freq_map_update_elem(&cpu, &new, BPF_ANY); |
Joel Fernandes | c5c0516 | 2018-10-16 13:26:56 -0700 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | char _license[] SEC("license") = "GPL"; |