chiark / gitweb /
2a588628912959b5950c026a8dd3f61199110c49
[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 <string>
26
27 #ifdef HAVE_CONFIG_H  // Built using autoconf
28 #include "stressapptest_config.h"
29 using namespace std;
30 using namespace __gnu_cxx;
31
32 typedef signed long long   int64;
33 typedef signed int         int32;
34 typedef signed short int   int16;
35 typedef signed char        int8;
36
37 typedef unsigned long long uint64;
38 typedef unsigned int       uint32;
39 typedef unsigned short     uint16;
40 typedef unsigned char      uint8;
41
42 #define DISALLOW_COPY_AND_ASSIGN(TypeName)        \
43   TypeName(const TypeName&);                      \
44   void operator=(const TypeName&)
45
46 inline const char* Timestamp() {
47   return STRESSAPPTEST_TIMESTAMP;
48 }
49
50 inline const char* BuildChangelist() {
51   return "open source release";
52 }
53
54 #else
55   #include "googlesattypes.h"
56 #endif
57 // Workaround to allow 32/64 bit conversion
58 // without running into strict aliasing problems.
59 union datacast_t {
60   uint64 l64;
61   struct {
62     uint32 l;
63     uint32 h;
64   } l32;
65 };
66
67
68 // File sync'd print to console and log
69 void logprintf(int priority, const char *format, ...);
70
71 // We print to stderr ourselves first in case we're in such a bad state that the
72 // logger can't work.
73 #define sat_assert(x) \
74 {\
75   if (!(x)) {\
76     fprintf(stderr, "Assertion failed at %s:%d\n", __FILE__, __LINE__);\
77     logprintf(0, "Assertion failed at %s:%d\n", __FILE__, __LINE__);\
78     exit(1);\
79   }\
80 }
81
82 #if !defined(CPU_SETSIZE)
83   // Define type and macros for cpu mask operations
84   // Note: this code is hacked together to deal with difference
85   // function signatures across versions of glibc, ie those that take
86   // cpu_set_t versus those that take unsigned long.  -johnhuang
87   typedef unsigned long cpu_set_t;
88   #define CPU_SETSIZE                   32
89   #define CPU_ISSET(index, cpu_set_ptr) (*(cpu_set_ptr) & 1 << (index))
90   #define CPU_SET(index, cpu_set_ptr)   (*(cpu_set_ptr) |= 1 << (index))
91   #define CPU_ZERO(cpu_set_ptr)         (*(cpu_set_ptr) = 0)
92   #define CPU_CLR(index, cpu_set_ptr)   (*(cpu_set_ptr) &= ~(1 << (index)))
93 #endif
94
95 // Make using CPUSET non-super-painful.
96 static inline uint32 cpuset_to_uint32(cpu_set_t *cpuset) {
97   uint32 value = 0;
98   for (int index = 0; index < CPU_SETSIZE; index++) {
99     if (CPU_ISSET(index, cpuset)) {
100       if (index < 32) {
101           value |= 1 << index;
102       } else {
103         logprintf(0, "Process Error: Cpu index (%d) higher than 32\n", index);
104         sat_assert(0);
105       }
106     }
107   }
108   return value;
109 }
110
111 static inline void cpuset_from_uint32(uint32 mask, cpu_set_t *cpuset) {
112   CPU_ZERO(cpuset);
113   for (int index = 0; index < 32; index++) {
114     if (mask & (1 << index))
115       CPU_SET(index, cpuset);
116   }
117 }
118
119 static const int32 kUSleepOneSecond = 1000000;
120
121 // This is guaranteed not to use signals.
122 inline bool sat_usleep(int32 microseconds) {
123   timespec req;
124   req.tv_sec = microseconds / 1000000;
125   // Convert microseconds argument to nano seconds.
126   req.tv_nsec = (microseconds % 1000000) * 1000;
127   return nanosleep(&req, NULL) == 0;
128 }
129
130 // This is guaranteed not to use signals.
131 inline bool sat_sleep(time_t seconds) {
132   timespec req;
133   req.tv_sec = seconds;
134   req.tv_nsec = 0;
135   return nanosleep(&req, NULL) == 0;
136 }
137
138 // Get an error code description for use in error messages.
139 //
140 // Args:
141 //   error_num: an errno error code
142 inline string ErrorString(int error_num) {
143   char buf[256];
144   return string(strerror_r(error_num, buf, sizeof buf));
145 }
146
147 // Define handy constants here
148 static const int kTicksPerSec = 100;
149 static const int kMegabyte = (1024LL*1024LL);
150 static const int kSatDiskPageMax = 32;
151 static const int kSatDiskPage = 8;
152 static const int kSatPageSize = (1024LL*1024LL);
153 static const int kCacheLineSize = 64;
154 static const uint16_t kNetworkPort = 19996;
155
156 #endif  // STRESSAPPTEST_SATTYPES_H_