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 <stdio.h>
19 #include <string.h>
20 #include <sys/mman.h>
21 #include <sys/stat.h>
22 #include <log/log.h>
23
24 #include <hardware/memtrack.h>
25
26 #include "memtrack_exynos.h"
27
28 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
29 #define min(x, y) ((x) < (y) ? (x) : (y))
30
31 #define ION_DEBUG_PATH "/sys/kernel/debug/ion/"
32 #define ION_DEBUG_CLIENT_PATH "/sys/kernel/debug/ion/clients"
33
34 static struct memtrack_record ion_record_templates[] = {
35 {
36 .flags = MEMTRACK_FLAG_SMAPS_UNACCOUNTED |
37 MEMTRACK_FLAG_PRIVATE |
38 MEMTRACK_FLAG_SYSTEM |
39 MEMTRACK_FLAG_NONSECURE,
40 },
41 };
42
ion_memtrack_get_memory(pid_t pid,int __unused type,struct memtrack_record * records,size_t * num_records)43 int ion_memtrack_get_memory(pid_t pid, int __unused type,
44 struct memtrack_record *records,
45 size_t *num_records)
46 {
47 size_t allocated_records = min(*num_records, ARRAY_SIZE(ion_record_templates));
48 FILE *fp;
49 struct stat s;
50 char line[1024];
51 char path[64];
52
53 *num_records = ARRAY_SIZE(ion_record_templates);
54
55 /* fastpath to return the necessary number of records */
56 if (allocated_records == 0) {
57 return 0;
58 }
59
60 if (lstat(ION_DEBUG_CLIENT_PATH, &s) == 0)
61 snprintf(path, sizeof(path), "%s/%d-0", ION_DEBUG_CLIENT_PATH, pid);
62 else
63 snprintf(path, sizeof(path), "%s/%d", ION_DEBUG_PATH, pid);
64
65 fp = fopen(path, "r");
66 if (fp == NULL) {
67 return -errno;
68 }
69
70 memcpy(records, ion_record_templates,
71 sizeof(struct memtrack_record) * allocated_records);
72
73 while (1) {
74 if (fgets(line, sizeof(line), fp) == NULL) {
75 break;
76 }
77
78 /* Format:
79 * heap_name: size_in_bytes
80 * ion_noncontig_he: 30134272
81 */
82 if (!strncmp(line, "ion_noncontig_he", 16)) {
83 unsigned int size_in_bytes;
84
85 int ret = sscanf(line, "%*s %u\n", &size_in_bytes);
86 if (ret == 1) {
87 records[0].size_in_bytes = size_in_bytes;
88 break;
89 }
90 }
91 }
92
93 fclose(fp);
94
95 return 0;
96 }
97