/** * @brief Return a monotonic time, in ms. * * @return A relative timestamp that can be used to compute * elapsed times. */ long int time_now_ms() { struct timespec time_now; get_time_now(&time_now); return time_to_ms(time_now); }
int bootstrap(call_t *c) { struct nodedata *nodedata = get_node_private_data(c); struct protocoleData *entitydata = get_entity_private_data(c); call_t c0 = {get_entity_bindings_down(c)->elts[0], c->node, c->entity}; /* get mac header overhead */ nodedata->overhead = GET_HEADER_SIZE(&c0); broadcast_hello(c, NULL); uint64_t at=get_time_now()+time_seconds_to_nanos(2); scheduler_add_callback(at, c, init_lbop, NULL); at=get_time_now()+time_seconds_to_nanos(3); scheduler_add_callback(at, c, broadcast_lmst, NULL); return 0; }
void omni_thread::get_time(unsigned long* abs_sec, unsigned long* abs_nsec, unsigned long rel_sec, unsigned long rel_nsec) { get_time_now(abs_sec, abs_nsec); *abs_nsec += rel_nsec; *abs_sec += rel_sec + *abs_nsec / 1000000000; *abs_nsec = *abs_nsec % 1000000000; }
krb5_error_code krb5_crypto_us_timeofday(krb5_int32 *seconds, krb5_int32 *microseconds) { struct time_now now; krb5_error_code err; now.sec = now.usec = 0; err = get_time_now(&now); if (err) return err; /* It would probably be more efficient to remove this mutex and use thread-local storage for last_time. But that could result in different threads getting the same value for time, which may be a technical violation of spec. */ err = k5_mutex_lock(&krb5int_us_time_mutex); if (err) return err; /* Just guessing: If the number of seconds hasn't changed, yet the microseconds are moving backwards, we probably just got a third instance of returning the same clock value from the system, so the saved value was artificially incremented. On Windows, where we get millisecond accuracy currently, that's quite likely. On UNIX, it appears that we always get new microsecond values, so this case should never trigger. */ /* Check for case where previously usec rollover caused bump in sec, putting now.sec in the past. But don't just use '<' because we need to properly handle the case where the administrator intentionally adjusted time backwards. */ if ((now.sec == last_time.sec-1) || ((now.sec == last_time.sec) && (now.usec <= last_time.usec))) { /* Correct 'now' to be exactly one microsecond later than 'last_time'. Note that _because_ we perform this hack, 'now' may be _earlier_ than 'last_time', even though the system time is monotonically increasing. */ now.sec = last_time.sec; now.usec = ++last_time.usec; if (now.usec >= 1000000) { ++now.sec; now.usec = 0; } } last_time.sec = now.sec; /* Remember for next time */ last_time.usec = now.usec; k5_mutex_unlock(&krb5int_us_time_mutex); *seconds = now.sec; *microseconds = now.usec; return 0; }
int MP4Pusher1::parsed_frame_cb(void *opaque, Frame *f, int is_video) { MP4Pusher1 *obj = (MP4Pusher1 *) opaque; int ret = 0; if (obj->m_prev_ts == -1) obj->m_prev_ts = f->m_ts; if (obj->m_tm_start == UINT64_MAX) obj->m_tm_start = get_time_now(); obj->on_frame(f->m_ts, f->m_dat, f->m_dat_len, is_video); if (f->m_ts > obj->m_prev_ts) { int32_t adjust_tm = get_time_now() - obj->m_tm_start - obj->m_prev_ts; if (f->m_ts - obj->m_prev_ts > adjust_tm) usleep((f->m_ts - obj->m_prev_ts - adjust_tm)*1000); obj->m_prev_ts = f->m_ts; } if (is_video) { if (obj->m_rtmp_hdl->send_video(f->m_ts, f->m_dat, f->m_dat_len) < 0) { LOGE("Send video data to rtmpserver failed"); ret = -1; } } else { if (obj->m_rtmp_hdl->send_audio(f->m_ts, f->m_dat, f->m_dat_len) < 0) { LOGE("Send audio data to rtmpserver failed"); ret = -1; } } return ret; }
//why not image:dump 因为也只有这里用,写到类里没意思。 void graph::dump_resource(const std::string& type) const { std::cout << "\n"__FUNCTION__" " << type << std::endl; unsigned long time = get_time_now(); file_source* source = find_file_source(type.c_str()); if (source) source->dump_resource(); if (type == "all") { dump_resource("image"); dump_resource("texture"); dump_resource("texture_font"); dump_resource("font"); } else if (type == "image") { for (auto it = image_map.begin(); it != image_map.end(); ++it) { const image* img = it->second; std::cout << it->first << " ref: " << img->m_ref << " time: " << TIME(img) << std::endl; } } else if (type == "texture") { for (auto it = texture_map.begin(); it != texture_map.end(); ++it) { const texture* tex = it->second; std::cout << it->first << " time: " << TIME(tex) << std::endl; } } else if (type == "texture_font") { for (auto it = texture_font_map.begin(); it != texture_font_map.end(); ++it) { const texture_font* tf = it->second; std::cout << it->first << " time: " << TIME(tf) << std::endl; for (auto it2 = tf->m_map_char.begin(); it2 != tf->m_map_char.end(); ++it2) { const texture_char* tc = it2->second; std::cout << "\t" << core::UnicodeCharToANSI(it2->first) << " time: " << TIME(tc) << std::endl; } } } else if (type == "font") { dump_font(); } }
int bootstrap(call_t *c) { struct nodedata *nodedata = get_node_private_data(c); struct protocoleData *entitydata = get_entity_private_data(c); call_t c0 = {get_entity_bindings_down(c)->elts[0], c->node, c->entity}; /* get mac header overhead */ nodedata->overhead = GET_HEADER_SIZE(&c0); int i; for(i = 0 ; i < get_node_count() ; i++) { nodedata->lastIDs[i] = -1; nodedata->energiesRem[i] = battery_remaining(c) - 2*getCoutFromDistance(getRange(c), entitydata->alpha, entitydata->c); } broadcast_hello(c, NULL); uint64_t at=get_time_now()+time_seconds_to_nanos(2); scheduler_add_callback(at, c, broadcast_hello2, NULL); return 0; }
void light_cycle(){ if(light_mode == OFF){ digitalWrite(light, LOW); return; } else if(light_mode == ON){ digitalWrite(light, HIGH); return; } // If the function has not returned yet, it means that the lights are in AUTO mode // Check only every minute... if(millis() - auto_time > 60000){ Time now = get_time_now(); bool on = true; if(now.hour <= wake_time.hour){ on = false; if(now.hour == wake_time.hour && now.minutes >= wake_time.minutes){ on = true; } } else if(now.hour >= sleep_time.hour){ on = false; if(now.hour == sleep_time.hour && now.minutes < sleep_time.minutes){ on = true; } } digitalWrite(light, on? HIGH : LOW); auto_time = millis(); } }
int omni_condition::timedwait(unsigned long abs_sec, unsigned long abs_nsec) { _internal_omni_thread_helper me; EnterCriticalSection(&crit); me->cond_next = NULL; me->cond_prev = waiting_tail; if (waiting_head == NULL) waiting_head = me; else waiting_tail->cond_next = me; waiting_tail = me; me->cond_waiting = TRUE; LeaveCriticalSection(&crit); mutex->unlock(); unsigned long now_sec, now_nsec; get_time_now(&now_sec, &now_nsec); DWORD timeout; if ((abs_sec <= now_sec) && ((abs_sec < now_sec) || (abs_nsec < now_nsec))) timeout = 0; else { timeout = (abs_sec-now_sec) * 1000; if( abs_nsec < now_nsec ) timeout -= (now_nsec-abs_nsec) / 1000000; else timeout += (abs_nsec-now_nsec) / 1000000; } DWORD result = WaitForSingleObject(me->cond_semaphore, timeout); if (result == WAIT_TIMEOUT) { EnterCriticalSection(&crit); if (me->cond_waiting) { if (me->cond_prev != NULL) me->cond_prev->cond_next = me->cond_next; else waiting_head = me->cond_next; if (me->cond_next != NULL) me->cond_next->cond_prev = me->cond_prev; else waiting_tail = me->cond_prev; me->cond_waiting = FALSE; LeaveCriticalSection(&crit); mutex->lock(); return 0; } // // We timed out but another thread still signalled us. Wait for // the semaphore (it _must_ have been signalled) to decrement it // again. Return that we were signalled, not that we timed out. // LeaveCriticalSection(&crit); result = WaitForSingleObject(me->cond_semaphore, INFINITE); } if (result != WAIT_OBJECT_0) throw omni_thread_fatal(GetLastError()); mutex->lock(); return 1; }
void graph::auto_clear_resource() { for (auto it = source_map.begin(); it != source_map.end(); ++it) { file_source* p = it->second; p->auto_clear_resource(); } unsigned long time = get_time_now(); for (auto it = image_map.begin(); it != image_map.end(); ) { image* img = it->second; if (img && TIME_NOTUSE(img,m_clear_image) && img->m_ref == 1) { #ifdef _DEBUG_RESOURCE std::cout << "clear image " << it->first << " time: " << TIME(img) << std::endl; #endif delete img; it = image_map.erase(it); } else ++it; //经过测试,压缩没啥用。 // else // { // if (img && TIME_NOTUSE(img,m_compress_image) && !img->is_compress()) // { // img->compress(); //#ifdef _DEBUG_RESOURCE // std::cout << "compress image " << it->first << " time: " << TIME(img) // << " size " << img->get_buf_size()/1024 << "->" << img->get_compress_size()/1024 << std::endl; //#endif // } // ++it; // } } for (auto it = texture_map.begin(); it != texture_map.end();) { const texture* tex = it->second; if (tex && TIME_NOTUSE(tex,m_clear_texture)) { #ifdef _DEBUG_RESOURCE std::cout << "clear texture " << it->first << " time: " << TIME(tex) << std::endl; #endif delete tex; it = texture_map.erase(it); } else ++it; } for (auto it = texture_font_map.begin(); it != texture_font_map.end(); ++it) { texture_font* tf = it->second; for (auto it2 = tf->m_map_char.begin(); it2 != tf->m_map_char.end(); ) { texture_char* tc = it2->second; if (TIME_NOTUSE(tc,m_clear_texturefont)) { #ifdef _DEBUG_RESOURCE std::cout << "clear font " << it->first << " char " << core::UnicodeCharToANSI(it2->first) << " time: " << TIME(tc) << std::endl; #endif tf->m_char_free.push_back(tc); it2 = tf->m_map_char.erase(it2); } else ++it2; } } for (auto it = texturesub_map.begin(); it != texturesub_map.end();) { texture_sub* tex = it->second; if (tex && TIME_NOTUSE(tex,m_clear_texture)) { #ifdef _DEBUG_RESOURCE std::cout << "clear texturesub " << it->first << " time: " << TIME(tex) << std::endl; #endif tex->m_tex->released_sub(tex); it = texturesub_map.erase(it); } else ++it; } for (auto it = texture_muls.begin(); it != texture_muls.end();) { texture_mul* tex = *it; if (tex && TIME_NOTUSE(tex->m_texture,m_clear_texture)) { #ifdef _DEBUG_RESOURCE std::cout << "clear texture_mul " << it->first << " time: " << TIME(tex) << std::endl; #endif delete tex; it = texture_muls.erase(it); } else ++it; } for (auto it = image_font_map.begin(); it != image_font_map.end();) { auto img = it->second; if (img && TIME_NOTUSE(img,m_clear_texture)) { #ifdef _DEBUG_RESOURCE std::cout << "clear image_font_map " << it->first << " time: " << TIME(img) << std::endl; #endif delete img; it = image_font_map.erase(it); } else ++it; } }
int omni_condition::timedwait(unsigned long abs_sec, unsigned long abs_nsec) { _internal_omni_thread_helper me; DosRequestMutexSem( crit , SEM_INDEFINITE_WAIT ); me->cond_next = NULL; me->cond_prev = waiting_tail; if (waiting_head == NULL) waiting_head = me; else waiting_tail->cond_next = me; waiting_tail = me; me->cond_waiting = TRUE; DosReleaseMutexSem( crit ); mutex->unlock(); unsigned long now_sec, now_nsec; get_time_now(&now_sec, &now_nsec); ULONG timeout; if ((abs_sec <= now_sec) && ((abs_sec < now_sec) || (abs_nsec < now_nsec))) timeout = 0; else { timeout = (abs_sec-now_sec) * 1000; if( abs_nsec < now_nsec ) timeout -= (now_nsec-abs_nsec) / 1000000; else timeout += (abs_nsec-now_nsec) / 1000000; } APIRET result = DosWaitEventSem(me->cond_semaphore, timeout); ULONG c; DosResetEventSem(me->cond_semaphore,&c); if ( result == ERROR_TIMEOUT ) { DosRequestMutexSem( crit , SEM_INDEFINITE_WAIT ); if (me->cond_waiting) { if (me->cond_prev != NULL) me->cond_prev->cond_next = me->cond_next; else waiting_head = me->cond_next; if (me->cond_next != NULL) me->cond_next->cond_prev = me->cond_prev; else waiting_tail = me->cond_prev; me->cond_waiting = FALSE; DosReleaseMutexSem( crit ); mutex->lock(); return 0; } // // We timed out but another thread still signalled us. Wait for // the semaphore (it _must_ have been signalled) to decrement it // again. Return that we were signalled, not that we timed out. // DosReleaseMutexSem( crit ); result = DosWaitEventSem(me->cond_semaphore, SEM_INDEFINITE_WAIT); ULONG c; DosResetEventSem(me->cond_semaphore,&c); } if (result != 0) throw omni_thread_fatal(result); mutex->lock(); return 1; }
static void page_flip_event(void *data) { struct modeset_out *out = data; struct timespec now; struct flip_data *priv = out->data; get_time_now(&now); /* initialize values on first flip */ if (priv->num_frames_drawn == 0) { priv->min_flip_time = UINT64_MAX; priv->max_flip_time = 0; priv->draw_start_time = now; priv->flip_time = now; priv->draw_total_time = 0; } /* measure min/max flip time */ if (priv->num_frames_drawn > 0) { uint64_t us; us = get_time_elapsed_us(&priv->flip_time, &now); priv->flip_time = now; if (us < priv->min_flip_time) priv->min_flip_time = us; if (us > priv->max_flip_time) priv->max_flip_time = us; } const int measure_interval = 100; if (priv->num_frames_drawn > 0 && priv->num_frames_drawn % measure_interval == 0) { uint64_t us; float flip_avg, draw_avg; us = get_time_elapsed_us(&priv->draw_start_time, &now); flip_avg = (float)us / measure_interval / 1000; draw_avg = (float)priv->draw_total_time / measure_interval / 1000; printf("Output %u: draw %f ms, flip avg/min/max %f/%f/%f\n", out->output_id, draw_avg, flip_avg, priv->min_flip_time / 1000.0, priv->max_flip_time / 1000.0); priv->draw_start_time = now; priv->draw_total_time = 0; priv->min_flip_time = UINT64_MAX; priv->max_flip_time = 0; } /* draw */ { /* back buffer */ struct framebuffer *buf = &out->bufs[(out->front_buf + 1) % out->num_buffers]; struct timespec ts1, ts2; get_time_now(&ts1); int old_xpos = (priv->bar_xpos + (buf->width - bar_width - bar_speed)) % (buf->width - bar_width); priv->bar_xpos = (priv->bar_xpos + bar_speed) % (buf->width - bar_width); drm_draw_color_bar(buf, old_xpos, priv->bar_xpos, bar_width); get_time_now(&ts2); priv->draw_total_time += get_time_elapsed_us(&ts1, &ts2); } priv->num_frames_drawn += 1; modeset_start_flip(out); }