1 /* 2 * Copyright 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <bpf_helpers.h> 18 19 /* 20 * On Android the number of active processes using gpu is limited. 21 * So this is assumed to be true: SUM(num_procs_using_gpu[i]) <= 1024 22 */ 23 #define GPU_MEM_TOTAL_MAP_SIZE 1024 24 25 /* 26 * This map maintains the global and per process gpu memory total counters. 27 * 28 * The KEY is ((gpu_id << 32) | pid) while VAL is the size in bytes. 29 * Use HASH type here since key is not int. 30 * Pass AID_GRAPHICS as gid since gpuservice is in the graphics group. 31 */ 32 DEFINE_BPF_MAP_GRO(gpu_mem_total_map, HASH, uint64_t, uint64_t, GPU_MEM_TOTAL_MAP_SIZE, 33 AID_GRAPHICS); 34 35 /* This struct aligns with the fields offsets of the raw tracepoint format */ 36 struct gpu_mem_total_args { 37 uint64_t ignore; 38 /* Actual fields start at offset 8 */ 39 uint32_t gpu_id; 40 uint32_t pid; 41 uint64_t size; 42 }; 43 44 /* 45 * This program parses the gpu_mem/gpu_mem_total tracepoint's data into 46 * {KEY, VAL} pair used to update the corresponding bpf map. 47 * 48 * Pass AID_GRAPHICS as gid since gpuservice is in the graphics group. 49 * Upon seeing size 0, the corresponding KEY needs to be cleaned up. 50 */ 51 DEFINE_BPF_PROG("tracepoint/gpu_mem/gpu_mem_total", AID_ROOT, AID_GRAPHICS, tp_gpu_mem_total) 52 (struct gpu_mem_total_args* args) { 53 uint64_t key = 0; 54 uint64_t cur_val = 0; 55 uint64_t* prev_val = NULL; 56 57 /* The upper 32 bits are for gpu_id while the lower is the pid */ 58 key = ((uint64_t)args->gpu_id << 32) | args->pid; 59 cur_val = args->size; 60 61 if (!cur_val) { 62 bpf_gpu_mem_total_map_delete_elem(&key); 63 return 0; 64 } 65 66 prev_val = bpf_gpu_mem_total_map_lookup_elem(&key); 67 if (prev_val) { 68 *prev_val = cur_val; 69 } else { 70 bpf_gpu_mem_total_map_update_elem(&key, &cur_val, BPF_NOEXIST); 71 } 72 return 0; 73 } 74 75 LICENSE("Apache 2.0"); 76