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