blob: 0c7254c143d4a729ef216850cdc4b0a8a6884d5c [file] [log] [blame]
Anders Lewisf4447b92017-06-23 15:53:59 -07001/*
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 Lewisac4f4b42017-08-08 18:29:51 -070019#include <math.h>
Anders Lewisf4447b92017-06-23 15:53:59 -070020#include <sched.h>
21#include <stdio.h>
22#include <string.h>
Anders Lewisac4f4b42017-08-08 18:29:51 -070023#include <wchar.h>
Anders Lewisa7b0f882017-07-24 20:01:13 -070024
Anders Lewisf4447b92017-06-23 15:53:59 -070025#include <cstdlib>
Anders Lewisf4447b92017-06-23 15:53:59 -070026
27// This function returns a pointer less than 2 * alignment + or_mask bytes into the array.
Anders Lewisa7b0f882017-07-24 20:01:13 -070028char* GetAlignedMemory(char* orig_ptr, size_t alignment, size_t or_mask) {
Anders Lewisf4447b92017-06-23 15:53:59 -070029 if ((alignment & (alignment - 1)) != 0) {
30 fprintf(stderr, "warning: alignment passed into GetAlignedMemory is not a power of two.\n");
31 std::abort();
32 }
33 if (or_mask > alignment) {
34 fprintf(stderr, "warning: or_mask passed into GetAlignedMemory is too high.\n");
35 std::abort();
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 Lewisa7b0f882017-07-24 20:01:13 -070049char* GetAlignedPtr(std::vector<char>* buf, size_t alignment, size_t nbytes) {
Anders Lewisf4447b92017-06-23 15:53:59 -070050 buf->resize(nbytes + 3 * alignment);
51 return GetAlignedMemory(buf->data(), alignment, 0);
52}
53
Anders Lewisac4f4b42017-08-08 18:29:51 -070054wchar_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 Lewisa7b0f882017-07-24 20:01:13 -070060char* GetAlignedPtrFilled(std::vector<char>* buf, size_t alignment, size_t nbytes, char fill_byte) {
Anders Lewisf4447b92017-06-23 15:53:59 -070061 char* buf_aligned = GetAlignedPtr(buf, alignment, nbytes);
62 memset(buf_aligned, fill_byte, nbytes);
63 return buf_aligned;
64}
65
Christopher Ferris5fc1f6e2017-07-12 23:10:07 -070066#if defined(__APPLE__)
Christopher Ferrisbad43c72017-07-12 19:33:32 -070067
68// Darwin doesn't support this, so do nothing.
69bool LockToCPU(int) {
70 return false;
71}
72
73#else
74
Anders Lewisa7b0f882017-07-24 20:01:13 -070075bool LockToCPU(long cpu_to_lock) {
Anders Lewisf4447b92017-06-23 15:53:59 -070076 cpu_set_t cpuset;
77
78 CPU_ZERO(&cpuset);
79 if (sched_getaffinity(0, sizeof(cpuset), &cpuset) != 0) {
80 perror("sched_getaffinity failed");
81 return false;
82 }
83
84 if (cpu_to_lock < 0) {
85 // Lock to the last active core we find.
86 for (int i = 0; i < CPU_SETSIZE; i++) {
87 if (CPU_ISSET(i, &cpuset)) {
88 cpu_to_lock = i;
89 }
90 }
91 } else if (!CPU_ISSET(cpu_to_lock, &cpuset)) {
Anders Lewisa7b0f882017-07-24 20:01:13 -070092 printf("Cpu %ld does not exist.\n", cpu_to_lock);
Anders Lewisf4447b92017-06-23 15:53:59 -070093 return false;
94 }
95
96 if (cpu_to_lock < 0) {
97 printf("Cannot find any valid cpu to lock.\n");
98 return false;
99 }
100
101 CPU_ZERO(&cpuset);
102 CPU_SET(cpu_to_lock, &cpuset);
103 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
104 perror("sched_setaffinity failed");
105 return false;
106 }
107
108 return true;
109}
Christopher Ferrisbad43c72017-07-12 19:33:32 -0700110
111#endif