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