Beispiel #1
0
class Monitor:
    def __init__(self):
        self._rate_detectors = defaultdict(RateDetector)

    def connect(self):
        self._client = LognplotTcpClient()
        self._client.connect()

    def update(self):
        for process_folder in os.listdir('/proc'):
            if re.match(r'\d+', process_folder):
                # print("Ow yeah!")
                timestamp = time.time()
                stat_filename = f'/proc/{process_folder}/stat'
                with open(stat_filename, 'r') as f:
                    line = f.read()
                # print(line)
                self.analyze_line(timestamp, line)
                # self.measure(timestamp, pid, filename, user_jiffies, kernel_jiffies)

    def analyze_line(self, timestamp, line):
        parts = line.split(' ')
        pid = int(parts[0])
        filename = parts[1]
        user_jiffies = int(parts[13])
        kernel_jiffies = int(parts[14])
        # print(pid, filename)
        self.measure(f'{filename}_{pid}_user', (timestamp, user_jiffies))
        self.measure(f'{filename}_{pid}_kernel', (timestamp, kernel_jiffies))
    
    def measure(self, signal_name, measurement):
        rate = self._rate_detectors[signal_name].update(measurement)
        # print(signal_name, rate)
        self._client.send_sample(signal_name, float(measurement[0]), float(rate))
Beispiel #2
0
def main():
    t = 0.0
    dt = 2.0
    client = LognplotTcpClient()
    client.connect()
    while True:
        print(f'Sending at t={t}')
        for x in range(20):
            client.send_sample(f"Trace_{x}", t, (t + x) % 36)
        time.sleep(dt)
        t += dt
Beispiel #3
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("csv_file")
    parser.add_argument("--delimiter",
                        default=",",
                        help="The value delimiter to use")
    parser.add_argument(
        "--skip-rows",
        default=0,
        type=int,
        help="Amount of rows to skip at the beginning of the file",
    )
    parser.add_argument(
        "--time-column",
        type=int,
        help=
        "The CSV column to use as time column. If not provided, the CSV row will be used as time.",
    )
    parser.add_argument("--lognplot-hostname", default="localhost", type=str)
    parser.add_argument("--lognplot-port", default="12345", type=int)
    args = parser.parse_args()
    print(args)

    lognplot_client = LognplotTcpClient(hostname=args.lognplot_hostname,
                                        port=args.lognplot_port)
    lognplot_client.connect()

    if hasattr(args, "time_column") and args.time_column is not None:
        time_column = int(args.time_column)
    else:
        time_column = None

    with open(args.csv_file, "r") as csv_file:
        reader = csv.reader(csv_file, delimiter=args.delimiter)
        for row_index, row in enumerate(reader):
            if row_index >= args.skip_rows:
                # print(row)

                timestamp = (float(row[time_column])
                             if time_column is not None else row_index)

                for column_index, column in enumerate(row):
                    name = f"csv_column_{column_index}"
                    lognplot_client.send_sample(name, timestamp, float(column))
Beispiel #4
0
class RosToLogNPlot:
    def __init__(self, lognplot_host, lognplot_port):
        self._subscriptions = {}
        self._lognplot_host = lognplot_host
        self._lognplot_port = lognplot_port

    def connect(self):
        try:
            self._client = LognplotTcpClient(hostname=self._lognplot_host,
                                             port=self._lognplot_port)
            self._client.connect()
        except ConnectionRefusedError:
            print("Error connecting to lognplot GUI!")
            self._client = None

    def is_connected(self):
        return bool(self._client)

    def run(self):
        self.node = rclpy.create_node("ros_to_lognplot")
        self.timer = self.node.create_timer(2.0, self._check_topics)
        self.node.create_subscription(Log, "/rosout", self.on_ros_out_msg, 0)
        rclpy.spin(self.node)
        rclpy.shutdown()

    def on_ros_out_msg(self, msg):
        signal_name = f'/rosout/{msg.name}'
        timestamp = time.time()
        text = msg.msg
        self.send_text(signal_name, timestamp, text)

    def _check_topics(self):
        """ Check which topics are present in the system, and subscribe to them all!
        """
        topics = self.node.get_topic_names_and_types()
        for topic_name, topic_type_name in topics:
            if not self.is_subscribed(topic_name):
                print("-", topic_name, "---", topic_type_name)
                topic_type = load_type(topic_type_name[0])
                self._subscribe_on_topic(topic_name, topic_type)

    def is_subscribed(self, topic_name):
        return topic_name in self._subscriptions

    def _subscribe_on_topic(self, topic_name, topic_type):
        assert topic_name not in self._subscriptions

        def handler(msg):
            timestamp = time.time()
            self.process_message(topic_name, topic_type, timestamp, msg)

        subscription = self.node.create_subscription(topic_type, topic_name,
                                                     handler,
                                                     qos_profile_sensor_data)
        self._subscriptions[topic_name] = subscription

    def process_message(self, topic_name, topic_type, timestamp, msg):
        """ Process an incoming ROS message.
        """
        self.process_value(topic_name, topic_type, timestamp, msg)

    def process_value(self, full_name, value_type, timestamp, value):
        if hasattr(value, "get_fields_and_field_types"):
            for field_name, field_type in value.get_fields_and_field_types(
            ).items():
                field_value = getattr(value, field_name)
                full_field_name = f"{full_name}.{field_name}"
                self.process_value(full_field_name, field_type, timestamp,
                                   field_value)
        else:
            if isinstance(value, (float, np.float32, int)):
                self.send_sample(full_name, timestamp, float(value))
            elif isinstance(value, (list, np.ndarray)):
                for element_index, element_value in enumerate(value):
                    element_name = f"{full_name}[{element_index}]"
                    element_type = None
                    self.process_value(element_name, element_type, timestamp,
                                       element_value)
            else:
                # Great panic! What now?
                # Ignore for now..
                pass

    def send_sample(self, signal_name: str, timestamp, value):
        """ Emit a single sample to the lognplot GUI. """
        if self._client:
            self._client.send_sample(signal_name, timestamp, value)

    def send_text(self, signal_name: str, timestamp, text):
        """ Emit a single text to the lognplot GUI. """
        if self._client:
            self._client.send_text(signal_name, timestamp, text)
Beispiel #5
0
def main():
    client = LognplotTcpClient()
    client.connect()
    topic = 'test'
    value = 1337.0
    client.send_sample(topic, 10.0, value)
Beispiel #6
0
# Connect to lognplot:
lognplot_client = LognplotTcpClient(
    hostname=args.lognplot_hostname, port=args.lognplot_port
)
lognplot_client.connect()

# Harvest data:

was_eos = False

while True:
    event = hawktracer_client.poll_event()

    if event:
        event_type, data = event
        if event_type == "HT_CallstackStringEvent":
            # Convert timestamp to second-ish units:
            timestamp = float(data["timestamp"]) / 1e9
            value = float(data["duration"])
            label = "hawk_tracer_{}".format(data["label"])
            lognplot_client.send_sample(label, timestamp, value)
        else:
            print("unhandled event", event_type, event)

    else:
        if was_eos:
            break
        was_eos = hawktracer_client.eos()
        time.sleep(0.01)