| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -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 "util.h" |
| 18 | |
| Anders Lewis | a98a5fb | 2017-08-09 16:52:19 -0700 | [diff] [blame] | 19 | #include <err.h> |
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 20 | #include <math.h> |
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 21 | #include <sched.h> |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| Elliott Hughes | e86a03f | 2025-06-05 09:23:19 -0700 | [diff] [blame] | 24 | #include <sys/param.h> |
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 25 | #include <wchar.h> |
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 26 | |
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 27 | #include <cstdlib> |
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 28 | |
| 29 | // This function returns a pointer less than 2 * alignment + or_mask bytes into the array. |
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 30 | char* GetAlignedMemory(char* orig_ptr, size_t alignment, size_t or_mask) { |
| Elliott Hughes | e86a03f | 2025-06-05 09:23:19 -0700 | [diff] [blame] | 31 | if (!powerof2(alignment)) { |
| Anders Lewis | a98a5fb | 2017-08-09 16:52:19 -0700 | [diff] [blame] | 32 | errx(1, "warning: alignment passed into GetAlignedMemory is not a power of two."); |
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 33 | } |
| 34 | if (or_mask > alignment) { |
| Anders Lewis | a98a5fb | 2017-08-09 16:52:19 -0700 | [diff] [blame] | 35 | errx(1, "warning: or_mask passed into GetAlignedMemory is too high."); |
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 36 | } |
| 37 | uintptr_t ptr = reinterpret_cast<uintptr_t>(orig_ptr); |
| 38 | if (alignment > 0) { |
| 39 | // When setting the alignment, set it to exactly the alignment chosen. |
| 40 | // The pointer returned will be guaranteed not to be aligned to anything |
| 41 | // more than that. |
| 42 | ptr += alignment - (ptr & (alignment - 1)); |
| 43 | ptr |= alignment | or_mask; |
| 44 | } |
| 45 | |
| 46 | return reinterpret_cast<char*>(ptr); |
| 47 | } |
| 48 | |
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 49 | char* GetAlignedPtr(std::vector<char>* buf, size_t alignment, size_t nbytes) { |
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 50 | buf->resize(nbytes + 3 * alignment); |
| 51 | return GetAlignedMemory(buf->data(), alignment, 0); |
| 52 | } |
| 53 | |
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 54 | wchar_t* GetAlignedPtr(std::vector<wchar_t>* buf, size_t alignment, size_t nchars) { |
| 55 | buf->resize(nchars + ceil((3 * alignment) / sizeof(wchar_t))); |
| 56 | return reinterpret_cast<wchar_t*>(GetAlignedMemory(reinterpret_cast<char*>(buf->data()), |
| 57 | alignment, 0)); |
| 58 | } |
| 59 | |
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 60 | char* GetAlignedPtrFilled(std::vector<char>* buf, size_t alignment, size_t nbytes, char fill_byte) { |
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 61 | char* buf_aligned = GetAlignedPtr(buf, alignment, nbytes); |
| 62 | memset(buf_aligned, fill_byte, nbytes); |
| 63 | return buf_aligned; |
| 64 | } |
| 65 | |
| Christopher Ferris | 5fc1f6e | 2017-07-12 23:10:07 -0700 | [diff] [blame] | 66 | #if defined(__APPLE__) |
| Christopher Ferris | bad43c7 | 2017-07-12 19:33:32 -0700 | [diff] [blame] | 67 | |
| 68 | // Darwin doesn't support this, so do nothing. |
| 69 | bool LockToCPU(int) { |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | #else |
| 74 | |
| Christopher Ferris | bfb7c76 | 2018-05-04 13:27:47 -0700 | [diff] [blame] | 75 | bool LockToCPU(int cpu_to_lock) { |
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 76 | cpu_set_t cpuset; |
| 77 | |
| 78 | CPU_ZERO(&cpuset); |
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 79 | CPU_SET(cpu_to_lock, &cpuset); |
| 80 | if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) { |
| Christopher Ferris | bfb7c76 | 2018-05-04 13:27:47 -0700 | [diff] [blame] | 81 | if (errno == EINVAL) { |
| 82 | printf("Invalid cpu %d\n", cpu_to_lock); |
| 83 | } else { |
| 84 | perror("sched_setaffinity failed"); |
| 85 | } |
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 86 | return false; |
| 87 | } |
| 88 | |
| 89 | return true; |
| 90 | } |
| Christopher Ferris | bad43c7 | 2017-07-12 19:33:32 -0700 | [diff] [blame] | 91 | |
| 92 | #endif |