static void measure_one_cycle(void) { struct timespec ts1, ts2, delta, sleep; int rc; current_sum_utime = 0; current_sum_stime = 0; current_sum_cpu_utime = 0; current_sum_cpu_stime = 0; if (nr_cycles) output->print_cycle_start(); else output->print_sync(); new_cycle = 1; rc = clock_gettime(CLOCK_MONOTONIC, &ts1); if (rc < 0) DIE_PERROR("clock_gettime failed"); query_tasks(); query_memory(); query_cpus(opt_all_cpus); print_tasks(); print_memory(); print_cpus(opt_all_cpus); rc = clock_gettime(CLOCK_MONOTONIC, &ts2); if (rc < 0) DIE_PERROR("clock_gettime failed"); timespec_delta(&ts1, &ts2, &delta); if (nr_cycles) output->print_cycle_end(&delta); /* check if we can meet the target measurement interval */ if (delta.tv_sec > target.tv_sec || (delta.tv_sec == target.tv_sec && delta.tv_nsec > target.tv_nsec)) { output->exit_output(); DIE("Target measurement intervall too small. Current overhead: %u seconds %lu ms\n", (int) delta.tv_sec, delta.tv_nsec / NSECS_PER_MSEC); } /* now go sleeping for the rest of the measurement interval */ if (nr_cycles) timespec_delta(&delta, &target, &sleep); else timespec_delta(&delta, &ts_sync, &sleep); wait_for_cycle_end(&sleep); nr_cycles++; }
/* * Informs the supervisor that something happened in this porcess's state. */ static int supervisor_send_inform_message(dme_ev_t ev) { sup_message_t msg = {}; char msctext[MAX_MSC_TEXT] = {}; int err = 0; timespec_t tnow; timespec_t tdelta; switch (ev) { case DME_EV_ENTERED_CRITICAL_REG: case DME_EV_EXITED_CRITICAL_REG: clock_gettime(CLOCK_REALTIME, &tnow); tdelta = timespec_delta(sup_tstamp, tnow); /* construct and send the message */ sup_msg_set(&msg, ev, tdelta.tv_sec, tdelta.tv_nsec, 0, msctext, sizeof(msctext)); err = dme_send_msg(SUPERVISOR_PID, (uint8*)&msg, SUPERVISOR_MESSAGE_LENGTH, msctext); /* set new sup_tstamp to tnow */ sup_tstamp.tv_sec = tnow.tv_sec; sup_tstamp.tv_nsec = tnow.tv_nsec; break; default: /* No need to send informs to the supervisor in other cases */ break; } return err; }
void print(enum log_level lvl, const char *fmt, ...) { struct timeval tv; va_list ap; va_start(ap, fmt); /* Timestamp */ gettimeofday(&tv, NULL); fprintf(stderr, "%8.3f ", timespec_delta(&epoch, &tv)); switch (lvl) { case DEBUG: fprintf(stderr, BLD("%-5s "), GRY("Debug")); break; case INFO: fprintf(stderr, BLD("%-5s "), WHT(" Info")); break; case WARN: fprintf(stderr, BLD("%-5s "), YEL(" Warn")); break; case ERROR: fprintf(stderr, BLD("%-5s "), RED("Error")); break; } if (_indent) { for (int i = 0; i < _indent-1; i++) fprintf(stderr, GFX("\x78") " "); fprintf(stderr, GFX("\x74") " "); } vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); va_end(ap); }
void stats_output(struct timespec *time_1, struct timespec *time_2, int count) { double elapsed_time; clock_gettime(CLOCK_REALTIME, time_2); elapsed_time = timespec_delta(*time_1, *time_2); *time_1 = *time_2; fprintf(stderr, "rdips: %f\n", (((double) count) / elapsed_time)); }
// ----------------------------------------------------------------------------- static void *tracking_thread(void *arg) { globals.tracking = 1; tracking_args_t *data = (tracking_args_t *)arg; track_coords_t coords; video_data_t vid_data; unsigned long buff_sz = 0; uint8_t *jpg_buf = NULL, *rgb_buff = NULL; int delta, frames_computed = 0, tracking_fps = 0, streaming_fps = 0; struct timespec t0, t1, tc; clock_gettime(CLOCK_REALTIME, &t0); while (data->running) { // lock the colordetect parameters and determine our tracking fps pthread_mutex_lock(&data->lock); streaming_fps = video_get_fps(); tracking_fps = MIN(data->trackingRate, streaming_fps); pthread_mutex_unlock(&data->lock); // if tracking is disable - sleep and check for a change every second if (tracking_fps <= 0 || globals.tracking == 0) { sleep(1); continue; } // make synchronous frame read -- sleep for a second and retry on fail if (!video_lock(&vid_data, ACCESS_SYNC)) { syslog(LOG_ERR, "colordetect failed to lock frame\n"); sleep(1); continue; } clock_gettime(CLOCK_REALTIME, &tc); delta = timespec_delta(&t0, &tc); if (((frames_computed * 1000000) / delta) >= tracking_fps) { video_unlock(); continue; } // copy the jpeg to our buffer now that we're safely locked if (buff_sz < vid_data.length) { free(jpg_buf); buff_sz = vid_data.length; jpg_buf = (uint8_t *)malloc(buff_sz); } memcpy(jpg_buf, vid_data.data, vid_data.length); video_unlock(); if (0 != jpeg_rd_mem(jpg_buf, buff_sz, &rgb_buff, &coords.width, &coords.height)) { colordetect_hsl_fp32(rgb_buff, &data->color, &coords); frames_computed++; } pthread_mutex_lock(&data->coord_lock); // copy over the updated coordinates to the global struct globals.coords = coords; // inform any listeners that new data is available pthread_cond_broadcast(&globals.coord_cond); pthread_mutex_unlock(&data->coord_lock); if (frames_computed >= streaming_fps) { // every time we hit the streaming fps, dump out the tracking rate clock_gettime(CLOCK_REALTIME, &t1); real_t delta = timespec_delta(&t0, &t1) / (real_t)1000000; syslog(LOG_INFO, "tracking fps: %f\n", streaming_fps / delta); frames_computed = 0; t0 = t1; } } pthread_exit(NULL); }
/* Process incoming messages */ int process_messages(void * cookie) { dbg_msg(""); sup_message_t srcmsg = {}; int err = 0; timespec_t tnow; timespec_t tdelta; timespec_t tprogdelta; if (err = sup_msg_parse(*(buff_t *)cookie, &srcmsg)) { return err; } clock_gettime(CLOCK_REALTIME, &tnow); tprogdelta = timespec_delta(tstamp_supervisor_start, tnow); switch(srcmsg.msg_type) { case DME_EV_ENTERED_CRITICAL_REG: nodes[srcmsg.process_id].state = PS_EXECUTING; dbg_msg("[%ld.%09lu] ENTERED CS: process %llu waited for %u.%09u seconds to enter the CS", tprogdelta.tv_sec, tprogdelta.tv_nsec, srcmsg.process_id, srcmsg.sec_tdelta, srcmsg.nsec_tdelta); /* Get synchronization delay */ tdelta = timespec_delta(tstamp_last_exited, tnow); dbg_msg("SYNCHRONIZATION DELAY is %ld.%09lu", tdelta.tv_sec, tdelta.tv_nsec); synchro_delays[received_resps_count].tv_sec = tdelta.tv_sec; synchro_delays[received_resps_count].tv_nsec = tdelta.tv_nsec; /* Get the response time */ response_times[received_resps_count].tv_sec = srcmsg.sec_tdelta; response_times[received_resps_count].tv_nsec = srcmsg.nsec_tdelta; /* Advance the responses counter */ received_resps_count++; dbg_msg("received_resps_count = %u", received_resps_count); if (!critical_region_is_sane()) { dbg_err("Unfortunately there are multiple processes in the CS at the same time!"); err = ERR_FATAL; } break; case DME_EV_EXITED_CRITICAL_REG: nodes[srcmsg.process_id].state = PS_IDLE; /* mark the time */ tstamp_last_exited.tv_sec = tnow.tv_sec; tstamp_last_exited.tv_nsec = tnow.tv_nsec; dbg_msg("[%ld.%09lu] EXITED CS: process %llu stayed for %u.%09u seconds in it's CS", tprogdelta.tv_sec, tprogdelta.tv_nsec, srcmsg.process_id, srcmsg.sec_tdelta, srcmsg.nsec_tdelta); break; default: /* Other types are invalid */ break; } return err; }