def __init__(self, tid): self._tid = tid self._request = GeNlMessage( TaskStatHelper.family_id, cmd=TASKSTATS_CMD_GET, attrs=[U32Attr(TASKSTATS_CMD_ATTR_PID, self._tid)], flags=NLM_F_REQUEST) self._stats_total = None self._stats_delta = Stats.build_all_zero() self.duration = None self._timestamp = time.time()
def update_task_stats(self): t0 = time.time() self.duration = t0 - self._timestamp self._timestamp = t0 self._request.send(TaskStatHelper.connection) try: reply = GeNlMessage.recv(TaskStatHelper.connection) except OSError as e: if e.errno == errno.ESRCH: # OSError: Netlink error: No such process (3) return raise for attr_type, attr_value in reply.attrs.items(): #if attr_type == TASKSTATS_TYPE_AGGR_TGID: if attr_type == TASKSTATS_TYPE_AGGR_PID: reply = attr_value.nested() break else: return taskstats_data = reply[TASKSTATS_TYPE_STATS].data if len(taskstats_data) < 272: # Short reply return taskstats_version = struct.unpack('H', taskstats_data[:2])[0] assert taskstats_version >= 4 self._update_stats(Stats(taskstats_data)) return self._stats_delta
class TaskCounter(object): def __init__(self, tid): self._tid = tid self._request = GeNlMessage( TaskStatHelper.family_id, cmd=TASKSTATS_CMD_GET, attrs=[U32Attr(TASKSTATS_CMD_ATTR_PID, self._tid)], flags=NLM_F_REQUEST) self._stats_total = None self._stats_delta = Stats.build_all_zero() self.duration = None self._timestamp = time.time() def _update_stats(self, stats): if not self._stats_total: self._stats_total = stats stats.delta(self._stats_total, self._stats_delta) self._stats_total = stats def update_task_stats(self): t0 = time.time() self.duration = t0 - self._timestamp self._timestamp = t0 self._request.send(TaskStatHelper.connection) try: reply = GeNlMessage.recv(TaskStatHelper.connection) except OSError as e: if e.errno == errno.ESRCH: # OSError: Netlink error: No such process (3) return raise for attr_type, attr_value in reply.attrs.items(): #if attr_type == TASKSTATS_TYPE_AGGR_TGID: if attr_type == TASKSTATS_TYPE_AGGR_PID: reply = attr_value.nested() break else: return taskstats_data = reply[TASKSTATS_TYPE_STATS].data if len(taskstats_data) < 272: # Short reply return taskstats_version = struct.unpack('H', taskstats_data[:2])[0] assert taskstats_version >= 4 self._update_stats(Stats(taskstats_data)) return self._stats_delta
def __init__(self, tid): self._tid = tid self._request = GeNlMessage(TaskStatHelper.family_id, cmd=TASKSTATS_CMD_GET, attrs=[U32Attr(TASKSTATS_CMD_ATTR_PID, self._tid)], flags=NLM_F_REQUEST) self._stats_total = None self._stats_delta = Stats.build_all_zero() self.duration = None self._timestamp = time.time()
class TaskCounter(object): def __init__(self, tid): self._tid = tid self._request = GeNlMessage(TaskStatHelper.family_id, cmd=TASKSTATS_CMD_GET, attrs=[U32Attr(TASKSTATS_CMD_ATTR_PID, self._tid)], flags=NLM_F_REQUEST) self._stats_total = None self._stats_delta = Stats.build_all_zero() self.duration = None self._timestamp = time.time() def _update_stats(self, stats): if not self._stats_total: self._stats_total = stats stats.delta(self._stats_total, self._stats_delta) self._stats_total = stats def update_task_stats(self): t0 = time.time() self.duration = t0 - self._timestamp self._timestamp = t0 self._request.send(TaskStatHelper.connection) try: reply = GeNlMessage.recv(TaskStatHelper.connection) except OSError as e: if e.errno == errno.ESRCH: # OSError: Netlink error: No such process (3) return raise for attr_type, attr_value in reply.attrs.items(): #if attr_type == TASKSTATS_TYPE_AGGR_TGID: if attr_type == TASKSTATS_TYPE_AGGR_PID: reply = attr_value.nested() break else: return taskstats_data = reply[TASKSTATS_TYPE_STATS].data if len(taskstats_data) < 272: # Short reply return taskstats_version = struct.unpack('H', taskstats_data[:2])[0] assert taskstats_version >= 4 self._update_stats(Stats(taskstats_data)) return self._stats_delta
def get_single_task_stats(self, thread): thread.task_stats_request.send(self.connection) try: reply = GeNlMessage.recv(self.connection) except OSError as e: if e.errno == errno.ESRCH: # OSError: Netlink error: No such process (3) return raise for attr_type, attr_value in reply.attrs.items(): if attr_type == TASKSTATS_TYPE_AGGR_PID: reply = attr_value.nested() break else: return taskstats_data = reply[TASKSTATS_TYPE_STATS].data if len(taskstats_data) < 272: # Short reply return taskstats_version = struct.unpack('H', taskstats_data[:2])[0] assert taskstats_version >= 4 return Stats(taskstats_data)
def build_request(self, tid): return GeNlMessage(self.family_id, cmd=TASKSTATS_CMD_GET, attrs=[U32Attr(TASKSTATS_CMD_ATTR_PID, tid)], flags=NLM_F_REQUEST)