blob: 83b8af36011874c4f3efcdbef86d20639b14b92a [file] [log] [blame]
Marco Nelissen1438f0b2016-05-13 10:43:19 -07001/*
2 * Copyright (C) 2016 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// for property functions below
18#define __STDINT_LIMITS
19#include <stdint.h>
20#define __STDC_FORMAT_MACROS
21#include <inttypes.h>
22
23
24#define LOG_TAG "MediaUtils"
25#define LOG_NDEBUG 0
26#include <utils/Log.h>
27
28#include <cutils/properties.h>
29#include <sys/resource.h>
30#include <unistd.h>
31
32#include "MediaUtils.h"
33
34namespace android {
35
36// following property functions borrowed from lmp properties.c
37static intmax_t property_get_imax(const char *key, intmax_t lower_bound, intmax_t upper_bound,
38 intmax_t default_value) {
39 if (!key) {
40 return default_value;
41 }
42
43 intmax_t result = default_value;
44 char buf[PROPERTY_VALUE_MAX] = {'\0',};
45 char *end = NULL;
46
47 int len = property_get(key, buf, "");
48 if (len > 0) {
49 int tmp = errno;
50 errno = 0;
51
52 // Infer base automatically
53 result = strtoimax(buf, &end, /*base*/0);
54 if ((result == INTMAX_MIN || result == INTMAX_MAX) && errno == ERANGE) {
55 // Over or underflow
56 result = default_value;
57 ALOGV("%s(%s,%" PRIdMAX ") - overflow", __FUNCTION__, key, default_value);
58 } else if (result < lower_bound || result > upper_bound) {
59 // Out of range of requested bounds
60 result = default_value;
61 ALOGV("%s(%s,%" PRIdMAX ") - out of range", __FUNCTION__, key, default_value);
62 } else if (end == buf) {
63 // Numeric conversion failed
64 result = default_value;
65 ALOGV("%s(%s,%" PRIdMAX ") - numeric conversion failed",
66 __FUNCTION__, key, default_value);
67 }
68
69 errno = tmp;
70 }
71
72 return result;
73}
74
75static int64_t property_get_int64(const char *key, int64_t default_value) {
76 return (int64_t)property_get_imax(key, INT64_MIN, INT64_MAX, default_value);
77}
78
79void limitProcessMemory(
80 const char *property,
81 size_t numberOfBytes,
82 size_t percentageOfTotalMem) {
83
84 long pageSize = sysconf(_SC_PAGESIZE);
85 long numPages = sysconf(_SC_PHYS_PAGES);
86 size_t maxMem = SIZE_MAX;
87
88 if (pageSize > 0 && numPages > 0) {
89 if (size_t(numPages) < SIZE_MAX / size_t(pageSize)) {
90 maxMem = size_t(numPages) * size_t(pageSize);
91 }
92 ALOGV("physMem: %zu", maxMem);
93 if (percentageOfTotalMem > 100) {
94 ALOGW("requested %zu%% of total memory, using 100%%", percentageOfTotalMem);
95 percentageOfTotalMem = 100;
96 }
97 maxMem = maxMem / 100 * percentageOfTotalMem;
98 if (numberOfBytes < maxMem) {
99 maxMem = numberOfBytes;
100 }
101 ALOGV("requested limit: %zu", maxMem);
102 } else {
103 ALOGW("couldn't determine total RAM");
104 }
105
106 int64_t propVal = property_get_int64(property, maxMem);
107 if (propVal > 0 && uint64_t(propVal) <= SIZE_MAX) {
108 maxMem = propVal;
109 }
110 ALOGV("actual limit: %zu", maxMem);
111
112 struct rlimit limit;
113 getrlimit(RLIMIT_AS, &limit);
114 ALOGV("original limits: %lld/%lld", (long long)limit.rlim_cur, (long long)limit.rlim_max);
115 limit.rlim_cur = maxMem;
116 setrlimit(RLIMIT_AS, &limit);
117 limit.rlim_cur = -1;
118 limit.rlim_max = -1;
119 getrlimit(RLIMIT_AS, &limit);
120 ALOGV("new limits: %lld/%lld", (long long)limit.rlim_cur, (long long)limit.rlim_max);
121
122}
123
124
125} // namespace android