float interval_measure(interval_t *interval) { clock_gettime(CLOCK_MONOTONIC, &interval->curr); float dt = (float)ts_diff(&interval->curr, &interval->prev) / 1000000000.0; interval->prev = interval->curr; return dt; }
// report our own pass void stats_self_pass( int64_t tval, void *arg ) { char *prfx = ctl->stats->self->prefix; struct timespec now; time_t ts; IOBUF *b; llts( tval, now ); ts = now.tv_sec; b = mem_new_buf( IO_BUF_SZ ); // TODO - more stats // stats types b = stats_report_types( ctl->stats->stats, ts, b ); b = stats_report_types( ctl->stats->adder, ts, b ); b = stats_report_types( ctl->stats->gauge, ts, b ); // memory b = stats_report_mtype( "hosts", ctl->mem->hosts, ts, b ); b = stats_report_mtype( "points", ctl->mem->points, ts, b ); b = stats_report_mtype( "dhash", ctl->mem->dhash, ts, b ); b = stats_report_mtype( "bufs", ctl->mem->iobufs, ts, b ); b = stats_report_mtype( "iolist", ctl->mem->iolist, ts, b ); bprintf( "mem.total.kb %d", ctl->mem->curr_kb ); bprintf( "uptime %.3f", ts_diff( now, ctl->init_time, NULL ) ); io_buf_send( b ); }
unsigned long int run_stats::get_duration_usec(void) { if (!m_start_time.tv_sec) return 0; if (m_end_time.tv_sec > 0) { return ts_diff(m_start_time, m_end_time); } else { return ts_diff_now(m_start_time); } }
int main(int argc, char *argv[]) { unsigned int dim_x = 80, dim_y = 40, time_steps = 80, num_threads = 3; if (argc > 1) time_steps = strtoul(argv[1], NULL, 0); if (argc > 2) dim_x = strtoul(argv[2], NULL, 0); if (argc > 3) dim_y = strtoul(argv[3], NULL, 0); if (argc > 4) num_threads = strtoul(argv[4], NULL, 0); if(dim_x < 9 || dim_y < 9) { printf("Invalid dim_x / dim_y!\n"); exit(EXIT_FAILURE); } size_t size = sizeof(unsigned char) * dim_x * dim_y; unsigned char *grid = malloc(size); if(grid == NULL) exit(EXIT_FAILURE); memset(grid, 0, size); r_pentomino(grid, dim_x, dim_y, dim_x/2, dim_y/2); printf("\nGame of Life: time_steps = %u; dim_x = %u; dim_y = %u; threads = %u \n\n", time_steps, dim_x, dim_y, num_threads); print_gol(grid, dim_x, dim_y); printf("\n\n"); struct timespec begin, end; clock_gettime(CLOCK_REALTIME, &begin); unsigned int living_cells = gol(grid, dim_x, dim_y, time_steps, num_threads); clock_gettime(CLOCK_REALTIME, &end); print_gol(grid, dim_x, dim_y); printf("Living Cells after %u time steps: %u\n", time_steps, living_cells); printf("\nProcessing Time: %.3lf seconds\n", ts_to_double(ts_diff(begin, end))); free(grid); return 0; }
int main(int argc, char** argv) { int N = 100; int num_threads = 4; int input; while ((input = getopt(argc, argv, "t:n:")) != -1) { switch (input) { case 't': if (sscanf(optarg, "%d", &num_threads) != 1) goto error; break; case 'n': if (sscanf(optarg, "%d", &N) != 1) goto error; break; case '?': error: printf( "Usage:\n" "-t \t number of threads used in computation\n" "-n \t number of iterations\n" "\n" "Example:\n" "%s -t 4 -n 2500\n", argv[0]); exit(EXIT_FAILURE); break; } } unsigned long **a = get_int64_twodim_array(N + 1); unsigned long **b = get_int64_twodim_array(N + 1); unsigned long **c = get_int64_twodim_array(N + 1); unsigned long **d = get_int64_twodim_array(N + 1); struct timespec begin, end; clock_gettime(CLOCK_REALTIME, &begin); compute(a, b, c, d, N, num_threads); clock_gettime(CLOCK_REALTIME, &end); printf("\nTime: %.3lf seconds\n", ts_to_double(ts_diff(begin, end))); free(a); free(b); free(c); free(d); return 0; }
void run_stats::summarize(totals& result) const { // aggregate all one_second_stats one_second_stats totals(0); for (std::vector<one_second_stats>::const_iterator i = m_stats.begin(); i != m_stats.end(); i++) { totals.merge(*i); } unsigned long int test_duration_usec = ts_diff(m_start_time, m_end_time); result.m_ops_set = totals.m_ops_set; result.m_ops_get = totals.m_ops_get; result.m_ops_wait = totals.m_ops_wait; result.m_ops = totals.m_ops_get + totals.m_ops_set + totals.m_ops_wait; result.m_bytes = totals.m_bytes_get + totals.m_bytes_set; result.m_ops_sec_set = (double) totals.m_ops_set / test_duration_usec * 1000000; if (totals.m_ops_set > 0) { result.m_latency_set = (double) (totals.m_total_set_latency / totals.m_ops_set) / 1000; } else { result.m_latency_set = 0; } result.m_bytes_sec_set = (totals.m_bytes_set / 1024.0) / test_duration_usec * 1000000; result.m_ops_sec_get = (double) totals.m_ops_get / test_duration_usec * 1000000; if (totals.m_ops_get > 0) { result.m_latency_get = (double) (totals.m_total_get_latency / totals.m_ops_get) / 1000; } else { result.m_latency_get = 0; } result.m_bytes_sec_get = (totals.m_bytes_get / 1024.0) / test_duration_usec * 1000000; result.m_hits_sec = (double) totals.m_get_hits / test_duration_usec * 1000000; result.m_misses_sec = (double) totals.m_get_misses / test_duration_usec * 1000000; result.m_ops_sec_wait = (double) totals.m_ops_wait / test_duration_usec * 1000000; if (totals.m_ops_wait > 0) { result.m_latency_wait = (double) (totals.m_total_wait_latency / totals.m_ops_wait) / 1000; } else { result.m_latency_wait = 0; } result.m_ops_sec = (double) result.m_ops / test_duration_usec * 1000000; if (result.m_ops > 0) { result.m_latency = (double) ((totals.m_total_get_latency + totals.m_total_set_latency + totals.m_total_wait_latency) / result.m_ops) / 1000; } else { result.m_latency = 0; } result.m_bytes_sec = (result.m_bytes / 1024.0) / test_duration_usec * 1000000; }
void run_stats::roll_cur_stats(struct timeval* ts) { struct timeval tv; if (!ts) { gettimeofday(&tv, NULL); ts = &tv; } unsigned int sec = ts_diff(m_start_time, *ts) / 1000000; if (sec > m_cur_stats.m_second) { m_stats.push_back(m_cur_stats); m_cur_stats.reset(sec); } }
void busy_work3(timespec length) { timespec curr_time; timespec twoms = { 0, 1000000 }; clock_gettime(CLOCK_THREAD_CPUTIME_ID, &curr_time); timespec target_time = curr_time + length; while(curr_time+twoms < target_time) { workload_for_1micros(1000); clock_gettime(CLOCK_THREAD_CPUTIME_ID, &curr_time); } timespec timelength; ts_diff(curr_time, target_time, timelength); long long iterations = timelength.tv_sec*1000000+timelength.tv_nsec/((long) 1000); long long i; for ( i = 0; i < iterations; ++i ) workload_for_1micros(1); }
void timer_stop(const struct timespec * const t0, const int64_t nsec, const char *warning) { struct timespec t1; int64_t diff; clock_gettime(CLOCK_MONOTONIC, &t1); if (before(&t1, t0)) { warn_backwards("monotonic timer", t0, &t1); return; } diff = ts_diff(&t1, t0); if (diff > nsec) { warnx("%s (took %lld nsec, over threshold of %lld nsec)", warning, (lld)diff, (lld)nsec); } }
//This function will take a timespec value and nanosleep until then void sleep_until_ts (timespec& end_time){ //Get current time timespec curr_time; get_time(&curr_time); //If we have already passed end_time, then return immediately if( curr_time > end_time ) return; //Otherwise, nanosleep timespec wait; ts_diff(curr_time, end_time, wait); while( nanosleep(&wait,&wait) != 0 ){ if ((wait.tv_sec == 0) && (wait.tv_nsec < 0)) break; if ((wait.tv_sec == 0) && (wait.tv_nsec == 0)) break; } return; }
int main(int argc, char **argv) { #define MAX_BITS (49) // 49-bit word. #define MAX_DATA (1*1024) // 1 KB data buffer of 49-bit data words - ~70 seconds @ 1 kHz. char data[MAX_DATA*MAX_BITS], word[MAX_BITS]; //int (*data_ptr)[MAX_DATA][MAX_BITS] = &data, (*bit_ptr)[MAX_BITS] = data; int i = 0, j = 0, flag = 0, bit_cnt = 0, data_cnt = 0, zones, cmd; int data0,data1,data2,data3,data4,data5,data6; char year3[2],year4[2],month[2],day[2],hour[2],minute[2]; FILE *out_file; struct sched_param param; struct timespec t, tmark; char msg[100] = "", oldMsg[100] = ""; struct fifo dataFifo; /* Declare ourself as a real time task */ param.sched_priority = MY_PRIORITY; if(sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) { perror("sched_setscheduler failed"); exit(-1); } /* Lock memory */ if(mlockall(MCL_CURRENT|MCL_FUTURE) == -1) { perror("mlockall failed"); exit(-2); } /* Pre-fault our stack */ stack_prefault(); for(i = 0; i < MAX_DATA*MAX_BITS; i++) data[i] = '0'; for(i = 0; i < MAX_BITS; i++) word[i] = '0'; // Set up gpio pointer for direct register access setup_io(); // Set up FIFO fifo_init(&dataFifo, data, MAX_DATA*MAX_BITS); // Set pin direction INP_GPIO(PI_DATA_OUT); // must use INP_GPIO before we can use OUT_GPIO OUT_GPIO(PI_DATA_OUT); INP_GPIO(PI_DATA_IN); INP_GPIO(PI_CLOCK_IN); // Set PI_DATA_OUT pin low. GPIO_CLR = 1<<PI_DATA_OUT; clock_gettime(CLOCK_MONOTONIC, &t); tmark = t; while (1) { //printf("pi_data:%i,data[%i][%i]:%i\n",GET_GPIO(PI_DATA_IN),data_cnt,bit_cnt,data[data_cnt][bit_cnt]); t.tv_nsec += INTERVAL; tnorm(&t); clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL); if (GET_GPIO(PI_CLOCK_IN) == PI_CLOCK_HI) flag = 1; else if ((GET_GPIO(PI_CLOCK_IN) == PI_CLOCK_LO) && (flag == 1)) { if (ts_diff(&t, &tmark) > 1100000) { // new word bit_cnt = 0; // start new word fifo_write(&dataFifo, word, MAX_BITS); // write current word to FIFO /*printf("%04i: wrote ", data_cnt); for(i=0; i<MAX_BITS; i++) printf("%c", word[i]); printf(" to dataFifo\n");*/ data_cnt++; } tmark = t; flag = 0; t.tv_nsec += INTERVAL; tnorm(&t); t.tv_nsec += INTERVAL; tnorm(&t); t.tv_nsec += INTERVAL; tnorm(&t); t.tv_nsec += INTERVAL; tnorm(&t); t.tv_nsec += INTERVAL; tnorm(&t); /*t.tv_nsec += INTERVAL; tnorm(&t); t.tv_nsec += INTERVAL; tnorm(&t); t.tv_nsec += INTERVAL; tnorm(&t); t.tv_nsec += INTERVAL; tnorm(&t); t.tv_nsec += INTERVAL; tnorm(&t);*/ clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL); // wait 50 uS for valid data word[bit_cnt++] = (GET_GPIO(PI_DATA_IN) == PI_DATA_HI) ? '0' : '1'; } if (data_cnt == MAX_DATA) break; } // decode and write to file if ( (out_file = fopen ("data", "w")) == NULL ) printf ("*** data could not be opened. \n" ); else for ( i = 0; i < MAX_DATA; i++ ) { strcpy(msg, ""); fifo_read(&dataFifo, word, MAX_BITS); /*printf("%04i: read ", i); for(j=0; j<MAX_BITS; j++) printf("%c", word[j]); printf(" from dataFifo\n");*/ cmd = getBinaryData(word,0,8); if (cmd == 0x05) { strcpy(msg, "LED Status: "); if (getBinaryData(word,12,1)) strcat(msg, "Error "); if (getBinaryData(word,13,1)) strcat(msg, "Bypass "); if (getBinaryData(word,14,1)) strcat(msg, "Memory "); if (getBinaryData(word,15,1)) strcat(msg, "Armed "); if (getBinaryData(word,16,1)) strcat(msg, "Ready "); } else if (cmd == 0xa5) { sprintf(year3, "%d", getBinaryData(word,9,4)); sprintf(year4, "%d", getBinaryData(word,13,4)); sprintf(month, "%d", getBinaryData(word,19,4)); sprintf(day, "%d", getBinaryData(word,23,5)); sprintf(hour, "%d", getBinaryData(word,28,5)); sprintf(minute, "%d", getBinaryData(word,33,6)); strcpy(msg, "Date: 20"); strcat(msg, year3); strcat(msg, year4); strcat(msg, "-"); strcat(msg, month); strcat(msg, "-"); strcat(msg, day); strcat(msg, " "); strcat(msg, hour); strcat(msg, ":"); strcat(msg, minute); } else if (cmd == 0x27) { strcpy(msg, "Zone1: "); zones = getBinaryData(word,41,8); if (zones & 1) strcat(msg, "1 "); if (zones & 2) strcat(msg, "2 "); if (zones & 4) strcat(msg, "3 "); if (zones & 8) strcat(msg, "4 "); if (zones & 16) strcat(msg, "5 "); if (zones & 32) strcat(msg, "6 "); if (zones & 64) strcat(msg, "7 "); if (zones & 128) strcat(msg, "8 "); } else if (cmd == 0x2d) { strcpy(msg, "Zone2: "); zones = getBinaryData(word,41,8); if (zones & 1) strcat(msg, "9 "); if (zones & 2) strcat(msg, "10 "); if (zones & 4) strcat(msg, "11 "); if (zones & 8) strcat(msg, "12 "); if (zones & 16) strcat(msg, "13 "); if (zones & 32) strcat(msg, "14 "); if (zones & 64) strcat(msg, "15 "); if (zones & 128) strcat(msg, "16 "); } else if (cmd == 0x34) { strcpy(msg, "Zone3: "); zones = getBinaryData(word,41,8); if (zones & 1) strcat(msg, "9 "); if (zones & 2) strcat(msg, "10 "); if (zones & 4) strcat(msg, "11 "); if (zones & 8) strcat(msg, "12 "); if (zones & 16) strcat(msg, "13 "); if (zones & 32) strcat(msg, "14 "); if (zones & 64) strcat(msg, "15 "); if (zones & 128) strcat(msg, "16 "); } else if (cmd == 0x3e) { strcpy(msg, "Zone4: "); zones = getBinaryData(word,41,8); if (zones & 1) strcat(msg, "9 "); if (zones & 2) strcat(msg, "10 "); if (zones & 4) strcat(msg, "11 "); if (zones & 8) strcat(msg, "12 "); if (zones & 16) strcat(msg, "13 "); if (zones & 32) strcat(msg, "14 "); if (zones & 64) strcat(msg, "15 "); if (zones & 128) strcat(msg, "16 "); } else strcpy(msg, "Unknown command."); data0 = getBinaryData(word,0,8); data1 = getBinaryData(word,8,8); data2 = getBinaryData(word,16,8); data3 = getBinaryData(word,24,8); data4 = getBinaryData(word,32,8); data5 = getBinaryData(word,40,8); data6 = getBinaryData(word,48,2); if (strcmp(msg, oldMsg) != 0) { fprintf (out_file, "data[%i],cmd:0x%02x,%s\n", i, cmd, msg); fprintf (out_file, "***data-all: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",data0,data1,data2,data3,data4,data5,data6); strcpy(oldMsg, msg); } } fclose (out_file); /* Unlock memory */ if(munlockall() == -1) { perror("munlockall failed"); exit(-2); } return 0; } // main