Primiano Tucci | a616648 | 2017-11-20 13:05:45 +0000 | [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 "tracing/src/ipc/posix_shared_memory.h" |
| 18 | |
Primiano Tucci | 69ca126 | 2017-11-20 19:42:22 +0000 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | #include <stdint.h> |
Primiano Tucci | fc63ad4 | 2017-11-21 01:39:32 +0000 | [diff] [blame] | 21 | #include <stdio.h> |
Primiano Tucci | 69ca126 | 2017-11-20 19:42:22 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <sys/mman.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <unistd.h> |
| 26 | |
Primiano Tucci | a616648 | 2017-11-20 13:05:45 +0000 | [diff] [blame] | 27 | #include <memory> |
| 28 | #include <utility> |
| 29 | |
| 30 | #include "base/logging.h" |
| 31 | |
| 32 | namespace perfetto { |
| 33 | |
Primiano Tucci | 69ca126 | 2017-11-20 19:42:22 +0000 | [diff] [blame] | 34 | // static |
| 35 | std::unique_ptr<PosixSharedMemory> PosixSharedMemory::Create(size_t size) { |
| 36 | // TODO: use memfd_create on Linux/Android if the kernel supports it (needs |
| 37 | // syscall.h, there is no glibc wrapper). If not, on Android fallback on |
| 38 | // ashmem and on Linux fallback on /dev/shm/perfetto-whatever. |
Primiano Tucci | fc63ad4 | 2017-11-21 01:39:32 +0000 | [diff] [blame] | 39 | FILE* tmp_file = tmpfile(); |
| 40 | PERFETTO_CHECK(tmp_file); |
| 41 | base::ScopedFile fd(fileno(tmp_file)); |
Primiano Tucci | 69ca126 | 2017-11-20 19:42:22 +0000 | [diff] [blame] | 42 | PERFETTO_CHECK(fd); |
Primiano Tucci | fc63ad4 | 2017-11-21 01:39:32 +0000 | [diff] [blame] | 43 | int res = ftruncate(fd.get(), static_cast<off_t>(size)); |
Primiano Tucci | 69ca126 | 2017-11-20 19:42:22 +0000 | [diff] [blame] | 44 | PERFETTO_CHECK(res == 0); |
| 45 | return MapFD(std::move(fd), size); |
| 46 | } |
| 47 | |
| 48 | // static |
| 49 | std::unique_ptr<PosixSharedMemory> PosixSharedMemory::AttachToFd( |
| 50 | base::ScopedFile fd) { |
| 51 | struct stat stat_buf = {}; |
| 52 | int res = fstat(fd.get(), &stat_buf); |
| 53 | PERFETTO_CHECK(res == 0 && stat_buf.st_size > 0); |
| 54 | return MapFD(std::move(fd), static_cast<size_t>(stat_buf.st_size)); |
| 55 | } |
| 56 | |
| 57 | // static |
| 58 | std::unique_ptr<PosixSharedMemory> PosixSharedMemory::MapFD(base::ScopedFile fd, |
| 59 | size_t size) { |
| 60 | PERFETTO_DCHECK(fd); |
| 61 | PERFETTO_DCHECK(size > 0); |
| 62 | void* start = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd.get(), 0); |
| 63 | PERFETTO_CHECK(start != MAP_FAILED); |
| 64 | return std::unique_ptr<PosixSharedMemory>( |
| 65 | new PosixSharedMemory(start, size, std::move(fd))); |
| 66 | } |
| 67 | |
| 68 | PosixSharedMemory::PosixSharedMemory(void* start, |
| 69 | size_t size, |
| 70 | base::ScopedFile fd) |
| 71 | : start_(start), size_(size), fd_(std::move(fd)) {} |
| 72 | |
| 73 | PosixSharedMemory::~PosixSharedMemory() { |
| 74 | munmap(start(), size()); |
| 75 | } |
| 76 | |
Primiano Tucci | a616648 | 2017-11-20 13:05:45 +0000 | [diff] [blame] | 77 | PosixSharedMemory::Factory::~Factory() {} |
| 78 | |
| 79 | std::unique_ptr<SharedMemory> PosixSharedMemory::Factory::CreateSharedMemory( |
| 80 | size_t size) { |
Primiano Tucci | 69ca126 | 2017-11-20 19:42:22 +0000 | [diff] [blame] | 81 | return PosixSharedMemory::Create(size); |
Primiano Tucci | a616648 | 2017-11-20 13:05:45 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | } // namespace perfetto |