xref: /aosp_15_r20/system/logging/logcat/process_names.h (revision 598139dc91b21518d67c408eaea2644226490971)
1*598139dcSAndroid Build Coastguard Worker /*
2*598139dcSAndroid Build Coastguard Worker  * Copyright (C) 2023 The Android Open Source Project
3*598139dcSAndroid Build Coastguard Worker  *
4*598139dcSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*598139dcSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*598139dcSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*598139dcSAndroid Build Coastguard Worker  *
8*598139dcSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*598139dcSAndroid Build Coastguard Worker  *
10*598139dcSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*598139dcSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*598139dcSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*598139dcSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*598139dcSAndroid Build Coastguard Worker  * limitations under the License.
15*598139dcSAndroid Build Coastguard Worker  */
16*598139dcSAndroid Build Coastguard Worker 
17*598139dcSAndroid Build Coastguard Worker #pragma once
18*598139dcSAndroid Build Coastguard Worker 
19*598139dcSAndroid Build Coastguard Worker #include <stdint.h>
20*598139dcSAndroid Build Coastguard Worker #include <string>
21*598139dcSAndroid Build Coastguard Worker 
22*598139dcSAndroid Build Coastguard Worker #include "utils/LruCache.h"
23*598139dcSAndroid Build Coastguard Worker 
24*598139dcSAndroid Build Coastguard Worker // An interface to associate pid to a name. Implemented by looking up /proc/PID.
25*598139dcSAndroid Build Coastguard Worker // To lower syscall impact, results are cached.
26*598139dcSAndroid Build Coastguard Worker class ProcessNames {
27*598139dcSAndroid Build Coastguard Worker   public:
ProcessNames()28*598139dcSAndroid Build Coastguard Worker     ProcessNames() : cache(kMaxCacheEntries) {}
29*598139dcSAndroid Build Coastguard Worker 
30*598139dcSAndroid Build Coastguard Worker     ~ProcessNames() = default;
31*598139dcSAndroid Build Coastguard Worker 
32*598139dcSAndroid Build Coastguard Worker     // Returns the executable name or in the case of an app, the package name associated
33*598139dcSAndroid Build Coastguard Worker     // with a process pid.
34*598139dcSAndroid Build Coastguard Worker     std::string Get(uint64_t pid);
35*598139dcSAndroid Build Coastguard Worker 
36*598139dcSAndroid Build Coastguard Worker   private:
37*598139dcSAndroid Build Coastguard Worker     const std::string ReadCmdline(uint64_t pid);
38*598139dcSAndroid Build Coastguard Worker     const std::string ReadComm(uint64_t pid);
39*598139dcSAndroid Build Coastguard Worker     const std::string Resolve(uint64_t pid);
40*598139dcSAndroid Build Coastguard Worker 
41*598139dcSAndroid Build Coastguard Worker     // kMaxCacheEntries should be picked to keep the memory footprint low (1) and yield a
42*598139dcSAndroid Build Coastguard Worker     // high cache hit rate (2).
43*598139dcSAndroid Build Coastguard Worker     // 1. We cache executable name or package name, which account for roughly 20 characters
44*598139dcSAndroid Build Coastguard Worker     //    each. Using a 100 figure results in 2 KiB for cache storage.
45*598139dcSAndroid Build Coastguard Worker     // 2. Difficult to tune since it depends on how many process are alive and how much they
46*598139dcSAndroid Build Coastguard Worker     //    generate towards liblob. From manual testing, 100 entries resulted in 99% cache hit
47*598139dcSAndroid Build Coastguard Worker     //    with AOSP 34, right after boot, and one app active. We could monitor this value by
48*598139dcSAndroid Build Coastguard Worker     //    augmenting the protobuffer and have a cache hit boolean to generate a cache hit figure
49*598139dcSAndroid Build Coastguard Worker     //    on the workstation.
50*598139dcSAndroid Build Coastguard Worker     static const uint64_t kMaxCacheEntries = 100;
51*598139dcSAndroid Build Coastguard Worker     android::LruCache<uint64_t, std::string> cache;
52*598139dcSAndroid Build Coastguard Worker };
53