• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 #pragma once
18 
19 #include <stdint.h>
20 #include <time.h>
21 
22 __attribute__((always_inline))
Nanotime()23 static uint64_t Nanotime() {
24   struct timespec t = {};
25   clock_gettime(CLOCK_MONOTONIC, &t);
26   return static_cast<uint64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec;
27 }
28 
29 __attribute__((always_inline))
MakeAllocationResident(void * ptr,size_t nbytes,int64_t present_bytes,int pagesize)30 static void MakeAllocationResident(void* ptr, size_t nbytes, int64_t present_bytes,
31                                                    int pagesize) {
32   if (present_bytes != -1 && static_cast<size_t>(present_bytes) < nbytes) {
33     nbytes = present_bytes;
34   }
35 
36   size_t start = 0;
37   uintptr_t page_aligned = reinterpret_cast<uintptr_t>(__builtin_align_up(ptr, pagesize));
38   uint8_t* data = reinterpret_cast<uint8_t*>(ptr);
39   if (page_aligned != reinterpret_cast<uintptr_t>(data)) {
40     // Make the first page of the allocation resident.
41     data[0] = 1;
42 
43     // Skip to the start of the next page.
44     start = page_aligned - reinterpret_cast<uintptr_t>(ptr);
45   }
46   for (size_t i = start; i < nbytes; i += pagesize) {
47     data[i] = 1;
48   }
49 }
50