Marco Nelissen | c57fe21 | 2016-05-31 09:45:43 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Christopher Ferris | 7a3180d | 2019-09-11 19:08:13 -0700 | [diff] [blame] | 25 | #include <bionic/malloc.h> |
Andy Hung | d7e0dd4 | 2020-03-27 16:42:34 -0700 | [diff] [blame] | 26 | #include <mediautils/LimitProcessMemory.h> |
Marco Nelissen | c57fe21 | 2016-05-31 09:45:43 -0700 | [diff] [blame] | 27 | |
Kostya Kortchinsky | e08896a | 2018-10-17 12:37:10 -0700 | [diff] [blame] | 28 | extern "C" void __scudo_set_rss_limit(size_t, int) __attribute__((weak)); |
Evgenii Stepanov | f1b0523 | 2017-08-02 16:36:56 -0700 | [diff] [blame] | 29 | |
Marco Nelissen | c57fe21 | 2016-05-31 09:45:43 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | |
Peter Collingbourne | 007ac1a | 2018-11-29 15:08:46 -0800 | [diff] [blame] | 32 | void limitProcessMemory(const char *property, size_t numberOfBytes, |
| 33 | size_t percentageOfTotalMem) { |
Marco Nelissen | c57fe21 | 2016-05-31 09:45:43 -0700 | [diff] [blame] | 34 | long pageSize = sysconf(_SC_PAGESIZE); |
| 35 | long numPages = sysconf(_SC_PHYS_PAGES); |
| 36 | size_t maxMem = SIZE_MAX; |
| 37 | |
| 38 | if (pageSize > 0 && numPages > 0) { |
| 39 | if (size_t(numPages) < SIZE_MAX / size_t(pageSize)) { |
| 40 | maxMem = size_t(numPages) * size_t(pageSize); |
| 41 | } |
| 42 | ALOGV("physMem: %zu", maxMem); |
| 43 | if (percentageOfTotalMem > 100) { |
| 44 | ALOGW("requested %zu%% of total memory, using 100%%", percentageOfTotalMem); |
| 45 | percentageOfTotalMem = 100; |
| 46 | } |
| 47 | maxMem = maxMem / 100 * percentageOfTotalMem; |
| 48 | if (numberOfBytes < maxMem) { |
| 49 | maxMem = numberOfBytes; |
| 50 | } |
| 51 | ALOGV("requested limit: %zu", maxMem); |
| 52 | } else { |
| 53 | ALOGW("couldn't determine total RAM"); |
| 54 | } |
| 55 | |
| 56 | int64_t propVal = property_get_int64(property, maxMem); |
| 57 | if (propVal > 0 && uint64_t(propVal) <= SIZE_MAX) { |
| 58 | maxMem = propVal; |
| 59 | } |
Evgenii Stepanov | f1b0523 | 2017-08-02 16:36:56 -0700 | [diff] [blame] | 60 | |
Peter Collingbourne | 007ac1a | 2018-11-29 15:08:46 -0800 | [diff] [blame] | 61 | // If Scudo is in use, enforce the hard RSS limit (in MB). |
| 62 | if (maxMem != SIZE_MAX && &__scudo_set_rss_limit != 0) { |
Kostya Kortchinsky | e08896a | 2018-10-17 12:37:10 -0700 | [diff] [blame] | 63 | __scudo_set_rss_limit(maxMem >> 20, 1); |
| 64 | ALOGV("Scudo hard RSS limit set to %zu MB", maxMem >> 20); |
| 65 | return; |
| 66 | } |
| 67 | |
Peter Collingbourne | 007ac1a | 2018-11-29 15:08:46 -0800 | [diff] [blame] | 68 | if (!android_mallopt(M_SET_ALLOCATION_LIMIT_BYTES, &maxMem, |
| 69 | sizeof(maxMem))) { |
| 70 | ALOGW("couldn't set allocation limit"); |
Evgenii Stepanov | f1b0523 | 2017-08-02 16:36:56 -0700 | [diff] [blame] | 71 | } |
Marco Nelissen | c57fe21 | 2016-05-31 09:45:43 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | } // namespace android |