1 // Copyright 2006 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #ifndef STRESSAPPTEST_SATTYPES_H_
16 #define STRESSAPPTEST_SATTYPES_H_
18 #include <arpa/inet.h>
22 #include <sys/types.h>
28 #ifdef HAVE_CONFIG_H // Built using autoconf
30 #include "stressapptest_config_android.h" // NOLINT
32 #include "stressapptest_config.h" // NOLINT
33 using namespace __gnu_cxx; //NOLINT
37 typedef signed long long int64;
38 typedef signed int int32;
39 typedef signed short int int16;
40 typedef signed char int8;
42 typedef unsigned long long uint64;
43 typedef unsigned int uint32;
44 typedef unsigned short uint16;
45 typedef unsigned char uint8;
47 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
48 TypeName(const TypeName&); \
49 void operator=(const TypeName&)
51 inline const char* Timestamp() {
52 return STRESSAPPTEST_TIMESTAMP;
55 inline const char* BuildChangelist() {
56 return "open source release";
59 static const bool kOpenSource = true;
60 #else // !HAVE_CONFIG_H
61 static const bool kOpenSource = false;
62 #include "googlesattypes.h" // NOLINT
63 #endif // HAVE_CONFIG_H
64 // Workaround to allow 32/64 bit conversion
65 // without running into strict aliasing problems.
75 // File sync'd print to console and log
76 void logprintf(int priority, const char *format, ...);
78 // Stop the log and dump any queued lines.
81 // We print to stderr ourselves first in case we're in such a bad state that the
83 #define sat_assert(x) \
87 fprintf(stderr, "Assertion failed at %s:%d\n", __FILE__, __LINE__);\
88 logprintf(0, "Assertion failed at %s:%d\n", __FILE__, __LINE__);\
93 #if !defined(CPU_SETSIZE)
94 // Define type and macros for cpu mask operations
95 // Note: this code is hacked together to deal with difference
96 // function signatures across versions of glibc, ie those that take
97 // cpu_set_t versus those that take unsigned long. -johnhuang
98 typedef uint64 cpu_set_t;
99 #define CPU_SETSIZE (sizeof(cpu_set_t) * 8)
100 #define CPU_ISSET(index, cpu_set_ptr) (*(cpu_set_ptr) & 1ull << (index))
101 #define CPU_SET(index, cpu_set_ptr) (*(cpu_set_ptr) |= 1ull << (index))
102 #define CPU_ZERO(cpu_set_ptr) (*(cpu_set_ptr) = 0)
103 #define CPU_CLR(index, cpu_set_ptr) (*(cpu_set_ptr) &= ~(1ull << (index)))
106 static inline bool cpuset_isequal(const cpu_set_t *c1, const cpu_set_t *c2) {
107 for (int i = 0; i < CPU_SETSIZE; ++i)
108 if ((CPU_ISSET(i, c1) != 0) != (CPU_ISSET(i, c2) != 0))
113 static inline bool cpuset_issubset(const cpu_set_t *c1, const cpu_set_t *c2) {
114 for (int i = 0; i < CPU_SETSIZE; ++i)
115 if (CPU_ISSET(i, c1) && !CPU_ISSET(i, c2))
120 static inline int cpuset_count(const cpu_set_t *cpuset) {
122 for (int i = 0; i < CPU_SETSIZE; ++i)
123 if (CPU_ISSET(i, cpuset))
128 static inline void cpuset_set_ab(cpu_set_t *cpuset, int a, int b) {
130 for (int i = a; i < b; ++i)
134 static inline string cpuset_format(const cpu_set_t *cpuset) {
136 int digit = 0, last_non_zero_size = 1;
137 for (int i = 0; i < CPU_SETSIZE; ++i) {
138 if (CPU_ISSET(i, cpuset)) {
139 digit |= 1 << (i & 3);
142 format += char(digit <= 9 ? '0' + digit: 'A' + digit - 10);
144 last_non_zero_size = format.size();
150 format += char(digit <= 9 ? '0' + digit: 'A' + digit - 10);
151 last_non_zero_size = format.size();
153 format.erase(last_non_zero_size);
154 reverse(format.begin(), format.end());
158 static const int32 kUSleepOneSecond = 1000000;
160 // This is guaranteed not to use signals.
161 inline bool sat_usleep(int32 microseconds) {
163 req.tv_sec = microseconds / 1000000;
164 // Convert microseconds argument to nano seconds.
165 req.tv_nsec = (microseconds % 1000000) * 1000;
166 return nanosleep(&req, NULL) == 0;
169 // This is guaranteed not to use signals.
170 inline bool sat_sleep(time_t seconds) {
172 req.tv_sec = seconds;
174 return nanosleep(&req, NULL) == 0;
177 // Get an error code description for use in error messages.
180 // error_num: an errno error code
181 inline string ErrorString(int error_num) {
183 #ifdef STRERROR_R_CHAR_P
184 return string(strerror_r(error_num, buf, sizeof buf));
186 if (strerror_r(error_num, buf, sizeof buf))
187 return "unknown failure";
193 // Execute the cpuid instruction and pass back the contents of the registers.
194 // This only works on x86 based platforms.
196 unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx) {
200 // CPUID features documented at:
201 // http://www.sandpile.org/ia32/cpuid.htm
202 #if defined(STRESSAPPTEST_CPU_I686) || defined(STRESSAPPTEST_CPU_X86_64)
203 #if defined(__PIC__) && defined(STRESSAPPTEST_CPU_I686)
204 // In PIC compilations using the i686 cpu type, ebx contains the address
205 // of the global offset table. The compiler can't properly handle constraints
206 // using the ebx register for this compile, so preserve the register
213 : "=a" (*eax), "=D" (*ebx), "=c" (*ecx), "=d" (*edx)
221 : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
225 #endif // defined(__PIC__) && defined(STRESSAPPTEST_CPU_I686)
226 #elif defined(STRESSAPPTEST_CPU_PPC)
228 #elif defined(STRESSAPPTEST_CPU_ARMV7A)
230 #elif defined(STRESSAPPTEST_CPU_AARCH64)
233 #warning "Unsupported CPU type."
237 // Define handy constants here
238 static const int kTicksPerSec = 100;
239 static const int kMegabyte = (1024LL*1024LL);
240 static const int kSatDiskPageMax = 32;
241 static const int kSatDiskPage = 8;
242 static const int kSatPageSize = (1024LL*1024LL);
243 static const int kCacheLineSize = 64;
244 static const uint16_t kNetworkPort = 19996;
246 #endif // STRESSAPPTEST_SATTYPES_H_