chiark / gitweb /
Update stressapptest to 1.0.3.
[stressapptest] / src / sattypes.h
1 // Copyright 2006 Google Inc. All Rights Reserved.
2
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
6
7 //      http://www.apache.org/licenses/LICENSE-2.0
8
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.
14
15 #ifndef STRESSAPPTEST_SATTYPES_H_
16 #define STRESSAPPTEST_SATTYPES_H_
17
18 #include <arpa/inet.h>
19 #include <sched.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <time.h>
24 #include <string.h>
25 #include <algorithm>
26 #include <string>
27
28 #ifdef HAVE_CONFIG_H  // Built using autoconf
29 #include "stressapptest_config.h"
30 using namespace std;
31 using namespace __gnu_cxx;
32
33 typedef signed long long   int64;
34 typedef signed int         int32;
35 typedef signed short int   int16;
36 typedef signed char        int8;
37
38 typedef unsigned long long uint64;
39 typedef unsigned int       uint32;
40 typedef unsigned short     uint16;
41 typedef unsigned char      uint8;
42
43 #define DISALLOW_COPY_AND_ASSIGN(TypeName)        \
44   TypeName(const TypeName&);                      \
45   void operator=(const TypeName&)
46
47 inline const char* Timestamp() {
48   return STRESSAPPTEST_TIMESTAMP;
49 }
50
51 inline const char* BuildChangelist() {
52   return "open source release";
53 }
54
55 static const bool kOpenSource = true;
56 #else
57 static const bool kOpenSource = false;
58   #include "googlesattypes.h"
59 #endif
60 // Workaround to allow 32/64 bit conversion
61 // without running into strict aliasing problems.
62 union datacast_t {
63   uint64 l64;
64   struct {
65     uint32 l;
66     uint32 h;
67   } l32;
68 };
69
70
71 // File sync'd print to console and log
72 void logprintf(int priority, const char *format, ...);
73
74 // We print to stderr ourselves first in case we're in such a bad state that the
75 // logger can't work.
76 #define sat_assert(x) \
77 {\
78   if (!(x)) {\
79     fprintf(stderr, "Assertion failed at %s:%d\n", __FILE__, __LINE__);\
80     logprintf(0, "Assertion failed at %s:%d\n", __FILE__, __LINE__);\
81     exit(1);\
82   }\
83 }
84
85 #if !defined(CPU_SETSIZE)
86   // Define type and macros for cpu mask operations
87   // Note: this code is hacked together to deal with difference
88   // function signatures across versions of glibc, ie those that take
89   // cpu_set_t versus those that take unsigned long.  -johnhuang
90   typedef uint64 cpu_set_t;
91   #define CPU_SETSIZE                   (sizeof(cpu_set_t) * 8)
92   #define CPU_ISSET(index, cpu_set_ptr) (*(cpu_set_ptr) & 1ull << (index))
93   #define CPU_SET(index, cpu_set_ptr)   (*(cpu_set_ptr) |= 1ull << (index))
94   #define CPU_ZERO(cpu_set_ptr)         (*(cpu_set_ptr) = 0)
95   #define CPU_CLR(index, cpu_set_ptr)   (*(cpu_set_ptr) &= ~(1ull << (index)))
96 #endif
97
98 static inline bool cpuset_isequal(const cpu_set_t *c1, const cpu_set_t *c2) {
99   for (int i = 0; i < CPU_SETSIZE; ++i)
100     if ((CPU_ISSET(i, c1) != 0) != (CPU_ISSET(i, c2) != 0))
101       return false;
102   return true;
103 }
104
105 static inline bool cpuset_issubset(const cpu_set_t *c1, const cpu_set_t *c2) {
106   for (int i = 0; i < CPU_SETSIZE; ++i)
107     if (CPU_ISSET(i, c1) && !CPU_ISSET(i, c2))
108       return false;
109   return true;
110 }
111
112 static inline int cpuset_count(const cpu_set_t *cpuset) {
113   int count = 0;
114   for (int i = 0; i < CPU_SETSIZE; ++i)
115     if (CPU_ISSET(i, cpuset))
116       ++count;
117   return count;
118 }
119
120 static inline void cpuset_set_ab(cpu_set_t *cpuset, int a, int b) {
121   CPU_ZERO(cpuset);
122   for (int i = a; i < b; ++i)
123     CPU_SET(i, cpuset);
124 }
125
126 static inline string cpuset_format(const cpu_set_t *cpuset) {
127   string format;
128   int digit = 0, last_non_zero_size = 1;
129   for (int i = 0; i < CPU_SETSIZE; ++i) {
130     if (CPU_ISSET(i, cpuset)) {
131       digit |= 1 << (i & 3);
132     }
133     if ((i & 3) == 3) {
134       format += char(digit <= 9 ? '0' + digit: 'A' + digit - 10);
135       if (digit) {
136         last_non_zero_size = format.size();
137         digit = 0;
138       }
139     }
140   }
141   if (digit) {
142     format += char(digit <= 9 ? '0' + digit: 'A' + digit - 10);
143     last_non_zero_size = format.size();
144   }
145   format.erase(last_non_zero_size);
146   reverse(format.begin(), format.end());
147   return format;
148 }
149
150 static const int32 kUSleepOneSecond = 1000000;
151
152 // This is guaranteed not to use signals.
153 inline bool sat_usleep(int32 microseconds) {
154   timespec req;
155   req.tv_sec = microseconds / 1000000;
156   // Convert microseconds argument to nano seconds.
157   req.tv_nsec = (microseconds % 1000000) * 1000;
158   return nanosleep(&req, NULL) == 0;
159 }
160
161 // This is guaranteed not to use signals.
162 inline bool sat_sleep(time_t seconds) {
163   timespec req;
164   req.tv_sec = seconds;
165   req.tv_nsec = 0;
166   return nanosleep(&req, NULL) == 0;
167 }
168
169 // Get an error code description for use in error messages.
170 //
171 // Args:
172 //   error_num: an errno error code
173 inline string ErrorString(int error_num) {
174   char buf[256];
175   return string(strerror_r(error_num, buf, sizeof buf));
176 }
177
178 // Define handy constants here
179 static const int kTicksPerSec = 100;
180 static const int kMegabyte = (1024LL*1024LL);
181 static const int kSatDiskPageMax = 32;
182 static const int kSatDiskPage = 8;
183 static const int kSatPageSize = (1024LL*1024LL);
184 static const int kCacheLineSize = 64;
185 static const uint16_t kNetworkPort = 19996;
186
187 #endif  // STRESSAPPTEST_SATTYPES_H_