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