John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "ProfileDataContainer.h" |
| 18 | |
Dan Albert | 55a3ec9 | 2017-10-12 13:27:26 -0700 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 21 | #include <cutils/ashmem.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 22 | #include <log/log.h> |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 23 | |
Dan Albert | 9b6a62f | 2017-12-12 16:08:11 -0800 | [diff] [blame] | 24 | #include <errno.h> |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 25 | #include <sys/mman.h> |
| 26 | |
| 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | |
| 30 | void ProfileDataContainer::freeData() { |
| 31 | if (mIsMapped) { |
| 32 | munmap(mData, sizeof(ProfileData)); |
| 33 | } else { |
| 34 | delete mData; |
| 35 | } |
| 36 | mIsMapped = false; |
| 37 | mData = nullptr; |
| 38 | } |
| 39 | |
| 40 | void ProfileDataContainer::rotateStorage() { |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 41 | std::lock_guard lock(mJankDataMutex); |
| 42 | |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 43 | // If we are mapped we want to stop using the ashmem backend and switch to malloc |
| 44 | // We are expecting a switchStorageToAshmem call to follow this, but it's not guaranteed |
| 45 | // If we aren't sitting on top of ashmem then just do a reset() as it's functionally |
| 46 | // equivalent do a free, malloc, reset. |
| 47 | if (mIsMapped) { |
| 48 | freeData(); |
| 49 | mData = new ProfileData; |
| 50 | } |
| 51 | mData->reset(); |
| 52 | } |
| 53 | |
| 54 | void ProfileDataContainer::switchStorageToAshmem(int ashmemfd) { |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 55 | std::lock_guard lock(mJankDataMutex); |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 56 | int regionSize = ashmem_get_size_region(ashmemfd); |
| 57 | if (regionSize < 0) { |
| 58 | int err = errno; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 59 | ALOGW("Failed to get ashmem region size from fd %d, err %d %s", ashmemfd, err, |
| 60 | strerror(err)); |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 61 | return; |
| 62 | } |
| 63 | if (regionSize < static_cast<int>(sizeof(ProfileData))) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 64 | ALOGW("Ashmem region is too small! Received %d, required %u", regionSize, |
| 65 | static_cast<unsigned int>(sizeof(ProfileData))); |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 66 | return; |
| 67 | } |
| 68 | ProfileData* newData = reinterpret_cast<ProfileData*>( |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 69 | mmap(NULL, sizeof(ProfileData), PROT_READ | PROT_WRITE, MAP_SHARED, ashmemfd, 0)); |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 70 | if (newData == MAP_FAILED) { |
| 71 | int err = errno; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 72 | ALOGW("Failed to move profile data to ashmem fd %d, error = %d", ashmemfd, err); |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 73 | return; |
| 74 | } |
| 75 | |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 76 | if (mData != nullptr) { |
| 77 | newData->mergeWith(*mData); |
| 78 | } |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 79 | freeData(); |
| 80 | mData = newData; |
| 81 | mIsMapped = true; |
| 82 | } |
| 83 | |
| 84 | } /* namespace uirenderer */ |
Dan Albert | 55a3ec9 | 2017-10-12 13:27:26 -0700 | [diff] [blame] | 85 | } /* namespace android */ |