blob: 6d22600e520f868d96ec0e3a10deacb737a42b4c [file] [log] [blame]
Primiano Tuccia6166482017-11-20 13:05:45 +00001/*
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 Tucci69ca1262017-11-20 19:42:22 +000019#include <fcntl.h>
20#include <stdint.h>
Primiano Tuccifc63ad42017-11-21 01:39:32 +000021#include <stdio.h>
Primiano Tucci69ca1262017-11-20 19:42:22 +000022#include <stdlib.h>
23#include <sys/mman.h>
24#include <sys/stat.h>
25#include <unistd.h>
26
Primiano Tuccia6166482017-11-20 13:05:45 +000027#include <memory>
28#include <utility>
29
Oystein Eftevaagdd727e42017-12-05 08:49:55 -080030#include "perfetto_base/logging.h"
Primiano Tuccia6166482017-11-20 13:05:45 +000031
32namespace perfetto {
33
Primiano Tucci69ca1262017-11-20 19:42:22 +000034// static
35std::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 Tuccifc63ad42017-11-21 01:39:32 +000039 FILE* tmp_file = tmpfile();
40 PERFETTO_CHECK(tmp_file);
41 base::ScopedFile fd(fileno(tmp_file));
Primiano Tucci69ca1262017-11-20 19:42:22 +000042 PERFETTO_CHECK(fd);
Primiano Tuccifc63ad42017-11-21 01:39:32 +000043 int res = ftruncate(fd.get(), static_cast<off_t>(size));
Primiano Tucci69ca1262017-11-20 19:42:22 +000044 PERFETTO_CHECK(res == 0);
45 return MapFD(std::move(fd), size);
46}
47
48// static
49std::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
58std::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
68PosixSharedMemory::PosixSharedMemory(void* start,
69 size_t size,
70 base::ScopedFile fd)
71 : start_(start), size_(size), fd_(std::move(fd)) {}
72
73PosixSharedMemory::~PosixSharedMemory() {
74 munmap(start(), size());
75}
76
Primiano Tuccia6166482017-11-20 13:05:45 +000077PosixSharedMemory::Factory::~Factory() {}
78
79std::unique_ptr<SharedMemory> PosixSharedMemory::Factory::CreateSharedMemory(
80 size_t size) {
Primiano Tucci69ca1262017-11-20 19:42:22 +000081 return PosixSharedMemory::Create(size);
Primiano Tuccia6166482017-11-20 13:05:45 +000082}
83
84} // namespace perfetto