blob: 31972faca2e7aa03638e22fcc773497a5c9dc40d [file] [log] [blame]
Marco Nelissenc57fe212016-05-31 09:45:43 -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#define LOG_TAG "MediaUtils"
18#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
21#include <cutils/properties.h>
22#include <sys/resource.h>
23#include <unistd.h>
24
Peter Collingbourne007ac1a2018-11-29 15:08:46 -080025#include <bionic_malloc.h>
26
Marco Nelissenc57fe212016-05-31 09:45:43 -070027#include "MediaUtils.h"
28
Kostya Kortchinskye08896a2018-10-17 12:37:10 -070029extern "C" void __scudo_set_rss_limit(size_t, int) __attribute__((weak));
Evgenii Stepanovf1b05232017-08-02 16:36:56 -070030
Marco Nelissenc57fe212016-05-31 09:45:43 -070031namespace android {
32
Peter Collingbourne007ac1a2018-11-29 15:08:46 -080033void limitProcessMemory(const char *property, size_t numberOfBytes,
34 size_t percentageOfTotalMem) {
Marco Nelissenc57fe212016-05-31 09:45:43 -070035 long pageSize = sysconf(_SC_PAGESIZE);
36 long numPages = sysconf(_SC_PHYS_PAGES);
37 size_t maxMem = SIZE_MAX;
38
39 if (pageSize > 0 && numPages > 0) {
40 if (size_t(numPages) < SIZE_MAX / size_t(pageSize)) {
41 maxMem = size_t(numPages) * size_t(pageSize);
42 }
43 ALOGV("physMem: %zu", maxMem);
44 if (percentageOfTotalMem > 100) {
45 ALOGW("requested %zu%% of total memory, using 100%%", percentageOfTotalMem);
46 percentageOfTotalMem = 100;
47 }
48 maxMem = maxMem / 100 * percentageOfTotalMem;
49 if (numberOfBytes < maxMem) {
50 maxMem = numberOfBytes;
51 }
52 ALOGV("requested limit: %zu", maxMem);
53 } else {
54 ALOGW("couldn't determine total RAM");
55 }
56
57 int64_t propVal = property_get_int64(property, maxMem);
58 if (propVal > 0 && uint64_t(propVal) <= SIZE_MAX) {
59 maxMem = propVal;
60 }
Evgenii Stepanovf1b05232017-08-02 16:36:56 -070061
Peter Collingbourne007ac1a2018-11-29 15:08:46 -080062 // If Scudo is in use, enforce the hard RSS limit (in MB).
63 if (maxMem != SIZE_MAX && &__scudo_set_rss_limit != 0) {
Kostya Kortchinskye08896a2018-10-17 12:37:10 -070064 __scudo_set_rss_limit(maxMem >> 20, 1);
65 ALOGV("Scudo hard RSS limit set to %zu MB", maxMem >> 20);
66 return;
67 }
68
Peter Collingbourne007ac1a2018-11-29 15:08:46 -080069 if (!android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &maxMem,
70 sizeof(maxMem))) {
71 ALOGW("couldn't set allocation limit");
Evgenii Stepanovf1b05232017-08-02 16:36:56 -070072 }
Marco Nelissenc57fe212016-05-31 09:45:43 -070073}
74
75} // namespace android