int64_t ThriftServer::getLoad(const std::string& counter, bool check_custom) { if (check_custom && getLoad_) { return getLoad_(counter); } int reqload = 0; int connload = 0; int queueload = 0; if (maxRequests_ > 0) { reqload = (100*(activeRequests_ + getPendingCount())) / ((float)maxRequests_); } auto ioGroup = getIOGroupSafe(); auto workerFactory = ioGroup != nullptr ? std::dynamic_pointer_cast<wangle::NamedThreadFactory>( ioGroup->getThreadFactory()) : nullptr; if (maxConnections_ > 0) { int32_t connections = 0; forEachWorker([&](wangle::Acceptor* acceptor) mutable { auto worker = dynamic_cast<Cpp2Worker*>(acceptor); connections += worker->getPendingCount(); }); connload = (100*connections) / (float)maxConnections_; } auto tm = getThreadManager(); if (tm) { auto codel = tm->getCodel(); if (codel) { queueload = codel->getLoad(); } } if (VLOG_IS_ON(1) && workerFactory) { FB_LOG_EVERY_MS(INFO, 1000 * 10) << workerFactory->getNamePrefix() << " load is: " << reqload << "% requests, " << connload << "% connections, " << queueload << "% queue time, " << activeRequests_ << " active reqs, " << getPendingCount() << " pending reqs"; } int load = std::max({reqload, connload, queueload}); return load; }
void NaughtyProcessMonitor::slotTimeout() { uint cpu = cpuLoad(); emit(load(cpu)); if(cpu > d->triggerLevel_ * (d->interval_ / 1000)) { uint load; QValueList< ulong > l(pidList()); for(QValueList< ulong >::ConstIterator it(l.begin()); it != l.end(); ++it) if(getLoad(*it, load)) _process(*it, load); } d->timer_->start(d->interval_, true); }
int main(void){ int log = 0; //Start Switch // DDRA = 0x00; // PORTA = 0x12; //Start PORT A for switch and IR sensors DDRA = 0xFC; PORTA = 0xFE; //LED Initial DDRC = 0x7F; PORTC = 0x7E; DDRD = 0x70; PORTD = 0x11; MotorInit(); initSerial(); char * readData = NULL; int isFinish = 0; sensorInit(); if (isCaptureMode ==1) dxl_write_byte( BROADCAST_ID, P_TORQUE_ENABLE, 0 ); while(1){ sensorTest(0); sensorTest(1); sensorTest(2); setMode(); if( checkSerialRead() > 0 ){ readData = getReadBuffer(); if( readData != NULL ){ // printf( "readData=%s\n", &readData[0] ); split( &readData[0] ); switch( serCmd[0] ){ case EVT_ACTION: ServoControl( serCmd[1] ); // setSpeedTest( serCmd[1] ); sendAck(1); break; case EVT_START_MOTION: startMotion( serCmd[1], serCmd[2] ); PORTC = ~(1 << (LED_MAX - 2)); sendAck(1); break; case EVT_STOP_MOTION: stopMotion(); sendAck(1); break; case EVT_FORCE_MOTION: forceMotion( serCmd[1], serCmd[2] ); break; case EVT_GET_NOW_ANGLE: getAngle(); break; case EVT_SET_ANGLE: setAngle(); case EVT_GET_ACT_ANGLE: if( serCmd[1] >= ACT_MAX ){ sendAck(0); }else{ sendActAngle(serCmd[1]); } break; case EVT_GET_LOAD: getLoad(); // printf( "%d\n", movingTime ); break; case EVT_GET_VOLTAGE: getVoltage(); break; case EVT_TORQUE_DISABLE: dxl_write_byte( BROADCAST_ID, P_TORQUE_ENABLE, 0 ); break; case EVT_WATCH_DOG: watchDogCnt = 0; break; case EVT_MOTION_EDIT: break; case 999: // printf( "finish\n"); sendAck(999); isFinish = 1; break; default: sendAck(0); } if( isFinish > 0 ){ MotorControl( 0, 0 ); break; } memset( readData, 0x00, SERIAL_BUFFER_SIZE ); } } memset( &serCmd[0], 0x00, sizeof(int) * SERIAL_BUFFER_SIZE ); if (~PINA & SW_START ) { if (log == 1) printf( "main() 0\n"); if( iStart > 0 ){ iStart = 0; PORTC = LED_BAT|LED_TxD|LED_RxD|LED_AUX|LED_MANAGE|LED_PROGRAM|LED_PLAY; if (isCaptureMode != 1) ServoControl( 0 ); } }else{ if( iStart == 0 ){ PORTC &= ~LED_PLAY; iStart = 1; } if( modeWait <= 0 ){ if (log == 1) printf( "main() 1\n"); setModeAction(); if (isMovetest == 1) { moveTest(); } else { move(); } }else{ if (log == 1) printf( "main() 2\n"); modeWait -= MAIN_DELAY; } } if (sensorValue[0] == 0 && sensorValueOld[0] != sensorValue[0]) { if (log == 1) printf( "### main() sensorValue[0] == 0\n"); PORTC |= LED_PROGRAM; //edit }else if (sensorValueOld[0] != sensorValue[0]){ if (log == 1) printf( "### main() sensorValue[0] == 1\n"); PORTC &= ~LED_PROGRAM; //edit } if (sensorValue[1] == 0 && sensorValueOld[1] != sensorValue[1]) { if (log == 1) printf( "### main() sensorValue[1] == 0\n"); PORTC |= LED_MANAGE; //mon }else if (sensorValueOld[1] != sensorValue[1]){ if (log == 1) printf( "### main() sensorValue[1] == 1\n"); PORTC &= ~LED_MANAGE; //mon } if (sensorValue[2] == 0 && sensorValueOld[2] != sensorValue[2]) { if (log == 1) printf( "### main() sensorValue[2] == 0\n"); PORTC |= LED_AUX; //AUX }else if (sensorValueOld[2] != sensorValue[2]){ if (log == 1) printf( "### main() sensorValue[2] == 1\n"); PORTC &= ~LED_AUX; //AUX } sensorValueOld[0] = sensorValue[0]; sensorValueOld[1] = sensorValue[1]; sensorValueOld[2] = sensorValue[2]; // walk pattern LED // brinkLED(); _delay_ms(MAIN_DELAY); watchDogCnt++; caputureCount1++; if (caputureCount1 == 25){ if (isCaptureMode == 1) getAngle(); caputureCount1 = 0; } } }
static int makeFields(struct hostInfoEnt *host, char *loadval[], char **dispindex, int option) { int j, id, nf, index; char *sp; char tmpfield[MAXFIELDSIZE]; char fmtField[MAXFIELDSIZE]; char firstFmt[MAXFIELDSIZE]; float real, avail, load; nf = 0; for(j=0; dispindex[j] && j < host->nIdx; j++, nf++) { int newIndexLen; id = nameToFmt(dispindex[j]); if (id == DEFAULT_FMT) newIndexLen = strlen(dispindex[j]); real = getLoad(dispindex[j], host->realLoad, &index); avail = getLoad(dispindex[j], host->load, &index); if (option == TRUE) load = avail; else { real = getLoad(dispindex[j], host->realLoad, &index); load = (avail >= real)? (avail - real):(real - avail); } if (load >= INFINIT_LOAD) sp = "- "; else { if (option == TRUE && (host->hStatus & HOST_STAT_BUSY) && (LSB_ISBUSYON (host->busySched, index) || LSB_ISBUSYON (host->busyStop, index))) { strcpy(firstFmt, fmt[id].busy); sprintf(fmtField, "%s%s",firstFmt, fmt[id].normFmt); sprintf(tmpfield, fmtField, load * fmt[id].scale); } else { strcpy(firstFmt, fmt[id].ok); sprintf(fmtField, "%s%s", firstFmt, fmt[id].normFmt); sprintf(tmpfield, fmtField, load * fmt[id].scale); } sp = stripSpaces(tmpfield); if (strlen(sp) > fmt[id].dispLen) { if (load > 1024) sprintf(fmtField, "%s%s", firstFmt, fmt[id].expFmt); else sprintf(fmtField, "%s%s", firstFmt, fmt[id].normFmt); if ((load > 1024) && ((!strcmp(fmt[id].name,"mem")) || (!strcmp(fmt[id].name,"tmp")) || (!strcmp(fmt[id].name,"swp")))) sprintf(tmpfield,fmtField,(load*fmt[id].scale)/1024); else sprintf(tmpfield,fmtField, (load * fmt[id].scale)); } sp = stripSpaces(tmpfield); } if (id == DEFAULT_FMT && newIndexLen >= 7){ char newFmt[10]; sprintf(newFmt, " %s%d%s", "%", newIndexLen, "s"); sprintf(loadval[j], newFmt, sp); } else sprintf(loadval[j], fmt[id].hdr, sp); } return(nf); }
int main(void) { //========= Peripheral Enable and Setup ===========// setup_LCD(); //LCD Screen setup setup_tmp36(); //TMP36 ADC thermometer setup setup_GPS(); //GPS Pin Setup setup_microSD(); //MicroSD Pin Setup setupTMP102(); setup_PID(&pid); setupBMS(); setup_pwm(); //Setup the buttons setup_buttonInterrupts(); sleepEnablePeripherals(); setup_timerInterrupt(); //========= Peripheral enable and run ============// enable_LCD(); //Start LCD Commmunication enable_GPS(); //Start GPS Communication clearDisplay(); //Refresh Display while(1) { //Listen for the GPS Data listen_GPS(); //Open the datalog file for writing open_datalog(); //Obtain vehicle speed char *velocity; char velocityArray[] = "000"; //velocity = velocityArray; velocity = getVelocity(); //Convert string to int int speedInteger = atoi(velocity); //Convert knots to mph speedInteger = 1.15 * speedInteger; //Return to string sprintf(velocity, "%i", speedInteger); // velocity = "50"; selectLineOne(); //Show velocity putPhrase("V:"); putPhrase(velocity); //Analog Temperature Sensor int anag_tempValue = get_analog_temp(); sprintf(anag_temp, "%i", anag_tempValue); //Digital Temperature Sensor int digi_tempValue = getTemperature(); sprintf(digi_temp, "%i", digi_tempValue); //Get BMS Level char load[] = "89"; char *load_value; load_value = load; //-------BMS Communication------ sendStartSequence(); getDataString(); load_value = getLoad(); //------------------------------ //If Cruise Control is on if(enableSys) { //If initialized if(obtain_speed) { //Debouncing //SysCtlDelay(533333); obtain_speed = 0; clearDisplay(); //Get set velocity set_velocity = 10; } //If driver increases set speed if(incr_speed) { //SysCtlDelay(533333); incr_speed = 0; set_velocity += 1; } //if driver decresaes set speed else if(decr_speed) { //SysCtlDelay(533333); decr_speed = 0; set_velocity -= 1; } //==============PID Portion of the Main Loop ==============// float process_value = atof(velocity); //Convert String to Int float error = set_velocity - process_value; //Calculate error float u_t = UpdatePID(&pid, error, 1.0); //Feed error to the PID uint32_t duty_cycle = u_t*scaleFactor; if(duty_cycle < 950){ duty_cycle = 50; } else if(duty_cycle > 0){ duty_cycle = 950; } pwm_out(1000, duty_cycle); //Scale and output the PID output //====================================================// //Show other essentials to the LCD putPhrase("mph/"); char set_point[5]; sprintf(set_point, "%imph", (int) set_velocity); putPhrase(set_point); putPhrase(" ON"); //Cruise control on } else { //Spacer putPhrase("mph "); putPhrase("Standby "); } //Select bottom line selectLineTwo(); //Display analog and digital temperatures putPhrase("T:"); putPhrase(anag_temp); putPhrase("/"); putPhrase(digi_temp); putPhrase("C "); //Show Load level putPhrase("B:"); putPhrase(load_value); putPhrase("%"); write_datalog(velocity, "ddmm.mmmmmmX", "ddmm.mmmmmX", getTime(), anag_temp, digi_temp, load_value); close(); //Put system in low power mode SysCtlSleep(); } }
DECLARE_EXPORT void LoadPlan::setResource(Resource* newres, bool check) { // Nothing to do if (res == newres) return; // Validate the argument if (!newres) throw DataException("Can't switch to NULL resource"); if (check) { // New resource must be a subresource of the load's resource. bool ok = false; for (const Resource* i = newres; i && !ok; i = i->getOwner()) if (i == getLoad()->getResource()) ok = true; if (!ok) throw DataException("Resource isn't matching the resource specified on the load"); // New resource must have the required skill if (getLoad()->getSkill()) { ok = false; for(Resource::skilllist::const_iterator s = newres->getSkills().begin(); s != newres->getSkills().end() && !ok; s++) if (s->getSkill() == getLoad()->getSkill()) ok = true; if (!ok) throw DataException("Resource misses the skill specified on the load"); } } // Mark entities as changed if (oper) oper->getOperation()->setChanged(); if (res && res!=newres) res->setChanged(); newres->setChanged(); // Update also the setup operationplans if (oper && oper->getOperation() != OperationSetup::setupoperation) { bool oldHasSetup = ld && !ld->getSetup().empty() // TODO not fully correct. If the load is changed, it is still possible that the old load had a setup, while ld doesn't have one any more... && res && res->getSetupMatrix(); bool newHasSetup = ld && !ld->getSetup().empty() && newres->getSetupMatrix(); OperationPlan *setupOpplan = NULL; if (oldHasSetup) { for (OperationPlan::iterator i(oper); i != oper->end(); ++i) if (i->getOperation() == OperationSetup::setupoperation) { setupOpplan = &*i; break; } if (!setupOpplan) oldHasSetup = false; } if (oldHasSetup) { if (newHasSetup) { // Case 1: Both the old and new load require a setup LoadPlan *setupLdplan = NULL; for (OperationPlan::LoadPlanIterator j = setupOpplan->beginLoadPlans(); j != setupOpplan->endLoadPlans(); ++j) if (j->getLoad() == ld) { setupLdplan = &*j; break; } if (!setupLdplan) throw LogicException("Can't find loadplan on setup operationplan"); // Update the loadplan setupOpplan->setEnd(setupOpplan->getDates().getEnd()); } else { // Case 2: Delete the old setup which is not required any more oper->eraseSubOperationPlan(setupOpplan); } } else { if (newHasSetup) { // Case 3: Create a new setup operationplan OperationSetup::setupoperation->createOperationPlan( 1, Date::infinitePast, oper->getDates().getEnd(), NULL, oper); } //else: // Case 4: No setup for the old or new load } } // Find the loadplan before the setup LoadPlan *prevldplan = NULL; if (getOperationPlan()->getOperation() == OperationSetup::setupoperation) { for (TimeLine<LoadPlan>::const_iterator i = getResource()->getLoadPlans().begin(isStart() ? getOtherLoadPlan() : this); i != getResource()->getLoadPlans().end(); --i) { const LoadPlan *l = dynamic_cast<const LoadPlan*>(&*i); if (l && l->getOperationPlan() != getOperationPlan() && l->getOperationPlan() != getOperationPlan()->getOwner() && !l->isStart()) { prevldplan = const_cast<LoadPlan*>(l); break; } } if (!prevldplan) { for (TimeLine<LoadPlan>::const_iterator i = getResource()->getLoadPlans().begin(isStart() ? getOtherLoadPlan() : this); i != getResource()->getLoadPlans().end(); ++i) { const LoadPlan *l = dynamic_cast<const LoadPlan*>(&*i); if (l && l->getOperationPlan() != getOperationPlan() && l->getOperationPlan() != getOperationPlan()->getOwner() && !l->isStart()) { prevldplan = const_cast<LoadPlan*>(l); break; } } } } // Change this loadplan and its brother for (LoadPlan *ldplan = getOtherLoadPlan(); true; ) { // Remove from the old resource, if there is one if (res) { res->loadplans.erase(ldplan); res->setChanged(); } // Insert in the new resource. // This code assumes the date and quantity of the loadplan don't change // when a new resource is assigned. ldplan->res = newres; newres->loadplans.insert( ldplan, ld->getLoadplanQuantity(ldplan), ld->getLoadplanDate(ldplan) ); // Repeat for the brother loadplan or exit if (ldplan != this) ldplan = this; else break; } // Update the setups on the old resource if (prevldplan) prevldplan->res->updateSetups(prevldplan); // Change the resource newres->setChanged(); }
int main(int argc, char** argv) { //INITIALIZATION //////////////// printf("\nMULE starting, please wait...\n"); if(signal(SIGUSR1, sig_mule) == SIG_ERR) { fprintf(stderr, "\nCRITICAL: mule signals can not be handled!\n"); exit(EXIT_FAILURE); } errno = 0; int config_fd = open("config", O_RDONLY); if(config_fd < 0) { perror("open"); return 1; } int16_t per_cnt = (int16_t)getPeripheralCNT(config_fd);//counts the potential peripherals if(per_cnt == 0xFF) return 1; if(!per_cnt) { printf("\nNo peripherals to load.\n"); } else { load_t* load = malloc(per_cnt * sizeof(load_t)); if(load == NULL) { perror("malloc"); return 1; } per_cnt = getLoad(config_fd, load); if(per_cnt < 0) return 1; else if(!per_cnt) printf("\nNo peripherals to load.\n"); load = realloc(load, per_cnt); if(per_cnt && load == NULL) { perror("realloc"); return 1; } //Alocate shared memory ////////////////////// int sim_pins = shm_open("/sim_pins", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); if(sim_pins < 0) { perror("shm_open"); return 1; } int per_pins = shm_open("/per_pins", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); if(per_pins < 0) { printf("\n\n\tS_IWGRP failed without sudo\n\n"); perror("shm_open"); return 1; } if((ftruncate(sim_pins, 41 * sizeof(uint8_t))) < 0) { perror("ftruncate"); return 1; } if((ftruncate(per_pins, 41 * sizeof(uint8_t))) < 0) { perror("ftruncate"); return 1; } if(fcntl(sim_pins, F_SETFD, (fcntl(sim_pins, F_GETFD, 0) & (~FD_CLOEXEC))) < 0) { perror("fcntl"); return 1; } if(fcntl(per_pins, F_SETFD, (fcntl(per_pins, F_GETFD, 0) & (~FD_CLOEXEC))) < 0) { perror("fcntl"); return 1; } ///////////////////// //Allocate strings of the appropriate length, for passing the file descriptors ////////////////////////////////////////////////////////////////////////////// struct rlimit cur_lim; if(getrlimit(RLIMIT_NOFILE, &cur_lim) < 0) { perror("getrlimit"); return 1; } uint8_t digit_no = (uint8_t)log10(cur_lim.rlim_max) + 1; char* sim_pins_str = calloc(digit_no, sizeof(char)); if(sim_pins_str == NULL) { perror("calloc"); return 1; } char* per_pins_str = calloc(digit_no, sizeof(char)); if(per_pins_str == NULL) { perror("calloc"); return 1; } if(sprintf(sim_pins_str, "%d", sim_pins) < 0) { perror("sprintf"); return 1; } if(sprintf(per_pins_str, "%d", per_pins) <0) { perror("sprintf"); return 1; } ///////////////////////////////////////////////////////////////////////////// //Open necessary semaphores /////////////////////////// const char* sem_names[5] = {NULL, NULL, NULL, NULL, NULL}; const char P0_sem_nm[] = "/P0_sem"; const char P1_sem_nm[] = "/P1_sem"; const char P2_sem_nm[] = "/P2_sem"; const char P3_sem_nm[] = "/P3_sem"; const char othr_sem_nm[] = "/othr_sem"; sem_t* sems[5] = {NULL, NULL, NULL, NULL}; uint8_t i = 0; for(i = 0; i < per_cnt; ++i) { if(((load + i) -> ports)[0]) sem_names[0] = P0_sem_nm; if(((load + i) -> ports)[1]) sem_names[1] = P1_sem_nm; if(((load + i) -> ports)[2]) sem_names[2] = P2_sem_nm; if(((load + i) -> ports)[3]) sem_names[3] = P3_sem_nm; if(((load + i) -> ports)[4]) sem_names[4] = othr_sem_nm; } for(i = 0; i < 5; ++i) { if(sem_names[i] != NULL) { sems[i] = sem_open(sem_names[i], O_CREAT, S_IRUSR | S_IWUSR, 1); if(sems[i] == SEM_FAILED) { perror("sem_open"); return 1; } } } ////////////////////////// if(argc > 1 && !strcmp(argv[1], "--clean")) goto CRASH_CLN; //setup the exec arguments, fork(), execl() ////////////////////////////////////////// pid_t* peripherals = NULL; if((peripherals = calloc(per_cnt, sizeof(pid_t))) == NULL) { perror("calloc"); return 1; } uint8_t j, k; char progname[256]; for(i = 0; i < per_cnt; ++i) { //retrieve the name of the program to be executed, from the pathname ///////////////////////////////////////////////////////////////////// j = strlen((load + i) -> pathname); while(((load + i) -> pathname)[j] != '/') { progname[j] = ((load + i) -> pathname)[j]; if(j-- == 0) break; } //if the above loop exited by '/' encounter, progname will contain garbage at the beginning, which has to be removed //j points to the garbage place farthest to the right, should it exist, so it has to be advanced by one if(++j)//if there is no garbage, j overflowed to 255, so after being incremented, it will become zero once again for(k = 0; j <= strlen(progname); ++k) progname[k] = progname[j++]; //////////////////////////////////////////////////////////////////// char empty[] = "x"; char* argv[9] = {empty, empty, empty, empty, empty, empty, empty, empty, NULL}; argv[0] = progname; argv[1] = sim_pins_str; argv[2] = per_pins_str; if(((load + i) -> ports)[0]) argv[3] = sem_names[0]; if(((load + i) -> ports)[1]) argv[4] = sem_names[1]; if(((load + i) -> ports)[2]) argv[5] = sem_names[2]; if(((load + i) -> ports)[3]) argv[6] = sem_names[3]; if(((load + i) -> ports)[4]) argv[7] = sem_names[4]; if((peripherals[i] = fork()) < 0) { perror("fork"); return 1; } if(!peripherals[i]) { if(execv((load + i) -> pathname, argv) < 0) { printf("\nERROR: Failed to execute peripheral %d pathname: %s" "\nProceeding with next one.\n", i, ((load + i) -> pathname)); perror("execv"); exit(EXIT_FAILURE); } } } ///////////////////////////////////////// //exec the MULE /////////////// pid_t mule = fork(); if(mule < 0) { perror("fork"); return 1; } if(!mule) { if(execl("mule", "mule", sim_pins_str, per_pins_str, P0_sem_nm, P1_sem_nm, P2_sem_nm, P3_sem_nm, othr_sem_nm, NULL) < 0) { printf("\nCRITICAL: Failed to execute the simulator!\n"); perror("execl"); exit(EXIT_FAILURE); } } ////////////// close(fileno(stdin)); sleep(1); //CLEANUP ///////// sigset_t mask; sigfillset(&mask); sigdelset(&mask, SIGUSR1); sigdelset(&mask, SIGINT); sigprocmask(SIG_SETMASK, &mask, NULL); sigsuspend(&mask); kill(mule, SIGINT); int status; for(i = 0; i < per_cnt; ++i) { kill(peripherals[i], SIGINT); wait(&status); fprintf(stderr, "\nEXIT STATUS %d\n", status); } wait(&status);//wait for the MULE CRASH_CLN: close(sim_pins); close(per_pins); shm_unlink("/sim_pins"); shm_unlink("/per_pins"); for(i = 0; i < 5; ++i) { sem_close(sems[i]); sem_unlink(sem_names[i]); } sem_unlink("/ROM_sim_sem"); sem_unlink("/sim_ROM_sem"); printf("\nSimulation ended...\n"); ///////// }// //////////////// return 0; }