static void getoptions (int argc, char *argv[]) { int cc; while ((cc = getopt (argc, argv, "hVlf:u:m:C:S:")) != EOF) { switch (cc) { case 'C': complete = getinterval (optarg); break; case 'S': start = getinterval (optarg); break; case 'V': fputs (_LS_VERSION_, stderr); exit (-1); case 'f': strcpy (logfile, optarg); break; case 'h': usage (argv[0]); case 'l': details = 1; break; case 'm': getarglist (optarg, hosts, MAX_HOSTS + 1); expandhostnames (); break; case 'u': getarglist (optarg, users, MAX_USERS + 1); if (strcmp (users[0], "all") == 0) users[0] = NULL; break; case '?': default: usage (argv[0]); } } }
/* * Process a single command. */ static int cmd_dispatch(char *cmd) { time_t beforesecs, aftersecs, secs; uint32_t beforensecs, afternsecs, nsecs; char *args[MAXMENUARGS]; int nargs=0; char *word; char *context; int i, result; for (word = strtok_r(cmd, " \t", &context); word != NULL; word = strtok_r(NULL, " \t", &context)) { if (nargs >= MAXMENUARGS) { kprintf("Command line has too many words\n"); return E2BIG; } args[nargs++] = word; } if (nargs==0) { return 0; } for (i=0; cmdtable[i].name; i++) { if (*cmdtable[i].name && !strcmp(args[0], cmdtable[i].name)) { KASSERT(cmdtable[i].func!=NULL); gettime(&beforesecs, &beforensecs); result = cmdtable[i].func(nargs, args); gettime(&aftersecs, &afternsecs); getinterval(beforesecs, beforensecs, aftersecs, afternsecs, &secs, &nsecs); kprintf("Operation took %lu.%09lu seconds\n", (unsigned long) secs, (unsigned long) nsecs); return result; } } kprintf("%s: Command not found\n", args[0]); return EINVAL; }
static void cat_simulation(void * unusedpointer, unsigned long catnumber) { int i; unsigned int bowl; time_t before_sec, after_sec, wait_sec; uint32_t before_nsec, after_nsec, wait_nsec; /* avoid unused variable warnings. */ (void) unusedpointer; (void) catnumber; for(i=0;i<NumLoops;i++) { /* make the cat sleep */ cat_sleep(CatSleepTime); /* choose bowl. legal bowl numbers range from 1 to NumBowls */ bowl = ((unsigned int)random() % NumBowls) + 1; gettime(&before_sec,&before_nsec); cat_before_eating(bowl); /* student-implemented function */ gettime(&after_sec,&after_nsec); /* make the cat eat */ cat_eat(bowl, CatEatTime, catnumber); cat_after_eating(bowl); /* student-implemented function */ /* update wait time statistics */ getinterval(before_sec,before_nsec,after_sec,after_nsec,&wait_sec,&wait_nsec); P(perf_mutex); cat_total_wait_secs += wait_sec; cat_total_wait_nsecs += wait_nsec; if (cat_total_wait_nsecs > 1000000000) { cat_total_wait_nsecs -= 1000000000; cat_total_wait_secs ++; } cat_wait_count++; V(perf_mutex); } /* indicate that this cat simulation is finished */ V(CatMouseWait); }
void eplister::mainfunction() { g_log.log(LOG_LV_INFO,"eplister thread come!"); int ifd = -1; sockaddr_in cliaddr; socklen_t addrlen = sizeof(cliaddr); //overtime handle thread; unsigned int uilastcheckalive = getticktime(); while (isrun()) { ifd = accept(m_listerfd,(sockaddr*)&cliaddr,&addrlen); if(ifd != -1) { g_log.log(LOG_LV_DEBUG,"link in,sock %d.",ifd); if (m_pep->bfull(ifd)) { g_log.log(LOG_LV_DEBUG,"link refuse,sock %d.",ifd); close(ifd); } else { m_pep->dispatch(ifd,cliaddr.sin_addr.s_addr,ntohs(cliaddr.sin_port)); } } else { if (errno == EAGAIN || errno == EWOULDBLOCK) usleep(1 * 1000); } if (getinterval(uilastcheckalive,getticktime()) > (MAXDIETIME / 2)) { m_pep->checkalive(); uilastcheckalive = getticktime(); } } g_log.log(LOG_LV_INFO,"eplister thread exit!"); }
void SpicePosition::init() { erract_c("SET", 0, (char*)"IGNORE"); const char * k = "../cfg/kernels.furnsh"; furnsh_c(k); std::vector<std::string> ks; kernels(ks); for (std::vector<std::string>::iterator i = ks.begin(); i != ks.end(); i++) { getbodies(*i); } for (BodyConstantsConstIterator it = m_bodies.begin(); it != m_bodies.end(); it++) { EphemeridesInterval & interval = m_intervals[(*it)->id()]; getinterval(ks, (*it)->id(), interval); if (it == m_bodies.begin()) { m_interval.a = interval.a; m_interval.b = interval.b; } else { m_interval.a = std::max(m_interval.a, interval.a); m_interval.b = std::min(m_interval.b, interval.b); } } m_interval.a += 100; m_interval.b -= 100; char timea[1000], timeb[1000]; timout_c (m_interval.a, "YYYY ::TDB", sizeof(timea), timea); timout_c (m_interval.b, "YYYY ::TDB", sizeof(timeb), timeb); erract_c("SET", 0, (char*)"DEFAULT"); }
bool Stamp2Showtime::toshowtime(u_int64 timestamp, u_int64& showtime, bool isVirtual, u_int64 sysTime) { double d_showtime; getinterval(); //不使用均匀时间戳算法 if(! isVirtual) { // 第一次计算显示时间的话需要设置时间戳到显示时间的转换基准 if(_isfirst) { _isfirst = false; reset(timestamp, sysTime); } d_showtime = _prev_showtime + (timestamp - _prev_timestamp); // 如果转换出的显示时间与当前时间差距大于1s会认为时间戳异常,重置转换基准 //显示时间小于系统时间,时间戳异常,重置转换基准 u_int64 diff2 = d_showtime > sysTime ? (d_showtime - sysTime) : (sysTime - d_showtime); if((diff2 >= 500) || (d_showtime < sysTime)) { reset(timestamp, sysTime); //显示时间重置 d_showtime = _base_showtime; LOG(LEVEL_INFO, "*********************** Reset Showtime"); } _prev_timestamp = timestamp; showtime = _prev_showtime = d_showtime; return true; } // 获取当前时间 #if 1 struct timeval tv; gettimeofday(&tv, NULL); u_int64 nowtime = ((u_int64)tv.tv_sec) * 1000 + tv.tv_usec / 1000; #else struct _timeb tb; _ftime(&tb); u_int64 nowtime = ((u_int64)tb.time) * 1000 + tb.millitm; #endif // 第一次计算显示时间的话需要设置时间戳到显示时间的转换基准 if(_isfirst) { _isfirst = false; reset(timestamp, nowtime); } //按统计到帧间隔计算显示时间 d_showtime = _prev_showtime + (double)_interval_val/_interval_div; //判断延迟情况 __int64 delay = d_showtime - nowtime; if(delay > CACHE_TIME*150/100) { _adjust_unit = -2; _adjust_count = CACHE_TIME*50/100; } else if(delay < CACHE_TIME*50/100) { _adjust_unit = 2; _adjust_count = CACHE_TIME*50/100/2; } else if(abs(delay - CACHE_TIME) < 30) { _adjust_unit = 0; _adjust_count = 0; } //计算显示时间 if(_adjust_count-- > 0) { d_showtime = _prev_showtime + (double)_interval_val/_interval_div + _adjust_unit; } else { d_showtime = _prev_showtime + (double)_interval_val/_interval_div; } // 如果转换出的显示时间与当前时间差距大于1s会认为时间戳异常,重置转换基准 u_int64 diff2 = d_showtime > nowtime ? (d_showtime - nowtime) : (nowtime - d_showtime); if(diff2 >= 500)// || (d_showtime < nowtime)) { reset(timestamp, nowtime); //显示时间重置 d_showtime = _base_showtime; LOG(LEVEL_INFO, "*********************** Reset Showtime"); } showtime = d_showtime; //LOG(LEVEL_INFO, "showtime=%lld, %lld interval=%d, %d, %d", showtime, (u_int64)_prev_showtime, _interval, _interval_val, _interval_div); _prev_showtime = d_showtime; return true; }
int catmouse(int nargs, char ** args) { int catindex, mouseindex, error; int i; int mean_cat_wait_usecs, mean_mouse_wait_usecs; time_t before_sec, after_sec, wait_sec; uint32_t before_nsec, after_nsec, wait_nsec; int total_bowl_milliseconds, total_eating_milliseconds, utilization_percent; /* check and process command line arguments */ if ((nargs != 9) && (nargs != 5)) { kprintf("Usage: <command> NUM_BOWLS NUM_CATS NUM_MICE NUM_LOOPS\n"); kprintf("or\n"); kprintf("Usage: <command> NUM_BOWLS NUM_CATS NUM_MICE NUM_LOOPS "); kprintf("CAT_EATING_TIME CAT_SLEEPING_TIME MOUSE_EATING_TIME MOUSE_SLEEPING_TIME\n"); return 1; // return failure indication } /* check the problem parameters, and set the global variables */ NumBowls = atoi(args[1]); if (NumBowls <= 0) { kprintf("catmouse: invalid number of bowls: %d\n",NumBowls); return 1; } NumCats = atoi(args[2]); if (NumCats < 0) { kprintf("catmouse: invalid number of cats: %d\n",NumCats); return 1; } NumMice = atoi(args[3]); if (NumMice < 0) { kprintf("catmouse: invalid number of mice: %d\n",NumMice); return 1; } NumLoops = atoi(args[4]); if (NumLoops <= 0) { kprintf("catmouse: invalid number of loops: %d\n",NumLoops); return 1; } if (nargs == 9) { CatEatTime = atoi(args[5]); if (CatEatTime < 0) { kprintf("catmouse: invalid cat eating time: %d\n",CatEatTime); return 1; } CatSleepTime = atoi(args[6]); if (CatSleepTime < 0) { kprintf("catmouse: invalid cat sleeping time: %d\n",CatSleepTime); return 1; } MouseEatTime = atoi(args[7]); if (MouseEatTime < 0) { kprintf("catmouse: invalid mouse eating time: %d\n",MouseEatTime); return 1; } MouseSleepTime = atoi(args[8]); if (MouseSleepTime < 0) { kprintf("catmouse: invalid mouse sleeping time: %d\n",MouseSleepTime); return 1; } } if ((NumMice >= INVALID_ANIMAL_NUM) || (NumCats >= INVALID_ANIMAL_NUM)) { panic("Trying to use too many cats or mice: limit = %d\n", INVALID_ANIMAL_NUM); } kprintf("Using %d bowls, %d cats, and %d mice. Looping %d times.\n", NumBowls,NumCats,NumMice,NumLoops); kprintf("Using cat eating time %d, cat sleeping time %d\n", CatEatTime, CatSleepTime); kprintf("Using mouse eating time %d, mouse sleeping time %d\n", MouseEatTime, MouseSleepTime); /* create the semaphore that is used to make the main thread wait for all of the cats and mice to finish */ CatMouseWait = sem_create("CatMouseWait",0); if (CatMouseWait == NULL) { panic("catmouse: could not create semaphore\n"); } /* initialize our simulation state */ initialize_bowls(); /* initialize the synchronization functions */ catmouse_sync_init(NumBowls); /* get current time, for measuring total simulation time */ gettime(&before_sec,&before_nsec); /* * Start NumCats cat_simulation() threads and NumMice mouse_simulation() threads. * Alternate cat and mouse creation. */ for (catindex = 0; catindex < NumCats; catindex++) { error = thread_fork("cat_simulation thread", NULL, cat_simulation, NULL, catindex); if (error) { panic("cat_simulation: thread_fork failed: %s\n", strerror(error)); } if (catindex < NumMice) { error = thread_fork("mouse_simulation thread", NULL, mouse_simulation, NULL, catindex); if (error) { panic("mouse_simulation: thread_fork failed: %s\n",strerror(error)); } } } /* launch any remaining mice */ for(mouseindex = catindex; mouseindex < NumMice; mouseindex++) { error = thread_fork("mouse_simulation thread", NULL, mouse_simulation, NULL, mouseindex); if (error) { panic("mouse_simulation: thread_fork failed: %s\n",strerror(error)); } } /* wait for all of the cats and mice to finish before terminating */ for(i=0;i<(NumCats+NumMice);i++) { P(CatMouseWait); } /* get current time, for measuring total simulation time */ gettime(&after_sec,&after_nsec); /* compute total simulation time */ getinterval(before_sec,before_nsec,after_sec,after_nsec,&wait_sec,&wait_nsec); /* compute and report bowl utilization */ total_bowl_milliseconds = (wait_sec*1000 + wait_nsec/1000000)*NumBowls; total_eating_milliseconds = (NumCats*CatEatTime + NumMice*MouseEatTime)*NumLoops*1000; if (total_bowl_milliseconds > 0) { utilization_percent = total_eating_milliseconds*100/total_bowl_milliseconds; kprintf("STATS: Bowl utilization: %d%%\n",utilization_percent); } /* clean up the semaphore that we created */ sem_destroy(CatMouseWait); /* clean up the synchronization state */ catmouse_sync_cleanup(NumBowls); /* clean up resources used for tracking bowl use */ cleanup_bowls(); if (cat_wait_count > 0) { /* some rounding error here - not significant if cat_wait_count << 1000000 */ mean_cat_wait_usecs = (cat_total_wait_secs*1000000+cat_total_wait_nsecs/1000)/cat_wait_count; kprintf("STATS: Mean cat waiting time: %d.%d seconds\n", mean_cat_wait_usecs/1000000,mean_cat_wait_usecs%1000000); } if (mouse_wait_count > 0) { /* some rounding error here - not significant if mouse_wait_count << 1000000 */ mean_mouse_wait_usecs = (mouse_total_wait_secs*1000000+mouse_total_wait_nsecs/1000)/mouse_wait_count; kprintf("STATS: Mean mouse waiting time: %d.%d seconds\n", mean_mouse_wait_usecs/1000000,mean_mouse_wait_usecs%1000000); } return 0; }