void RunTimeCounterFo_exec( plc_sThread *tp, pwr_sClass_RunTimeCounterFo *o) { pwr_tDeltaTime TimeSince; pwr_sClass_RunTimeCounter *co = (pwr_sClass_RunTimeCounter *) o->PlcConnectP; if ( !co) return; if ( *o->ResetP && !o->OldReset) co->TripReset = 1; time_FloatToD( &TimeSince, *o->ScanTime); /* Test if New Trip */ if (co->TripReset) { co->OldTripNOfStarts = co->TripNOfStarts; co->OldTripUsage = co->TripUsage; co->OldTripTime = co->TripTime; co->OldTripRunTime = co->TripRunTime; co->TripNOfStarts = 0; co->TripRunTime.tv_sec = co->TripRunTime.tv_nsec = 0; co->TripTime.tv_sec = co->TripTime.tv_nsec = 0; time_GetTime( &co->ResetTime); co->TripReset = 0; } /* Update Calendar time */ time_Dadd( &co->TotalTime, &co->TotalTime, &TimeSince); time_Dadd( &co->TripTime, &co->TripTime, &TimeSince); /* Test if running */ o->Start = 0; if (*o->RunningP) { /* New start ? */ if ( !o->Running) { o->Start = 1; co->TotalNOfStarts++; co->TripNOfStarts++; time_GetTime( &co->StartTime); } /* End if new start */ /* Update Running Time */ time_Dadd( &co->TripRunTime, &co->TripRunTime, &TimeSince); time_Dadd( &co->TotalRunTime, &co->TotalRunTime, &TimeSince); } /* End if Running */ o->Running = co->Running = *o->RunningP; /* Calculate usage % */ if ( co->TotalRunTime.tv_sec) co->TotalUsage = ((float)co->TotalRunTime.tv_sec) / co->TotalTime.tv_sec * 100; if ( co->TripTime.tv_sec) co->TripUsage = ((float)co->TripRunTime.tv_sec) / co->TripTime.tv_sec * 100; o->OldReset = *o->ResetP; }
pwr_tDeltaTime * time_Uptime ( pwr_tStatus *status, pwr_tDeltaTime *tp, pwr_tDeltaTime *ap ) { pwr_tDeltaTime time; long tics; void *argv[2]; pwr_dStatus(sts, status, TIME__SUCCESS); if (tp == NULL) tp = &time; argv[0] = (void *) 1; argv[1] = &tics; *sts = sys$cmexec(&uptime, argv); tp->tv_sec = tics / 100; tp->tv_nsec = (tics % 100) * 10000000; pwr_Assert(tp->tv_sec >= 0 && tp->tv_nsec >= 0); if (ap != NULL) return time_Dadd(tp, tp, ap); else return tp; }
pwr_tDeltaTime * time_Uptime ( pwr_tStatus *status, pwr_tDeltaTime *tp, pwr_tDeltaTime *ap ) { pwr_tDeltaTime time; long tics; struct tms buff; static int tics_per_sec = 0; static pwr_tTime boot_time = {0,0}; static pwr_tDeltaTime max_diff = {0,20000000}; pwr_tDeltaTime uptime_tics; pwr_tTime current_time; pwr_tDeltaTime diff; pwr_dStatus(sts, status, TIME__SUCCESS); if ( !tics_per_sec) tics_per_sec = sysconf(_SC_CLK_TCK); if (tp == NULL) tp = &time; tics = times(&buff); uptime_tics.tv_sec = tics / tics_per_sec; uptime_tics.tv_nsec = (tics % tics_per_sec) * (1000000000 / tics_per_sec); // pwr_Assert(tp->tv_sec >= 0 && tp->tv_nsec >= 0); clock_gettime( CLOCK_REALTIME, ¤t_time); if ( !boot_time.tv_sec) { time_Asub( &boot_time, ¤t_time, &uptime_tics); *tp = uptime_tics; } else { time_Adiff( tp, ¤t_time, &boot_time); time_Dsub( &diff, tp, &uptime_tics); time_Dabs(NULL, &diff); if ( time_Dcomp(&diff, &max_diff) > 0) { time_Asub( &boot_time, ¤t_time, &uptime_tics); *tp = uptime_tics; *status = TIME__CLKCHANGE; } } if (ap != NULL) return time_Dadd(tp, tp, ap); else return tp; }
static int tlog_checktime( pwr_tDeltaTime *time_new, pwr_tDeltaTime *time_old, float maxdiff) { pwr_tDeltaTime tim_maxdiff; pwr_tDeltaTime tim_limlow; pwr_tDeltaTime tim_limhigh; int sts; time_FloatToD( &tim_maxdiff, maxdiff); time_Dadd( &tim_limhigh, time_new, &tim_maxdiff); time_Dsub( &tim_limlow, time_new, &tim_maxdiff); if ( time_Dcomp( time_old, &tim_limlow) == -1) return TLOG__TIME_LT; if ( time_Dcomp( time_old, &tim_limhigh) == 1) return TLOG__TIME_GT; return TLOG__TIME_EQ; /************* tim_maxdiff.high = -1; tim_maxdiff.low = -maxdiff * 10000000; if ( maxdiff == 0) tim_maxdiff.low = -1; sts = lib$add_times( time_new, &tim_maxdiff, &tim_limhigh); sts = lib$sub_times( time_new, &tim_maxdiff, &tim_limlow); if ( sts == LIB$_NEGTIM) { tim_limlow.high = -1; tim_limlow.low = -1; } sts = lib$sub_times( time_old, &tim_limlow, &testtime); if ( sts == LIB$_NEGTIM) return TLOG__TIME_LT; sts = lib$sub_times( &tim_limhigh, time_old, &testtime); if ( sts == LIB$_NEGTIM) return TLOG__TIME_GT; return TLOG__TIME_EQ; ************/ }
pwr_tDeltaTime * time_Uptime ( pwr_tStatus *status, pwr_tDeltaTime *tp, pwr_tDeltaTime *ap ) { pwr_tDeltaTime time; unsigned long tics; static pwr_tUInt64 tics_64; struct tms buff; static int tics_per_sec = 0; static pwr_tTime boot_time = {0,0}; static pwr_tDeltaTime max_diff = {0, 20000000}; pwr_tDeltaTime uptime_tics; pwr_tTime current_time; pwr_tDeltaTime diff; static pwr_tUInt16 msb_flips = 0; static pwr_tBoolean old_high_bit = 0; pwr_tBoolean high_bit; lldiv_t uptime_s; pwr_dStatus(sts, status, TIME__SUCCESS); if ( !tics_per_sec) tics_per_sec = sysconf(_SC_CLK_TCK); if (tp == NULL) tp = &time; tics = times(&buff); high_bit = tics >> (32 - 1); if (!high_bit && old_high_bit) msb_flips++; old_high_bit = high_bit; tics_64 = ((pwr_tUInt64) msb_flips << 32) | tics; uptime_s = lldiv(tics_64, (pwr_tInt64) tics_per_sec); uptime_tics.tv_sec = (pwr_tInt64) uptime_s.quot; uptime_tics.tv_nsec = ((pwr_tUInt64) uptime_s.rem) * (1000000000 / tics_per_sec); // pwr_Assert(tp->tv_sec >= 0 && tp->tv_nsec >= 0); time_GetTime( ¤t_time); if ( !boot_time.tv_sec) { time_Asub( &boot_time, ¤t_time, &uptime_tics); *tp = uptime_tics; } else { time_Adiff( tp, ¤t_time, &boot_time); time_Dsub( &diff, tp, &uptime_tics); time_Dabs(NULL, &diff); if ( time_Dcomp(&diff, &max_diff) > 0) { time_Asub( &boot_time, ¤t_time, &uptime_tics); *tp = uptime_tics; if (status != NULL) { *status = TIME__CLKCHANGE; } } } if (ap != NULL) return time_Dadd(tp, tp, ap); else return tp; }
int csup_Exec(pwr_tStatus* status, lst_sEntry* lh, pwr_tDeltaTime* next_start, pwr_tDeltaTime* stop, pwr_tTime* now) { int action = 0; csup_sObject* cp; pwr_tDeltaTime nextLimit; pwr_dStatus(sts, status, CSUP__SUCCESS); if (lh == NULL || next_start == NULL || now == NULL || stop == NULL) pwr_Return(NO, sts, CSUP__NULPOINT); while ((cp = lst_Succ(NULL, lh, &lh)) != NULL) { pwr_sClass_CycleSup* o = cp->o; if (cp->is_owner) { if (time_Dcomp(&o->NextLimit, NULL) > 0) { if (time_Dcomp(stop, &o->NextLimit) > 0) { if (!o->Delayed) { o->DelayedTime = *now; o->Delayed = TRUE; o->Timely = FALSE; } o->DelayCount++; o->LastDelay = *now; action = MAX(action, o->DelayAction); } else if (!o->Timely) { o->Timely = TRUE; o->TimelyTime = *now; } } if ((o->DelayLimit.tv_sec & 1 << 31) != (o->DelayLimit.tv_nsec & 1 << 31)) { // printf("DelayLimit.tv_sec: %d, DelayLimit.tv_nsec: %d\n", // o->DelayLimit.tv_sec, o->DelayLimit.tv_nsec); errh_Info("DelayLimit.tv_sec: %d, DelayLimit.tv_nsec: %d", o->DelayLimit.tv_sec, o->DelayLimit.tv_nsec); } if ((next_start->tv_sec & 1 << 31) != (next_start->tv_nsec & 1 << 31)) { // printf("next_start->tv_sec: %d, next_start->tv_nsec: %d\n", // next_start->tv_sec, next_start->tv_nsec); errh_Info("next_start->tv_sec: %d, next_start->tv_nsec: %d", next_start->tv_sec, next_start->tv_nsec); } time_Dadd(&nextLimit, next_start, &o->DelayLimit); /* If we update the tv_nsec field first it is possible that emon will detect a slip even if it is not. */ o->NextLimit.tv_sec = nextLimit.tv_sec; o->NextLimit.tv_nsec = nextLimit.tv_nsec; o->CycleCount++; } else { /* Not owner, check stall delay */ if (o->DelayAction == 2) { nextLimit.tv_nsec = o->NextLimit.tv_nsec; nextLimit.tv_sec = o->NextLimit.tv_sec; if (time_Dcomp(&nextLimit, NULL) > 0 && time_Dcomp(stop, &nextLimit) > 0) { o->DelayCount++; o->LastDelay = *now; action = MAX(action, o->DelayAction); } } } } return action; }