def main(argv): log_file = '%s.log' % FUNCTION_NAME log_level = logging.INFO global test_temp if len(sys.argv) != 1: try: opts, args = getopt.getopt(argv,'hdlt:',['lfile=']) except getopt.GetoptError: print 'Usage: %s [-d] [-l <log_file>]' % sys.argv[0] return 0 for opt, arg in opts: if opt == '-h': print 'Usage: %s [-d] [-l <log_file>]' % sys.argv[0] return 0 elif opt in ('-d', '--debug'): log_level = logging.DEBUG elif opt in ('-l', '--lfile'): log_file = arg if sys.argv[1]== '-t': if len(sys.argv)!=7: print "temp test, need input six temp" return 0 i=0 for x in range(2, 7): test_temp_list[i]= int(sys.argv[x])*1000 i=i+1 test_temp = 1 log_level = logging.DEBUG print test_temp_list fan = FanUtil() fan.set_fan_duty_cycle(38) print "set default fan speed to 37.5%" monitor = device_monitor(log_file, log_level) # Loop forever, doing something useful hopefully: while True: monitor.manage_fans() time.sleep(5)
def manage_fans(self): global fan_policy_state global fan_fail global test_temp global test_temp_list global alarm_state global temp_test_data LEVEL_FAN_DEF=0 LEVEL_FAN_MID=1 LEVEL_FAN_MAX=2 LEVEL_TEMP_HIGH=3 LEVEL_TEMP_CRITICAL=4 fan_policy_f2b = { LEVEL_FAN_DEF: [38, 0x4, 0, 38000], LEVEL_FAN_MID: [63, 0x6, 38000, 46000], LEVEL_FAN_MAX: [100, 0xE, 46000, 58000], LEVEL_TEMP_HIGH: [100, 0xE, 58000, 66000], LEVEL_TEMP_CRITICAL: [100, 0xE, 58000, 200000], } fan_policy_b2f = { LEVEL_FAN_DEF: [38, 0x4, 0, 34000], LEVEL_FAN_MID: [63, 0x8, 34000, 44000], LEVEL_FAN_MAX: [100, 0xE, 44000, 59000], LEVEL_TEMP_HIGH: [100, 0xE, 59000, 67000], LEVEL_TEMP_CRITICAL: [100, 0xE, 59000, 200000], } fan_policy = fan_policy_f2b thermal = ThermalUtil() fan = FanUtil() fan_dir=fan.get_fan_dir(1) if fan_dir == 1: fan_dri=1 #something wrong, set fan_dir to default val else: fan_policy = fan_policy_b2f ori_pwm=fan.get_fan_duty_cycle() new_pwm=0 logging.debug('fan_dir=%d, ori_pwm=%d', fan_dir, ori_pwm) logging.debug('test_temp=%d', test_temp) if test_temp==0: temp1 = thermal._get_thermal_val(1) temp2 = thermal._get_thermal_val(2) temp3 = thermal._get_thermal_val(3) temp4 = thermal._get_thermal_val(4) temp5 = thermal._get_thermal_val(5) else: temp1 = test_temp_list[0] temp2 = test_temp_list[1] temp3 = test_temp_list[2] temp4 = test_temp_list[3] temp5 = test_temp_list[4] fan_fail=0 if temp3==0: temp_get=50000 # if one detect sensor is fail or zero, assign temp=50000, let fan to 75% logging.debug('lm75_49 detect fail, so set temp_get=50000, let fan to 75%') elif temp4==0: temp_get=50000 # if one detect sensor is fail or zero, assign temp=50000, let fan to 75% logging.debug('lm75_4b detect fail, so set temp_get=50000, let fan to 75%') else: temp_get= (temp3 + temp4)/2 # Use (sensor_LM75_4a + sensor_LM75_4b) /2 ori_state=fan_policy_state #temp_test_data=temp_test_data+1000 #temp_get = temp_get + temp_test_data #print "Unit test:temp_get=%d"%temp_get fan_policy_state=self.get_state_from_fan_policy(temp_get, fan_policy) print "temp3=%d"%temp3 print "temp4=%d"%temp4 #print "temp_get=%d"%temp_get logging.debug('lm75_48=%d, lm75_49=%d, lm75_4a=%d, lm_4b=%d, lm_4b=%d', temp1,temp2,temp3,temp4,temp5) logging.debug('ori_state=%d, fan_policy_state=%d', ori_state, fan_policy_state) new_pwm = fan_policy[fan_policy_state][0] if fan_fail==0: logging.debug('new_fan_cycle=%d', new_pwm) if fan_fail==0: if new_pwm!=ori_pwm: fan.set_fan_duty_cycle(new_pwm) logging.info('Set fan speed from %d to %d', ori_pwm, new_pwm) #Check Fan status #for i in range (fan.FAN_NUM_1_IDX, fan.FAN_NUM_ON_MAIN_BROAD+1): # if fan.get_fan_status(i)==0: # new_pwm=100 # logging.debug('fan_%d fail, set pwm to 100',i) # if test_temp==0: # fan_fail=1 # fan.set_fan_duty_cycle(new_pwm) # break # else: # fan_fail=0 #if fan_policy_state == ori_state: # return True #else: new_state = fan_policy_state #logging.warning('Temperature high alarm testing') if ori_state==LEVEL_FAN_DEF: if new_state==LEVEL_TEMP_HIGH: if alarm_state==0: logging.warning('Alarm for temperature high is detected') alarm_state=1 if new_state==LEVEL_TEMP_CRITICAL: logging.critical('Alarm for temperature critical is detected, reboot DUT') time.sleep(2) os.system('reboot') if ori_state==LEVEL_FAN_MID: if new_state==LEVEL_TEMP_HIGH: if alarm_state==0: logging.warning('Alarm for temperature high is detected') alarm_state=1 if new_state==LEVEL_TEMP_CRITICAL: logging.critical('Alarm for temperature critical is detected') time.sleep(2) os.system('reboot') if ori_state==LEVEL_FAN_MAX: if new_state==LEVEL_TEMP_HIGH: if alarm_state==0: logging.warning('Alarm for temperature high is detected') alarm_state=1 if new_state==LEVEL_TEMP_CRITICAL: logging.critical('Alarm for temperature critical is detected') time.sleep(2) os.system('reboot') if alarm_state==1: if temp_get < (fan_policy[3][0] - 5000): #below 65 C, clear alarm logging.warning('Alarm for temperature high is cleared') alarm_state=0 if ori_state==LEVEL_TEMP_HIGH: if new_state==LEVEL_TEMP_CRITICAL: logging.critical('Alarm for temperature critical is detected') time.sleep(2) os.system('reboot') if new_state <= LEVEL_FAN_MID: logging.warning('Alarm for temperature high is cleared') alarm_state=0 if new_state <= LEVEL_FAN_MAX: if temp_get < (fan_policy[3][0] - 5000): #below 65 C, clear alarm logging.warning('Alarm for temperature high is cleared') alarm_state=0 if ori_state==LEVEL_TEMP_CRITICAL: if new_state <= LEVEL_FAN_MAX: logging.warning('Alarm for temperature critical is cleared') return True
def manage_fans(self): fan_policy_f2b = { 0: [32, 0, 174000], 1: [38, 170000, 182000], 2: [50, 178000, 190000], 3: [63, 186000, 0], } fan_policy_b2f = { 0: [32, 0, 140000], 1: [38, 135000, 150000], 2: [50, 145000, 160000], 3: [69, 15500, 0], } thermal = ThermalUtil() fan = FanUtil() get_temp = thermal.get_thermal_temp() cur_duty_cycle = fan.get_fan_duty_cycle() for x in range(fan.get_idx_fan_start(), fan.get_num_fans() + 1): fan_status = fan.get_fan_status(x) if fan_status is None: logging.debug( 'INFO. SET new_perc to %d (FAN stauts is None. fan_num:%d)', 100, x) return False if fan_status is False: logging.debug( 'INFO. SET new_perc to %d (FAN fault. fan_num:%d)', 100, x) fan.set_fan_duty_cycle(45) return True logging.debug('INFO. fan_status is True (fan_num:%d)', x) if fan_status is not None and fan_status is not False: fan_dir = fan.get_fan_dir(1) for x in range(0, 4): if cur_duty_cycle == fan_policy_f2b[x][0]: break if fan_dir == 1: if x == 4: fan.set_fan_duty_cycle(fan_policy_f2b[0][0]) new_duty_cycle = cur_duty_cycle # if temp > up_levle, else if temp < down_level if get_temp > fan_policy_f2b[x][2] and x != 3: new_duty_cycle = fan_policy_f2b[x + 1][0] logging.debug( 'INFO. THERMAL temp UP, temp %d > %d , new_duty_cycle=%d', get_temp, fan_policy_f2b[x][2], new_duty_cycle) elif get_temp < fan_policy_f2b[x][1]: new_duty_cycle = fan_policy_f2b[x - 1][0] logging.debug( 'INFO. THERMAL temp down, temp %d < %d , new_duty_cycle=%d', get_temp, fan_policy_f2b[x][1], new_duty_cycle) if new_duty_cycle == cur_duty_cycle: return True else: if x == 4: fan.set_fan_duty_cycle(fan_policy_b2f[0][0]) new_duty_cycle = cur_duty_cycle # if temp > up_levle, else if temp < down_level if get_temp > fan_policy_b2f[x][1] and x != 3: new_duty_cycle = fan_policy_b2f[x + 1][0] logging.debug( 'INFO. THERMAL temp UP, temp %d > %d , new_duty_cycle=%d', get_temp, fan_policy_b2f[x][2], new_duty_cycle) elif get_temp < fan_policy_b2f[x][0] and x != 0: new_duty_cycle = fan_policy_b2f[x - 1][0] logging.debug( 'INFO. THERMAL temp down, temp %d < %d , new_duty_cycle=%d', get_temp, fan_policy_b2f[x][1], new_duty_cycle) if new_duty_cycle == cur_duty_cycle: return True fan.set_fan_duty_cycle(new_duty_cycle) return True
def manage_fans(self): global fan_policy_state global fan_fail global test_temp global test_temp_list global alarm_state global temp_test_data LEVEL_FAN_DEF=0 LEVEL_FAN_MID=1 LEVEL_FAN_MAX=2 LEVEL_TEMP_HIGH=3 LEVEL_TEMP_CRITICAL=4 fan_policy_f2b = { LEVEL_FAN_DEF: [38, 0x4, 0, 38000], LEVEL_FAN_MID: [63, 0x6, 38000, 46000], LEVEL_FAN_MAX: [100, 0xE, 46000, 58000], LEVEL_TEMP_HIGH: [100, 0xE, 58000, 66000], LEVEL_TEMP_CRITICAL: [100, 0xE, 58000, 200000], } fan_policy_b2f = { LEVEL_FAN_DEF: [38, 0x4, 0, 34000], LEVEL_FAN_MID: [63, 0x8, 34000, 44000], LEVEL_FAN_MAX: [100, 0xE, 44000, 59000], LEVEL_TEMP_HIGH: [100, 0xE, 59000, 67000], LEVEL_TEMP_CRITICAL: [100, 0xE, 59000, 200000], } fan_policy = fan_policy_f2b thermal = ThermalUtil() fan = FanUtil() fan_dir=fan.get_fan_dir(1) if fan_dir == 1: fan_dri=1 #something wrong, set fan_dir to default val else: fan_policy = fan_policy_b2f ori_pwm=fan.get_fan_duty_cycle() new_pwm=0 logging.debug('fan_dir=%d, ori_pwm=%d', fan_dir, ori_pwm) logging.debug('test_temp=%d', test_temp) if test_temp==0: temp1 = thermal._get_thermal_val(1) temp2 = thermal._get_thermal_val(2) temp3 = thermal._get_thermal_val(3) temp4 = thermal._get_thermal_val(4) temp5 = thermal._get_thermal_val(5) else: temp1 = test_temp_list[0] temp2 = test_temp_list[1] temp3 = test_temp_list[2] temp4 = test_temp_list[3] temp5 = test_temp_list[4] fan_fail=0 if temp3==0: temp_get=50000 # if one detect sensor is fail or zero, assign temp=50000, let fan to 75% logging.debug('lm75_49 detect fail, so set temp_get=50000, let fan to 75%') elif temp4==0: temp_get=50000 # if one detect sensor is fail or zero, assign temp=50000, let fan to 75% logging.debug('lm75_4b detect fail, so set temp_get=50000, let fan to 75%') else: temp_get= (temp3 + temp4)/2 # Use (sensor_LM75_4a + sensor_LM75_4b) /2 ori_state=fan_policy_state #temp_test_data=temp_test_data+1000 #temp_get = temp_get + temp_test_data #print "Unit test:temp_get=%d"%temp_get fan_policy_state=self.get_state_from_fan_policy(temp_get, fan_policy) #print "temp3=%d"%temp3 #print "temp4=%d"%temp4 #print "temp_get=%d"%temp_get logging.debug('lm75_48=%d, lm75_49=%d, lm75_4a=%d, lm_4b=%d, lm_4b=%d', temp1,temp2,temp3,temp4,temp5) logging.debug('ori_state=%d, fan_policy_state=%d', ori_state, fan_policy_state) new_pwm = fan_policy[fan_policy_state][0] if fan_fail==0: logging.debug('new_fan_cycle=%d', new_pwm) if fan_fail==0: if new_pwm!=ori_pwm: fan.set_fan_duty_cycle(new_pwm) logging.info('Set fan speed from %d to %d', ori_pwm, new_pwm) #Check Fan status for i in range (fan.FAN_NUM_1_IDX, fan.FAN_NUM_ON_MAIN_BROAD+1): if fan.get_fan_status(i)==0: new_pwm=100 logging.debug('fan_%d fail, set pwm to 100',i) if test_temp==0: fan_fail=1 fan.set_fan_duty_cycle(new_pwm) break else: fan_fail=0 #if fan_policy_state == ori_state: # return True #else: new_state = fan_policy_state #logging.warning('Temperature high alarm testing') if ori_state==LEVEL_FAN_DEF: if new_state==LEVEL_TEMP_HIGH: if alarm_state==0: logging.warning('Alarm for temperature high is detected') alarm_state=1 if new_state==LEVEL_TEMP_CRITICAL: logging.critical('Alarm for temperature critical is detected, reboot DUT') time.sleep(2) os.system('reboot') if ori_state==LEVEL_FAN_MID: if new_state==LEVEL_TEMP_HIGH: if alarm_state==0: logging.warning('Alarm for temperature high is detected') alarm_state=1 if new_state==LEVEL_TEMP_CRITICAL: logging.critical('Alarm for temperature critical is detected') time.sleep(2) os.system('reboot') if ori_state==LEVEL_FAN_MAX: if new_state==LEVEL_TEMP_HIGH: if alarm_state==0: logging.warning('Alarm for temperature high is detected') alarm_state=1 if new_state==LEVEL_TEMP_CRITICAL: logging.critical('Alarm for temperature critical is detected') time.sleep(2) os.system('reboot') if alarm_state==1: if temp_get < (fan_policy[3][0] - 5000): #below 65 C, clear alarm logging.warning('Alarm for temperature high is cleared') alarm_state=0 if ori_state==LEVEL_TEMP_HIGH: if new_state==LEVEL_TEMP_CRITICAL: logging.critical('Alarm for temperature critical is detected') time.sleep(2) os.system('reboot') if new_state <= LEVEL_FAN_MID: logging.warning('Alarm for temperature high is cleared') alarm_state=0 if new_state <= LEVEL_FAN_MAX: if temp_get < (fan_policy[3][0] - 5000): #below 65 C, clear alarm logging.warning('Alarm for temperature high is cleared') alarm_state=0 if ori_state==LEVEL_TEMP_CRITICAL: if new_state <= LEVEL_FAN_MAX: logging.warning('Alarm for temperature critical is cleared') return True