#!/usr/bin/env python3 import sys if sys.version_info[0] < 3: raise Exception("Must be using Python 3") import os import sys import time import socket from pathlib import Path from collections import defaultdict import http.server import socketserver import socket import asyncio import websockets from multiprocessing import Process, Manager import numpy as np from http import HTTPStatus from io import BytesIO from drawing import Readable, GeneratePIC from copy import deepcopy JSON_GLOBAL_DICTIONARY = {} def get_ip(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.settimeout(0) try: hostname = socket.gethostname() IPAddr = socket.gethostbyname(hostname) s.connect(({IPAddr}, 1)) except Exception: IPAddr = "127.0.0.1" finally: return IPAddr IPAddr = get_ip() PORT, WebSocketPort = 8000, 18000 SP = "\u00A0" def get_top_n(code): TOP_N = 10 top_n = sorted(deepcopy(code), key=lambda x: x[-1], reverse=True)[:TOP_N] return [ (line_num, hitc, 0, run_time) for _, _, _, _, line_num, _, hitc, run_time in top_n ] def wave_info(df, id): dic = { "Issue": df["issued_ins"][id], "Valu": df["valu_ins"][id], "Valu_stall": df["valu_stalls"][id], "Salu": df["salu_ins"][id], "Salu_stall": df["salu_stalls"][id], "Vmem": df["vmem_ins"][id], "Vmem_stall": df["vmem_stalls"][id], "Smem": df["smem_ins"][id], "Smem_stall": df["smem_stalls"][id], "Flat": df["flat_ins"][id], "Flat_stall": df["flat_stalls"][id], "Lds": df["lds_ins"][id], "Lds_stall": df["lds_stalls"][id], "Br": df["br_ins"][id], "Br_stall": df["br_stalls"][id], } dic["Issue_stall"] = int(np.sum([dic[key] for key in dic.keys() if "_STALL" in key])) return dic def extract_data(df, se_number): if len(df["id"]) == 0 or len(df["instructions"]) == 0 or len(df["timeline"]) == 0: return None wave_filenames = [] flight_count = [] wave_slot_count = [ {df["wave_slot"][wave_id]: 0 for wave_id in df["id"]} for k in range(4) ] print("Number of waves:", len(df["id"])) allwaves_maxline = 0 for wave_id in df["id"]: stitched, loopCount, mem_unroll, count, maxline, num_insts = df["instructions"][ wave_id ] timeline = df["timeline"][wave_id] if len(stitched) == 0 or len(timeline) == 0 or len(stitched) != num_insts: continue allwaves_maxline = max(allwaves_maxline, maxline) flight_count.append(count) wave_entry = { "id": int(df["id"][wave_id]), "simd": int(df["simd"][wave_id]), "slot": int(df["wave_slot"][wave_id]), "begin": int(df["begin_time"][wave_id]), "end": int(df["end_time"][wave_id]), "info": wave_info(df, wave_id), "instructions": stitched, "timeline": timeline, "waitcnt": mem_unroll, } data_obj = { "name": "SE".format(se_number), "duration": sum(dur for (_, dur) in timeline), "wave": wave_entry, "loop_count": loopCount, "top_n": [], "num_stitched": len(stitched), "num_insts": num_insts, "websocket_port": WebSocketPort, "generation_time": time.ctime(), } simd_id = df["simd"][wave_id] slot_id = df["wave_slot"][wave_id] slot_count = wave_slot_count[simd_id][slot_id] wave_slot_count[simd_id][slot_id] += 1 OUT = ( "se" + str(se_number) + "_sm" + str(simd_id) + "_sl" + str(slot_id) + "_wv" + str(slot_count) + ".json" ) JSON_GLOBAL_DICTIONARY[OUT] = Readable(data_obj) wave_filenames.append((OUT, df["begin_time"][wave_id], df["end_time"][wave_id])) data_obj = { "name": "SE".format(se_number), "websocket_port": WebSocketPort, "generation_time": time.ctime(), } se_filename = None if len(wave_filenames) > 0: se_filename = "se" + str(se_number) + "_info.json" JSON_GLOBAL_DICTIONARY[se_filename] = Readable(data_obj) return flight_count, wave_filenames, se_filename, allwaves_maxline class NoCacheHTTPRequestHandler(http.server.SimpleHTTPRequestHandler): def end_headers(self): self.send_my_headers() http.server.SimpleHTTPRequestHandler.end_headers(self) def send_my_headers(self): self.send_header("Cache-Control", "no-cache, no-store, must-revalidate") self.send_header("Pragma", "no-cache") self.send_header("Expires", "0") def do_GET(self): if ( ".png?" in self.path and self.path.split("/")[-1] not in JSON_GLOBAL_DICTIONARY.keys() ): selections = [int(s) != 0 for s in self.path.split(".png?")[-1]] counters_json, imagebytes = GeneratePIC( self.drawinfo, selections[1:], selections[0] ) JSON_GLOBAL_DICTIONARY["graph_options.json"] = counters_json JSON_GLOBAL_DICTIONARY[self.path.split("/")[-1]] = imagebytes[ self.path.split("/")[-1].split("?")[0] ] if ".json" in self.path or ".png" in self.path: try: response_file = JSON_GLOBAL_DICTIONARY[self.path.split("/")[-1]] except: print("Invalid json request:", self.path) print(JSON_GLOBAL_DICTIONARY.keys()) self.send_error(HTTPStatus.NOT_FOUND, "File not found") return self.send_response(HTTPStatus.OK) self.send_header("Content-Length", str(len(response_file))) if ".b" in self.path: self.send_header("Content-type", "application/octet-stream") response_file = BytesIO(response_file) elif "timeline.png" in self.path: self.send_header("Content-type", "image/png") else: self.send_header("Content-type", "application/json") self.send_header("Last-Modified", self.date_time_string(time.time())) self.end_headers() self.copyfile(response_file, self.wfile) elif self.path in ["/", "/styles.css", "/index.html", "/logo.svg"]: http.server.SimpleHTTPRequestHandler.do_GET(self) else: print("Invalid request:", self.path) self.send_error(HTTPStatus.NOT_FOUND, "File not found") class RocTCPServer(socketserver.TCPServer): def server_bind(self): self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.socket.bind(self.server_address) def run_server(drawinfo): Handler = NoCacheHTTPRequestHandler Handler.drawinfo = drawinfo os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)), "ui/")) # os.chdir('ui/') try: with RocTCPServer((IPAddr, PORT), Handler) as httpd: httpd.serve_forever() except KeyboardInterrupt: pass def fix_space(line): line = line.replace(" ", SP) line = line.replace("\t", SP * 4) return line def WebSocketserver(websocket, path): data = websocket.recv() cpp, ln, _ = data.split(":") ln = int(ln) HL, EMP = "highlight", "" content = None print("loading...") try: f = open(cpp, "r", errors="replace") content = "".join( '