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