1 /*
2  * Copyright (C) 2022 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 #pragma once
18 
19 #include <map>
20 #include <string>
21 #include <unistd.h>
22 
23 /*
24  * This header contains utilities to read the linux system /proc/pid files
25  *
26  * The format of this is not guaranteed to be stable, so use for diagnostic purposes only.
27  *
28  * The linux "proc" directory documentation:
29  * https://kernel.org/doc/Documentation/filesystems/proc.txt
30  * https://www.kernel.org/doc/html/latest/filesystems/proc.html?highlight=proc%20pid#chapter-3-per-process-parameters
31  */
32 
33 namespace android::mediautils {
34 
35 /**
36  * Return the thread schedule information for tid.
37  *
38  * String will be empty if the process does not have permission to
39  * access the /proc/pid tables, or if not on a Linux device.
40  *
41  * Linux scheduler documentation:
42  * https://www.kernel.org/doc/html/latest/scheduler/index.html
43  * https://man7.org/linux/man-pages/man7/sched.7.html
44  *
45  * Sample as follows:
46 
47 AudioOut_8D (10800, #threads: 36)
48 -------------------------------------------------------------------
49 se.exec_start                                :       8132077.598026
50 se.vruntime                                  :        798689.872087
51 se.sum_exec_runtime                          :        136466.957838
52 se.nr_migrations                             :               132487
53 se.statistics.sum_sleep_runtime              :       5629794.565945
54 se.statistics.wait_start                     :             0.000000
55 se.statistics.sleep_start                    :       8195727.586392
56 se.statistics.block_start                    :             0.000000
57 se.statistics.sleep_max                      :       1995665.869808
58 se.statistics.block_max                      :             0.591675
59 se.statistics.exec_max                       :             2.477580
60 se.statistics.slice_max                      :             0.000000
61 se.statistics.wait_max                       :             8.608642
62 se.statistics.wait_sum                       :          4683.266835
63 se.statistics.wait_count                     :               300964
64 se.statistics.iowait_sum                     :             0.000000
65 se.statistics.iowait_count                   :                    0
66 se.statistics.nr_migrations_cold             :                    0
67 se.statistics.nr_failed_migrations_affine    :                  297
68 se.statistics.nr_failed_migrations_running   :                 1412
69 se.statistics.nr_failed_migrations_hot       :                   96
70 se.statistics.nr_forced_migrations           :                   26
71 se.statistics.nr_wakeups                     :               281263
72 se.statistics.nr_wakeups_sync                :                   84
73 se.statistics.nr_wakeups_migrate             :               132322
74 se.statistics.nr_wakeups_local               :                 2165
75 se.statistics.nr_wakeups_remote              :               279098
76 se.statistics.nr_wakeups_affine              :                    0
77 se.statistics.nr_wakeups_affine_attempts     :                    0
78 se.statistics.nr_wakeups_passive             :                    0
79 se.statistics.nr_wakeups_idle                :                    0
80 avg_atom                                     :             0.453434
81 avg_per_cpu                                  :             1.030040
82 nr_switches                                  :               300963
83 nr_voluntary_switches                        :               281252
84 nr_involuntary_switches                      :                19711
85 se.load.weight                               :             73477120
86 se.avg.load_sum                              :                   58
87 se.avg.runnable_sum                          :                27648
88 se.avg.util_sum                              :                21504
89 se.avg.load_avg                              :                   48
90 se.avg.runnable_avg                          :                    0
91 se.avg.util_avg                              :                    0
92 se.avg.last_update_time                      :        8132075824128
93 se.avg.util_est.ewma                         :                    8
94 se.avg.util_est.enqueued                     :                    1
95 uclamp.min                                   :                    0
96 uclamp.max                                   :                 1024
97 effective uclamp.min                         :                    0
98 effective uclamp.max                         :                 1024
99 policy                                       :                    0
100 prio                                         :                  101
101 clock-delta                                  :                  163
102 */
103 std::string getThreadSchedAsString(pid_t tid);
104 
105 /**
106  * Returns map for the raw thread schedule string.
107  */
108 std::map<std::string, double> parseThreadSchedString(const std::string& schedString);
109 
110 /**
111  * Returns map for /proc/pid/task/tid/sched
112  */
getThreadSchedAsMap(pid_t tid)113 inline std::map<std::string, double> getThreadSchedAsMap(pid_t tid) {
114     return parseThreadSchedString(getThreadSchedAsString(tid));
115 }
116 
117 // TODO: Extend to other /proc/pid file information.
118 //
119 // See "ps" command get_ps().
120 // https://cs.android.com/android/platform/superproject/+/master:external/toybox/toys/posix/ps.c;l=707
121 
122 } // android::mediautils
123