def _validate_limits(self, **kwargs): """ Tells you if the values passed in exceed the maximums reported by the drive limits Must have already called connect on client to use Returns: {'success' : [True | False], 'response' : [(str) Error Response | None]} """ # Connect a temporary client temp_client = ShenziClient(self._client.serial_number, self._client.interface) result = temp_client.connect() if result != 0: return {'success':False,'response':'failed to connect client. status: '+str(result)} temp_client.close() limits = temp_client.sign_on_msg.body.getLog.limits for key, value in kwargs.iteritems(): try: if value != None and value > getattr(limits, key): return {'success':False,'response':"exceeded drive's reported limit for "+str(key)+\ ". drive limit="+str(getattr(limits, key))} except AttributeError: return {'success':False,'response':" failed to find a limit called "+str(key)} return {'success':True,'response':None}
def query_getlog(drives_of_interest): get_log_data = {} for drive in drives_of_interest: client = ShenziClient(drive) if client.connect(max_attempts=1) != 0: continue get_log_data[drive] = {} # configuration get_log_data[drive]['serial_number'] = client.sign_on_msg.body.getLog.configuration.serialNumber get_log_data[drive]['firmware_version'] = client.sign_on_msg.body.getLog.configuration.version get_log_data[drive]['model'] = client.sign_on_msg.body.getLog.configuration.model get_log_data[drive]['compilation_date'] = client.sign_on_msg.body.getLog.configuration.compilationDate get_log_data[drive]['source_hash'] = client.sign_on_msg.body.getLog.configuration.sourceHash get_log_data[drive]['protocol_version'] = client.sign_on_msg.body.getLog.configuration.protocolVersion get_log_data[drive]['protocol_compilation_date'] = client.sign_on_msg.body.getLog.configuration.protocolCompilationDate get_log_data[drive]['protocol_source_hash'] = client.sign_on_msg.body.getLog.configuration.protocolSourceHash get_log_data[drive]['current_power_level'] = client.sign_on_msg.body.getLog.configuration.currentPowerLevel # limits get_log_data[drive]['max_key_size'] = client.sign_on_msg.body.getLog.limits.maxKeySize get_log_data[drive]['max_value_size'] = client.sign_on_msg.body.getLog.limits.maxValueSize get_log_data[drive]['max_version_size'] = client.sign_on_msg.body.getLog.limits.maxVersionSize get_log_data[drive]['max_tag_size'] = client.sign_on_msg.body.getLog.limits.maxTagSize get_log_data[drive]['max_connections'] = client.sign_on_msg.body.getLog.limits.maxConnections get_log_data[drive]['max_outstanding_read_requests'] = client.sign_on_msg.body.getLog.limits.maxOutstandingReadRequests get_log_data[drive]['max_outstanding_write_requests'] = client.sign_on_msg.body.getLog.limits.maxOutstandingWriteRequests get_log_data[drive]['max_message_size'] = client.sign_on_msg.body.getLog.limits.maxMessageSize get_log_data[drive]['max_key_range_count'] = client.sign_on_msg.body.getLog.limits.maxKeyRangeCount get_log_data[drive]['max_identity_count'] = client.sign_on_msg.body.getLog.limits.maxIdentityCount get_log_data[drive]['max_pin_size'] = client.sign_on_msg.body.getLog.limits.maxPinSize get_log = client.blocking_cmd("get_log", types=[1,2,5,7], device='firmware_version') if get_log['success']: # messages get_log_data[drive]['messages'] = get_log['cmd'].body.getLog.messages # temperatures for item in get_log['cmd'].body.getLog.temperatures: if item.name == "HDA": get_log_data[drive]['hda_temp'] = str(item.current) elif item.name == "CPU": get_log_data[drive]['cpu_temp'] = str(item.current) # capacities get_log_data[drive]['nominal_capacity_in_bytes'] = get_log['cmd'].body.getLog.capacity.nominalCapacityInBytes get_log_data[drive]['portion_full'] = get_log['cmd'].body.getLog.capacity.portionFull # f3 version get_log_data[drive]['f3_version'] = get_log['value'] get_log = client.blocking_cmd("get_log", types=[7], device='uboot_version') if get_log['success']: # uboot version get_log_data[drive]['uboot_version'] = get_log['value'] return get_log_data
def __init__(self, serial_number, interface=DEFAULT_INTERFACE): self._client = ShenziClient(serial_number, interface=interface) self._header = HEADER self._timer_thread = Thread() self._reporting_thread = Thread() self._kill_switch = Event() self.is_running = Event() self._report_lock = Lock() self._initialize_values()
def __init__(self, identity, random_seed, kill_switch, serial_number_list): self._key_gen = KeyGen.create_keygen("fixed")(dict( {'random_seed': random_seed}, **KEY_SPECIFIER)) self._perf_data_lock = Lock() self._batch_group_lock = Lock() self._completed_batch_group = Event() self._current_batch_group = 0 self._success_response_count = 0 self._clients = [ShenziClient(drive) for drive in serial_number_list] self.id = str(identity) self.key_prefix = str(random_seed).rjust( self._key_gen.key_size, '\x00')[-self._key_gen.key_size:] self.kill_switch = kill_switch
class PerformanceTracker(object): def __init__(self, serial_number, interface=DEFAULT_INTERFACE): self._client = ShenziClient(serial_number, interface=interface) self._header = HEADER self._timer_thread = Thread() self._reporting_thread = Thread() self._kill_switch = Event() self.is_running = Event() self._report_lock = Lock() self._initialize_values() def set_header(self, new_header): self._header = new_header def start(self, reporting_interval): """ Initialize variables, print header, and start the reporting loop thread """ if self.is_running.isSet(): print cfg.StrFmt.Notice("performance tracker is already running") return self.is_running.set() self._kill_switch.clear() self._initialize_values(time.time()) self._timer_thread = Thread(target=self._report, args=(reporting_interval, ), name="Performance Tracker Timer Thread") self._timer_thread.start() def stop(self): """ Set the kill switch and wait for the reporting loop thread to finish """ self._kill_switch.set() if self._timer_thread.isAlive(): self._timer_thread.join(5) if self._reporting_thread.isAlive(): self._reporting_thread.join(5) self._client.close() print cfg.StrFmt.Notice("PerformanceTracker. final response count : " + str(self._resp_count)) self.is_running.clear() def log_op(self, latency=None, value=0): """ Used by the owning script to log latency and bytes transfered for a given message """ with self._report_lock: self._resp_count += 1 self._b1 += value if latency: self._lat_sum += latency self._lat_max = max(latency, self._lat_max) self._lat_min = min(latency, self._lat_min) self._lat_resp_count += 1 def _initialize_values(self, t0=0): # Helpful method for setup self._t0 = t0 self._b0 = self._b1 = 0 self._resp_count = 0 self._lat_resp_count = 0 self._lat_sum = 0 self._lat_min = '' self._lat_max = None if not self._client.is_connected: self._client.connect(max_attempts=1) def _report(self, reporting_interval): # Continuous loop that will wait (used instead of sleep so it's interruptible) for # the given reporting interval and then spawn a thread to report data print ','.join(self._header) while not self._kill_switch.isSet(): self._kill_switch.wait(reporting_interval) if self._reporting_thread.isAlive(): self._reporting_thread.join() self._reporting_thread = Thread( target=self._print_data, name="Performance Tracker Report Thread") self._reporting_thread.start() def _print_data(self): # Snapshot the current data and then report according to the header defined up top snapshot = self._snapshot_data() if not self._kill_switch.isSet(): output = [] for field in self._header: if field in snapshot: output.append(str(snapshot[field])) else: output.append('') print ','.join(output) def _snapshot_data(self): # Snapshot the necessary information, calculate the rest, and return a dictionary of values snapshot = {} # Wait for data lock to grab current data with self._report_lock: time_stamp = time.time() snapshot['bytes_xfer_total'] = self._b1 snapshot['resp_count_total'] = self._resp_count snapshot['latency_max'] = self._lat_max snapshot['latency_min'] = self._lat_min lat_resp_count = self._lat_resp_count lat_sum = self._lat_sum self._lat_resp_count = 0 self._lat_sum = 0 self._lat_max = None self._lat_min = '' if self._kill_switch.isSet(): return snapshot # Get getlog info from the drive snapshot.update(self._retrieve_getlog_data()) # Calculate / format other data if lat_resp_count != 0: snapshot['latency_ave'] = (lat_sum / lat_resp_count) if not snapshot['latency_max']: del snapshot['latency_max'] snapshot['bytes_xfer'] = snapshot['bytes_xfer_total'] - self._b0 snapshot['MB/s'] = (snapshot['bytes_xfer'] / (time_stamp - self._t0)) * .000001 snapshot['time_stamp'] = datetime.datetime.fromtimestamp( time_stamp).strftime('%Y/%m/%d %H:%M:%S') snapshot['ipv4_addr'] = self._client.hostname snapshot['serial_number'] = self._client.serial_number self._t0 = time_stamp self._b0 = snapshot['bytes_xfer_total'] return snapshot def _retrieve_getlog_data(self): if not self._client.is_connected: status = self._client.connect(max_attempts=1) if status != 0: print cfg.StrFmt.Notice('PerformanceTracker. [Errno ' + str(status) + '] failed to connect for getlog') return {} response = self._client.blocking_cmd('get_log', types=[1, 2], timeout=GET_LOG_TIMEOUT) if response['success']: results = { 'percent_full': ("%.2f" % float(response['cmd'].body.getLog.capacity.portionFull * 100)) } for temp in response['cmd'].body.getLog.temperatures: if temp.name == "HDA": results['hda_temp'] = str(temp.current) elif temp.name == "CPU": results['cpu_temp'] = str(temp.current) return results print cfg.StrFmt.Notice('PerformanceTracker. failed get_log:' + cfg.StrFmt.ProtoStatus(response['cmd'].status)) return {}
def __init__(self, serial_number, interface, script_name): super(StartWaitStopClientAPI, self).__init__(script_name) self.status = 1 self._client = ShenziClient(serial_number, interface, verbosity=2)
class StartWaitStopClientAPI(StartWaitStopAPI): def __init__(self, serial_number, interface, script_name): super(StartWaitStopClientAPI, self).__init__(script_name) self.status = 1 self._client = ShenziClient(serial_number, interface, verbosity=2) def start(self, verbose=True): """ Clear all events and start the main activity thread """ self.status = 2 if verbose: print cfg.StrFmt.Notice('starting '+self.script_name) self._client.status_dsm('starting '+self.script_name) super(StartWaitStopClientAPI, self).start() def stop(self, activity_call=False, last_call=True, verbose=True): """ Set the appropriate events, wait for main activity thread to finish, and close client """ super(StartWaitStopClientAPI, self).stop(activity_call=activity_call, last_call=False) if verbose: print cfg.StrFmt.Notice('stopping '+self.script_name) if self._client.is_connected: if verbose: print cfg.StrFmt.Notice('closing client (flush, wait, close)') self._client.flush_all_data() self._client.wait_q(outstanding=0) self._client.close() if self.status < 100: self.status = 0 if verbose: if activity_call: print cfg.StrFmt.Notice('internally stopped '+self.script_name) self._client.status_dsm('internally stopped '+self.script_name) else: print cfg.StrFmt.Notice('stopped '+self.script_name) self._client.status_dsm('stopped '+self.script_name) if last_call: self._stop_complete.set()