1/*
2 * Copyright (C) 2013 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 <errno.h>
18#include <stdbool.h>
19#include <stdio.h>
20#include <string.h>
21#include <sys/mman.h>
22
23#include <hardware/memtrack.h>
24
25#include "memtrack_msm.h"
26
27#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
28#define min(x, y) ((x) < (y) ? (x) : (y))
29
30struct memtrack_record record_templates[] = {
31    {
32        .flags = MEMTRACK_FLAG_SMAPS_ACCOUNTED |
33                 MEMTRACK_FLAG_PRIVATE |
34                 MEMTRACK_FLAG_NONSECURE,
35    },
36    {
37        .flags = MEMTRACK_FLAG_SMAPS_UNACCOUNTED |
38                 MEMTRACK_FLAG_PRIVATE |
39                 MEMTRACK_FLAG_NONSECURE,
40    },
41};
42
43int kgsl_memtrack_get_memory(pid_t pid, enum memtrack_type type,
44                             struct memtrack_record *records,
45                             size_t *num_records)
46{
47    size_t allocated_records = min(*num_records, ARRAY_SIZE(record_templates));
48    int i;
49    FILE *fp;
50    char line[1024];
51    char tmp[128];
52    bool is_surfaceflinger = false;
53    size_t accounted_size = 0;
54    size_t unaccounted_size = 0;
55
56    *num_records = ARRAY_SIZE(record_templates);
57
58    /* fastpath to return the necessary number of records */
59    if (allocated_records == 0) {
60        return 0;
61    }
62
63    snprintf(tmp, sizeof(tmp), "/proc/%d/cmdline", pid);
64    fp = fopen(tmp, "r");
65    if (fp != NULL) {
66        if (fgets(line, sizeof(line), fp)) {
67            if (strcmp(line, "/system/bin/surfaceflinger") == 0)
68                is_surfaceflinger = true;
69        }
70        fclose(fp);
71    }
72
73    memcpy(records, record_templates,
74           sizeof(struct memtrack_record) * allocated_records);
75
76    sprintf(tmp, "/d/kgsl/proc/%d/mem", pid);
77    fp = fopen(tmp, "r");
78    if (fp == NULL) {
79        return -errno;
80    }
81
82    /* Go through each line of <pid>/mem file and for every entry of type "gpumem"
83     * check if the gpubuffer entry is usermapped or not. If the entry is usermapped
84     * count the entry as accounted else count the entry as unaccounted.
85     */
86    while (1) {
87        unsigned long size;
88        char line_type[7];
89        char flags[7];
90        char line_usage[19];
91        int ret;
92
93        if (fgets(line, sizeof(line), fp) == NULL) {
94            break;
95        }
96
97        /* Format:
98         *  gpuaddr useraddr     size    id flags       type            usage sglen
99         * 545ba000 545ba000     4096     1 ----pY     gpumem      arraybuffer     1
100         */
101        ret = sscanf(line, "%*x %*lx %lu %*d %6s %6s %18s %*d\n",
102                     &size, flags, line_type, line_usage);
103        if (ret != 4) {
104            continue;
105        }
106
107        if (type == MEMTRACK_TYPE_GL && strcmp(line_type, "gpumem") == 0) {
108            if (flags[6] == 'Y')
109                accounted_size += size;
110            else
111                unaccounted_size += size;
112        } else if (type == MEMTRACK_TYPE_GRAPHICS && strcmp(line_type, "ion") == 0) {
113            if (!is_surfaceflinger || strcmp(line_usage, "egl_image") != 0) {
114                unaccounted_size += size;
115            }
116        }
117    }
118
119    if (allocated_records > 0) {
120        records[0].size_in_bytes = accounted_size;
121    }
122    if (allocated_records > 1) {
123        records[1].size_in_bytes = unaccounted_size;
124    }
125
126    fclose(fp);
127
128    return 0;
129}
130