ostream& operator<<(ostream&s, timeStamp z) { time_t s1970 = static_cast<time_t>(z.getI(timeUnit::sec(), timeBase::b1970())); int64_t nsI1970 = static_cast<int64_t>(s1970) * I64_C(1000000000); int64_t max_time_t = (I64_C(1)<<((sizeof(time_t)*8)-1))-1; // eg. (2^(32-1)) -1 if(! z.isInitialized()) { s << "[uninitialized]"; } else if(z < timeStamp(0, timeUnit::year(), timeBase::b1970()) || z > timeStamp(max_time_t, timeUnit::sec(), timeBase::b1970())) { timeLength tl(z - timeStamp(0, timeUnit::year(), timeBase::b1970())); // setting the oct flag is just a hack to tell the timeLength ostream<< // to print the time compressed #if !defined(mips_sgi_irix6_4) && !defined(i386_unknown_nt4_0) &&!defined(mips_unknown_ce2_11) //ccw 10 apr 2001 ostream::fmtflags oldflags; #else long oldflags; // irix,nt environment doesn't define ostream::fmtflags #endif oldflags = s.flags(ostream::oct); s << "[1970 + " << tl << "]"; s.flags(oldflags); } else { int64_t nstot = z.getI(timeUnit::ns(), timeBase::b1970()); int64_t nsrem = nstot - nsI1970; char dateStr[50]; s1970 = mktime(localtime(&s1970)); strcpy(dateStr, ctime(&s1970)); char *p = strstr(dateStr, "\n"); // erase the nl if(p != NULL) { *p++ = 0; *p = 0; } char strFmt[30]; insCommas(static_cast<int>(nsrem), strFmt); s << "[" << dateStr << " " << strFmt << "ns]"; } return s; }
static int S_can_create_big_files(void) { int result = 0; FILE *fh = fopen64("_charm_large_file_test", "w+"); if (!fh) { return false; } else { /* Bail unless seek succeeds. */ int64_t check_seek = fseeko64(fh, I64_C(5000000000), SEEK_SET); if (check_seek != -1) { /* Bail unless we write successfully. */ if (fprintf(fh, "X") == 1) { result = true; } } if (fclose(fh)) { result = false; } } /* Truncate, just in case the call to remove fails. */ fh = fopen64("_charm_large_file_test", "w"); if (fh != NULL) { fclose(fh); } remove("_charm_large_file_test"); return result; }
static void vcatf_i64(VArray *tests) { CharBuf *wanted = S_get_cb("foo bar -5000000000 baz"); i64_t num = I64_C(-5000000000); CharBuf *got = S_get_cb("foo "); CB_catf(got, "bar %i64 baz", num); VA_Push(tests, (Obj*)TestCB_new(wanted, got)); }
// getFrSpec is a helper routine for the gt, lt operators // n : numerator, d: denominator // ra: returns the numerator high 32 bits / denom (always positive) // rb: returns the numerator low 32 bits / denom (always positive) // rsign: -1 for negative, 1 for positive result void getFrSpec(int64_t n, int64_t d, double *ra, double *rb, int *rsign) { int sign = 1; if(n < 0) { n = -n; sign = -sign; } if(d < 0) { d = -d; sign = -sign; } *rsign = sign; int64_t upperI = n & I64_C(0xFFFFFFFF00000000); *ra = static_cast<double>(upperI) / static_cast<double>(d); int64_t lowerI = n & 0xFFFFFFFF; *rb = static_cast<double>(lowerI) / static_cast<double>(d); }
static void test_vcatf_i64(TestBatch *batch) { CharBuf *wanted = S_get_cb("foo bar -5000000000 baz"); int64_t num = I64_C(-5000000000); CharBuf *got = S_get_cb("foo "); CB_catf(got, "bar %i64 baz", num); TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%i64"); DECREF(wanted); DECREF(got); }
static INLINE bool_t SI_init_read_only(FSFileHandle *self) { // Open. self->fd = open((char*)CB_Get_Ptr8(self->path), SI_posix_flags(self->flags), 0666); if (self->fd == -1) { self->fd = 0; Err_set_error(Err_new(CB_newf("Can't open '%o': %s", self->path, strerror(errno)))); return false; } // Derive len. self->len = lseek64(self->fd, I64_C(0), SEEK_END); if (self->len == -1) { Err_set_error(Err_new(CB_newf("lseek64 on %o failed: %s", self->path, strerror(errno)))); return false; } else { int64_t check_val = lseek64(self->fd, I64_C(0), SEEK_SET); if (check_val == -1) { Err_set_error(Err_new(CB_newf("lseek64 on %o failed: %s", self->path, strerror(errno)))); return false; } } // Get system page size. #if defined(_SC_PAGESIZE) self->page_size = sysconf(_SC_PAGESIZE); #elif defined(_SC_PAGE_SIZE) self->page_size = sysconf(_SC_PAGE_SIZE); #else #error "Can't determine system memory page size" #endif return true; }
static void test_seg_name_and_num(TestBatch *batch) { Segment *segment_z = Seg_new(35); CharBuf *seg_z_name = Seg_num_to_name(35); TEST_TRUE(batch, Seg_Get_Number(segment_z) == I64_C(35), "Get_Number"); TEST_TRUE(batch, CB_Equals_Str(Seg_Get_Name(segment_z), "seg_z", 5), "Get_Name"); TEST_TRUE(batch, CB_Equals_Str(seg_z_name, "seg_z", 5), "num_to_name"); DECREF(seg_z_name); DECREF(segment_z); }
FSFileHandle* FSFH_do_open(FSFileHandle *self, const CharBuf *path, uint32_t flags) { FH_do_open((FileHandle*)self, path, flags); if (!path || !CB_Get_Size(path)) { Err_set_error(Err_new(CB_newf("Missing required param 'path'"))); CFISH_DECREF(self); return NULL; } // Attempt to open file. if (flags & FH_WRITE_ONLY) { self->fd = open((char*)CB_Get_Ptr8(path), SI_posix_flags(flags), 0666); if (self->fd == -1) { self->fd = 0; Err_set_error(Err_new(CB_newf("Attempt to open '%o' failed: %s", path, strerror(errno)))); CFISH_DECREF(self); return NULL; } if (flags & FH_EXCLUSIVE) { self->len = 0; } else { // Derive length. self->len = lseek64(self->fd, I64_C(0), SEEK_END); if (self->len == -1) { Err_set_error(Err_new(CB_newf("lseek64 on %o failed: %s", self->path, strerror(errno)))); CFISH_DECREF(self); return NULL; } else { int64_t check_val = lseek64(self->fd, I64_C(0), SEEK_SET); if (check_val == -1) { Err_set_error(Err_new(CB_newf("lseek64 on %o failed: %s", self->path, strerror(errno)))); CFISH_DECREF(self); return NULL; } } } } else if (flags & FH_READ_ONLY) { if (SI_init_read_only(self)) { // On 64-bit systems, map the whole file up-front. if (IS_64_BIT && self->len) { self->buf = (char*)SI_map(self, 0, self->len); if (!self->buf) { // An error occurred during SI_map, which has set // Err_error for us already. CFISH_DECREF(self); return NULL; } } } else { CFISH_DECREF(self); return NULL; } } else { Err_set_error(Err_new(CB_newf("Must specify FH_READ_ONLY or FH_WRITE_ONLY to open '%o'", path))); CFISH_DECREF(self); return NULL; } return self; }
rawTime64 pd_thread::getRawCpuTime_sw() { // returns user+sys time from the user area of the inferior process. // Since AIX 4.1 doesn't have a /proc file system, this is slightly // more complicated than solaris or the others. // It must not stop the inferior process or assume it is stopped. // It must be "in sync" with rtinst's DYNINSTgetCPUtime() // Idea number one: use getprocs() (which needs to be included anyway // because of a use above) to grab the process table info. // We probably want pi_ru.ru_utime and pi_ru.ru_stime. // int lwp_id: thread ID of desired time. Ignored for now. // int pid: process ID that we want the time for. // int getprocs (struct procsinfo *ProcessBuffer, // Array of procsinfos // int ProcessSize, // sizeof(procsinfo) // struct fdsinfo *FileBuffer, // Array of fdsinfos // int FileSize, // sizeof(...) // pid_t *IndexPointer, // Next PID after call // int Count); // How many to retrieve // Constant for the number of processes wanted in info const unsigned int numProcsWanted = 1; struct procsinfo procInfoBuf[numProcsWanted]; struct fdsinfo fdsInfoBuf[numProcsWanted]; int numProcsReturned; // The pid sent to getProcs() is modified, so make a copy pid_t wantedPid = pd_proc->getPid(); // We really don't need to recalculate the size of the structures // every call through here. The compiler should optimize these // to constants. const int sizeProcInfo = sizeof(struct procsinfo); const int sizeFdsInfo = sizeof(struct fdsinfo); numProcsReturned = getprocs(procInfoBuf, sizeProcInfo, fdsInfoBuf, sizeFdsInfo, &wantedPid, numProcsWanted); if (numProcsReturned == -1) // We have an error perror("Failure in getInferiorCPUtime"); // Now we have the process table information. Since there is no description // other than the header file, I've included descriptions of used fields. /* struct procsinfo { // valid when the process is a zombie only unsigned long pi_utime; / this process user time unsigned long pi_stime; / this process system time // accounting and profiling data unsigned long pi_start; // time at which process began struct rusage pi_ru; // this process' rusage info struct rusage pi_cru; // children's rusage info }; */ // Other things are included, but we don't need 'em here. // In addition, the fdsinfo returned is ignored, since we don't need // open file descriptor data. // This isn't great, since the returned time is in seconds run. It works // (horribly) for now, though. Multiply it by a million and we'll call // it a day. Back to the drawing board. // Get the time (user+system?) in seconds rawTime64 result = (rawTime64) procInfoBuf[0].pi_ru.ru_utime.tv_sec + // User time (rawTime64) procInfoBuf[0].pi_ru.ru_stime.tv_sec; // System time result *= I64_C(1000000); // It looks like the tv_usec fields are actually nanoseconds in this // case. If so, it's undocumented -- but I'm getting numbers like // "980000000" which is either 980 _million_ microseconds (i.e. 980sec) // or .98 seconds if the units are nanoseconds. // IF STRANGE RESULTS HAPPEN IN THE TIMERS, make sure that usec is // actually nanos, not micros. rawTime64 nanoseconds = (rawTime64) procInfoBuf[0].pi_ru.ru_utime.tv_usec + // User time (rawTime64) procInfoBuf[0].pi_ru.ru_stime.tv_usec; //System time result += (nanoseconds / 1000); if (result < sw_previous_) // Time ran backwards? { // When the process exits we often get a final time call. // If the result is 0(.0), don't print an error. if (result) { char errLine[150]; sprintf(errLine,"process::getRawCpuTime_sw - time going backwards in " "daemon - cur: %lld, prev: %lld\n", result, sw_previous_); cerr << errLine; pdlogLine(errLine); } result = sw_previous_; } else sw_previous_=result; return result; }
int main() { /* define some variables initialized by some "portable" constants */ int8 i8 = I8_C(0x01); int16 i16 = I16_C(0x0123); int32 i32 = I32_C(0x01234567); int64 i64 = I64_C(0x0123456789abcdef), j64 = I64_C(0); uint8 *p; int i; float32 f32 = F32_C(0.3232), tmp_f32; float64 f64 = F64_C(0.646464646464), tmp_f64; unsigned char nf32[] = { 0x3e, 0xa5, 0x7a, 0x78 }; unsigned char nf64[] = { 0x3f, 0xe4, 0xaf, 0xd6, 0xa0, 0x52, 0xa8, 0x9c }; /* Portable printf (fprintf, ...) calls */ printf("i8=" X8_L ", i16=" X16_L ", i32=" X32_L ", i64 = " X64_L "\n", i8, i16, i32, i64); /* Convert a number to network format */ j64 = hton_int64(i64); /* perform some simple sanity checks */ p = (uint8*) &j64; for (i = 0 ; i < 64/8 ; i += 2) { if (p[i/2] != ((i<<4)|(i+1))) { printf("Error: conversion to network format contains an error (byte %d: %x != %x)!\n", i/2, (int) p[i/2], ((i<<4)|(i+1))); return -1; } } /* convert a number in network format to host format */ j64 = hton_int64(j64); if (j64 != i64) { printf("Error: conversion from network format to host format contains an error!\n"); return -1; } printf("f = " G32_L ", f64 = " G64_L "\n", f32, f64); printf("f = " F32_L ", f64 = " F64_L "\n", f32, f64); printf("f = " E32_L ", f64 = " E64_L "\n", f32, f64); tmp_f32 = hton_float32(f32); if (tmp_f32 != *(float32*) nf32) { printf("Error: float32 conversation to network format failed\n!"); return -1; } tmp_f32 = ntoh_float32(tmp_f32); if (tmp_f32 != f32) { printf("Error: float32 conversion failed (" G32_L "!=" G32_L ")\n", tmp_f32, f32); dump((unsigned char*) &tmp_f32, sizeof(tmp_f32)); dump((unsigned char*) &f32, sizeof(f32)); return -1; } tmp_f64 = hton_float64(f64); if (tmp_f64 != *(float64*) nf64) { printf("Error: float64 conversation to network format failed\n!"); dump((unsigned char*) &tmp_f64, sizeof(tmp_f64)); return -1; } tmp_f64 = ntoh_float64(tmp_f64); if (tmp_f64 != f64) { printf("Error: float64 conversion failed!\n"); dump((unsigned char*) &tmp_f64, sizeof(tmp_f64)); dump((unsigned char*) &f64, sizeof(f64)); return -1; } printf("autoconfig.h passed simple consistency tests\n"); return 0; }
const timeUnit *timeUnit::dayHelp() { int64_t ns_per_hour = hour().get_ns_per_unit().getNumer(); return new timeUnit(fraction(I64_C(24) * ns_per_hour)); }
const timeUnit *timeUnit::hourHelp() { int64_t ns_per_min = minute().get_ns_per_unit().getNumer(); return new timeUnit(fraction(I64_C(60) * ns_per_min)); }
const timeUnit *timeUnit::minHelp() { int64_t ns_per_sec = sec().get_ns_per_unit().getNumer(); return new timeUnit(fraction(I64_C(60) * ns_per_sec)); }
int num = (int) (dt / (double)I64_MAX); double rolloverAmt = dt - ((double) num * (double)I64_MAX); return (int64_t) rolloverAmt; } const timeStamp *timeStamp::_ts1800 = NULL; const timeStamp *timeStamp::_ts1970 = NULL; const timeStamp *timeStamp::_tsStd = NULL; const timeStamp *timeStamp::_ts2200 = NULL; // This 88 quintillion number is some 280 years. For the timeLength class, // this amount of time should be safely out of reach. The timeStamp class is // internally based at the year of timeBase::StdBaseMark, ie. year 2000, the // uninitialized value represents a timeStamp around year 2280, plenty safe // out of range for a long time. We want the same number, eg. 8, for every // digit of this number so it can be easily recognized from a debugger. const int64_t timeParent::uninitializedValue = I64_C(8888888888888888888); const timeStamp *timeStamp::ts1800Help() { timeStamp *p_ts1970 = new timeStamp(0, timeUnit::year(), timeBase::b1970()); *p_ts1970 -= 53*timeLength::year() + 17*timeLength::leapYear(); //1900 - 1970 // beginning 2000 (leap year) - beg 2100 (no leap year), 25 4 year spans *p_ts1970 -= 76*timeLength::year() + 24*timeLength::leapYear();// 1800 - 1900 return p_ts1970; } const timeStamp *timeStamp::ts1970Help() { return new timeStamp(0, timeUnit::sec(), timeBase::b1970()); } const timeStamp *timeStamp::tsStdHelp() { return new timeStamp(0, timeUnit::year(), timeBase::bStd());